message.go 17 KB

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