-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.ts
More file actions
235 lines (224 loc) · 7.54 KB
/
Copy pathindex.ts
File metadata and controls
235 lines (224 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* Supervisor pattern — a router model dispatches one user ask to one of
* several typed specialist workers, and the parent composes the result.
*
* Shows:
* - `routeRequest`: a structured-output request whose model picks a
* `specialist` ('researcher' | 'coder' | 'writer').
* - a `choice` state dispatching to the chosen specialist request, each
* with its own model ref and distinct system prompt.
* - the parent composing `{ specialist, answer }` as its output.
*
* Dual-mode: `runSupervisorExample(options?)` takes injectable executors
* (the test passes mocks — keyless CI); the direct run below uses real
* models via `createAiSdkExecutors` + `openai('gpt-5.4-mini')`.
*
* Run: OPENAI_API_KEY=... npx tsx examples/supervisor/index.ts
*/
import { z } from "zod";
import { openai } from "@ai-sdk/openai";
import { runAgent, setupAgent, type RunAgentOptions } from "@statelyai/agent";
import { createAiSdkExecutors, defineModels } from "@statelyai/agent/ai-sdk";
const specialistSchema = z.enum(["researcher", "coder", "writer"]);
const routeSchema = z.object({
specialist: specialistSchema,
reason: z.string(),
});
export const models = defineModels({
supervisor: openai("gpt-5.4-mini"),
researcher: openai("gpt-5.4-mini"),
coder: openai("gpt-5.4-mini"),
writer: openai("gpt-5.4-mini"),
});
const agentSetup = setupAgent({
models,
context: z.object({
request: z.string(),
specialist: specialistSchema.nullable(),
reason: z.string().nullable(),
answer: z.string().nullable(),
}),
input: z.object({ request: z.string() }),
output: z.object({
answer: z.string(),
specialist: specialistSchema,
reason: z.string(),
}),
requests: {
routeRequest: {
schemas: {
input: z.object({ request: z.string() }),
output: routeSchema,
},
model: "supervisor",
system:
"You are a supervisor. Route the user request to exactly one specialist: " +
'"researcher" (facts, comparisons, background), "coder" (code, APIs, ' +
'debugging), or "writer" (prose, summaries, messaging).',
prompt: ({ input }) => input.request,
},
// One typed specialist per route, each a distinct model ref + system prompt.
researcher: {
schemas: {
input: z.object({ request: z.string() }),
output: z.string(),
},
model: "researcher",
system: "You are a research specialist. Answer with concise, factual findings.",
prompt: ({ input }) => input.request,
},
coder: {
schemas: {
input: z.object({ request: z.string() }),
output: z.string(),
},
model: "coder",
system:
"You are a coding specialist. Answer with correct, minimal code and a short explanation.",
prompt: ({ input }) => input.request,
},
writer: {
schemas: {
input: z.object({ request: z.string() }),
output: z.string(),
},
model: "writer",
system: "You are a writing specialist. Answer with clear, well-structured prose.",
prompt: ({ input }) => input.request,
},
},
});
export const supervisorSchemas = agentSetup.schemas;
export const supervisorMachine = agentSetup.createMachine({
id: "supervisor",
context: ({ input }) => ({
request: input.request,
specialist: null,
reason: null,
answer: null,
}),
output: ({ context }) => ({
answer: context.answer ?? "",
specialist: context.specialist ?? "researcher",
reason: context.reason ?? "",
}),
initial: "routing",
states: {
routing: {
invoke: {
id: "routeRequest",
src: "routeRequest",
input: ({ context }) => ({ request: context.request }),
onDone: ({ output }) => ({
target: "dispatch",
context: { specialist: output.specialist, reason: output.reason },
}),
// On failure, fall through to done — the root output fills best-effort
// defaults for the unset fields.
onError: { target: "done" },
},
},
dispatch: {
type: "choice",
choice: ({ context }) => ({ target: context.specialist ?? "researcher" }),
},
researcher: {
invoke: {
id: "researcher",
src: "researcher",
input: ({ context }) => ({ request: context.request }),
onDone: ({ output }) => ({
target: "done",
context: { answer: output },
}),
onError: { target: "done" },
},
},
coder: {
invoke: {
id: "coder",
src: "coder",
input: ({ context }) => ({ request: context.request }),
onDone: ({ output }) => ({
target: "done",
context: { answer: output },
}),
onError: { target: "done" },
},
},
writer: {
invoke: {
id: "writer",
src: "writer",
input: ({ context }) => ({ request: context.request }),
onDone: ({ output }) => ({
target: "done",
context: { answer: output },
}),
onError: { target: "done" },
},
},
done: { type: "final" },
},
});
export async function runSupervisorExample(options?: RunAgentOptions<typeof supervisorMachine>) {
const result = await runAgent(supervisorMachine, {
input: {
request: "Write a friendly release announcement for our new SDK.",
},
...(options && Object.keys(options).length > 0
? options
: { executors: createAiSdkExecutors({ models }) }),
});
if (result.status !== "done") {
throw new Error(`Supervisor example did not complete: ${result.status}`);
}
return result.output;
}
// The specialist state names the router can dispatch to — used to narrate which
// branch each request actually flows through.
const SPECIALIST_STATES = ["researcher", "coder", "writer"] as const;
// Run directly (`tsx index.ts`); skipped when a test imports this module.
if (import.meta.url === new URL(process.argv[1]!, "file:").href) {
if (!process.env.OPENAI_API_KEY) {
console.error("Set OPENAI_API_KEY to run this example.");
process.exit(1);
}
void (async () => {
const executors = createAiSdkExecutors({ models });
// Three requests that clearly belong to three different specialists — the
// point is to see the router genuinely dispatch to researcher, coder, AND
// writer, not collapse everything into one branch.
const requests = [
"What year was the TCP protocol first standardized, and by whom?",
"Write a TypeScript function that debounces an async function.",
"Draft a warm two-sentence thank-you note to a conference organizer.",
];
for (const request of requests) {
// Record which specialist state this request transitions through.
const specialistsHit: string[] = [];
const result = await runAgent(supervisorMachine, {
input: { request },
executors,
onTransition: ({ value }) => {
const state = String(value);
if ((SPECIALIST_STATES as readonly string[]).includes(state)) {
specialistsHit.push(state);
}
console.log(` [state] ${state}`);
},
});
if (result.status !== "done") {
throw new Error(`Supervisor example did not complete: ${result.status}`);
}
const { specialist, reason, answer } = result.output;
console.log(`Request: ${request}`);
console.log(`Routed through: ${specialistsHit.join(" → ") || "(none)"}`);
console.log(`Route decision: ${specialist} (${reason})`);
console.log(`Answer: ${answer}\n`);
}
})().catch((error) => {
console.error(error);
process.exitCode = 1;
});
}