@tanstack/ai 0.0.1
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/LICENSE +21 -0
- package/README.md +131 -0
- package/dist/esm/base-adapter.d.ts +35 -0
- package/dist/esm/base-adapter.js +12 -0
- package/dist/esm/base-adapter.js.map +1 -0
- package/dist/esm/core/chat-common-options.d.ts +52 -0
- package/dist/esm/core/chat.d.ts +30 -0
- package/dist/esm/core/chat.js +533 -0
- package/dist/esm/core/chat.js.map +1 -0
- package/dist/esm/core/embedding.d.ts +8 -0
- package/dist/esm/core/embedding.js +33 -0
- package/dist/esm/core/embedding.js.map +1 -0
- package/dist/esm/core/summarize.d.ts +9 -0
- package/dist/esm/core/summarize.js +36 -0
- package/dist/esm/core/summarize.js.map +1 -0
- package/dist/esm/event-client.d.ts +311 -0
- package/dist/esm/event-client.js +62 -0
- package/dist/esm/event-client.js.map +1 -0
- package/dist/esm/index.d.ts +16 -0
- package/dist/esm/index.js +50 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/message-converters.d.ts +52 -0
- package/dist/esm/message-converters.js +162 -0
- package/dist/esm/message-converters.js.map +1 -0
- package/dist/esm/stream/index.d.ts +11 -0
- package/dist/esm/stream/json-parser.d.ts +38 -0
- package/dist/esm/stream/json-parser.js +28 -0
- package/dist/esm/stream/json-parser.js.map +1 -0
- package/dist/esm/stream/message-updaters.d.ts +44 -0
- package/dist/esm/stream/message-updaters.js +141 -0
- package/dist/esm/stream/message-updaters.js.map +1 -0
- package/dist/esm/stream/processor.d.ts +242 -0
- package/dist/esm/stream/processor.js +693 -0
- package/dist/esm/stream/processor.js.map +1 -0
- package/dist/esm/stream/strategies.d.ts +43 -0
- package/dist/esm/stream/strategies.js +54 -0
- package/dist/esm/stream/strategies.js.map +1 -0
- package/dist/esm/stream/types.d.ts +71 -0
- package/dist/esm/tools/tool-calls.d.ts +112 -0
- package/dist/esm/tools/tool-calls.js +302 -0
- package/dist/esm/tools/tool-calls.js.map +1 -0
- package/dist/esm/tools/tool-definition.d.ts +125 -0
- package/dist/esm/tools/tool-definition.js +25 -0
- package/dist/esm/tools/tool-definition.js.map +1 -0
- package/dist/esm/tools/zod-converter.d.ts +30 -0
- package/dist/esm/tools/zod-converter.js +36 -0
- package/dist/esm/tools/zod-converter.js.map +1 -0
- package/dist/esm/types.d.ts +619 -0
- package/dist/esm/utilities/agent-loop-strategies.d.ts +59 -0
- package/dist/esm/utilities/agent-loop-strategies.js +23 -0
- package/dist/esm/utilities/agent-loop-strategies.js.map +1 -0
- package/dist/esm/utilities/chat-options.d.ts +6 -0
- package/dist/esm/utilities/chat-options.js +7 -0
- package/dist/esm/utilities/chat-options.js.map +1 -0
- package/dist/esm/utilities/messages.d.ts +30 -0
- package/dist/esm/utilities/messages.js +7 -0
- package/dist/esm/utilities/messages.js.map +1 -0
- package/dist/esm/utilities/stream-to-response.d.ts +48 -0
- package/dist/esm/utilities/stream-to-response.js +62 -0
- package/dist/esm/utilities/stream-to-response.js.map +1 -0
- package/package.json +65 -0
- package/src/base-adapter.ts +85 -0
- package/src/core/chat-common-options.ts +55 -0
- package/src/core/chat.ts +771 -0
- package/src/core/embedding.ts +54 -0
- package/src/core/summarize.ts +56 -0
- package/src/event-client.ts +389 -0
- package/src/index.ts +68 -0
- package/src/message-converters.ts +285 -0
- package/src/stream/index.ts +41 -0
- package/src/stream/json-parser.ts +58 -0
- package/src/stream/message-updaters.ts +275 -0
- package/src/stream/processor.ts +1092 -0
- package/src/stream/strategies.ts +78 -0
- package/src/stream/types.ts +94 -0
- package/src/tools/tool-calls.ts +471 -0
- package/src/tools/tool-definition.ts +206 -0
- package/src/tools/zod-converter.ts +85 -0
- package/src/types.ts +872 -0
- package/src/utilities/agent-loop-strategies.ts +85 -0
- package/src/utilities/chat-options.ts +35 -0
- package/src/utilities/messages.ts +63 -0
- package/src/utilities/stream-to-response.ts +116 -0
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { ChunkRecording, ChunkStrategy, ProcessorResult, ProcessorState, ToolCallState, ToolResultState } from './types.js';
|
|
2
|
+
import { ModelMessage, StreamChunk, ToolCall, 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
|
+
onTextUpdate?: (messageId: string, content: string) => void;
|
|
23
|
+
onToolCallStateChange?: (messageId: string, toolCallId: string, state: ToolCallState, args: string) => void;
|
|
24
|
+
onThinkingUpdate?: (messageId: string, content: string) => void;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Legacy handlers for backward compatibility
|
|
28
|
+
* These are the old callback-style handlers
|
|
29
|
+
*/
|
|
30
|
+
export interface StreamProcessorHandlers {
|
|
31
|
+
onTextUpdate?: (content: string) => void;
|
|
32
|
+
onThinkingUpdate?: (content: string) => void;
|
|
33
|
+
onToolCallStart?: (index: number, id: string, name: string) => void;
|
|
34
|
+
onToolCallDelta?: (index: number, args: string) => void;
|
|
35
|
+
onToolCallComplete?: (index: number, id: string, name: string, args: string) => void;
|
|
36
|
+
onToolCallStateChange?: (index: number, id: string, name: string, state: ToolCallState, args: string, parsedArgs?: any) => void;
|
|
37
|
+
onToolResultStateChange?: (toolCallId: string, content: string, state: ToolResultState, error?: string) => void;
|
|
38
|
+
onApprovalRequested?: (toolCallId: string, toolName: string, input: any, approvalId: string) => void;
|
|
39
|
+
onToolInputAvailable?: (toolCallId: string, toolName: string, input: any) => void;
|
|
40
|
+
onStreamEnd?: (content: string, toolCalls?: Array<ToolCall>) => void;
|
|
41
|
+
onError?: (error: {
|
|
42
|
+
message: string;
|
|
43
|
+
code?: string;
|
|
44
|
+
}) => void;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Options for StreamProcessor
|
|
48
|
+
*/
|
|
49
|
+
export interface StreamProcessorOptions {
|
|
50
|
+
chunkStrategy?: ChunkStrategy;
|
|
51
|
+
/** New event-driven handlers */
|
|
52
|
+
events?: StreamProcessorEvents;
|
|
53
|
+
/** Legacy callback handlers (for backward compatibility) */
|
|
54
|
+
handlers?: StreamProcessorHandlers;
|
|
55
|
+
jsonParser?: {
|
|
56
|
+
parse: (jsonString: string) => any;
|
|
57
|
+
};
|
|
58
|
+
/** Enable recording for replay testing */
|
|
59
|
+
recording?: boolean;
|
|
60
|
+
/** Initial messages to populate the processor */
|
|
61
|
+
initialMessages?: Array<UIMessage>;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* StreamProcessor - State machine for processing AI response streams
|
|
65
|
+
*
|
|
66
|
+
* Manages the full UIMessage[] conversation and emits events on changes.
|
|
67
|
+
*
|
|
68
|
+
* State tracking:
|
|
69
|
+
* - Full message array
|
|
70
|
+
* - Current assistant message being streamed
|
|
71
|
+
* - Text content accumulation
|
|
72
|
+
* - Multiple parallel tool calls
|
|
73
|
+
* - Tool call completion detection
|
|
74
|
+
*
|
|
75
|
+
* Tool call completion is detected when:
|
|
76
|
+
* 1. A new tool call starts at a different index
|
|
77
|
+
* 2. Text content arrives
|
|
78
|
+
* 3. Stream ends
|
|
79
|
+
*/
|
|
80
|
+
export declare class StreamProcessor {
|
|
81
|
+
private chunkStrategy;
|
|
82
|
+
private events;
|
|
83
|
+
private handlers;
|
|
84
|
+
private jsonParser;
|
|
85
|
+
private recordingEnabled;
|
|
86
|
+
private messages;
|
|
87
|
+
private currentAssistantMessageId;
|
|
88
|
+
private totalTextContent;
|
|
89
|
+
private currentSegmentText;
|
|
90
|
+
private lastEmittedText;
|
|
91
|
+
private thinkingContent;
|
|
92
|
+
private toolCalls;
|
|
93
|
+
private toolCallOrder;
|
|
94
|
+
private finishReason;
|
|
95
|
+
private isDone;
|
|
96
|
+
private hasToolCallsSinceTextStart;
|
|
97
|
+
private recording;
|
|
98
|
+
private recordingStartTime;
|
|
99
|
+
constructor(options?: StreamProcessorOptions);
|
|
100
|
+
/**
|
|
101
|
+
* Set the messages array (e.g., from persisted state)
|
|
102
|
+
*/
|
|
103
|
+
setMessages(messages: Array<UIMessage>): void;
|
|
104
|
+
/**
|
|
105
|
+
* Add a user message to the conversation
|
|
106
|
+
*/
|
|
107
|
+
addUserMessage(content: string): UIMessage;
|
|
108
|
+
/**
|
|
109
|
+
* Start streaming a new assistant message
|
|
110
|
+
* Returns the message ID
|
|
111
|
+
*/
|
|
112
|
+
startAssistantMessage(): string;
|
|
113
|
+
/**
|
|
114
|
+
* Add a tool result (called by client after handling onToolCall)
|
|
115
|
+
*/
|
|
116
|
+
addToolResult(toolCallId: string, output: any, error?: string): void;
|
|
117
|
+
/**
|
|
118
|
+
* Add an approval response (called by client after handling onApprovalRequest)
|
|
119
|
+
*/
|
|
120
|
+
addToolApprovalResponse(approvalId: string, approved: boolean): void;
|
|
121
|
+
/**
|
|
122
|
+
* Get the conversation as ModelMessages (for sending to LLM)
|
|
123
|
+
*/
|
|
124
|
+
toModelMessages(): Array<ModelMessage>;
|
|
125
|
+
/**
|
|
126
|
+
* Get current messages
|
|
127
|
+
*/
|
|
128
|
+
getMessages(): Array<UIMessage>;
|
|
129
|
+
/**
|
|
130
|
+
* Check if all tool calls in the last assistant message are complete
|
|
131
|
+
* Useful for auto-continue logic
|
|
132
|
+
*/
|
|
133
|
+
areAllToolsComplete(): boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Remove messages after a certain index (for reload/retry)
|
|
136
|
+
*/
|
|
137
|
+
removeMessagesAfter(index: number): void;
|
|
138
|
+
/**
|
|
139
|
+
* Clear all messages
|
|
140
|
+
*/
|
|
141
|
+
clearMessages(): void;
|
|
142
|
+
/**
|
|
143
|
+
* Process a stream and emit events through handlers
|
|
144
|
+
*/
|
|
145
|
+
process(stream: AsyncIterable<any>): Promise<ProcessorResult>;
|
|
146
|
+
/**
|
|
147
|
+
* Process a single chunk from the stream
|
|
148
|
+
*/
|
|
149
|
+
processChunk(chunk: StreamChunk): void;
|
|
150
|
+
/**
|
|
151
|
+
* Handle a content chunk
|
|
152
|
+
*/
|
|
153
|
+
private handleContentChunk;
|
|
154
|
+
/**
|
|
155
|
+
* Handle a tool call chunk
|
|
156
|
+
*/
|
|
157
|
+
private handleToolCallChunk;
|
|
158
|
+
/**
|
|
159
|
+
* Handle a tool result chunk
|
|
160
|
+
*/
|
|
161
|
+
private handleToolResultChunk;
|
|
162
|
+
/**
|
|
163
|
+
* Handle a done chunk
|
|
164
|
+
*/
|
|
165
|
+
private handleDoneChunk;
|
|
166
|
+
/**
|
|
167
|
+
* Handle an error chunk
|
|
168
|
+
*/
|
|
169
|
+
private handleErrorChunk;
|
|
170
|
+
/**
|
|
171
|
+
* Handle a thinking chunk
|
|
172
|
+
*/
|
|
173
|
+
private handleThinkingChunk;
|
|
174
|
+
/**
|
|
175
|
+
* Handle an approval-requested chunk
|
|
176
|
+
*/
|
|
177
|
+
private handleApprovalRequestedChunk;
|
|
178
|
+
/**
|
|
179
|
+
* Handle a tool-input-available chunk
|
|
180
|
+
*/
|
|
181
|
+
private handleToolInputAvailableChunk;
|
|
182
|
+
/**
|
|
183
|
+
* Detect if an incoming content chunk represents a NEW text segment
|
|
184
|
+
*/
|
|
185
|
+
private isNewTextSegment;
|
|
186
|
+
/**
|
|
187
|
+
* Complete all tool calls
|
|
188
|
+
*/
|
|
189
|
+
private completeAllToolCalls;
|
|
190
|
+
/**
|
|
191
|
+
* Mark a tool call as complete and emit event
|
|
192
|
+
*/
|
|
193
|
+
private completeToolCall;
|
|
194
|
+
/**
|
|
195
|
+
* Emit pending text update
|
|
196
|
+
*/
|
|
197
|
+
private emitTextUpdate;
|
|
198
|
+
/**
|
|
199
|
+
* Emit messages change event
|
|
200
|
+
*/
|
|
201
|
+
private emitMessagesChange;
|
|
202
|
+
/**
|
|
203
|
+
* Finalize the stream - complete all pending operations
|
|
204
|
+
*/
|
|
205
|
+
finalizeStream(): void;
|
|
206
|
+
/**
|
|
207
|
+
* Get completed tool calls in API format
|
|
208
|
+
*/
|
|
209
|
+
private getCompletedToolCalls;
|
|
210
|
+
/**
|
|
211
|
+
* Get current result
|
|
212
|
+
*/
|
|
213
|
+
private getResult;
|
|
214
|
+
/**
|
|
215
|
+
* Get current processor state (legacy)
|
|
216
|
+
*/
|
|
217
|
+
getState(): ProcessorState;
|
|
218
|
+
/**
|
|
219
|
+
* Start recording chunks
|
|
220
|
+
*/
|
|
221
|
+
startRecording(): void;
|
|
222
|
+
/**
|
|
223
|
+
* Get the current recording
|
|
224
|
+
*/
|
|
225
|
+
getRecording(): ChunkRecording | null;
|
|
226
|
+
/**
|
|
227
|
+
* Reset stream state (but keep messages)
|
|
228
|
+
*/
|
|
229
|
+
private resetStreamState;
|
|
230
|
+
/**
|
|
231
|
+
* Full reset (including messages)
|
|
232
|
+
*/
|
|
233
|
+
reset(): void;
|
|
234
|
+
/**
|
|
235
|
+
* Replay a recording through the processor
|
|
236
|
+
*/
|
|
237
|
+
static replay(recording: ChunkRecording, options?: StreamProcessorOptions): Promise<ProcessorResult>;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Create an async iterable from a recording
|
|
241
|
+
*/
|
|
242
|
+
export declare function createReplayStream(recording: ChunkRecording): AsyncIterable<StreamChunk>;
|