UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

315 lines • 12.4 kB
function isRecord(value) { return typeof value === "object" && value !== null && !Array.isArray(value); } function readRecord(record, key) { const value = record?.[key]; return isRecord(value) ? value : undefined; } function readArray(record, key) { const value = record?.[key]; return Array.isArray(value) ? value : []; } function readString(record, key) { const value = record?.[key]; return typeof value === "string" ? value : undefined; } function readStringArray(record, key) { return readArray(record, key).filter((value) => typeof value === "string"); } function formatProviderName(message) { return message.provider ? `${message.provider} · ` : ""; } function pluralize(count, singular, plural = `${singular}s`) { return `${count} ${count === 1 ? singular : plural}`; } function shorten(value, max) { return value.length <= max ? value : `${value.slice(0, max - 1)}…`; } function quoted(value) { return JSON.stringify(value); } function unique(values) { return [...new Set(values)]; } function getWebSearchResults(raw) { if (!isRecord(raw) || !Array.isArray(raw.content)) { return undefined; } return raw.content.filter(isRecord).filter((item) => readString(item, "type") === "web_search_result"); } function sourceFromRecord(record) { if (!record) return undefined; const title = readString(record, "title") ?? readString(record, "name"); const url = readString(record, "url") ?? readString(record, "uri") ?? readString(record, "retrievedUrl") ?? readString(record, "sourceUrl"); const snippet = readString(record, "snippet") ?? readString(record, "text") ?? readString(record, "summary") ?? readString(record, "page_age"); const status = readString(record, "status") ?? readString(record, "urlRetrievalStatus"); if (!title && !url && !snippet && !status) return undefined; return { ...(title && { title }), ...(url && { url }), ...(snippet && { snippet }), ...(status && { status }) }; } function sourceFromGoogleGroundingChunk(record) { return sourceFromRecord(readRecord(record, "web") ?? record); } function formatQueries(queries) { const values = unique(queries); if (values.length === 0) return []; if (values.length === 1) return [`query: ${quoted(values[0])}`]; return ["queries:", ...values.map((query) => `- ${quoted(query)}`)]; } function formatSources(sources, label, expanded) { if (sources.length === 0) return []; const visibleSources = expanded ? sources : sources.slice(0, 3); const lines = [pluralize(sources.length, label)]; for (const source of visibleSources) { const title = source.title ? shorten(source.title, 100) : undefined; const url = source.url ? shorten(source.url, 120) : undefined; const status = source.status ? ` (${source.status})` : ""; if (title && url) { lines.push(`${title} ${url}${status}`); } else if (title) { lines.push(`${title}${status}`); } else if (url) { lines.push(`${url}${status}`); } else if (source.status) { lines.push(source.status); } if (source.snippet) { lines.push(` ${shorten(source.snippet, 160)}`); } } if (!expanded && sources.length > visibleSources.length) { lines.push(`… ${sources.length - visibleSources.length} more ${label}s`); } return lines; } /** Decoded size of a base64 payload, computed without allocating the bytes. */ function base64ByteLength(value) { const padding = value.endsWith("==") ? 2 : value.endsWith("=") ? 1 : 0; return Math.max(0, Math.floor(value.length / 4) * 3 - padding); } /** * Renders an OpenAI `image_generation_call` without ever echoing the payload. * The base64 result is deliberately reduced to a byte count, and no path is shown: * the file only exists once the message_end externalization pass has replaced this * block with text, which this renderer never sees. */ function formatImageGenerationCallBody(raw) { const record = isRecord(raw) ? raw : undefined; if (!record) return undefined; const status = readString(record, "status"); const result = readString(record, "result"); const revisedPrompt = readString(record, "revised_prompt"); const lines = [ ...(status ? [`status: ${status}`] : []), ...(result ? [`${base64ByteLength(result)} bytes`] : []), ...(revisedPrompt?.trim() ? [`revised prompt: ${shorten(revisedPrompt, 200)}`] : []), ]; return lines.length > 0 ? lines.join("\n") : undefined; } function formatServerToolUseBody(raw) { const record = isRecord(raw) ? raw : undefined; const input = readRecord(record, "input"); const query = readString(input, "query"); if (query) { return `query: ${quoted(query)}`; } const name = readString(record, "name"); if (name) { return `name: ${name}`; } return undefined; } /** * Anthropic answers a `tool_search_tool_bm25` / `_regex` server call with either a * `tool_search_tool_search_result` carrying `tool_reference` blocks, or a * `tool_search_tool_result_error` carrying `error_code` / `error_message` (still HTTP 200). */ function formatToolSearchResultBody(raw, expanded) { const content = readRecord(isRecord(raw) ? raw : undefined, "content"); if (!content) { return undefined; } const errorCode = readString(content, "error_code"); if (errorCode) { const message = readString(content, "error_message"); return message ? `${errorCode}: ${shorten(message, 200)}` : errorCode; } const names = unique(readArray(content, "tool_references").flatMap((reference) => { const name = readString(isRecord(reference) ? reference : undefined, "tool_name"); return name ? [name] : []; })); if (names.length === 0) { return "0 tools"; } const visible = expanded ? names : names.slice(0, 10); const lines = [pluralize(names.length, "tool"), ...visible.map((name) => shorten(name, 100))]; if (names.length > visible.length) { lines.push(`… ${names.length - visible.length} more tools`); } return lines.join("\n"); } function formatAnthropicWebSearchResultBody(raw, expanded) { const results = getWebSearchResults(raw); if (!results) { return undefined; } if (results.length === 0) { return "0 results"; } const sources = results.flatMap((result) => { const source = sourceFromRecord(result); return source ? [source] : []; }); return formatSources(sources, "result", expanded).join("\n"); } function formatOpenAiWebSearchCallBody(raw, expanded) { const record = isRecord(raw) ? raw : undefined; if (!record) return undefined; const action = readRecord(record, "action"); const status = readString(record, "status"); const queries = [ ...(readString(action, "query") ? [readString(action, "query")] : []), ...readStringArray(action, "queries"), ]; const sources = readArray(action, "sources").flatMap((rawSource) => { const source = sourceFromRecord(isRecord(rawSource) ? rawSource : undefined); return source ? [source] : []; }); const lines = [ ...(status ? [`status: ${status}`] : []), ...formatQueries(queries), ...formatSources(sources, "source", expanded), ]; return lines.length > 0 ? lines.join("\n") : undefined; } function formatGoogleGroundingMetadataBody(raw, expanded) { const record = isRecord(raw) ? raw : undefined; if (!record) return undefined; const queries = readStringArray(record, "webSearchQueries"); const sources = readArray(record, "groundingChunks").flatMap((rawChunk) => { const source = isRecord(rawChunk) ? sourceFromGoogleGroundingChunk(rawChunk) : undefined; return source ? [source] : []; }); const lines = [...formatQueries(queries), ...formatSources(sources, "source", expanded)]; return lines.length > 0 ? lines.join("\n") : undefined; } function formatGoogleUrlContextMetadataBody(raw, expanded) { const record = isRecord(raw) ? raw : undefined; if (!record) return undefined; const sources = readArray(record, "urlMetadata").flatMap((rawUrl) => { const source = sourceFromRecord(isRecord(rawUrl) ? rawUrl : undefined); return source ? [source] : []; }); const lines = formatSources(sources, "url", expanded); return lines.length > 0 ? lines.join("\n") : undefined; } function formatSpecializedProviderNativeSummary(message, content, expanded) { const provider = formatProviderName(message); const marker = expanded ? "▾" : "▸"; if (content.subtype === "server_tool_use") { const raw = isRecord(content.raw) ? content.raw : undefined; const name = readString(raw, "name"); if (name) { return `${marker} ${provider}${name} · server_tool_use`; } } if (content.subtype === "web_search_tool_result") { return `${marker} ${provider}web_search results`; } if (content.subtype === "tool_search_tool_result") { return `${marker} ${provider}tool_search results`; } if (content.subtype === "web_search_call") { const raw = isRecord(content.raw) ? content.raw : undefined; const status = readString(raw, "status"); return `${marker} ${provider}web_search${status ? ` · ${status}` : " · web_search_call"}`; } if (content.subtype === "image_generation_call") { const raw = isRecord(content.raw) ? content.raw : undefined; const status = readString(raw, "status"); return `${marker} ${provider}image_generation${status ? ` · ${status}` : " · image_generation_call"}`; } if (content.subtype === "groundingMetadata") { const body = formatGoogleGroundingMetadataBody(content.raw, expanded); if (body) { return `${marker} ${provider}google_search results`; } } if (content.subtype === "urlContextMetadata") { const body = formatGoogleUrlContextMetadataBody(content.raw, expanded); if (body) { return `${marker} ${provider}url_context results`; } } return undefined; } function formatSpecializedProviderNativeBody(content, expanded) { if (content.subtype === "server_tool_use") { return formatServerToolUseBody(content.raw); } if (content.subtype === "web_search_tool_result") { return formatAnthropicWebSearchResultBody(content.raw, expanded); } if (content.subtype === "tool_search_tool_result") { return formatToolSearchResultBody(content.raw, expanded); } if (content.subtype === "web_search_call") { return formatOpenAiWebSearchCallBody(content.raw, expanded); } if (content.subtype === "image_generation_call") { return formatImageGenerationCallBody(content.raw); } if (content.subtype === "groundingMetadata") { return formatGoogleGroundingMetadataBody(content.raw, expanded); } if (content.subtype === "urlContextMetadata") { return formatGoogleUrlContextMetadataBody(content.raw, expanded); } return undefined; } export function stringifyProviderNative(raw) { try { return JSON.stringify(raw, null, 2) ?? "null"; } catch { return String(raw); } } export function formatProviderNativeSummary(message, content, expanded) { const specialized = formatSpecializedProviderNativeSummary(message, content, expanded); if (specialized) { return specialized; } const provider = formatProviderName(message); const marker = expanded ? "▾" : "▸"; return `${marker} ${provider}providerNative · ${content.subtype}`; } export function formatProviderNativeBody(content, expanded) { const specialized = formatSpecializedProviderNativeBody(content, expanded); if (specialized) { return specialized; } const rawJson = stringifyProviderNative(content.raw); if (expanded || rawJson.length <= 2000) { return rawJson; } return `${rawJson.slice(0, 2000)}…`; } //# sourceMappingURL=provider-native-rendering.js.map