message.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. package chat
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "slices"
  6. "strings"
  7. "time"
  8. "github.com/charmbracelet/lipgloss/v2"
  9. "github.com/charmbracelet/lipgloss/v2/compat"
  10. "github.com/charmbracelet/x/ansi"
  11. "github.com/muesli/reflow/truncate"
  12. "github.com/sst/opencode-sdk-go"
  13. "github.com/sst/opencode/internal/app"
  14. "github.com/sst/opencode/internal/components/diff"
  15. "github.com/sst/opencode/internal/styles"
  16. "github.com/sst/opencode/internal/theme"
  17. "github.com/sst/opencode/internal/util"
  18. "golang.org/x/text/cases"
  19. "golang.org/x/text/language"
  20. )
  21. type blockRenderer struct {
  22. textColor compat.AdaptiveColor
  23. border bool
  24. borderColor *compat.AdaptiveColor
  25. borderColorRight bool
  26. paddingTop int
  27. paddingBottom int
  28. paddingLeft int
  29. paddingRight int
  30. marginTop int
  31. marginBottom int
  32. }
  33. type renderingOption func(*blockRenderer)
  34. func WithTextColor(color compat.AdaptiveColor) renderingOption {
  35. return func(c *blockRenderer) {
  36. c.textColor = color
  37. }
  38. }
  39. func WithNoBorder() renderingOption {
  40. return func(c *blockRenderer) {
  41. c.border = false
  42. }
  43. }
  44. func WithBorderColor(color compat.AdaptiveColor) renderingOption {
  45. return func(c *blockRenderer) {
  46. c.borderColor = &color
  47. }
  48. }
  49. func WithBorderColorRight(color compat.AdaptiveColor) renderingOption {
  50. return func(c *blockRenderer) {
  51. c.borderColorRight = true
  52. c.borderColor = &color
  53. }
  54. }
  55. func WithMarginTop(padding int) renderingOption {
  56. return func(c *blockRenderer) {
  57. c.marginTop = padding
  58. }
  59. }
  60. func WithMarginBottom(padding int) renderingOption {
  61. return func(c *blockRenderer) {
  62. c.marginBottom = padding
  63. }
  64. }
  65. func WithPadding(padding int) renderingOption {
  66. return func(c *blockRenderer) {
  67. c.paddingTop = padding
  68. c.paddingBottom = padding
  69. c.paddingLeft = padding
  70. c.paddingRight = padding
  71. }
  72. }
  73. func WithPaddingLeft(padding int) renderingOption {
  74. return func(c *blockRenderer) {
  75. c.paddingLeft = padding
  76. }
  77. }
  78. func WithPaddingRight(padding int) renderingOption {
  79. return func(c *blockRenderer) {
  80. c.paddingRight = padding
  81. }
  82. }
  83. func WithPaddingTop(padding int) renderingOption {
  84. return func(c *blockRenderer) {
  85. c.paddingTop = padding
  86. }
  87. }
  88. func WithPaddingBottom(padding int) renderingOption {
  89. return func(c *blockRenderer) {
  90. c.paddingBottom = padding
  91. }
  92. }
  93. func renderContentBlock(
  94. app *app.App,
  95. content string,
  96. width int,
  97. options ...renderingOption,
  98. ) string {
  99. t := theme.CurrentTheme()
  100. renderer := &blockRenderer{
  101. textColor: t.TextMuted(),
  102. border: true,
  103. paddingTop: 1,
  104. paddingBottom: 1,
  105. paddingLeft: 2,
  106. paddingRight: 2,
  107. }
  108. for _, option := range options {
  109. option(renderer)
  110. }
  111. borderColor := t.BackgroundPanel()
  112. if renderer.borderColor != nil {
  113. borderColor = *renderer.borderColor
  114. }
  115. style := styles.NewStyle().
  116. Foreground(renderer.textColor).
  117. Background(t.BackgroundPanel()).
  118. PaddingTop(renderer.paddingTop).
  119. PaddingBottom(renderer.paddingBottom).
  120. PaddingLeft(renderer.paddingLeft).
  121. PaddingRight(renderer.paddingRight).
  122. AlignHorizontal(lipgloss.Left)
  123. if renderer.border {
  124. style = style.
  125. BorderStyle(lipgloss.ThickBorder()).
  126. BorderLeft(true).
  127. BorderRight(true).
  128. BorderLeftForeground(borderColor).
  129. BorderLeftBackground(t.Background()).
  130. BorderRightForeground(t.BackgroundPanel()).
  131. BorderRightBackground(t.Background())
  132. if renderer.borderColorRight {
  133. style = style.
  134. BorderLeftBackground(t.Background()).
  135. BorderLeftForeground(t.BackgroundPanel()).
  136. BorderRightForeground(borderColor).
  137. BorderRightBackground(t.Background())
  138. }
  139. }
  140. content = style.Render(content)
  141. if renderer.marginTop > 0 {
  142. for range renderer.marginTop {
  143. content = "\n" + content
  144. }
  145. }
  146. if renderer.marginBottom > 0 {
  147. for range renderer.marginBottom {
  148. content = content + "\n"
  149. }
  150. }
  151. return content
  152. }
  153. func renderText(
  154. app *app.App,
  155. message opencode.MessageUnion,
  156. text string,
  157. author string,
  158. showToolDetails bool,
  159. width int,
  160. extra string,
  161. fileParts []opencode.FilePart,
  162. toolCalls ...opencode.ToolPart,
  163. ) string {
  164. t := theme.CurrentTheme()
  165. var ts time.Time
  166. backgroundColor := t.BackgroundPanel()
  167. var content string
  168. switch casted := message.(type) {
  169. case opencode.AssistantMessage:
  170. ts = time.UnixMilli(int64(casted.Time.Created))
  171. content = util.ToMarkdown(text, width, backgroundColor)
  172. case opencode.UserMessage:
  173. ts = time.UnixMilli(int64(casted.Time.Created))
  174. base := styles.NewStyle().Foreground(t.Text()).Background(backgroundColor)
  175. text = ansi.WordwrapWc(text, width-6, " -")
  176. // Build list of attachment filenames for highlighting
  177. for _, filePart := range fileParts {
  178. atFilename := "@" + filePart.Filename
  179. // Find and highlight complete @filename references
  180. highlightStyle := base.Foreground(t.Secondary())
  181. text = strings.ReplaceAll(text, atFilename, highlightStyle.Render(atFilename))
  182. }
  183. content = base.Width(width - 6).Render(text)
  184. }
  185. timestamp := ts.
  186. Local().
  187. Format("02 Jan 2006 03:04 PM")
  188. if time.Now().Format("02 Jan 2006") == timestamp[:11] {
  189. // don't show the date if it's today
  190. timestamp = timestamp[12:]
  191. }
  192. info := fmt.Sprintf("%s (%s)", author, timestamp)
  193. info = styles.NewStyle().Foreground(t.TextMuted()).Render(info)
  194. if !showToolDetails && toolCalls != nil && len(toolCalls) > 0 {
  195. content = content + "\n\n"
  196. for _, toolCall := range toolCalls {
  197. title := renderToolTitle(toolCall, width)
  198. style := styles.NewStyle()
  199. if toolCall.State.Status == opencode.ToolPartStateStatusError {
  200. style = style.Foreground(t.Error())
  201. }
  202. title = style.Render(title)
  203. title = "∟ " + title + "\n"
  204. content = content + title
  205. }
  206. }
  207. sections := []string{content, info}
  208. if extra != "" {
  209. sections = append(sections, "\n"+extra)
  210. }
  211. content = strings.Join(sections, "\n")
  212. switch message.(type) {
  213. case opencode.UserMessage:
  214. return renderContentBlock(
  215. app,
  216. content,
  217. width,
  218. WithTextColor(t.Text()),
  219. WithBorderColorRight(t.Secondary()),
  220. )
  221. case opencode.AssistantMessage:
  222. return renderContentBlock(
  223. app,
  224. content,
  225. width,
  226. WithBorderColor(t.Accent()),
  227. )
  228. }
  229. return ""
  230. }
  231. func renderToolDetails(
  232. app *app.App,
  233. toolCall opencode.ToolPart,
  234. width int,
  235. ) string {
  236. measure := util.Measure("chat.renderToolDetails")
  237. defer measure("tool", toolCall.Tool)
  238. ignoredTools := []string{"todoread"}
  239. if slices.Contains(ignoredTools, toolCall.Tool) {
  240. return ""
  241. }
  242. if toolCall.State.Status == opencode.ToolPartStateStatusPending {
  243. title := renderToolTitle(toolCall, width)
  244. return renderContentBlock(app, title, width)
  245. }
  246. var result *string
  247. if toolCall.State.Output != "" {
  248. result = &toolCall.State.Output
  249. }
  250. toolInputMap := make(map[string]any)
  251. if toolCall.State.Input != nil {
  252. value := toolCall.State.Input
  253. if m, ok := value.(map[string]any); ok {
  254. toolInputMap = m
  255. keys := make([]string, 0, len(toolInputMap))
  256. for key := range toolInputMap {
  257. keys = append(keys, key)
  258. }
  259. slices.Sort(keys)
  260. }
  261. }
  262. body := ""
  263. t := theme.CurrentTheme()
  264. backgroundColor := t.BackgroundPanel()
  265. borderColor := t.BackgroundPanel()
  266. defaultStyle := styles.NewStyle().Background(backgroundColor).Width(width - 6).Render
  267. if toolCall.State.Metadata != nil {
  268. metadata := toolCall.State.Metadata.(map[string]any)
  269. switch toolCall.Tool {
  270. case "read":
  271. var preview any
  272. if metadata != nil {
  273. preview = metadata["preview"]
  274. }
  275. if preview != nil && toolInputMap["filePath"] != nil {
  276. filename := toolInputMap["filePath"].(string)
  277. body = preview.(string)
  278. body = util.RenderFile(filename, body, width, util.WithTruncate(6))
  279. }
  280. case "edit":
  281. if filename, ok := toolInputMap["filePath"].(string); ok {
  282. var diffField any
  283. if metadata != nil {
  284. diffField = metadata["diff"]
  285. }
  286. if diffField != nil {
  287. patch := diffField.(string)
  288. var formattedDiff string
  289. if width < 120 {
  290. formattedDiff, _ = diff.FormatUnifiedDiff(
  291. filename,
  292. patch,
  293. diff.WithWidth(width-2),
  294. )
  295. } else {
  296. formattedDiff, _ = diff.FormatDiff(
  297. filename,
  298. patch,
  299. diff.WithWidth(width-2),
  300. )
  301. }
  302. body = strings.TrimSpace(formattedDiff)
  303. style := styles.NewStyle().
  304. Background(backgroundColor).
  305. Foreground(t.TextMuted()).
  306. Padding(1, 2).
  307. Width(width - 4)
  308. if diagnostics := renderDiagnostics(metadata, filename, backgroundColor, width-6); diagnostics != "" {
  309. diagnostics = style.Render(diagnostics)
  310. body += "\n" + diagnostics
  311. }
  312. title := renderToolTitle(toolCall, width)
  313. title = style.Render(title)
  314. content := title + "\n" + body
  315. content = renderContentBlock(
  316. app,
  317. content,
  318. width,
  319. WithPadding(0),
  320. WithBorderColor(borderColor),
  321. )
  322. return content
  323. }
  324. }
  325. case "write":
  326. if filename, ok := toolInputMap["filePath"].(string); ok {
  327. if content, ok := toolInputMap["content"].(string); ok {
  328. body = util.RenderFile(filename, content, width)
  329. if diagnostics := renderDiagnostics(metadata, filename, backgroundColor, width-4); diagnostics != "" {
  330. body += "\n\n" + diagnostics
  331. }
  332. }
  333. }
  334. case "bash":
  335. command := toolInputMap["command"].(string)
  336. body = fmt.Sprintf("```console\n$ %s\n", command)
  337. stdout := metadata["stdout"]
  338. if stdout != nil {
  339. body += ansi.Strip(fmt.Sprintf("%s", stdout))
  340. }
  341. body += "```"
  342. body = util.ToMarkdown(body, width, backgroundColor)
  343. case "webfetch":
  344. if format, ok := toolInputMap["format"].(string); ok && result != nil {
  345. body = *result
  346. body = util.TruncateHeight(body, 10)
  347. if format == "html" || format == "markdown" {
  348. body = util.ToMarkdown(body, width, backgroundColor)
  349. }
  350. }
  351. case "todowrite":
  352. todos := metadata["todos"]
  353. if todos != nil {
  354. for _, item := range todos.([]any) {
  355. todo := item.(map[string]any)
  356. content := todo["content"].(string)
  357. switch todo["status"] {
  358. case "completed":
  359. body += fmt.Sprintf("- [x] %s\n", content)
  360. case "cancelled":
  361. // strike through cancelled todo
  362. body += fmt.Sprintf("- [ ] ~~%s~~\n", content)
  363. case "in_progress":
  364. // highlight in progress todo
  365. body += fmt.Sprintf("- [ ] `%s`\n", content)
  366. default:
  367. body += fmt.Sprintf("- [ ] %s\n", content)
  368. }
  369. }
  370. body = util.ToMarkdown(body, width, backgroundColor)
  371. }
  372. case "task":
  373. summary := metadata["summary"]
  374. if summary != nil {
  375. toolcalls := summary.([]any)
  376. steps := []string{}
  377. for _, item := range toolcalls {
  378. data, _ := json.Marshal(item)
  379. var toolCall opencode.ToolPart
  380. _ = json.Unmarshal(data, &toolCall)
  381. step := renderToolTitle(toolCall, width)
  382. step = "∟ " + step
  383. steps = append(steps, step)
  384. }
  385. body = strings.Join(steps, "\n")
  386. }
  387. body = defaultStyle(body)
  388. default:
  389. if result == nil {
  390. empty := ""
  391. result = &empty
  392. }
  393. body = *result
  394. body = util.TruncateHeight(body, 10)
  395. body = defaultStyle(body)
  396. }
  397. }
  398. error := ""
  399. if toolCall.State.Status == opencode.ToolPartStateStatusError {
  400. error = toolCall.State.Error
  401. }
  402. if error != "" {
  403. body = styles.NewStyle().
  404. Width(width - 6).
  405. Foreground(t.Error()).
  406. Background(backgroundColor).
  407. Render(error)
  408. }
  409. if body == "" && error == "" && result != nil {
  410. body = *result
  411. body = util.TruncateHeight(body, 10)
  412. body = defaultStyle(body)
  413. }
  414. if body == "" {
  415. body = defaultStyle("")
  416. }
  417. title := renderToolTitle(toolCall, width)
  418. content := title + "\n\n" + body
  419. return renderContentBlock(app, content, width, WithBorderColor(borderColor))
  420. }
  421. func renderToolName(name string) string {
  422. switch name {
  423. case "webfetch":
  424. return "Fetch"
  425. default:
  426. normalizedName := name
  427. if after, ok := strings.CutPrefix(name, "opencode_"); ok {
  428. normalizedName = after
  429. }
  430. return cases.Title(language.Und).String(normalizedName)
  431. }
  432. }
  433. func getTodoPhase(metadata map[string]any) string {
  434. todos, ok := metadata["todos"].([]any)
  435. if !ok || len(todos) == 0 {
  436. return "Plan"
  437. }
  438. counts := map[string]int{"pending": 0, "completed": 0}
  439. for _, item := range todos {
  440. if todo, ok := item.(map[string]any); ok {
  441. if status, ok := todo["status"].(string); ok {
  442. counts[status]++
  443. }
  444. }
  445. }
  446. total := len(todos)
  447. switch {
  448. case counts["pending"] == total:
  449. return "Creating plan"
  450. case counts["completed"] == total:
  451. return "Completing plan"
  452. default:
  453. return "Updating plan"
  454. }
  455. }
  456. func getTodoTitle(toolCall opencode.ToolPart) string {
  457. if toolCall.State.Status == opencode.ToolPartStateStatusCompleted {
  458. if metadata, ok := toolCall.State.Metadata.(map[string]any); ok {
  459. return getTodoPhase(metadata)
  460. }
  461. }
  462. return "Plan"
  463. }
  464. func renderToolTitle(
  465. toolCall opencode.ToolPart,
  466. width int,
  467. ) string {
  468. if toolCall.State.Status == opencode.ToolPartStateStatusPending {
  469. title := renderToolAction(toolCall.Tool)
  470. return styles.NewStyle().Width(width - 6).Render(title)
  471. }
  472. toolArgs := ""
  473. toolArgsMap := make(map[string]any)
  474. if toolCall.State.Input != nil {
  475. value := toolCall.State.Input
  476. if m, ok := value.(map[string]any); ok {
  477. toolArgsMap = m
  478. keys := make([]string, 0, len(toolArgsMap))
  479. for key := range toolArgsMap {
  480. keys = append(keys, key)
  481. }
  482. slices.Sort(keys)
  483. firstKey := ""
  484. if len(keys) > 0 {
  485. firstKey = keys[0]
  486. }
  487. toolArgs = renderArgs(&toolArgsMap, firstKey)
  488. }
  489. }
  490. title := renderToolName(toolCall.Tool)
  491. switch toolCall.Tool {
  492. case "read":
  493. toolArgs = renderArgs(&toolArgsMap, "filePath")
  494. title = fmt.Sprintf("%s %s", title, toolArgs)
  495. case "edit", "write":
  496. if filename, ok := toolArgsMap["filePath"].(string); ok {
  497. title = fmt.Sprintf("%s %s", title, util.Relative(filename))
  498. }
  499. case "bash":
  500. if description, ok := toolArgsMap["description"].(string); ok {
  501. title = fmt.Sprintf("%s %s", title, description)
  502. }
  503. case "task":
  504. description := toolArgsMap["description"]
  505. subagent := toolArgsMap["subagent_type"]
  506. if description != nil && subagent != nil {
  507. title = fmt.Sprintf("%s[%s] %s", title, subagent, description)
  508. } else if description != nil {
  509. title = fmt.Sprintf("%s %s", title, description)
  510. }
  511. case "webfetch":
  512. toolArgs = renderArgs(&toolArgsMap, "url")
  513. title = fmt.Sprintf("%s %s", title, toolArgs)
  514. case "todowrite":
  515. title = getTodoTitle(toolCall)
  516. case "todoread":
  517. return "Plan"
  518. default:
  519. toolName := renderToolName(toolCall.Tool)
  520. title = fmt.Sprintf("%s %s", toolName, toolArgs)
  521. }
  522. title = truncate.StringWithTail(title, uint(width-6), "...")
  523. return title
  524. }
  525. func renderToolAction(name string) string {
  526. switch name {
  527. case "task":
  528. return "Delegating..."
  529. case "bash":
  530. return "Writing command..."
  531. case "edit":
  532. return "Preparing edit..."
  533. case "webfetch":
  534. return "Fetching from the web..."
  535. case "glob":
  536. return "Finding files..."
  537. case "grep":
  538. return "Searching content..."
  539. case "list":
  540. return "Listing directory..."
  541. case "read":
  542. return "Reading file..."
  543. case "write":
  544. return "Preparing write..."
  545. case "todowrite", "todoread":
  546. return "Planning..."
  547. case "patch":
  548. return "Preparing patch..."
  549. }
  550. return "Working..."
  551. }
  552. func renderArgs(args *map[string]any, titleKey string) string {
  553. if args == nil || len(*args) == 0 {
  554. return ""
  555. }
  556. keys := make([]string, 0, len(*args))
  557. for key := range *args {
  558. keys = append(keys, key)
  559. }
  560. slices.Sort(keys)
  561. title := ""
  562. parts := []string{}
  563. for _, key := range keys {
  564. value := (*args)[key]
  565. if value == nil {
  566. continue
  567. }
  568. if key == "filePath" || key == "path" {
  569. value = util.Relative(value.(string))
  570. }
  571. if key == titleKey {
  572. title = fmt.Sprintf("%s", value)
  573. continue
  574. }
  575. parts = append(parts, fmt.Sprintf("%s=%v", key, value))
  576. }
  577. if len(parts) == 0 {
  578. return title
  579. }
  580. return fmt.Sprintf("%s (%s)", title, strings.Join(parts, ", "))
  581. }
  582. // Diagnostic represents an LSP diagnostic
  583. type Diagnostic struct {
  584. Range struct {
  585. Start struct {
  586. Line int `json:"line"`
  587. Character int `json:"character"`
  588. } `json:"start"`
  589. } `json:"range"`
  590. Severity int `json:"severity"`
  591. Message string `json:"message"`
  592. }
  593. // renderDiagnostics formats LSP diagnostics for display in the TUI
  594. func renderDiagnostics(
  595. metadata map[string]any,
  596. filePath string,
  597. backgroundColor compat.AdaptiveColor,
  598. width int,
  599. ) string {
  600. if diagnosticsData, ok := metadata["diagnostics"].(map[string]any); ok {
  601. if fileDiagnostics, ok := diagnosticsData[filePath].([]any); ok {
  602. var errorDiagnostics []string
  603. for _, diagInterface := range fileDiagnostics {
  604. diagMap, ok := diagInterface.(map[string]any)
  605. if !ok {
  606. continue
  607. }
  608. // Parse the diagnostic
  609. var diag Diagnostic
  610. diagBytes, err := json.Marshal(diagMap)
  611. if err != nil {
  612. continue
  613. }
  614. if err := json.Unmarshal(diagBytes, &diag); err != nil {
  615. continue
  616. }
  617. // Only show error diagnostics (severity === 1)
  618. if diag.Severity != 1 {
  619. continue
  620. }
  621. line := diag.Range.Start.Line + 1 // 1-based
  622. column := diag.Range.Start.Character + 1 // 1-based
  623. errorDiagnostics = append(
  624. errorDiagnostics,
  625. fmt.Sprintf("Error [%d:%d] %s", line, column, diag.Message),
  626. )
  627. }
  628. if len(errorDiagnostics) == 0 {
  629. return ""
  630. }
  631. t := theme.CurrentTheme()
  632. var result strings.Builder
  633. for _, diagnostic := range errorDiagnostics {
  634. if result.Len() > 0 {
  635. result.WriteString("\n\n")
  636. }
  637. diagnostic = ansi.WordwrapWc(diagnostic, width, " -")
  638. result.WriteString(
  639. styles.NewStyle().
  640. Background(backgroundColor).
  641. Foreground(t.Error()).
  642. Render(diagnostic),
  643. )
  644. }
  645. return result.String()
  646. }
  647. }
  648. return ""
  649. // diagnosticsData should be a map[string][]Diagnostic
  650. // strDiagnosticsData := diagnosticsData.Raw()
  651. // diagnosticsMap := gjson.Parse(strDiagnosticsData).Value().(map[string]any)
  652. // fileDiagnostics, ok := diagnosticsMap[filePath]
  653. // if !ok {
  654. // return ""
  655. // }
  656. // diagnosticsList, ok := fileDiagnostics.([]any)
  657. // if !ok {
  658. // return ""
  659. // }
  660. }