import { readStructuredAgentOutput } from "#veryfront/agent/output-schema.ts"; import { AGENT_PROVIDER_AUTH_ERROR, OUTPUT_SCHEMA_INVALID_ERROR, PROVIDER_STREAM_PROTOCOL_ERROR, } from "#veryfront/chat/provider-error-registry.ts"; import { defineSchema, getJsonValueSchema, type JsonValue } from "#veryfront/schemas/index.ts"; import { ExecutorAgentError, executorAgentFailureCode, executorAgentJson, getExecutorAgentFailureCodeSchema, } from "../hosted/executor-agent-schema.ts"; import { getToolResultSequenceSchema } from "#veryfront/runtime/tool-result-sequence.ts"; const getUsageSchema = defineSchema((v) => { const count = v.number().nonnegative().optional(); return v.object({ inputTokens: count, outputTokens: count, totalTokens: count, promptTokens: count, completionTokens: count, reasoningTokens: count, cachedInputTokens: count, cacheReadInputTokens: count, cacheCreationInputTokens: count, cacheCreation1hInputTokens: count, billableInputTokens: count, billableOutputTokens: count, costUsd: count, providerInputCostUsd: count, providerOutputCostUsd: count, providerCostUsd: count, veryfrontInputChargeUsd: count, veryfrontOutputChargeUsd: count, veryfrontChargeUsd: count, veryfrontBilledUsd: count, costCredits: count, inputTokenDetails: v.object({ noCacheTokens: count, cacheReadTokens: count, cacheWriteTokens: count, }).strict().optional(), outputTokenDetails: v.object({ textTokens: count, reasoningTokens: count }).strict().optional(), costSource: v.enum(["gateway", "missing", "partial"] as const).optional(), billingMode: v.enum(["direct", "deferred"] as const).optional(), usageCaptureStatus: v.enum(["complete", "partial", "missing"] as const).optional(), }).strict(); }); /** The runtime data-event vocabulary consumed by the hosted UI converter. */ export const getExecutorDataEventSchema = defineSchema((v) => { const json = getJsonValueSchema(); const id = v.string().min(1); const flags = { providerExecuted: v.boolean().optional(), dynamic: v.boolean().optional(), preliminary: v.boolean().optional(), }; return v.union([ v.object({ type: v.literal("message-start"), messageId: id.optional() }).strict(), v.object({ type: v.enum(["message-finish", "finish"] as const), finishReason: v.string().optional(), usage: getUsageSchema().optional(), totalUsage: getUsageSchema().optional(), data: json.optional(), object: json.optional(), objectEncoding: v.literal("snapshot").optional(), }).strict(), v.object({ type: v.literal("step-start"), privateRuntimeObservation: v.object({ version: v.literal(1), kind: v.literal("step_started"), stepId: v.string().uuid().transform((value) => value.toLowerCase()), }).strict().optional(), }).strict(), v.object({ type: v.literal("step-end"), privateRuntimeObservation: v.object({ version: v.literal(1), kind: v.literal("step_ended"), stepId: v.string().uuid().transform((value) => value.toLowerCase()), }).strict().optional(), }).strict(), v.object({ type: v.enum(["text-start", "text-end", "reasoning-start"] as const), id: id.optional(), privateRuntimeObservation: v.object({ version: v.literal(1), kind: v.literal("step_message"), stepId: v.string().uuid().transform((value) => value.toLowerCase()), messageSpanId: v.string().uuid().transform((value) => value.toLowerCase()), }).strict().optional(), }).strict(), v.object({ type: v.literal("reasoning-end"), id: id.optional(), signature: v.string().optional(), redactedData: v.string().optional(), privateRuntimeObservation: v.object({ version: v.literal(1), kind: v.literal("step_message"), stepId: v.string().uuid().transform((value) => value.toLowerCase()), messageSpanId: v.string().uuid().transform((value) => value.toLowerCase()), }).strict().optional(), }).strict(), v.object({ type: v.enum(["text-delta", "reasoning-delta"] as const), id: id.optional(), delta: v.string(), privateRuntimeObservation: v.object({ version: v.literal(1), kind: v.literal("step_message"), stepId: v.string().uuid().transform((value) => value.toLowerCase()), messageSpanId: v.string().uuid().transform((value) => value.toLowerCase()), }).strict().optional(), }).strict(), v.object({ type: v.literal("tool-input-start"), toolCallId: id, toolName: id, privateToolCallOccurrenceId: v.string().uuid().optional(), privateObservedToolResult: v.literal(true).optional(), ...flags, }) .strict().refine( (event) => event.privateObservedToolResult !== true || event.privateToolCallOccurrenceId === undefined, "Observed tool results cannot carry dispatch admission", ), v.object({ type: v.literal("tool-input-delta"), toolCallId: id, inputTextDelta: v.string().optional(), delta: v.string().optional(), }).strict().refine( (event) => event.inputTextDelta !== undefined || event.delta !== undefined, "Tool input delta is required", ), v.object({ type: v.literal("tool-input-available"), toolCallId: id, toolName: id, input: json, ...flags, }).strict(), // The runtime emitter omits undefined results during JSON serialization. v.object({ type: v.literal("tool-output-available"), toolCallId: id, output: json.optional(), privateToolResultSequence: getToolResultSequenceSchema().optional(), ...flags, }) .strict(), v.object({ type: v.literal("tool-input-error"), toolCallId: id, toolName: id.optional(), input: json.optional(), errorText: v.string(), privateToolResultSequence: getToolResultSequenceSchema().optional(), ...flags, }).strict(), v.object({ type: v.literal("tool-output-denied"), toolCallId: id, privateToolResultSequence: getToolResultSequenceSchema().optional(), }).strict(), v.object({ type: v.literal("tool-output-error"), toolCallId: id, errorText: v.string(), privateToolResultSequence: getToolResultSequenceSchema().optional(), ...flags, }).strict(), v.object({ type: v.literal("error"), error: v.string(), code: v.string().optional() }).strict(), v.object({ type: v.literal("data"), data: v.record(v.string(), json) }).strict(), v.object({ type: v.literal("data-veryfront.runtime_context"), data: json.optional(), privateRuntimeObservation: v.object({ version: v.literal(1), kind: v.literal("execution_entry"), occurrenceId: v.string().uuid().transform((value) => value.toLowerCase()), }).strict().optional(), }).strict(), v.object({ type: v.string().regex(/^data-.+$/), data: json.optional() }).strict(), v.object({ type: v.literal("source-url"), sourceId: id.optional(), url: v.string(), title: v.string().optional(), }).strict(), v.object({ type: v.literal("source-document"), sourceId: id, title: v.string(), mediaType: v.string(), filename: v.string().optional(), }).strict(), v.object({ type: v.literal("file"), url: v.string(), mediaType: v.string(), filename: v.string().optional(), }).strict(), ]); }); export const getExecutorAgentStreamFrameSchema = defineSchema((v) => v.discriminatedUnion("type", [ v.object({ type: v.literal("ready") }).strict(), v.object({ type: v.literal("event"), event: getExecutorDataEventSchema() }).strict(), v.object({ type: v.literal("complete") }).strict(), v.object({ type: v.literal("failure"), phase: v.enum(["setup", "stream"] as const), code: getExecutorAgentFailureCodeSchema(), }).strict(), ]) ); export function parseExecutorDataEvent(input: unknown): JsonValue & { type: string } { const result = getExecutorDataEventSchema().safeParse(input); if (!result.success) throw new ExecutorAgentError("EXECUTOR_AGENT_INVALID_STREAM"); const data = result.data.type === "error" ? (() => { const code = executorAgentFailureCode( { code: "code" in result.data ? result.data.code : undefined }, "EXECUTOR_AGENT_STREAM_FAILED", ); return { type: "error", code, error: code === AGENT_PROVIDER_AUTH_ERROR.code ? AGENT_PROVIDER_AUTH_ERROR.message : code === OUTPUT_SCHEMA_INVALID_ERROR.code ? OUTPUT_SCHEMA_INVALID_ERROR.message : code === PROVIDER_STREAM_PROTOCOL_ERROR.code ? PROVIDER_STREAM_PROTOCOL_ERROR.message : code, }; })() : result.data; const value = executorAgentJson(data, "EXECUTOR_AGENT_INVALID_STREAM"); if ( value === null || typeof value !== "object" || Array.isArray(value) || typeof value.type !== "string" ) { throw new ExecutorAgentError("EXECUTOR_AGENT_INVALID_STREAM"); } if ( (value.type === "message-finish" || value.type === "finish") && (Object.hasOwn(value, "data") || Object.hasOwn(value, "object")) ) { try { const structured = readStructuredAgentOutput(value); if (!structured) { throw new ExecutorAgentError("EXECUTOR_AGENT_INVALID_STREAM"); } return { ...value, data: structured.data, object: structured.data, type: value.type, } as JsonValue & { type: string }; } catch { throw new ExecutorAgentError("EXECUTOR_AGENT_INVALID_STREAM"); } } return { ...value, type: value.type }; }