message.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. package chat
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "path/filepath"
  7. "strings"
  8. "time"
  9. "github.com/charmbracelet/lipgloss"
  10. "github.com/charmbracelet/x/ansi"
  11. "github.com/opencode-ai/opencode/internal/config"
  12. "github.com/opencode-ai/opencode/internal/diff"
  13. "github.com/opencode-ai/opencode/internal/llm/agent"
  14. "github.com/opencode-ai/opencode/internal/llm/models"
  15. "github.com/opencode-ai/opencode/internal/llm/tools"
  16. "github.com/opencode-ai/opencode/internal/message"
  17. "github.com/opencode-ai/opencode/internal/tui/styles"
  18. "github.com/opencode-ai/opencode/internal/tui/theme"
  19. )
  20. type uiMessageType int
  21. const (
  22. userMessageType uiMessageType = iota
  23. assistantMessageType
  24. toolMessageType
  25. maxResultHeight = 10
  26. )
  27. type uiMessage struct {
  28. ID string
  29. messageType uiMessageType
  30. position int
  31. height int
  32. content string
  33. }
  34. func toMarkdown(content string, focused bool, width int) string {
  35. r := styles.GetMarkdownRenderer(width)
  36. rendered, _ := r.Render(content)
  37. return rendered
  38. }
  39. func renderMessage(msg string, isUser bool, isFocused bool, width int, info ...string) string {
  40. t := theme.CurrentTheme()
  41. style := styles.BaseStyle().
  42. Width(width - 1).
  43. BorderLeft(true).
  44. Foreground(t.TextMuted()).
  45. BorderForeground(t.Primary()).
  46. BorderStyle(lipgloss.ThickBorder())
  47. if isUser {
  48. style = style.BorderForeground(t.Secondary())
  49. }
  50. // Apply markdown formatting and handle background color
  51. parts := []string{
  52. styles.ForceReplaceBackgroundWithLipgloss(toMarkdown(msg, isFocused, width), t.Background()),
  53. }
  54. // Remove newline at the end
  55. parts[0] = strings.TrimSuffix(parts[0], "\n")
  56. if len(info) > 0 {
  57. parts = append(parts, info...)
  58. }
  59. rendered := style.Render(
  60. lipgloss.JoinVertical(
  61. lipgloss.Left,
  62. parts...,
  63. ),
  64. )
  65. return rendered
  66. }
  67. func renderUserMessage(msg message.Message, isFocused bool, width int, position int) uiMessage {
  68. var styledAttachments []string
  69. t := theme.CurrentTheme()
  70. baseStyle := styles.BaseStyle()
  71. attachmentStyles := baseStyle.
  72. MarginLeft(1).
  73. Background(t.TextMuted()).
  74. Foreground(t.Text())
  75. for _, attachment := range msg.BinaryContent() {
  76. file := filepath.Base(attachment.Path)
  77. var filename string
  78. if len(file) > 10 {
  79. filename = fmt.Sprintf(" %s %s...", styles.DocumentIcon, file[0:7])
  80. } else {
  81. filename = fmt.Sprintf(" %s %s", styles.DocumentIcon, file)
  82. }
  83. styledAttachments = append(styledAttachments, attachmentStyles.Render(filename))
  84. }
  85. // Add timestamp info
  86. info := []string{}
  87. timestamp := time.Unix(msg.CreatedAt, 0).Format("15:04:05")
  88. username, _ := config.GetUsername()
  89. info = append(info, baseStyle.
  90. Width(width-1).
  91. Foreground(t.TextMuted()).
  92. Render(fmt.Sprintf(" %s (%s)", username, timestamp)),
  93. )
  94. content := ""
  95. if len(styledAttachments) > 0 {
  96. attachmentContent := baseStyle.Width(width).Render(lipgloss.JoinHorizontal(lipgloss.Left, styledAttachments...))
  97. content = renderMessage(msg.Content().String(), true, isFocused, width, append(info, attachmentContent)...)
  98. } else {
  99. content = renderMessage(msg.Content().String(), true, isFocused, width, info...)
  100. }
  101. userMsg := uiMessage{
  102. ID: msg.ID,
  103. messageType: userMessageType,
  104. position: position,
  105. height: lipgloss.Height(content),
  106. content: content,
  107. }
  108. return userMsg
  109. }
  110. // Returns multiple uiMessages because of the tool calls
  111. func renderAssistantMessage(
  112. msg message.Message,
  113. msgIndex int,
  114. allMessages []message.Message, // we need this to get tool results and the user message
  115. messagesService message.Service, // We need this to get the task tool messages
  116. focusedUIMessageId string,
  117. width int,
  118. position int,
  119. showToolMessages bool,
  120. ) []uiMessage {
  121. messages := []uiMessage{}
  122. content := msg.Content().String()
  123. thinking := msg.IsThinking()
  124. thinkingContent := msg.ReasoningContent().Thinking
  125. finished := msg.IsFinished()
  126. finishData := msg.FinishPart()
  127. info := []string{}
  128. t := theme.CurrentTheme()
  129. baseStyle := styles.BaseStyle()
  130. // Always add timestamp info
  131. timestamp := time.Unix(msg.CreatedAt, 0).Format("15:04:05")
  132. modelName := "Assistant"
  133. if msg.Model != "" {
  134. modelName = models.SupportedModels[msg.Model].Name
  135. }
  136. info = append(info, baseStyle.
  137. Width(width-1).
  138. Foreground(t.TextMuted()).
  139. Render(fmt.Sprintf(" %s (%s)", modelName, timestamp)),
  140. )
  141. if finished {
  142. // Add finish info if available
  143. switch finishData.Reason {
  144. case message.FinishReasonCanceled:
  145. info = append(info, baseStyle.
  146. Width(width-1).
  147. Foreground(t.Warning()).
  148. Render("(canceled)"),
  149. )
  150. case message.FinishReasonError:
  151. info = append(info, baseStyle.
  152. Width(width-1).
  153. Foreground(t.Error()).
  154. Render("(error)"),
  155. )
  156. case message.FinishReasonPermissionDenied:
  157. info = append(info, baseStyle.
  158. Width(width-1).
  159. Foreground(t.Info()).
  160. Render("(permission denied)"),
  161. )
  162. }
  163. }
  164. if content != "" || (finished && finishData.Reason == message.FinishReasonEndTurn) {
  165. if content == "" {
  166. content = "*Finished without output*"
  167. }
  168. content = renderMessage(content, false, true, width, info...)
  169. messages = append(messages, uiMessage{
  170. ID: msg.ID,
  171. messageType: assistantMessageType,
  172. position: position,
  173. height: lipgloss.Height(content),
  174. content: content,
  175. })
  176. position += messages[0].height
  177. position++ // for the space
  178. } else if thinking && thinkingContent != "" {
  179. // Render the thinking content with timestamp
  180. content = renderMessage(thinkingContent, false, msg.ID == focusedUIMessageId, width, info...)
  181. messages = append(messages, uiMessage{
  182. ID: msg.ID,
  183. messageType: assistantMessageType,
  184. position: position,
  185. height: lipgloss.Height(content),
  186. content: content,
  187. })
  188. position += lipgloss.Height(content)
  189. position++ // for the space
  190. }
  191. // Only render tool messages if they should be shown
  192. if showToolMessages {
  193. for i, toolCall := range msg.ToolCalls() {
  194. toolCallContent := renderToolMessage(
  195. toolCall,
  196. allMessages,
  197. messagesService,
  198. focusedUIMessageId,
  199. false,
  200. width,
  201. i+1,
  202. )
  203. messages = append(messages, toolCallContent)
  204. position += toolCallContent.height
  205. position++ // for the space
  206. }
  207. }
  208. return messages
  209. }
  210. func findToolResponse(toolCallID string, futureMessages []message.Message) *message.ToolResult {
  211. for _, msg := range futureMessages {
  212. for _, result := range msg.ToolResults() {
  213. if result.ToolCallID == toolCallID {
  214. return &result
  215. }
  216. }
  217. }
  218. return nil
  219. }
  220. func toolName(name string) string {
  221. switch name {
  222. case agent.AgentToolName:
  223. return "Task"
  224. case tools.BashToolName:
  225. return "Bash"
  226. case tools.EditToolName:
  227. return "Edit"
  228. case tools.FetchToolName:
  229. return "Fetch"
  230. case tools.GlobToolName:
  231. return "Glob"
  232. case tools.GrepToolName:
  233. return "Grep"
  234. case tools.LSToolName:
  235. return "List"
  236. case tools.SourcegraphToolName:
  237. return "Sourcegraph"
  238. case tools.ViewToolName:
  239. return "View"
  240. case tools.WriteToolName:
  241. return "Write"
  242. case tools.PatchToolName:
  243. return "Patch"
  244. }
  245. return name
  246. }
  247. func getToolAction(name string) string {
  248. switch name {
  249. case agent.AgentToolName:
  250. return "Preparing prompt..."
  251. case tools.BashToolName:
  252. return "Building command..."
  253. case tools.EditToolName:
  254. return "Preparing edit..."
  255. case tools.FetchToolName:
  256. return "Writing fetch..."
  257. case tools.GlobToolName:
  258. return "Finding files..."
  259. case tools.GrepToolName:
  260. return "Searching content..."
  261. case tools.LSToolName:
  262. return "Listing directory..."
  263. case tools.SourcegraphToolName:
  264. return "Searching code..."
  265. case tools.ViewToolName:
  266. return "Reading file..."
  267. case tools.WriteToolName:
  268. return "Preparing write..."
  269. case tools.PatchToolName:
  270. return "Preparing patch..."
  271. }
  272. return "Working..."
  273. }
  274. // renders params, params[0] (params[1]=params[2] ....)
  275. func renderParams(paramsWidth int, params ...string) string {
  276. if len(params) == 0 {
  277. return ""
  278. }
  279. mainParam := params[0]
  280. if len(mainParam) > paramsWidth {
  281. mainParam = mainParam[:paramsWidth-3] + "..."
  282. }
  283. if len(params) == 1 {
  284. return mainParam
  285. }
  286. otherParams := params[1:]
  287. // create pairs of key/value
  288. // if odd number of params, the last one is a key without value
  289. if len(otherParams)%2 != 0 {
  290. otherParams = append(otherParams, "")
  291. }
  292. parts := make([]string, 0, len(otherParams)/2)
  293. for i := 0; i < len(otherParams); i += 2 {
  294. key := otherParams[i]
  295. value := otherParams[i+1]
  296. if value == "" {
  297. continue
  298. }
  299. parts = append(parts, fmt.Sprintf("%s=%s", key, value))
  300. }
  301. partsRendered := strings.Join(parts, ", ")
  302. remainingWidth := paramsWidth - lipgloss.Width(partsRendered) - 5 // for the space
  303. if remainingWidth < 30 {
  304. // No space for the params, just show the main
  305. return mainParam
  306. }
  307. if len(parts) > 0 {
  308. mainParam = fmt.Sprintf("%s (%s)", mainParam, strings.Join(parts, ", "))
  309. }
  310. return ansi.Truncate(mainParam, paramsWidth, "...")
  311. }
  312. func removeWorkingDirPrefix(path string) string {
  313. wd := config.WorkingDirectory()
  314. if strings.HasPrefix(path, wd) {
  315. path = strings.TrimPrefix(path, wd)
  316. }
  317. if strings.HasPrefix(path, "/") {
  318. path = strings.TrimPrefix(path, "/")
  319. }
  320. if strings.HasPrefix(path, "./") {
  321. path = strings.TrimPrefix(path, "./")
  322. }
  323. if strings.HasPrefix(path, "../") {
  324. path = strings.TrimPrefix(path, "../")
  325. }
  326. return path
  327. }
  328. func renderToolParams(paramWidth int, toolCall message.ToolCall) string {
  329. params := ""
  330. switch toolCall.Name {
  331. case agent.AgentToolName:
  332. var params agent.AgentParams
  333. json.Unmarshal([]byte(toolCall.Input), &params)
  334. prompt := strings.ReplaceAll(params.Prompt, "\n", " ")
  335. return renderParams(paramWidth, prompt)
  336. case tools.BashToolName:
  337. var params tools.BashParams
  338. json.Unmarshal([]byte(toolCall.Input), &params)
  339. command := strings.ReplaceAll(params.Command, "\n", " ")
  340. return renderParams(paramWidth, command)
  341. case tools.EditToolName:
  342. var params tools.EditParams
  343. json.Unmarshal([]byte(toolCall.Input), &params)
  344. filePath := removeWorkingDirPrefix(params.FilePath)
  345. return renderParams(paramWidth, filePath)
  346. case tools.FetchToolName:
  347. var params tools.FetchParams
  348. json.Unmarshal([]byte(toolCall.Input), &params)
  349. url := params.URL
  350. toolParams := []string{
  351. url,
  352. }
  353. if params.Format != "" {
  354. toolParams = append(toolParams, "format", params.Format)
  355. }
  356. if params.Timeout != 0 {
  357. toolParams = append(toolParams, "timeout", (time.Duration(params.Timeout) * time.Second).String())
  358. }
  359. return renderParams(paramWidth, toolParams...)
  360. case tools.GlobToolName:
  361. var params tools.GlobParams
  362. json.Unmarshal([]byte(toolCall.Input), &params)
  363. pattern := params.Pattern
  364. toolParams := []string{
  365. pattern,
  366. }
  367. if params.Path != "" {
  368. toolParams = append(toolParams, "path", params.Path)
  369. }
  370. return renderParams(paramWidth, toolParams...)
  371. case tools.GrepToolName:
  372. var params tools.GrepParams
  373. json.Unmarshal([]byte(toolCall.Input), &params)
  374. pattern := params.Pattern
  375. toolParams := []string{
  376. pattern,
  377. }
  378. if params.Path != "" {
  379. toolParams = append(toolParams, "path", params.Path)
  380. }
  381. if params.Include != "" {
  382. toolParams = append(toolParams, "include", params.Include)
  383. }
  384. if params.LiteralText {
  385. toolParams = append(toolParams, "literal", "true")
  386. }
  387. return renderParams(paramWidth, toolParams...)
  388. case tools.LSToolName:
  389. var params tools.LSParams
  390. json.Unmarshal([]byte(toolCall.Input), &params)
  391. path := params.Path
  392. if path == "" {
  393. path = "."
  394. }
  395. return renderParams(paramWidth, path)
  396. case tools.SourcegraphToolName:
  397. var params tools.SourcegraphParams
  398. json.Unmarshal([]byte(toolCall.Input), &params)
  399. return renderParams(paramWidth, params.Query)
  400. case tools.ViewToolName:
  401. var params tools.ViewParams
  402. json.Unmarshal([]byte(toolCall.Input), &params)
  403. filePath := removeWorkingDirPrefix(params.FilePath)
  404. toolParams := []string{
  405. filePath,
  406. }
  407. if params.Limit != 0 {
  408. toolParams = append(toolParams, "limit", fmt.Sprintf("%d", params.Limit))
  409. }
  410. if params.Offset != 0 {
  411. toolParams = append(toolParams, "offset", fmt.Sprintf("%d", params.Offset))
  412. }
  413. return renderParams(paramWidth, toolParams...)
  414. case tools.WriteToolName:
  415. var params tools.WriteParams
  416. json.Unmarshal([]byte(toolCall.Input), &params)
  417. filePath := removeWorkingDirPrefix(params.FilePath)
  418. return renderParams(paramWidth, filePath)
  419. default:
  420. input := strings.ReplaceAll(toolCall.Input, "\n", " ")
  421. params = renderParams(paramWidth, input)
  422. }
  423. return params
  424. }
  425. func truncateHeight(content string, height int) string {
  426. lines := strings.Split(content, "\n")
  427. if len(lines) > height {
  428. return strings.Join(lines[:height], "\n")
  429. }
  430. return content
  431. }
  432. func renderToolResponse(toolCall message.ToolCall, response message.ToolResult, width int) string {
  433. t := theme.CurrentTheme()
  434. baseStyle := styles.BaseStyle()
  435. if response.IsError {
  436. errContent := fmt.Sprintf("Error: %s", strings.ReplaceAll(response.Content, "\n", " "))
  437. errContent = ansi.Truncate(errContent, width-1, "...")
  438. return baseStyle.
  439. Width(width).
  440. Foreground(t.Error()).
  441. Render(errContent)
  442. }
  443. resultContent := truncateHeight(response.Content, maxResultHeight)
  444. switch toolCall.Name {
  445. case agent.AgentToolName:
  446. return styles.ForceReplaceBackgroundWithLipgloss(
  447. toMarkdown(response.Content, false, width),
  448. t.Background(),
  449. )
  450. case tools.BashToolName:
  451. resultContent = fmt.Sprintf("```bash\n%s\n```", resultContent)
  452. return styles.ForceReplaceBackgroundWithLipgloss(
  453. toMarkdown(resultContent, true, width),
  454. t.Background(),
  455. )
  456. case tools.EditToolName:
  457. metadata := tools.EditResponseMetadata{}
  458. json.Unmarshal([]byte(response.Metadata), &metadata)
  459. formattedDiff, _ := diff.FormatDiff(metadata.Diff, diff.WithTotalWidth(width))
  460. return formattedDiff
  461. case tools.FetchToolName:
  462. var params tools.FetchParams
  463. json.Unmarshal([]byte(toolCall.Input), &params)
  464. mdFormat := "markdown"
  465. switch params.Format {
  466. case "text":
  467. mdFormat = "text"
  468. case "html":
  469. mdFormat = "html"
  470. }
  471. resultContent = fmt.Sprintf("```%s\n%s\n```", mdFormat, resultContent)
  472. return styles.ForceReplaceBackgroundWithLipgloss(
  473. toMarkdown(resultContent, true, width),
  474. t.Background(),
  475. )
  476. case tools.GlobToolName:
  477. return baseStyle.Width(width).Foreground(t.TextMuted()).Render(resultContent)
  478. case tools.GrepToolName:
  479. return baseStyle.Width(width).Foreground(t.TextMuted()).Render(resultContent)
  480. case tools.LSToolName:
  481. return baseStyle.Width(width).Foreground(t.TextMuted()).Render(resultContent)
  482. case tools.SourcegraphToolName:
  483. return baseStyle.Width(width).Foreground(t.TextMuted()).Render(resultContent)
  484. case tools.ViewToolName:
  485. metadata := tools.ViewResponseMetadata{}
  486. json.Unmarshal([]byte(response.Metadata), &metadata)
  487. ext := filepath.Ext(metadata.FilePath)
  488. if ext == "" {
  489. ext = ""
  490. } else {
  491. ext = strings.ToLower(ext[1:])
  492. }
  493. resultContent = fmt.Sprintf("```%s\n%s\n```", ext, truncateHeight(metadata.Content, maxResultHeight))
  494. return styles.ForceReplaceBackgroundWithLipgloss(
  495. toMarkdown(resultContent, true, width),
  496. t.Background(),
  497. )
  498. case tools.WriteToolName:
  499. params := tools.WriteParams{}
  500. json.Unmarshal([]byte(toolCall.Input), &params)
  501. metadata := tools.WriteResponseMetadata{}
  502. json.Unmarshal([]byte(response.Metadata), &metadata)
  503. ext := filepath.Ext(params.FilePath)
  504. if ext == "" {
  505. ext = ""
  506. } else {
  507. ext = strings.ToLower(ext[1:])
  508. }
  509. resultContent = fmt.Sprintf("```%s\n%s\n```", ext, truncateHeight(params.Content, maxResultHeight))
  510. return styles.ForceReplaceBackgroundWithLipgloss(
  511. toMarkdown(resultContent, true, width),
  512. t.Background(),
  513. )
  514. default:
  515. resultContent = fmt.Sprintf("```text\n%s\n```", resultContent)
  516. return styles.ForceReplaceBackgroundWithLipgloss(
  517. toMarkdown(resultContent, true, width),
  518. t.Background(),
  519. )
  520. }
  521. }
  522. func renderToolMessage(
  523. toolCall message.ToolCall,
  524. allMessages []message.Message,
  525. messagesService message.Service,
  526. focusedUIMessageId string,
  527. nested bool,
  528. width int,
  529. position int,
  530. ) uiMessage {
  531. if nested {
  532. width = width - 3
  533. }
  534. t := theme.CurrentTheme()
  535. baseStyle := styles.BaseStyle()
  536. style := baseStyle.
  537. Width(width - 1).
  538. BorderLeft(true).
  539. BorderStyle(lipgloss.ThickBorder()).
  540. PaddingLeft(1).
  541. BorderForeground(t.TextMuted())
  542. response := findToolResponse(toolCall.ID, allMessages)
  543. toolNameText := baseStyle.Foreground(t.TextMuted()).
  544. Render(fmt.Sprintf("%s: ", toolName(toolCall.Name)))
  545. if !toolCall.Finished {
  546. // Get a brief description of what the tool is doing
  547. toolAction := getToolAction(toolCall.Name)
  548. progressText := baseStyle.
  549. Width(width - 2 - lipgloss.Width(toolNameText)).
  550. Foreground(t.TextMuted()).
  551. Render(fmt.Sprintf("%s", toolAction))
  552. content := style.Render(lipgloss.JoinHorizontal(lipgloss.Left, toolNameText, progressText))
  553. toolMsg := uiMessage{
  554. messageType: toolMessageType,
  555. position: position,
  556. height: lipgloss.Height(content),
  557. content: content,
  558. }
  559. return toolMsg
  560. }
  561. params := renderToolParams(width-2-lipgloss.Width(toolNameText), toolCall)
  562. responseContent := ""
  563. if response != nil {
  564. responseContent = renderToolResponse(toolCall, *response, width-2)
  565. responseContent = strings.TrimSuffix(responseContent, "\n")
  566. } else {
  567. responseContent = baseStyle.
  568. Italic(true).
  569. Width(width - 2).
  570. Foreground(t.TextMuted()).
  571. Render("Waiting for response...")
  572. }
  573. parts := []string{}
  574. if !nested {
  575. formattedParams := baseStyle.
  576. Width(width - 2 - lipgloss.Width(toolNameText)).
  577. Foreground(t.TextMuted()).
  578. Render(params)
  579. parts = append(parts, lipgloss.JoinHorizontal(lipgloss.Left, toolNameText, formattedParams))
  580. } else {
  581. prefix := baseStyle.
  582. Foreground(t.TextMuted()).
  583. Render(" └ ")
  584. formattedParams := baseStyle.
  585. Width(width - 2 - lipgloss.Width(toolNameText)).
  586. Foreground(t.TextMuted()).
  587. Render(params)
  588. parts = append(parts, lipgloss.JoinHorizontal(lipgloss.Left, prefix, toolNameText, formattedParams))
  589. }
  590. if toolCall.Name == agent.AgentToolName {
  591. taskMessages, _ := messagesService.List(context.Background(), toolCall.ID)
  592. toolCalls := []message.ToolCall{}
  593. for _, v := range taskMessages {
  594. toolCalls = append(toolCalls, v.ToolCalls()...)
  595. }
  596. for _, call := range toolCalls {
  597. rendered := renderToolMessage(call, []message.Message{}, messagesService, focusedUIMessageId, true, width, 0)
  598. parts = append(parts, rendered.content)
  599. }
  600. }
  601. if responseContent != "" && !nested {
  602. parts = append(parts, responseContent)
  603. }
  604. content := style.Render(
  605. lipgloss.JoinVertical(
  606. lipgloss.Left,
  607. parts...,
  608. ),
  609. )
  610. if nested {
  611. content = lipgloss.JoinVertical(
  612. lipgloss.Left,
  613. parts...,
  614. )
  615. }
  616. toolMsg := uiMessage{
  617. messageType: toolMessageType,
  618. position: position,
  619. height: lipgloss.Height(content),
  620. content: content,
  621. }
  622. return toolMsg
  623. }
  624. // Helper function to format the time difference between two Unix timestamps
  625. func formatTimestampDiff(start, end int64) string {
  626. diffSeconds := float64(end-start) / 1000.0 // Convert to seconds
  627. if diffSeconds < 1 {
  628. return fmt.Sprintf("%dms", int(diffSeconds*1000))
  629. }
  630. if diffSeconds < 60 {
  631. return fmt.Sprintf("%.1fs", diffSeconds)
  632. }
  633. return fmt.Sprintf("%.1fm", diffSeconds/60)
  634. }