message.go 16 KB

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