message-part.tsx 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. import {
  2. Component,
  3. createEffect,
  4. createMemo,
  5. createSignal,
  6. For,
  7. Match,
  8. Show,
  9. Switch,
  10. onCleanup,
  11. type JSX,
  12. } from "solid-js"
  13. import stripAnsi from "strip-ansi"
  14. import { Dynamic } from "solid-js/web"
  15. import {
  16. AgentPart,
  17. AssistantMessage,
  18. FilePart,
  19. Message as MessageType,
  20. Part as PartType,
  21. ReasoningPart,
  22. TextPart,
  23. ToolPart,
  24. UserMessage,
  25. Todo,
  26. QuestionRequest,
  27. QuestionAnswer,
  28. QuestionInfo,
  29. } from "@opencode-ai/sdk/v2"
  30. import { createStore } from "solid-js/store"
  31. import { useData } from "../context"
  32. import { useDiffComponent } from "../context/diff"
  33. import { useCodeComponent } from "../context/code"
  34. import { useDialog } from "../context/dialog"
  35. import { BasicTool } from "./basic-tool"
  36. import { GenericTool } from "./basic-tool"
  37. import { Button } from "./button"
  38. import { Card } from "./card"
  39. import { Icon } from "./icon"
  40. import { Checkbox } from "./checkbox"
  41. import { DiffChanges } from "./diff-changes"
  42. import { Markdown } from "./markdown"
  43. import { ImagePreview } from "./image-preview"
  44. import { getDirectory as _getDirectory, getFilename } from "@opencode-ai/util/path"
  45. import { checksum } from "@opencode-ai/util/encode"
  46. import { Tooltip } from "./tooltip"
  47. import { IconButton } from "./icon-button"
  48. import { createAutoScroll } from "../hooks"
  49. import { createResizeObserver } from "@solid-primitives/resize-observer"
  50. interface Diagnostic {
  51. range: {
  52. start: { line: number; character: number }
  53. end: { line: number; character: number }
  54. }
  55. message: string
  56. severity?: number
  57. }
  58. function getDiagnostics(
  59. diagnosticsByFile: Record<string, Diagnostic[]> | undefined,
  60. filePath: string | undefined,
  61. ): Diagnostic[] {
  62. if (!diagnosticsByFile || !filePath) return []
  63. const diagnostics = diagnosticsByFile[filePath] ?? []
  64. return diagnostics.filter((d) => d.severity === 1).slice(0, 3)
  65. }
  66. function DiagnosticsDisplay(props: { diagnostics: Diagnostic[] }): JSX.Element {
  67. return (
  68. <Show when={props.diagnostics.length > 0}>
  69. <div data-component="diagnostics">
  70. <For each={props.diagnostics}>
  71. {(diagnostic) => (
  72. <div data-slot="diagnostic">
  73. <span data-slot="diagnostic-label">Error</span>
  74. <span data-slot="diagnostic-location">
  75. [{diagnostic.range.start.line + 1}:{diagnostic.range.start.character + 1}]
  76. </span>
  77. <span data-slot="diagnostic-message">{diagnostic.message}</span>
  78. </div>
  79. )}
  80. </For>
  81. </div>
  82. </Show>
  83. )
  84. }
  85. export interface MessageProps {
  86. message: MessageType
  87. parts: PartType[]
  88. }
  89. export interface MessagePartProps {
  90. part: PartType
  91. message: MessageType
  92. hideDetails?: boolean
  93. defaultOpen?: boolean
  94. }
  95. export type PartComponent = Component<MessagePartProps>
  96. export const PART_MAPPING: Record<string, PartComponent | undefined> = {}
  97. const TEXT_RENDER_THROTTLE_MS = 100
  98. function same<T>(a: readonly T[], b: readonly T[]) {
  99. if (a === b) return true
  100. if (a.length !== b.length) return false
  101. return a.every((x, i) => x === b[i])
  102. }
  103. function createThrottledValue(getValue: () => string) {
  104. const [value, setValue] = createSignal(getValue())
  105. let timeout: ReturnType<typeof setTimeout> | undefined
  106. let last = 0
  107. createEffect(() => {
  108. const next = getValue()
  109. const now = Date.now()
  110. const remaining = TEXT_RENDER_THROTTLE_MS - (now - last)
  111. if (remaining <= 0) {
  112. if (timeout) {
  113. clearTimeout(timeout)
  114. timeout = undefined
  115. }
  116. last = now
  117. setValue(next)
  118. return
  119. }
  120. if (timeout) clearTimeout(timeout)
  121. timeout = setTimeout(() => {
  122. last = Date.now()
  123. setValue(next)
  124. timeout = undefined
  125. }, remaining)
  126. })
  127. onCleanup(() => {
  128. if (timeout) clearTimeout(timeout)
  129. })
  130. return value
  131. }
  132. function relativizeProjectPaths(text: string, directory?: string) {
  133. if (!text) return ""
  134. if (!directory) return text
  135. return text.split(directory).join("")
  136. }
  137. function getDirectory(path: string | undefined) {
  138. const data = useData()
  139. return relativizeProjectPaths(_getDirectory(path), data.directory)
  140. }
  141. export function getSessionToolParts(store: ReturnType<typeof useData>["store"], sessionId: string): ToolPart[] {
  142. const messages = store.message[sessionId]?.filter((m) => m.role === "assistant")
  143. if (!messages) return []
  144. const parts: ToolPart[] = []
  145. for (const m of messages) {
  146. const msgParts = store.part[m.id]
  147. if (msgParts) {
  148. for (const p of msgParts) {
  149. if (p && p.type === "tool") parts.push(p as ToolPart)
  150. }
  151. }
  152. }
  153. return parts
  154. }
  155. import type { IconProps } from "./icon"
  156. export type ToolInfo = {
  157. icon: IconProps["name"]
  158. title: string
  159. subtitle?: string
  160. }
  161. export function getToolInfo(tool: string, input: any = {}): ToolInfo {
  162. switch (tool) {
  163. case "read":
  164. return {
  165. icon: "glasses",
  166. title: "Read",
  167. subtitle: input.filePath ? getFilename(input.filePath) : undefined,
  168. }
  169. case "list":
  170. return {
  171. icon: "bullet-list",
  172. title: "List",
  173. subtitle: input.path ? getFilename(input.path) : undefined,
  174. }
  175. case "glob":
  176. return {
  177. icon: "magnifying-glass-menu",
  178. title: "Glob",
  179. subtitle: input.pattern,
  180. }
  181. case "grep":
  182. return {
  183. icon: "magnifying-glass-menu",
  184. title: "Grep",
  185. subtitle: input.pattern,
  186. }
  187. case "webfetch":
  188. return {
  189. icon: "window-cursor",
  190. title: "Webfetch",
  191. subtitle: input.url,
  192. }
  193. case "task":
  194. return {
  195. icon: "task",
  196. title: `${input.subagent_type || "task"} Agent`,
  197. subtitle: input.description,
  198. }
  199. case "bash":
  200. return {
  201. icon: "console",
  202. title: "Shell",
  203. subtitle: input.description,
  204. }
  205. case "edit":
  206. return {
  207. icon: "code-lines",
  208. title: "Edit",
  209. subtitle: input.filePath ? getFilename(input.filePath) : undefined,
  210. }
  211. case "write":
  212. return {
  213. icon: "code-lines",
  214. title: "Write",
  215. subtitle: input.filePath ? getFilename(input.filePath) : undefined,
  216. }
  217. case "apply_patch":
  218. return {
  219. icon: "code-lines",
  220. title: "Patch",
  221. subtitle: input.files?.length ? `${input.files.length} file${input.files.length > 1 ? "s" : ""}` : undefined,
  222. }
  223. case "todowrite":
  224. return {
  225. icon: "checklist",
  226. title: "To-dos",
  227. }
  228. case "todoread":
  229. return {
  230. icon: "checklist",
  231. title: "Read to-dos",
  232. }
  233. case "question":
  234. return {
  235. icon: "bubble-5",
  236. title: "Questions",
  237. }
  238. default:
  239. return {
  240. icon: "mcp",
  241. title: tool,
  242. }
  243. }
  244. }
  245. export function registerPartComponent(type: string, component: PartComponent) {
  246. PART_MAPPING[type] = component
  247. }
  248. export function Message(props: MessageProps) {
  249. return (
  250. <Switch>
  251. <Match when={props.message.role === "user" && props.message}>
  252. {(userMessage) => <UserMessageDisplay message={userMessage() as UserMessage} parts={props.parts} />}
  253. </Match>
  254. <Match when={props.message.role === "assistant" && props.message}>
  255. {(assistantMessage) => (
  256. <AssistantMessageDisplay message={assistantMessage() as AssistantMessage} parts={props.parts} />
  257. )}
  258. </Match>
  259. </Switch>
  260. )
  261. }
  262. export function AssistantMessageDisplay(props: { message: AssistantMessage; parts: PartType[] }) {
  263. const emptyParts: PartType[] = []
  264. const filteredParts = createMemo(
  265. () =>
  266. props.parts.filter((x) => {
  267. return x.type !== "tool" || (x as ToolPart).tool !== "todoread"
  268. }),
  269. emptyParts,
  270. { equals: same },
  271. )
  272. return <For each={filteredParts()}>{(part) => <Part part={part} message={props.message} />}</For>
  273. }
  274. export function UserMessageDisplay(props: { message: UserMessage; parts: PartType[] }) {
  275. const dialog = useDialog()
  276. const [copied, setCopied] = createSignal(false)
  277. const [expanded, setExpanded] = createSignal(false)
  278. const [canExpand, setCanExpand] = createSignal(false)
  279. let textRef: HTMLDivElement | undefined
  280. const updateCanExpand = () => {
  281. const el = textRef
  282. if (!el) return
  283. if (expanded()) return
  284. setCanExpand(el.scrollHeight > el.clientHeight + 2)
  285. }
  286. createResizeObserver(
  287. () => textRef,
  288. () => {
  289. updateCanExpand()
  290. },
  291. )
  292. const textPart = createMemo(
  293. () => props.parts?.find((p) => p.type === "text" && !(p as TextPart).synthetic) as TextPart | undefined,
  294. )
  295. const text = createMemo(() => textPart()?.text || "")
  296. createEffect(() => {
  297. text()
  298. updateCanExpand()
  299. })
  300. const files = createMemo(() => (props.parts?.filter((p) => p.type === "file") as FilePart[]) ?? [])
  301. const attachments = createMemo(() =>
  302. files()?.filter((f) => {
  303. const mime = f.mime
  304. return mime.startsWith("image/") || mime === "application/pdf"
  305. }),
  306. )
  307. const inlineFiles = createMemo(() =>
  308. files().filter((f) => {
  309. const mime = f.mime
  310. return !mime.startsWith("image/") && mime !== "application/pdf" && f.source?.text?.start !== undefined
  311. }),
  312. )
  313. const agents = createMemo(() => (props.parts?.filter((p) => p.type === "agent") as AgentPart[]) ?? [])
  314. const openImagePreview = (url: string, alt?: string) => {
  315. dialog.show(() => <ImagePreview src={url} alt={alt} />)
  316. }
  317. const handleCopy = async () => {
  318. const content = text()
  319. if (!content) return
  320. await navigator.clipboard.writeText(content)
  321. setCopied(true)
  322. setTimeout(() => setCopied(false), 2000)
  323. }
  324. const toggleExpanded = () => {
  325. if (!canExpand()) return
  326. setExpanded((value) => !value)
  327. }
  328. return (
  329. <div data-component="user-message" data-expanded={expanded()} data-can-expand={canExpand()}>
  330. <Show when={attachments().length > 0}>
  331. <div data-slot="user-message-attachments">
  332. <For each={attachments()}>
  333. {(file) => (
  334. <div
  335. data-slot="user-message-attachment"
  336. data-type={file.mime.startsWith("image/") ? "image" : "file"}
  337. onClick={() => {
  338. if (file.mime.startsWith("image/") && file.url) {
  339. openImagePreview(file.url, file.filename)
  340. }
  341. }}
  342. >
  343. <Show
  344. when={file.mime.startsWith("image/") && file.url}
  345. fallback={
  346. <div data-slot="user-message-attachment-icon">
  347. <Icon name="folder" />
  348. </div>
  349. }
  350. >
  351. <img data-slot="user-message-attachment-image" src={file.url} alt={file.filename ?? "attachment"} />
  352. </Show>
  353. </div>
  354. )}
  355. </For>
  356. </div>
  357. </Show>
  358. <Show when={text()}>
  359. <div data-slot="user-message-text" ref={(el) => (textRef = el)} onClick={toggleExpanded}>
  360. <HighlightedText text={text()} references={inlineFiles()} agents={agents()} />
  361. <button
  362. data-slot="user-message-expand"
  363. type="button"
  364. aria-label={expanded() ? "Collapse message" : "Expand message"}
  365. onClick={(event) => {
  366. event.stopPropagation()
  367. toggleExpanded()
  368. }}
  369. >
  370. <Icon name="chevron-down" size="small" />
  371. </button>
  372. <div data-slot="user-message-copy-wrapper">
  373. <Tooltip value={copied() ? "Copied!" : "Copy"} placement="top" gutter={8}>
  374. <IconButton
  375. icon={copied() ? "check" : "copy"}
  376. variant="secondary"
  377. onClick={(event) => {
  378. event.stopPropagation()
  379. handleCopy()
  380. }}
  381. />
  382. </Tooltip>
  383. </div>
  384. </div>
  385. </Show>
  386. </div>
  387. )
  388. }
  389. type HighlightSegment = { text: string; type?: "file" | "agent" }
  390. function HighlightedText(props: { text: string; references: FilePart[]; agents: AgentPart[] }) {
  391. const segments = createMemo(() => {
  392. const text = props.text
  393. const allRefs: { start: number; end: number; type: "file" | "agent" }[] = [
  394. ...props.references
  395. .filter((r) => r.source?.text?.start !== undefined && r.source?.text?.end !== undefined)
  396. .map((r) => ({ start: r.source!.text!.start, end: r.source!.text!.end, type: "file" as const })),
  397. ...props.agents
  398. .filter((a) => a.source?.start !== undefined && a.source?.end !== undefined)
  399. .map((a) => ({ start: a.source!.start, end: a.source!.end, type: "agent" as const })),
  400. ].sort((a, b) => a.start - b.start)
  401. const result: HighlightSegment[] = []
  402. let lastIndex = 0
  403. for (const ref of allRefs) {
  404. if (ref.start < lastIndex) continue
  405. if (ref.start > lastIndex) {
  406. result.push({ text: text.slice(lastIndex, ref.start) })
  407. }
  408. result.push({ text: text.slice(ref.start, ref.end), type: ref.type })
  409. lastIndex = ref.end
  410. }
  411. if (lastIndex < text.length) {
  412. result.push({ text: text.slice(lastIndex) })
  413. }
  414. return result
  415. })
  416. return (
  417. <For each={segments()}>
  418. {(segment) => (
  419. <span
  420. classList={{
  421. "text-syntax-property": segment.type === "file",
  422. "text-syntax-type": segment.type === "agent",
  423. }}
  424. >
  425. {segment.text}
  426. </span>
  427. )}
  428. </For>
  429. )
  430. }
  431. export function Part(props: MessagePartProps) {
  432. const component = createMemo(() => PART_MAPPING[props.part.type])
  433. return (
  434. <Show when={component()}>
  435. <Dynamic
  436. component={component()}
  437. part={props.part}
  438. message={props.message}
  439. hideDetails={props.hideDetails}
  440. defaultOpen={props.defaultOpen}
  441. />
  442. </Show>
  443. )
  444. }
  445. export interface ToolProps {
  446. input: Record<string, any>
  447. metadata: Record<string, any>
  448. tool: string
  449. output?: string
  450. status?: string
  451. hideDetails?: boolean
  452. defaultOpen?: boolean
  453. forceOpen?: boolean
  454. locked?: boolean
  455. }
  456. export type ToolComponent = Component<ToolProps>
  457. const state: Record<
  458. string,
  459. {
  460. name: string
  461. render?: ToolComponent
  462. }
  463. > = {}
  464. export function registerTool(input: { name: string; render?: ToolComponent }) {
  465. state[input.name] = input
  466. return input
  467. }
  468. export function getTool(name: string) {
  469. return state[name]?.render
  470. }
  471. export const ToolRegistry = {
  472. register: registerTool,
  473. render: getTool,
  474. }
  475. PART_MAPPING["tool"] = function ToolPartDisplay(props) {
  476. const data = useData()
  477. const part = props.part as ToolPart
  478. const permission = createMemo(() => {
  479. const next = data.store.permission?.[props.message.sessionID]?.[0]
  480. if (!next || !next.tool) return undefined
  481. if (next.tool!.callID !== part.callID) return undefined
  482. return next
  483. })
  484. const questionRequest = createMemo(() => {
  485. const next = data.store.question?.[props.message.sessionID]?.[0]
  486. if (!next || !next.tool) return undefined
  487. if (next.tool!.callID !== part.callID) return undefined
  488. return next
  489. })
  490. const [showPermission, setShowPermission] = createSignal(false)
  491. const [showQuestion, setShowQuestion] = createSignal(false)
  492. createEffect(() => {
  493. const perm = permission()
  494. if (perm) {
  495. const timeout = setTimeout(() => setShowPermission(true), 50)
  496. onCleanup(() => clearTimeout(timeout))
  497. } else {
  498. setShowPermission(false)
  499. }
  500. })
  501. createEffect(() => {
  502. const question = questionRequest()
  503. if (question) {
  504. const timeout = setTimeout(() => setShowQuestion(true), 50)
  505. onCleanup(() => clearTimeout(timeout))
  506. } else {
  507. setShowQuestion(false)
  508. }
  509. })
  510. const [forceOpen, setForceOpen] = createSignal(false)
  511. createEffect(() => {
  512. if (permission() || questionRequest()) setForceOpen(true)
  513. })
  514. const respond = (response: "once" | "always" | "reject") => {
  515. const perm = permission()
  516. if (!perm || !data.respondToPermission) return
  517. data.respondToPermission({
  518. sessionID: perm.sessionID,
  519. permissionID: perm.id,
  520. response,
  521. })
  522. }
  523. const emptyInput: Record<string, any> = {}
  524. const emptyMetadata: Record<string, any> = {}
  525. const input = () => part.state?.input ?? emptyInput
  526. // @ts-expect-error
  527. const metadata = () => part.state?.metadata ?? emptyMetadata
  528. const render = ToolRegistry.render(part.tool) ?? GenericTool
  529. return (
  530. <div data-component="tool-part-wrapper" data-permission={showPermission()} data-question={showQuestion()}>
  531. <Switch>
  532. <Match when={part.state.status === "error" && part.state.error}>
  533. {(error) => {
  534. const cleaned = error().replace("Error: ", "")
  535. const [title, ...rest] = cleaned.split(": ")
  536. return (
  537. <Card variant="error">
  538. <div data-component="tool-error">
  539. <Icon name="circle-ban-sign" size="small" />
  540. <Switch>
  541. <Match when={title && title.length < 30}>
  542. <div data-slot="message-part-tool-error-content">
  543. <div data-slot="message-part-tool-error-title">{title}</div>
  544. <span data-slot="message-part-tool-error-message">{rest.join(": ")}</span>
  545. </div>
  546. </Match>
  547. <Match when={true}>
  548. <span data-slot="message-part-tool-error-message">{cleaned}</span>
  549. </Match>
  550. </Switch>
  551. </div>
  552. </Card>
  553. )
  554. }}
  555. </Match>
  556. <Match when={true}>
  557. <Dynamic
  558. component={render}
  559. input={input()}
  560. tool={part.tool}
  561. metadata={metadata()}
  562. // @ts-expect-error
  563. output={part.state.output}
  564. status={part.state.status}
  565. hideDetails={props.hideDetails}
  566. forceOpen={forceOpen()}
  567. locked={showPermission() || showQuestion()}
  568. defaultOpen={props.defaultOpen}
  569. />
  570. </Match>
  571. </Switch>
  572. <Show when={showPermission() && permission()}>
  573. <div data-component="permission-prompt">
  574. <div data-slot="permission-actions">
  575. <Button variant="ghost" size="small" onClick={() => respond("reject")}>
  576. Deny
  577. </Button>
  578. <Button variant="secondary" size="small" onClick={() => respond("always")}>
  579. Allow always
  580. </Button>
  581. <Button variant="primary" size="small" onClick={() => respond("once")}>
  582. Allow once
  583. </Button>
  584. </div>
  585. </div>
  586. </Show>
  587. <Show when={showQuestion() && questionRequest()}>{(request) => <QuestionPrompt request={request()} />}</Show>
  588. </div>
  589. )
  590. }
  591. PART_MAPPING["text"] = function TextPartDisplay(props) {
  592. const data = useData()
  593. const part = props.part as TextPart
  594. const displayText = () => relativizeProjectPaths((part.text ?? "").trim(), data.directory)
  595. const throttledText = createThrottledValue(displayText)
  596. return (
  597. <Show when={throttledText()}>
  598. <div data-component="text-part">
  599. <Markdown text={throttledText()} cacheKey={part.id} />
  600. </div>
  601. </Show>
  602. )
  603. }
  604. PART_MAPPING["reasoning"] = function ReasoningPartDisplay(props) {
  605. const part = props.part as ReasoningPart
  606. const text = () => part.text.trim()
  607. const throttledText = createThrottledValue(text)
  608. return (
  609. <Show when={throttledText()}>
  610. <div data-component="reasoning-part">
  611. <Markdown text={throttledText()} cacheKey={part.id} />
  612. </div>
  613. </Show>
  614. )
  615. }
  616. ToolRegistry.register({
  617. name: "read",
  618. render(props) {
  619. const args: string[] = []
  620. if (props.input.offset) args.push("offset=" + props.input.offset)
  621. if (props.input.limit) args.push("limit=" + props.input.limit)
  622. return (
  623. <BasicTool
  624. {...props}
  625. icon="glasses"
  626. trigger={{
  627. title: "Read",
  628. subtitle: props.input.filePath ? getFilename(props.input.filePath) : "",
  629. args,
  630. }}
  631. />
  632. )
  633. },
  634. })
  635. ToolRegistry.register({
  636. name: "list",
  637. render(props) {
  638. return (
  639. <BasicTool
  640. {...props}
  641. icon="bullet-list"
  642. trigger={{ title: "List", subtitle: getDirectory(props.input.path || "/") }}
  643. >
  644. <Show when={props.output}>
  645. {(output) => (
  646. <div data-component="tool-output" data-scrollable>
  647. <Markdown text={output()} />
  648. </div>
  649. )}
  650. </Show>
  651. </BasicTool>
  652. )
  653. },
  654. })
  655. ToolRegistry.register({
  656. name: "glob",
  657. render(props) {
  658. return (
  659. <BasicTool
  660. {...props}
  661. icon="magnifying-glass-menu"
  662. trigger={{
  663. title: "Glob",
  664. subtitle: getDirectory(props.input.path || "/"),
  665. args: props.input.pattern ? ["pattern=" + props.input.pattern] : [],
  666. }}
  667. >
  668. <Show when={props.output}>
  669. {(output) => (
  670. <div data-component="tool-output" data-scrollable>
  671. <Markdown text={output()} />
  672. </div>
  673. )}
  674. </Show>
  675. </BasicTool>
  676. )
  677. },
  678. })
  679. ToolRegistry.register({
  680. name: "grep",
  681. render(props) {
  682. const args: string[] = []
  683. if (props.input.pattern) args.push("pattern=" + props.input.pattern)
  684. if (props.input.include) args.push("include=" + props.input.include)
  685. return (
  686. <BasicTool
  687. {...props}
  688. icon="magnifying-glass-menu"
  689. trigger={{
  690. title: "Grep",
  691. subtitle: getDirectory(props.input.path || "/"),
  692. args,
  693. }}
  694. >
  695. <Show when={props.output}>
  696. {(output) => (
  697. <div data-component="tool-output" data-scrollable>
  698. <Markdown text={output()} />
  699. </div>
  700. )}
  701. </Show>
  702. </BasicTool>
  703. )
  704. },
  705. })
  706. ToolRegistry.register({
  707. name: "webfetch",
  708. render(props) {
  709. return (
  710. <BasicTool
  711. {...props}
  712. icon="window-cursor"
  713. trigger={{
  714. title: "Webfetch",
  715. subtitle: props.input.url || "",
  716. args: props.input.format ? ["format=" + props.input.format] : [],
  717. action: (
  718. <div data-component="tool-action">
  719. <Icon name="square-arrow-top-right" size="small" />
  720. </div>
  721. ),
  722. }}
  723. >
  724. <Show when={props.output}>
  725. {(output) => (
  726. <div data-component="tool-output" data-scrollable>
  727. <Markdown text={output()} />
  728. </div>
  729. )}
  730. </Show>
  731. </BasicTool>
  732. )
  733. },
  734. })
  735. ToolRegistry.register({
  736. name: "task",
  737. render(props) {
  738. const data = useData()
  739. const summary = () =>
  740. (props.metadata.summary ?? []) as { id: string; tool: string; state: { status: string; title?: string } }[]
  741. const autoScroll = createAutoScroll({
  742. working: () => true,
  743. overflowAnchor: "auto",
  744. })
  745. const childSessionId = () => props.metadata.sessionId as string | undefined
  746. const childPermission = createMemo(() => {
  747. const sessionId = childSessionId()
  748. if (!sessionId) return undefined
  749. const permissions = data.store.permission?.[sessionId] ?? []
  750. return permissions[0]
  751. })
  752. const childToolPart = createMemo(() => {
  753. const perm = childPermission()
  754. if (!perm || !perm.tool) return undefined
  755. const sessionId = childSessionId()
  756. if (!sessionId) return undefined
  757. // Find the tool part that matches the permission's callID
  758. const messages = data.store.message[sessionId] ?? []
  759. const message = messages.findLast((m) => m.id === perm.tool!.messageID)
  760. if (!message) return undefined
  761. const parts = data.store.part[message.id] ?? []
  762. for (const part of parts) {
  763. if (part.type === "tool" && (part as ToolPart).callID === perm.tool!.callID) {
  764. return { part: part as ToolPart, message }
  765. }
  766. }
  767. return undefined
  768. })
  769. const respond = (response: "once" | "always" | "reject") => {
  770. const perm = childPermission()
  771. if (!perm || !data.respondToPermission) return
  772. data.respondToPermission({
  773. sessionID: perm.sessionID,
  774. permissionID: perm.id,
  775. response,
  776. })
  777. }
  778. const handleSubtitleClick = () => {
  779. const sessionId = childSessionId()
  780. if (sessionId && data.navigateToSession) {
  781. data.navigateToSession(sessionId)
  782. }
  783. }
  784. const renderChildToolPart = () => {
  785. const toolData = childToolPart()
  786. if (!toolData) return null
  787. const { part } = toolData
  788. const render = ToolRegistry.render(part.tool) ?? GenericTool
  789. // @ts-expect-error
  790. const metadata = part.state?.metadata ?? {}
  791. const input = part.state?.input ?? {}
  792. return (
  793. <Dynamic
  794. component={render}
  795. input={input}
  796. tool={part.tool}
  797. metadata={metadata}
  798. // @ts-expect-error
  799. output={part.state.output}
  800. status={part.state.status}
  801. defaultOpen={true}
  802. />
  803. )
  804. }
  805. return (
  806. <div data-component="tool-part-wrapper" data-permission={!!childPermission()}>
  807. <Switch>
  808. <Match when={childPermission()}>
  809. <>
  810. <Show
  811. when={childToolPart()}
  812. fallback={
  813. <BasicTool
  814. icon="task"
  815. defaultOpen={true}
  816. trigger={{
  817. title: `${props.input.subagent_type || props.tool} Agent`,
  818. titleClass: "capitalize",
  819. subtitle: props.input.description,
  820. }}
  821. onSubtitleClick={handleSubtitleClick}
  822. />
  823. }
  824. >
  825. {renderChildToolPart()}
  826. </Show>
  827. <div data-component="permission-prompt">
  828. <div data-slot="permission-actions">
  829. <Button variant="ghost" size="small" onClick={() => respond("reject")}>
  830. Deny
  831. </Button>
  832. <Button variant="secondary" size="small" onClick={() => respond("always")}>
  833. Allow always
  834. </Button>
  835. <Button variant="primary" size="small" onClick={() => respond("once")}>
  836. Allow once
  837. </Button>
  838. </div>
  839. </div>
  840. </>
  841. </Match>
  842. <Match when={true}>
  843. <BasicTool
  844. icon="task"
  845. defaultOpen={true}
  846. trigger={{
  847. title: `${props.input.subagent_type || props.tool} Agent`,
  848. titleClass: "capitalize",
  849. subtitle: props.input.description,
  850. }}
  851. onSubtitleClick={handleSubtitleClick}
  852. >
  853. <div
  854. ref={autoScroll.scrollRef}
  855. onScroll={autoScroll.handleScroll}
  856. data-component="tool-output"
  857. data-scrollable
  858. >
  859. <div ref={autoScroll.contentRef} data-component="task-tools">
  860. <For each={summary()}>
  861. {(item) => {
  862. const info = getToolInfo(item.tool)
  863. return (
  864. <div data-slot="task-tool-item">
  865. <Icon name={info.icon} size="small" />
  866. <span data-slot="task-tool-title">{info.title}</span>
  867. <Show when={item.state.title}>
  868. <span data-slot="task-tool-subtitle">{item.state.title}</span>
  869. </Show>
  870. </div>
  871. )
  872. }}
  873. </For>
  874. </div>
  875. </div>
  876. </BasicTool>
  877. </Match>
  878. </Switch>
  879. </div>
  880. )
  881. },
  882. })
  883. ToolRegistry.register({
  884. name: "bash",
  885. render(props) {
  886. return (
  887. <BasicTool
  888. {...props}
  889. icon="console"
  890. trigger={{
  891. title: "Shell",
  892. subtitle: props.input.description,
  893. }}
  894. >
  895. <div data-component="tool-output" data-scrollable>
  896. <Markdown
  897. text={`\`\`\`command\n$ ${props.input.command ?? props.metadata.command ?? ""}${props.output || props.metadata.output ? "\n\n" + stripAnsi(props.output || props.metadata.output) : ""}\n\`\`\``}
  898. />
  899. </div>
  900. </BasicTool>
  901. )
  902. },
  903. })
  904. ToolRegistry.register({
  905. name: "edit",
  906. render(props) {
  907. const diffComponent = useDiffComponent()
  908. const diagnostics = createMemo(() => getDiagnostics(props.metadata.diagnostics, props.input.filePath))
  909. const filename = () => getFilename(props.input.filePath ?? "")
  910. return (
  911. <BasicTool
  912. {...props}
  913. icon="code-lines"
  914. trigger={
  915. <div data-component="edit-trigger">
  916. <div data-slot="message-part-title-area">
  917. <div data-slot="message-part-title">Edit {filename()}</div>
  918. <Show when={props.input.filePath?.includes("/")}>
  919. <div data-slot="message-part-path">
  920. <span data-slot="message-part-directory">{getDirectory(props.input.filePath!)}</span>
  921. </div>
  922. </Show>
  923. </div>
  924. <div data-slot="message-part-actions">
  925. <Show when={props.metadata.filediff}>
  926. <DiffChanges changes={props.metadata.filediff} />
  927. </Show>
  928. </div>
  929. </div>
  930. }
  931. >
  932. <Show when={props.metadata.filediff?.path || props.input.filePath}>
  933. <div data-component="edit-content">
  934. <Dynamic
  935. component={diffComponent}
  936. before={{
  937. name: props.metadata?.filediff?.file || props.input.filePath,
  938. contents: props.metadata?.filediff?.before || props.input.oldString,
  939. }}
  940. after={{
  941. name: props.metadata?.filediff?.file || props.input.filePath,
  942. contents: props.metadata?.filediff?.after || props.input.newString,
  943. }}
  944. />
  945. </div>
  946. </Show>
  947. <DiagnosticsDisplay diagnostics={diagnostics()} />
  948. </BasicTool>
  949. )
  950. },
  951. })
  952. ToolRegistry.register({
  953. name: "write",
  954. render(props) {
  955. const codeComponent = useCodeComponent()
  956. const diagnostics = createMemo(() => getDiagnostics(props.metadata.diagnostics, props.input.filePath))
  957. const filename = () => getFilename(props.input.filePath ?? "")
  958. return (
  959. <BasicTool
  960. {...props}
  961. icon="code-lines"
  962. trigger={
  963. <div data-component="write-trigger">
  964. <div data-slot="message-part-title-area">
  965. <div data-slot="message-part-title">Write {filename()}</div>
  966. <Show when={props.input.filePath?.includes("/")}>
  967. <div data-slot="message-part-path">
  968. <span data-slot="message-part-directory">{getDirectory(props.input.filePath!)}</span>
  969. </div>
  970. </Show>
  971. </div>
  972. <div data-slot="message-part-actions">{/* <DiffChanges diff={diff} /> */}</div>
  973. </div>
  974. }
  975. >
  976. <Show when={props.input.content}>
  977. <div data-component="write-content">
  978. <Dynamic
  979. component={codeComponent}
  980. file={{
  981. name: props.input.filePath,
  982. contents: props.input.content,
  983. cacheKey: checksum(props.input.content),
  984. }}
  985. overflow="scroll"
  986. />
  987. </div>
  988. </Show>
  989. <DiagnosticsDisplay diagnostics={diagnostics()} />
  990. </BasicTool>
  991. )
  992. },
  993. })
  994. interface ApplyPatchFile {
  995. filePath: string
  996. relativePath: string
  997. type: "add" | "update" | "delete" | "move"
  998. diff: string
  999. before: string
  1000. after: string
  1001. additions: number
  1002. deletions: number
  1003. movePath?: string
  1004. }
  1005. ToolRegistry.register({
  1006. name: "apply_patch",
  1007. render(props) {
  1008. const diffComponent = useDiffComponent()
  1009. const files = createMemo(() => (props.metadata.files ?? []) as ApplyPatchFile[])
  1010. const subtitle = createMemo(() => {
  1011. const count = files().length
  1012. if (count === 0) return ""
  1013. return `${count} file${count > 1 ? "s" : ""}`
  1014. })
  1015. return (
  1016. <BasicTool
  1017. {...props}
  1018. icon="code-lines"
  1019. trigger={{
  1020. title: "Patch",
  1021. subtitle: subtitle(),
  1022. }}
  1023. >
  1024. <Show when={files().length > 0}>
  1025. <div data-component="apply-patch-files">
  1026. <For each={files()}>
  1027. {(file) => (
  1028. <div data-component="apply-patch-file">
  1029. <div data-slot="apply-patch-file-header">
  1030. <Switch>
  1031. <Match when={file.type === "delete"}>
  1032. <span data-slot="apply-patch-file-action" data-type="delete">
  1033. Deleted
  1034. </span>
  1035. </Match>
  1036. <Match when={file.type === "add"}>
  1037. <span data-slot="apply-patch-file-action" data-type="add">
  1038. Created
  1039. </span>
  1040. </Match>
  1041. <Match when={file.type === "move"}>
  1042. <span data-slot="apply-patch-file-action" data-type="move">
  1043. Moved
  1044. </span>
  1045. </Match>
  1046. <Match when={file.type === "update"}>
  1047. <span data-slot="apply-patch-file-action" data-type="update">
  1048. Patched
  1049. </span>
  1050. </Match>
  1051. </Switch>
  1052. <span data-slot="apply-patch-file-path">{file.relativePath}</span>
  1053. <Show when={file.type !== "delete"}>
  1054. <DiffChanges changes={{ additions: file.additions, deletions: file.deletions }} />
  1055. </Show>
  1056. <Show when={file.type === "delete"}>
  1057. <span data-slot="apply-patch-deletion-count">-{file.deletions}</span>
  1058. </Show>
  1059. </div>
  1060. <Show when={file.type !== "delete"}>
  1061. <div data-component="apply-patch-file-diff">
  1062. <Dynamic
  1063. component={diffComponent}
  1064. before={{ name: file.filePath, contents: file.before }}
  1065. after={{ name: file.filePath, contents: file.after }}
  1066. />
  1067. </div>
  1068. </Show>
  1069. </div>
  1070. )}
  1071. </For>
  1072. </div>
  1073. </Show>
  1074. </BasicTool>
  1075. )
  1076. },
  1077. })
  1078. ToolRegistry.register({
  1079. name: "todowrite",
  1080. render(props) {
  1081. const todos = createMemo(() => {
  1082. const meta = props.metadata?.todos
  1083. if (Array.isArray(meta)) return meta
  1084. const input = props.input.todos
  1085. if (Array.isArray(input)) return input
  1086. return []
  1087. })
  1088. const subtitle = createMemo(() => {
  1089. const list = todos()
  1090. if (list.length === 0) return ""
  1091. return `${list.filter((t: Todo) => t.status === "completed").length}/${list.length}`
  1092. })
  1093. return (
  1094. <BasicTool
  1095. {...props}
  1096. defaultOpen
  1097. icon="checklist"
  1098. trigger={{
  1099. title: "To-dos",
  1100. subtitle: subtitle(),
  1101. }}
  1102. >
  1103. <Show when={todos().length}>
  1104. <div data-component="todos">
  1105. <For each={todos()}>
  1106. {(todo: Todo) => (
  1107. <Checkbox readOnly checked={todo.status === "completed"}>
  1108. <div data-slot="message-part-todo-content" data-completed={todo.status === "completed"}>
  1109. {todo.content}
  1110. </div>
  1111. </Checkbox>
  1112. )}
  1113. </For>
  1114. </div>
  1115. </Show>
  1116. </BasicTool>
  1117. )
  1118. },
  1119. })
  1120. ToolRegistry.register({
  1121. name: "question",
  1122. render(props) {
  1123. const questions = createMemo(() => (props.input.questions ?? []) as QuestionInfo[])
  1124. const answers = createMemo(() => (props.metadata.answers ?? []) as QuestionAnswer[])
  1125. const completed = createMemo(() => answers().length > 0)
  1126. const subtitle = createMemo(() => {
  1127. const count = questions().length
  1128. if (count === 0) return ""
  1129. if (completed()) return `${count} answered`
  1130. return `${count} question${count > 1 ? "s" : ""}`
  1131. })
  1132. return (
  1133. <BasicTool
  1134. {...props}
  1135. defaultOpen={completed()}
  1136. icon="bubble-5"
  1137. trigger={{
  1138. title: "Questions",
  1139. subtitle: subtitle(),
  1140. }}
  1141. >
  1142. <Show when={completed()}>
  1143. <div data-component="question-answers">
  1144. <For each={questions()}>
  1145. {(q, i) => {
  1146. const answer = () => answers()[i()] ?? []
  1147. return (
  1148. <div data-slot="question-answer-item">
  1149. <div data-slot="question-text">{q.question}</div>
  1150. <div data-slot="answer-text">{answer().join(", ") || "(no answer)"}</div>
  1151. </div>
  1152. )
  1153. }}
  1154. </For>
  1155. </div>
  1156. </Show>
  1157. </BasicTool>
  1158. )
  1159. },
  1160. })
  1161. function QuestionPrompt(props: { request: QuestionRequest }) {
  1162. const data = useData()
  1163. const questions = createMemo(() => props.request.questions)
  1164. const single = createMemo(() => questions().length === 1 && questions()[0]?.multiple !== true)
  1165. const [store, setStore] = createStore({
  1166. tab: 0,
  1167. answers: [] as QuestionAnswer[],
  1168. custom: [] as string[],
  1169. editing: false,
  1170. })
  1171. const question = createMemo(() => questions()[store.tab])
  1172. const confirm = createMemo(() => !single() && store.tab === questions().length)
  1173. const options = createMemo(() => question()?.options ?? [])
  1174. const input = createMemo(() => store.custom[store.tab] ?? "")
  1175. const multi = createMemo(() => question()?.multiple === true)
  1176. const customPicked = createMemo(() => {
  1177. const value = input()
  1178. if (!value) return false
  1179. return store.answers[store.tab]?.includes(value) ?? false
  1180. })
  1181. function submit() {
  1182. const answers = questions().map((_, i) => store.answers[i] ?? [])
  1183. data.replyToQuestion?.({
  1184. requestID: props.request.id,
  1185. answers,
  1186. })
  1187. }
  1188. function reject() {
  1189. data.rejectQuestion?.({
  1190. requestID: props.request.id,
  1191. })
  1192. }
  1193. function pick(answer: string, custom: boolean = false) {
  1194. const answers = [...store.answers]
  1195. answers[store.tab] = [answer]
  1196. setStore("answers", answers)
  1197. if (custom) {
  1198. const inputs = [...store.custom]
  1199. inputs[store.tab] = answer
  1200. setStore("custom", inputs)
  1201. }
  1202. if (single()) {
  1203. data.replyToQuestion?.({
  1204. requestID: props.request.id,
  1205. answers: [[answer]],
  1206. })
  1207. return
  1208. }
  1209. setStore("tab", store.tab + 1)
  1210. }
  1211. function toggle(answer: string) {
  1212. const existing = store.answers[store.tab] ?? []
  1213. const next = [...existing]
  1214. const index = next.indexOf(answer)
  1215. if (index === -1) next.push(answer)
  1216. if (index !== -1) next.splice(index, 1)
  1217. const answers = [...store.answers]
  1218. answers[store.tab] = next
  1219. setStore("answers", answers)
  1220. }
  1221. function selectTab(index: number) {
  1222. setStore("tab", index)
  1223. setStore("editing", false)
  1224. }
  1225. function selectOption(optIndex: number) {
  1226. if (optIndex === options().length) {
  1227. setStore("editing", true)
  1228. return
  1229. }
  1230. const opt = options()[optIndex]
  1231. if (!opt) return
  1232. if (multi()) {
  1233. toggle(opt.label)
  1234. return
  1235. }
  1236. pick(opt.label)
  1237. }
  1238. function handleCustomSubmit(e: Event) {
  1239. e.preventDefault()
  1240. const value = input().trim()
  1241. if (!value) {
  1242. setStore("editing", false)
  1243. return
  1244. }
  1245. if (multi()) {
  1246. const existing = store.answers[store.tab] ?? []
  1247. const next = [...existing]
  1248. if (!next.includes(value)) next.push(value)
  1249. const answers = [...store.answers]
  1250. answers[store.tab] = next
  1251. setStore("answers", answers)
  1252. setStore("editing", false)
  1253. return
  1254. }
  1255. pick(value, true)
  1256. setStore("editing", false)
  1257. }
  1258. return (
  1259. <div data-component="question-prompt">
  1260. <Show when={!single()}>
  1261. <div data-slot="question-tabs">
  1262. <For each={questions()}>
  1263. {(q, index) => {
  1264. const active = () => index() === store.tab
  1265. const answered = () => (store.answers[index()]?.length ?? 0) > 0
  1266. return (
  1267. <button
  1268. data-slot="question-tab"
  1269. data-active={active()}
  1270. data-answered={answered()}
  1271. onClick={() => selectTab(index())}
  1272. >
  1273. {q.header}
  1274. </button>
  1275. )
  1276. }}
  1277. </For>
  1278. <button data-slot="question-tab" data-active={confirm()} onClick={() => selectTab(questions().length)}>
  1279. Confirm
  1280. </button>
  1281. </div>
  1282. </Show>
  1283. <Show when={!confirm()}>
  1284. <div data-slot="question-content">
  1285. <div data-slot="question-text">
  1286. {question()?.question}
  1287. {multi() ? " (select all that apply)" : ""}
  1288. </div>
  1289. <div data-slot="question-options">
  1290. <For each={options()}>
  1291. {(opt, i) => {
  1292. const picked = () => store.answers[store.tab]?.includes(opt.label) ?? false
  1293. return (
  1294. <button data-slot="question-option" data-picked={picked()} onClick={() => selectOption(i())}>
  1295. <span data-slot="option-label">{opt.label}</span>
  1296. <Show when={opt.description}>
  1297. <span data-slot="option-description">{opt.description}</span>
  1298. </Show>
  1299. <Show when={picked()}>
  1300. <Icon name="check-small" size="normal" />
  1301. </Show>
  1302. </button>
  1303. )
  1304. }}
  1305. </For>
  1306. <button
  1307. data-slot="question-option"
  1308. data-picked={customPicked()}
  1309. onClick={() => selectOption(options().length)}
  1310. >
  1311. <span data-slot="option-label">Type your own answer</span>
  1312. <Show when={!store.editing && input()}>
  1313. <span data-slot="option-description">{input()}</span>
  1314. </Show>
  1315. <Show when={customPicked()}>
  1316. <Icon name="check-small" size="normal" />
  1317. </Show>
  1318. </button>
  1319. <Show when={store.editing}>
  1320. <form data-slot="custom-input-form" onSubmit={handleCustomSubmit}>
  1321. <input
  1322. ref={(el) => setTimeout(() => el.focus(), 0)}
  1323. type="text"
  1324. data-slot="custom-input"
  1325. placeholder="Type your answer..."
  1326. value={input()}
  1327. onInput={(e) => {
  1328. const inputs = [...store.custom]
  1329. inputs[store.tab] = e.currentTarget.value
  1330. setStore("custom", inputs)
  1331. }}
  1332. />
  1333. <Button type="submit" variant="primary" size="small">
  1334. {multi() ? "Add" : "Submit"}
  1335. </Button>
  1336. <Button type="button" variant="ghost" size="small" onClick={() => setStore("editing", false)}>
  1337. Cancel
  1338. </Button>
  1339. </form>
  1340. </Show>
  1341. </div>
  1342. </div>
  1343. </Show>
  1344. <Show when={confirm()}>
  1345. <div data-slot="question-review">
  1346. <div data-slot="review-title">Review your answers</div>
  1347. <For each={questions()}>
  1348. {(q, index) => {
  1349. const value = () => store.answers[index()]?.join(", ") ?? ""
  1350. const answered = () => Boolean(value())
  1351. return (
  1352. <div data-slot="review-item">
  1353. <span data-slot="review-label">{q.question}</span>
  1354. <span data-slot="review-value" data-answered={answered()}>
  1355. {answered() ? value() : "(not answered)"}
  1356. </span>
  1357. </div>
  1358. )
  1359. }}
  1360. </For>
  1361. </div>
  1362. </Show>
  1363. <div data-slot="question-actions">
  1364. <Button variant="ghost" size="small" onClick={reject}>
  1365. Dismiss
  1366. </Button>
  1367. <Show when={!single()}>
  1368. <Show when={confirm()}>
  1369. <Button variant="primary" size="small" onClick={submit}>
  1370. Submit
  1371. </Button>
  1372. </Show>
  1373. <Show when={!confirm() && multi()}>
  1374. <Button
  1375. variant="secondary"
  1376. size="small"
  1377. onClick={() => selectTab(store.tab + 1)}
  1378. disabled={(store.answers[store.tab]?.length ?? 0) === 0}
  1379. >
  1380. Next
  1381. </Button>
  1382. </Show>
  1383. </Show>
  1384. </div>
  1385. </div>
  1386. )
  1387. }