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

api.chat.ts

211 lines8.3 KB

1import { createFileRoute } from '@tanstack/react-router'
2import {
3 chat,
4 chatParamsFromRequestBody,
5 maxIterations,
6 toServerSentEventsResponse,
7} from '@tanstack/ai'
8import type { StreamChunk } from '@tanstack/ai'
9import type { Feature, Provider } from '@/lib/types'
10import { createTextAdapter } from '@/lib/providers'
11import { featureConfigs } from '@/lib/features'
12import { guitarRecommendationSchema, recipeSchema } from '@/lib/schemas'
13​
14const DEFAULT_SYSTEM_PROMPT = 'You are a helpful assistant for a guitar store.'
15​
16export const Route = createFileRoute('/api/chat')({
17 server: {
18 handlers: {
19 POST: async ({ request }) => {
20 await import('@/lib/llmock-server').then((m) => m.ensureLLMock())
21 if (request.signal.aborted) {
22 return new Response(null, { status: 499 })
23 }
24​
25 const abortController = new AbortController()
26​
27 let params
28 try {
29 params = await chatParamsFromRequestBody(await request.json())
30 } catch (error) {
31 return new Response(
32 error instanceof Error ? error.message : 'Bad request',
33 { status: 400 },
34 )
35 }
36​
37 const fp = params.forwardedProps as Record<string, unknown>
38 const provider: Provider = (
39 typeof fp.provider === 'string' ? fp.provider : 'openai'
40 ) as Provider
41 const feature: Feature = (
42 typeof fp.feature === 'string' ? fp.feature : 'chat'
43 ) as Feature
44 const testId = typeof fp.testId === 'string' ? fp.testId : undefined
45 const aimockPort =
46 fp.aimockPort != null ? Number(fp.aimockPort) : undefined
47 const previousInteractionId: string | undefined =
48 typeof fp.previousInteractionId === 'string'
49 ? fp.previousInteractionId
50 : undefined
51​
52 const config = featureConfigs[feature]
53 const modelOverride = config.modelOverrides?.[provider]
54 const adapterOptions = createTextAdapter(
55 provider,
56 modelOverride,
57 aimockPort,
58 testId,
59 feature,
60 )
61​
62 const modelOptions = previousInteractionId
63 ? {
64 ...config.modelOptions,
65 previous_interaction_id: previousInteractionId,
66 }
67 : config.modelOptions
68​
69 try {
70 const systemPrompt = config.systemPrompt ?? DEFAULT_SYSTEM_PROMPT
71​
72 // Test-only flag — when truthy, the route promotes the system
73 // prompt to object-form and attaches Anthropic `cache_control`
74 // metadata. Enables system-prompt-metadata.spec.ts to verify
75 // cache_control reaches the wire via the aimock journal.
76 const systemPromptCacheControl =
77 fp.systemPromptCacheControl === true
78 ? ({ type: 'ephemeral' as const } as const)
79 : undefined
80 const systemPrompts = systemPromptCacheControl
81 ? [
82 {
83 content: systemPrompt,
84 metadata: { cache_control: systemPromptCacheControl },
85 // The route is provider-generic; the metadata type is
86 // adapter-narrowed and only meaningful for Anthropic, so
87 // a single bridge cast lives here at the test entry.
88 // eslint-disable-next-line @typescript-eslint/no-explicit-any
89 } as any,
90 ]
91 : [systemPrompt]
92​
93 // Test-only flag — when set to `true`, the route passes
94 // structured root-level observability `metadata` (objects,
95 // arrays) to `chat()`. Enables root-metadata-wire.spec.ts to
96 // verify the call still completes: the adapter keeps root
97 // metadata off the provider request, so the OpenRouter SDK's
98 // Record<string, string> outbound validation never sees it
99 // (regression coverage for #735). Wire-absence itself is
100 // asserted by the adapter unit tests.
101 const rootObservabilityMetadata =
102 fp.structuredRootMetadata === true
103 ? {
104 observationName: 'e2e-root-metadata',
105 tags: ['a', 'b'],
106 prompt: { name: 'p', version: 1 },
107 }
108 : undefined
109​
110 // Two structured-output-streaming features differ only in which
111 // schema they bind to. Branched per-feature so TS can pick the
112 // right `chat<TSchema>()` overload without a `never` cast.
113 const stream =
114 feature === 'structured-output-stream'
115 ? chat({
116 ...adapterOptions,
117 modelOptions,
118 systemPrompts,
119 messages: params.messages,
120 threadId: params.threadId,
121 runId: params.runId,
122 ...(params.parentRunId && {
123 parentRunId: params.parentRunId,
124 }),
125 ...(params.resume && { resume: params.resume }),
126 outputSchema: guitarRecommendationSchema,
127 stream: true,
128 abortController,
129 })
130 : feature === 'multi-turn-structured'
131 ? chat({
132 ...adapterOptions,
133 modelOptions,
134 systemPrompts,
135 messages: params.messages,
136 threadId: params.threadId,
137 runId: params.runId,
138 ...(params.parentRunId && {
139 parentRunId: params.parentRunId,
140 }),
141 ...(params.resume && { resume: params.resume }),
142 outputSchema: recipeSchema,
143 stream: true,
144 abortController,
145 })
146 : feature === 'agentic-structured-stream'
147 ? chat({
148 ...adapterOptions,
149 tools: config.tools,
150 modelOptions,
151 systemPrompts,
152 agentLoopStrategy: maxIterations(5),
153 messages: params.messages,
154 threadId: params.threadId,
155 runId: params.runId,
156 ...(params.parentRunId && {
157 parentRunId: params.parentRunId,
158 }),
159 ...(params.resume && { resume: params.resume }),
160 outputSchema: guitarRecommendationSchema,
161 stream: true,
162 abortController,
163 })
164 : chat({
165 ...adapterOptions,
166 tools: config.tools,
167 modelOptions,
168 systemPrompts,
169 agentLoopStrategy: maxIterations(5),
170 messages: params.messages,
171 threadId: params.threadId,
172 runId: params.runId,
173 ...(params.parentRunId && {
174 parentRunId: params.parentRunId,
175 }),
176 ...(params.resume && { resume: params.resume }),
177 ...(rootObservabilityMetadata && {
178 metadata: rootObservabilityMetadata,
179 }),
180 abortController,
181 })
182​
183 // Cast: `chat()` returns `AsyncIterable<StreamChunk> |
184 // StructuredOutputStream<T>`. Both yield AG-UI events at runtime
185 // but the typed-CUSTOM-events variants on `StructuredOutputStream`
186 // aren't structurally assignable to the bare `StreamChunk` union
187 // (zod-passthrough index-signature variance). The runtime is fine
188 // either way.
189 return toServerSentEventsResponse(
190 stream as AsyncIterable<StreamChunk>,
191 { abortController },
192 )
193 } catch (error) {
194 console.error('[api.chat] Error:', error)
195 if (
196 (error instanceof Error && error.name === 'AbortError') ||
197 abortController.signal.aborted
198 ) {
199 return new Response(null, { status: 499 })
200 }
201 const message =
202 error instanceof Error ? error.message : 'An error occurred'
203 return new Response(JSON.stringify({ error: message }), {
204 status: 500,
205 headers: { 'Content-Type': 'application/json' },
206 })
207 }
208 },
209 },
210 },
211})