message.go 21 KB

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