Open History to see the last commit
1import { toolDefinition } from '@tanstack/ai'
2import { z } from 'zod'
3
4/**
5 * Willowbrook Wildlife Sanctuary interrupt playground.
6 *
7 * Each tool isolates one interrupt behavior. Server tools run on the server
8 * after approval; client tools run in the browser after approval. A tool is
9 * "client" purely by having a `.client()` implementation and no `.server()`.
10 */
11
12// 1. Server tool, approval only (boolean approve / reject).
13export const admitRescue = toolDefinition({
14 name: 'admitRescue',
15 description: 'Admit a rescued animal into the sanctuary intake ward.',
16 inputSchema: z.object({
17 species: z.string(),
18 name: z.string(),
19 }),
20 outputSchema: z.object({ intakeId: z.string(), status: z.string() }),
21 needsApproval: true,
22})
23
24// 2. Server tool, approval + one shared payload schema for both branches.
25export const scheduleVetCheck = toolDefinition({
26 name: 'scheduleVetCheck',
27 description: 'Schedule a veterinary check for an animal in care.',
28 inputSchema: z.object({
29 animal: z.string(),
30 urgency: z.enum(['routine', 'urgent']),
31 }),
32 outputSchema: z.object({ visitId: z.string() }),
33 needsApproval: true,
34 approvalSchema: z.object({ note: z.string().min(1) }),
35})
36
37// 3. Server tool, approval + separate approve / reject payload schemas.
38export const finalizeAdoption = toolDefinition({
39 name: 'finalizeAdoption',
40 description: 'Finalize the adoption of an animal to a new home.',
41 inputSchema: z.object({
42 animal: z.string(),
43 adopter: z.string(),
44 }),
45 outputSchema: z.object({ certificateId: z.string() }),
46 needsApproval: true,
47 approvalSchema: {
48 approve: z.object({
49 adopterName: z.string().min(1),
50 homeCheckPassed: z.boolean(),
51 }),
52 reject: z.object({ reason: z.string().min(1) }),
53 },
54})
55
56// 4. Server tool, approval with edited arguments (change the plan before it runs).
57export const assignEnclosure = toolDefinition({
58 name: 'assignEnclosure',
59 description: 'Assign an animal to an enclosure.',
60 inputSchema: z.object({
61 animal: z.string(),
62 enclosure: z.string(),
63 sizeSqm: z.number().positive(),
64 }),
65 outputSchema: z.object({
66 assignmentId: z.string(),
67 enclosure: z.string(),
68 sizeSqm: z.number(),
69 }),
70 needsApproval: true,
71})
72
73// 5. Client tool, approval only. Runs in the browser after approval.
74export const printIntakeTag = toolDefinition({
75 name: 'printIntakeTag',
76 description: "Print an intake tag on this device's label printer.",
77 inputSchema: z.object({ animal: z.string() }),
78 outputSchema: z.object({ tag: z.string() }),
79 needsApproval: true,
80})
81
82// 6. Client tool, approval + shared payload schema.
83export const logFieldSighting = toolDefinition({
84 name: 'logFieldSighting',
85 description: 'Log a field sighting captured from this device.',
86 inputSchema: z.object({ species: z.string(), location: z.string() }),
87 outputSchema: z.object({ sightingId: z.string() }),
88 needsApproval: true,
89 approvalSchema: z.object({ note: z.string().min(1) }),
90})
91
92// 7. Client tool, approval + branch payload schemas.
93export const shareAdoptionStory = toolDefinition({
94 name: 'shareAdoptionStory',
95 description: 'Share an adoption story to a social channel from this device.',
96 inputSchema: z.object({ animal: z.string() }),
97 outputSchema: z.object({ url: z.string() }),
98 needsApproval: true,
99 approvalSchema: {
100 approve: z.object({ channel: z.enum(['instagram', 'newsletter']) }),
101 reject: z.object({ reason: z.string().min(1) }),
102 },
103})
104
105// 8. Client tool, approval with edited arguments.
106export const printCertificate = toolDefinition({
107 name: 'printCertificate',
108 description: 'Print an adoption certificate on this device.',
109 inputSchema: z.object({
110 animal: z.string(),
111 adopter: z.string(),
112 date: z.string(),
113 }),
114 outputSchema: z.object({ certificate: z.string() }),
115 needsApproval: true,
116})
117
118/** The generic interrupt's response schema (a non-tool application pause). */
119export const feedingScheduleResponseSchema = {
120 $schema: 'https://json-schema.org/draft/2020-12/schema',
121 type: 'object',
122 properties: {
123 mealsPerDay: { type: 'integer', minimum: 1, maximum: 6 },
124 diet: { type: 'string', minLength: 2 },
125 },
126 required: ['mealsPerDay', 'diet'],
127 additionalProperties: false,
128} as const
129
130export type ScenarioGroup = 'server' | 'client' | 'generic' | 'batch'
131
132export interface Scenario {
133 id: string
134 group: ScenarioGroup
135 title: string
136 blurb: string
137 /** The message the button sends to the chat. */
138 message: string
139 /** When set, the server forces this tool via provider tool_choice. */
140 forceTool?: string
141 /** When true, the server attaches the generic-interrupt middleware. */
142 generic?: boolean
143}
144
145export const scenarios: ReadonlyArray<Scenario> = [
146 {
147 id: 'admit',
148 group: 'server',
149 title: 'Admit a rescue',
150 blurb: 'Server tool, plain approve / reject.',
151 message: "Admit the rescued red fox named 'Rusty' to intake.",
152 forceTool: admitRescue.name,
153 },
154 {
155 id: 'vet',
156 group: 'server',
157 title: 'Schedule a vet check',
158 blurb: 'Server tool, one shared note on the decision.',
159 message: 'Book an urgent vet check for Rusty.',
160 forceTool: scheduleVetCheck.name,
161 },
162 {
163 id: 'adopt',
164 group: 'server',
165 title: 'Finalize an adoption',
166 blurb: 'Server tool, different approve vs reject payloads.',
167 message: "Finalize Luna the barn owl's adoption by Dana Rivers.",
168 forceTool: finalizeAdoption.name,
169 },
170 {
171 id: 'enclosure',
172 group: 'server',
173 title: 'Assign an enclosure',
174 blurb: 'Server tool, edit the plan before approving.',
175 message: 'Assign Rusty to enclosure Aspen with 18 square metres.',
176 forceTool: assignEnclosure.name,
177 },
178 {
179 id: 'tag',
180 group: 'client',
181 title: 'Print an intake tag',
182 blurb: 'Client tool, plain approve / reject, runs in the browser.',
183 message: 'Print an intake tag for Rusty.',
184 forceTool: printIntakeTag.name,
185 },
186 {
187 id: 'sighting',
188 group: 'client',
189 title: 'Log a field sighting',
190 blurb: 'Client tool, one shared note.',
191 message: 'Log a field sighting of a barn owl near the North Meadow.',
192 forceTool: logFieldSighting.name,
193 },
194 {
195 id: 'story',
196 group: 'client',
197 title: 'Share an adoption story',
198 blurb: 'Client tool, different approve vs reject payloads.',
199 message: "Share Luna the owl's adoption story.",
200 forceTool: shareAdoptionStory.name,
201 },
202 {
203 id: 'certificate',
204 group: 'client',
205 title: 'Print a certificate',
206 blurb: 'Client tool, edit the details before approving.',
207 message:
208 'Print an adoption certificate for Luna, adopted by Dana Rivers today.',
209 forceTool: printCertificate.name,
210 },
211 {
212 id: 'feeding',
213 group: 'generic',
214 title: 'Set a feeding schedule',
215 blurb: 'Generic interrupt, no tool. The app asks and validates.',
216 message: 'Help me set up a feeding schedule for Rusty.',
217 generic: true,
218 },
219 {
220 id: 'batch',
221 group: 'batch',
222 title: 'Admit three rescues',
223 blurb: 'Several approvals at once. Resolve each or resolve all.',
224 message:
225 'Admit three rescued animals to intake: a red fox named Rusty, a barn owl named Luna, and a hedgehog named Pip.',
226 },
227]