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

api.codemode.ts

197 lines6.5 KB

1import { createFileRoute } from '@tanstack/react-router'
2import { chat, maxIterations, toServerSentEventsStream } from '@tanstack/ai'
3import { createCodeMode } from '@tanstack/ai-code-mode'
4import { anthropicText } from '@tanstack/ai-anthropic'
5import { openaiText } from '@tanstack/ai-openai'
6import { geminiText } from '@tanstack/ai-gemini'
7import type { AnyTextAdapter, StreamChunk } from '@tanstack/ai'
8import { allTools } from '@/lib/tools'
9import { CODE_MODE_SYSTEM_PROMPT } from '@/lib/prompts'
10import { exportConversationToPdfTool } from '@/lib/tools/export-pdf-tool'
11import { maxTokensModelOptions } from '@/lib/max-tokens-model-options'
12​
13type Provider = 'anthropic' | 'openai' | 'gemini'
14​
15function getAdapter(provider: Provider, model?: string): AnyTextAdapter {
16 switch (provider) {
17 case 'openai':
18 return openaiText((model || 'gpt-4o') as 'gpt-4o')
19 case 'gemini':
20 return geminiText((model || 'gemini-2.5-flash') as 'gemini-2.5-flash')
21 case 'anthropic':
22 default:
23 return anthropicText((model || 'claude-haiku-4-5') as 'claude-haiku-4-5')
24 }
26​
27import {
28 createIsolateDriver,
29 type IsolateVM,
30} from '@/lib/create-isolate-driver'
31​
32type CodeModeEntry = {
33 tool: ReturnType<typeof createCodeMode>['tool']
34 systemPrompt: string
36const codeModeCacheByVM = new Map<IsolateVM, CodeModeEntry>()
37​
38async function getCodeModeTools(vm: IsolateVM = 'node') {
39 let entry = codeModeCacheByVM.get(vm)
40 if (!entry) {
41 const driver = await createIsolateDriver(vm)
42 const { tool, systemPrompt } = createCodeMode({
43 driver,
44 tools: allTools,
45 timeout: 60000,
46 memoryLimit: 128,
47 })
48 entry = { tool, systemPrompt }
49 codeModeCacheByVM.set(vm, entry)
50 }
51 return entry
53​
54export const Route = createFileRoute('/_npm-github-chat/api/codemode')({
55 server: {
56 handlers: {
57 POST: async ({ request }) => {
58 const requestSignal = request.signal
59 if (requestSignal.aborted) {
60 return new Response(null, { status: 499 })
61 }
62​
63 const abortController = new AbortController()
64 const body = await request.json()
65 const { messages, forwardedProps: data } = body
66​
67 const provider: Provider = data?.provider || 'anthropic'
68 const model: string | undefined = data?.model
69 const vm: IsolateVM = data?.vm || 'node'
70 const serverRuntime =
71 typeof (globalThis as { Bun?: unknown }).Bun !== 'undefined'
72 ? 'bun'
73 : 'node'
74 console.info(
75 `[api/codemode] request provider=${provider} model=${model ?? 'default'} vm=${vm} serverRuntime=${serverRuntime}`,
76 )
77​
78 const adapter = getAdapter(provider, model)
79 const baseChatStream = adapter.chatStream.bind(adapter)
80 let llmCallCount = 0
81 let totalContextBytes = 0
82 const textEncoder = new TextEncoder()
83 const instrumentedAdapter: AnyTextAdapter = {
84 ...adapter,
85 chatStream: (options) => {
86 llmCallCount += 1
87 let contextBytes = 0
88 try {
89 contextBytes = textEncoder.encode(
90 JSON.stringify(options.messages ?? []),
91 ).length
92 } catch {
93 contextBytes = 0
94 }
95 totalContextBytes += contextBytes
96 const averageContextBytes =
97 llmCallCount > 0
98 ? Math.round(totalContextBytes / llmCallCount)
99 : 0
100 const stream = baseChatStream(options)
101 async function* instrumentedStream(): AsyncGenerator<StreamChunk> {
102 yield {
103 type: 'CUSTOM',
104 timestamp: Date.now(),
105 name: 'code_mode:llm_call',
106 value: {
107 count: llmCallCount,
108 contextBytes,
109 totalContextBytes,
110 averageContextBytes,
111 },
112 } as StreamChunk
113 for await (const chunk of stream) {
114 yield chunk
115 }
116 }
117 return instrumentedStream()
118 },
119 }
120 const { tool, systemPrompt } = await getCodeModeTools(vm)
121​
122 try {
123 const stream = chat({
124 threadId: body.threadId,
125 adapter: instrumentedAdapter,
126 messages,
127 tools: [tool, exportConversationToPdfTool],
128 systemPrompts: [CODE_MODE_SYSTEM_PROMPT, systemPrompt],
129 agentLoopStrategy: maxIterations(15),
130 abortController,
131 // Increase max tokens to allow for complex code generation
132 modelOptions: maxTokensModelOptions(adapter, 8192),
133 })
134​
135 const requestStartTimeMs = Date.now()
136 const instrumentedStream =
137 (async function* (): AsyncGenerator<StreamChunk> {
138 yield {
139 type: 'CUSTOM',
140 timestamp: requestStartTimeMs,
141 name: 'code_mode:chat_start',
142 value: { startTimeMs: requestStartTimeMs },
143 } as StreamChunk
144 for await (const chunk of stream) {
145 if (chunk.type === 'RUN_FINISHED') {
146 const endTimeMs = Date.now()
147 yield {
148 type: 'CUSTOM',
149 timestamp: endTimeMs,
150 name: 'code_mode:chat_end',
151 value: {
152 endTimeMs,
153 durationMs: endTimeMs - requestStartTimeMs,
154 },
155 } as StreamChunk
156 }
157 yield chunk
158 }
159 })()
160​
161 const sseStream = toServerSentEventsStream(
162 instrumentedStream,
163 abortController,
164 )
165​
166 return new Response(sseStream, {
167 headers: {
168 'Content-Type': 'text/event-stream',
169 'Cache-Control': 'no-cache',
170 Connection: 'keep-alive',
171 },
172 })
173 } catch (error: unknown) {
174 console.error('[API Route] Error:', error)
175​
176 if (
177 (error instanceof Error && error.name === 'AbortError') ||
178 abortController.signal.aborted
179 ) {
180 return new Response(null, { status: 499 })
181 }
182​
183 return new Response(
184 JSON.stringify({
185 error:
186 error instanceof Error ? error.message : 'An error occurred',
187 }),
188 {
189 status: 500,
190 headers: { 'Content-Type': 'application/json' },
191 },
192 )
193 }
194 },
195 },
196 },
197})