import { Component, createEffect, createMemo, createSignal, For, Match, Show, Switch, onCleanup, type JSX, } from "solid-js" import stripAnsi from "strip-ansi" import { Dynamic } from "solid-js/web" import { AgentPart, AssistantMessage, FilePart, Message as MessageType, Part as PartType, ReasoningPart, TextPart, ToolPart, UserMessage, Todo, QuestionRequest, QuestionAnswer, QuestionInfo, } from "@opencode-ai/sdk/v2" import { createStore } from "solid-js/store" import { useData } from "../context" import { useDiffComponent } from "../context/diff" import { useCodeComponent } from "../context/code" import { useDialog } from "../context/dialog" import { useI18n } from "../context/i18n" import { BasicTool } from "./basic-tool" import { GenericTool } from "./basic-tool" import { Accordion } from "./accordion" import { Button } from "./button" import { Card } from "./card" import { Collapsible } from "./collapsible" import { FileIcon } from "./file-icon" import { Icon } from "./icon" import { Checkbox } from "./checkbox" import { DiffChanges } from "./diff-changes" import { Markdown } from "./markdown" import { ImagePreview } from "./image-preview" import { getDirectory as _getDirectory, getFilename } from "@opencode-ai/util/path" import { checksum } from "@opencode-ai/util/encode" import { Tooltip } from "./tooltip" import { IconButton } from "./icon-button" import { TextShimmer } from "./text-shimmer" interface Diagnostic { range: { start: { line: number; character: number } end: { line: number; character: number } } message: string severity?: number } function getDiagnostics( diagnosticsByFile: Record | undefined, filePath: string | undefined, ): Diagnostic[] { if (!diagnosticsByFile || !filePath) return [] const diagnostics = diagnosticsByFile[filePath] ?? [] return diagnostics.filter((d) => d.severity === 1).slice(0, 3) } function DiagnosticsDisplay(props: { diagnostics: Diagnostic[] }): JSX.Element { const i18n = useI18n() return ( 0}>
{(diagnostic) => (
{i18n.t("ui.messagePart.diagnostic.error")} [{diagnostic.range.start.line + 1}:{diagnostic.range.start.character + 1}] {diagnostic.message}
)}
) } export interface MessageProps { message: MessageType parts: PartType[] showAssistantCopyPartID?: string | null interrupted?: boolean } export interface MessagePartProps { part: PartType message: MessageType hideDetails?: boolean defaultOpen?: boolean showAssistantCopyPartID?: string | null } export type PartComponent = Component export const PART_MAPPING: Record = {} const TEXT_RENDER_THROTTLE_MS = 100 function createThrottledValue(getValue: () => string) { const [value, setValue] = createSignal(getValue()) let timeout: ReturnType | undefined let last = 0 createEffect(() => { const next = getValue() const now = Date.now() const remaining = TEXT_RENDER_THROTTLE_MS - (now - last) if (remaining <= 0) { if (timeout) { clearTimeout(timeout) timeout = undefined } last = now setValue(next) return } if (timeout) clearTimeout(timeout) timeout = setTimeout(() => { last = Date.now() setValue(next) timeout = undefined }, remaining) }) onCleanup(() => { if (timeout) clearTimeout(timeout) }) return value } function relativizeProjectPaths(text: string, directory?: string) { if (!text) return "" if (!directory) return text return text.split(directory).join("") } function getDirectory(path: string | undefined) { const data = useData() return relativizeProjectPaths(_getDirectory(path), data.directory) } import type { IconProps } from "./icon" export type ToolInfo = { icon: IconProps["name"] title: string subtitle?: string } export function getToolInfo(tool: string, input: any = {}): ToolInfo { const i18n = useI18n() switch (tool) { case "read": return { icon: "glasses", title: i18n.t("ui.tool.read"), subtitle: input.filePath ? getFilename(input.filePath) : undefined, } case "list": return { icon: "bullet-list", title: i18n.t("ui.tool.list"), subtitle: input.path ? getFilename(input.path) : undefined, } case "glob": return { icon: "magnifying-glass-menu", title: i18n.t("ui.tool.glob"), subtitle: input.pattern, } case "grep": return { icon: "magnifying-glass-menu", title: i18n.t("ui.tool.grep"), subtitle: input.pattern, } case "webfetch": return { icon: "window-cursor", title: i18n.t("ui.tool.webfetch"), subtitle: input.url, } case "task": return { icon: "task", title: i18n.t("ui.tool.agent", { type: input.subagent_type || "task" }), subtitle: input.description, } case "bash": return { icon: "console", title: i18n.t("ui.tool.shell"), subtitle: input.description, } case "edit": return { icon: "code-lines", title: i18n.t("ui.messagePart.title.edit"), subtitle: input.filePath ? getFilename(input.filePath) : undefined, } case "write": return { icon: "code-lines", title: i18n.t("ui.messagePart.title.write"), subtitle: input.filePath ? getFilename(input.filePath) : undefined, } case "apply_patch": return { icon: "code-lines", title: i18n.t("ui.tool.patch"), subtitle: input.files?.length ? `${input.files.length} ${i18n.t(input.files.length > 1 ? "ui.common.file.other" : "ui.common.file.one")}` : undefined, } case "todowrite": return { icon: "checklist", title: i18n.t("ui.tool.todos"), } case "todoread": return { icon: "checklist", title: i18n.t("ui.tool.todos.read"), } case "question": return { icon: "bubble-5", title: i18n.t("ui.tool.questions"), } default: return { icon: "mcp", title: tool, } } } const CONTEXT_GROUP_TOOLS = new Set(["read", "glob", "grep", "list"]) const HIDDEN_TOOLS = new Set(["todowrite", "todoread"]) function list(value: T[] | undefined | null, fallback: T[]) { if (Array.isArray(value)) return value return fallback } function renderable(part: PartType) { if (part.type === "tool") { if (HIDDEN_TOOLS.has(part.tool)) return false if (part.tool === "question") return part.state.status !== "pending" && part.state.status !== "running" return true } if (part.type === "text") return !!part.text?.trim() if (part.type === "reasoning") return !!part.text?.trim() return !!PART_MAPPING[part.type] } export function AssistantParts(props: { messages: AssistantMessage[] showAssistantCopyPartID?: string | null working?: boolean }) { const data = useData() const emptyParts: PartType[] = [] const grouped = createMemo(() => { const keys: string[] = [] const items: Record< string, { type: "part"; part: PartType; message: AssistantMessage } | { type: "context"; parts: ToolPart[] } > = {} const push = ( key: string, item: { type: "part"; part: PartType; message: AssistantMessage } | { type: "context"; parts: ToolPart[] }, ) => { keys.push(key) items[key] = item } const parts = props.messages.flatMap((message) => list(data.store.part?.[message.id], emptyParts) .filter(renderable) .map((part) => ({ message, part })), ) let start = -1 const flush = (end: number) => { if (start < 0) return const first = parts[start] const last = parts[end] if (!first || !last) { start = -1 return } push(`context:${first.part.id}`, { type: "context", parts: parts .slice(start, end + 1) .map((x) => x.part) .filter((part): part is ToolPart => isContextGroupTool(part)), }) start = -1 } parts.forEach((item, index) => { if (isContextGroupTool(item.part)) { if (start < 0) start = index return } flush(index - 1) push(`part:${item.message.id}:${item.part.id}`, { type: "part", part: item.part, message: item.message }) }) flush(parts.length - 1) return { keys, items } }) const last = createMemo(() => grouped().keys.at(-1)) return ( {(key) => { const item = createMemo(() => grouped().items[key]) const ctx = createMemo(() => { const value = item() if (!value) return if (value.type !== "context") return return value }) const part = createMemo(() => { const value = item() if (!value) return if (value.type !== "part") return return value }) const tail = createMemo(() => last() === key) return ( <> {(entry) => } {(entry) => ( )} ) }} ) } function isContextGroupTool(part: PartType): part is ToolPart { return part.type === "tool" && CONTEXT_GROUP_TOOLS.has(part.tool) } function contextToolDetail(part: ToolPart): string | undefined { const info = getToolInfo(part.tool, part.state.input ?? {}) if (info.subtitle) return info.subtitle if (part.state.status === "error") return part.state.error if ((part.state.status === "running" || part.state.status === "completed") && part.state.title) return part.state.title const description = part.state.input?.description if (typeof description === "string") return description return undefined } function contextToolTrigger(part: ToolPart, i18n: ReturnType) { const input = (part.state.input ?? {}) as Record const path = typeof input.path === "string" ? input.path : "/" const filePath = typeof input.filePath === "string" ? input.filePath : undefined const pattern = typeof input.pattern === "string" ? input.pattern : undefined const include = typeof input.include === "string" ? input.include : undefined const offset = typeof input.offset === "number" ? input.offset : undefined const limit = typeof input.limit === "number" ? input.limit : undefined switch (part.tool) { case "read": { const args: string[] = [] if (offset !== undefined) args.push("offset=" + offset) if (limit !== undefined) args.push("limit=" + limit) return { title: i18n.t("ui.tool.read"), subtitle: filePath ? getFilename(filePath) : "", args, } } case "list": return { title: i18n.t("ui.tool.list"), subtitle: getDirectory(path), } case "glob": return { title: i18n.t("ui.tool.glob"), subtitle: getDirectory(path), args: pattern ? ["pattern=" + pattern] : [], } case "grep": { const args: string[] = [] if (pattern) args.push("pattern=" + pattern) if (include) args.push("include=" + include) return { title: i18n.t("ui.tool.grep"), subtitle: getDirectory(path), args, } } default: { const info = getToolInfo(part.tool, input) return { title: info.title, subtitle: info.subtitle || contextToolDetail(part), args: [], } } } } function contextToolSummary(parts: ToolPart[]) { const read = parts.filter((part) => part.tool === "read").length const search = parts.filter((part) => part.tool === "glob" || part.tool === "grep").length const list = parts.filter((part) => part.tool === "list").length return [ read ? `${read} ${read === 1 ? "read" : "reads"}` : undefined, search ? `${search} ${search === 1 ? "search" : "searches"}` : undefined, list ? `${list} ${list === 1 ? "list" : "lists"}` : undefined, ].filter((value): value is string => !!value) } export function registerPartComponent(type: string, component: PartComponent) { PART_MAPPING[type] = component } export function Message(props: MessageProps) { return ( {(userMessage) => ( )} {(assistantMessage) => ( )} ) } export function AssistantMessageDisplay(props: { message: AssistantMessage parts: PartType[] showAssistantCopyPartID?: string | null }) { const grouped = createMemo(() => { const keys: string[] = [] const items: Record = {} const push = (key: string, item: { type: "part"; part: PartType } | { type: "context"; parts: ToolPart[] }) => { keys.push(key) items[key] = item } const parts = props.parts let start = -1 const flush = (end: number) => { if (start < 0) return const first = parts[start] const last = parts[end] if (!first || !last) { start = -1 return } push(`context:${first.id}`, { type: "context", parts: parts.slice(start, end + 1).filter((part): part is ToolPart => isContextGroupTool(part)), }) start = -1 } parts.forEach((part, index) => { if (!renderable(part)) return if (isContextGroupTool(part)) { if (start < 0) start = index return } flush(index - 1) push(`part:${part.id}`, { type: "part", part }) }) flush(parts.length - 1) return { keys, items } }) return ( {(key) => { const item = createMemo(() => grouped().items[key]) const ctx = createMemo(() => { const value = item() if (!value) return if (value.type !== "context") return return value }) const part = createMemo(() => { const value = item() if (!value) return if (value.type !== "part") return return value }) return ( <> {(entry) => } {(entry) => ( )} ) }} ) } function ContextToolGroup(props: { parts: ToolPart[]; busy?: boolean }) { const i18n = useI18n() const [open, setOpen] = createSignal(false) const pending = createMemo( () => !!props.busy || props.parts.some((part) => part.state.status === "pending" || part.state.status === "running"), ) const summary = createMemo(() => contextToolSummary(props.parts)) const details = createMemo(() => summary().join(", ")) return (
{i18n.t("ui.sessionTurn.status.gatheredContext")} {details()} } > {details()}
{(part) => { const trigger = contextToolTrigger(part, i18n) const running = part.state.status === "pending" || part.state.status === "running" return (
{trigger.subtitle} {(arg) => {arg}}
) }}
) } export function UserMessageDisplay(props: { message: UserMessage; parts: PartType[]; interrupted?: boolean }) { const data = useData() const dialog = useDialog() const i18n = useI18n() const [copied, setCopied] = createSignal(false) const textPart = createMemo( () => props.parts?.find((p) => p.type === "text" && !(p as TextPart).synthetic) as TextPart | undefined, ) const text = createMemo(() => textPart()?.text || "") const files = createMemo(() => (props.parts?.filter((p) => p.type === "file") as FilePart[]) ?? []) const attachments = createMemo(() => files()?.filter((f) => { const mime = f.mime return mime.startsWith("image/") || mime === "application/pdf" }), ) const inlineFiles = createMemo(() => files().filter((f) => { const mime = f.mime return !mime.startsWith("image/") && mime !== "application/pdf" && f.source?.text?.start !== undefined }), ) const agents = createMemo(() => (props.parts?.filter((p) => p.type === "agent") as AgentPart[]) ?? []) const model = createMemo(() => { const providerID = props.message.model?.providerID const modelID = props.message.model?.modelID if (!providerID || !modelID) return "" const match = data.store.provider?.all?.find((p) => p.id === providerID) return match?.models?.[modelID]?.name ?? modelID }) const stamp = createMemo(() => { const created = props.message.time?.created if (typeof created !== "number") return "" const date = new Date(created) const hours = date.getHours() const hour12 = hours % 12 || 12 const minute = String(date.getMinutes()).padStart(2, "0") return `${hour12}:${minute} ${hours < 12 ? "AM" : "PM"}` }) const metaHead = createMemo(() => { const agent = props.message.agent const items = [agent ? agent[0]?.toUpperCase() + agent.slice(1) : "", model()] return items.filter((x) => !!x).join("\u00A0\u00B7\u00A0") }) const metaTail = createMemo(() => { const items = [stamp(), props.interrupted ? i18n.t("ui.message.interrupted") : ""] return items.filter((x) => !!x).join("\u00A0\u00B7\u00A0") }) const openImagePreview = (url: string, alt?: string) => { dialog.show(() => ) } const handleCopy = async () => { const content = text() if (!content) return await navigator.clipboard.writeText(content) setCopied(true) setTimeout(() => setCopied(false), 2000) } return (
0}>
{(file) => (
{ if (file.mime.startsWith("image/") && file.url) { openImagePreview(file.url, file.filename) } }} >
} > {file.filename
)}
<>
{metaHead()} {"\u00A0\u00B7\u00A0"} {metaTail()} e.preventDefault()} onClick={(event) => { event.stopPropagation() handleCopy() }} aria-label={copied() ? i18n.t("ui.message.copied") : i18n.t("ui.message.copyMessage")} />
) } type HighlightSegment = { text: string; type?: "file" | "agent" } function HighlightedText(props: { text: string; references: FilePart[]; agents: AgentPart[] }) { const segments = createMemo(() => { const text = props.text const allRefs: { start: number; end: number; type: "file" | "agent" }[] = [ ...props.references .filter((r) => r.source?.text?.start !== undefined && r.source?.text?.end !== undefined) .map((r) => ({ start: r.source!.text!.start, end: r.source!.text!.end, type: "file" as const })), ...props.agents .filter((a) => a.source?.start !== undefined && a.source?.end !== undefined) .map((a) => ({ start: a.source!.start, end: a.source!.end, type: "agent" as const })), ].sort((a, b) => a.start - b.start) const result: HighlightSegment[] = [] let lastIndex = 0 for (const ref of allRefs) { if (ref.start < lastIndex) continue if (ref.start > lastIndex) { result.push({ text: text.slice(lastIndex, ref.start) }) } result.push({ text: text.slice(ref.start, ref.end), type: ref.type }) lastIndex = ref.end } if (lastIndex < text.length) { result.push({ text: text.slice(lastIndex) }) } return result }) return {(segment) => {segment.text}} } export function Part(props: MessagePartProps) { const component = createMemo(() => PART_MAPPING[props.part.type]) return ( ) } export interface ToolProps { input: Record metadata: Record tool: string output?: string status?: string hideDetails?: boolean defaultOpen?: boolean forceOpen?: boolean locked?: boolean } export type ToolComponent = Component const state: Record< string, { name: string render?: ToolComponent } > = {} export function registerTool(input: { name: string; render?: ToolComponent }) { state[input.name] = input return input } export function getTool(name: string) { return state[name]?.render } export const ToolRegistry = { register: registerTool, render: getTool, } PART_MAPPING["tool"] = function ToolPartDisplay(props) { const data = useData() const i18n = useI18n() const part = props.part as ToolPart if (part.tool === "todowrite" || part.tool === "todoread") return null const hideQuestion = createMemo( () => part.tool === "question" && (part.state.status === "pending" || part.state.status === "running"), ) const permission = createMemo(() => { const next = data.store.permission?.[props.message.sessionID]?.[0] if (!next || !next.tool) return undefined if (next.tool!.callID !== part.callID) return undefined return next }) const questionRequest = createMemo(() => { const next = data.store.question?.[props.message.sessionID]?.[0] if (!next || !next.tool) return undefined if (next.tool!.callID !== part.callID) return undefined return next }) const [showPermission, setShowPermission] = createSignal(false) const [showQuestion, setShowQuestion] = createSignal(false) createEffect(() => { const perm = permission() if (perm) { const timeout = setTimeout(() => setShowPermission(true), 50) onCleanup(() => clearTimeout(timeout)) } else { setShowPermission(false) } }) createEffect(() => { const question = questionRequest() if (question) { const timeout = setTimeout(() => setShowQuestion(true), 50) onCleanup(() => clearTimeout(timeout)) } else { setShowQuestion(false) } }) const [forceOpen, setForceOpen] = createSignal(false) createEffect(() => { if (permission() || questionRequest()) setForceOpen(true) }) const respond = (response: "once" | "always" | "reject") => { const perm = permission() if (!perm || !data.respondToPermission) return data.respondToPermission({ sessionID: perm.sessionID, permissionID: perm.id, response, }) } const emptyInput: Record = {} const emptyMetadata: Record = {} const input = () => part.state?.input ?? emptyInput // @ts-expect-error const partMetadata = () => part.state?.metadata ?? emptyMetadata const metadata = () => { const perm = permission() if (perm?.metadata) return { ...perm.metadata, ...partMetadata() } return partMetadata() } const render = ToolRegistry.render(part.tool) ?? GenericTool return (
{(error) => { const cleaned = error().replace("Error: ", "") if (part.tool === "question" && cleaned.includes("dismissed this question")) { return (
{i18n.t("ui.tool.questions")} dismissed
) } const [title, ...rest] = cleaned.split(": ") return (
{title}
{rest.join(": ")}
{cleaned}
) }}
{(request) => }
) } PART_MAPPING["text"] = function TextPartDisplay(props) { const data = useData() const i18n = useI18n() const part = props.part as TextPart const interrupted = createMemo( () => props.message.role === "assistant" && (props.message as AssistantMessage).error?.name === "MessageAbortedError", ) const model = createMemo(() => { if (props.message.role !== "assistant") return "" const message = props.message as AssistantMessage const match = data.store.provider?.all?.find((p) => p.id === message.providerID) return match?.models?.[message.modelID]?.name ?? message.modelID }) const duration = createMemo(() => { if (props.message.role !== "assistant") return "" const message = props.message as AssistantMessage const completed = message.time.completed if (typeof completed !== "number") return "" const ms = completed - message.time.created if (!(ms >= 0)) return "" const total = Math.round(ms / 1000) if (total < 60) return `${total}s` const minutes = Math.floor(total / 60) const seconds = total % 60 return `${minutes}m ${seconds}s` }) const meta = createMemo(() => { if (props.message.role !== "assistant") return "" const agent = (props.message as AssistantMessage).agent const items = [ agent ? agent[0]?.toUpperCase() + agent.slice(1) : "", model(), duration(), interrupted() ? i18n.t("ui.message.interrupted") : "", ] return items.filter((x) => !!x).join(" \u00B7 ") }) const displayText = () => relativizeProjectPaths((part.text ?? "").trim(), data.directory) const throttledText = createThrottledValue(displayText) const isLastTextPart = createMemo(() => { const last = (data.store.part?.[props.message.id] ?? []) .filter((item): item is TextPart => item?.type === "text" && !!item.text?.trim()) .at(-1) return last?.id === part.id }) const showCopy = createMemo(() => { if (props.message.role !== "assistant") return isLastTextPart() if (props.showAssistantCopyPartID === null) return false if (typeof props.showAssistantCopyPartID === "string") return props.showAssistantCopyPartID === part.id return isLastTextPart() }) const [copied, setCopied] = createSignal(false) const handleCopy = async () => { const content = displayText() if (!content) return await navigator.clipboard.writeText(content) setCopied(true) setTimeout(() => setCopied(false), 2000) } return (
e.preventDefault()} onClick={handleCopy} aria-label={copied() ? i18n.t("ui.message.copied") : i18n.t("ui.message.copyResponse")} /> {meta()}
) } PART_MAPPING["reasoning"] = function ReasoningPartDisplay(props) { const part = props.part as ReasoningPart const text = () => part.text.trim() const throttledText = createThrottledValue(text) return (
) } ToolRegistry.register({ name: "read", render(props) { const data = useData() const i18n = useI18n() const args: string[] = [] if (props.input.offset) args.push("offset=" + props.input.offset) if (props.input.limit) args.push("limit=" + props.input.limit) const loaded = createMemo(() => { if (props.status !== "completed") return [] const value = props.metadata.loaded if (!value || !Array.isArray(value)) return [] return value.filter((p): p is string => typeof p === "string") }) return ( <> {(filepath) => (
{i18n.t("ui.tool.loaded")} {relativizeProjectPaths(filepath, data.directory)}
)}
) }, }) ToolRegistry.register({ name: "list", render(props) { const i18n = useI18n() return ( {(output) => (
)}
) }, }) ToolRegistry.register({ name: "glob", render(props) { const i18n = useI18n() return ( )} ) }, }) ToolRegistry.register({ name: "grep", render(props) { const i18n = useI18n() const args: string[] = [] if (props.input.pattern) args.push("pattern=" + props.input.pattern) if (props.input.include) args.push("include=" + props.input.include) return ( {(output) => (
)}
) }, }) ToolRegistry.register({ name: "webfetch", render(props) { const i18n = useI18n() const pending = createMemo(() => props.status === "pending" || props.status === "running") const url = createMemo(() => { const value = props.input.url if (typeof value !== "string") return "" return value }) return (
} /> ) }, }) ToolRegistry.register({ name: "task", render(props) { const data = useData() const i18n = useI18n() const childSessionId = () => props.metadata.sessionId as string | undefined const title = createMemo(() => i18n.t("ui.tool.agent", { type: props.input.subagent_type || props.tool })) const description = createMemo(() => { const value = props.input.description if (typeof value === "string") return value return undefined }) const running = createMemo(() => props.status === "pending" || props.status === "running") const href = createMemo(() => { const sessionId = childSessionId() if (!sessionId) return const direct = data.sessionHref?.(sessionId) if (direct) return direct if (typeof window === "undefined") return const path = window.location.pathname const idx = path.indexOf("/session") if (idx === -1) return return `${path.slice(0, idx)}/session/${sessionId}` }) const handleLinkClick = (e: MouseEvent) => { const sessionId = childSessionId() const url = href() if (!sessionId || !url) return e.stopPropagation() if (e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return const nav = data.navigateToSession if (!nav || typeof window === "undefined") return e.preventDefault() const before = window.location.pathname + window.location.search + window.location.hash nav(sessionId) setTimeout(() => { const after = window.location.pathname + window.location.search + window.location.hash if (after === before) window.location.assign(url) }, 50) } const titleContent = () => const trigger = () => (
{titleContent()} {(url) => ( {description()} )} {description()}
) return }, }) ToolRegistry.register({ name: "bash", render(props) { const i18n = useI18n() const text = createMemo(() => { const cmd = props.input.command ?? props.metadata.command ?? "" const out = stripAnsi(props.output || props.metadata.output || "") return `$ ${cmd}${out ? "\n\n" + out : ""}` }) const [copied, setCopied] = createSignal(false) const handleCopy = async () => { const content = text() if (!content) return await navigator.clipboard.writeText(content) setCopied(true) setTimeout(() => setCopied(false), 2000) } return (
e.preventDefault()} onClick={handleCopy} aria-label={copied() ? i18n.t("ui.message.copied") : i18n.t("ui.message.copy")} />
              {text()}
            
) }, }) ToolRegistry.register({ name: "edit", render(props) { const i18n = useI18n() const diffComponent = useDiffComponent() const diagnostics = createMemo(() => getDiagnostics(props.metadata.diagnostics, props.input.filePath)) const filename = () => getFilename(props.input.filePath ?? "") const pending = () => props.status === "pending" || props.status === "running" return (
{filename()}
{getDirectory(props.input.filePath!)}
} >
) }, }) ToolRegistry.register({ name: "write", render(props) { const i18n = useI18n() const codeComponent = useCodeComponent() const diagnostics = createMemo(() => getDiagnostics(props.metadata.diagnostics, props.input.filePath)) const filename = () => getFilename(props.input.filePath ?? "") const pending = () => props.status === "pending" || props.status === "running" return (
{filename()}
{getDirectory(props.input.filePath!)}
{/* */}
} >
) }, }) interface ApplyPatchFile { filePath: string relativePath: string type: "add" | "update" | "delete" | "move" diff: string before: string after: string additions: number deletions: number movePath?: string } ToolRegistry.register({ name: "apply_patch", render(props) { const i18n = useI18n() const diffComponent = useDiffComponent() const files = createMemo(() => (props.metadata.files ?? []) as ApplyPatchFile[]) const [expanded, setExpanded] = createSignal([]) let seeded = false createEffect(() => { const list = files() if (list.length === 0) return if (seeded) return seeded = true setExpanded(list.filter((f) => f.type !== "delete").map((f) => f.filePath)) }) const subtitle = createMemo(() => { const count = files().length if (count === 0) return "" return `${count} ${i18n.t(count > 1 ? "ui.common.file.other" : "ui.common.file.one")}` }) return ( 0}> setExpanded(Array.isArray(value) ? value : value ? [value] : [])} > {(file) => { const active = createMemo(() => expanded().includes(file.filePath)) const [visible, setVisible] = createSignal(false) createEffect(() => { if (!active()) { setVisible(false) return } requestAnimationFrame(() => { if (!active()) return setVisible(true) }) }) return (
{`\u202A${getDirectory(file.relativePath)}\u202C`} {getFilename(file.relativePath)}
{i18n.t("ui.patch.action.created")} {i18n.t("ui.patch.action.deleted")} {i18n.t("ui.patch.action.moved")}
) }}
) }, }) ToolRegistry.register({ name: "todowrite", render(props) { const i18n = useI18n() const todos = createMemo(() => { const meta = props.metadata?.todos if (Array.isArray(meta)) return meta const input = props.input.todos if (Array.isArray(input)) return input return [] }) const subtitle = createMemo(() => { const list = todos() if (list.length === 0) return "" return `${list.filter((t: Todo) => t.status === "completed").length}/${list.length}` }) return (
{(todo: Todo) => ( {todo.content} )}
) }, }) ToolRegistry.register({ name: "question", render(props) { const i18n = useI18n() const questions = createMemo(() => (props.input.questions ?? []) as QuestionInfo[]) const answers = createMemo(() => (props.metadata.answers ?? []) as QuestionAnswer[]) const completed = createMemo(() => answers().length > 0) const subtitle = createMemo(() => { const count = questions().length if (count === 0) return "" if (completed()) return i18n.t("ui.question.subtitle.answered", { count }) return `${count} ${i18n.t(count > 1 ? "ui.common.question.other" : "ui.common.question.one")}` }) return (
{(q, i) => { const answer = () => answers()[i()] ?? [] return (
{q.question}
{answer().join(", ") || i18n.t("ui.question.answer.none")}
) }}
) }, }) function QuestionPrompt(props: { request: QuestionRequest }) { const data = useData() const i18n = useI18n() const questions = createMemo(() => props.request.questions) const single = createMemo(() => questions().length === 1 && questions()[0]?.multiple !== true) const [store, setStore] = createStore({ tab: 0, answers: [] as QuestionAnswer[], custom: [] as string[], editing: false, }) const question = createMemo(() => questions()[store.tab]) const confirm = createMemo(() => !single() && store.tab === questions().length) const options = createMemo(() => question()?.options ?? []) const input = createMemo(() => store.custom[store.tab] ?? "") const multi = createMemo(() => question()?.multiple === true) const customPicked = createMemo(() => { const value = input() if (!value) return false return store.answers[store.tab]?.includes(value) ?? false }) function submit() { const answers = questions().map((_, i) => store.answers[i] ?? []) data.replyToQuestion?.({ requestID: props.request.id, answers, }) } function reject() { data.rejectQuestion?.({ requestID: props.request.id, }) } function pick(answer: string, custom: boolean = false) { const answers = [...store.answers] answers[store.tab] = [answer] setStore("answers", answers) if (custom) { const inputs = [...store.custom] inputs[store.tab] = answer setStore("custom", inputs) } if (single()) { data.replyToQuestion?.({ requestID: props.request.id, answers: [[answer]], }) return } setStore("tab", store.tab + 1) } function toggle(answer: string) { const existing = store.answers[store.tab] ?? [] const next = [...existing] const index = next.indexOf(answer) if (index === -1) next.push(answer) if (index !== -1) next.splice(index, 1) const answers = [...store.answers] answers[store.tab] = next setStore("answers", answers) } function selectTab(index: number) { setStore("tab", index) setStore("editing", false) } function selectOption(optIndex: number) { if (optIndex === options().length) { setStore("editing", true) return } const opt = options()[optIndex] if (!opt) return if (multi()) { toggle(opt.label) return } pick(opt.label) } function handleCustomSubmit(e: Event) { e.preventDefault() const value = input().trim() if (!value) { setStore("editing", false) return } if (multi()) { const existing = store.answers[store.tab] ?? [] const next = [...existing] if (!next.includes(value)) next.push(value) const answers = [...store.answers] answers[store.tab] = next setStore("answers", answers) setStore("editing", false) return } pick(value, true) setStore("editing", false) } return (
{(q, index) => { const active = () => index() === store.tab const answered = () => (store.answers[index()]?.length ?? 0) > 0 return ( ) }}
{question()?.question} {multi() ? " " + i18n.t("ui.question.multiHint") : ""}
{(opt, i) => { const picked = () => store.answers[store.tab]?.includes(opt.label) ?? false return ( ) }}
setTimeout(() => el.focus(), 0)} type="text" data-slot="custom-input" placeholder={i18n.t("ui.question.custom.placeholder")} value={input()} onInput={(e) => { const inputs = [...store.custom] inputs[store.tab] = e.currentTarget.value setStore("custom", inputs) }} />
{i18n.t("ui.messagePart.review.title")}
{(q, index) => { const value = () => store.answers[index()]?.join(", ") ?? "" const answered = () => Boolean(value()) return (
{q.question} {answered() ? value() : i18n.t("ui.question.review.notAnswered")}
) }}
) }