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

api.chat.ts

103 lines3.5 KB

1import { createFileRoute } from '@tanstack/solid-router'
2import { chat, maxIterations, toServerSentEventsStream } from '@tanstack/ai'
3import { anthropicText } from '@tanstack/ai-anthropic'
4import { serverTools } from '@/lib/guitar-tools'
5​
6const SYSTEM_PROMPT = `You are a helpful assistant for a guitar store.
7​
8CRITICAL INSTRUCTIONS - YOU MUST FOLLOW THIS EXACT WORKFLOW:
9​
10When a user asks for a guitar recommendation:
111. FIRST: Use the getGuitars tool (no parameters needed)
122. SECOND: Use the recommendGuitar tool with the ID of the guitar you want to recommend
133. NEVER write a recommendation directly - ALWAYS use the recommendGuitar tool
14​
15IMPORTANT:
16- The recommendGuitar tool will display the guitar in a special, appealing format
17- You MUST use recommendGuitar for ANY guitar recommendation
18- ONLY recommend guitars from our inventory (use getGuitars first)
19- The recommendGuitar tool has a buy button - this is how customers purchase
20- Do NOT describe the guitar yourself - let the recommendGuitar tool do it
21​
22Example workflow:
23User: "I want an acoustic guitar"
24Step 1: Call getGuitars()
25Step 2: Call recommendGuitar(id: "6")
26Step 3: Done - do NOT add any text after calling recommendGuitar
28​
29export const Route = createFileRoute('/api/chat')({
30 server: {
31 handlers: {
32 POST: async ({ request }) => {
33 if (!process.env.ANTHROPIC_API_KEY) {
34 return new Response(
35 JSON.stringify({
36 error:
37 'ANTHROPIC_API_KEY not configured. Please add it to .env or .env.local',
38 }),
39 {
40 status: 500,
41 headers: { 'Content-Type': 'application/json' },
42 },
43 )
44 }
45​
46 // Capture request signal before reading body (it may be aborted after body is consumed)
47 const requestSignal = request.signal
48​
49 // If request is already aborted, return early
50 if (requestSignal?.aborted) {
51 return new Response(null, { status: 499 }) // 499 = Client Closed Request
52 }
53​
54 const abortController = new AbortController()
55​
56 const { messages } = await request.json()
57 try {
58 // Use the stream abort signal for proper cancellation handling
59 const stream = chat({
60 adapter: anthropicText('claude-sonnet-4-5'),
61 tools: serverTools,
62 systemPrompts: [SYSTEM_PROMPT],
63 agentLoopStrategy: maxIterations(20),
64 messages,
65 modelOptions: {
66 thinking: {
67 type: 'enabled',
68 budget_tokens: 10000,
69 },
70 },
71 abortController,
72 })
73​
74 const readableStream = toServerSentEventsStream(
75 stream,
76 abortController,
77 )
78 return new Response(readableStream, {
79 headers: {
80 'Content-Type': 'text/event-stream',
81 'Cache-Control': 'no-cache',
82 Connection: 'keep-alive',
83 },
84 })
85 } catch (error: any) {
86 // If request was aborted, return early (don't send error response)
87 if (error.name === 'AbortError' || abortController.signal.aborted) {
88 return new Response(null, { status: 499 }) // 499 = Client Closed Request
89 }
90 return new Response(
91 JSON.stringify({
92 error: error.message || 'An error occurred',
93 }),
94 {
95 status: 500,
96 headers: { 'Content-Type': 'application/json' },
97 },
98 )
99 }
100 },
101 },
102 },
103})