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