1- import { createFileRoute } from " @tanstack/react-router" ;
2- import { chat , maxIterations , toStreamResponse } from " @tanstack/ai" ;
3- import { anthropicText } from " @tanstack/ai-anthropic" ;
4- import { openaiText } from " @tanstack/ai-openai" ;
5- import { geminiText } from " @tanstack/ai-gemini" ;
6- import { ollamaText } from " @tanstack/ai-ollama" ;
1+ import { createFileRoute } from ' @tanstack/react-router'
2+ import { chat , maxIterations , toServerSentEventsResponse } from ' @tanstack/ai'
3+ import { anthropicText } from ' @tanstack/ai-anthropic'
4+ import { openaiText } from ' @tanstack/ai-openai'
5+ import { geminiText } from ' @tanstack/ai-gemini'
6+ import { ollamaText } from ' @tanstack/ai-ollama'
77
8- import { getGuitars , recommendGuitarToolDef } from " @/lib/example.guitar-tools" ;
9- import type { Provider } from " @/lib/model-selection" ;
8+ import { getGuitars , recommendGuitarToolDef } from ' @/lib/example.guitar-tools'
9+ import type { Provider } from ' @/lib/model-selection'
1010
1111const SYSTEM_PROMPT = `You are a helpful assistant for a store that sells guitars.
1212
@@ -23,39 +23,39 @@ IMPORTANT:
2323- ONLY recommend guitars from our inventory (use getGuitars first)
2424- The recommendGuitar tool has a buy button - this is how customers purchase
2525- Do NOT describe the guitar yourself - let the recommendGuitar tool do it
26- ` ;
26+ `
2727
28- export const Route = createFileRoute ( " /demo/api/tanchat" ) ( {
28+ export const Route = createFileRoute ( ' /demo/api/tanchat' ) ( {
2929 server : {
3030 handlers : {
3131 POST : async ( { request } ) => {
3232 // Capture request signal before reading body (it may be aborted after body is consumed)
33- const requestSignal = request . signal ;
33+ const requestSignal = request . signal
3434
3535 // If request is already aborted, return early
3636 if ( requestSignal . aborted ) {
37- return new Response ( null , { status : 499 } ) ; // 499 = Client Closed Request
37+ return new Response ( null , { status : 499 } ) // 499 = Client Closed Request
3838 }
3939
40- const abortController = new AbortController ( ) ;
40+ const abortController = new AbortController ( )
4141
4242 try {
43- const body = await request . json ( ) ;
44- const { messages } = body ;
45- const data = body . data || { } ;
46- const provider : Provider = data . provider || " anthropic" ;
47- const model : string = data . model || " claude-haiku-4-5" ;
43+ const body = await request . json ( )
44+ const { messages } = body
45+ const data = body . data || { }
46+ const provider : Provider = data . provider || ' anthropic'
47+ const model : string = data . model || ' claude-haiku-4-5'
4848
4949 // Adapter factory pattern for multi-vendor support
5050 const adapterConfig = {
5151 anthropic : ( ) =>
52- anthropicText ( ( model || " claude-haiku-4-5" ) as any ) ,
53- openai : ( ) => openaiText ( ( model || " gpt-4o" ) as any ) ,
54- gemini : ( ) => geminiText ( ( model || " gemini-2.0-flash-exp" ) as any ) ,
55- ollama : ( ) => ollamaText ( ( model || " mistral:7b" ) as any ) ,
56- } ;
52+ anthropicText ( ( model || ' claude-haiku-4-5' ) as any ) ,
53+ openai : ( ) => openaiText ( ( model || ' gpt-4o' ) as any ) ,
54+ gemini : ( ) => geminiText ( ( model || ' gemini-2.0-flash-exp' ) as any ) ,
55+ ollama : ( ) => ollamaText ( ( model || ' mistral:7b' ) as any ) ,
56+ }
5757
58- const adapter = adapterConfig [ provider ] ( ) ;
58+ const adapter = adapterConfig [ provider ] ( )
5959
6060 const stream = chat ( {
6161 adapter,
@@ -67,23 +67,23 @@ export const Route = createFileRoute("/demo/api/tanchat")({
6767 agentLoopStrategy : maxIterations ( 5 ) ,
6868 messages,
6969 abortController,
70- } ) ;
70+ } )
7171
72- return toStreamResponse ( stream , { abortController } ) ;
72+ return toServerSentEventsResponse ( stream , { abortController } )
7373 } catch ( error : any ) {
7474 // If request was aborted, return early (don't send error response)
75- if ( error . name === " AbortError" || abortController . signal . aborted ) {
76- return new Response ( null , { status : 499 } ) ; // 499 = Client Closed Request
75+ if ( error . name === ' AbortError' || abortController . signal . aborted ) {
76+ return new Response ( null , { status : 499 } ) // 499 = Client Closed Request
7777 }
7878 return new Response (
79- JSON . stringify ( { error : " Failed to process chat request" } ) ,
79+ JSON . stringify ( { error : ' Failed to process chat request' } ) ,
8080 {
8181 status : 500 ,
82- headers : { " Content-Type" : " application/json" } ,
83- }
84- ) ;
82+ headers : { ' Content-Type' : ' application/json' } ,
83+ } ,
84+ )
8585 }
8686 } ,
8787 } ,
8888 } ,
89- } ) ;
89+ } )
0 commit comments