@tanstack/ai 0.36.0 → 0.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/activities/chat/adapter.d.ts +163 -0
- package/dist/esm/activities/chat/adapter.js +17 -0
- package/dist/esm/activities/chat/adapter.js.map +1 -0
- package/dist/esm/activities/chat/agent-loop-strategies.d.ts +59 -0
- package/dist/esm/activities/chat/agent-loop-strategies.js +23 -0
- package/dist/esm/activities/chat/agent-loop-strategies.js.map +1 -0
- package/dist/esm/activities/chat/index.d.ts +270 -0
- package/dist/esm/activities/chat/index.js +1724 -0
- package/dist/esm/activities/chat/index.js.map +1 -0
- package/dist/esm/activities/chat/mcp/manager.d.ts +25 -0
- package/dist/esm/activities/chat/mcp/manager.js +71 -0
- package/dist/esm/activities/chat/mcp/manager.js.map +1 -0
- package/dist/esm/activities/chat/mcp/types.d.ts +56 -0
- package/dist/esm/activities/chat/messages.d.ts +78 -0
- package/dist/esm/activities/chat/messages.js +374 -0
- package/dist/esm/activities/chat/messages.js.map +1 -0
- package/dist/esm/activities/chat/middleware/builder.d.ts +46 -0
- package/dist/esm/activities/chat/middleware/builder.js +17 -0
- package/dist/esm/activities/chat/middleware/builder.js.map +1 -0
- package/dist/esm/activities/chat/middleware/capabilities.d.ts +93 -0
- package/dist/esm/activities/chat/middleware/capabilities.js +45 -0
- package/dist/esm/activities/chat/middleware/capabilities.js.map +1 -0
- package/dist/esm/activities/chat/middleware/compose.d.ts +87 -0
- package/dist/esm/activities/chat/middleware/compose.js +510 -0
- package/dist/esm/activities/chat/middleware/compose.js.map +1 -0
- package/dist/esm/activities/chat/middleware/define.d.ts +20 -0
- package/dist/esm/activities/chat/middleware/define.js +7 -0
- package/dist/esm/activities/chat/middleware/define.js.map +1 -0
- package/dist/esm/activities/chat/middleware/index.d.ts +10 -0
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.d.ts +89 -0
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.js +76 -0
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.js.map +1 -0
- package/dist/esm/activities/chat/middleware/types.d.ts +405 -0
- package/dist/esm/activities/chat/middleware/validate.d.ts +19 -0
- package/dist/esm/activities/chat/middleware/validate.js +30 -0
- package/dist/esm/activities/chat/middleware/validate.js.map +1 -0
- package/dist/esm/activities/chat/runtime-context-types.d.ts +43 -0
- package/dist/esm/activities/chat/stream/index.d.ts +11 -0
- package/dist/esm/activities/chat/stream/json-parser.d.ts +38 -0
- package/dist/esm/activities/chat/stream/json-parser.js +28 -0
- package/dist/esm/activities/chat/stream/json-parser.js.map +1 -0
- package/dist/esm/activities/chat/stream/message-updaters.d.ts +81 -0
- package/dist/esm/activities/chat/stream/message-updaters.js +253 -0
- package/dist/esm/activities/chat/stream/message-updaters.js.map +1 -0
- package/dist/esm/activities/chat/stream/processor.d.ts +439 -0
- package/dist/esm/activities/chat/stream/processor.js +1426 -0
- package/dist/esm/activities/chat/stream/processor.js.map +1 -0
- package/dist/esm/activities/chat/stream/strategies.d.ts +43 -0
- package/dist/esm/activities/chat/stream/strategies.js +54 -0
- package/dist/esm/activities/chat/stream/strategies.js.map +1 -0
- package/dist/esm/activities/chat/stream/types.d.ts +90 -0
- package/dist/esm/activities/chat/tools/lazy-tool-manager.d.ts +82 -0
- package/dist/esm/activities/chat/tools/lazy-tool-manager.js +194 -0
- package/dist/esm/activities/chat/tools/lazy-tool-manager.js.map +1 -0
- package/dist/esm/activities/chat/tools/lazy-tools.d.ts +15 -0
- package/dist/esm/activities/chat/tools/lazy-tools.js +16 -0
- package/dist/esm/activities/chat/tools/lazy-tools.js.map +1 -0
- package/dist/esm/activities/chat/tools/schema-converter.d.ts +140 -0
- package/dist/esm/activities/chat/tools/schema-converter.js +167 -0
- package/dist/esm/activities/chat/tools/schema-converter.js.map +1 -0
- package/dist/esm/activities/chat/tools/tool-calls.d.ts +140 -0
- package/dist/esm/activities/chat/tools/tool-calls.js +512 -0
- package/dist/esm/activities/chat/tools/tool-calls.js.map +1 -0
- package/dist/esm/activities/chat/tools/tool-definition.d.ts +135 -0
- package/dist/esm/activities/chat/tools/tool-definition.js +25 -0
- package/dist/esm/activities/chat/tools/tool-definition.js.map +1 -0
- package/dist/esm/activities/error-payload.d.ts +30 -0
- package/dist/esm/activities/error-payload.js +54 -0
- package/dist/esm/activities/error-payload.js.map +1 -0
- package/dist/esm/activities/generateAudio/adapter.d.ts +62 -0
- package/dist/esm/activities/generateAudio/adapter.js +16 -0
- package/dist/esm/activities/generateAudio/adapter.js.map +1 -0
- package/dist/esm/activities/generateAudio/index.d.ts +85 -0
- package/dist/esm/activities/generateAudio/index.js +114 -0
- package/dist/esm/activities/generateAudio/index.js.map +1 -0
- package/dist/esm/activities/generateImage/adapter.d.ts +78 -0
- package/dist/esm/activities/generateImage/adapter.js +16 -0
- package/dist/esm/activities/generateImage/adapter.js.map +1 -0
- package/dist/esm/activities/generateImage/index.d.ts +134 -0
- package/dist/esm/activities/generateImage/index.js +121 -0
- package/dist/esm/activities/generateImage/index.js.map +1 -0
- package/dist/esm/activities/generateSpeech/adapter.d.ts +62 -0
- package/dist/esm/activities/generateSpeech/adapter.js +16 -0
- package/dist/esm/activities/generateSpeech/adapter.js.map +1 -0
- package/dist/esm/activities/generateSpeech/index.d.ts +96 -0
- package/dist/esm/activities/generateSpeech/index.js +119 -0
- package/dist/esm/activities/generateSpeech/index.js.map +1 -0
- package/dist/esm/activities/generateTranscription/adapter.d.ts +62 -0
- package/dist/esm/activities/generateTranscription/adapter.js +16 -0
- package/dist/esm/activities/generateTranscription/adapter.js.map +1 -0
- package/dist/esm/activities/generateTranscription/index.d.ts +109 -0
- package/dist/esm/activities/generateTranscription/index.js +109 -0
- package/dist/esm/activities/generateTranscription/index.js.map +1 -0
- package/dist/esm/activities/generateVideo/adapter.d.ts +145 -0
- package/dist/esm/activities/generateVideo/adapter.js +30 -0
- package/dist/esm/activities/generateVideo/adapter.js.map +1 -0
- package/dist/esm/activities/generateVideo/index.d.ts +223 -0
- package/dist/esm/activities/generateVideo/index.js +295 -0
- package/dist/esm/activities/generateVideo/index.js.map +1 -0
- package/dist/esm/activities/generateVideo/snap.d.ts +14 -0
- package/dist/esm/activities/generateVideo/snap.js +54 -0
- package/dist/esm/activities/generateVideo/snap.js.map +1 -0
- package/dist/esm/activities/index.d.ts +27 -0
- package/dist/esm/activities/index.js +43 -0
- package/dist/esm/activities/index.js.map +1 -0
- package/dist/esm/activities/middleware/index.d.ts +2 -0
- package/dist/esm/activities/middleware/run.d.ts +20 -0
- package/dist/esm/activities/middleware/run.js +42 -0
- package/dist/esm/activities/middleware/run.js.map +1 -0
- package/dist/esm/activities/middleware/types.d.ts +118 -0
- package/dist/esm/activities/stream-generation-result.d.ts +15 -0
- package/dist/esm/activities/stream-generation-result.js +49 -0
- package/dist/esm/activities/stream-generation-result.js.map +1 -0
- package/dist/esm/activities/summarize/adapter.d.ts +74 -0
- package/dist/esm/activities/summarize/adapter.js +16 -0
- package/dist/esm/activities/summarize/adapter.js.map +1 -0
- package/dist/esm/activities/summarize/chat-stream-summarize.d.ts +45 -0
- package/dist/esm/activities/summarize/chat-stream-summarize.js +212 -0
- package/dist/esm/activities/summarize/chat-stream-summarize.js.map +1 -0
- package/dist/esm/activities/summarize/index.d.ts +108 -0
- package/dist/esm/activities/summarize/index.js +113 -0
- package/dist/esm/activities/summarize/index.js.map +1 -0
- package/dist/esm/adapter-internals.d.ts +5 -0
- package/dist/esm/adapter-internals.js +10 -0
- package/dist/esm/adapter-internals.js.map +1 -0
- package/dist/esm/client.d.ts +44 -0
- package/dist/esm/client.js +67 -0
- package/dist/esm/client.js.map +1 -0
- package/dist/esm/extend-adapter.d.ts +152 -0
- package/dist/esm/extend-adapter.js +21 -0
- package/dist/esm/extend-adapter.js.map +1 -0
- package/dist/esm/index.d.ts +45 -0
- package/dist/esm/index.js +109 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/logger/console-logger.d.ts +29 -0
- package/dist/esm/logger/console-logger.js +83 -0
- package/dist/esm/logger/console-logger.js.map +1 -0
- package/dist/esm/logger/internal-logger.d.ts +41 -0
- package/dist/esm/logger/internal-logger.js +86 -0
- package/dist/esm/logger/internal-logger.js.map +1 -0
- package/dist/esm/logger/resolve.d.ts +14 -0
- package/dist/esm/logger/resolve.js +54 -0
- package/dist/esm/logger/resolve.js.map +1 -0
- package/dist/esm/logger/types.d.ts +75 -0
- package/dist/esm/middlewares/content-guard.d.ts +77 -0
- package/dist/esm/middlewares/content-guard.js +156 -0
- package/dist/esm/middlewares/content-guard.js.map +1 -0
- package/dist/esm/middlewares/index.d.ts +2 -0
- package/dist/esm/middlewares/index.js +7 -0
- package/dist/esm/middlewares/index.js.map +1 -0
- package/dist/esm/middlewares/otel.d.ts +81 -0
- package/dist/esm/middlewares/otel.js +745 -0
- package/dist/esm/middlewares/otel.js.map +1 -0
- package/dist/esm/middlewares/tool-cache.d.ts +1 -0
- package/dist/esm/middlewares/usage-attributes.d.ts +24 -0
- package/dist/esm/middlewares/usage-attributes.js +43 -0
- package/dist/esm/middlewares/usage-attributes.js.map +1 -0
- package/dist/esm/realtime/index.d.ts +28 -0
- package/dist/esm/realtime/index.js +8 -0
- package/dist/esm/realtime/index.js.map +1 -0
- package/dist/esm/realtime/types.d.ts +282 -0
- package/dist/esm/stream-to-response.d.ts +102 -0
- package/dist/esm/stream-to-response.js +121 -0
- package/dist/esm/stream-to-response.js.map +1 -0
- package/dist/esm/strip-to-spec-middleware.d.ts +18 -0
- package/dist/esm/strip-to-spec-middleware.js +20 -0
- package/dist/esm/strip-to-spec-middleware.js.map +1 -0
- package/dist/esm/system-prompts.d.ts +66 -0
- package/dist/esm/system-prompts.js +23 -0
- package/dist/esm/system-prompts.js.map +1 -0
- package/dist/esm/tool-registry.d.ts +81 -0
- package/dist/esm/tool-registry.js +49 -0
- package/dist/esm/tool-registry.js.map +1 -0
- package/dist/esm/tools/provider-tool.d.ts +30 -0
- package/dist/esm/tools/provider-tool.js +7 -0
- package/dist/esm/tools/provider-tool.js.map +1 -0
- package/dist/esm/types.d.ts +1594 -0
- package/dist/esm/utilities/ag-ui-wire.d.ts +44 -0
- package/dist/esm/utilities/ag-ui-wire.js +107 -0
- package/dist/esm/utilities/ag-ui-wire.js.map +1 -0
- package/dist/esm/utilities/chat-params.d.ts +85 -0
- package/dist/esm/utilities/chat-params.js +100 -0
- package/dist/esm/utilities/chat-params.js.map +1 -0
- package/dist/esm/utilities/errors.d.ts +13 -0
- package/dist/esm/utilities/errors.js +22 -0
- package/dist/esm/utilities/errors.js.map +1 -0
- package/dist/esm/utilities/media-prompt.d.ts +35 -0
- package/dist/esm/utilities/media-prompt.js +43 -0
- package/dist/esm/utilities/media-prompt.js.map +1 -0
- package/dist/esm/utilities/numbers.d.ts +8 -0
- package/dist/esm/utilities/numbers.js +12 -0
- package/dist/esm/utilities/numbers.js.map +1 -0
- package/dist/esm/utilities/sampling-keys.d.ts +20 -0
- package/dist/esm/utilities/sampling-keys.js +20 -0
- package/dist/esm/utilities/sampling-keys.js.map +1 -0
- package/dist/esm/utilities/tool-result.d.ts +21 -0
- package/dist/esm/utilities/tool-result.js +37 -0
- package/dist/esm/utilities/tool-result.js.map +1 -0
- package/dist/esm/utilities/usage.d.ts +31 -0
- package/dist/esm/utilities/usage.js +11 -0
- package/dist/esm/utilities/usage.js.map +1 -0
- package/dist/esm/utils.d.ts +17 -0
- package/dist/esm/utils.js +20 -0
- package/dist/esm/utils.js.map +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import { ChunkRecording, ChunkStrategy, ProcessorResult, ProcessorState, ToolCallState } from './types.js';
|
|
2
|
+
import { ContentPart, ModelMessage, StreamChunk, UIMessage } from '../../../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Events emitted by the StreamProcessor
|
|
5
|
+
*/
|
|
6
|
+
export interface StreamProcessorEvents {
|
|
7
|
+
onMessagesChange?: (messages: Array<UIMessage>) => void;
|
|
8
|
+
onStreamStart?: () => void;
|
|
9
|
+
onStreamEnd?: (message: UIMessage) => void;
|
|
10
|
+
onError?: (error: Error) => void;
|
|
11
|
+
onToolCall?: (args: {
|
|
12
|
+
toolCallId: string;
|
|
13
|
+
toolName: string;
|
|
14
|
+
input: any;
|
|
15
|
+
}) => void;
|
|
16
|
+
onApprovalRequest?: (args: {
|
|
17
|
+
toolCallId: string;
|
|
18
|
+
toolName: string;
|
|
19
|
+
input: any;
|
|
20
|
+
approvalId: string;
|
|
21
|
+
}) => void;
|
|
22
|
+
onCustomEvent?: (eventType: string, data: unknown, context: {
|
|
23
|
+
toolCallId?: string;
|
|
24
|
+
}) => void;
|
|
25
|
+
onTextUpdate?: (messageId: string, content: string) => void;
|
|
26
|
+
onToolCallStateChange?: (messageId: string, toolCallId: string, state: ToolCallState, args: string) => void;
|
|
27
|
+
onThinkingUpdate?: (messageId: string, stepId: string, content: string) => void;
|
|
28
|
+
onStructuredOutputChange?: (args: {
|
|
29
|
+
phase: 'start' | 'update' | 'complete' | 'error';
|
|
30
|
+
messageId: string;
|
|
31
|
+
status: 'streaming' | 'complete' | 'error';
|
|
32
|
+
raw: string;
|
|
33
|
+
partial?: unknown;
|
|
34
|
+
data?: unknown;
|
|
35
|
+
reasoning?: string;
|
|
36
|
+
errorMessage?: string;
|
|
37
|
+
delta?: string;
|
|
38
|
+
}) => void;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Options for StreamProcessor
|
|
42
|
+
*/
|
|
43
|
+
export interface StreamProcessorOptions {
|
|
44
|
+
chunkStrategy?: ChunkStrategy;
|
|
45
|
+
/** Event-driven handlers */
|
|
46
|
+
events?: StreamProcessorEvents;
|
|
47
|
+
jsonParser?: {
|
|
48
|
+
parse: (jsonString: string) => any;
|
|
49
|
+
};
|
|
50
|
+
/** Enable recording for replay testing */
|
|
51
|
+
recording?: boolean;
|
|
52
|
+
/** Initial messages to populate the processor */
|
|
53
|
+
initialMessages?: Array<UIMessage>;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* StreamProcessor - State machine for processing AI response streams
|
|
57
|
+
*
|
|
58
|
+
* Manages the full UIMessage[] conversation and emits events on changes.
|
|
59
|
+
* Trusts the adapter contract: adapters emit clean AG-UI events in the
|
|
60
|
+
* correct order.
|
|
61
|
+
*
|
|
62
|
+
* State tracking:
|
|
63
|
+
* - Full message array
|
|
64
|
+
* - Per-message stream state (text, tool calls, thinking)
|
|
65
|
+
* - Multiple concurrent message streams
|
|
66
|
+
* - Tool call completion via TOOL_CALL_END events
|
|
67
|
+
*
|
|
68
|
+
* @see docs/chat-architecture.md#streamprocessor-internal-state — State field reference
|
|
69
|
+
* @see docs/chat-architecture.md#adapter-contract — What this class expects from adapters
|
|
70
|
+
*/
|
|
71
|
+
export declare class StreamProcessor {
|
|
72
|
+
private readonly chunkStrategy;
|
|
73
|
+
private readonly events;
|
|
74
|
+
private readonly jsonParser;
|
|
75
|
+
private recordingEnabled;
|
|
76
|
+
private messages;
|
|
77
|
+
private readonly messageStates;
|
|
78
|
+
private readonly activeMessageIds;
|
|
79
|
+
private readonly toolCallToMessage;
|
|
80
|
+
private pendingManualMessageId;
|
|
81
|
+
private pendingThinkingStepId;
|
|
82
|
+
private readonly structuredMessageIds;
|
|
83
|
+
private readonly structuredOutputUpdateBatches;
|
|
84
|
+
private readonly activeRuns;
|
|
85
|
+
private finishReason;
|
|
86
|
+
private hasError;
|
|
87
|
+
private isDone;
|
|
88
|
+
private recording;
|
|
89
|
+
private recordingStartTime;
|
|
90
|
+
constructor(options?: StreamProcessorOptions);
|
|
91
|
+
/**
|
|
92
|
+
* Set the messages array (e.g., from persisted state)
|
|
93
|
+
*/
|
|
94
|
+
setMessages(messages: Array<UIMessage>): void;
|
|
95
|
+
/**
|
|
96
|
+
* Add a user message to the conversation.
|
|
97
|
+
* Supports both simple string content and multimodal content arrays.
|
|
98
|
+
*
|
|
99
|
+
* @param content - The message content (string or array of content parts)
|
|
100
|
+
* @param id - Optional custom message ID (generated if not provided)
|
|
101
|
+
* @returns The created UIMessage
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* // Simple text message
|
|
106
|
+
* processor.addUserMessage('Hello!')
|
|
107
|
+
*
|
|
108
|
+
* // Multimodal message with image
|
|
109
|
+
* processor.addUserMessage([
|
|
110
|
+
* { type: 'text', content: 'What is in this image?' },
|
|
111
|
+
* { type: 'image', source: { type: 'url', value: 'https://example.com/photo.jpg' } }
|
|
112
|
+
* ])
|
|
113
|
+
*
|
|
114
|
+
* // With custom ID
|
|
115
|
+
* processor.addUserMessage('Hello!', 'custom-id-123')
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
addUserMessage(content: string | Array<ContentPart>, id?: string): UIMessage;
|
|
119
|
+
/**
|
|
120
|
+
* Prepare for a new assistant message stream.
|
|
121
|
+
* Does NOT create the message immediately -- the message is created lazily
|
|
122
|
+
* when the first content-bearing chunk arrives via ensureAssistantMessage().
|
|
123
|
+
* This prevents empty assistant messages from flickering in the UI when
|
|
124
|
+
* auto-continuation produces no content.
|
|
125
|
+
*/
|
|
126
|
+
prepareAssistantMessage(): void;
|
|
127
|
+
/**
|
|
128
|
+
* @deprecated Use prepareAssistantMessage() instead. This eagerly creates
|
|
129
|
+
* an assistant message which can cause empty message flicker.
|
|
130
|
+
*/
|
|
131
|
+
startAssistantMessage(messageId?: string): string;
|
|
132
|
+
/**
|
|
133
|
+
* Get the current assistant message ID (if one has been created).
|
|
134
|
+
* Returns null if prepareAssistantMessage() was called but no content
|
|
135
|
+
* has arrived yet.
|
|
136
|
+
*/
|
|
137
|
+
getCurrentAssistantMessageId(): string | null;
|
|
138
|
+
/**
|
|
139
|
+
* Add a tool result (called by client after handling onToolCall)
|
|
140
|
+
*/
|
|
141
|
+
addToolResult(toolCallId: string, output: any, error?: string): void;
|
|
142
|
+
/**
|
|
143
|
+
* Add an approval response (called by client after handling onApprovalRequest)
|
|
144
|
+
*/
|
|
145
|
+
addToolApprovalResponse(approvalId: string, approved: boolean): void;
|
|
146
|
+
/**
|
|
147
|
+
* Get the conversation as ModelMessages (for sending to LLM)
|
|
148
|
+
*/
|
|
149
|
+
toModelMessages(): Array<ModelMessage>;
|
|
150
|
+
/**
|
|
151
|
+
* Get current messages
|
|
152
|
+
*/
|
|
153
|
+
getMessages(): Array<UIMessage>;
|
|
154
|
+
/**
|
|
155
|
+
* Check if all tool calls in the last assistant message are complete
|
|
156
|
+
* Useful for auto-continue logic
|
|
157
|
+
*/
|
|
158
|
+
areAllToolsComplete(): boolean;
|
|
159
|
+
/**
|
|
160
|
+
* Remove messages after a certain index (for reload/retry)
|
|
161
|
+
*/
|
|
162
|
+
removeMessagesAfter(index: number): void;
|
|
163
|
+
/**
|
|
164
|
+
* Clear all messages
|
|
165
|
+
*/
|
|
166
|
+
clearMessages(): void;
|
|
167
|
+
/**
|
|
168
|
+
* Process a stream and emit events through handlers
|
|
169
|
+
*/
|
|
170
|
+
process(stream: AsyncIterable<any>): Promise<ProcessorResult>;
|
|
171
|
+
/**
|
|
172
|
+
* Process a single chunk from the stream.
|
|
173
|
+
*
|
|
174
|
+
* Central dispatch for all AG-UI events. Each event type maps to a specific
|
|
175
|
+
* handler. Events not listed in the switch are intentionally ignored
|
|
176
|
+
* (STEP_STARTED, STATE_SNAPSHOT, STATE_DELTA).
|
|
177
|
+
*
|
|
178
|
+
* @see docs/chat-architecture.md#adapter-contract — Expected event types and ordering
|
|
179
|
+
*/
|
|
180
|
+
processChunk(chunk: StreamChunk): void;
|
|
181
|
+
/**
|
|
182
|
+
* Create a new MessageStreamState for a message
|
|
183
|
+
*/
|
|
184
|
+
private createMessageState;
|
|
185
|
+
/**
|
|
186
|
+
* Get the MessageStreamState for a message
|
|
187
|
+
*/
|
|
188
|
+
private getMessageState;
|
|
189
|
+
/**
|
|
190
|
+
* Promote a pending stepId from a STEP_STARTED that fired before the
|
|
191
|
+
* assistant message existed onto the given message state, so the next
|
|
192
|
+
* thinking event (STEP_FINISHED or REASONING_MESSAGE_CONTENT) attributes
|
|
193
|
+
* to the correct step.
|
|
194
|
+
*/
|
|
195
|
+
private consumePendingThinkingStep;
|
|
196
|
+
/**
|
|
197
|
+
* Get the most recent active assistant message ID.
|
|
198
|
+
* Used as fallback for events that don't include a messageId.
|
|
199
|
+
*/
|
|
200
|
+
private getActiveAssistantMessageId;
|
|
201
|
+
/**
|
|
202
|
+
* Ensure an active assistant message exists, creating one if needed.
|
|
203
|
+
* Used for backward compat when events arrive without prior TEXT_MESSAGE_START.
|
|
204
|
+
*
|
|
205
|
+
* On reconnect/resume, a TEXT_MESSAGE_CONTENT may arrive for a message that
|
|
206
|
+
* already exists in this.messages (e.g. from initialMessages or a prior
|
|
207
|
+
* MESSAGES_SNAPSHOT) but whose transient state was cleared. In that case we
|
|
208
|
+
* hydrate state from the existing message rather than creating a duplicate.
|
|
209
|
+
*/
|
|
210
|
+
private ensureAssistantMessage;
|
|
211
|
+
/**
|
|
212
|
+
* Handle TEXT_MESSAGE_START event
|
|
213
|
+
*/
|
|
214
|
+
private handleTextMessageStartEvent;
|
|
215
|
+
/**
|
|
216
|
+
* Handle TEXT_MESSAGE_END event
|
|
217
|
+
*/
|
|
218
|
+
private handleTextMessageEndEvent;
|
|
219
|
+
/**
|
|
220
|
+
* Handle MESSAGES_SNAPSHOT event
|
|
221
|
+
*/
|
|
222
|
+
private handleMessagesSnapshotEvent;
|
|
223
|
+
/**
|
|
224
|
+
* Handle TEXT_MESSAGE_CONTENT event.
|
|
225
|
+
*
|
|
226
|
+
* Accumulates delta into both currentSegmentText (for UI emission) and
|
|
227
|
+
* totalTextContent (for ProcessorResult). Lazily creates the assistant
|
|
228
|
+
* UIMessage on first content. Uses updateTextPart() which replaces the
|
|
229
|
+
* last TextPart or creates a new one depending on part ordering.
|
|
230
|
+
*
|
|
231
|
+
* @see docs/chat-architecture.md#single-shot-text-response — Text accumulation step-by-step
|
|
232
|
+
* @see docs/chat-architecture.md#uimessage-part-ordering-invariants — Replace vs. push logic
|
|
233
|
+
*/
|
|
234
|
+
private handleTextMessageContentEvent;
|
|
235
|
+
/**
|
|
236
|
+
* Handle TOOL_CALL_START event.
|
|
237
|
+
*
|
|
238
|
+
* Creates a new InternalToolCallState entry in the toolCalls Map and appends
|
|
239
|
+
* a ToolCallPart to the UIMessage. Duplicate toolCallId is a no-op.
|
|
240
|
+
*
|
|
241
|
+
* CRITICAL: This MUST be received before any TOOL_CALL_ARGS for the same
|
|
242
|
+
* toolCallId. Args for unknown IDs are silently dropped.
|
|
243
|
+
*
|
|
244
|
+
* @see docs/chat-architecture.md#single-shot-tool-call-response — Tool call state transitions
|
|
245
|
+
* @see docs/chat-architecture.md#parallel-tool-calls-single-shot — Parallel tracking by ID
|
|
246
|
+
* @see docs/chat-architecture.md#adapter-contract — Ordering requirements
|
|
247
|
+
*/
|
|
248
|
+
private handleToolCallStartEvent;
|
|
249
|
+
/**
|
|
250
|
+
* Handle TOOL_CALL_ARGS event.
|
|
251
|
+
*
|
|
252
|
+
* Appends the delta to the tool call's accumulated arguments string.
|
|
253
|
+
* Transitions state from awaiting-input → input-streaming on first non-empty delta.
|
|
254
|
+
* Attempts partial JSON parse on each update for UI preview.
|
|
255
|
+
*
|
|
256
|
+
* If toolCallId is not found in the Map (no preceding TOOL_CALL_START),
|
|
257
|
+
* this event is silently dropped.
|
|
258
|
+
*
|
|
259
|
+
* @see docs/chat-architecture.md#single-shot-tool-call-response — Step-by-step tool call processing
|
|
260
|
+
*/
|
|
261
|
+
private handleToolCallArgsEvent;
|
|
262
|
+
/**
|
|
263
|
+
* Handle TOOL_CALL_END event — authoritative signal that a tool call's input is finalized.
|
|
264
|
+
*
|
|
265
|
+
* This event has a DUAL ROLE:
|
|
266
|
+
* - Without `result`: Signals arguments are done (from adapter). Transitions to input-complete.
|
|
267
|
+
* - With `result`: Signals tool was executed and result is available (from TextEngine).
|
|
268
|
+
* Creates both output on the tool-call part AND a tool-result part.
|
|
269
|
+
*
|
|
270
|
+
* If `input` is provided, it overrides the accumulated string parse as the
|
|
271
|
+
* canonical parsed arguments.
|
|
272
|
+
*
|
|
273
|
+
* @see docs/chat-architecture.md#tool-results-and-the-tool_call_end-dual-role — Full explanation
|
|
274
|
+
* @see docs/chat-architecture.md#single-shot-tool-call-response — End-to-end flow
|
|
275
|
+
*/
|
|
276
|
+
private handleToolCallEndEvent;
|
|
277
|
+
private extractToolResultError;
|
|
278
|
+
/**
|
|
279
|
+
* Handle TOOL_CALL_RESULT event (AG-UI spec).
|
|
280
|
+
*
|
|
281
|
+
* Creates a tool-result part and updates the tool-call output field,
|
|
282
|
+
* mirroring the logic from TOOL_CALL_END when it carries a result.
|
|
283
|
+
* This is the spec-compliant path for delivering tool results to the client.
|
|
284
|
+
*/
|
|
285
|
+
private handleToolCallResultEvent;
|
|
286
|
+
/**
|
|
287
|
+
* Handle RUN_STARTED event.
|
|
288
|
+
*
|
|
289
|
+
* Registers the run so that RUN_FINISHED can determine whether other
|
|
290
|
+
* runs are still active before finalizing.
|
|
291
|
+
*/
|
|
292
|
+
private handleRunStartedEvent;
|
|
293
|
+
/**
|
|
294
|
+
* Handle RUN_FINISHED event.
|
|
295
|
+
*
|
|
296
|
+
* Records the finishReason and removes the run from activeRuns.
|
|
297
|
+
* Only finalizes when no more runs are active, so that concurrent
|
|
298
|
+
* runs don't interfere with each other.
|
|
299
|
+
*
|
|
300
|
+
* @see docs/chat-architecture.md#single-shot-tool-call-response — finishReason semantics
|
|
301
|
+
* @see docs/chat-architecture.md#adapter-contract — Why RUN_FINISHED is mandatory
|
|
302
|
+
*/
|
|
303
|
+
private handleRunFinishedEvent;
|
|
304
|
+
/**
|
|
305
|
+
* Handle RUN_ERROR event
|
|
306
|
+
*/
|
|
307
|
+
private handleRunErrorEvent;
|
|
308
|
+
/**
|
|
309
|
+
* Handle STEP_STARTED event (for thinking/reasoning content).
|
|
310
|
+
*
|
|
311
|
+
* Records the stepId so that subsequent STEP_FINISHED deltas accumulate
|
|
312
|
+
* into their own ThinkingPart. Does not create a message — the message
|
|
313
|
+
* is lazily created when the first STEP_FINISHED content arrives.
|
|
314
|
+
*/
|
|
315
|
+
private handleStepStartedEvent;
|
|
316
|
+
/**
|
|
317
|
+
* Handle STEP_FINISHED event (for thinking/reasoning content).
|
|
318
|
+
*
|
|
319
|
+
* Accumulates delta into the current thinking step's content and updates
|
|
320
|
+
* the corresponding ThinkingPart in the UIMessage.
|
|
321
|
+
*
|
|
322
|
+
* @see docs/chat-architecture.md#thinkingreasoning-content — Thinking flow
|
|
323
|
+
*/
|
|
324
|
+
private handleStepFinishedEvent;
|
|
325
|
+
/**
|
|
326
|
+
* Handle REASONING_MESSAGE_CONTENT event (AG-UI reasoning protocol).
|
|
327
|
+
*
|
|
328
|
+
* Accumulates reasoning delta into thinking content and updates the
|
|
329
|
+
* corresponding ThinkingPart in the UIMessage.
|
|
330
|
+
*/
|
|
331
|
+
private handleReasoningMessageContentEvent;
|
|
332
|
+
/**
|
|
333
|
+
* Handle CUSTOM event.
|
|
334
|
+
*
|
|
335
|
+
* Handles special custom events emitted by the TextEngine (not adapters):
|
|
336
|
+
* - 'tool-input-available': Client tool needs execution. Fires onToolCall.
|
|
337
|
+
* - 'approval-requested': Tool needs user approval. Updates tool-call part
|
|
338
|
+
* state and fires onApprovalRequest.
|
|
339
|
+
*
|
|
340
|
+
* @see docs/chat-architecture.md#client-tools-and-approval-flows — Full flow details
|
|
341
|
+
*/
|
|
342
|
+
private handleCustomEvent;
|
|
343
|
+
/**
|
|
344
|
+
* Detect if an incoming content chunk represents a NEW text segment
|
|
345
|
+
*/
|
|
346
|
+
private isNewTextSegment;
|
|
347
|
+
/**
|
|
348
|
+
* Complete all tool calls across all active messages — safety net for stream termination.
|
|
349
|
+
*
|
|
350
|
+
* Called by RUN_FINISHED and finalizeStream(). Force-transitions any tool call
|
|
351
|
+
* not yet in input-complete state. Handles cases where TOOL_CALL_END was
|
|
352
|
+
* missed (adapter bug, network error, aborted stream).
|
|
353
|
+
*
|
|
354
|
+
* @see docs/chat-architecture.md#single-shot-tool-call-response — Safety net behavior
|
|
355
|
+
*/
|
|
356
|
+
private completeAllToolCalls;
|
|
357
|
+
/**
|
|
358
|
+
* Complete all tool calls for a specific message
|
|
359
|
+
*/
|
|
360
|
+
private completeAllToolCallsForMessage;
|
|
361
|
+
/**
|
|
362
|
+
* Mark a tool call as complete and emit event
|
|
363
|
+
*/
|
|
364
|
+
private completeToolCall;
|
|
365
|
+
/**
|
|
366
|
+
* Whether the rendered tool-call part for the given id has reached the
|
|
367
|
+
* terminal 'error' state. Used to prevent the completion safety net from
|
|
368
|
+
* downgrading a failed call back to 'input-complete'.
|
|
369
|
+
*/
|
|
370
|
+
private isToolCallPartErrored;
|
|
371
|
+
/**
|
|
372
|
+
* Emit pending text update for a specific message.
|
|
373
|
+
*
|
|
374
|
+
* Calls updateTextPart() which has critical append-vs-replace logic:
|
|
375
|
+
* - If last UIMessage part is TextPart → replaces its content (same segment).
|
|
376
|
+
* - If last part is anything else → pushes new TextPart (new segment after tools).
|
|
377
|
+
*
|
|
378
|
+
* @see docs/chat-architecture.md#uimessage-part-ordering-invariants — Replace vs. push logic
|
|
379
|
+
*/
|
|
380
|
+
private emitTextUpdateForMessage;
|
|
381
|
+
private queueStructuredOutputUpdate;
|
|
382
|
+
private flushStructuredOutputUpdate;
|
|
383
|
+
private emitStructuredOutputChange;
|
|
384
|
+
/**
|
|
385
|
+
* Emit messages change event
|
|
386
|
+
*/
|
|
387
|
+
private emitMessagesChange;
|
|
388
|
+
/**
|
|
389
|
+
* Finalize the stream — complete all pending operations.
|
|
390
|
+
*
|
|
391
|
+
* Called when the async iterable ends (stream closed). Acts as the final
|
|
392
|
+
* safety net: completes any remaining tool calls, flushes un-emitted text,
|
|
393
|
+
* and fires onStreamEnd.
|
|
394
|
+
*
|
|
395
|
+
* @see docs/chat-architecture.md#single-shot-text-response — Finalization step
|
|
396
|
+
*/
|
|
397
|
+
finalizeStream(): void;
|
|
398
|
+
/**
|
|
399
|
+
* Get completed tool calls in API format (aggregated across all messages)
|
|
400
|
+
*/
|
|
401
|
+
private getCompletedToolCalls;
|
|
402
|
+
/**
|
|
403
|
+
* Get current result (aggregated across all messages)
|
|
404
|
+
*/
|
|
405
|
+
private getResult;
|
|
406
|
+
/**
|
|
407
|
+
* Get current processor state (aggregated across all messages)
|
|
408
|
+
*/
|
|
409
|
+
getState(): ProcessorState;
|
|
410
|
+
/**
|
|
411
|
+
* Start recording chunks
|
|
412
|
+
*/
|
|
413
|
+
startRecording(): void;
|
|
414
|
+
/**
|
|
415
|
+
* Get the current recording
|
|
416
|
+
*/
|
|
417
|
+
getRecording(): ChunkRecording | null;
|
|
418
|
+
/**
|
|
419
|
+
* Reset stream state (but keep messages)
|
|
420
|
+
*/
|
|
421
|
+
private resetStreamState;
|
|
422
|
+
/**
|
|
423
|
+
* Full reset (including messages)
|
|
424
|
+
*/
|
|
425
|
+
reset(): void;
|
|
426
|
+
/**
|
|
427
|
+
* Check if a message contains only whitespace text and no other meaningful parts
|
|
428
|
+
* (no tool calls, tool results, thinking, etc.)
|
|
429
|
+
*/
|
|
430
|
+
private isWhitespaceOnlyMessage;
|
|
431
|
+
/**
|
|
432
|
+
* Replay a recording through the processor
|
|
433
|
+
*/
|
|
434
|
+
static replay(recording: ChunkRecording, options?: StreamProcessorOptions): Promise<ProcessorResult>;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Create an async iterable from a recording
|
|
438
|
+
*/
|
|
439
|
+
export declare function createReplayStream(recording: ChunkRecording): AsyncIterable<StreamChunk>;
|