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

api.tools-test.ts

100 lines3.0 KB

1import { createFileRoute } from '@tanstack/react-router'
2import {
3 chat,
4 chatParamsFromRequestBody,
5 maxIterations,
6 toServerSentEventsResponse,
7} from '@tanstack/ai'
8import { createTextAdapter } from '@/lib/providers'
9import { getToolsForScenario } from '@/lib/tools-test-tools'
10​
11export const Route = createFileRoute('/api/tools-test')({
12 server: {
13 handlers: {
14 POST: async ({ request }) => {
15 const requestSignal = request.signal
16​
17 if (requestSignal?.aborted) {
18 return new Response(null, { status: 499 })
19 }
20​
21 const abortController = new AbortController()
22​
23 let params
24 try {
25 params = await chatParamsFromRequestBody(await request.json())
26 } catch (error) {
27 return new Response(
28 error instanceof Error ? error.message : 'Bad request',
29 { status: 400 },
30 )
31 }
32​
33 const fp = params.forwardedProps as Record<string, unknown>
34 const scenario =
35 typeof fp.scenario === 'string' ? fp.scenario : 'text-only'
36 const testId: string | undefined =
37 typeof fp.testId === 'string' ? fp.testId : undefined
38 const aimockPort: number | undefined =
39 fp.aimockPort != null ? Number(fp.aimockPort) : undefined
40​
41 try {
42 // Special error scenario: return a stream that immediately errors
43 if (scenario === 'error') {
44 const errorStream = (async function* () {
45 yield {
46 type: 'RUN_STARTED' as const,
47 runId: 'error-test',
48 timestamp: Date.now(),
49 }
50 yield {
51 type: 'RUN_ERROR' as const,
52 runId: 'error-test',
53 error: {
54 message: 'Test error: Something went wrong during generation',
55 },
56 timestamp: Date.now(),
57 }
58 })()
59 return toServerSentEventsResponse(errorStream, { abortController })
60 }
61​
62 const adapterOptions = createTextAdapter(
63 'openai',
64 undefined,
65 aimockPort,
66 testId,
67 )
68​
69 const tools = getToolsForScenario(scenario)
70​
71 const stream = chat({
72 ...adapterOptions,
73 messages: params.messages,
74 tools,
75 threadId: params.threadId,
76 runId: params.runId,
77 agentLoopStrategy: maxIterations(20),
78 abortController,
79 })
80​
81 return toServerSentEventsResponse(stream, { abortController })
82 } catch (error: any) {
83 console.error('[Tools Test API] Error:', error)
84 if (error.name === 'AbortError' || abortController.signal.aborted) {
85 return new Response(null, { status: 499 })
86 }
87 return new Response(
88 JSON.stringify({
89 error: error.message || 'An error occurred',
90 }),
91 {
92 status: 500,
93 headers: { 'Content-Type': 'application/json' },
94 },
95 )
96 }
97 },
98 },
99 },
100})