message.go 24 KB

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