-
-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathapi.product-codemode.ts
More file actions
314 lines (272 loc) · 9.74 KB
/
api.product-codemode.ts
File metadata and controls
314 lines (272 loc) · 9.74 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
313
314
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { createFileRoute } from '@tanstack/react-router'
import { chat, maxIterations, toServerSentEventsStream } from '@tanstack/ai'
import { createCodeMode } from '@tanstack/ai-code-mode'
import { anthropicText } from '@tanstack/ai-anthropic'
import { openaiText } from '@tanstack/ai-openai'
import { geminiText } from '@tanstack/ai-gemini'
import { zaiText } from '@tanstack/ai-zai'
import {
createAlwaysTrustedStrategy,
createSkillManagementTools,
createSkillsSystemPrompt,
skillsToTools,
} from '@tanstack/ai-code-mode-skills'
import { createFileSkillStorage } from '@tanstack/ai-code-mode-skills/storage'
import type { AnyTextAdapter, ServerTool, StreamChunk } from '@tanstack/ai'
import type { IsolateDriver } from '@tanstack/ai-code-mode'
import { productTools } from '@/lib/tools/product-tools'
type Provider = 'anthropic' | 'openai' | 'gemini' | 'zai'
function getAdapter(provider: Provider, model?: string): AnyTextAdapter {
switch (provider) {
case 'openai':
return openaiText((model || 'gpt-4o') as 'gpt-4o')
case 'gemini':
return geminiText((model || 'gemini-2.5-flash') as 'gemini-2.5-flash')
case 'zai':
return zaiText((model || 'glm-4.7') as 'glm-4.7')
case 'anthropic':
default:
return anthropicText((model || 'claude-haiku-4-5') as 'claude-haiku-4-5')
}
}
const PRODUCT_CODE_MODE_SYSTEM_PROMPT = `You are an analytical assistant for a shoe product catalog. You can execute TypeScript code to query the product API and compute answers.
## Available External APIs (inside execute_typescript)
- \`external_getProductListPage({ page })\` — Returns { productIds: string[], totalPages: number } (10 product IDs per page, 1-based page number)
- \`external_getProductByID({ id })\` — Returns full product details: { id, name, brand, price, category, color, sizeRange }
## Strategy
The product API is paginated. To get all products:
1. Call getProductListPage with page 1 to get the first page of IDs and totalPages
2. Fetch remaining pages (2 through totalPages) to get all product IDs
3. Fetch each product by ID
4. Compute the answer from the full dataset
Always write efficient code that does all of this in a single execution — use Promise.all to parallelize fetches.`
// --- Code mode tool (shared by both paths) ---
let codeModeCache: {
tool: ReturnType<typeof createCodeMode>['tool']
systemPrompt: string
driver: IsolateDriver
} | null = null
async function getCodeModeTools() {
if (!codeModeCache) {
const { createIsolateDriver } = await import('@/lib/create-isolate-driver')
const driver = await createIsolateDriver('node')
const { tool, systemPrompt } = createCodeMode({
driver,
tools: productTools,
timeout: 60000,
memoryLimit: 128,
})
codeModeCache = { tool, systemPrompt, driver }
}
return codeModeCache
}
// --- Skills storage (lazy, only used when withSkills=true) ---
const __dirname = fileURLToPath(new URL('.', import.meta.url))
const skillsDir = resolve(__dirname, '../../../.skills')
const trustStrategy = createAlwaysTrustedStrategy()
const skillStorage = createFileSkillStorage({
directory: skillsDir,
trustStrategy,
})
let skillManagementToolsCache: ReturnType<
typeof createSkillManagementTools
> | null = null
function getSkillManagementTools() {
if (!skillManagementToolsCache) {
skillManagementToolsCache = createSkillManagementTools({
storage: skillStorage,
trustStrategy,
})
}
return skillManagementToolsCache
}
const SKILL_REGISTRATION_PROMPT = `## Skill Registration — MANDATORY
After every successful \`execute_typescript\` call you MUST register the code as a reusable skill using \`register_skill\` — unless an identical skill already exists.
Rules:
- \`name\`: descriptive snake_case (e.g. \`get_average_product_price\`)
- \`code\`: the TypeScript code, parameterised with an \`input\` variable where useful
- \`inputSchema\` / \`outputSchema\`: valid JSON Schema **strings**
- If a skill with the same name exists, skip registration
This is not optional — skill registration is a core part of your workflow.`
async function getSkillToolsAndPrompt(driver: IsolateDriver): Promise<{
skillTools: Array<ServerTool<any, any, any>>
skillsPrompt: string
}> {
const allSkills = await skillStorage.loadAll()
const skillIndex = await skillStorage.loadIndex()
const skillTools =
allSkills.length > 0
? skillsToTools({
skills: allSkills,
driver,
tools: productTools,
storage: skillStorage,
timeout: 60000,
memoryLimit: 128,
})
: []
const libraryPrompt = createSkillsSystemPrompt({
selectedSkills: allSkills,
totalSkillCount: skillIndex.length,
skillsAsTools: true,
})
const skillsPrompt = libraryPrompt + '\n\n' + SKILL_REGISTRATION_PROMPT
return { skillTools, skillsPrompt }
}
// --- Instrumentation helper ---
function instrumentAdapter(adapter: AnyTextAdapter): {
adapter: AnyTextAdapter
} {
const baseChatStream = adapter.chatStream.bind(adapter)
let llmCallCount = 0
let totalContextBytes = 0
const textEncoder = new TextEncoder()
const instrumented: AnyTextAdapter = {
...adapter,
chatStream: (options) => {
llmCallCount += 1
let contextBytes = 0
try {
contextBytes = textEncoder.encode(
JSON.stringify(options.messages ?? []),
).length
} catch {
contextBytes = 0
}
totalContextBytes += contextBytes
const averageContextBytes =
llmCallCount > 0 ? Math.round(totalContextBytes / llmCallCount) : 0
const stream = baseChatStream(options)
async function* instrumentedStream(): AsyncGenerator<StreamChunk> {
yield {
type: 'CUSTOM',
model: adapter.model,
timestamp: Date.now(),
name: 'product_codemode:llm_call',
value: {
count: llmCallCount,
contextBytes,
totalContextBytes,
averageContextBytes,
},
} as StreamChunk
for await (const chunk of stream) {
yield chunk
}
}
return instrumentedStream()
},
}
return { adapter: instrumented }
}
function wrapWithTimingEvents(
stream: AsyncGenerator<StreamChunk>,
adapter: AnyTextAdapter,
): AsyncGenerator<StreamChunk> {
const requestStartTimeMs = Date.now()
return (async function* (): AsyncGenerator<StreamChunk> {
yield {
type: 'CUSTOM',
model: adapter.model,
timestamp: requestStartTimeMs,
name: 'product_codemode:chat_start',
value: { startTimeMs: requestStartTimeMs },
} as StreamChunk
for await (const chunk of stream) {
if (chunk.type === 'RUN_FINISHED') {
const endTimeMs = Date.now()
yield {
type: 'CUSTOM',
model: adapter.model,
timestamp: endTimeMs,
name: 'product_codemode:chat_end',
value: {
endTimeMs,
durationMs: endTimeMs - requestStartTimeMs,
},
} as StreamChunk
}
yield chunk
}
})()
}
// --- Route ---
export const Route = createFileRoute('/_home/api/product-codemode')({
server: {
handlers: {
POST: async ({ request }) => {
if (request.signal.aborted) {
return new Response(null, { status: 499 })
}
const abortController = new AbortController()
const body = await request.json()
const { messages, data } = body
const provider: Provider = data?.provider || 'anthropic'
const model: string | undefined = data?.model
const withSkills: boolean = data?.withSkills === true
const rawAdapter = getAdapter(provider, model)
const { adapter: instrumentedAdapter } = instrumentAdapter(rawAdapter)
try {
const {
tool: codeModeTool,
systemPrompt: codeModePrompt,
driver,
} = await getCodeModeTools()
let tools: Array<ServerTool<any, any, any>> = [codeModeTool]
let systemPrompts = [PRODUCT_CODE_MODE_SYSTEM_PROMPT, codeModePrompt]
if (withSkills) {
const { skillTools, skillsPrompt } =
await getSkillToolsAndPrompt(driver)
tools = [codeModeTool, ...getSkillManagementTools(), ...skillTools]
systemPrompts = [
PRODUCT_CODE_MODE_SYSTEM_PROMPT,
codeModePrompt,
skillsPrompt,
]
}
const stream = chat({
adapter: instrumentedAdapter,
messages,
tools,
systemPrompts,
agentLoopStrategy: maxIterations(15),
abortController,
maxTokens: 8192,
})
const instrumentedStream = wrapWithTimingEvents(stream, rawAdapter)
const sseStream = toServerSentEventsStream(
instrumentedStream,
abortController,
)
return new Response(sseStream, {
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
},
})
} catch (error: unknown) {
console.error('[API Product Code Mode] Error:', error)
if (
(error instanceof Error && error.name === 'AbortError') ||
abortController.signal.aborted
) {
return new Response(null, { status: 499 })
}
return new Response(
JSON.stringify({
error:
error instanceof Error ? error.message : 'An error occurred',
}),
{
status: 500,
headers: { 'Content-Type': 'application/json' },
},
)
}
},
},
},
})