/** * What the UI shows for a tool call: a compact line ("Searched the web for: sepsis bundle") with * a status icon, and details (input, output, pages) when the user expands it. */ import type { McpToolBinding, ToolKind, WebSource } from './mcp.ts'; export interface ToolActivity { id: string; /** The model-facing tool name. */ name: string; /** What is happening, in words ("Writing 15 quiz questions"). */ label: string; status: 'running' | 'done' | 'error'; /** While running: what the step is doing right now ("Checking the questions with Python"). */ stage?: string; /** A short result ("12 questions") or error message, shown in the details. */ summary?: string; kind?: ToolKind | 'tool'; /** Server name for MCP tools. */ server?: string; /** The query, page or argument the call is about. */ subject?: string; /** Pages a web tool returned. */ sources?: WebSource[]; /** Arguments and output, clipped, for the details view. */ input?: string; output?: string; } const clip = (text: string, max: number) => (text.length > max ? `${text.slice(0, max - 1)}…` : text); function hostOf(url: string): string { try { return new URL(url).hostname.replace(/^www\./, ''); } catch { return url; } } /** The subject of a call, from its arguments: the search, the pages, or the first text argument. */ export function toolSubject(kind: ToolKind | 'tool', input: Record): string | undefined { const str = (v: unknown) => (typeof v === 'string' && v.trim() ? v.trim() : undefined); if (kind === 'web_search') { const queries = Array.isArray(input.search_queries) ? input.search_queries.map(String).filter(Boolean) : []; return clip(queries[0] ?? str(input.objective) ?? '', 120) || undefined; } if (kind === 'web_fetch') { const urls = Array.isArray(input.urls) ? input.urls.map(String) : []; const hosts = [...new Set(urls.map(hostOf))]; return hosts.length ? clip(hosts.slice(0, 3).join(', ') + (hosts.length > 3 ? ` and ${hosts.length - 3} more` : ''), 120) : undefined; } if (kind === 'web_research') return clip(str(input.input) ?? str(input.question) ?? str(input.query) ?? '', 120) || undefined; for (const v of Object.values(input)) { const s = str(v); if (s) return clip(s, 100); } return undefined; } /** Activity fields for an MCP (or web) call before it runs. */ export function mcpActivity(binding: McpToolBinding, id: string, input: Record): ToolActivity { const subject = toolSubject(binding.kind, input ?? {}); const label = binding.kind === 'web_search' ? `Searching the web${subject ? ` for ${subject}` : ''}` : binding.kind === 'web_fetch' ? `Reading ${subject ?? 'web pages'}` : binding.kind === 'web_research' ? `Researching${subject ? ` ${subject}` : ' on the web'}` : `${binding.serverName}: ${binding.title}`; return { id, name: binding.def.name, label, status: 'running', kind: binding.kind, server: binding.kind === 'mcp' ? binding.serverName : undefined, subject, input: clip(JSON.stringify(input ?? {}, null, 2), 2000) }; } export function clipOutput(text: string): string { return clip(text, 3000); }