|
| 1 | +import { atomicTarget, bashFailureStatus, observedBashLines, sanitizeAtomicLabel } from "./atomic-tool-observation.ts"; |
| 2 | +import type { ToolExecutionIdentity, ToolExecutionRenderState } from "./tool-execution-types.ts"; |
| 3 | + |
| 4 | +const PASSTHROUGH_TOOLS = new Set(["monitor", "todo", "create_goal", "get_goal", "update_goal"]); |
| 5 | +const STATUS_TOOLS = new Set([ |
| 6 | + "task", |
| 7 | + "task_create", |
| 8 | + "task_get", |
| 9 | + "task_list", |
| 10 | + "task_update", |
| 11 | + "team_create", |
| 12 | + "team_delete", |
| 13 | +]); |
| 14 | + |
| 15 | +function record(value: unknown): Record<string, unknown> | undefined { |
| 16 | + return typeof value === "object" && value !== null ? (value as Record<string, unknown>) : undefined; |
| 17 | +} |
| 18 | + |
| 19 | +function count(value: unknown): number | undefined { |
| 20 | + return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : undefined; |
| 21 | +} |
| 22 | + |
| 23 | +function plural(value: number, noun: string): string { |
| 24 | + return `${value} ${noun}${value === 1 ? "" : "s"}`; |
| 25 | +} |
| 26 | + |
| 27 | +function knownCount(toolName: string, args: unknown, details: unknown): string | undefined { |
| 28 | + const detailFields = record(details); |
| 29 | + const argFields = record(args); |
| 30 | + switch (toolName) { |
| 31 | + case "read": { |
| 32 | + const totalLines = count(record(detailFields?.truncation)?.totalLines); |
| 33 | + return totalLines === undefined ? undefined : plural(totalLines, "line"); |
| 34 | + } |
| 35 | + case "lsp_diagnostics": { |
| 36 | + const diagnostics = count(detailFields?.totalDiagnostics); |
| 37 | + return diagnostics === undefined ? undefined : plural(diagnostics, "diagnostic"); |
| 38 | + } |
| 39 | + case "lsp_find_references": { |
| 40 | + const references = |
| 41 | + count(detailFields?.totalReferences) ?? |
| 42 | + (Array.isArray(detailFields?.references) ? detailFields.references.length : undefined); |
| 43 | + return references === undefined ? undefined : plural(references, "reference"); |
| 44 | + } |
| 45 | + case "lsp_symbols": { |
| 46 | + const symbols = |
| 47 | + count(detailFields?.totalSymbols) ?? |
| 48 | + (Array.isArray(detailFields?.symbols) ? detailFields.symbols.length : undefined); |
| 49 | + return symbols === undefined ? undefined : plural(symbols, "symbol"); |
| 50 | + } |
| 51 | + case "web_search": { |
| 52 | + const results = |
| 53 | + count(detailFields?.totalResults) ?? |
| 54 | + (Array.isArray(detailFields?.results) ? detailFields.results.length : undefined); |
| 55 | + return results === undefined ? undefined : plural(results, "result"); |
| 56 | + } |
| 57 | + case "multi_tool_use.parallel": |
| 58 | + return Array.isArray(argFields?.tool_uses) ? plural(argFields.tool_uses.length, "call") : undefined; |
| 59 | + case "task": |
| 60 | + return Array.isArray(argFields?.tasks) ? plural(argFields.tasks.length, "task") : undefined; |
| 61 | + case "team_create": { |
| 62 | + const members = record(argFields?.inline_spec)?.members; |
| 63 | + return Array.isArray(members) ? plural(members.length, "member") : undefined; |
| 64 | + } |
| 65 | + default: |
| 66 | + return undefined; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function knownStatus(toolName: string, state: ToolExecutionRenderState): string | undefined { |
| 71 | + if (STATUS_TOOLS.has(toolName)) { |
| 72 | + const details = record(state.result?.details); |
| 73 | + const status = typeof details?.status === "string" ? sanitizeAtomicLabel(details.status) : undefined; |
| 74 | + if (status) return status; |
| 75 | + if (state.isPartial) { |
| 76 | + const activity = record(details?.progress)?.activity; |
| 77 | + const partial = details?.phase ?? activity; |
| 78 | + return typeof partial === "string" ? sanitizeAtomicLabel(partial) : undefined; |
| 79 | + } |
| 80 | + } |
| 81 | + return state.result?.isError ? "failed" : undefined; |
| 82 | +} |
| 83 | + |
| 84 | +export function isAtomicToolPassthrough(identity: ToolExecutionIdentity): boolean { |
| 85 | + return identity.trustedBuiltIn && PASSTHROUGH_TOOLS.has(identity.toolName); |
| 86 | +} |
| 87 | + |
| 88 | +export class AtomicToolMetadata { |
| 89 | + readonly name: string; |
| 90 | + readonly supportsProgressSpinner: boolean; |
| 91 | + target: string | undefined; |
| 92 | + facts: string | undefined; |
| 93 | + isError = false; |
| 94 | + private readonly identity: ToolExecutionIdentity; |
| 95 | + private retainedLines?: number; |
| 96 | + private retainedLinesTruncated = false; |
| 97 | + private retainedCalls?: number; |
| 98 | + |
| 99 | + constructor(identity: ToolExecutionIdentity, state: ToolExecutionRenderState) { |
| 100 | + this.identity = identity; |
| 101 | + this.name = sanitizeAtomicLabel(identity.toolName); |
| 102 | + this.supportsProgressSpinner = |
| 103 | + identity.trustedBuiltIn && (identity.toolName === "bash" || identity.toolName === "eval"); |
| 104 | + this.update(state); |
| 105 | + } |
| 106 | + |
| 107 | + update(state: ToolExecutionRenderState): void { |
| 108 | + this.target = undefined; |
| 109 | + this.facts = undefined; |
| 110 | + this.isError = false; |
| 111 | + try { |
| 112 | + this.isError = state.result?.isError === true; |
| 113 | + this.target = atomicTarget(this.identity, state.args); |
| 114 | + if (this.identity.trustedBuiltIn && this.identity.toolName === "bash") { |
| 115 | + const observed = observedBashLines(state.result); |
| 116 | + if (observed) { |
| 117 | + if (observed.count > (this.retainedLines ?? 0)) { |
| 118 | + this.retainedLines = observed.count; |
| 119 | + this.retainedLinesTruncated = observed.truncated; |
| 120 | + } else if (observed.truncated) { |
| 121 | + this.retainedLinesTruncated = true; |
| 122 | + } |
| 123 | + } |
| 124 | + } else if (this.identity.trustedBuiltIn && this.identity.toolName === "eval") { |
| 125 | + const calls = record(state.result?.details)?.toolCalls; |
| 126 | + if (Array.isArray(calls)) this.retainedCalls = Math.max(this.retainedCalls ?? 0, calls.length); |
| 127 | + } |
| 128 | + this.facts = this.buildFacts(state); |
| 129 | + } catch { |
| 130 | + this.facts = undefined; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private buildFacts(state: ToolExecutionRenderState): string | undefined { |
| 135 | + if (this.identity.trustedBuiltIn && this.identity.toolName === "bash" && this.retainedLines !== undefined) { |
| 136 | + const suffix = this.retainedLinesTruncated ? "+" : ""; |
| 137 | + const lines = `${this.retainedLines}${suffix} line${this.retainedLines === 1 ? "" : "s"}`; |
| 138 | + return [lines, bashFailureStatus(state.result)].filter(Boolean).join(" · "); |
| 139 | + } |
| 140 | + if (this.identity.trustedBuiltIn && this.identity.toolName === "eval" && this.retainedCalls !== undefined) { |
| 141 | + return plural(this.retainedCalls, "call"); |
| 142 | + } |
| 143 | + if (!this.identity.trustedBuiltIn) return this.isError ? "failed" : undefined; |
| 144 | + return ( |
| 145 | + [ |
| 146 | + knownCount(this.identity.toolName, state.args, state.result?.details), |
| 147 | + knownStatus(this.identity.toolName, state), |
| 148 | + ] |
| 149 | + .filter(Boolean) |
| 150 | + .join(" · ") || undefined |
| 151 | + ); |
| 152 | + } |
| 153 | +} |
0 commit comments