import type { ObserverEvent, PromptSection } from "./agentSessionTypes"; import { findSproutToolName, isGenericToolTitle, normalizeToolName, } from "./agentSessionToolCatalog"; import { asRecord, asString, titleCase } from "./agentSessionUtils"; export function extractPromptText(payload: Record): string { const params = asRecord(payload.params); const prompt = params.prompt; if (!Array.isArray(prompt)) return ""; return prompt.map(extractBlockText).filter(Boolean).join("\n"); } export function parsePromptText(text: string): { sections: PromptSection[]; userText: string; userTitle: string; } { const sections = parsePromptSections(text); if (sections.length === 0) { return { sections: [], userText: text.trim(), userTitle: "Prompt" }; } const eventSection = sections.find((section) => section.title.toLowerCase().startsWith("sprout event"), ); const eventContent = eventSection ? extractEventContent(eventSection.body) : ""; const eventKind = eventSection?.title.split(":").slice(1).join(":").trim(); return { sections, userText: eventContent, userTitle: eventKind ? titleCase(eventKind) : "Sprout event", }; } function parsePromptSections(text: string): PromptSection[] { const sections: PromptSection[] = []; let current: PromptSection | null = null; const preamble: string[] = []; for (const line of text.split(/\r?\n/)) { const header = line.match(/^\[([^\]]+)]\s*$/); if (header) { if (current) { sections.push({ title: current.title, body: current.body.trim(), }); } else if (preamble.join("\n").trim()) { sections.push({ title: "Prompt", body: preamble.join("\n").trim() }); } current = { title: header[1], body: "" }; continue; } if (current) { current.body += current.body ? `\n${line}` : line; } else { preamble.push(line); } } if (current) { sections.push({ title: current.title, body: current.body.trim() }); } else if (preamble.join("\n").trim()) { sections.push({ title: "Prompt", body: preamble.join("\n").trim() }); } return sections; } function extractEventContent(body: string): string { const contentMatch = body.match(/^Content:\s*(.*)$/m); return contentMatch?.[1]?.trim() ?? ""; } export function extractContentText(value: unknown): string { if (typeof value === "string") return value; if (Array.isArray(value)) return value.map(extractBlockText).join("\n"); return extractBlockText(value); } export function extractBlockText(value: unknown): string { if (typeof value === "string") return value; if (Array.isArray(value)) return value.map(extractBlockText).join("\n"); const record = asRecord(value); const nestedContent = record.content; const rawOutput = record.rawOutput; const nestedText = nestedContent && typeof nestedContent === "object" ? extractBlockText(nestedContent) : ""; const rawOutputText = rawOutput === undefined || rawOutput === null ? "" : typeof rawOutput === "string" ? rawOutput : JSON.stringify(rawOutput, null, 2); const directText = asString(record.text) ?? asString(record.content); return directText || nestedText || rawOutputText || ""; } export function extractToolArgs( update: Record, ): Record { const candidates = [ update.args, update.arguments, update.input, update.rawInput, ]; for (const candidate of candidates) { if ( candidate && typeof candidate === "object" && !Array.isArray(candidate) ) { return candidate as Record; } } return {}; } export function extractToolIdentity(update: Record): { title: string; toolName: string; sproutToolName: string | null; } { const candidates = collectToolNameCandidates(update); const knownName = candidates .map((candidate) => findSproutToolName(candidate, true)) .find((candidate): candidate is string => Boolean(candidate)) ?? findSproutToolName(JSON.stringify(update), false); const firstSpecific = candidates.find( (candidate) => !isGenericToolTitle(candidate), ); const title = asString(update.title) ?? knownName ?? firstSpecific ?? "Tool call"; return { title, toolName: knownName ?? normalizeToolName(firstSpecific ?? title), sproutToolName: knownName, }; } function collectToolNameCandidates(update: Record): string[] { const args = extractToolArgs(update); const tool = asRecord(update.tool); const input = asRecord(update.input); const rawInput = asRecord(update.rawInput); const candidates = [ update.toolName, update.tool_name, update.name, update.title, update.kind, tool.name, tool.toolName, args.toolName, args.tool_name, args.name, args.method, input.toolName, input.tool_name, input.name, rawInput.toolName, rawInput.tool_name, rawInput.name, ]; return candidates.flatMap((candidate) => { const value = asString(candidate); return value ? [value] : []; }); } export function extractToolResult(update: Record): string { const contentText = extractContentText(update.content); if (contentText) return contentText; return extractBlockText(update.rawOutput); } export function describeTurnStarted(payload: unknown): string { const record = asRecord(payload); const ids = Array.isArray(record.triggeringEventIds) ? record.triggeringEventIds.filter( (id): id is string => typeof id === "string", ) : []; return ids.length > 0 ? `Triggered by ${ids.length === 1 ? "1 event" : `${ids.length} events`}.` : ""; } export function describeSessionResolved(payload: unknown): string { const record = asRecord(payload); const isNewSession = record.isNewSession === true; return isNewSession ? "New session created." : ""; } export function describeRawEvent(event: ObserverEvent): string { const payload = asRecord(event.payload); const method = asString(payload.method); if (method === "session/update") { const update = asRecord(asRecord(payload.params).update); return asString(update.sessionUpdate) ?? method; } return method ?? event.kind; }