Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import {
3 EventType,
4 chat,
5 chatParamsFromRequestBody,
6 maxIterations,
7 toServerSentEventsResponse,
8} from '@tanstack/ai'
9import type { AnyTextAdapter, StreamChunk } from '@tanstack/ai'
10import type { TestRuntimeContext } from '@/lib/tools-test-tools'
11import { createTextAdapter } from '@/lib/providers'
12import { getToolsForScenario } from '@/lib/tools-test-tools'
13
14const runtimeContextScenarios = new Set([
15 'server-context',
16 'client-context',
17 'client-server-context',
18])
19
20function createRuntimeContextAdapter(scenario: string): AnyTextAdapter {
21 return {
22 kind: 'text',
23 name: 'runtime-context-test',
24 model: 'runtime-context-test',
25 '~types': {
26 providerOptions: {},
27 inputModalities: ['text'],
28 messageMetadataByModality: {},
29 toolCapabilities: [],
30 toolCallMetadata: undefined,
31 systemPromptMetadata: undefined,
32 },
33 async *chatStream(options): AsyncGenerator<StreamChunk> {
34 const model = 'runtime-context-test'
35 const runId = options.runId ?? 'runtime-context-run'
36 const threadId = options.threadId ?? 'runtime-context-thread'
37 const messageId = `${runId}-message`
38 const hasToolResult = options.messages.some(
39 (message) => message.role === 'tool',
40 )
41
42 yield {
43 type: EventType.RUN_STARTED,
44 runId,
45 threadId,
46 model,
47 timestamp: Date.now(),
48 }
49
50 if (!hasToolResult) {
51 const toolName =
52 scenario === 'client-context'
53 ? 'read_client_context'
54 : 'read_server_context'
55 const toolCallId = `${scenario}-tool-call`
56
57 yield {
58 type: EventType.TEXT_MESSAGE_START,
59 messageId,
60 role: 'assistant',
61 model,
62 timestamp: Date.now(),
63 }
64 yield {
65 type: EventType.TEXT_MESSAGE_CONTENT,
66 messageId,
67 delta: 'Reading runtime context.',
68 model,
69 timestamp: Date.now(),
70 }
71 yield {
72 type: EventType.TEXT_MESSAGE_END,
73 messageId,
74 model,
75 timestamp: Date.now(),
76 }
77 yield {
78 type: EventType.TOOL_CALL_START,
79 toolCallId,
80 toolCallName: toolName,
81 toolName,
82 model,
83 timestamp: Date.now(),
84 }
85 yield {
86 type: EventType.TOOL_CALL_ARGS,
87 toolCallId,
88 delta: '{}',
89 model,
90 timestamp: Date.now(),
91 }
92 yield {
93 type: EventType.TOOL_CALL_END,
94 toolCallId,
95 toolCallName: toolName,
96 toolName,
97 input: {},
98 model,
99 timestamp: Date.now(),
100 }
101 yield {
102 type: EventType.RUN_FINISHED,
103 runId,
104 threadId,
105 model,
106 finishReason: 'tool_calls',
107 timestamp: Date.now(),
108 }
109 return
110 }
111
112 yield {
113 type: EventType.TEXT_MESSAGE_START,
114 messageId,
115 role: 'assistant',
116 model,
117 timestamp: Date.now(),
118 }
119 yield {
120 type: EventType.TEXT_MESSAGE_CONTENT,
121 messageId,
122 delta: 'Runtime context was read.',
123 model,
124 timestamp: Date.now(),
125 }
126 yield {
127 type: EventType.TEXT_MESSAGE_END,
128 messageId,
129 model,
130 timestamp: Date.now(),
131 }
132 yield {
133 type: EventType.RUN_FINISHED,
134 runId,
135 threadId,
136 model,
137 finishReason: 'stop',
138 timestamp: Date.now(),
139 }
140 },
141 structuredOutput: async () => ({ data: {}, rawText: '{}' }),
142 }
143}
144
145export const Route = createFileRoute('/api/tools-test')({
146 server: {
147 handlers: {
148 POST: async ({ request }) => {
149 const requestSignal = request.signal
150
151 if (requestSignal?.aborted) {
152 return new Response(null, { status: 499 })
153 }
154
155 const abortController = new AbortController()
156
157 let params
158 try {
159 params = await chatParamsFromRequestBody(await request.json())
160 } catch (error) {
161 return new Response(
162 error instanceof Error ? error.message : 'Bad request',
163 { status: 400 },
164 )
165 }
166
167 const fp = params.forwardedProps
168 const scenario =
169 typeof fp.scenario === 'string' ? fp.scenario : 'text-only'
170 const testId: string | undefined =
171 typeof fp.testId === 'string' ? fp.testId : undefined
172 const aimockPort: number | undefined =
173 fp.aimockPort != null ? Number(fp.aimockPort) : undefined
174
175 try {
176 // Special error scenario: return a stream that immediately errors
177 if (scenario === 'error') {
178 const errorStream =
179 (async function* (): AsyncGenerator<StreamChunk> {
180 yield {
181 type: EventType.RUN_STARTED,
182 runId: 'error-test',
183 threadId: 'error-test',
184 timestamp: Date.now(),
185 }
186 yield {
187 type: EventType.RUN_ERROR,
188 message: 'Test error: Something went wrong during generation',
189 timestamp: Date.now(),
190 code: 'provider_error',
191 // Mirrors a provider's structured error body forwarded by the
192 // adapters as `rawEvent`. Asserts it survives SSE transport,
193 // the strip-to-spec middleware, and reaches the consumer.
194 rawEvent: {
195 provider_name: 'test-provider',
196 raw: { reason: 'upstream overloaded' },
197 },
198 }
199 })()
200 return toServerSentEventsResponse(errorStream, { abortController })
201 }
202
203 const adapterOptions = runtimeContextScenarios.has(scenario)
204 ? { adapter: createRuntimeContextAdapter(scenario) }
205 : createTextAdapter('openai', undefined, aimockPort, testId)
206
207 const tools = getToolsForScenario(scenario)
208 const runtimeContext: TestRuntimeContext =
209 scenario === 'client-server-context' &&
210 typeof fp.runtimeUserId === 'string'
211 ? {
212 userId: fp.runtimeUserId,
213 tenantId: 'server-tenant-context',
214 source: 'forwarded-props',
215 }
216 : {
217 userId: 'server-user-context',
218 tenantId: 'server-tenant-context',
219 source: 'server-route',
220 }
221
222 const stream = chat({
223 ...adapterOptions,
224 messages: params.messages,
225 tools,
226 context: runtimeContext,
227 threadId: params.threadId,
228 runId: params.runId,
229 agentLoopStrategy: maxIterations(20),
230 abortController,
231 })
232
233 return toServerSentEventsResponse(stream, { abortController })
234 } catch (error) {
235 console.error('[Tools Test API] Error:', error)
236 if (
237 (error instanceof Error && error.name === 'AbortError') ||
238 abortController.signal.aborted
239 ) {
240 return new Response(null, { status: 499 })
241 }
242 const message =
243 error instanceof Error ? error.message : 'An error occurred'
244 return new Response(JSON.stringify({ error: message }), {
245 status: 500,
246 headers: { 'Content-Type': 'application/json' },
247 })
248 }
249 },
250 },
251 },
252})