/** * Model Runtime Stream Handler * * Processes model-runtime `streamText()` fullStream parts and emits SSE * events in the Data Stream Protocol format. Stream parts map 1:1 to the * framework SSE protocol with minimal field remapping. * * @module agent/runtime/chat-stream-handler */ import type { RuntimeStreamResult } from "./runtime-tool-types.js"; import { type StreamLifecyclePolicy, type StreamOutcome } from "../streaming/lifecycle/index.js"; import type { StreamLifecycleMode } from "./stream-lifecycle-mode.js"; import { createStreamLifecycleShadow, type StreamLifecycleShadowReport } from "./stream-lifecycle-shadow.js"; type TraceAttributePrimitive = string | number | boolean; type TraceAttributeValue = TraceAttributePrimitive | readonly TraceAttributePrimitive[]; export interface StreamingToolCall { id: string; name: string; arguments: string; inputDeltas?: string[]; inputAnnounced?: boolean; inputAvailable?: boolean; providerExecuted?: boolean; dynamic?: boolean; } export interface StreamingToolResult { toolCallId: string; toolName: string; output?: unknown; error?: unknown; providerExecuted?: boolean; dynamic?: boolean; preliminary?: boolean; } /** * Flush a tool call's buffered `tool-input-start` and input deltas to the * client. * * The start event is withheld until the call commits — `tool-input-end`, * `tool-input-available` or `tool-call`. `tool-call` rebuilds the entry from * its own `toolName` and announces from there, so a name that supersedes the * one seen at `tool-input-start` is the one the client is given, and the * superseded name never reaches the wire. * * Idempotent via `inputAnnounced`, which is also what gates the terminal * `tool-output-error`. A call whose stream ended before any commit event was * never announced, so it must be announced here before its failure can * render. Such a call has no superseding name to wait for: the event that * would carry one never arrived, and `inputAvailable` stays false, which is * what makes that terminal path reachable at all. */ export declare function announceStreamedToolCallInput(controller: ReadableStreamDefaultController, encoder: TextEncoder, toolCall: StreamingToolCall): void; export interface StreamingReasoningPart { id: string; text: string; signature?: string; redactedData?: string; } export interface ChatStreamState { accumulatedText: string; reasoningParts: StreamingReasoningPart[]; finishReason: string | null; providerMetadata?: Record; toolCalls: Map; toolResults: StreamingToolResult[]; suppressedToolCalls: { id: string; name: string; }[]; usage: { promptTokens: number; completionTokens: number; totalTokens: number; cachedInputTokens?: number; cacheCreationInputTokens?: number; cacheReadInputTokens?: number; reasoningTokens?: number; billableInputTokens?: number; billableOutputTokens?: number; costUsd?: number; providerInputCostUsd?: number; providerOutputCostUsd?: number; providerCostUsd?: number; veryfrontInputChargeUsd?: number; veryfrontOutputChargeUsd?: number; veryfrontChargeUsd?: number; veryfrontBilledUsd?: number; costCredits?: number; costSource?: "gateway" | "missing" | "partial"; billingMode?: "direct" | "deferred"; usageCaptureStatus?: "complete" | "partial" | "missing"; }; streamOutcome?: StreamOutcome; } export interface ChatStreamCallbacks { onChunk?: (chunk: string) => void; onUsage?: (usage: { promptTokens?: number; completionTokens?: number; totalTokens?: number; cachedInputTokens?: number; cacheCreationInputTokens?: number; cacheReadInputTokens?: number; reasoningTokens?: number; billableInputTokens?: number; billableOutputTokens?: number; costUsd?: number; providerInputCostUsd?: number; providerOutputCostUsd?: number; providerCostUsd?: number; veryfrontInputChargeUsd?: number; veryfrontOutputChargeUsd?: number; veryfrontChargeUsd?: number; veryfrontBilledUsd?: number; costCredits?: number; costSource?: "gateway" | "missing" | "partial"; billingMode?: "direct" | "deferred"; usageCaptureStatus?: "complete" | "partial" | "missing"; }) => void; providerExecutedToolNames?: readonly string[]; availableToolNames?: readonly string[]; localToolInputIdleTimeoutMs?: number; localToolCommitGraceMs?: number; streamIdleTimeoutMs?: number; streamLifecycleMode?: StreamLifecycleMode; streamLifecyclePolicy?: Partial; onLifecycleShadowReport?: (report: StreamLifecycleShadowReport) => void; /** @internal Host timer seam for deterministic stream-lifecycle tests. */ setTimeoutFn?: typeof setTimeout; /** @internal Host timer seam for deterministic stream-lifecycle tests. */ clearTimeoutFn?: typeof clearTimeout; traceSpanName?: string; traceAttributes?: Record; } export declare function summarizeProviderToolDebugValue(value: unknown): unknown; export declare function createStreamState(): ChatStreamState; /** * Process the model-runtime fullStream and emit SSE events. * * Stream parts map directly to our Data Stream Protocol SSE events: * - text-delta → text-delta SSE (with id and delta) * - tool-input-start → tool-input-start SSE * - tool-input-delta → tool-input-delta SSE * - tool-call → tool-input-available SSE (accumulated input) * - finish → captures finishReason and usage */ export interface RuntimeStreamSource { open(signal: AbortSignal): RuntimeStreamResult; } export declare function createRuntimeStreamSource(open: (signal: AbortSignal) => RuntimeStreamResult): RuntimeStreamSource; export declare function isRuntimeStreamSource(value: RuntimeStreamResult | RuntimeStreamSource): value is RuntimeStreamSource; export declare function resolveRuntimeLifecyclePolicy(callbacks?: ChatStreamCallbacks): StreamLifecyclePolicy; interface ProcessStreamInternals { createShadow: typeof createStreamLifecycleShadow; } export declare function processStream(result: RuntimeStreamResult | RuntimeStreamSource, state: ChatStreamState, controller: ReadableStreamDefaultController, encoder: TextEncoder, textPartId: string | undefined, callbacks?: ChatStreamCallbacks, abortSignal?: AbortSignal): Promise; export declare function processStreamInternal(resultOrSource: RuntimeStreamResult | RuntimeStreamSource, state: ChatStreamState, controller: ReadableStreamDefaultController, encoder: TextEncoder, textPartId: string | undefined, callbacks: ChatStreamCallbacks | undefined, abortSignal: AbortSignal | undefined, internals: ProcessStreamInternals): Promise; export {}; //# sourceMappingURL=chat-stream-handler.d.ts.map