Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

index.ts

224 lines5.7 KB

1/**
2 * Sample trace files for testing the stream processor.
3 *
4 * Each trace is a list of AG-UI events, as the server streams them.
5 */
6​
7import type { ChunkRecording } from '@tanstack/ai'
8​
9// Helper to create a base recording
10function createRecording(chunks: Array<any>): ChunkRecording {
11 return {
12 version: '1.0',
13 timestamp: Date.now(),
14 chunks: chunks.map((chunk, index) => ({
15 chunk,
16 timestamp: Date.now() + index * 10,
17 index,
18 })),
19 }
21​
22const event = (type: string, fields: Record<string, unknown> = {}) => ({
23 type,
24 timestamp: Date.now(),
25 ...fields,
26})
27​
28const runStarted = () =>
29 event('RUN_STARTED', { runId: 'run-1', threadId: 'thread-1' })
30​
31const runFinished = (
32 finishReason: 'stop' | 'tool_calls',
33 outcome?: Record<string, unknown>,
34) =>
35 event('RUN_FINISHED', {
36 runId: 'run-1',
37 threadId: 'thread-1',
38 ...(outcome ? { outcome } : {}),
39 metadata: { tanstack: { finishReason } },
40 })
41​
42/** A text message, streamed as one content event per delta. */
43const text = (deltas: Array<string>, messageId = 'msg-1') => [
44 event('TEXT_MESSAGE_START', { messageId, role: 'assistant' }),
45 ...deltas.map((delta) => event('TEXT_MESSAGE_CONTENT', { messageId, delta })),
46 event('TEXT_MESSAGE_END', { messageId }),
48​
49const toolStart = (toolCallId: string, toolCallName: string) =>
50 event('TOOL_CALL_START', { toolCallId, toolCallName })
51​
52const toolArgs = (toolCallId: string, delta: string) =>
53 event('TOOL_CALL_ARGS', { toolCallId, delta })
54​
55const toolEnd = (toolCallId: string) => event('TOOL_CALL_END', { toolCallId })
56​
57/** A tool call, with its arguments streamed as one args event per delta. */
58const toolCall = (
59 toolCallId: string,
60 toolCallName: string,
61 argDeltas: Array<string>,
62) => [
63 toolStart(toolCallId, toolCallName),
64 ...argDeltas.map((delta) => toolArgs(toolCallId, delta)),
65 toolEnd(toolCallId),
67​
68/**
69 * Simple text streaming - "Hello world!" character by character
70 */
71export const textSimple = createRecording([
72 runStarted(),
73 ...text('Hello world!'.split('')),
74 runFinished('stop'),
75])
76​
77/**
78 * Text with punctuation - multiple sentences
79 */
80export const textPunctuation = createRecording([
81 runStarted(),
82 ...text(['Hello', ' world', '!', ' How', ' are', ' you?']),
83 runFinished('stop'),
84])
85​
86/**
87 * Single tool call with streaming arguments
88 */
89export const toolCallSingle = createRecording([
90 runStarted(),
91 ...toolCall('call_1', 'getWeather', ['{"lo', 'cation":', ' "Paris"}']),
92 runFinished('tool_calls'),
93])
94​
95/**
96 * Multiple parallel tool calls
97 */
98export const toolCallParallel = createRecording([
99 runStarted(),
100 toolStart('call_1', 'getWeather'),
101 toolArgs('call_1', '{"lo'),
102 toolStart('call_2', 'getTime'),
103 toolArgs('call_2', '{"ci'),
104 toolArgs('call_1', 'cation":"Paris"}'),
105 toolArgs('call_2', 'ty":"Tokyo"}'),
106 toolEnd('call_1'),
107 toolEnd('call_2'),
108 runFinished('tool_calls'),
109])
110​
111/**
112 * Tool call followed by text (tool result response)
113 */
114export const toolCallWithText = createRecording([
115 runStarted(),
116 ...toolCall('call_1', 'getWeather', ['{"location":"Paris"}']),
117 ...text(['The weather in Paris is', ' sunny', ' and warm.']),
118 runFinished('stop'),
119])
120​
121/**
122 * Tool result event
123 */
124export const toolResult = createRecording([
125 runStarted(),
126 ...toolCall('call_1', 'calculate', ['{"expression":"2+2"}']),
127 event('TOOL_CALL_RESULT', {
128 messageId: 'tool-result-1',
129 toolCallId: 'call_1',
130 content: '4',
131 role: 'tool',
132 }),
133 ...text(['The result is 4.'], 'msg-2'),
134 runFinished('stop'),
135])
136​
137/**
138 * Thinking/reasoning events
139 */
140export const thinking = createRecording([
141 runStarted(),
142 event('REASONING_START', { messageId: 'reasoning-1' }),
143 event('REASONING_MESSAGE_START', {
144 messageId: 'reasoning-1',
145 role: 'reasoning',
146 }),
147 event('REASONING_MESSAGE_CONTENT', {
148 messageId: 'reasoning-1',
149 delta: 'Let me think about this...',
150 }),
151 event('REASONING_MESSAGE_CONTENT', {
152 messageId: 'reasoning-1',
153 delta: ' I need to consider the weather patterns.',
154 }),
155 event('REASONING_MESSAGE_END', { messageId: 'reasoning-1' }),
156 event('REASONING_END', { messageId: 'reasoning-1' }),
157 ...text(['Based on my analysis, ', 'it looks like rain.']),
158 runFinished('stop'),
159])
160​
161/**
162 * Approval requested flow: the run stops with an approval interrupt
163 */
164export const approvalRequested = createRecording([
165 runStarted(),
166 ...toolCall('call_1', 'deleteFile', ['{"path":"/tmp/test.txt"}']),
167 runFinished('tool_calls', {
168 type: 'interrupt',
169 interrupts: [
170 {
171 id: 'approval_call_1',
172 reason: 'tool_call',
173 toolCallId: 'call_1',
174 metadata: {
175 kind: 'approval',
176 toolName: 'deleteFile',
177 input: { path: '/tmp/test.txt' },
178 },
179 },
180 ],
181 }),
182])
183​
184/**
185 * Client tool flow: the run stops with a client tool interrupt
186 */
187export const toolInputAvailable = createRecording([
188 runStarted(),
189 ...toolCall('call_1', 'getUserLocation', ['{}']),
190 runFinished('tool_calls', {
191 type: 'interrupt',
192 interrupts: [
193 {
194 id: 'client_tool_call_1',
195 reason: 'tanstack:client_tool_execution',
196 toolCallId: 'call_1',
197 metadata: {
198 kind: 'client_tool',
199 toolName: 'getUserLocation',
200 input: {},
201 },
202 },
203 ],
204 }),
205])
206​
207/**
208 * Error event
209 */
210export const error = createRecording([
211 runStarted(),
212 event('TEXT_MESSAGE_START', { messageId: 'msg-1', role: 'assistant' }),
213 event('TEXT_MESSAGE_CONTENT', { messageId: 'msg-1', delta: 'Starting...' }),
214 event('RUN_ERROR', { message: 'Rate limit exceeded', code: 'rate_limit' }),
215])
216​
217/**
218 * Many small content deltas
219 */
220export const deltaOnly = createRecording([
221 runStarted(),
222 ...text(['Hello', ' ', 'world', '!']),
223 runFinished('stop'),
224])