Open History to see the last commit
1import { toolDefinition } from '@tanstack/ai'
2import { z } from 'zod'
3
4// Common schema for all simulator tools
5const simulatorInputSchema = z.object({
6 text: z
7 .string()
8 .optional()
9 .describe('Optional text to include in the result'),
10 delay: z
11 .number()
12 .optional()
13 .describe('Delay in seconds before returning the result'),
14})
15
16const simulatorOutputSchema = z.object({
17 toolName: z.string(),
18 text: z.string().optional(),
19 delayApplied: z.number().optional(),
20 executedOn: z.enum(['server', 'client']),
21 timestamp: z.number(),
22})
23
24// Helper to create a delayed result
25const createResult = async (
26 toolName: string,
27 executedOn: 'server' | 'client',
28 args: { text?: string; delay?: number },
29) => {
30 const delayMs = args.delay
31 if (delayMs && delayMs > 0) {
32 await new Promise((resolve) => setTimeout(resolve, delayMs * 1000))
33 }
34 return {
35 toolName,
36 text: args.text,
37 delayApplied: args.delay,
38 executedOn,
39 timestamp: Date.now(),
40 }
41}
42
43// ============================================================================
44// 1. Server Tool (executed only on server)
45// ============================================================================
46export const serverToolDef = toolDefinition({
47 name: 'serverTool',
48 description:
49 'A server-side only tool. Execution happens on the server and result is streamed back.',
50 inputSchema: simulatorInputSchema,
51 outputSchema: simulatorOutputSchema,
52})
53
54export const serverTool = serverToolDef.server((args) =>
55 createResult('serverTool', 'server', args),
56)
57
58// ============================================================================
59// 2. Server Tool with Approval (requires user approval, executed on server)
60// ============================================================================
61export const serverToolWithApprovalDef = toolDefinition({
62 name: 'serverToolWithApproval',
63 description:
64 'A server-side tool that requires user approval before execution.',
65 inputSchema: simulatorInputSchema,
66 outputSchema: simulatorOutputSchema,
67 needsApproval: true,
68})
69
70export const serverToolWithApproval = serverToolWithApprovalDef.server((args) =>
71 createResult('serverToolWithApproval', 'server', args),
72)
73
74// ============================================================================
75// 3. Client Tool (executed only on client)
76// ============================================================================
77export const clientToolDef = toolDefinition({
78 name: 'clientTool',
79 description:
80 'A client-side only tool. Execution happens in the browser and result is sent back.',
81 inputSchema: simulatorInputSchema,
82 outputSchema: simulatorOutputSchema,
83})
84
85// Client implementation is provided by the consumer via .client()
86
87// ============================================================================
88// 4. Client Tool with Approval (requires user approval, executed on client)
89// ============================================================================
90export const clientToolWithApprovalDef = toolDefinition({
91 name: 'clientToolWithApproval',
92 description:
93 'A client-side tool that requires user approval before execution.',
94 inputSchema: simulatorInputSchema,
95 outputSchema: simulatorOutputSchema,
96 needsApproval: true,
97})
98
99// Client implementation is provided by the consumer via .client()
100
101// ============================================================================
102// 5. Client/Server Tool (has both server and client implementations)
103// ============================================================================
104export const clientServerToolDef = toolDefinition({
105 name: 'clientServerTool',
106 description:
107 'A hybrid tool with both server and client implementations. Client takes precedence.',
108 inputSchema: simulatorInputSchema,
109 outputSchema: simulatorOutputSchema,
110})
111
112export const clientServerTool = clientServerToolDef.server((args) =>
113 createResult('clientServerTool', 'server', args),
114)
115
116// Client implementation is provided by the consumer via .client()
117
118// ============================================================================
119// 6. Client/Server Tool with Approval (requires approval, has both implementations)
120// ============================================================================
121export const clientServerToolWithApprovalDef = toolDefinition({
122 name: 'clientServerToolWithApproval',
123 description:
124 'A hybrid tool with both server and client implementations that requires approval.',
125 inputSchema: simulatorInputSchema,
126 outputSchema: simulatorOutputSchema,
127 needsApproval: true,
128})
129
130export const clientServerToolWithApproval =
131 clientServerToolWithApprovalDef.server((args) =>
132 createResult('clientServerToolWithApproval', 'server', args),
133 )
134
135// Client implementation is provided by the consumer via .client()
136
137// ============================================================================
138// Exports for convenience
139// ============================================================================
140
141// All tool definitions (for client-side registration)
142export const allToolDefs = [
143 serverToolDef,
144 serverToolWithApprovalDef,
145 clientToolDef,
146 clientToolWithApprovalDef,
147 clientServerToolDef,
148 clientServerToolWithApprovalDef,
149] as const
150
151// Server tools with implementations (for server API)
152export const serverTools = [
153 serverTool,
154 serverToolWithApproval,
155 clientServerTool,
156 clientServerToolWithApproval,
157]
158
159// Helper to create client tool result
160export const createClientResult = (
161 toolName: string,
162 args: { text?: string; delay?: number },
163) => createResult(toolName, 'client', args)