-
-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathapi.tanchat.ts
More file actions
312 lines (288 loc) · 10.1 KB
/
api.tanchat.ts
File metadata and controls
312 lines (288 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import { createFileRoute } from '@tanstack/react-router'
import {
chat,
createChatOptions,
maxIterations,
toServerSentEventsResponse,
} from '@tanstack/ai'
import { openaiText } from '@tanstack/ai-openai'
import { ollamaText } from '@tanstack/ai-ollama'
import { anthropicText } from '@tanstack/ai-anthropic'
import { geminiText } from '@tanstack/ai-gemini'
import { openRouterText } from '@tanstack/ai-openrouter'
import { grokText } from '@tanstack/ai-grok'
import type { AnyTextAdapter, ChatMiddleware } from '@tanstack/ai'
import { groqText } from '@tanstack/ai-groq'
import {
addToCartToolDef,
addToWishListToolDef,
calculateFinancing,
compareGuitars,
getGuitars,
getPersonalGuitarPreferenceToolDef,
recommendGuitarToolDef,
searchGuitars,
} from '@/lib/guitar-tools'
type Provider =
| 'openai'
| 'anthropic'
| 'gemini'
| 'ollama'
| 'grok'
| 'groq'
| 'openrouter'
const SYSTEM_PROMPT = `You are a helpful assistant for a guitar store.
CRITICAL INSTRUCTIONS - YOU MUST FOLLOW THIS EXACT WORKFLOW:
When a user asks for a guitar recommendation:
1. FIRST: Use the getGuitars tool (no parameters needed)
2. SECOND: Use the recommendGuitar tool with the ID of the guitar you want to recommend
3. NEVER write a recommendation directly - ALWAYS use the recommendGuitar tool
IMPORTANT:
- The recommendGuitar tool will display the guitar in a special, appealing format
- You MUST use recommendGuitar for ANY guitar recommendation
- ONLY recommend guitars from our inventory (use getGuitars first)
- The recommendGuitar tool has a buy button - this is how customers purchase
- Do NOT describe the guitar yourself - let the recommendGuitar tool do it
Example workflow:
User: "I want an acoustic guitar"
Step 1: Call getGuitars()
Step 2: Call recommendGuitar(id: "6")
Step 3: Done - do NOT add any text after calling recommendGuitar
`
const addToCartToolServer = addToCartToolDef.server((args, context) => {
context?.emitCustomEvent('tool:progress', {
tool: 'addToCart',
message: `Adding ${args.quantity}x guitar ${args.guitarId} to cart`,
})
const cartId = 'CART_' + Date.now()
context?.emitCustomEvent('tool:progress', {
tool: 'addToCart',
message: `Cart ${cartId} created successfully`,
})
return {
success: true,
cartId,
guitarId: args.guitarId,
quantity: args.quantity,
totalItems: args.quantity,
}
})
const loggingMiddleware: ChatMiddleware = {
name: 'logging',
onConfig(ctx, config) {
console.log(
`[logging] onConfig iteration=${ctx.iteration} model=${ctx.model} tools=${config.tools.length}`,
)
},
onStart(ctx) {
console.log(`[logging] onStart requestId=${ctx.requestId}`)
},
onIteration(ctx, info) {
console.log(`[logging] onIteration iteration=${info.iteration}`)
},
onBeforeToolCall(ctx, toolCtx) {
console.log(`[logging] onBeforeToolCall tool=${toolCtx.toolName}`)
},
onAfterToolCall(ctx, info) {
console.log(
`[logging] onAfterToolCall tool=${info.toolName} result=${JSON.stringify(info.result).slice(0, 100)}`,
)
},
onFinish(ctx, info) {
console.log(
`[logging] onFinish reason=${info.finishReason} iterations=${ctx.iteration}`,
)
},
onUsage(ctx, usage) {
console.log(
`[logging] onUsage tokens=${usage.totalTokens} input=${usage.promptTokens} output=${usage.completionTokens}, total: ${usage.totalTokens}`,
)
},
}
// ===========================
// TypedStreamChunk showcase — type-safe tool call events
// ===========================
//
// When `chat()` receives tools with typed schemas, the returned stream
// carries type information on TOOL_CALL_START and TOOL_CALL_END events.
// No casts, no `as any` — just narrow by `chunk.type` and everything is typed.
const tools = [
getGuitars,
recommendGuitarToolDef,
addToCartToolServer,
addToWishListToolDef,
getPersonalGuitarPreferenceToolDef,
compareGuitars,
calculateFinancing,
searchGuitars,
] as const
async function typedStreamShowcase() {
const stream = chat({
adapter: openaiText('gpt-4o'),
messages: [
{ role: 'user' as const, content: 'Recommend an acoustic guitar' },
],
tools,
})
for await (const chunk of stream) {
switch (chunk.type) {
case 'TOOL_CALL_START':
// ✅ chunk.toolName is typed as the union of all tool name literals:
// 'getGuitars' | 'recommendGuitar' | 'addToCart' | 'addToWishList'
// | 'getPersonalGuitarPreference' | 'compareGuitars'
// | 'calculateFinancing' | 'searchGuitars'
//
// ❌ Without TypedStreamChunk, this would just be `string`
console.log(`Tool call started: ${chunk.toolName}`)
break
case 'TOOL_CALL_END':
// ✅ chunk.toolName — same typed literal union as above
// ✅ chunk.input — union of all tool input types, inferred from Zod schemas:
// | {}
// | { id: string | number }
// | { guitarId: string; quantity: number }
// | { guitarId: string }
// | { guitarIds: number[] }
// | { guitarId: number; months: number }
// | { query: string }
console.log(`Tool call ended: ${chunk.toolName}`, chunk.input)
break
case 'TEXT_MESSAGE_CONTENT':
// Non-tool events are unaffected — still fully typed
console.log(chunk.delta)
break
}
}
}
// Suppress unused warning — this is a showcase, not called at runtime
void typedStreamShowcase
export const Route = createFileRoute('/api/tanchat')({
server: {
handlers: {
POST: async ({ request }) => {
// Capture request signal before reading body (it may be aborted after body is consumed)
const requestSignal = request.signal
// If request is already aborted, return early
if (requestSignal.aborted) {
return new Response(null, { status: 499 }) // 499 = Client Closed Request
}
const abortController = new AbortController()
const body = await request.json()
const { messages, data } = body
// Extract provider and model from data
const provider: Provider = data?.provider || 'openai'
const model: string = data?.model || 'gpt-4o'
const conversationId: string | undefined = data?.conversationId
// Pre-define typed adapter configurations with full type inference
// Model is passed to the adapter factory function for type-safe autocomplete
const adapterConfig: Record<
Provider,
() => { adapter: AnyTextAdapter }
> = {
anthropic: () =>
createChatOptions({
adapter: anthropicText(
(model || 'claude-sonnet-4-5') as 'claude-sonnet-4-5',
),
}),
openrouter: () =>
createChatOptions({
adapter: openRouterText('openai/gpt-5.1'),
modelOptions: {
models: ['openai/chatgpt-4o-latest'],
route: 'fallback',
reasoning: {
effort: 'medium',
},
},
}),
gemini: () =>
createChatOptions({
adapter: geminiText(
(model || 'gemini-2.5-flash') as 'gemini-2.5-flash',
),
modelOptions: {
thinkingConfig: {
includeThoughts: true,
thinkingBudget: 100,
},
},
}),
grok: () =>
createChatOptions({
adapter: grokText((model || 'grok-3') as 'grok-3'),
modelOptions: {},
}),
groq: () =>
createChatOptions({
adapter: groqText(
(model ||
'llama-3.3-70b-versatile') as 'llama-3.3-70b-versatile',
),
}),
ollama: () =>
createChatOptions({
adapter: ollamaText((model || 'gpt-oss:120b') as 'gpt-oss:120b'),
modelOptions: { think: 'low', options: { top_k: 1 } },
}),
openai: () =>
createChatOptions({
adapter: openaiText((model || 'gpt-4o') as 'gpt-4o'),
modelOptions: {},
}),
}
try {
// Get typed adapter options using createChatOptions pattern
const options = adapterConfig[provider]()
// Note: We cast to AsyncIterable<StreamChunk> because all chat adapters
// return streams, but TypeScript sees a union of all possible return types
const stream = chat({
...options,
tools: [
getGuitars, // Server tool
recommendGuitarToolDef, // No server execute - client will handle
addToCartToolServer,
addToWishListToolDef,
getPersonalGuitarPreferenceToolDef,
// Lazy tools - discovered on demand
compareGuitars,
calculateFinancing,
searchGuitars,
],
middleware: [loggingMiddleware],
systemPrompts: [SYSTEM_PROMPT],
agentLoopStrategy: maxIterations(20),
messages,
abortController,
conversationId,
})
return toServerSentEventsResponse(stream, { abortController })
} catch (error: any) {
console.error('[API Route] Error in chat request:', {
message: error?.message,
name: error?.name,
status: error?.status,
statusText: error?.statusText,
code: error?.code,
type: error?.type,
stack: error?.stack,
error: error,
})
// If request was aborted, return early (don't send error response)
if (error.name === 'AbortError' || abortController.signal.aborted) {
return new Response(null, { status: 499 }) // 499 = Client Closed Request
}
return new Response(
JSON.stringify({
error: error.message || 'An error occurred',
}),
{
status: 500,
headers: { 'Content-Type': 'application/json' },
},
)
}
},
},
},
})