Open History to see the last commit
1import type { ModelMessage, StreamChunk } from '@tanstack/ai'
2
3/**
4 * Stub LLM for testing tool calls without burning tokens
5 * Detects which tool to call from user message keywords
6 */
7export async function* stubLLM(
8 messages: ModelMessage[],
9): AsyncIterable<StreamChunk> {
10 const lastMessage = messages[messages.length - 1]
11 const userMessage = lastMessage?.content?.toLowerCase() || ''
12
13 const baseId = `stub_${Date.now()}`
14 const timestamp = Date.now()
15
16 // Check if we have any assistant messages with tool calls already
17 // If so, this is a continuation after approval/execution, not a new request
18 // Handle both ModelMessage (toolCalls) and UIMessage (parts) formats
19 const hasExistingToolCalls = messages.some((m) => {
20 if (m.role !== 'assistant') return false
21 // Check ModelMessage format
22 if (m.toolCalls && m.toolCalls.length > 0) return true
23 // Check UIMessage format
24 if ((m as any).parts) {
25 const parts = (m as any).parts
26 return parts.some((p: any) => p.type === 'tool-call')
27 }
28 return false
29 })
30
31 if (hasExistingToolCalls && lastMessage?.role === 'assistant') {
32 // This means we're being called after an approval/tool execution
33 // Check if the user approved or denied by looking at tool results
34 let wasApproved = false
35 let wasDenied = false
36
37 // Check for tool results
38 const toolResults = messages.filter((m) => m.role === 'tool')
39 if (toolResults.length > 0) {
40 // Check if any were successful or had errors
41 for (const result of toolResults) {
42 try {
43 const parsed = JSON.parse(result.content || '{}')
44 if (parsed.error && parsed.error.includes('declined')) {
45 wasDenied = true
46 } else if (parsed.success || !parsed.error) {
47 wasApproved = true
48 }
49 } catch {
50 // If we can't parse, assume success
51 wasApproved = true
52 }
53 }
54 } else {
55 // No tool results yet, must have just gotten approval response
56 // Check the assistant message for approval status in UIMessage format
57 if ((lastMessage as any).parts) {
58 const parts = (lastMessage as any).parts
59 for (const part of parts) {
60 if (part.type === 'tool-call' && part.approval) {
61 if (part.approval.approved === true) {
62 wasApproved = true
63 } else if (part.approval.approved === false) {
64 wasDenied = true
65 }
66 }
67 }
68 } else if (lastMessage.toolCalls) {
69 // This is a ModelMessage, check if it has approval info in content
70 // (won't have it here, but we can infer from lack of tool results)
71 // Default to approved for now
72 wasApproved = true
73 }
74 }
75
76 let response = ''
77 if (wasDenied) {
78 response =
79 'No worries! Maybe another time. Let me know if you need anything else.'
80 } else if (wasApproved) {
81 response = 'All set! Let me know if you need anything else.'
82 } else {
83 response = 'Let me know if you need anything else!'
84 }
85
86 for (let i = 0; i < response.length; i++) {
87 yield {
88 type: 'content',
89 id: baseId,
90 model: 'stub-llm',
91 timestamp,
92 delta: response[i],
93 content: response.substring(0, i + 1),
94 role: 'assistant',
95 }
96 }
97
98 yield {
99 type: 'done',
100 id: baseId,
101 model: 'stub-llm',
102 timestamp,
103 finishReason: 'stop',
104 }
105 return
106 }
107
108 // Check if this is a follow-up after tool execution
109 const hasToolResults = messages.some((m) => m.role === 'tool')
110
111 if (hasToolResults) {
112 // Check if user approved or denied
113 const lastAssistant = [...messages]
114 .reverse()
115 .find((m) => m.role === 'assistant' && m.toolCalls)
116
117 if (lastAssistant) {
118 // Look for approval in tool results
119 const approvedTools = messages
120 .filter((m) => m.role === 'tool')
121 .filter((m) => {
122 try {
123 const result = JSON.parse(m.content || '{}')
124 return !result.error
125 } catch {
126 return true
127 }
128 })
129
130 const deniedTools = messages
131 .filter((m) => m.role === 'tool')
132 .filter((m) => {
133 try {
134 const result = JSON.parse(m.content || '{}')
135 return result.error?.includes('declined')
136 } catch {
137 return false
138 }
139 })
140
141 let responseText = ''
142 if (approvedTools.length > 0) {
143 responseText = "Good for you! I've processed that request."
144 } else if (deniedTools.length > 0) {
145 responseText = 'Bummer! Maybe another time.'
146 } else {
147 responseText = 'Complete! If you need anything else, feel free to ask.'
148 }
149
150 // Send final response
151 for (const char of responseText) {
152 const accumulated = responseText.substring(
153 0,
154 responseText.indexOf(char) + 1,
155 )
156 yield {
157 type: 'content',
158 id: baseId,
159 model: 'stub-llm',
160 timestamp,
161 delta: char,
162 content: accumulated,
163 role: 'assistant',
164 }
165 }
166
167 yield {
168 type: 'done',
169 id: baseId,
170 model: 'stub-llm',
171 timestamp,
172 finishReason: 'stop',
173 }
174 return
175 }
176 }
177
178 // Detect which tool to call based on user message
179 if (userMessage.includes('preference')) {
180 // Send initial text
181 const initText = 'Let me check your preferences...'
182 for (let i = 0; i < initText.length; i++) {
183 yield {
184 type: 'content',
185 id: baseId,
186 model: 'stub-llm',
187 timestamp,
188 delta: initText[i],
189 content: initText.substring(0, i + 1),
190 role: 'assistant',
191 }
192 }
193
194 // Call getPersonalGuitarPreference
195 yield {
196 type: 'tool_call',
197 id: baseId,
198 model: 'stub-llm',
199 timestamp,
200 toolCall: {
201 id: `call_${Date.now()}`,
202 type: 'function',
203 function: {
204 name: 'getPersonalGuitarPreference',
205 arguments: '{}',
206 },
207 },
208 index: 0,
209 }
210
211 yield {
212 type: 'done',
213 id: baseId,
214 model: 'stub-llm',
215 timestamp,
216 finishReason: 'tool_calls',
217 }
218 return
219 }
220
221 if (userMessage.includes('recommend') || userMessage.includes('acoustic')) {
222 // Send initial text
223 const initText = 'Let me find the perfect guitar for you!'
224 for (let i = 0; i < initText.length; i++) {
225 yield {
226 type: 'content',
227 id: baseId,
228 model: 'stub-llm',
229 timestamp,
230 delta: initText[i],
231 content: initText.substring(0, i + 1),
232 role: 'assistant',
233 }
234 }
235
236 // Call getGuitars then recommendGuitar
237 const getGuitarsId = `call_${Date.now()}_1`
238 yield {
239 type: 'tool_call',
240 id: baseId,
241 model: 'stub-llm',
242 timestamp,
243 toolCall: {
244 id: getGuitarsId,
245 type: 'function',
246 function: {
247 name: 'getGuitars',
248 arguments: '{}',
249 },
250 },
251 index: 0,
252 }
253
254 yield {
255 type: 'done',
256 id: baseId,
257 model: 'stub-llm',
258 timestamp,
259 finishReason: 'tool_calls',
260 }
261
262 // After getGuitars result, call recommendGuitar
263 const recommendId = `call_${Date.now()}_2`
264 yield {
265 type: 'tool_call',
266 id: baseId + '_2',
267 model: 'stub-llm',
268 timestamp: timestamp + 100,
269 toolCall: {
270 id: recommendId,
271 type: 'function',
272 function: {
273 name: 'recommendGuitar',
274 arguments: JSON.stringify({ id: '6' }),
275 },
276 },
277 index: 0,
278 }
279
280 yield {
281 type: 'done',
282 id: baseId + '_2',
283 model: 'stub-llm',
284 timestamp: timestamp + 100,
285 finishReason: 'tool_calls',
286 }
287 return
288 }
289
290 if (userMessage.includes('wish list')) {
291 // Send initial text
292 const initText =
293 "I'll add that to your wish list. Just need your approval first!"
294 for (let i = 0; i < initText.length; i++) {
295 yield {
296 type: 'content',
297 id: baseId,
298 model: 'stub-llm',
299 timestamp,
300 delta: initText[i],
301 content: initText.substring(0, i + 1),
302 role: 'assistant',
303 }
304 }
305
306 // Call addToWishList (needs approval)
307 yield {
308 type: 'tool_call',
309 id: baseId,
310 model: 'stub-llm',
311 timestamp,
312 toolCall: {
313 id: `call_${Date.now()}`,
314 type: 'function',
315 function: {
316 name: 'addToWishList',
317 arguments: JSON.stringify({ guitarId: '6' }),
318 },
319 },
320 index: 0,
321 }
322
323 yield {
324 type: 'done',
325 id: baseId,
326 model: 'stub-llm',
327 timestamp,
328 finishReason: 'tool_calls',
329 }
330 return
331 }
332
333 if (userMessage.includes('cart')) {
334 // Send initial text
335 const initText =
336 "Ready to add to your cart! I'll need your approval to proceed."
337 for (let i = 0; i < initText.length; i++) {
338 yield {
339 type: 'content',
340 id: baseId,
341 model: 'stub-llm',
342 timestamp,
343 delta: initText[i],
344 content: initText.substring(0, i + 1),
345 role: 'assistant',
346 }
347 }
348
349 // Call addToCart (needs approval)
350 yield {
351 type: 'tool_call',
352 id: baseId,
353 model: 'stub-llm',
354 timestamp,
355 toolCall: {
356 id: `call_${Date.now()}`,
357 type: 'function',
358 function: {
359 name: 'addToCart',
360 arguments: JSON.stringify({ guitarId: '6', quantity: 1 }),
361 },
362 },
363 index: 0,
364 }
365
366 yield {
367 type: 'done',
368 id: baseId,
369 model: 'stub-llm',
370 timestamp,
371 finishReason: 'tool_calls',
372 }
373 return
374 }
375
376 // Default response
377 const response =
378 'I can help with guitar preferences, recommendations, wish lists, and cart!'
379 for (const char of response) {
380 const accumulated = response.substring(0, response.indexOf(char) + 1)
381 yield {
382 type: 'content',
383 id: baseId,
384 model: 'stub-llm',
385 timestamp,
386 delta: char,
387 content: accumulated,
388 role: 'assistant',
389 }
390 }
391
392 yield {
393 type: 'done',
394 id: baseId,
395 model: 'stub-llm',
396 timestamp,
397 finishReason: 'stop',
398 }
399}