message.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. package chat
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "path/filepath"
  6. "slices"
  7. "strings"
  8. "time"
  9. "unicode"
  10. "github.com/charmbracelet/lipgloss/v2"
  11. "github.com/charmbracelet/lipgloss/v2/compat"
  12. "github.com/charmbracelet/x/ansi"
  13. "github.com/sst/opencode/internal/app"
  14. "github.com/sst/opencode/internal/components/diff"
  15. "github.com/sst/opencode/internal/layout"
  16. "github.com/sst/opencode/internal/styles"
  17. "github.com/sst/opencode/internal/theme"
  18. "github.com/sst/opencode/pkg/client"
  19. "golang.org/x/text/cases"
  20. "golang.org/x/text/language"
  21. )
  22. func toMarkdown(content string, width int, backgroundColor compat.AdaptiveColor) string {
  23. r := styles.GetMarkdownRenderer(width, backgroundColor)
  24. content = strings.ReplaceAll(content, app.RootPath+"/", "")
  25. rendered, _ := r.Render(content)
  26. lines := strings.Split(rendered, "\n")
  27. if len(lines) > 0 {
  28. firstLine := lines[0]
  29. cleaned := ansi.Strip(firstLine)
  30. nospace := strings.ReplaceAll(cleaned, " ", "")
  31. if nospace == "" {
  32. lines = lines[1:]
  33. }
  34. if len(lines) > 0 {
  35. lastLine := lines[len(lines)-1]
  36. cleaned = ansi.Strip(lastLine)
  37. nospace = strings.ReplaceAll(cleaned, " ", "")
  38. if nospace == "" {
  39. lines = lines[:len(lines)-1]
  40. }
  41. }
  42. }
  43. content = strings.Join(lines, "\n")
  44. return strings.TrimSuffix(content, "\n")
  45. }
  46. type blockRenderer struct {
  47. align *lipgloss.Position
  48. borderColor *compat.AdaptiveColor
  49. fullWidth bool
  50. paddingTop int
  51. paddingBottom int
  52. paddingLeft int
  53. paddingRight int
  54. marginTop int
  55. marginBottom int
  56. }
  57. type renderingOption func(*blockRenderer)
  58. func WithFullWidth() renderingOption {
  59. return func(c *blockRenderer) {
  60. c.fullWidth = true
  61. }
  62. }
  63. func WithAlign(align lipgloss.Position) renderingOption {
  64. return func(c *blockRenderer) {
  65. c.align = &align
  66. }
  67. }
  68. func WithBorderColor(color compat.AdaptiveColor) renderingOption {
  69. return func(c *blockRenderer) {
  70. c.borderColor = &color
  71. }
  72. }
  73. func WithMarginTop(padding int) renderingOption {
  74. return func(c *blockRenderer) {
  75. c.marginTop = padding
  76. }
  77. }
  78. func WithMarginBottom(padding int) renderingOption {
  79. return func(c *blockRenderer) {
  80. c.marginBottom = padding
  81. }
  82. }
  83. func WithPaddingLeft(padding int) renderingOption {
  84. return func(c *blockRenderer) {
  85. c.paddingLeft = padding
  86. }
  87. }
  88. func WithPaddingRight(padding int) renderingOption {
  89. return func(c *blockRenderer) {
  90. c.paddingRight = padding
  91. }
  92. }
  93. func WithPaddingTop(padding int) renderingOption {
  94. return func(c *blockRenderer) {
  95. c.paddingTop = padding
  96. }
  97. }
  98. func WithPaddingBottom(padding int) renderingOption {
  99. return func(c *blockRenderer) {
  100. c.paddingBottom = padding
  101. }
  102. }
  103. func renderContentBlock(content string, options ...renderingOption) string {
  104. t := theme.CurrentTheme()
  105. renderer := &blockRenderer{
  106. fullWidth: false,
  107. paddingTop: 1,
  108. paddingBottom: 1,
  109. paddingLeft: 2,
  110. paddingRight: 2,
  111. }
  112. for _, option := range options {
  113. option(renderer)
  114. }
  115. style := styles.NewStyle().Foreground(t.TextMuted()).Background(t.BackgroundPanel()).
  116. // MarginTop(renderer.marginTop).
  117. // MarginBottom(renderer.marginBottom).
  118. PaddingTop(renderer.paddingTop).
  119. PaddingBottom(renderer.paddingBottom).
  120. PaddingLeft(renderer.paddingLeft).
  121. PaddingRight(renderer.paddingRight).
  122. BorderStyle(lipgloss.ThickBorder())
  123. align := lipgloss.Left
  124. if renderer.align != nil {
  125. align = *renderer.align
  126. }
  127. borderColor := t.BackgroundPanel()
  128. if renderer.borderColor != nil {
  129. borderColor = *renderer.borderColor
  130. }
  131. switch align {
  132. case lipgloss.Left:
  133. style = style.
  134. BorderLeft(true).
  135. BorderRight(true).
  136. AlignHorizontal(align).
  137. BorderLeftForeground(borderColor).
  138. BorderLeftBackground(t.Background()).
  139. BorderRightForeground(t.BackgroundPanel()).
  140. BorderRightBackground(t.Background())
  141. case lipgloss.Right:
  142. style = style.
  143. BorderRight(true).
  144. BorderLeft(true).
  145. AlignHorizontal(align).
  146. BorderRightForeground(borderColor).
  147. BorderRightBackground(t.Background()).
  148. BorderLeftForeground(t.BackgroundPanel()).
  149. BorderLeftBackground(t.Background())
  150. }
  151. if renderer.fullWidth {
  152. style = style.Width(layout.Current.Container.Width)
  153. }
  154. content = style.Render(content)
  155. content = lipgloss.PlaceHorizontal(
  156. layout.Current.Container.Width,
  157. align,
  158. content,
  159. styles.WhitespaceStyle(t.Background()),
  160. )
  161. content = lipgloss.PlaceHorizontal(
  162. layout.Current.Viewport.Width,
  163. lipgloss.Center,
  164. content,
  165. styles.WhitespaceStyle(t.Background()),
  166. )
  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 calculatePadding() int {
  180. if layout.Current.Viewport.Width < 80 {
  181. return 5
  182. } else if layout.Current.Viewport.Width < 120 {
  183. return 15
  184. } else {
  185. return 20
  186. }
  187. }
  188. func renderText(message client.MessageInfo, text string, author string) string {
  189. t := theme.CurrentTheme()
  190. width := layout.Current.Container.Width
  191. padding := calculatePadding()
  192. timestamp := time.UnixMilli(int64(message.Metadata.Time.Created)).Local().Format("02 Jan 2006 03:04 PM")
  193. if time.Now().Format("02 Jan 2006") == timestamp[:11] {
  194. // don't show the date if it's today
  195. timestamp = timestamp[12:]
  196. }
  197. info := fmt.Sprintf("%s (%s)", author, timestamp)
  198. textWidth := max(lipgloss.Width(text), lipgloss.Width(info))
  199. markdownWidth := min(textWidth, width-padding-4) // -4 for the border and padding
  200. if message.Role == client.Assistant {
  201. markdownWidth = width - padding - 4 - 3
  202. }
  203. if message.Role == client.User {
  204. text = strings.ReplaceAll(text, "<", "\\<")
  205. text = strings.ReplaceAll(text, ">", "\\>")
  206. }
  207. content := toMarkdown(text, markdownWidth, t.BackgroundPanel())
  208. content = strings.Join([]string{content, info}, "\n")
  209. switch message.Role {
  210. case client.User:
  211. return renderContentBlock(content,
  212. WithAlign(lipgloss.Right),
  213. WithBorderColor(t.Secondary()),
  214. )
  215. case client.Assistant:
  216. return renderContentBlock(content,
  217. WithAlign(lipgloss.Left),
  218. WithBorderColor(t.Accent()),
  219. )
  220. }
  221. return ""
  222. }
  223. func renderToolInvocation(
  224. toolCall client.MessageToolInvocationToolCall,
  225. result *string,
  226. metadata client.MessageMetadata_Tool_AdditionalProperties,
  227. showDetails bool,
  228. isLast bool,
  229. contentOnly bool,
  230. ) string {
  231. ignoredTools := []string{"todoread"}
  232. if slices.Contains(ignoredTools, toolCall.ToolName) {
  233. return ""
  234. }
  235. outerWidth := layout.Current.Container.Width
  236. innerWidth := outerWidth - 6
  237. paddingTop := 0
  238. paddingBottom := 0
  239. if showDetails {
  240. paddingTop = 1
  241. if result == nil || *result == "" {
  242. paddingBottom = 1
  243. }
  244. }
  245. t := theme.CurrentTheme()
  246. style := styles.NewStyle().
  247. Foreground(t.TextMuted()).
  248. Background(t.BackgroundPanel()).
  249. Width(outerWidth).
  250. PaddingTop(paddingTop).
  251. PaddingBottom(paddingBottom).
  252. PaddingLeft(2).
  253. PaddingRight(2).
  254. BorderLeft(true).
  255. BorderRight(true).
  256. BorderBackground(t.Background()).
  257. BorderForeground(t.BackgroundPanel()).
  258. BorderStyle(lipgloss.ThickBorder())
  259. if toolCall.State == "partial-call" {
  260. title := renderToolAction(toolCall.ToolName)
  261. if !showDetails {
  262. title = "∟ " + title
  263. padding := calculatePadding()
  264. style := styles.NewStyle().
  265. Background(t.BackgroundPanel()).
  266. Width(outerWidth - padding - 4 - 3)
  267. return renderContentBlock(style.Render(title),
  268. WithAlign(lipgloss.Left),
  269. WithBorderColor(t.Accent()),
  270. WithPaddingTop(0),
  271. WithPaddingBottom(1),
  272. )
  273. }
  274. style = style.Foreground(t.TextMuted())
  275. return style.Render(title)
  276. }
  277. toolArgs := ""
  278. toolArgsMap := make(map[string]any)
  279. if toolCall.Args != nil {
  280. value := *toolCall.Args
  281. if m, ok := value.(map[string]any); ok {
  282. toolArgsMap = m
  283. keys := make([]string, 0, len(toolArgsMap))
  284. for key := range toolArgsMap {
  285. keys = append(keys, key)
  286. }
  287. slices.Sort(keys)
  288. firstKey := ""
  289. if len(keys) > 0 {
  290. firstKey = keys[0]
  291. }
  292. toolArgs = renderArgs(&toolArgsMap, firstKey)
  293. }
  294. }
  295. body := ""
  296. error := ""
  297. finished := result != nil && *result != ""
  298. if e, ok := metadata.Get("error"); ok && e.(bool) == true {
  299. if m, ok := metadata.Get("message"); ok {
  300. style = style.BorderLeftForeground(t.Error())
  301. error = styles.NewStyle().
  302. Foreground(t.Error()).
  303. Background(t.BackgroundPanel()).
  304. Render(m.(string))
  305. error = renderContentBlock(
  306. error,
  307. WithFullWidth(),
  308. WithBorderColor(t.Error()),
  309. WithMarginBottom(1),
  310. )
  311. }
  312. }
  313. title := ""
  314. switch toolCall.ToolName {
  315. case "read":
  316. toolArgs = renderArgs(&toolArgsMap, "filePath")
  317. title = fmt.Sprintf("READ %s", toolArgs)
  318. if preview, ok := metadata.Get("preview"); ok && toolArgsMap["filePath"] != nil {
  319. filename := toolArgsMap["filePath"].(string)
  320. body = preview.(string)
  321. body = renderFile(filename, body, WithTruncate(6))
  322. }
  323. case "edit":
  324. if filename, ok := toolArgsMap["filePath"].(string); ok {
  325. title = fmt.Sprintf("EDIT %s", relative(filename))
  326. if d, ok := metadata.Get("diff"); ok {
  327. patch := d.(string)
  328. var formattedDiff string
  329. if layout.Current.Viewport.Width < 80 {
  330. formattedDiff, _ = diff.FormatUnifiedDiff(
  331. filename,
  332. patch,
  333. diff.WithWidth(layout.Current.Container.Width-2),
  334. )
  335. } else {
  336. diffWidth := min(layout.Current.Viewport.Width-2, 120)
  337. formattedDiff, _ = diff.FormatDiff(filename, patch, diff.WithTotalWidth(diffWidth))
  338. }
  339. formattedDiff = strings.TrimSpace(formattedDiff)
  340. formattedDiff = styles.NewStyle().
  341. BorderStyle(lipgloss.ThickBorder()).
  342. BorderBackground(t.Background()).
  343. BorderForeground(t.BackgroundPanel()).
  344. BorderLeft(true).
  345. BorderRight(true).
  346. Render(formattedDiff)
  347. if showDetails {
  348. style = style.Width(lipgloss.Width(formattedDiff))
  349. title += "\n"
  350. }
  351. body = strings.TrimSpace(formattedDiff)
  352. body = lipgloss.Place(
  353. layout.Current.Viewport.Width,
  354. lipgloss.Height(body)+1,
  355. lipgloss.Center,
  356. lipgloss.Top,
  357. body,
  358. styles.WhitespaceStyle(t.Background()),
  359. )
  360. // Add diagnostics at the bottom if they exist
  361. if diagnostics := renderDiagnostics(metadata, filename); diagnostics != "" {
  362. body += "\n" + renderContentBlock(diagnostics, WithFullWidth(), WithBorderColor(t.Error()))
  363. }
  364. }
  365. }
  366. case "write":
  367. if filename, ok := toolArgsMap["filePath"].(string); ok {
  368. title = fmt.Sprintf("WRITE %s", relative(filename))
  369. if content, ok := toolArgsMap["content"].(string); ok {
  370. body = renderFile(filename, content)
  371. // Add diagnostics at the bottom if they exist
  372. if diagnostics := renderDiagnostics(metadata, filename); diagnostics != "" {
  373. body += "\n" + renderContentBlock(diagnostics, WithFullWidth(), WithBorderColor(t.Error()))
  374. }
  375. }
  376. }
  377. case "bash":
  378. if description, ok := toolArgsMap["description"].(string); ok {
  379. title = fmt.Sprintf("SHELL %s", description)
  380. }
  381. if stdout, ok := metadata.Get("stdout"); ok {
  382. command := toolArgsMap["command"].(string)
  383. stdout := stdout.(string)
  384. body = fmt.Sprintf("```console\n> %s\n%s```", command, stdout)
  385. body = toMarkdown(body, innerWidth, t.BackgroundPanel())
  386. body = renderContentBlock(body, WithFullWidth(), WithMarginBottom(1))
  387. }
  388. case "webfetch":
  389. toolArgs = renderArgs(&toolArgsMap, "url")
  390. title = fmt.Sprintf("FETCH %s", toolArgs)
  391. if format, ok := toolArgsMap["format"].(string); ok {
  392. if result != nil {
  393. body = *result
  394. body = truncateHeight(body, 10)
  395. if format == "html" || format == "markdown" {
  396. body = toMarkdown(body, innerWidth, t.BackgroundPanel())
  397. }
  398. body = renderContentBlock(body, WithFullWidth(), WithMarginBottom(1))
  399. }
  400. }
  401. case "todowrite":
  402. title = fmt.Sprintf("PLAN")
  403. if to, ok := metadata.Get("todos"); ok && finished {
  404. todos := to.([]any)
  405. for _, todo := range todos {
  406. t := todo.(map[string]any)
  407. content := t["content"].(string)
  408. switch t["status"].(string) {
  409. case "completed":
  410. body += fmt.Sprintf("- [x] %s\n", content)
  411. // case "in-progress":
  412. // body += fmt.Sprintf("- [ ] %s\n", content)
  413. default:
  414. body += fmt.Sprintf("- [ ] %s\n", content)
  415. }
  416. }
  417. body = toMarkdown(body, innerWidth, t.BackgroundPanel())
  418. body = renderContentBlock(body, WithFullWidth(), WithMarginBottom(1))
  419. }
  420. case "task":
  421. if description, ok := toolArgsMap["description"].(string); ok {
  422. title = fmt.Sprintf("TASK %s", description)
  423. if summary, ok := metadata.Get("summary"); ok {
  424. toolcalls := summary.([]any)
  425. // toolcalls :=
  426. steps := []string{}
  427. for _, toolcall := range toolcalls {
  428. call := toolcall.(map[string]any)
  429. if toolInvocation, ok := call["toolInvocation"].(map[string]any); ok {
  430. data, _ := json.Marshal(toolInvocation)
  431. var toolCall client.MessageToolInvocationToolCall
  432. _ = json.Unmarshal(data, &toolCall)
  433. if metadata, ok := call["metadata"].(map[string]any); ok {
  434. data, _ = json.Marshal(metadata)
  435. var toolMetadata client.MessageMetadata_Tool_AdditionalProperties
  436. _ = json.Unmarshal(data, &toolMetadata)
  437. step := renderToolInvocation(
  438. toolCall,
  439. nil,
  440. toolMetadata,
  441. false,
  442. false,
  443. true,
  444. )
  445. steps = append(steps, step)
  446. }
  447. }
  448. }
  449. body = strings.Join(steps, "\n")
  450. body = renderContentBlock(body, WithFullWidth(), WithMarginBottom(1))
  451. }
  452. }
  453. default:
  454. toolName := renderToolName(toolCall.ToolName)
  455. title = fmt.Sprintf("%s %s", toolName, toolArgs)
  456. if result == nil {
  457. empty := ""
  458. result = &empty
  459. }
  460. body = *result
  461. body = truncateHeight(body, 10)
  462. body = renderContentBlock(body, WithFullWidth(), WithMarginBottom(1))
  463. }
  464. if contentOnly {
  465. title = "∟ " + title
  466. return title
  467. }
  468. if !showDetails {
  469. title = "∟ " + title
  470. padding := calculatePadding()
  471. style := styles.NewStyle().Background(t.BackgroundPanel()).Width(outerWidth - padding - 4 - 3)
  472. paddingBottom := 0
  473. if isLast {
  474. paddingBottom = 1
  475. }
  476. return renderContentBlock(style.Render(title),
  477. WithAlign(lipgloss.Left),
  478. WithBorderColor(t.Accent()),
  479. WithPaddingTop(0),
  480. WithPaddingBottom(paddingBottom),
  481. )
  482. }
  483. if body == "" && error == "" {
  484. body = *result
  485. body = truncateHeight(body, 10)
  486. body = renderContentBlock(body, WithFullWidth(), WithMarginBottom(1))
  487. }
  488. content := style.Render(title)
  489. content = lipgloss.PlaceHorizontal(
  490. layout.Current.Viewport.Width,
  491. lipgloss.Center,
  492. content,
  493. styles.WhitespaceStyle(t.Background()),
  494. )
  495. if showDetails && body != "" && error == "" {
  496. content += "\n" + body
  497. }
  498. if showDetails && error != "" {
  499. content += "\n" + error
  500. }
  501. return content
  502. }
  503. func renderToolName(name string) string {
  504. switch name {
  505. case "list":
  506. return "LIST"
  507. case "webfetch":
  508. return "FETCH"
  509. case "todowrite":
  510. return "PLAN"
  511. default:
  512. normalizedName := name
  513. if strings.HasPrefix(name, "opencode_") {
  514. normalizedName = strings.TrimPrefix(name, "opencode_")
  515. }
  516. return cases.Upper(language.Und).String(normalizedName)
  517. }
  518. }
  519. type fileRenderer struct {
  520. filename string
  521. content string
  522. height int
  523. }
  524. type fileRenderingOption func(*fileRenderer)
  525. func WithTruncate(height int) fileRenderingOption {
  526. return func(c *fileRenderer) {
  527. c.height = height
  528. }
  529. }
  530. func renderFile(filename string, content string, options ...fileRenderingOption) string {
  531. t := theme.CurrentTheme()
  532. renderer := &fileRenderer{
  533. filename: filename,
  534. content: content,
  535. }
  536. for _, option := range options {
  537. option(renderer)
  538. }
  539. lines := []string{}
  540. for line := range strings.SplitSeq(content, "\n") {
  541. line = strings.TrimRightFunc(line, unicode.IsSpace)
  542. line = strings.ReplaceAll(line, "\t", " ")
  543. lines = append(lines, line)
  544. }
  545. content = strings.Join(lines, "\n")
  546. width := layout.Current.Container.Width - 8
  547. if renderer.height > 0 {
  548. content = truncateHeight(content, renderer.height)
  549. }
  550. content = fmt.Sprintf("```%s\n%s\n```", extension(renderer.filename), content)
  551. content = toMarkdown(content, width, t.BackgroundPanel())
  552. return renderContentBlock(content, WithFullWidth(), WithMarginBottom(1))
  553. }
  554. func renderToolAction(name string) string {
  555. switch name {
  556. case "task":
  557. return "Searching..."
  558. case "bash":
  559. return "Building command..."
  560. case "edit":
  561. return "Preparing edit..."
  562. case "webfetch":
  563. return "Fetching from the web..."
  564. case "glob":
  565. return "Finding files..."
  566. case "grep":
  567. return "Searching content..."
  568. case "list":
  569. return "Listing directory..."
  570. case "read":
  571. return "Reading file..."
  572. case "write":
  573. return "Preparing write..."
  574. case "todowrite", "todoread":
  575. return "Planning..."
  576. case "patch":
  577. return "Preparing patch..."
  578. case "batch":
  579. return "Running batch operations..."
  580. }
  581. return "Working..."
  582. }
  583. func renderArgs(args *map[string]any, titleKey string) string {
  584. if args == nil || len(*args) == 0 {
  585. return ""
  586. }
  587. keys := make([]string, 0, len(*args))
  588. for key := range *args {
  589. keys = append(keys, key)
  590. }
  591. slices.Sort(keys)
  592. title := ""
  593. parts := []string{}
  594. for _, key := range keys {
  595. value := (*args)[key]
  596. if value == nil {
  597. continue
  598. }
  599. if key == "filePath" || key == "path" {
  600. value = relative(value.(string))
  601. }
  602. if key == titleKey {
  603. title = fmt.Sprintf("%s", value)
  604. continue
  605. }
  606. parts = append(parts, fmt.Sprintf("%s=%v", key, value))
  607. }
  608. if len(parts) == 0 {
  609. return title
  610. }
  611. return fmt.Sprintf("%s (%s)", title, strings.Join(parts, ", "))
  612. }
  613. func truncateHeight(content string, height int) string {
  614. lines := strings.Split(content, "\n")
  615. if len(lines) > height {
  616. return strings.Join(lines[:height], "\n")
  617. }
  618. return content
  619. }
  620. func relative(path string) string {
  621. return strings.TrimPrefix(path, app.RootPath+"/")
  622. }
  623. func extension(path string) string {
  624. ext := filepath.Ext(path)
  625. if ext == "" {
  626. ext = ""
  627. } else {
  628. ext = strings.ToLower(ext[1:])
  629. }
  630. return ext
  631. }
  632. // Diagnostic represents an LSP diagnostic
  633. type Diagnostic struct {
  634. Range struct {
  635. Start struct {
  636. Line int `json:"line"`
  637. Character int `json:"character"`
  638. } `json:"start"`
  639. } `json:"range"`
  640. Severity int `json:"severity"`
  641. Message string `json:"message"`
  642. }
  643. // renderDiagnostics formats LSP diagnostics for display in the TUI
  644. func renderDiagnostics(metadata client.MessageMetadata_Tool_AdditionalProperties, filePath string) string {
  645. diagnosticsData, ok := metadata.Get("diagnostics")
  646. if !ok {
  647. return ""
  648. }
  649. // diagnosticsData should be a map[string][]Diagnostic
  650. diagnosticsMap, ok := diagnosticsData.(map[string]interface{})
  651. if !ok {
  652. return ""
  653. }
  654. fileDiagnostics, ok := diagnosticsMap[filePath]
  655. if !ok {
  656. return ""
  657. }
  658. diagnosticsList, ok := fileDiagnostics.([]interface{})
  659. if !ok {
  660. return ""
  661. }
  662. var errorDiagnostics []string
  663. for _, diagInterface := range diagnosticsList {
  664. diagMap, ok := diagInterface.(map[string]interface{})
  665. if !ok {
  666. continue
  667. }
  668. // Parse the diagnostic
  669. var diag Diagnostic
  670. diagBytes, err := json.Marshal(diagMap)
  671. if err != nil {
  672. continue
  673. }
  674. if err := json.Unmarshal(diagBytes, &diag); err != nil {
  675. continue
  676. }
  677. // Only show error diagnostics (severity === 1)
  678. if diag.Severity != 1 {
  679. continue
  680. }
  681. line := diag.Range.Start.Line + 1 // 1-based
  682. column := diag.Range.Start.Character + 1 // 1-based
  683. errorDiagnostics = append(errorDiagnostics, fmt.Sprintf("Error [%d:%d] %s", line, column, diag.Message))
  684. }
  685. if len(errorDiagnostics) == 0 {
  686. return ""
  687. }
  688. t := theme.CurrentTheme()
  689. var result strings.Builder
  690. for _, diagnostic := range errorDiagnostics {
  691. if result.Len() > 0 {
  692. result.WriteString("\n")
  693. }
  694. result.WriteString(styles.NewStyle().Foreground(t.Error()).Render(diagnostic))
  695. }
  696. return result.String()
  697. }