message.go 17 KB

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