Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

generic-interrupt-playground.ts

125 lines3.2 KB

1import {
2 defineInterrupt,
3 INTERRUPT_BOUNDARY_PHASES,
4 INTERRUPT_TOOL_RESUMES,
5 toolDefinition,
6} from '@tanstack/ai'
7import { z } from 'zod'
8import type { InterruptBoundaryPhase, InterruptToolResume } from '@tanstack/ai'
9​
10export const reviewPlan = defineInterrupt({
11 id: 'review-plan',
12 payloadSchema: z.object({
13 title: z.string(),
14 boundary: z.enum(INTERRUPT_BOUNDARY_PHASES),
15 }),
16 responseSchema: z.object({
17 approved: z.boolean(),
18 note: z.string().min(1),
19 }),
20})
21​
22export const AUDIENCE_OPTIONS = ['students', 'staff', 'mixed'] as const
23​
24export const chooseAudience = defineInterrupt({
25 id: 'choose-audience',
26 payloadSchema: z.object({
27 question: z.string(),
28 options: z.array(z.enum(AUDIENCE_OPTIONS)),
29 }),
30 responseSchema: z.object({
31 audience: z.enum(AUDIENCE_OPTIONS),
32 }),
33})
34​
35export const playgroundInterrupts = [reviewPlan, chooseAudience] as const
36​
37export const inspectPlan = toolDefinition({
38 name: 'inspectPlan',
39 description: 'Inspect a published plan on the server.',
40 inputSchema: z.object({
41 planId: z.string(),
42 }),
43 outputSchema: z.object({
44 inspected: z.boolean(),
45 planId: z.string(),
46 }),
47})
48​
49export interface PlaygroundScenario {
50 id: string
51 boundary: InterruptBoundaryPhase
52 title: string
53 blurb: string
54 message: string
55 needsTool: boolean
57​
58export const playgroundScenarios: ReadonlyArray<PlaygroundScenario> = [
59 {
60 id: 'before-model',
61 boundary: 'beforeModel',
62 title: 'Before the model',
63 blurb: 'The run pauses before the first model call.',
64 message: 'Plan a one-hour visit for a school group.',
65 needsTool: false,
66 },
67 {
68 id: 'after-model',
69 boundary: 'afterModel',
70 title: 'After the model',
71 blurb: 'The run pauses after the model writes a draft.',
72 message: 'Write a short welcome for new volunteers.',
73 needsTool: false,
74 },
75 {
76 id: 'before-tools',
77 boundary: 'beforeTools',
78 title: 'Before tools',
79 blurb: 'The run pauses after the model asks for a tool, before it runs.',
80 message: 'Inspect plan PLAN-42.',
81 needsTool: true,
82 },
83 {
84 id: 'after-tools',
85 boundary: 'afterTools',
86 title: 'After tools',
87 blurb: 'The run pauses after the tool result is ready.',
88 message: 'Inspect plan PLAN-42 and then summarize it.',
89 needsTool: true,
90 },
92​
93export function isPlaygroundBoundary(
94 value: unknown,
95): value is InterruptBoundaryPhase {
96 return (
97 typeof value === 'string' &&
98 INTERRUPT_BOUNDARY_PHASES.some((boundary) => boundary === value)
99 )
101​
102export function isPlaygroundPolicy(
103 value: unknown,
104): value is InterruptToolResume {
105 return (
106 typeof value === 'string' &&
107 INTERRUPT_TOOL_RESUMES.some((policy) => policy === value)
108 )
110​
111export function readPlaygroundForwarded(value: unknown): {
112 boundary: InterruptBoundaryPhase
113 policy: InterruptToolResume
114} {
115 if (!value || typeof value !== 'object' || Array.isArray(value)) {
116 return { boundary: 'beforeModel', policy: 'continue' }
117 }
118 const record: Record<string, unknown> = { ...value }
119 return {
120 boundary: isPlaygroundBoundary(record.boundary)
121 ? record.boundary
122 : 'beforeModel',
123 policy: isPlaygroundPolicy(record.policy) ? record.policy : 'continue',
124 }