Open History to see the last commit
1import { toolDefinition } from '@tanstack/ai'
2import { z } from 'zod'
3
4export type TestRuntimeContext = {
5 userId: string
6 tenantId: string
7 source: string
8}
9
10/**
11 * Server-side tool definitions (for tools that execute on the server)
12 */
13export const serverTools = {
14 read_server_context: toolDefinition({
15 name: 'read_server_context',
16 description: 'Read the typed runtime context provided by the server',
17 inputSchema: z.object({}),
18 outputSchema: z.object({
19 userId: z.string(),
20 tenantId: z.string(),
21 source: z.string(),
22 }),
23 }).server<TestRuntimeContext>(async (_args, context) => {
24 return {
25 userId: context.context.userId,
26 tenantId: context.context.tenantId,
27 source: context.context.source,
28 }
29 }),
30
31 check_status: toolDefinition({
32 name: 'check_status',
33 description: 'Check system status (no required input)',
34 inputSchema: z.object({
35 component: z.string().optional(),
36 }),
37 }).server(async (args) => {
38 return JSON.stringify({
39 status: 'ok',
40 component: args.component || 'all',
41 timestamp: Date.now(),
42 })
43 }),
44
45 get_weather: toolDefinition({
46 name: 'get_weather',
47 description: 'Get weather for a city',
48 inputSchema: z.object({
49 city: z.string(),
50 }),
51 }).server(async (args) => {
52 return JSON.stringify({
53 city: args.city,
54 temperature: 72,
55 condition: 'sunny',
56 })
57 }),
58
59 fetch_data: toolDefinition({
60 name: 'fetch_data',
61 description: 'Fetch data from a source',
62 inputSchema: z.object({
63 source: z.string(),
64 }),
65 }).server(async (args) => {
66 return JSON.stringify({
67 source: args.source,
68 data: [1, 2, 3, 4, 5],
69 })
70 }),
71
72 get_time: toolDefinition({
73 name: 'get_time',
74 description: 'Get current time in timezone',
75 inputSchema: z.object({
76 timezone: z.string(),
77 }),
78 }).server(async (args) => {
79 return JSON.stringify({
80 timezone: args.timezone,
81 time: '14:30:00',
82 })
83 }),
84
85 delete_file: toolDefinition({
86 name: 'delete_file',
87 description: 'Delete a file (requires approval)',
88 inputSchema: z.object({
89 path: z.string(),
90 }),
91 needsApproval: true,
92 }).server(async (args) => {
93 return JSON.stringify({
94 deleted: true,
95 path: args.path,
96 })
97 }),
98}
99
100/**
101 * Client-side tool definitions (tools that execute on the client)
102 * These use .client() without an execute function - execution happens on client side
103 */
104export const clientToolDefinitions = {
105 read_client_context: toolDefinition({
106 name: 'read_client_context',
107 description: 'Read the typed runtime context provided by the client',
108 inputSchema: z.object({}),
109 outputSchema: z.object({
110 userId: z.string(),
111 tenantId: z.string(),
112 source: z.string(),
113 }),
114 }).client(),
115
116 show_notification: toolDefinition({
117 name: 'show_notification',
118 description: 'Show a notification to the user',
119 inputSchema: z.object({
120 message: z.string(),
121 type: z.enum(['info', 'warning', 'error']),
122 }),
123 }).client(),
124
125 display_chart: toolDefinition({
126 name: 'display_chart',
127 description: 'Display a chart on the screen',
128 inputSchema: z.object({
129 type: z.enum(['bar', 'line', 'pie']),
130 data: z.array(z.number()),
131 }),
132 }).client(),
133}
134
135export const searchInventory = toolDefinition({
136 name: 'search_inventory',
137 description: 'Search the guitar inventory by keyword',
138 inputSchema: z.object({
139 query: z.string(),
140 }),
141 lazy: true,
142}).server(async (args) => {
143 return JSON.stringify({
144 query: args.query,
145 results: [{ id: 1, name: 'Fender Stratocaster', match: 'name' }],
146 })
147})
148
149export const failingTool = toolDefinition({
150 name: 'failing_tool',
151 description: 'A tool that always throws an error',
152 inputSchema: z.object({
153 message: z.string(),
154 }),
155}).server(async (args) => {
156 throw new Error(`Tool execution failed: ${args.message}`)
157})
158
159export const processOrder = toolDefinition({
160 name: 'process_order',
161 description: 'Process a guitar order with progress updates',
162 inputSchema: z.object({
163 guitarId: z.number(),
164 quantity: z.number(),
165 }),
166}).server(async (args, context) => {
167 context?.emitCustomEvent('order:progress', {
168 step: 'validating',
169 message: `Validating order for guitar ${args.guitarId}`,
170 })
171
172 context?.emitCustomEvent('order:progress', {
173 step: 'processing',
174 message: `Processing ${args.quantity} items`,
175 })
176
177 return JSON.stringify({
178 orderId: 'ORD-123',
179 guitarId: args.guitarId,
180 quantity: args.quantity,
181 status: 'completed',
182 })
183})
184
185// Available test scenarios (UI list with id/label/category)
186export const SCENARIO_LIST = [
187 { id: 'text-only', label: 'Text Only (No Tools)', category: 'basic' },
188 { id: 'server-tool-single', label: 'Single Server Tool', category: 'basic' },
189 { id: 'client-tool-single', label: 'Single Client Tool', category: 'basic' },
190 { id: 'approval-tool', label: 'Approval Required Tool', category: 'basic' },
191 {
192 id: 'sequence-server-client',
193 label: 'Server \u2192 Client Sequence',
194 category: 'basic',
195 },
196 { id: 'parallel-tools', label: 'Parallel Tools', category: 'basic' },
197 {
198 id: 'lazy-tool-discovery',
199 label: 'Lazy Tool Discovery',
200 category: 'basic',
201 },
202 { id: 'custom-events', label: 'Custom Event Emitting', category: 'basic' },
203 {
204 id: 'server-context',
205 label: 'Server Runtime Context',
206 category: 'basic',
207 },
208 {
209 id: 'client-context',
210 label: 'Client Runtime Context',
211 category: 'basic',
212 },
213 {
214 id: 'client-server-context',
215 label: 'Forwarded Props to Server Context',
216 category: 'basic',
217 },
218 { id: 'error', label: 'Error Response', category: 'basic' },
219 {
220 id: 'tool-error',
221 label: 'Tool Throws Error',
222 category: 'basic',
223 },
224 {
225 id: 'null-tool-input',
226 label: 'Null Tool Input (Regression #265)',
227 category: 'basic',
228 },
229 // Race condition / event flow scenarios
230 {
231 id: 'sequential-client-tools',
232 label: 'Sequential Client Tools (2)',
233 category: 'race',
234 },
235 {
236 id: 'parallel-client-tools',
237 label: 'Parallel Client Tools',
238 category: 'race',
239 },
240 {
241 id: 'sequential-approvals',
242 label: 'Sequential Approvals (2)',
243 category: 'race',
244 },
245 { id: 'parallel-approvals', label: 'Parallel Approvals', category: 'race' },
246 {
247 id: 'client-then-approval',
248 label: 'Client \u2192 Approval',
249 category: 'race',
250 },
251 {
252 id: 'approval-then-client',
253 label: 'Approval \u2192 Client',
254 category: 'race',
255 },
256 {
257 id: 'server-then-two-clients',
258 label: 'Server \u2192 2 Clients',
259 category: 'race',
260 },
261 {
262 id: 'triple-client-sequence',
263 label: 'Triple Client Sequence',
264 category: 'race',
265 },
266]
267
268/**
269 * Get the tools needed for a specific scenario
270 */
271export function getToolsForScenario(scenario: string) {
272 switch (scenario) {
273 case 'text-only':
274 return []
275
276 case 'server-tool-single':
277 return [serverTools.get_weather]
278
279 case 'client-tool-single':
280 return [clientToolDefinitions.show_notification]
281
282 case 'server-context':
283 return [serverTools.read_server_context]
284
285 case 'client-context':
286 return [clientToolDefinitions.read_client_context]
287
288 case 'client-server-context':
289 return [serverTools.read_server_context]
290
291 case 'approval-tool':
292 return [serverTools.delete_file]
293
294 case 'sequence-server-client':
295 return [serverTools.fetch_data, clientToolDefinitions.display_chart]
296
297 case 'parallel-tools':
298 return [serverTools.get_weather, serverTools.get_time]
299
300 // Race condition / event flow scenarios
301 case 'sequential-client-tools':
302 return [clientToolDefinitions.show_notification]
303
304 case 'parallel-client-tools':
305 return [
306 clientToolDefinitions.show_notification,
307 clientToolDefinitions.display_chart,
308 ]
309
310 case 'sequential-approvals':
311 return [serverTools.delete_file]
312
313 case 'parallel-approvals':
314 return [serverTools.delete_file]
315
316 case 'client-then-approval':
317 return [clientToolDefinitions.show_notification, serverTools.delete_file]
318
319 case 'approval-then-client':
320 return [serverTools.delete_file, clientToolDefinitions.show_notification]
321
322 case 'server-then-two-clients':
323 return [
324 serverTools.fetch_data,
325 clientToolDefinitions.show_notification,
326 clientToolDefinitions.display_chart,
327 ]
328
329 case 'triple-client-sequence':
330 return [
331 clientToolDefinitions.show_notification,
332 clientToolDefinitions.display_chart,
333 ]
334
335 case 'lazy-tool-discovery':
336 return [serverTools.get_weather, searchInventory]
337
338 case 'custom-events':
339 return [processOrder]
340
341 case 'tool-error':
342 return [failingTool]
343
344 case 'null-tool-input':
345 return [serverTools.check_status]
346
347 default:
348 return []
349 }
350}