Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import { chat, maxIterations, toServerSentEventsResponse } from '@tanstack/ai'
3import type { AIAdapter, ChatOptions, StreamChunk } from '@tanstack/ai'
4
5import {
6 clientServerTool,
7 clientServerToolWithApproval,
8 clientToolDef,
9 clientToolWithApprovalDef,
10 serverTool,
11 serverToolWithApproval,
12} from '@/lib/simulator-tools'
13
14/**
15 * Tool call syntax: toolName({ arg: value, arg2: value2 })
16 * Multiple calls can be separated by newlines or semicolons
17 *
18 * Examples:
19 * serverTool({ text: "hello" })
20 * clientTool({ delay: 2 })
21 * serverToolWithApproval({ text: "needs approval", delay: 1 })
22 */
23const TOOL_CALL_REGEX = /(\w+)\s*\(\s*(\{[^}]*\})\s*\)/g
24
25interface ParsedToolCall {
26 name: string
27 arguments: Record<string, any>
28}
29
30function parseToolCalls(message: string): Array<ParsedToolCall> {
31 TOOL_CALL_REGEX.lastIndex = 0
32 const toolCalls: Array<ParsedToolCall> = []
33 let match
34
35 while ((match = TOOL_CALL_REGEX.exec(message)) !== null) {
36 const name = match[1]
37 const argsString = match[2]
38
39 try {
40 // Parse the JSON-like arguments
41 // Handle simple cases like { text: "hello" } by converting to proper JSON
42 const jsonArgs = argsString.replace(/(\w+)\s*:/g, '"$1":')
43 const args = JSON.parse(jsonArgs)
44 toolCalls.push({ name, arguments: args })
45 } catch {
46 // If parsing fails, try to parse as-is
47 try {
48 const args = JSON.parse(argsString)
49 toolCalls.push({ name, arguments: args })
50 } catch {
51 console.error(`Failed to parse tool call arguments: ${argsString}`)
52 }
53 }
54 }
55
56 return toolCalls
57}
58
59// Valid tool names
60const VALID_TOOLS = new Set([
61 'serverTool',
62 'serverToolWithApproval',
63 'clientTool',
64 'clientToolWithApproval',
65 'clientServerTool',
66 'clientServerToolWithApproval',
67])
68
69/**
70 * Simulated LLM adapter that:
71 * - Echoes messages back if no tool calls detected
72 * - Parses tool call syntax and generates appropriate chunks
73 */
74function createSimulatorAdapter(): AIAdapter {
75 return {
76 name: 'simulator',
77 models: ['simulator-v1'] as const,
78 _modelProviderOptionsByName: {},
79
80 async *chatStream(options: ChatOptions): AsyncIterable<StreamChunk> {
81 const messages = options.messages
82 const lastMessage = messages[messages.length - 1]
83
84 // Check if this is a tool result - if so, acknowledge it
85 if (lastMessage?.role === 'tool') {
86 const timestamp = Date.now()
87 const id = `sim-${timestamp}`
88
89 // Generate acknowledgment response
90 const content =
91 'Tool execution completed. The result has been processed.'
92
93 // Stream content character by character for realistic effect
94 let accumulated = ''
95 for (const char of content) {
96 accumulated += char
97 yield {
98 type: 'content',
99 id,
100 model: 'simulator-v1',
101 timestamp,
102 delta: char,
103 content: accumulated,
104 role: 'assistant',
105 }
106 // Small delay for streaming effect
107 await new Promise((resolve) => setTimeout(resolve, 10))
108 }
109
110 yield {
111 type: 'done',
112 id,
113 model: 'simulator-v1',
114 timestamp: Date.now(),
115 finishReason: 'stop',
116 usage: {
117 promptTokens: 10,
118 completionTokens: content.length,
119 totalTokens: 10 + content.length,
120 },
121 }
122 return
123 }
124
125 // Get the user's message content
126 const userContent =
127 typeof lastMessage?.content === 'string'
128 ? lastMessage.content
129 : Array.isArray(lastMessage?.content)
130 ? lastMessage.content
131 .filter((p: any) => p.type === 'text')
132 .map((p: any) => p.content)
133 .join(' ')
134 : ''
135
136 // Parse for tool calls
137 const toolCalls = parseToolCalls(userContent)
138 const validToolCalls = toolCalls.filter((tc) => VALID_TOOLS.has(tc.name))
139
140 const timestamp = Date.now()
141 const id = `sim-${timestamp}`
142
143 if (validToolCalls.length === 0) {
144 // No tool calls - echo the message back
145 const echoContent = `[Echo] ${userContent}`
146
147 // Stream content character by character
148 let accumulated = ''
149 for (const char of echoContent) {
150 accumulated += char
151 yield {
152 type: 'content',
153 id,
154 model: 'simulator-v1',
155 timestamp,
156 delta: char,
157 content: accumulated,
158 role: 'assistant',
159 }
160 // Small delay for streaming effect
161 await new Promise((resolve) => setTimeout(resolve, 15))
162 }
163
164 yield {
165 type: 'done',
166 id,
167 model: 'simulator-v1',
168 timestamp: Date.now(),
169 finishReason: 'stop',
170 usage: {
171 promptTokens: 10,
172 completionTokens: echoContent.length,
173 totalTokens: 10 + echoContent.length,
174 },
175 }
176 } else {
177 // Generate tool calls
178 for (let i = 0; i < validToolCalls.length; i++) {
179 const tc = validToolCalls[i]
180 const toolCallId = `call-${timestamp}-${i}`
181 const argsJson = JSON.stringify(tc.arguments)
182
183 // Stream tool call arguments character by character
184 // The arguments field contains the DELTA (incremental), not accumulated
185 for (const char of argsJson) {
186 yield {
187 type: 'tool_call',
188 id,
189 model: 'simulator-v1',
190 timestamp,
191 toolCall: {
192 id: toolCallId,
193 type: 'function',
194 function: {
195 name: tc.name,
196 arguments: char, // Delta only, not accumulated
197 },
198 },
199 index: i,
200 }
201 // Small delay for streaming effect
202 await new Promise((resolve) => setTimeout(resolve, 5))
203 }
204 }
205
206 yield {
207 type: 'done',
208 id,
209 model: 'simulator-v1',
210 timestamp: Date.now(),
211 finishReason: 'tool_calls',
212 usage: { promptTokens: 10, completionTokens: 50, totalTokens: 60 },
213 }
214 }
215 },
216
217 async summarize() {
218 throw new Error('Summarize not supported in simulator')
219 },
220
221 async createEmbeddings() {
222 throw new Error('Embeddings not supported in simulator')
223 },
224 }
225}
226
227export const Route = createFileRoute('/api/simulator-chat')({
228 server: {
229 handlers: {
230 POST: async ({ request }) => {
231 if (request.signal.aborted) {
232 return new Response(null, { status: 499 })
233 }
234
235 const abortController = new AbortController()
236 const body = await request.json()
237 const messages = body.messages
238
239 try {
240 const adapter = createSimulatorAdapter()
241
242 const stream = chat({
243 adapter: adapter as any,
244 model: 'simulator-v1',
245 tools: [
246 // Server tools with implementations
247 serverTool,
248 serverToolWithApproval,
249 clientServerTool,
250 clientServerToolWithApproval,
251 // Client-only tools (no server execute)
252 clientToolDef,
253 clientToolWithApprovalDef,
254 ],
255 systemPrompts: [],
256 agentLoopStrategy: maxIterations(10),
257 messages,
258 abortController,
259 })
260
261 return toServerSentEventsResponse(stream, { abortController })
262 } catch (error: any) {
263 console.error('[Simulator API] Error:', error)
264 if (error.name === 'AbortError' || abortController.signal.aborted) {
265 return new Response(null, { status: 499 })
266 }
267 return new Response(
268 JSON.stringify({ error: error.message || 'An error occurred' }),
269 {
270 status: 500,
271 headers: { 'Content-Type': 'application/json' },
272 },
273 )
274 }
275 },
276 },
277 },
278})