message.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. package chat
  2. import (
  3. "fmt"
  4. "log/slog"
  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.Info.Path.Root+"/", "")
  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.BackgroundSubtle()).
  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.BackgroundSubtle()
  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.BackgroundSubtle()).
  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.BackgroundSubtle()).
  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. )
  162. content = lipgloss.PlaceHorizontal(
  163. layout.Current.Viewport.Width,
  164. lipgloss.Center,
  165. content,
  166. )
  167. return content
  168. }
  169. func renderText(message client.MessageInfo, text string, author string) string {
  170. t := theme.CurrentTheme()
  171. width := layout.Current.Container.Width
  172. padding := 0
  173. if layout.Current.Viewport.Width < 80 {
  174. padding = 5
  175. } else if layout.Current.Viewport.Width < 120 {
  176. padding = 15
  177. } else {
  178. padding = 20
  179. }
  180. timestamp := time.UnixMilli(int64(message.Metadata.Time.Created)).Local().Format("02 Jan 2006 03:04 PM")
  181. if time.Now().Format("02 Jan 2006") == timestamp[:11] {
  182. // don't show the date if it's today
  183. timestamp = timestamp[12:]
  184. }
  185. info := fmt.Sprintf("%s (%s)", author, timestamp)
  186. align := lipgloss.Left
  187. switch message.Role {
  188. case client.User:
  189. align = lipgloss.Right
  190. case client.Assistant:
  191. align = lipgloss.Left
  192. }
  193. textWidth := max(lipgloss.Width(text), lipgloss.Width(info))
  194. markdownWidth := min(textWidth, width-padding-4) // -4 for the border and padding
  195. content := toMarkdown(text, markdownWidth, t.BackgroundSubtle())
  196. content = lipgloss.JoinVertical(align, content, info)
  197. switch message.Role {
  198. case client.User:
  199. return renderContentBlock(content,
  200. WithAlign(lipgloss.Right),
  201. WithBorderColor(t.Secondary()),
  202. )
  203. case client.Assistant:
  204. return renderContentBlock(content,
  205. WithAlign(lipgloss.Left),
  206. WithBorderColor(t.Accent()),
  207. )
  208. }
  209. return ""
  210. }
  211. func renderToolInvocation(
  212. toolCall client.MessageToolInvocationToolCall,
  213. result *string,
  214. metadata client.MessageInfo_Metadata_Tool_AdditionalProperties,
  215. showResult bool,
  216. ) string {
  217. ignoredTools := []string{"opencode_todoread"}
  218. if slices.Contains(ignoredTools, toolCall.ToolName) {
  219. return ""
  220. }
  221. outerWidth := layout.Current.Container.Width
  222. innerWidth := outerWidth - 6
  223. paddingTop := 0
  224. paddingBottom := 0
  225. if showResult {
  226. paddingTop = 1
  227. if result == nil || *result == "" {
  228. paddingBottom = 1
  229. }
  230. }
  231. t := theme.CurrentTheme()
  232. style := styles.Muted().
  233. Width(outerWidth).
  234. Background(t.BackgroundSubtle()).
  235. PaddingTop(paddingTop).
  236. PaddingBottom(paddingBottom).
  237. PaddingLeft(2).
  238. PaddingRight(2).
  239. BorderLeft(true).
  240. BorderRight(true).
  241. BorderForeground(t.BackgroundSubtle()).
  242. BorderStyle(lipgloss.ThickBorder())
  243. if toolCall.State == "partial-call" {
  244. style = style.Foreground(t.TextMuted())
  245. return style.Render(renderToolAction(toolCall.ToolName))
  246. }
  247. toolArgs := ""
  248. toolArgsMap := make(map[string]any)
  249. if toolCall.Args != nil {
  250. value := *toolCall.Args
  251. m, ok := value.(map[string]any)
  252. if ok {
  253. toolArgsMap = m
  254. firstKey := ""
  255. for key := range toolArgsMap {
  256. firstKey = key
  257. break
  258. }
  259. toolArgs = renderArgs(&toolArgsMap, firstKey)
  260. }
  261. }
  262. if len(toolArgsMap) == 0 {
  263. slog.Debug("no args")
  264. }
  265. body := ""
  266. error := ""
  267. finished := result != nil && *result != ""
  268. if e, ok := metadata.Get("error"); ok && e.(bool) == true {
  269. if m, ok := metadata.Get("message"); ok {
  270. style = style.BorderLeftForeground(t.Error())
  271. error = styles.BaseStyle().
  272. Background(t.BackgroundSubtle()).
  273. Foreground(t.Error()).
  274. Render(m.(string))
  275. error = renderContentBlock(
  276. error,
  277. WithFullWidth(),
  278. WithBorderColor(t.Error()),
  279. WithMarginBottom(1),
  280. )
  281. }
  282. }
  283. elapsed := ""
  284. start := metadata.Time.Start
  285. end := metadata.Time.End
  286. durationMs := end - start
  287. duration := time.Duration(durationMs * float32(time.Millisecond))
  288. roundedDuration := time.Duration(duration.Round(time.Millisecond))
  289. if durationMs > 1000 {
  290. roundedDuration = time.Duration(duration.Round(time.Second))
  291. }
  292. elapsed = styles.Muted().Render(roundedDuration.String())
  293. title := ""
  294. switch toolCall.ToolName {
  295. case "opencode_read":
  296. toolArgs = renderArgs(&toolArgsMap, "filePath")
  297. title = fmt.Sprintf("Read: %s %s", toolArgs, elapsed)
  298. if preview, ok := metadata.Get("preview"); ok && toolArgsMap["filePath"] != nil {
  299. filename := toolArgsMap["filePath"].(string)
  300. body = preview.(string)
  301. body = renderFile(filename, body, WithTruncate(6))
  302. }
  303. case "opencode_edit":
  304. if filename, ok := toolArgsMap["filePath"].(string); ok {
  305. title = fmt.Sprintf("Edit: %s %s", relative(filename), elapsed)
  306. if d, ok := metadata.Get("diff"); ok {
  307. patch := d.(string)
  308. var formattedDiff string
  309. if layout.Current.Viewport.Width < 80 {
  310. formattedDiff, _ = diff.FormatUnifiedDiff(
  311. filename,
  312. patch,
  313. diff.WithWidth(layout.Current.Container.Width-2),
  314. )
  315. } else {
  316. diffWidth := min(layout.Current.Viewport.Width-2, 120)
  317. formattedDiff, _ = diff.FormatDiff(filename, patch, diff.WithTotalWidth(diffWidth))
  318. }
  319. formattedDiff = strings.TrimSpace(formattedDiff)
  320. formattedDiff = lipgloss.NewStyle().
  321. BorderStyle(lipgloss.ThickBorder()).
  322. BorderForeground(t.BackgroundSubtle()).
  323. BorderLeft(true).
  324. BorderRight(true).
  325. Render(formattedDiff)
  326. if showResult {
  327. style = style.Width(lipgloss.Width(formattedDiff))
  328. title += "\n"
  329. }
  330. body = strings.TrimSpace(formattedDiff)
  331. body = lipgloss.Place(
  332. layout.Current.Viewport.Width,
  333. lipgloss.Height(body)+1,
  334. lipgloss.Center,
  335. lipgloss.Top,
  336. body,
  337. )
  338. }
  339. }
  340. case "opencode_write":
  341. if filename, ok := toolArgsMap["filePath"].(string); ok {
  342. title = fmt.Sprintf("Write: %s %s", relative(filename), elapsed)
  343. if content, ok := toolArgsMap["content"].(string); ok {
  344. body = renderFile(filename, content)
  345. }
  346. }
  347. case "opencode_bash":
  348. if description, ok := toolArgsMap["description"].(string); ok {
  349. title = fmt.Sprintf("Shell: %s %s", description, elapsed)
  350. }
  351. if stdout, ok := metadata.Get("stdout"); ok {
  352. command := toolArgsMap["command"].(string)
  353. stdout := stdout.(string)
  354. body = fmt.Sprintf("```console\n> %s\n%s```", command, stdout)
  355. body = toMarkdown(body, innerWidth, t.BackgroundSubtle())
  356. body = renderContentBlock(body, WithFullWidth(), WithMarginBottom(1))
  357. }
  358. case "opencode_webfetch":
  359. toolArgs = renderArgs(&toolArgsMap, "url")
  360. title = fmt.Sprintf("Fetching: %s %s", toolArgs, elapsed)
  361. if format, ok := toolArgsMap["format"].(string); ok {
  362. body = *result
  363. body = truncateHeight(body, 10)
  364. if format == "html" || format == "markdown" {
  365. body = toMarkdown(body, innerWidth, t.BackgroundSubtle())
  366. }
  367. body = renderContentBlock(body, WithFullWidth(), WithMarginBottom(1))
  368. }
  369. case "opencode_todowrite":
  370. title = fmt.Sprintf("Planning %s", elapsed)
  371. if to, ok := metadata.Get("todos"); ok && finished {
  372. todos := to.([]any)
  373. for _, todo := range todos {
  374. t := todo.(map[string]any)
  375. content := t["content"].(string)
  376. switch t["status"].(string) {
  377. case "completed":
  378. body += fmt.Sprintf("- [x] %s\n", content)
  379. // case "in-progress":
  380. // body += fmt.Sprintf("- [ ] %s\n", content)
  381. default:
  382. body += fmt.Sprintf("- [ ] %s\n", content)
  383. }
  384. }
  385. body = toMarkdown(body, innerWidth, t.BackgroundSubtle())
  386. body = renderContentBlock(body, WithFullWidth(), WithMarginBottom(1))
  387. }
  388. default:
  389. toolName := renderToolName(toolCall.ToolName)
  390. title = fmt.Sprintf("%s: %s %s", toolName, toolArgs, elapsed)
  391. body = *result
  392. body = truncateHeight(body, 10)
  393. body = renderContentBlock(body, WithFullWidth(), WithMarginBottom(1))
  394. }
  395. if body == "" && error == "" {
  396. body = *result
  397. body = truncateHeight(body, 10)
  398. body = renderContentBlock(body, WithFullWidth(), WithMarginBottom(1))
  399. }
  400. content := style.Render(title)
  401. content = lipgloss.PlaceHorizontal(layout.Current.Viewport.Width, lipgloss.Center, content)
  402. if showResult && body != "" && error == "" {
  403. content += "\n" + body
  404. }
  405. if showResult && error != "" {
  406. content += "\n" + error
  407. }
  408. return content
  409. }
  410. func renderToolName(name string) string {
  411. switch name {
  412. // case agent.AgentToolName:
  413. // return "Task"
  414. case "opencode_ls":
  415. return "List"
  416. case "opencode_webfetch":
  417. return "Fetch"
  418. case "opencode_todoread":
  419. return "Planning"
  420. case "opencode_todowrite":
  421. return "Planning"
  422. default:
  423. normalizedName := name
  424. if strings.HasPrefix(name, "opencode_") {
  425. normalizedName = strings.TrimPrefix(name, "opencode_")
  426. }
  427. return cases.Title(language.Und).String(normalizedName)
  428. }
  429. }
  430. type fileRenderer struct {
  431. filename string
  432. content string
  433. height int
  434. }
  435. type fileRenderingOption func(*fileRenderer)
  436. func WithTruncate(height int) fileRenderingOption {
  437. return func(c *fileRenderer) {
  438. c.height = height
  439. }
  440. }
  441. func renderFile(filename string, content string, options ...fileRenderingOption) string {
  442. t := theme.CurrentTheme()
  443. renderer := &fileRenderer{
  444. filename: filename,
  445. content: content,
  446. }
  447. for _, option := range options {
  448. option(renderer)
  449. }
  450. lines := []string{}
  451. for line := range strings.SplitSeq(content, "\n") {
  452. line = strings.TrimRightFunc(line, unicode.IsSpace)
  453. line = strings.ReplaceAll(line, "\t", " ")
  454. lines = append(lines, line)
  455. }
  456. content = strings.Join(lines, "\n")
  457. width := layout.Current.Container.Width - 8
  458. if renderer.height > 0 {
  459. content = truncateHeight(content, renderer.height)
  460. }
  461. content = fmt.Sprintf("```%s\n%s\n```", extension(renderer.filename), content)
  462. content = toMarkdown(content, width, t.BackgroundSubtle())
  463. return renderContentBlock(content, WithFullWidth(), WithMarginBottom(1))
  464. }
  465. func renderToolAction(name string) string {
  466. switch name {
  467. // case agent.AgentToolName:
  468. // return "Preparing prompt..."
  469. case "opencode_bash":
  470. return "Building command..."
  471. case "opencode_edit":
  472. return "Preparing edit..."
  473. case "opencode_fetch":
  474. return "Writing fetch..."
  475. case "opencode_glob":
  476. return "Finding files..."
  477. case "opencode_grep":
  478. return "Searching content..."
  479. case "opencode_ls":
  480. return "Listing directory..."
  481. case "opencode_read":
  482. return "Reading file..."
  483. case "opencode_write":
  484. return "Preparing write..."
  485. case "opencode_patch":
  486. return "Preparing patch..."
  487. case "opencode_batch":
  488. return "Running batch operations..."
  489. }
  490. return "Working..."
  491. }
  492. func renderArgs(args *map[string]any, titleKey string) string {
  493. if args == nil || len(*args) == 0 {
  494. return ""
  495. }
  496. title := ""
  497. parts := []string{}
  498. for key, value := range *args {
  499. if value == nil {
  500. continue
  501. }
  502. if key == "filePath" || key == "path" {
  503. value = relative(value.(string))
  504. }
  505. if key == titleKey {
  506. title = fmt.Sprintf("%s", value)
  507. continue
  508. }
  509. parts = append(parts, fmt.Sprintf("%s=%v", key, value))
  510. }
  511. if len(parts) == 0 {
  512. return title
  513. }
  514. return fmt.Sprintf("%s (%s)", title, strings.Join(parts, ", "))
  515. }
  516. func truncateHeight(content string, height int) string {
  517. lines := strings.Split(content, "\n")
  518. if len(lines) > height {
  519. return strings.Join(lines[:height], "\n")
  520. }
  521. return content
  522. }
  523. func relative(path string) string {
  524. return strings.TrimPrefix(path, app.Info.Path.Root+"/")
  525. }
  526. func extension(path string) string {
  527. ext := filepath.Ext(path)
  528. if ext == "" {
  529. ext = ""
  530. } else {
  531. ext = strings.ToLower(ext[1:])
  532. }
  533. return ext
  534. }