message.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. package chat
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "maps"
  6. "slices"
  7. "strings"
  8. "time"
  9. "github.com/charmbracelet/lipgloss/v2"
  10. "github.com/charmbracelet/lipgloss/v2/compat"
  11. "github.com/charmbracelet/x/ansi"
  12. "github.com/muesli/reflow/truncate"
  13. "github.com/sst/opencode-sdk-go"
  14. "github.com/sst/opencode/internal/app"
  15. "github.com/sst/opencode/internal/components/diff"
  16. "github.com/sst/opencode/internal/styles"
  17. "github.com/sst/opencode/internal/theme"
  18. "github.com/sst/opencode/internal/util"
  19. "golang.org/x/text/cases"
  20. "golang.org/x/text/language"
  21. )
  22. type blockRenderer struct {
  23. textColor compat.AdaptiveColor
  24. backgroundColor compat.AdaptiveColor
  25. border bool
  26. borderColor *compat.AdaptiveColor
  27. borderLeft bool
  28. borderRight bool
  29. paddingTop int
  30. paddingBottom int
  31. paddingLeft int
  32. paddingRight int
  33. marginTop int
  34. marginBottom int
  35. }
  36. type renderingOption func(*blockRenderer)
  37. func WithTextColor(color compat.AdaptiveColor) renderingOption {
  38. return func(c *blockRenderer) {
  39. c.textColor = color
  40. }
  41. }
  42. func WithBackgroundColor(color compat.AdaptiveColor) renderingOption {
  43. return func(c *blockRenderer) {
  44. c.backgroundColor = color
  45. }
  46. }
  47. func WithNoBorder() renderingOption {
  48. return func(c *blockRenderer) {
  49. c.border = false
  50. }
  51. }
  52. func WithBorderColor(color compat.AdaptiveColor) renderingOption {
  53. return func(c *blockRenderer) {
  54. c.borderColor = &color
  55. }
  56. }
  57. func WithBorderLeft() renderingOption {
  58. return func(c *blockRenderer) {
  59. c.borderLeft = true
  60. c.borderRight = false
  61. }
  62. }
  63. func WithBorderRight() renderingOption {
  64. return func(c *blockRenderer) {
  65. c.borderLeft = false
  66. c.borderRight = true
  67. }
  68. }
  69. func WithBorderBoth(value bool) renderingOption {
  70. return func(c *blockRenderer) {
  71. if value {
  72. c.borderLeft = true
  73. c.borderRight = true
  74. }
  75. }
  76. }
  77. func WithMarginTop(padding int) renderingOption {
  78. return func(c *blockRenderer) {
  79. c.marginTop = padding
  80. }
  81. }
  82. func WithMarginBottom(padding int) renderingOption {
  83. return func(c *blockRenderer) {
  84. c.marginBottom = padding
  85. }
  86. }
  87. func WithPadding(padding int) renderingOption {
  88. return func(c *blockRenderer) {
  89. c.paddingTop = padding
  90. c.paddingBottom = padding
  91. c.paddingLeft = padding
  92. c.paddingRight = padding
  93. }
  94. }
  95. func WithPaddingLeft(padding int) renderingOption {
  96. return func(c *blockRenderer) {
  97. c.paddingLeft = padding
  98. }
  99. }
  100. func WithPaddingRight(padding int) renderingOption {
  101. return func(c *blockRenderer) {
  102. c.paddingRight = padding
  103. }
  104. }
  105. func WithPaddingTop(padding int) renderingOption {
  106. return func(c *blockRenderer) {
  107. c.paddingTop = padding
  108. }
  109. }
  110. func WithPaddingBottom(padding int) renderingOption {
  111. return func(c *blockRenderer) {
  112. c.paddingBottom = padding
  113. }
  114. }
  115. func renderContentBlock(
  116. app *app.App,
  117. content string,
  118. width int,
  119. options ...renderingOption,
  120. ) string {
  121. t := theme.CurrentTheme()
  122. renderer := &blockRenderer{
  123. textColor: t.TextMuted(),
  124. backgroundColor: t.BackgroundPanel(),
  125. border: true,
  126. borderLeft: true,
  127. borderRight: false,
  128. paddingTop: 1,
  129. paddingBottom: 1,
  130. paddingLeft: 2,
  131. paddingRight: 2,
  132. }
  133. for _, option := range options {
  134. option(renderer)
  135. }
  136. borderColor := t.BackgroundPanel()
  137. if renderer.borderColor != nil {
  138. borderColor = *renderer.borderColor
  139. }
  140. style := styles.NewStyle().
  141. Foreground(renderer.textColor).
  142. Background(renderer.backgroundColor).
  143. PaddingTop(renderer.paddingTop).
  144. PaddingBottom(renderer.paddingBottom).
  145. PaddingLeft(renderer.paddingLeft).
  146. PaddingRight(renderer.paddingRight).
  147. AlignHorizontal(lipgloss.Left)
  148. if renderer.border {
  149. style = style.
  150. BorderStyle(lipgloss.ThickBorder()).
  151. BorderLeft(true).
  152. BorderRight(true).
  153. BorderLeftForeground(t.BackgroundPanel()).
  154. BorderLeftBackground(t.Background()).
  155. BorderRightForeground(t.BackgroundPanel()).
  156. BorderRightBackground(t.Background())
  157. if renderer.borderLeft {
  158. style = style.BorderLeftForeground(borderColor)
  159. }
  160. if renderer.borderRight {
  161. style = style.BorderRightForeground(borderColor)
  162. }
  163. } else {
  164. style = style.PaddingLeft(renderer.paddingLeft + 1).PaddingRight(renderer.paddingRight + 1)
  165. }
  166. content = style.Render(content)
  167. if renderer.marginTop > 0 {
  168. for range renderer.marginTop {
  169. content = "\n" + content
  170. }
  171. }
  172. if renderer.marginBottom > 0 {
  173. for range renderer.marginBottom {
  174. content = content + "\n"
  175. }
  176. }
  177. return content
  178. }
  179. func renderText(
  180. app *app.App,
  181. message opencode.MessageUnion,
  182. text string,
  183. author string,
  184. showToolDetails bool,
  185. width int,
  186. extra string,
  187. isThinking bool,
  188. fileParts []opencode.FilePart,
  189. agentParts []opencode.AgentPart,
  190. toolCalls ...opencode.ToolPart,
  191. ) string {
  192. t := theme.CurrentTheme()
  193. var ts time.Time
  194. backgroundColor := t.BackgroundPanel()
  195. var content string
  196. switch casted := message.(type) {
  197. case opencode.AssistantMessage:
  198. backgroundColor = t.Background()
  199. if isThinking {
  200. backgroundColor = t.BackgroundPanel()
  201. }
  202. ts = time.UnixMilli(int64(casted.Time.Created))
  203. if casted.Time.Completed > 0 {
  204. ts = time.UnixMilli(int64(casted.Time.Completed))
  205. }
  206. content = util.ToMarkdown(text, width, backgroundColor)
  207. if isThinking {
  208. content = styles.NewStyle().Background(backgroundColor).Foreground(t.TextMuted()).Render("Thinking") + "\n\n" + content
  209. }
  210. case opencode.UserMessage:
  211. ts = time.UnixMilli(int64(casted.Time.Created))
  212. base := styles.NewStyle().Foreground(t.Text()).Background(backgroundColor)
  213. var result strings.Builder
  214. lastEnd := int64(0)
  215. // Apply highlighting to filenames and base style to rest of text BEFORE wrapping
  216. textLen := int64(len(text))
  217. // Collect all parts to highlight (both file and agent parts)
  218. type highlightPart struct {
  219. start int64
  220. end int64
  221. color compat.AdaptiveColor
  222. }
  223. var highlights []highlightPart
  224. // Add file parts with secondary color
  225. for _, filePart := range fileParts {
  226. highlights = append(highlights, highlightPart{
  227. start: filePart.Source.Text.Start,
  228. end: filePart.Source.Text.End,
  229. color: t.Secondary(),
  230. })
  231. }
  232. // Add agent parts with secondary color (same as file parts)
  233. for _, agentPart := range agentParts {
  234. highlights = append(highlights, highlightPart{
  235. start: agentPart.Source.Start,
  236. end: agentPart.Source.End,
  237. color: t.Secondary(),
  238. })
  239. }
  240. // Sort highlights by start position
  241. slices.SortFunc(highlights, func(a, b highlightPart) int {
  242. if a.start < b.start {
  243. return -1
  244. }
  245. if a.start > b.start {
  246. return 1
  247. }
  248. return 0
  249. })
  250. // Merge overlapping highlights to prevent duplication
  251. merged := make([]highlightPart, 0)
  252. for _, part := range highlights {
  253. if len(merged) == 0 {
  254. merged = append(merged, part)
  255. continue
  256. }
  257. last := &merged[len(merged)-1]
  258. // If current part overlaps with the last one, merge them
  259. if part.start <= last.end {
  260. if part.end > last.end {
  261. last.end = part.end
  262. }
  263. } else {
  264. merged = append(merged, part)
  265. }
  266. }
  267. for _, part := range merged {
  268. highlight := base.Foreground(part.color)
  269. start, end := part.start, part.end
  270. if end > textLen {
  271. end = textLen
  272. }
  273. if start > textLen {
  274. start = textLen
  275. }
  276. if start > lastEnd {
  277. result.WriteString(base.Render(text[lastEnd:start]))
  278. }
  279. if start < end {
  280. result.WriteString(highlight.Render(text[start:end]))
  281. }
  282. lastEnd = end
  283. }
  284. if lastEnd < textLen {
  285. result.WriteString(base.Render(text[lastEnd:]))
  286. }
  287. // wrap styled text
  288. styledText := result.String()
  289. styledText = strings.ReplaceAll(styledText, "-", "\u2011")
  290. wrappedText := ansi.WordwrapWc(styledText, width-6, " ")
  291. wrappedText = strings.ReplaceAll(wrappedText, "\u2011", "-")
  292. content = base.Width(width - 6).Render(wrappedText)
  293. }
  294. timestamp := ts.
  295. Local().
  296. Format("02 Jan 2006 03:04 PM")
  297. if time.Now().Format("02 Jan 2006") == timestamp[:11] {
  298. timestamp = timestamp[12:]
  299. }
  300. timestamp = styles.NewStyle().
  301. Background(backgroundColor).
  302. Foreground(t.TextMuted()).
  303. Render(" (" + timestamp + ")")
  304. // Check if this is an assistant message with agent information
  305. var modelAndAgentSuffix string
  306. if assistantMsg, ok := message.(opencode.AssistantMessage); ok && assistantMsg.Mode != "" {
  307. // Find the agent index by name to get the correct color
  308. var agentIndex int
  309. for i, agent := range app.Agents {
  310. if agent.Name == assistantMsg.Mode {
  311. agentIndex = i
  312. break
  313. }
  314. }
  315. // Get agent color based on the original agent index (same as status bar)
  316. agentColor := util.GetAgentColor(agentIndex)
  317. // Style the agent name with the same color as status bar
  318. agentName := cases.Title(language.Und).String(assistantMsg.Mode)
  319. styledAgentName := styles.NewStyle().
  320. Background(backgroundColor).
  321. Foreground(agentColor).
  322. Render(agentName + " ")
  323. styledModelID := styles.NewStyle().
  324. Background(backgroundColor).
  325. Foreground(t.TextMuted()).
  326. Render(assistantMsg.ModelID)
  327. modelAndAgentSuffix = styledAgentName + styledModelID
  328. }
  329. var info string
  330. if modelAndAgentSuffix != "" {
  331. info = modelAndAgentSuffix + timestamp
  332. } else {
  333. info = author + timestamp
  334. }
  335. if !showToolDetails && toolCalls != nil && len(toolCalls) > 0 {
  336. for _, toolCall := range toolCalls {
  337. title := renderToolTitle(toolCall, width-2)
  338. style := styles.NewStyle()
  339. if toolCall.State.Status == opencode.ToolPartStateStatusError {
  340. style = style.Foreground(t.Error())
  341. }
  342. title = style.Render(title)
  343. title = "\n∟ " + title
  344. content = content + title
  345. }
  346. }
  347. sections := []string{content}
  348. if extra != "" {
  349. sections = append(sections, "\n"+extra+"\n")
  350. }
  351. sections = append(sections, info)
  352. content = strings.Join(sections, "\n")
  353. switch message.(type) {
  354. case opencode.UserMessage:
  355. return renderContentBlock(
  356. app,
  357. content,
  358. width,
  359. WithTextColor(t.Text()),
  360. WithBorderColor(t.Secondary()),
  361. )
  362. case opencode.AssistantMessage:
  363. if isThinking {
  364. return renderContentBlock(
  365. app,
  366. content,
  367. width,
  368. WithTextColor(t.Text()),
  369. WithBackgroundColor(t.BackgroundPanel()),
  370. WithBorderColor(t.BackgroundPanel()),
  371. )
  372. }
  373. return renderContentBlock(
  374. app,
  375. content,
  376. width,
  377. WithNoBorder(),
  378. WithBackgroundColor(t.Background()),
  379. )
  380. }
  381. return ""
  382. }
  383. func renderToolDetails(
  384. app *app.App,
  385. toolCall opencode.ToolPart,
  386. permission opencode.Permission,
  387. width int,
  388. ) string {
  389. measure := util.Measure("chat.renderToolDetails")
  390. defer measure("tool", toolCall.Tool)
  391. ignoredTools := []string{"todoread"}
  392. if slices.Contains(ignoredTools, toolCall.Tool) {
  393. return ""
  394. }
  395. if toolCall.State.Status == opencode.ToolPartStateStatusPending {
  396. title := renderToolTitle(toolCall, width)
  397. return renderContentBlock(app, title, width)
  398. }
  399. var result *string
  400. if toolCall.State.Output != "" {
  401. result = &toolCall.State.Output
  402. }
  403. toolInputMap := make(map[string]any)
  404. if toolCall.State.Input != nil {
  405. value := toolCall.State.Input
  406. if m, ok := value.(map[string]any); ok {
  407. toolInputMap = m
  408. keys := make([]string, 0, len(toolInputMap))
  409. for key := range toolInputMap {
  410. keys = append(keys, key)
  411. }
  412. slices.Sort(keys)
  413. }
  414. }
  415. body := ""
  416. t := theme.CurrentTheme()
  417. backgroundColor := t.BackgroundPanel()
  418. borderColor := t.BackgroundPanel()
  419. defaultStyle := styles.NewStyle().Background(backgroundColor).Width(width - 6).Render
  420. permissionContent := ""
  421. if permission.ID != "" {
  422. borderColor = t.Warning()
  423. base := styles.NewStyle().Background(backgroundColor)
  424. text := base.Foreground(t.Text()).Bold(true).Render
  425. muted := base.Foreground(t.TextMuted()).Render
  426. permissionContent = "Permission required to run this tool:\n\n"
  427. permissionContent += text(
  428. "enter ",
  429. ) + muted(
  430. "accept ",
  431. ) + text(
  432. "a",
  433. ) + muted(
  434. " accept always ",
  435. ) + text(
  436. "esc",
  437. ) + muted(
  438. " reject",
  439. )
  440. }
  441. if permission.Metadata != nil {
  442. metadata, ok := toolCall.State.Metadata.(map[string]any)
  443. if metadata == nil || !ok {
  444. metadata = map[string]any{}
  445. }
  446. maps.Copy(metadata, permission.Metadata)
  447. toolCall.State.Metadata = metadata
  448. }
  449. if toolCall.State.Metadata != nil {
  450. metadata := toolCall.State.Metadata.(map[string]any)
  451. switch toolCall.Tool {
  452. case "read":
  453. var preview any
  454. if metadata != nil {
  455. preview = metadata["preview"]
  456. }
  457. if preview != nil && toolInputMap["filePath"] != nil {
  458. filename := toolInputMap["filePath"].(string)
  459. body = preview.(string)
  460. body = util.RenderFile(filename, body, width, util.WithTruncate(6))
  461. }
  462. case "edit":
  463. if filename, ok := toolInputMap["filePath"].(string); ok {
  464. var diffField any
  465. if metadata != nil {
  466. diffField = metadata["diff"]
  467. }
  468. if diffField != nil {
  469. patch := diffField.(string)
  470. var formattedDiff string
  471. if width < 120 {
  472. formattedDiff, _ = diff.FormatUnifiedDiff(
  473. filename,
  474. patch,
  475. diff.WithWidth(width-2),
  476. )
  477. } else {
  478. formattedDiff, _ = diff.FormatDiff(
  479. filename,
  480. patch,
  481. diff.WithWidth(width-2),
  482. )
  483. }
  484. body = strings.TrimSpace(formattedDiff)
  485. style := styles.NewStyle().
  486. Background(backgroundColor).
  487. Foreground(t.TextMuted()).
  488. Padding(1, 2).
  489. Width(width - 4)
  490. if diagnostics := renderDiagnostics(metadata, filename, backgroundColor, width-6); diagnostics != "" {
  491. diagnostics = style.Render(diagnostics)
  492. body += "\n" + diagnostics
  493. }
  494. title := renderToolTitle(toolCall, width)
  495. title = style.Render(title)
  496. content := title + "\n" + body
  497. if toolCall.State.Status == opencode.ToolPartStateStatusError {
  498. errorStyle := styles.NewStyle().
  499. Background(backgroundColor).
  500. Foreground(t.Error()).
  501. Padding(1, 2).
  502. Width(width - 4)
  503. errorContent := errorStyle.Render(toolCall.State.Error)
  504. content += "\n" + errorContent
  505. }
  506. if permissionContent != "" {
  507. permissionContent = styles.NewStyle().
  508. Background(backgroundColor).
  509. Padding(1, 2).
  510. Render(permissionContent)
  511. content += "\n" + permissionContent
  512. }
  513. content = renderContentBlock(
  514. app,
  515. content,
  516. width,
  517. WithPadding(0),
  518. WithBorderColor(borderColor),
  519. WithBorderBoth(permission.ID != ""),
  520. )
  521. return content
  522. }
  523. }
  524. case "write":
  525. if filename, ok := toolInputMap["filePath"].(string); ok {
  526. if content, ok := toolInputMap["content"].(string); ok {
  527. body = util.RenderFile(filename, content, width)
  528. if diagnostics := renderDiagnostics(metadata, filename, backgroundColor, width-4); diagnostics != "" {
  529. body += "\n\n" + diagnostics
  530. }
  531. }
  532. }
  533. case "bash":
  534. command := toolInputMap["command"].(string)
  535. body = fmt.Sprintf("```console\n$ %s\n", command)
  536. output := metadata["output"]
  537. if output != nil {
  538. body += ansi.Strip(fmt.Sprintf("%s", output))
  539. }
  540. body += "```"
  541. body = util.ToMarkdown(body, width, backgroundColor)
  542. case "webfetch":
  543. if format, ok := toolInputMap["format"].(string); ok && result != nil {
  544. body = *result
  545. body = util.TruncateHeight(body, 10)
  546. if format == "html" || format == "markdown" {
  547. body = util.ToMarkdown(body, width, backgroundColor)
  548. }
  549. }
  550. case "todowrite":
  551. todos := metadata["todos"]
  552. if todos != nil {
  553. for _, item := range todos.([]any) {
  554. todo := item.(map[string]any)
  555. content := todo["content"].(string)
  556. switch todo["status"] {
  557. case "completed":
  558. body += fmt.Sprintf("- [x] %s\n", content)
  559. case "cancelled":
  560. // strike through cancelled todo
  561. body += fmt.Sprintf("- [ ] ~~%s~~\n", content)
  562. case "in_progress":
  563. // highlight in progress todo
  564. body += fmt.Sprintf("- [ ] `%s`\n", content)
  565. default:
  566. body += fmt.Sprintf("- [ ] %s\n", content)
  567. }
  568. }
  569. body = util.ToMarkdown(body, width, backgroundColor)
  570. }
  571. case "task":
  572. summary := metadata["summary"]
  573. if summary != nil {
  574. toolcalls := summary.([]any)
  575. steps := []string{}
  576. for _, item := range toolcalls {
  577. data, _ := json.Marshal(item)
  578. var toolCall opencode.ToolPart
  579. _ = json.Unmarshal(data, &toolCall)
  580. step := renderToolTitle(toolCall, width-2)
  581. step = "∟ " + step
  582. steps = append(steps, step)
  583. }
  584. body = strings.Join(steps, "\n")
  585. }
  586. body = defaultStyle(body)
  587. default:
  588. if result == nil {
  589. empty := ""
  590. result = &empty
  591. }
  592. body = *result
  593. body = util.TruncateHeight(body, 10)
  594. body = defaultStyle(body)
  595. }
  596. }
  597. error := ""
  598. if toolCall.State.Status == opencode.ToolPartStateStatusError {
  599. error = toolCall.State.Error
  600. }
  601. if error != "" {
  602. errorContent := styles.NewStyle().
  603. Width(width - 6).
  604. Foreground(t.Error()).
  605. Background(backgroundColor).
  606. Render(error)
  607. if body == "" {
  608. body = errorContent
  609. } else {
  610. body += "\n\n" + errorContent
  611. }
  612. }
  613. if body == "" && error == "" && result != nil {
  614. body = *result
  615. body = util.TruncateHeight(body, 10)
  616. body = defaultStyle(body)
  617. }
  618. if body == "" {
  619. body = defaultStyle("")
  620. }
  621. title := renderToolTitle(toolCall, width)
  622. content := title + "\n\n" + body
  623. if permissionContent != "" {
  624. content += "\n\n\n" + permissionContent
  625. }
  626. return renderContentBlock(
  627. app,
  628. content,
  629. width,
  630. WithBorderColor(borderColor),
  631. WithBorderBoth(permission.ID != ""),
  632. )
  633. }
  634. func renderToolName(name string) string {
  635. switch name {
  636. case "bash":
  637. return "Shell"
  638. case "webfetch":
  639. return "Fetch"
  640. case "invalid":
  641. return "Invalid"
  642. default:
  643. normalizedName := name
  644. if after, ok := strings.CutPrefix(name, "opencode_"); ok {
  645. normalizedName = after
  646. }
  647. return cases.Title(language.Und).String(normalizedName)
  648. }
  649. }
  650. func getTodoPhase(metadata map[string]any) string {
  651. todos, ok := metadata["todos"].([]any)
  652. if !ok || len(todos) == 0 {
  653. return "Plan"
  654. }
  655. counts := map[string]int{"pending": 0, "completed": 0}
  656. for _, item := range todos {
  657. if todo, ok := item.(map[string]any); ok {
  658. if status, ok := todo["status"].(string); ok {
  659. counts[status]++
  660. }
  661. }
  662. }
  663. total := len(todos)
  664. switch {
  665. case counts["pending"] == total:
  666. return "Creating plan"
  667. case counts["completed"] == total:
  668. return "Completing plan"
  669. default:
  670. return "Updating plan"
  671. }
  672. }
  673. func getTodoTitle(toolCall opencode.ToolPart) string {
  674. if toolCall.State.Status == opencode.ToolPartStateStatusCompleted {
  675. if metadata, ok := toolCall.State.Metadata.(map[string]any); ok {
  676. return getTodoPhase(metadata)
  677. }
  678. }
  679. return "Plan"
  680. }
  681. func renderToolTitle(
  682. toolCall opencode.ToolPart,
  683. width int,
  684. ) string {
  685. if toolCall.State.Status == opencode.ToolPartStateStatusPending {
  686. title := renderToolAction(toolCall.Tool)
  687. return styles.NewStyle().Width(width - 6).Render(title)
  688. }
  689. toolArgs := ""
  690. toolArgsMap := make(map[string]any)
  691. if toolCall.State.Input != nil {
  692. value := toolCall.State.Input
  693. if m, ok := value.(map[string]any); ok {
  694. toolArgsMap = m
  695. keys := make([]string, 0, len(toolArgsMap))
  696. for key := range toolArgsMap {
  697. keys = append(keys, key)
  698. }
  699. slices.Sort(keys)
  700. firstKey := ""
  701. if len(keys) > 0 {
  702. firstKey = keys[0]
  703. }
  704. toolArgs = renderArgs(&toolArgsMap, firstKey)
  705. }
  706. }
  707. title := renderToolName(toolCall.Tool)
  708. switch toolCall.Tool {
  709. case "read":
  710. toolArgs = renderArgs(&toolArgsMap, "filePath")
  711. title = fmt.Sprintf("%s %s", title, toolArgs)
  712. case "edit", "write":
  713. if filename, ok := toolArgsMap["filePath"].(string); ok {
  714. title = fmt.Sprintf("%s %s", title, util.Relative(filename))
  715. }
  716. case "bash":
  717. if description, ok := toolArgsMap["description"].(string); ok {
  718. title = fmt.Sprintf("%s %s", title, description)
  719. }
  720. case "task":
  721. description := toolArgsMap["description"]
  722. subagent := toolArgsMap["subagent_type"]
  723. if description != nil && subagent != nil {
  724. title = fmt.Sprintf("%s[%s] %s", title, subagent, description)
  725. } else if description != nil {
  726. title = fmt.Sprintf("%s %s", title, description)
  727. }
  728. case "webfetch":
  729. toolArgs = renderArgs(&toolArgsMap, "url")
  730. title = fmt.Sprintf("%s %s", title, toolArgs)
  731. case "todowrite":
  732. title = getTodoTitle(toolCall)
  733. case "todoread":
  734. return "Plan"
  735. case "invalid":
  736. if actualTool, ok := toolArgsMap["tool"].(string); ok {
  737. title = renderToolName(actualTool)
  738. }
  739. default:
  740. toolName := renderToolName(toolCall.Tool)
  741. title = fmt.Sprintf("%s %s", toolName, toolArgs)
  742. }
  743. title = truncate.StringWithTail(title, uint(width-6), "...")
  744. if toolCall.State.Error != "" {
  745. t := theme.CurrentTheme()
  746. title = styles.NewStyle().Foreground(t.Error()).Render(title)
  747. }
  748. return title
  749. }
  750. func renderToolAction(name string) string {
  751. switch name {
  752. case "task":
  753. return "Delegating..."
  754. case "bash":
  755. return "Writing command..."
  756. case "edit":
  757. return "Preparing edit..."
  758. case "webfetch":
  759. return "Fetching from the web..."
  760. case "glob":
  761. return "Finding files..."
  762. case "grep":
  763. return "Searching content..."
  764. case "list":
  765. return "Listing directory..."
  766. case "read":
  767. return "Reading file..."
  768. case "write":
  769. return "Preparing write..."
  770. case "todowrite", "todoread":
  771. return "Planning..."
  772. case "patch":
  773. return "Preparing patch..."
  774. }
  775. return "Working..."
  776. }
  777. func renderArgs(args *map[string]any, titleKey string) string {
  778. if args == nil || len(*args) == 0 {
  779. return ""
  780. }
  781. keys := make([]string, 0, len(*args))
  782. for key := range *args {
  783. keys = append(keys, key)
  784. }
  785. slices.Sort(keys)
  786. title := ""
  787. parts := []string{}
  788. for _, key := range keys {
  789. value := (*args)[key]
  790. if value == nil {
  791. continue
  792. }
  793. if key == "filePath" || key == "path" {
  794. value = util.Relative(value.(string))
  795. }
  796. if key == titleKey {
  797. title = fmt.Sprintf("%s", value)
  798. continue
  799. }
  800. parts = append(parts, fmt.Sprintf("%s=%v", key, value))
  801. }
  802. if len(parts) == 0 {
  803. return title
  804. }
  805. return fmt.Sprintf("%s (%s)", title, strings.Join(parts, ", "))
  806. }
  807. // Diagnostic represents an LSP diagnostic
  808. type Diagnostic struct {
  809. Range struct {
  810. Start struct {
  811. Line int `json:"line"`
  812. Character int `json:"character"`
  813. } `json:"start"`
  814. } `json:"range"`
  815. Severity int `json:"severity"`
  816. Message string `json:"message"`
  817. }
  818. // renderDiagnostics formats LSP diagnostics for display in the TUI
  819. func renderDiagnostics(
  820. metadata map[string]any,
  821. filePath string,
  822. backgroundColor compat.AdaptiveColor,
  823. width int,
  824. ) string {
  825. if diagnosticsData, ok := metadata["diagnostics"].(map[string]any); ok {
  826. if fileDiagnostics, ok := diagnosticsData[filePath].([]any); ok {
  827. var errorDiagnostics []string
  828. for _, diagInterface := range fileDiagnostics {
  829. diagMap, ok := diagInterface.(map[string]any)
  830. if !ok {
  831. continue
  832. }
  833. // Parse the diagnostic
  834. var diag Diagnostic
  835. diagBytes, err := json.Marshal(diagMap)
  836. if err != nil {
  837. continue
  838. }
  839. if err := json.Unmarshal(diagBytes, &diag); err != nil {
  840. continue
  841. }
  842. // Only show error diagnostics (severity === 1)
  843. if diag.Severity != 1 {
  844. continue
  845. }
  846. line := diag.Range.Start.Line + 1 // 1-based
  847. column := diag.Range.Start.Character + 1 // 1-based
  848. errorDiagnostics = append(
  849. errorDiagnostics,
  850. fmt.Sprintf("Error [%d:%d] %s", line, column, diag.Message),
  851. )
  852. }
  853. if len(errorDiagnostics) == 0 {
  854. return ""
  855. }
  856. t := theme.CurrentTheme()
  857. var result strings.Builder
  858. for _, diagnostic := range errorDiagnostics {
  859. if result.Len() > 0 {
  860. result.WriteString("\n\n")
  861. }
  862. diagnostic = ansi.WordwrapWc(diagnostic, width, " -")
  863. result.WriteString(
  864. styles.NewStyle().
  865. Background(backgroundColor).
  866. Foreground(t.Error()).
  867. Render(diagnostic),
  868. )
  869. }
  870. return result.String()
  871. }
  872. }
  873. return ""
  874. // diagnosticsData should be a map[string][]Diagnostic
  875. // strDiagnosticsData := diagnosticsData.Raw()
  876. // diagnosticsMap := gjson.Parse(strDiagnosticsData).Value().(map[string]any)
  877. // fileDiagnostics, ok := diagnosticsMap[filePath]
  878. // if !ok {
  879. // return ""
  880. // }
  881. // diagnosticsList, ok := fileDiagnostics.([]any)
  882. // if !ok {
  883. // return ""
  884. // }
  885. }