message.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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"
  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) string {
  22. r := styles.GetMarkdownRenderer(width)
  23. content = strings.ReplaceAll(content, app.Info.Path.Root+"/", "")
  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 *lipgloss.AdaptiveColor
  48. fullWidth bool
  49. paddingTop int
  50. paddingBottom int
  51. }
  52. type renderingOption func(*blockRenderer)
  53. func WithFullWidth() renderingOption {
  54. return func(c *blockRenderer) {
  55. c.fullWidth = true
  56. }
  57. }
  58. func WithAlign(align lipgloss.Position) renderingOption {
  59. return func(c *blockRenderer) {
  60. c.align = &align
  61. }
  62. }
  63. func WithBorderColor(color lipgloss.AdaptiveColor) renderingOption {
  64. return func(c *blockRenderer) {
  65. c.borderColor = &color
  66. }
  67. }
  68. func WithPaddingTop(padding int) renderingOption {
  69. return func(c *blockRenderer) {
  70. c.paddingTop = padding
  71. }
  72. }
  73. func WithPaddingBottom(padding int) renderingOption {
  74. return func(c *blockRenderer) {
  75. c.paddingBottom = padding
  76. }
  77. }
  78. func renderContentBlock(content string, options ...renderingOption) string {
  79. t := theme.CurrentTheme()
  80. renderer := &blockRenderer{
  81. fullWidth: false,
  82. }
  83. for _, option := range options {
  84. option(renderer)
  85. }
  86. style := styles.BaseStyle().
  87. PaddingTop(1).
  88. PaddingBottom(1).
  89. PaddingLeft(2).
  90. PaddingRight(2).
  91. Background(t.BackgroundSubtle()).
  92. Foreground(t.TextMuted()).
  93. BorderStyle(lipgloss.ThickBorder())
  94. align := lipgloss.Left
  95. if renderer.align != nil {
  96. align = *renderer.align
  97. }
  98. borderColor := t.BackgroundSubtle()
  99. if renderer.borderColor != nil {
  100. borderColor = *renderer.borderColor
  101. }
  102. switch align {
  103. case lipgloss.Left:
  104. style = style.
  105. BorderLeft(true).
  106. BorderRight(true).
  107. AlignHorizontal(align).
  108. BorderLeftForeground(borderColor).
  109. BorderLeftBackground(t.Background()).
  110. BorderRightForeground(t.BackgroundSubtle()).
  111. BorderRightBackground(t.Background())
  112. case lipgloss.Right:
  113. style = style.
  114. BorderRight(true).
  115. BorderLeft(true).
  116. AlignHorizontal(align).
  117. BorderRightForeground(borderColor).
  118. BorderRightBackground(t.Background()).
  119. BorderLeftForeground(t.BackgroundSubtle()).
  120. BorderLeftBackground(t.Background())
  121. }
  122. content = styles.ForceReplaceBackgroundWithLipgloss(content, t.BackgroundSubtle())
  123. if renderer.fullWidth {
  124. style = style.Width(layout.Current.Container.Width - 2)
  125. }
  126. content = style.Render(content)
  127. if renderer.paddingTop > 0 {
  128. content = strings.Repeat("\n", renderer.paddingTop) + content
  129. }
  130. if renderer.paddingBottom > 0 {
  131. content = content + strings.Repeat("\n", renderer.paddingBottom)
  132. }
  133. content = lipgloss.PlaceHorizontal(
  134. layout.Current.Container.Width,
  135. align,
  136. content,
  137. lipgloss.WithWhitespaceBackground(t.Background()),
  138. )
  139. content = lipgloss.PlaceHorizontal(
  140. layout.Current.Viewport.Width,
  141. lipgloss.Center,
  142. content,
  143. lipgloss.WithWhitespaceBackground(t.Background()),
  144. )
  145. return content
  146. }
  147. func renderText(message client.MessageInfo, text string, author string) string {
  148. t := theme.CurrentTheme()
  149. width := layout.Current.Container.Width
  150. padding := 0
  151. switch layout.Current.Size {
  152. case layout.LayoutSizeSmall:
  153. padding = 5
  154. case layout.LayoutSizeNormal:
  155. padding = 10
  156. case layout.LayoutSizeLarge:
  157. padding = 15
  158. }
  159. timestamp := time.UnixMilli(int64(message.Metadata.Time.Created)).Local().Format("02 Jan 2006 03:04 PM")
  160. if time.Now().Format("02 Jan 2006") == timestamp[:11] {
  161. // don't show the date if it's today
  162. timestamp = timestamp[12:]
  163. }
  164. info := styles.BaseStyle().
  165. Foreground(t.TextMuted()).
  166. Render(fmt.Sprintf("%s (%s)", author, timestamp))
  167. align := lipgloss.Left
  168. switch message.Role {
  169. case client.User:
  170. align = lipgloss.Right
  171. case client.Assistant:
  172. align = lipgloss.Left
  173. }
  174. textWidth := lipgloss.Width(text)
  175. markdownWidth := min(textWidth, width-padding-4) // -4 for the border and padding
  176. content := toMarkdown(text, markdownWidth)
  177. content = lipgloss.JoinVertical(align, content, info)
  178. switch message.Role {
  179. case client.User:
  180. return renderContentBlock(content,
  181. WithAlign(lipgloss.Right),
  182. WithBorderColor(t.Secondary()),
  183. )
  184. case client.Assistant:
  185. return renderContentBlock(content,
  186. WithAlign(lipgloss.Left),
  187. WithBorderColor(t.Primary()),
  188. )
  189. }
  190. return ""
  191. }
  192. func renderToolInvocation(
  193. toolCall client.MessageToolInvocationToolCall,
  194. result *string,
  195. metadata map[string]any,
  196. showResult bool,
  197. ) string {
  198. ignoredTools := []string{"opencode_todoread"}
  199. if slices.Contains(ignoredTools, toolCall.ToolName) {
  200. return ""
  201. }
  202. padding := 1
  203. outerWidth := layout.Current.Container.Width - 1 // subtract 1 for the border
  204. innerWidth := outerWidth - padding - 4 // -4 for the border and padding
  205. t := theme.CurrentTheme()
  206. style := styles.Muted().
  207. Width(outerWidth).
  208. PaddingLeft(padding).
  209. BorderLeft(true).
  210. BorderForeground(t.BorderSubtle()).
  211. BorderStyle(lipgloss.ThickBorder())
  212. if toolCall.State == "partial-call" {
  213. style = style.Foreground(t.TextMuted())
  214. return style.Render(renderToolAction(toolCall.ToolName))
  215. }
  216. toolArgs := ""
  217. toolArgsMap := make(map[string]any)
  218. if toolCall.Args != nil {
  219. value := *toolCall.Args
  220. m, ok := value.(map[string]any)
  221. if ok {
  222. toolArgsMap = m
  223. firstKey := ""
  224. for key := range toolArgsMap {
  225. firstKey = key
  226. break
  227. }
  228. toolArgs = renderArgs(&toolArgsMap, firstKey)
  229. }
  230. }
  231. if len(toolArgsMap) == 0 {
  232. slog.Debug("no args")
  233. }
  234. body := ""
  235. finished := result != nil && *result != ""
  236. if finished {
  237. body = *result
  238. }
  239. if metadata["error"] != nil && metadata["message"] != nil {
  240. body = styles.BaseStyle().
  241. Width(outerWidth).
  242. Foreground(t.Error()).
  243. Render(metadata["message"].(string))
  244. }
  245. elapsed := ""
  246. if metadata["time"] != nil {
  247. timeMap := metadata["time"].(map[string]any)
  248. start := timeMap["start"].(float64)
  249. end := timeMap["end"].(float64)
  250. durationMs := end - start
  251. duration := time.Duration(durationMs * float64(time.Millisecond))
  252. roundedDuration := time.Duration(duration.Round(time.Millisecond))
  253. if durationMs > 1000 {
  254. roundedDuration = time.Duration(duration.Round(time.Second))
  255. }
  256. elapsed = styles.Muted().Render(roundedDuration.String())
  257. }
  258. title := ""
  259. switch toolCall.ToolName {
  260. case "opencode_read":
  261. toolArgs = renderArgs(&toolArgsMap, "filePath")
  262. title = fmt.Sprintf("Read: %s %s", toolArgs, elapsed)
  263. body = ""
  264. filename := toolArgsMap["filePath"].(string)
  265. if metadata["preview"] != nil {
  266. body = metadata["preview"].(string)
  267. body = renderFile(filename, body, WithTruncate(6))
  268. }
  269. case "opencode_edit":
  270. filename := toolArgsMap["filePath"].(string)
  271. title = fmt.Sprintf("Edit: %s %s", relative(filename), elapsed)
  272. if metadata["diff"] != nil {
  273. patch := metadata["diff"].(string)
  274. diffWidth := min(layout.Current.Viewport.Width, 120)
  275. formattedDiff, _ := diff.FormatDiff(filename, patch, diff.WithTotalWidth(diffWidth))
  276. body = strings.TrimSpace(formattedDiff)
  277. body = lipgloss.Place(
  278. layout.Current.Viewport.Width,
  279. lipgloss.Height(body)+2,
  280. lipgloss.Center,
  281. lipgloss.Center,
  282. body,
  283. lipgloss.WithWhitespaceBackground(t.Background()),
  284. )
  285. }
  286. case "opencode_write":
  287. filename := toolArgsMap["filePath"].(string)
  288. title = fmt.Sprintf("Write: %s %s", relative(filename), elapsed)
  289. content := toolArgsMap["content"].(string)
  290. body = renderFile(filename, content)
  291. case "opencode_bash":
  292. description := toolArgsMap["description"].(string)
  293. title = fmt.Sprintf("Shell: %s %s", description, elapsed)
  294. if metadata["stdout"] != nil {
  295. command := toolArgsMap["command"].(string)
  296. stdout := metadata["stdout"].(string)
  297. body = fmt.Sprintf("```console\n> %s\n%s```", command, stdout)
  298. body = toMarkdown(body, innerWidth)
  299. body = renderContentBlock(body, WithFullWidth(), WithPaddingTop(1), WithPaddingBottom(1))
  300. }
  301. case "opencode_webfetch":
  302. title = fmt.Sprintf("Fetching: %s %s", toolArgs, elapsed)
  303. format := toolArgsMap["format"].(string)
  304. body = truncateHeight(body, 10)
  305. if format == "html" || format == "markdown" {
  306. body = toMarkdown(body, innerWidth)
  307. }
  308. body = renderContentBlock(body, WithFullWidth(), WithPaddingTop(1), WithPaddingBottom(1))
  309. case "opencode_todowrite":
  310. title = fmt.Sprintf("Planning... %s", elapsed)
  311. if finished && metadata["todos"] != nil {
  312. body = ""
  313. todos := metadata["todos"].([]any)
  314. for _, todo := range todos {
  315. t := todo.(map[string]any)
  316. content := t["content"].(string)
  317. switch t["status"].(string) {
  318. case "completed":
  319. body += fmt.Sprintf("- [x] %s\n", content)
  320. // case "in-progress":
  321. // body += fmt.Sprintf("- [ ] _%s_\n", content)
  322. default:
  323. body += fmt.Sprintf("- [ ] %s\n", content)
  324. }
  325. }
  326. body = toMarkdown(body, innerWidth)
  327. body = renderContentBlock(body, WithFullWidth(), WithPaddingTop(1), WithPaddingBottom(1))
  328. }
  329. default:
  330. toolName := renderToolName(toolCall.ToolName)
  331. title = fmt.Sprintf("%s: %s %s", toolName, toolArgs, elapsed)
  332. body = truncateHeight(body, 10)
  333. body = renderContentBlock(body, WithFullWidth(), WithPaddingTop(1), WithPaddingBottom(1))
  334. }
  335. content := style.Render(title)
  336. content = lipgloss.PlaceHorizontal(layout.Current.Viewport.Width, lipgloss.Center, content)
  337. content = styles.ForceReplaceBackgroundWithLipgloss(content, t.Background())
  338. if showResult && body != "" {
  339. content += "\n" + body
  340. }
  341. return content
  342. }
  343. func renderToolName(name string) string {
  344. switch name {
  345. // case agent.AgentToolName:
  346. // return "Task"
  347. case "opencode_ls":
  348. return "List"
  349. case "opencode_webfetch":
  350. return "Fetch"
  351. case "opencode_todoread":
  352. return "Planning"
  353. case "opencode_todowrite":
  354. return "Planning"
  355. default:
  356. normalizedName := name
  357. if strings.HasPrefix(name, "opencode_") {
  358. normalizedName = strings.TrimPrefix(name, "opencode_")
  359. }
  360. return cases.Title(language.Und).String(normalizedName)
  361. }
  362. }
  363. type fileRenderer struct {
  364. filename string
  365. content string
  366. height int
  367. }
  368. type fileRenderingOption func(*fileRenderer)
  369. func WithTruncate(height int) fileRenderingOption {
  370. return func(c *fileRenderer) {
  371. c.height = height
  372. }
  373. }
  374. func renderFile(filename string, content string, options ...fileRenderingOption) string {
  375. renderer := &fileRenderer{
  376. filename: filename,
  377. content: content,
  378. }
  379. for _, option := range options {
  380. option(renderer)
  381. }
  382. // TODO: is this even needed?
  383. lines := []string{}
  384. for line := range strings.SplitSeq(content, "\n") {
  385. line = strings.TrimRightFunc(line, unicode.IsSpace)
  386. line = strings.ReplaceAll(line, "\t", " ")
  387. lines = append(lines, line)
  388. }
  389. content = strings.Join(lines, "\n")
  390. width := layout.Current.Container.Width - 6
  391. if renderer.height > 0 {
  392. content = truncateHeight(content, renderer.height)
  393. }
  394. content = fmt.Sprintf("```%s\n%s\n```", extension(renderer.filename), content)
  395. content = toMarkdown(content, width)
  396. // ensure no line is wider than the width
  397. // truncated := []string{}
  398. // for line := range strings.SplitSeq(content, "\n") {
  399. // line = strings.TrimRightFunc(line, unicode.IsSpace)
  400. // // if lipgloss.Width(line) > width-3 {
  401. // line = ansi.Truncate(line, width-3, "")
  402. // // }
  403. // truncated = append(truncated, line)
  404. // }
  405. // content = strings.Join(truncated, "\n")
  406. return renderContentBlock(content, WithFullWidth(), WithPaddingTop(1), WithPaddingBottom(1))
  407. }
  408. func renderToolAction(name string) string {
  409. switch name {
  410. // case agent.AgentToolName:
  411. // return "Preparing prompt..."
  412. case "opencode_bash":
  413. return "Building command..."
  414. case "opencode_edit":
  415. return "Preparing edit..."
  416. case "opencode_fetch":
  417. return "Writing fetch..."
  418. case "opencode_glob":
  419. return "Finding files..."
  420. case "opencode_grep":
  421. return "Searching content..."
  422. case "opencode_ls":
  423. return "Listing directory..."
  424. case "opencode_read":
  425. return "Reading file..."
  426. case "opencode_write":
  427. return "Preparing write..."
  428. case "opencode_patch":
  429. return "Preparing patch..."
  430. case "opencode_batch":
  431. return "Running batch operations..."
  432. }
  433. return "Working..."
  434. }
  435. func renderArgs(args *map[string]any, titleKey string) string {
  436. if args == nil || len(*args) == 0 {
  437. return ""
  438. }
  439. title := ""
  440. parts := []string{}
  441. for key, value := range *args {
  442. if value == nil {
  443. continue
  444. }
  445. if key == "filePath" || key == "path" {
  446. value = relative(value.(string))
  447. }
  448. if key == titleKey {
  449. title = fmt.Sprintf("%s", value)
  450. continue
  451. }
  452. parts = append(parts, fmt.Sprintf("%s=%v", key, value))
  453. }
  454. if len(parts) == 0 {
  455. return title
  456. }
  457. return fmt.Sprintf("%s (%s)", title, strings.Join(parts, ", "))
  458. }
  459. func truncateHeight(content string, height int) string {
  460. lines := strings.Split(content, "\n")
  461. if len(lines) > height {
  462. return strings.Join(lines[:height], "\n")
  463. }
  464. return content
  465. }
  466. func relative(path string) string {
  467. return strings.TrimPrefix(path, app.Info.Path.Root+"/")
  468. }
  469. func extension(path string) string {
  470. ext := filepath.Ext(path)
  471. if ext == "" {
  472. ext = ""
  473. } else {
  474. ext = strings.ToLower(ext[1:])
  475. }
  476. return ext
  477. }