message.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. package chat
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "slices"
  6. "strings"
  7. "time"
  8. "unicode"
  9. "github.com/charmbracelet/lipgloss/v2"
  10. "github.com/charmbracelet/lipgloss/v2/compat"
  11. "github.com/charmbracelet/x/ansi"
  12. "github.com/sst/opencode/internal/app"
  13. "github.com/sst/opencode/internal/components/diff"
  14. "github.com/sst/opencode/internal/layout"
  15. "github.com/sst/opencode/internal/styles"
  16. "github.com/sst/opencode/internal/theme"
  17. "github.com/sst/opencode/pkg/client"
  18. "golang.org/x/text/cases"
  19. "golang.org/x/text/language"
  20. )
  21. func toMarkdown(content string, width int, backgroundColor compat.AdaptiveColor) string {
  22. r := styles.GetMarkdownRenderer(width, backgroundColor)
  23. content = strings.ReplaceAll(content, app.RootPath+"/", "")
  24. rendered, _ := r.Render(content)
  25. lines := strings.Split(rendered, "\n")
  26. if len(lines) > 0 {
  27. firstLine := lines[0]
  28. cleaned := ansi.Strip(firstLine)
  29. nospace := strings.ReplaceAll(cleaned, " ", "")
  30. if nospace == "" {
  31. lines = lines[1:]
  32. }
  33. if len(lines) > 0 {
  34. lastLine := lines[len(lines)-1]
  35. cleaned = ansi.Strip(lastLine)
  36. nospace = strings.ReplaceAll(cleaned, " ", "")
  37. if nospace == "" {
  38. lines = lines[:len(lines)-1]
  39. }
  40. }
  41. }
  42. content = strings.Join(lines, "\n")
  43. return strings.TrimSuffix(content, "\n")
  44. }
  45. type blockRenderer struct {
  46. align *lipgloss.Position
  47. borderColor *compat.AdaptiveColor
  48. fullWidth bool
  49. paddingTop int
  50. paddingBottom int
  51. paddingLeft int
  52. paddingRight int
  53. marginTop int
  54. marginBottom int
  55. }
  56. type renderingOption func(*blockRenderer)
  57. func WithFullWidth() renderingOption {
  58. return func(c *blockRenderer) {
  59. c.fullWidth = true
  60. }
  61. }
  62. func WithAlign(align lipgloss.Position) renderingOption {
  63. return func(c *blockRenderer) {
  64. c.align = &align
  65. }
  66. }
  67. func WithBorderColor(color compat.AdaptiveColor) renderingOption {
  68. return func(c *blockRenderer) {
  69. c.borderColor = &color
  70. }
  71. }
  72. func WithMarginTop(padding int) renderingOption {
  73. return func(c *blockRenderer) {
  74. c.marginTop = padding
  75. }
  76. }
  77. func WithMarginBottom(padding int) renderingOption {
  78. return func(c *blockRenderer) {
  79. c.marginBottom = padding
  80. }
  81. }
  82. func WithPaddingLeft(padding int) renderingOption {
  83. return func(c *blockRenderer) {
  84. c.paddingLeft = padding
  85. }
  86. }
  87. func WithPaddingRight(padding int) renderingOption {
  88. return func(c *blockRenderer) {
  89. c.paddingRight = padding
  90. }
  91. }
  92. func WithPaddingTop(padding int) renderingOption {
  93. return func(c *blockRenderer) {
  94. c.paddingTop = padding
  95. }
  96. }
  97. func WithPaddingBottom(padding int) renderingOption {
  98. return func(c *blockRenderer) {
  99. c.paddingBottom = padding
  100. }
  101. }
  102. func renderContentBlock(content string, options ...renderingOption) string {
  103. t := theme.CurrentTheme()
  104. renderer := &blockRenderer{
  105. fullWidth: false,
  106. paddingTop: 1,
  107. paddingBottom: 1,
  108. paddingLeft: 2,
  109. paddingRight: 2,
  110. }
  111. for _, option := range options {
  112. option(renderer)
  113. }
  114. style := styles.BaseStyle().
  115. // MarginTop(renderer.marginTop).
  116. // MarginBottom(renderer.marginBottom).
  117. PaddingTop(renderer.paddingTop).
  118. PaddingBottom(renderer.paddingBottom).
  119. PaddingLeft(renderer.paddingLeft).
  120. PaddingRight(renderer.paddingRight).
  121. Background(t.BackgroundSubtle()).
  122. Foreground(t.TextMuted()).
  123. BorderStyle(lipgloss.ThickBorder())
  124. align := lipgloss.Left
  125. if renderer.align != nil {
  126. align = *renderer.align
  127. }
  128. borderColor := t.BackgroundSubtle()
  129. if renderer.borderColor != nil {
  130. borderColor = *renderer.borderColor
  131. }
  132. switch align {
  133. case lipgloss.Left:
  134. style = style.
  135. BorderLeft(true).
  136. BorderRight(true).
  137. AlignHorizontal(align).
  138. BorderLeftForeground(borderColor).
  139. BorderLeftBackground(t.Background()).
  140. BorderRightForeground(t.BackgroundSubtle()).
  141. BorderRightBackground(t.Background())
  142. case lipgloss.Right:
  143. style = style.
  144. BorderRight(true).
  145. BorderLeft(true).
  146. AlignHorizontal(align).
  147. BorderRightForeground(borderColor).
  148. BorderRightBackground(t.Background()).
  149. BorderLeftForeground(t.BackgroundSubtle()).
  150. BorderLeftBackground(t.Background())
  151. }
  152. if renderer.fullWidth {
  153. style = style.Width(layout.Current.Container.Width)
  154. }
  155. content = style.Render(content)
  156. content = lipgloss.PlaceHorizontal(
  157. layout.Current.Container.Width,
  158. align,
  159. content,
  160. lipgloss.WithWhitespaceStyle(lipgloss.NewStyle().Background(t.Background())),
  161. )
  162. content = lipgloss.PlaceHorizontal(
  163. layout.Current.Viewport.Width,
  164. lipgloss.Center,
  165. content,
  166. lipgloss.WithWhitespaceStyle(lipgloss.NewStyle().Background(t.Background())),
  167. )
  168. if renderer.marginTop > 0 {
  169. for range renderer.marginTop {
  170. content = "\n" + content
  171. }
  172. }
  173. if renderer.marginBottom > 0 {
  174. for range renderer.marginBottom {
  175. content = content + "\n"
  176. }
  177. }
  178. return content
  179. }
  180. func calculatePadding() int {
  181. if layout.Current.Viewport.Width < 80 {
  182. return 5
  183. } else if layout.Current.Viewport.Width < 120 {
  184. return 15
  185. } else {
  186. return 20
  187. }
  188. }
  189. func renderText(message client.MessageInfo, text string, author string) string {
  190. t := theme.CurrentTheme()
  191. width := layout.Current.Container.Width
  192. padding := calculatePadding()
  193. timestamp := time.UnixMilli(int64(message.Metadata.Time.Created)).Local().Format("02 Jan 2006 03:04 PM")
  194. if time.Now().Format("02 Jan 2006") == timestamp[:11] {
  195. // don't show the date if it's today
  196. timestamp = timestamp[12:]
  197. }
  198. info := fmt.Sprintf("%s (%s)", author, timestamp)
  199. textWidth := max(lipgloss.Width(text), lipgloss.Width(info))
  200. markdownWidth := min(textWidth, width-padding-4) // -4 for the border and padding
  201. if message.Role == client.Assistant {
  202. markdownWidth = width - padding - 4
  203. }
  204. content := toMarkdown(text, markdownWidth, t.BackgroundSubtle())
  205. content = strings.Join([]string{content, info}, "\n")
  206. switch message.Role {
  207. case client.User:
  208. return renderContentBlock(content,
  209. WithAlign(lipgloss.Right),
  210. WithBorderColor(t.Secondary()),
  211. )
  212. case client.Assistant:
  213. return renderContentBlock(content,
  214. WithAlign(lipgloss.Left),
  215. WithBorderColor(t.Accent()),
  216. )
  217. }
  218. return ""
  219. }
  220. func renderToolInvocation(
  221. toolCall client.MessageToolInvocationToolCall,
  222. result *string,
  223. metadata client.MessageInfo_Metadata_Tool_AdditionalProperties,
  224. showDetails bool,
  225. isLast bool,
  226. ) string {
  227. ignoredTools := []string{"todoread"}
  228. if slices.Contains(ignoredTools, toolCall.ToolName) {
  229. return ""
  230. }
  231. outerWidth := layout.Current.Container.Width
  232. innerWidth := outerWidth - 6
  233. paddingTop := 0
  234. paddingBottom := 0
  235. if showDetails {
  236. paddingTop = 1
  237. if result == nil || *result == "" {
  238. paddingBottom = 1
  239. }
  240. }
  241. t := theme.CurrentTheme()
  242. style := styles.Muted().
  243. Width(outerWidth).
  244. Background(t.BackgroundSubtle()).
  245. PaddingTop(paddingTop).
  246. PaddingBottom(paddingBottom).
  247. PaddingLeft(2).
  248. PaddingRight(2).
  249. BorderLeft(true).
  250. BorderRight(true).
  251. BorderBackground(t.Background()).
  252. BorderForeground(t.BackgroundSubtle()).
  253. BorderStyle(lipgloss.ThickBorder())
  254. if toolCall.State == "partial-call" {
  255. title := renderToolAction(toolCall.ToolName)
  256. if !showDetails {
  257. title = "∟ " + title
  258. padding := calculatePadding()
  259. style := lipgloss.NewStyle().Width(outerWidth - padding - 4).Background(t.BackgroundSubtle())
  260. return renderContentBlock(style.Render(title),
  261. WithAlign(lipgloss.Left),
  262. WithBorderColor(t.Accent()),
  263. WithPaddingTop(0),
  264. WithPaddingBottom(1),
  265. )
  266. }
  267. style = style.Foreground(t.TextMuted())
  268. return style.Render(title)
  269. }
  270. toolArgs := ""
  271. toolArgsMap := make(map[string]any)
  272. if toolCall.Args != nil {
  273. value := *toolCall.Args
  274. m, ok := value.(map[string]any)
  275. if ok {
  276. toolArgsMap = m
  277. firstKey := ""
  278. for key := range toolArgsMap {
  279. firstKey = key
  280. break
  281. }
  282. toolArgs = renderArgs(&toolArgsMap, firstKey)
  283. }
  284. }
  285. body := ""
  286. error := ""
  287. finished := result != nil && *result != ""
  288. if e, ok := metadata.Get("error"); ok && e.(bool) == true {
  289. if m, ok := metadata.Get("message"); ok {
  290. style = style.BorderLeftForeground(t.Error())
  291. error = styles.BaseStyle().
  292. Background(t.BackgroundSubtle()).
  293. Foreground(t.Error()).
  294. Render(m.(string))
  295. error = renderContentBlock(
  296. error,
  297. WithFullWidth(),
  298. WithBorderColor(t.Error()),
  299. WithMarginBottom(1),
  300. )
  301. }
  302. }
  303. elapsed := ""
  304. start := metadata.Time.Start
  305. end := metadata.Time.End
  306. durationMs := end - start
  307. duration := time.Duration(durationMs * float32(time.Millisecond))
  308. roundedDuration := time.Duration(duration.Round(time.Millisecond))
  309. if durationMs > 1000 {
  310. roundedDuration = time.Duration(duration.Round(time.Second))
  311. }
  312. elapsed = styles.Muted().Render(roundedDuration.String())
  313. title := ""
  314. switch toolCall.ToolName {
  315. case "read":
  316. toolArgs = renderArgs(&toolArgsMap, "filePath")
  317. title = fmt.Sprintf("READ %s %s", toolArgs, elapsed)
  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 %s", relative(filename), elapsed)
  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 = lipgloss.NewStyle().
  341. BorderStyle(lipgloss.ThickBorder()).
  342. BorderBackground(t.Background()).
  343. BorderForeground(t.BackgroundSubtle()).
  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. lipgloss.WithWhitespaceStyle(lipgloss.NewStyle().Background(t.Background())),
  359. )
  360. }
  361. }
  362. case "write":
  363. if filename, ok := toolArgsMap["filePath"].(string); ok {
  364. title = fmt.Sprintf("WRITE %s %s", relative(filename), elapsed)
  365. if content, ok := toolArgsMap["content"].(string); ok {
  366. body = renderFile(filename, content)
  367. }
  368. }
  369. case "bash":
  370. if description, ok := toolArgsMap["description"].(string); ok {
  371. title = fmt.Sprintf("SHELL %s %s", description, elapsed)
  372. }
  373. if stdout, ok := metadata.Get("stdout"); ok {
  374. command := toolArgsMap["command"].(string)
  375. stdout := stdout.(string)
  376. body = fmt.Sprintf("```console\n> %s\n%s```", command, stdout)
  377. body = toMarkdown(body, innerWidth, t.BackgroundSubtle())
  378. body = renderContentBlock(body, WithFullWidth(), WithMarginBottom(1))
  379. }
  380. case "webfetch":
  381. toolArgs = renderArgs(&toolArgsMap, "url")
  382. title = fmt.Sprintf("FETCH %s %s", toolArgs, elapsed)
  383. if format, ok := toolArgsMap["format"].(string); ok {
  384. body = *result
  385. body = truncateHeight(body, 10)
  386. if format == "html" || format == "markdown" {
  387. body = toMarkdown(body, innerWidth, t.BackgroundSubtle())
  388. }
  389. body = renderContentBlock(body, WithFullWidth(), WithMarginBottom(1))
  390. }
  391. case "todowrite":
  392. title = fmt.Sprintf("PLAN %s", elapsed)
  393. if to, ok := metadata.Get("todos"); ok && finished {
  394. todos := to.([]any)
  395. for _, todo := range todos {
  396. t := todo.(map[string]any)
  397. content := t["content"].(string)
  398. switch t["status"].(string) {
  399. case "completed":
  400. body += fmt.Sprintf("- [x] %s\n", content)
  401. // case "in-progress":
  402. // body += fmt.Sprintf("- [ ] %s\n", content)
  403. default:
  404. body += fmt.Sprintf("- [ ] %s\n", content)
  405. }
  406. }
  407. body = toMarkdown(body, innerWidth, t.BackgroundSubtle())
  408. body = renderContentBlock(body, WithFullWidth(), WithMarginBottom(1))
  409. }
  410. default:
  411. toolName := renderToolName(toolCall.ToolName)
  412. title = fmt.Sprintf("%s %s %s", toolName, toolArgs, elapsed)
  413. body = *result
  414. body = truncateHeight(body, 10)
  415. body = renderContentBlock(body, WithFullWidth(), WithMarginBottom(1))
  416. }
  417. if !showDetails {
  418. title = "∟ " + title
  419. padding := calculatePadding()
  420. style := lipgloss.NewStyle().Width(outerWidth - padding - 4).Background(t.BackgroundSubtle())
  421. paddingBottom := 0
  422. if isLast {
  423. paddingBottom = 1
  424. }
  425. return renderContentBlock(style.Render(title),
  426. WithAlign(lipgloss.Left),
  427. WithBorderColor(t.Accent()),
  428. WithPaddingTop(0),
  429. WithPaddingBottom(paddingBottom),
  430. )
  431. }
  432. if body == "" && error == "" {
  433. body = *result
  434. body = truncateHeight(body, 10)
  435. body = renderContentBlock(body, WithFullWidth(), WithMarginBottom(1))
  436. }
  437. content := style.Render(title)
  438. content = lipgloss.PlaceHorizontal(
  439. layout.Current.Viewport.Width,
  440. lipgloss.Center,
  441. content,
  442. lipgloss.WithWhitespaceStyle(lipgloss.NewStyle().Background(t.Background())),
  443. )
  444. if showDetails && body != "" && error == "" {
  445. content += "\n" + body
  446. }
  447. if showDetails && error != "" {
  448. content += "\n" + error
  449. }
  450. return content
  451. }
  452. func renderToolName(name string) string {
  453. switch name {
  454. // case agent.AgentToolName:
  455. // return "Task"
  456. case "list":
  457. return "LIST"
  458. case "webfetch":
  459. return "FETCH"
  460. case "todowrite":
  461. return "PLAN"
  462. default:
  463. normalizedName := name
  464. if strings.HasPrefix(name, "opencode_") {
  465. normalizedName = strings.TrimPrefix(name, "opencode_")
  466. }
  467. return cases.Upper(language.Und).String(normalizedName)
  468. }
  469. }
  470. type fileRenderer struct {
  471. filename string
  472. content string
  473. height int
  474. }
  475. type fileRenderingOption func(*fileRenderer)
  476. func WithTruncate(height int) fileRenderingOption {
  477. return func(c *fileRenderer) {
  478. c.height = height
  479. }
  480. }
  481. func renderFile(filename string, content string, options ...fileRenderingOption) string {
  482. t := theme.CurrentTheme()
  483. renderer := &fileRenderer{
  484. filename: filename,
  485. content: content,
  486. }
  487. for _, option := range options {
  488. option(renderer)
  489. }
  490. lines := []string{}
  491. for line := range strings.SplitSeq(content, "\n") {
  492. line = strings.TrimRightFunc(line, unicode.IsSpace)
  493. line = strings.ReplaceAll(line, "\t", " ")
  494. lines = append(lines, line)
  495. }
  496. content = strings.Join(lines, "\n")
  497. width := layout.Current.Container.Width - 8
  498. if renderer.height > 0 {
  499. content = truncateHeight(content, renderer.height)
  500. }
  501. content = fmt.Sprintf("```%s\n%s\n```", extension(renderer.filename), content)
  502. content = toMarkdown(content, width, t.BackgroundSubtle())
  503. return renderContentBlock(content, WithFullWidth(), WithMarginBottom(1))
  504. }
  505. func renderToolAction(name string) string {
  506. switch name {
  507. // case agent.AgentToolName:
  508. // return "Preparing prompt..."
  509. case "bash":
  510. return "Building command..."
  511. case "edit":
  512. return "Preparing edit..."
  513. case "webfetch":
  514. return "Writing fetch..."
  515. case "glob":
  516. return "Finding files..."
  517. case "grep":
  518. return "Searching content..."
  519. case "list":
  520. return "Listing directory..."
  521. case "read":
  522. return "Reading file..."
  523. case "write":
  524. return "Preparing write..."
  525. case "todowrite", "todoread":
  526. return "Planning..."
  527. case "patch":
  528. return "Preparing patch..."
  529. case "batch":
  530. return "Running batch operations..."
  531. }
  532. return "Working..."
  533. }
  534. func renderArgs(args *map[string]any, titleKey string) string {
  535. if args == nil || len(*args) == 0 {
  536. return ""
  537. }
  538. title := ""
  539. parts := []string{}
  540. for key, value := range *args {
  541. if value == nil {
  542. continue
  543. }
  544. if key == "filePath" || key == "path" {
  545. value = relative(value.(string))
  546. }
  547. if key == titleKey {
  548. title = fmt.Sprintf("%s", value)
  549. continue
  550. }
  551. parts = append(parts, fmt.Sprintf("%s=%v", key, value))
  552. }
  553. if len(parts) == 0 {
  554. return title
  555. }
  556. return fmt.Sprintf("%s (%s)", title, strings.Join(parts, ", "))
  557. }
  558. func truncateHeight(content string, height int) string {
  559. lines := strings.Split(content, "\n")
  560. if len(lines) > height {
  561. return strings.Join(lines[:height], "\n")
  562. }
  563. return content
  564. }
  565. func relative(path string) string {
  566. return strings.TrimPrefix(path, app.RootPath+"/")
  567. }
  568. func extension(path string) string {
  569. ext := filepath.Ext(path)
  570. if ext == "" {
  571. ext = ""
  572. } else {
  573. ext = strings.ToLower(ext[1:])
  574. }
  575. return ext
  576. }