import type { SessionMessageAssistantTool } from "@opencode/client/promise" import { Badge } from "@opencode/ui/badge" import { useDialog } from "@opencode/ui/context/dialog" import { type UiI18n, useI18n } from "@opencode/ui/context/i18n" import { Icon } from "@opencode/ui/icon" import { IconButton } from "@opencode/ui/icon-button" import { TextShimmer } from "@opencode/ui/text-shimmer" import { Tooltip } from "@opencode/ui/tooltip" import { createMemo, Show } from "solid-js" import { BasicTool } from "../../components/basic-tool" import { currentToolError, currentToolInput, currentToolMetadata, currentToolOutput, executeToolCallCount, } from "../../message/current-tool-state" import { stripAnsi } from "../../tools/ansi" import { ExecuteDetails, toolInput, type ToolProps } from "../../tools/tool-renderer" import { CODEMODE_LLM_TOOL, CODEMODE_SEARCH_TOOL, collapseExecuteCallsFromMetadata, splitToolNamespace, } from "../codemode" function formatExecuteError( status: string | undefined, metadata: ToolProps["metadata"], errorText: string | undefined, outputText: string | undefined, i18n: UiI18n, ) { if (status !== "error" && metadata.error !== true) return undefined const fallback = i18n.t("ui.toolErrorCard.failed") const text = stripAnsi((status === "error" ? errorText : undefined) || outputText || fallback) return text.replace(/^Error:\s*/, "").trim() || fallback } function formatExecuteOutput( status: string | undefined, metadata: ToolProps["metadata"], outputText: string | undefined, ) { if (status !== "completed" || metadata.error === true) return "" const text = (outputText ?? "").trimEnd() return text && text !== "undefined" ? text : "" } /** Whether a parent `execute` tool part has finished streaming code that can be inspected in `ExecuteDetails`. */ export function canViewExecuteCode(tool: SessionMessageAssistantTool | undefined): tool is SessionMessageAssistantTool { if (!tool || tool.state.status === "streaming") return false return !!(toolInput(currentToolInput(tool)).code ?? "").trimEnd() } /** Hover-only `` button that opens `ExecuteDetails` for the parent `execute` script of a flattened row. */ export function ExecuteCodeAction(props: { tool: SessionMessageAssistantTool }) { const i18n = useI18n() const dialog = useDialog() const code = createMemo(() => (toolInput(currentToolInput(props.tool)).code ?? "").trimEnd()) const error = createMemo(() => formatExecuteError( props.tool.state.status, currentToolMetadata(props.tool), currentToolError(props.tool), currentToolOutput(props.tool), i18n, ), ) const output = createMemo(() => formatExecuteOutput(props.tool.state.status, currentToolMetadata(props.tool), currentToolOutput(props.tool)), ) const openDetails = (event: MouseEvent) => { event.stopPropagation() dialog.show(() => ( )) } return ( } size="normal" variant="ghost" aria-label={i18n.t("ui.tool.execute.viewCode")} aria-haspopup="dialog" onClick={openDetails} /> ) } /** * The New timeline's `Script` row, used when a Code Mode `execute` call stays unflattened * (0-call pure-JS scripts, streaming scripts, or > K fan-out loops). * * Shows a single clean row with a concise summary and a `` button to inspect the JS code * and return output / error inside the modal. Never dumps an inline Output row in the timeline. */ export function ScriptTool(props: ToolProps) { const i18n = useI18n() const dialog = useDialog() const pending = () => props.status === "streaming" || props.status === "running" const code = createMemo(() => (toolInput(props.input).code ?? "").trimEnd()) const error = createMemo(() => formatExecuteError(props.status, props.metadata, props.error, props.output, i18n)) const output = createMemo(() => formatExecuteOutput(props.status, props.metadata, props.output)) const summary = createMemo(() => scriptSummary(props.metadata, props.status, i18n)) const viewable = () => props.status !== "streaming" && !!code() const openDetails = () => dialog.show(() => ) return (
{(text) => {text()}} {i18n.t("ui.toolErrorCard.failed")}
} action={ viewable() ? ( } size="normal" variant="ghost" aria-label={i18n.t("ui.tool.execute.viewCode")} aria-haspopup="dialog" onClick={openDetails} /> ) : undefined } /> ) } function scriptSummary(metadata: ToolProps["metadata"], status: ToolProps["status"], i18n: UiI18n): string | undefined { if (metadata.jobID !== undefined || (status === "completed" && metadata.background === true)) return i18n.t("ui.timeline.script.backgrounded") const total = executeToolCallCount(metadata) if (total === 0) return undefined const collapsed = collapseExecuteCallsFromMetadata(metadata) if (collapsed.length === 0) return i18n.plural("ui.tool.execute.calls", total) let reads = 0 let searches = 0 let commands = 0 let modelCalls = 0 let subagents = 0 let edits = 0 const namespaces = new Set() collapsed.forEach((call) => { const n = call.count if (call.tool === "read") { reads += n return } if (call.tool === "glob" || call.tool === "grep" || call.tool === "list" || call.tool === CODEMODE_SEARCH_TOOL) { searches += n return } if (call.tool === "shell") { commands += n return } if (call.tool === CODEMODE_LLM_TOOL) { modelCalls += n return } if (call.tool === "subagent") { subagents += n return } if (call.tool === "edit" || call.tool === "write" || call.tool === "patch") { edits += n return } const ns = splitToolNamespace(call.tool)?.namespace if (ns) namespaces.add(ns) }) const parts = [ ...(reads > 0 ? [i18n.plural("ui.timeline.activity.reads", reads)] : []), ...(searches > 0 ? [i18n.plural("ui.timeline.activity.searches", searches)] : []), ...(commands > 0 ? [i18n.plural("ui.timeline.activity.commands", commands)] : []), ...(modelCalls > 0 ? [i18n.plural("ui.timeline.activity.modelCalls", modelCalls)] : []), ...(subagents > 0 ? [i18n.plural("ui.timeline.activity.subagents", subagents)] : []), ...(edits > 0 ? [i18n.plural("ui.timeline.activity.editedFiles", edits)] : []), ...[...namespaces], ] if (parts.length > 0) return parts.join(", ") return i18n.plural("ui.tool.execute.calls", total) }