Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import {
3 chat,
4 chatParamsFromRequestBody,
5 maxIterations,
6 toServerSentEventsResponse,
7} from '@tanstack/ai'
8import type { Feature, Provider } from '@/lib/types'
9import { createTextAdapter } from '@/lib/providers'
10import { featureConfigs } from '@/lib/features'
11import { guitarRecommendationSchema, recipeSchema } from '@/lib/schemas'
12
13const DEFAULT_SYSTEM_PROMPT = 'You are a helpful assistant for a guitar store.'
14
15export const Route = createFileRoute('/api/chat')({
16 server: {
17 handlers: {
18 POST: async ({ request }) => {
19 await import('@/lib/llmock-server').then((m) => m.ensureLLMock())
20 if (request.signal.aborted) {
21 return new Response(null, { status: 499 })
22 }
23
24 const abortController = new AbortController()
25
26 let params
27 try {
28 params = await chatParamsFromRequestBody(await request.json())
29 } catch (error) {
30 return new Response(
31 error instanceof Error ? error.message : 'Bad request',
32 { status: 400 },
33 )
34 }
35
36 const fp = params.forwardedProps as Record<string, unknown>
37 const provider: Provider = (
38 typeof fp.provider === 'string' ? fp.provider : 'openai'
39 ) as Provider
40 const feature: Feature = (
41 typeof fp.feature === 'string' ? fp.feature : 'chat'
42 ) as Feature
43 const testId = typeof fp.testId === 'string' ? fp.testId : undefined
44 const aimockPort =
45 fp.aimockPort != null ? Number(fp.aimockPort) : undefined
46
47 const config = featureConfigs[feature]
48 const modelOverride = config.modelOverrides?.[provider]
49 const adapterOptions = createTextAdapter(
50 provider,
51 modelOverride,
52 aimockPort,
53 testId,
54 )
55
56 try {
57 const systemPrompt = config.systemPrompt ?? DEFAULT_SYSTEM_PROMPT
58
59 // Test-only flag — when truthy, the route promotes the system
60 // prompt to object-form and attaches Anthropic `cache_control`
61 // metadata. Enables system-prompt-metadata.spec.ts to verify
62 // cache_control reaches the wire via the aimock journal.
63 const systemPromptCacheControl =
64 fp.systemPromptCacheControl === true
65 ? ({ type: 'ephemeral' as const } as const)
66 : undefined
67 const systemPrompts = systemPromptCacheControl
68 ? [
69 {
70 content: systemPrompt,
71 metadata: { cache_control: systemPromptCacheControl },
72 // The route is provider-generic; the metadata type is
73 // adapter-narrowed and only meaningful for Anthropic, so
74 // a single bridge cast lives here at the test entry.
75 // eslint-disable-next-line @typescript-eslint/no-explicit-any
76 } as any,
77 ]
78 : [systemPrompt]
79
80 // Two structured-output-streaming features differ only in which
81 // schema they bind to. Branched per-feature so TS can pick the
82 // right `chat<TSchema>()` overload without a `never` cast.
83 const stream =
84 feature === 'structured-output-stream'
85 ? chat({
86 ...adapterOptions,
87 modelOptions: config.modelOptions,
88 systemPrompts,
89 messages: params.messages,
90 threadId: params.threadId,
91 runId: params.runId,
92 outputSchema: guitarRecommendationSchema,
93 stream: true,
94 abortController,
95 })
96 : feature === 'multi-turn-structured'
97 ? chat({
98 ...adapterOptions,
99 modelOptions: config.modelOptions,
100 systemPrompts,
101 messages: params.messages,
102 threadId: params.threadId,
103 runId: params.runId,
104 outputSchema: recipeSchema,
105 stream: true,
106 abortController,
107 })
108 : chat({
109 ...adapterOptions,
110 tools: config.tools,
111 modelOptions: config.modelOptions,
112 systemPrompts,
113 agentLoopStrategy: maxIterations(5),
114 messages: params.messages,
115 threadId: params.threadId,
116 runId: params.runId,
117 abortController,
118 })
119
120 return toServerSentEventsResponse(stream, { abortController })
121 } catch (error: any) {
122 console.error(`[api.chat] Error:`, error.message)
123 if (error.name === 'AbortError' || abortController.signal.aborted) {
124 return new Response(null, { status: 499 })
125 }
126 return new Response(
127 JSON.stringify({ error: error.message || 'An error occurred' }),
128 {
129 status: 500,
130 headers: { 'Content-Type': 'application/json' },
131 },
132 )
133 }
134 },
135 },
136 },
137})