message.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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/sst/opencode/internal/config"
  12. "github.com/sst/opencode/internal/diff"
  13. "github.com/sst/opencode/internal/llm/agent"
  14. "github.com/sst/opencode/internal/llm/models"
  15. "github.com/sst/opencode/internal/llm/tools"
  16. "github.com/sst/opencode/internal/message"
  17. "github.com/sst/opencode/internal/tui/styles"
  18. "github.com/sst/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 := msg.CreatedAt.Local().Format("02 Jan 2006 03:04 PM")
  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 := strings.TrimSpace(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 := msg.CreatedAt.Local().Format("02 Jan 2006 03:04 PM")
  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.ViewToolName:
  237. return "View"
  238. case tools.WriteToolName:
  239. return "Write"
  240. case tools.PatchToolName:
  241. return "Patch"
  242. case tools.BatchToolName:
  243. return "Batch"
  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.ViewToolName:
  264. return "Reading file..."
  265. case tools.WriteToolName:
  266. return "Preparing write..."
  267. case tools.PatchToolName:
  268. return "Preparing patch..."
  269. case tools.BatchToolName:
  270. return "Running batch operations..."
  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.ViewToolName:
  397. var params tools.ViewParams
  398. json.Unmarshal([]byte(toolCall.Input), &params)
  399. filePath := removeWorkingDirPrefix(params.FilePath)
  400. toolParams := []string{
  401. filePath,
  402. }
  403. if params.Limit != 0 {
  404. toolParams = append(toolParams, "limit", fmt.Sprintf("%d", params.Limit))
  405. }
  406. if params.Offset != 0 {
  407. toolParams = append(toolParams, "offset", fmt.Sprintf("%d", params.Offset))
  408. }
  409. return renderParams(paramWidth, toolParams...)
  410. case tools.WriteToolName:
  411. var params tools.WriteParams
  412. json.Unmarshal([]byte(toolCall.Input), &params)
  413. filePath := removeWorkingDirPrefix(params.FilePath)
  414. return renderParams(paramWidth, filePath)
  415. case tools.BatchToolName:
  416. var params tools.BatchParams
  417. json.Unmarshal([]byte(toolCall.Input), &params)
  418. return renderParams(paramWidth, fmt.Sprintf("%d parallel calls", len(params.Calls)))
  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(resultContent, 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.ViewToolName:
  483. metadata := tools.ViewResponseMetadata{}
  484. json.Unmarshal([]byte(response.Metadata), &metadata)
  485. ext := filepath.Ext(metadata.FilePath)
  486. if ext == "" {
  487. ext = ""
  488. } else {
  489. ext = strings.ToLower(ext[1:])
  490. }
  491. resultContent = fmt.Sprintf("```%s\n%s\n```", ext, truncateHeight(metadata.Content, maxResultHeight))
  492. return styles.ForceReplaceBackgroundWithLipgloss(
  493. toMarkdown(resultContent, true, width),
  494. t.Background(),
  495. )
  496. case tools.WriteToolName:
  497. params := tools.WriteParams{}
  498. json.Unmarshal([]byte(toolCall.Input), &params)
  499. metadata := tools.WriteResponseMetadata{}
  500. json.Unmarshal([]byte(response.Metadata), &metadata)
  501. ext := filepath.Ext(params.FilePath)
  502. if ext == "" {
  503. ext = ""
  504. } else {
  505. ext = strings.ToLower(ext[1:])
  506. }
  507. resultContent = fmt.Sprintf("```%s\n%s\n```", ext, truncateHeight(params.Content, maxResultHeight))
  508. return styles.ForceReplaceBackgroundWithLipgloss(
  509. toMarkdown(resultContent, true, width),
  510. t.Background(),
  511. )
  512. case tools.BatchToolName:
  513. var batchResult tools.BatchResult
  514. if err := json.Unmarshal([]byte(resultContent), &batchResult); err != nil {
  515. return baseStyle.Width(width).Foreground(t.Error()).Render(fmt.Sprintf("Error parsing batch result: %s", err))
  516. }
  517. var toolCalls []string
  518. for i, result := range batchResult.Results {
  519. toolName := toolName(result.ToolName)
  520. // Format the tool input as a string
  521. inputStr := string(result.ToolInput)
  522. // Format the result
  523. var resultStr string
  524. if result.Error != "" {
  525. resultStr = fmt.Sprintf("Error: %s", result.Error)
  526. } else {
  527. var toolResponse tools.ToolResponse
  528. if err := json.Unmarshal(result.Result, &toolResponse); err != nil {
  529. resultStr = "Error parsing tool response"
  530. } else {
  531. resultStr = truncateHeight(toolResponse.Content, 3)
  532. }
  533. }
  534. // Format the tool call
  535. toolCall := fmt.Sprintf("%d. %s: %s\n %s", i+1, toolName, inputStr, resultStr)
  536. toolCalls = append(toolCalls, toolCall)
  537. }
  538. return baseStyle.Width(width).Foreground(t.TextMuted()).Render(strings.Join(toolCalls, "\n\n"))
  539. default:
  540. resultContent = fmt.Sprintf("```text\n%s\n```", resultContent)
  541. return styles.ForceReplaceBackgroundWithLipgloss(
  542. toMarkdown(resultContent, true, width),
  543. t.Background(),
  544. )
  545. }
  546. }
  547. func renderToolMessage(
  548. toolCall message.ToolCall,
  549. allMessages []message.Message,
  550. messagesService message.Service,
  551. focusedUIMessageId string,
  552. nested bool,
  553. width int,
  554. position int,
  555. ) uiMessage {
  556. if nested {
  557. width = width - 3
  558. }
  559. t := theme.CurrentTheme()
  560. baseStyle := styles.BaseStyle()
  561. style := baseStyle.
  562. Width(width - 1).
  563. BorderLeft(true).
  564. BorderStyle(lipgloss.ThickBorder()).
  565. PaddingLeft(1).
  566. BorderForeground(t.TextMuted())
  567. response := findToolResponse(toolCall.ID, allMessages)
  568. toolNameText := baseStyle.Foreground(t.TextMuted()).
  569. Render(fmt.Sprintf("%s: ", toolName(toolCall.Name)))
  570. if !toolCall.Finished {
  571. // Get a brief description of what the tool is doing
  572. toolAction := getToolAction(toolCall.Name)
  573. progressText := baseStyle.
  574. Width(width - 2 - lipgloss.Width(toolNameText)).
  575. Foreground(t.TextMuted()).
  576. Render(fmt.Sprintf("%s", toolAction))
  577. content := style.Render(lipgloss.JoinHorizontal(lipgloss.Left, toolNameText, progressText))
  578. toolMsg := uiMessage{
  579. messageType: toolMessageType,
  580. position: position,
  581. height: lipgloss.Height(content),
  582. content: content,
  583. }
  584. return toolMsg
  585. }
  586. params := renderToolParams(width-1-lipgloss.Width(toolNameText), toolCall)
  587. responseContent := ""
  588. if response != nil {
  589. responseContent = renderToolResponse(toolCall, *response, width-2)
  590. responseContent = strings.TrimSuffix(responseContent, "\n")
  591. } else {
  592. responseContent = baseStyle.
  593. Italic(true).
  594. Width(width - 2).
  595. Foreground(t.TextMuted()).
  596. Render("Waiting for response...")
  597. }
  598. parts := []string{}
  599. if !nested {
  600. formattedParams := baseStyle.
  601. Width(width - 2 - lipgloss.Width(toolNameText)).
  602. Foreground(t.TextMuted()).
  603. Render(params)
  604. parts = append(parts, lipgloss.JoinHorizontal(lipgloss.Left, toolNameText, formattedParams))
  605. } else {
  606. prefix := baseStyle.
  607. Foreground(t.TextMuted()).
  608. Render(" └ ")
  609. formattedParams := baseStyle.
  610. Width(width - 2 - lipgloss.Width(toolNameText)).
  611. Foreground(t.TextMuted()).
  612. Render(params)
  613. parts = append(parts, lipgloss.JoinHorizontal(lipgloss.Left, prefix, toolNameText, formattedParams))
  614. }
  615. if toolCall.Name == agent.AgentToolName {
  616. taskMessages, _ := messagesService.List(context.Background(), toolCall.ID)
  617. toolCalls := []message.ToolCall{}
  618. for _, v := range taskMessages {
  619. toolCalls = append(toolCalls, v.ToolCalls()...)
  620. }
  621. for _, call := range toolCalls {
  622. rendered := renderToolMessage(call, []message.Message{}, messagesService, focusedUIMessageId, true, width, 0)
  623. parts = append(parts, rendered.content)
  624. }
  625. }
  626. if responseContent != "" && !nested {
  627. parts = append(parts, responseContent)
  628. }
  629. content := style.Render(
  630. lipgloss.JoinVertical(
  631. lipgloss.Left,
  632. parts...,
  633. ),
  634. )
  635. if nested {
  636. content = lipgloss.JoinVertical(
  637. lipgloss.Left,
  638. parts...,
  639. )
  640. }
  641. toolMsg := uiMessage{
  642. messageType: toolMessageType,
  643. position: position,
  644. height: lipgloss.Height(content),
  645. content: content,
  646. }
  647. return toolMsg
  648. }