message-part.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. import { Component, createMemo, For, Match, Show, Switch } from "solid-js"
  2. import { Dynamic } from "solid-js/web"
  3. import {
  4. AssistantMessage,
  5. Message as MessageType,
  6. Part as PartType,
  7. TextPart,
  8. ToolPart,
  9. UserMessage,
  10. } from "@opencode-ai/sdk"
  11. import { BasicTool } from "./basic-tool"
  12. import { GenericTool } from "./basic-tool"
  13. import { Card } from "./card"
  14. import { Icon } from "./icon"
  15. import { Checkbox } from "./checkbox"
  16. import { Diff } from "./diff"
  17. import { DiffChanges } from "./diff-changes"
  18. import { Markdown } from "./markdown"
  19. export interface MessageProps {
  20. message: MessageType
  21. parts: PartType[]
  22. }
  23. export interface MessagePartProps {
  24. part: PartType
  25. message: MessageType
  26. hideDetails?: boolean
  27. }
  28. export type PartComponent = Component<MessagePartProps>
  29. export const PART_MAPPING: Record<string, PartComponent | undefined> = {}
  30. function getFilename(path: string) {
  31. if (!path) return ""
  32. const trimmed = path.replace(/[\/]+$/, "")
  33. const parts = trimmed.split("/")
  34. return parts[parts.length - 1] ?? ""
  35. }
  36. function getDirectory(path: string) {
  37. const parts = path.split("/")
  38. const dir = parts.slice(0, parts.length - 1).join("/")
  39. return dir ? dir + "/" : ""
  40. }
  41. export function registerPartComponent(type: string, component: PartComponent) {
  42. PART_MAPPING[type] = component
  43. }
  44. export function Message(props: MessageProps) {
  45. return (
  46. <Switch>
  47. <Match when={props.message.role === "user" && props.message}>
  48. {(userMessage) => (
  49. <UserMessageDisplay message={userMessage() as UserMessage} parts={props.parts} />
  50. )}
  51. </Match>
  52. <Match when={props.message.role === "assistant" && props.message}>
  53. {(assistantMessage) => (
  54. <AssistantMessageDisplay
  55. message={assistantMessage() as AssistantMessage}
  56. parts={props.parts}
  57. />
  58. )}
  59. </Match>
  60. </Switch>
  61. )
  62. }
  63. export function AssistantMessageDisplay(props: { message: AssistantMessage; parts: PartType[] }) {
  64. const filteredParts = createMemo(() => {
  65. return props.parts?.filter((x) => {
  66. if (x.type === "reasoning") return false
  67. return x.type !== "tool" || (x as ToolPart).tool !== "todoread"
  68. })
  69. })
  70. return <For each={filteredParts()}>{(part) => <Part part={part} message={props.message} />}</For>
  71. }
  72. export function UserMessageDisplay(props: { message: UserMessage; parts: PartType[] }) {
  73. const text = createMemo(() =>
  74. props.parts
  75. ?.filter((p) => p.type === "text" && !(p as TextPart).synthetic)
  76. ?.map((p) => (p as TextPart).text)
  77. ?.join(""),
  78. )
  79. return <div data-component="user-message">{text()}</div>
  80. }
  81. export function Part(props: MessagePartProps) {
  82. const component = createMemo(() => PART_MAPPING[props.part.type])
  83. return (
  84. <Show when={component()}>
  85. <Dynamic
  86. component={component()}
  87. part={props.part}
  88. message={props.message}
  89. hideDetails={props.hideDetails}
  90. />
  91. </Show>
  92. )
  93. }
  94. export interface ToolProps {
  95. input: Record<string, any>
  96. metadata: Record<string, any>
  97. tool: string
  98. output?: string
  99. hideDetails?: boolean
  100. }
  101. export type ToolComponent = Component<ToolProps>
  102. const state: Record<
  103. string,
  104. {
  105. name: string
  106. render?: ToolComponent
  107. }
  108. > = {}
  109. export function registerTool(input: { name: string; render?: ToolComponent }) {
  110. state[input.name] = input
  111. return input
  112. }
  113. export function getTool(name: string) {
  114. return state[name]?.render
  115. }
  116. export const ToolRegistry = {
  117. register: registerTool,
  118. render: getTool,
  119. }
  120. PART_MAPPING["tool"] = function ToolPartDisplay(props) {
  121. const part = props.part as ToolPart
  122. const component = createMemo(() => {
  123. const render = ToolRegistry.render(part.tool) ?? GenericTool
  124. const metadata = part.state.status === "pending" ? {} : (part.state.metadata ?? {})
  125. const input = part.state.status === "completed" ? part.state.input : {}
  126. return (
  127. <Switch>
  128. <Match when={part.state.status === "error" && part.state.error}>
  129. {(error) => {
  130. const cleaned = error().replace("Error: ", "")
  131. const [title, ...rest] = cleaned.split(": ")
  132. return (
  133. <Card variant="error">
  134. <div data-component="tool-error">
  135. <Icon name="circle-ban-sign" size="small" data-slot="tool-error-icon" />
  136. <Switch>
  137. <Match when={title && title.length < 30}>
  138. <div data-slot="tool-error-content">
  139. <div data-slot="tool-error-title">{title}</div>
  140. <span data-slot="tool-error-message">{rest.join(": ")}</span>
  141. </div>
  142. </Match>
  143. <Match when={true}>
  144. <span data-slot="tool-error-message">{cleaned}</span>
  145. </Match>
  146. </Switch>
  147. </div>
  148. </Card>
  149. )
  150. }}
  151. </Match>
  152. <Match when={true}>
  153. <Dynamic
  154. component={render}
  155. input={input}
  156. tool={part.tool}
  157. metadata={metadata}
  158. output={part.state.status === "completed" ? part.state.output : undefined}
  159. hideDetails={props.hideDetails}
  160. />
  161. </Match>
  162. </Switch>
  163. )
  164. })
  165. return <Show when={component()}>{component()}</Show>
  166. }
  167. PART_MAPPING["text"] = function TextPartDisplay(props) {
  168. const part = props.part as TextPart
  169. return (
  170. <Show when={part.text.trim()}>
  171. <div data-component="text-part">
  172. <Markdown text={part.text.trim()} />
  173. </div>
  174. </Show>
  175. )
  176. }
  177. PART_MAPPING["reasoning"] = function ReasoningPartDisplay(props) {
  178. const part = props.part as any
  179. return (
  180. <Show when={part.text.trim()}>
  181. <div data-component="reasoning-part">
  182. <Markdown text={part.text.trim()} />
  183. </div>
  184. </Show>
  185. )
  186. }
  187. ToolRegistry.register({
  188. name: "read",
  189. render(props) {
  190. return (
  191. <BasicTool
  192. icon="glasses"
  193. trigger={{
  194. title: "Read",
  195. subtitle: props.input.filePath ? getFilename(props.input.filePath) : "",
  196. }}
  197. />
  198. )
  199. },
  200. })
  201. ToolRegistry.register({
  202. name: "list",
  203. render(props) {
  204. return (
  205. <BasicTool
  206. icon="bullet-list"
  207. trigger={{ title: "List", subtitle: getDirectory(props.input.path || "/") }}
  208. >
  209. <Show when={false && props.output}>
  210. <div data-component="tool-output">{props.output}</div>
  211. </Show>
  212. </BasicTool>
  213. )
  214. },
  215. })
  216. ToolRegistry.register({
  217. name: "glob",
  218. render(props) {
  219. return (
  220. <BasicTool
  221. icon="magnifying-glass-menu"
  222. trigger={{
  223. title: "Glob",
  224. subtitle: getDirectory(props.input.path || "/"),
  225. args: props.input.pattern ? ["pattern=" + props.input.pattern] : [],
  226. }}
  227. >
  228. <Show when={false && props.output}>
  229. <div data-component="tool-output">{props.output}</div>
  230. </Show>
  231. </BasicTool>
  232. )
  233. },
  234. })
  235. ToolRegistry.register({
  236. name: "grep",
  237. render(props) {
  238. const args = []
  239. if (props.input.pattern) args.push("pattern=" + props.input.pattern)
  240. if (props.input.include) args.push("include=" + props.input.include)
  241. return (
  242. <BasicTool
  243. icon="magnifying-glass-menu"
  244. trigger={{
  245. title: "Grep",
  246. subtitle: getDirectory(props.input.path || "/"),
  247. args,
  248. }}
  249. >
  250. <Show when={false && props.output}>
  251. <div data-component="tool-output">{props.output}</div>
  252. </Show>
  253. </BasicTool>
  254. )
  255. },
  256. })
  257. ToolRegistry.register({
  258. name: "webfetch",
  259. render(props) {
  260. return (
  261. <BasicTool
  262. icon="window-cursor"
  263. trigger={{
  264. title: "Webfetch",
  265. subtitle: props.input.url || "",
  266. args: props.input.format ? ["format=" + props.input.format] : [],
  267. action: (
  268. <div data-component="tool-action">
  269. <Icon name="square-arrow-top-right" size="small" />
  270. </div>
  271. ),
  272. }}
  273. >
  274. <Show when={false && props.output}>
  275. <div data-component="tool-output">{props.output}</div>
  276. </Show>
  277. </BasicTool>
  278. )
  279. },
  280. })
  281. ToolRegistry.register({
  282. name: "task",
  283. render(props) {
  284. return (
  285. <BasicTool
  286. icon="task"
  287. trigger={{
  288. title: `${props.input.subagent_type || props.tool} Agent`,
  289. titleClass: "capitalize",
  290. subtitle: props.input.description,
  291. }}
  292. >
  293. <Show when={false && props.output}>
  294. <div data-component="tool-output">{props.output}</div>
  295. </Show>
  296. </BasicTool>
  297. )
  298. },
  299. })
  300. ToolRegistry.register({
  301. name: "bash",
  302. render(props) {
  303. return (
  304. <BasicTool
  305. icon="console"
  306. trigger={{
  307. title: "Shell",
  308. subtitle: "Ran " + props.input.command,
  309. }}
  310. >
  311. <Show when={false && props.output}>
  312. <div data-component="tool-output">{props.output}</div>
  313. </Show>
  314. </BasicTool>
  315. )
  316. },
  317. })
  318. ToolRegistry.register({
  319. name: "edit",
  320. render(props) {
  321. return (
  322. <BasicTool
  323. icon="code-lines"
  324. trigger={
  325. <div data-component="edit-trigger">
  326. <div data-slot="title-area">
  327. <div data-slot="title">Edit</div>
  328. <div data-slot="path">
  329. <Show when={props.input.filePath?.includes("/")}>
  330. <span data-slot="directory">{getDirectory(props.input.filePath!)}</span>
  331. </Show>
  332. <span data-slot="filename">{getFilename(props.input.filePath ?? "")}</span>
  333. </div>
  334. </div>
  335. <div data-slot="actions">
  336. <Show when={props.metadata.filediff}>
  337. <DiffChanges changes={props.metadata.filediff} />
  338. </Show>
  339. </div>
  340. </div>
  341. }
  342. >
  343. <Show when={props.metadata.filediff}>
  344. <div data-component="edit-content">
  345. <Diff
  346. before={{
  347. name: getFilename(props.metadata.filediff.path),
  348. contents: props.metadata.filediff.before,
  349. }}
  350. after={{
  351. name: getFilename(props.metadata.filediff.path),
  352. contents: props.metadata.filediff.after,
  353. }}
  354. />
  355. </div>
  356. </Show>
  357. </BasicTool>
  358. )
  359. },
  360. })
  361. ToolRegistry.register({
  362. name: "write",
  363. render(props) {
  364. return (
  365. <BasicTool
  366. icon="code-lines"
  367. trigger={
  368. <div data-component="write-trigger">
  369. <div data-slot="title-area">
  370. <div data-slot="title">Write</div>
  371. <div data-slot="path">
  372. <Show when={props.input.filePath?.includes("/")}>
  373. <span data-slot="directory">{getDirectory(props.input.filePath!)}</span>
  374. </Show>
  375. <span data-slot="filename">{getFilename(props.input.filePath ?? "")}</span>
  376. </div>
  377. </div>
  378. <div data-slot="actions">{/* <DiffChanges diff={diff} /> */}</div>
  379. </div>
  380. }
  381. >
  382. <Show when={false && props.output}>
  383. <div data-component="tool-output">{props.output}</div>
  384. </Show>
  385. </BasicTool>
  386. )
  387. },
  388. })
  389. ToolRegistry.register({
  390. name: "todowrite",
  391. render(props) {
  392. return (
  393. <BasicTool
  394. icon="checklist"
  395. trigger={{
  396. title: "To-dos",
  397. subtitle: `${props.input.todos?.filter((t: any) => t.status === "completed").length}/${props.input.todos?.length}`,
  398. }}
  399. >
  400. <Show when={props.input.todos?.length}>
  401. <div data-component="todos">
  402. <For each={props.input.todos}>
  403. {(todo: any) => (
  404. <Checkbox readOnly checked={todo.status === "completed"}>
  405. <div data-slot="todo-content" data-completed={todo.status === "completed"}>
  406. {todo.content}
  407. </div>
  408. </Checkbox>
  409. )}
  410. </For>
  411. </div>
  412. </Show>
  413. </BasicTool>
  414. )
  415. },
  416. })