@statelyai/agent 2.0.0-alpha.21 → 2.0.0-alpha.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,46 @@
1
1
  import { c as AgentTools, v as StandardSchemaV1 } from "./types-DSdj2tGs.cjs";
2
- import { AnyStateMachine } from "xstate";
2
+ import { AnyActorRef, AnyStateMachine, EventObject, MachineContext, MetaObject, StateMachine, StateSchema, StateValue } from "xstate";
3
3
 
4
+ //#region src/machines/internal.d.ts
5
+ /**
6
+ * The machine type a preset factory returns: its context, event union, input
7
+ * and output are all concrete, so callers get typed `runAgent` input and
8
+ * output instead of `AnyStateMachine`. The remaining slots (children, state
9
+ * value/tags/config, source maps) are left permissive — a preset builds its
10
+ * states dynamically, so there is no literal state schema to infer.
11
+ *
12
+ * @internal
13
+ */
14
+ type PresetMachine<TContext extends MachineContext, TInput, TOutput, TEvent extends EventObject = EventObject> = StateMachine<TContext, TEvent, Record<string, AnyActorRef | undefined>, StateValue, string, TInput, TOutput, EventObject, MetaObject, StateSchema, any, any, any, any>;
15
+ /** One delegated unit of work, lowered to an inline text request. */
16
+ interface PresetRequestEntry {
17
+ /** What this entry is for. Shown to the routing/supervising model. */
18
+ description?: string;
19
+ /** System prompt for this entry's model call. */
20
+ instructions?: string;
21
+ /** Model ref (or `models` alias). Falls back to the factory's `model`. */
22
+ model?: string;
23
+ /** Structured output schema. Omitted means plain text. */
24
+ outputSchema?: StandardSchemaV1;
25
+ /** Tools the host runs inside this one request. */
26
+ tools?: AgentTools;
27
+ /** Bounds the host-side tool loop (lowered to the request's typed `maxSteps`). */
28
+ maxSteps?: number;
29
+ }
30
+ /** One delegated unit of work, lowered to an invoked child machine. */
31
+ interface PresetMachineEntry {
32
+ /** What this entry is for. Shown to the routing/supervising model. */
33
+ description?: string;
34
+ /** The child machine to invoke. Registered as an actor source, so `runAgent` binds its executors. */
35
+ machine: AnyStateMachine;
36
+ /** Builds the child's `input`. Defaults to `{ prompt }` (`{ message }` for handoff). */
37
+ input?: (args: {
38
+ prompt: string;
39
+ }) => unknown;
40
+ }
41
+ /** A route/branch/worker/agent entry: an inline request, or a child machine. */
42
+ type PresetEntry = PresetRequestEntry | PresetMachineEntry;
43
+ //#endregion
4
44
  //#region src/machines/tool-loop.d.ts
5
45
  /** Config for {@link createToolLoopMachine}. */
6
46
  interface CreateToolLoopMachineConfig {
@@ -24,6 +64,16 @@ type ToolLoopContext = {
24
64
  prompt: string;
25
65
  result: unknown;
26
66
  };
67
+ /** Machine input of a {@link createToolLoopMachine} machine. */
68
+ type ToolLoopInput = {
69
+ prompt: string;
70
+ };
71
+ /** Machine output of a {@link createToolLoopMachine} machine. */
72
+ type ToolLoopOutput = {
73
+ result: unknown;
74
+ };
75
+ /** The machine {@link createToolLoopMachine} returns. */
76
+ type ToolLoopMachine = PresetMachine<ToolLoopContext, ToolLoopInput, ToolLoopOutput>;
27
77
  /**
28
78
  * The single-state tool loop: one text request carries the `tools`, and the
29
79
  * host runs the tool loop inside it (`maxSteps` bounds it). Selecting and
@@ -46,7 +96,7 @@ type ToolLoopContext = {
46
96
  * // Snapshots and log entries carry machine.version ("1") automatically.
47
97
  * ```
48
98
  */
49
- declare function createToolLoopMachine(config: CreateToolLoopMachineConfig): AnyStateMachine;
99
+ declare function createToolLoopMachine(config: CreateToolLoopMachineConfig): ToolLoopMachine;
50
100
  //#endregion
51
101
  //#region src/machines/sequential.d.ts
52
102
  /** Arguments a {@link SequentialStep}'s `prompt` receives. */
@@ -88,6 +138,17 @@ type SequentialContext = {
88
138
  results: Record<string, unknown>;
89
139
  previous: unknown;
90
140
  };
141
+ /** Machine input of a {@link createSequentialMachine} machine. */
142
+ type SequentialInput = {
143
+ prompt: string;
144
+ };
145
+ /** Machine output of a {@link createSequentialMachine} machine. */
146
+ type SequentialOutput = {
147
+ results: Record<string, unknown>;
148
+ output: unknown;
149
+ };
150
+ /** The machine {@link createSequentialMachine} returns. */
151
+ type SequentialMachine = PresetMachine<SequentialContext, SequentialInput, SequentialOutput>;
91
152
  /**
92
153
  * A prompt chain: each step is one state, and each step's output feeds the
93
154
  * next. The default prompt for a step is the previous step's output, so a
@@ -106,56 +167,41 @@ type SequentialContext = {
106
167
  * });
107
168
  * ```
108
169
  */
109
- declare function createSequentialMachine(config: CreateSequentialMachineConfig): AnyStateMachine;
110
- //#endregion
111
- //#region src/machines/internal.d.ts
112
- /** One delegated unit of work, lowered to an inline text request. */
113
- interface PresetRequestEntry {
114
- /** What this entry is for. Shown to the routing/supervising model. */
115
- description?: string;
116
- /** System prompt for this entry's model call. */
117
- instructions?: string;
118
- /** Model ref (or `models` alias). Falls back to the factory's `model`. */
119
- model?: string;
120
- /** Structured output schema. Omitted means plain text. */
121
- outputSchema?: StandardSchemaV1;
122
- /** Tools the host runs inside this one request. */
123
- tools?: AgentTools;
124
- /** Bounds the host-side tool loop (lowered to the request's typed `maxSteps`). */
125
- maxSteps?: number;
126
- }
127
- /** One delegated unit of work, lowered to an invoked child machine. */
128
- interface PresetMachineEntry {
129
- /** What this entry is for. Shown to the routing/supervising model. */
130
- description?: string;
131
- /** The child machine to invoke. Registered as an actor source, so `runAgent` binds its executors. */
132
- machine: AnyStateMachine;
133
- /** Builds the child's `input`. Defaults to `{ prompt }` (`{ message }` for handoff). */
134
- input?: (args: {
135
- prompt: string;
136
- }) => unknown;
137
- }
138
- /** A route/branch/worker/agent entry: an inline request, or a child machine. */
139
- type PresetEntry = PresetRequestEntry | PresetMachineEntry;
170
+ declare function createSequentialMachine(config: CreateSequentialMachineConfig): SequentialMachine;
140
171
  //#endregion
141
172
  //#region src/machines/router.d.ts
142
173
  /** Config for {@link createRouterMachine}. */
143
- interface CreateRouterMachineConfig {
174
+ interface CreateRouterMachineConfig<TRoutes extends Record<string, PresetEntry> = Record<string, PresetEntry>> {
144
175
  /** Model ref for the routing decision, and the default for request routes. */
145
176
  model: string;
146
177
  /** System prompt for the routing decision. */
147
178
  instructions?: string;
148
179
  /** The legal destinations, keyed by route name. Each is an inline request or a child machine. */
149
- routes: Record<string, PresetEntry>;
180
+ routes: TRoutes;
150
181
  /** Route taken when the routing decision errors. Without it, a failed decision ends the run. */
151
- fallback?: string;
182
+ fallback?: keyof TRoutes & string;
152
183
  }
153
184
  /** Context of a {@link createRouterMachine} machine. */
154
- type RouterContext = {
185
+ type RouterContext<TRoute extends string = string> = {
155
186
  prompt: string;
156
- route: string | null;
187
+ route: TRoute | null;
157
188
  result: unknown;
158
189
  };
190
+ /** Machine input of a {@link createRouterMachine} machine. */
191
+ type RouterInput = {
192
+ prompt: string;
193
+ };
194
+ /** Machine output of a {@link createRouterMachine} machine. */
195
+ type RouterOutput<TRoute extends string = string> = {
196
+ route: TRoute | null;
197
+ result: unknown;
198
+ };
199
+ /** The events a {@link createRouterMachine} machine accepts: one per declared route. */
200
+ type RouterEvent<TRoute extends string = string> = {
201
+ type: `ROUTE_${TRoute}`;
202
+ };
203
+ /** The machine {@link createRouterMachine} returns. */
204
+ type RouterMachine<TRoute extends string = string> = PresetMachine<RouterContext<TRoute>, RouterInput, RouterOutput<TRoute>, RouterEvent<TRoute>>;
159
205
  /** The event a route decision chooses: `ROUTE_<name>`. */
160
206
  declare function routeEventType(route: string): string;
161
207
  /**
@@ -177,7 +223,7 @@ declare function routeEventType(route: string): string;
177
223
  * });
178
224
  * ```
179
225
  */
180
- declare function createRouterMachine(config: CreateRouterMachineConfig): AnyStateMachine;
226
+ declare function createRouterMachine<const TRoutes extends Record<string, PresetEntry>>(config: CreateRouterMachineConfig<TRoutes>): RouterMachine<keyof TRoutes & string>;
181
227
  //#endregion
182
228
  //#region src/machines/parallel.d.ts
183
229
  /** Config for {@link createParallelMachine}. */
@@ -192,6 +238,16 @@ type ParallelContext = {
192
238
  prompt: string;
193
239
  results: Record<string, unknown>;
194
240
  };
241
+ /** Machine input of a {@link createParallelMachine} machine. */
242
+ type ParallelInput = {
243
+ prompt: string;
244
+ };
245
+ /** Machine output of a {@link createParallelMachine} machine. */
246
+ type ParallelOutput = {
247
+ results: Record<string, unknown>;
248
+ };
249
+ /** The machine {@link createParallelMachine} returns. */
250
+ type ParallelMachine = PresetMachine<ParallelContext, ParallelInput, ParallelOutput>;
195
251
  /**
196
252
  * Static fan-out: every branch runs concurrently as its own region of one
197
253
  * parallel state, and the run joins when all of them finish. Results are keyed
@@ -213,7 +269,7 @@ type ParallelContext = {
213
269
  * });
214
270
  * ```
215
271
  */
216
- declare function createParallelMachine(config: CreateParallelMachineConfig): AnyStateMachine;
272
+ declare function createParallelMachine(config: CreateParallelMachineConfig): ParallelMachine;
217
273
  //#endregion
218
274
  //#region src/machines/loop.d.ts
219
275
  /** The accumulated state a {@link CreateLoopMachineConfig.until} predicate reads. */
@@ -247,6 +303,18 @@ type LoopContext = {
247
303
  results: unknown[];
248
304
  last: unknown;
249
305
  };
306
+ /** Machine input of a {@link createLoopMachine} machine. */
307
+ type LoopInput = {
308
+ prompt: string;
309
+ };
310
+ /** Machine output of a {@link createLoopMachine} machine. */
311
+ type LoopOutput = {
312
+ iterations: number;
313
+ results: unknown[];
314
+ last: unknown;
315
+ };
316
+ /** The machine {@link createLoopMachine} returns. */
317
+ type LoopMachine = PresetMachine<LoopContext, LoopInput, LoopOutput>;
250
318
  /**
251
319
  * A bounded repeat: run the body, check `until` over the accumulated state,
252
320
  * and either stop or go again. `maxTurns` is a guard, so the loop cannot
@@ -263,27 +331,44 @@ type LoopContext = {
263
331
  * });
264
332
  * ```
265
333
  */
266
- declare function createLoopMachine(config: CreateLoopMachineConfig): AnyStateMachine;
334
+ declare function createLoopMachine(config: CreateLoopMachineConfig): LoopMachine;
267
335
  //#endregion
268
336
  //#region src/machines/supervisor.d.ts
269
337
  /** Config for {@link createSupervisorMachine}. */
270
- interface CreateSupervisorMachineConfig {
338
+ interface CreateSupervisorMachineConfig<TWorkers extends Record<string, PresetEntry> = Record<string, PresetEntry>> {
271
339
  /** Model ref for the supervising decision, and the default for request workers. */
272
340
  model: string;
273
341
  /** System prompt for the supervising decision. */
274
342
  instructions?: string;
275
343
  /** The workers the supervisor may delegate to, keyed by name. */
276
- workers: Record<string, PresetEntry>;
344
+ workers: TWorkers;
277
345
  /** Hard upper bound on delegations, enforced by a guard. Default 6. */
278
346
  maxTurns?: number;
279
347
  }
280
348
  /** Context of a {@link createSupervisorMachine} machine. */
281
- type SupervisorContext = {
349
+ type SupervisorContext<TWorker extends string = string> = {
282
350
  task: string;
283
351
  results: Record<string, unknown>;
284
352
  turns: number;
285
- worker: string | null;
353
+ worker: TWorker | null;
354
+ };
355
+ /** Machine input of a {@link createSupervisorMachine} machine. */
356
+ type SupervisorInput = {
357
+ task: string;
286
358
  };
359
+ /** Machine output of a {@link createSupervisorMachine} machine. */
360
+ type SupervisorOutput = {
361
+ results: Record<string, unknown>;
362
+ turns: number;
363
+ };
364
+ /** The events a {@link createSupervisorMachine} machine accepts: one per worker, plus `FINISH`. */
365
+ type SupervisorEvent<TWorker extends string = string> = {
366
+ type: `DELEGATE_${TWorker}`;
367
+ } | {
368
+ type: typeof FINISH_EVENT_TYPE;
369
+ };
370
+ /** The machine {@link createSupervisorMachine} returns. */
371
+ type SupervisorMachine<TWorker extends string = string> = PresetMachine<SupervisorContext<TWorker>, SupervisorInput, SupervisorOutput, SupervisorEvent<TWorker>>;
287
372
  /** The event a supervising decision chooses to delegate to a worker: `DELEGATE_<name>`. */
288
373
  declare function delegateEventType(worker: string): string;
289
374
  /** The event a supervising decision chooses to stop. */
@@ -314,24 +399,36 @@ declare const FINISH_EVENT_TYPE = "FINISH";
314
399
  * });
315
400
  * ```
316
401
  */
317
- declare function createSupervisorMachine(config: CreateSupervisorMachineConfig): AnyStateMachine;
402
+ declare function createSupervisorMachine<const TWorkers extends Record<string, PresetEntry>>(config: CreateSupervisorMachineConfig<TWorkers>): SupervisorMachine<keyof TWorkers & string>;
318
403
  //#endregion
319
404
  //#region src/machines/handoff.d.ts
320
405
  /** Config for {@link createHandoffMachine}. */
321
- interface CreateHandoffMachineConfig {
406
+ interface CreateHandoffMachineConfig<TAgents extends Record<string, PresetEntry> = Record<string, PresetEntry>> {
322
407
  /** The peer agents, keyed by name. Each is an inline request or a child machine. */
323
- agents: Record<string, PresetEntry>;
408
+ agents: TAgents;
324
409
  /** Which agent holds the mic at the start of a run. */
325
- defaultActiveAgent: string;
410
+ defaultActiveAgent: keyof TAgents & string;
326
411
  /** Default model ref for request agents that declare none. */
327
412
  model?: string;
328
413
  }
329
414
  /** Context of a {@link createHandoffMachine} machine. */
330
- type HandoffContext = {
415
+ type HandoffContext<TAgent extends string = string> = {
331
416
  message: string;
332
- activeAgent: string;
417
+ activeAgent: TAgent;
333
418
  reply: unknown;
334
419
  };
420
+ /** Machine input of a {@link createHandoffMachine} machine. */
421
+ type HandoffInput<TAgent extends string = string> = {
422
+ message: string;
423
+ activeAgent?: TAgent;
424
+ };
425
+ /** The events a {@link createHandoffMachine} machine accepts: one per peer agent. */
426
+ type HandoffEvent<TAgent extends string = string> = {
427
+ type: `transfer_to_${TAgent}`;
428
+ message?: string;
429
+ };
430
+ /** The machine {@link createHandoffMachine} returns. There is no final state, so it has no output. */
431
+ type HandoffMachine<TAgent extends string = string> = PresetMachine<HandoffContext<TAgent>, HandoffInput<TAgent>, undefined, HandoffEvent<TAgent>>;
335
432
  /** The event that hands the mic to a peer: `transfer_to_<name>`. */
336
433
  declare function transferEventType(agent: string): string;
337
434
  /**
@@ -364,6 +461,6 @@ declare function transferEventType(agent: string): string;
364
461
  * });
365
462
  * ```
366
463
  */
367
- declare function createHandoffMachine(config: CreateHandoffMachineConfig): AnyStateMachine;
464
+ declare function createHandoffMachine<const TAgents extends Record<string, PresetEntry>>(config: CreateHandoffMachineConfig<TAgents>): HandoffMachine<keyof TAgents & string>;
368
465
  //#endregion
369
- export { type CreateHandoffMachineConfig, type CreateLoopMachineConfig, type CreateParallelMachineConfig, type CreateRouterMachineConfig, type CreateSequentialMachineConfig, type CreateSupervisorMachineConfig, type CreateToolLoopMachineConfig, FINISH_EVENT_TYPE, type HandoffContext, type LoopContext, type LoopState, type ParallelContext, type PresetEntry, type PresetMachineEntry, type PresetRequestEntry, type RouterContext, type SequentialContext, type SequentialPromptArgs, type SequentialStep, type SupervisorContext, type ToolLoopContext, createHandoffMachine, createLoopMachine, createParallelMachine, createRouterMachine, createSequentialMachine, createSupervisorMachine, createToolLoopMachine, delegateEventType, routeEventType, transferEventType };
466
+ export { type CreateHandoffMachineConfig, type CreateLoopMachineConfig, type CreateParallelMachineConfig, type CreateRouterMachineConfig, type CreateSequentialMachineConfig, type CreateSupervisorMachineConfig, type CreateToolLoopMachineConfig, FINISH_EVENT_TYPE, type HandoffContext, type HandoffEvent, type HandoffInput, type HandoffMachine, type LoopContext, type LoopInput, type LoopMachine, type LoopOutput, type LoopState, type ParallelContext, type ParallelInput, type ParallelMachine, type ParallelOutput, type PresetEntry, type PresetMachine, type PresetMachineEntry, type PresetRequestEntry, type RouterContext, type RouterEvent, type RouterInput, type RouterMachine, type RouterOutput, type SequentialContext, type SequentialInput, type SequentialMachine, type SequentialOutput, type SequentialPromptArgs, type SequentialStep, type SupervisorContext, type SupervisorEvent, type SupervisorInput, type SupervisorMachine, type SupervisorOutput, type ToolLoopContext, type ToolLoopInput, type ToolLoopMachine, type ToolLoopOutput, createHandoffMachine, createLoopMachine, createParallelMachine, createRouterMachine, createSequentialMachine, createSupervisorMachine, createToolLoopMachine, delegateEventType, routeEventType, transferEventType };
@@ -1,6 +1,46 @@
1
1
  import { c as AgentTools, v as StandardSchemaV1 } from "./types-CvWRGFxP.mjs";
2
- import { AnyStateMachine } from "xstate";
2
+ import { AnyActorRef, AnyStateMachine, EventObject, MachineContext, MetaObject, StateMachine, StateSchema, StateValue } from "xstate";
3
3
 
4
+ //#region src/machines/internal.d.ts
5
+ /**
6
+ * The machine type a preset factory returns: its context, event union, input
7
+ * and output are all concrete, so callers get typed `runAgent` input and
8
+ * output instead of `AnyStateMachine`. The remaining slots (children, state
9
+ * value/tags/config, source maps) are left permissive — a preset builds its
10
+ * states dynamically, so there is no literal state schema to infer.
11
+ *
12
+ * @internal
13
+ */
14
+ type PresetMachine<TContext extends MachineContext, TInput, TOutput, TEvent extends EventObject = EventObject> = StateMachine<TContext, TEvent, Record<string, AnyActorRef | undefined>, StateValue, string, TInput, TOutput, EventObject, MetaObject, StateSchema, any, any, any, any>;
15
+ /** One delegated unit of work, lowered to an inline text request. */
16
+ interface PresetRequestEntry {
17
+ /** What this entry is for. Shown to the routing/supervising model. */
18
+ description?: string;
19
+ /** System prompt for this entry's model call. */
20
+ instructions?: string;
21
+ /** Model ref (or `models` alias). Falls back to the factory's `model`. */
22
+ model?: string;
23
+ /** Structured output schema. Omitted means plain text. */
24
+ outputSchema?: StandardSchemaV1;
25
+ /** Tools the host runs inside this one request. */
26
+ tools?: AgentTools;
27
+ /** Bounds the host-side tool loop (lowered to the request's typed `maxSteps`). */
28
+ maxSteps?: number;
29
+ }
30
+ /** One delegated unit of work, lowered to an invoked child machine. */
31
+ interface PresetMachineEntry {
32
+ /** What this entry is for. Shown to the routing/supervising model. */
33
+ description?: string;
34
+ /** The child machine to invoke. Registered as an actor source, so `runAgent` binds its executors. */
35
+ machine: AnyStateMachine;
36
+ /** Builds the child's `input`. Defaults to `{ prompt }` (`{ message }` for handoff). */
37
+ input?: (args: {
38
+ prompt: string;
39
+ }) => unknown;
40
+ }
41
+ /** A route/branch/worker/agent entry: an inline request, or a child machine. */
42
+ type PresetEntry = PresetRequestEntry | PresetMachineEntry;
43
+ //#endregion
4
44
  //#region src/machines/tool-loop.d.ts
5
45
  /** Config for {@link createToolLoopMachine}. */
6
46
  interface CreateToolLoopMachineConfig {
@@ -24,6 +64,16 @@ type ToolLoopContext = {
24
64
  prompt: string;
25
65
  result: unknown;
26
66
  };
67
+ /** Machine input of a {@link createToolLoopMachine} machine. */
68
+ type ToolLoopInput = {
69
+ prompt: string;
70
+ };
71
+ /** Machine output of a {@link createToolLoopMachine} machine. */
72
+ type ToolLoopOutput = {
73
+ result: unknown;
74
+ };
75
+ /** The machine {@link createToolLoopMachine} returns. */
76
+ type ToolLoopMachine = PresetMachine<ToolLoopContext, ToolLoopInput, ToolLoopOutput>;
27
77
  /**
28
78
  * The single-state tool loop: one text request carries the `tools`, and the
29
79
  * host runs the tool loop inside it (`maxSteps` bounds it). Selecting and
@@ -46,7 +96,7 @@ type ToolLoopContext = {
46
96
  * // Snapshots and log entries carry machine.version ("1") automatically.
47
97
  * ```
48
98
  */
49
- declare function createToolLoopMachine(config: CreateToolLoopMachineConfig): AnyStateMachine;
99
+ declare function createToolLoopMachine(config: CreateToolLoopMachineConfig): ToolLoopMachine;
50
100
  //#endregion
51
101
  //#region src/machines/sequential.d.ts
52
102
  /** Arguments a {@link SequentialStep}'s `prompt` receives. */
@@ -88,6 +138,17 @@ type SequentialContext = {
88
138
  results: Record<string, unknown>;
89
139
  previous: unknown;
90
140
  };
141
+ /** Machine input of a {@link createSequentialMachine} machine. */
142
+ type SequentialInput = {
143
+ prompt: string;
144
+ };
145
+ /** Machine output of a {@link createSequentialMachine} machine. */
146
+ type SequentialOutput = {
147
+ results: Record<string, unknown>;
148
+ output: unknown;
149
+ };
150
+ /** The machine {@link createSequentialMachine} returns. */
151
+ type SequentialMachine = PresetMachine<SequentialContext, SequentialInput, SequentialOutput>;
91
152
  /**
92
153
  * A prompt chain: each step is one state, and each step's output feeds the
93
154
  * next. The default prompt for a step is the previous step's output, so a
@@ -106,56 +167,41 @@ type SequentialContext = {
106
167
  * });
107
168
  * ```
108
169
  */
109
- declare function createSequentialMachine(config: CreateSequentialMachineConfig): AnyStateMachine;
110
- //#endregion
111
- //#region src/machines/internal.d.ts
112
- /** One delegated unit of work, lowered to an inline text request. */
113
- interface PresetRequestEntry {
114
- /** What this entry is for. Shown to the routing/supervising model. */
115
- description?: string;
116
- /** System prompt for this entry's model call. */
117
- instructions?: string;
118
- /** Model ref (or `models` alias). Falls back to the factory's `model`. */
119
- model?: string;
120
- /** Structured output schema. Omitted means plain text. */
121
- outputSchema?: StandardSchemaV1;
122
- /** Tools the host runs inside this one request. */
123
- tools?: AgentTools;
124
- /** Bounds the host-side tool loop (lowered to the request's typed `maxSteps`). */
125
- maxSteps?: number;
126
- }
127
- /** One delegated unit of work, lowered to an invoked child machine. */
128
- interface PresetMachineEntry {
129
- /** What this entry is for. Shown to the routing/supervising model. */
130
- description?: string;
131
- /** The child machine to invoke. Registered as an actor source, so `runAgent` binds its executors. */
132
- machine: AnyStateMachine;
133
- /** Builds the child's `input`. Defaults to `{ prompt }` (`{ message }` for handoff). */
134
- input?: (args: {
135
- prompt: string;
136
- }) => unknown;
137
- }
138
- /** A route/branch/worker/agent entry: an inline request, or a child machine. */
139
- type PresetEntry = PresetRequestEntry | PresetMachineEntry;
170
+ declare function createSequentialMachine(config: CreateSequentialMachineConfig): SequentialMachine;
140
171
  //#endregion
141
172
  //#region src/machines/router.d.ts
142
173
  /** Config for {@link createRouterMachine}. */
143
- interface CreateRouterMachineConfig {
174
+ interface CreateRouterMachineConfig<TRoutes extends Record<string, PresetEntry> = Record<string, PresetEntry>> {
144
175
  /** Model ref for the routing decision, and the default for request routes. */
145
176
  model: string;
146
177
  /** System prompt for the routing decision. */
147
178
  instructions?: string;
148
179
  /** The legal destinations, keyed by route name. Each is an inline request or a child machine. */
149
- routes: Record<string, PresetEntry>;
180
+ routes: TRoutes;
150
181
  /** Route taken when the routing decision errors. Without it, a failed decision ends the run. */
151
- fallback?: string;
182
+ fallback?: keyof TRoutes & string;
152
183
  }
153
184
  /** Context of a {@link createRouterMachine} machine. */
154
- type RouterContext = {
185
+ type RouterContext<TRoute extends string = string> = {
155
186
  prompt: string;
156
- route: string | null;
187
+ route: TRoute | null;
157
188
  result: unknown;
158
189
  };
190
+ /** Machine input of a {@link createRouterMachine} machine. */
191
+ type RouterInput = {
192
+ prompt: string;
193
+ };
194
+ /** Machine output of a {@link createRouterMachine} machine. */
195
+ type RouterOutput<TRoute extends string = string> = {
196
+ route: TRoute | null;
197
+ result: unknown;
198
+ };
199
+ /** The events a {@link createRouterMachine} machine accepts: one per declared route. */
200
+ type RouterEvent<TRoute extends string = string> = {
201
+ type: `ROUTE_${TRoute}`;
202
+ };
203
+ /** The machine {@link createRouterMachine} returns. */
204
+ type RouterMachine<TRoute extends string = string> = PresetMachine<RouterContext<TRoute>, RouterInput, RouterOutput<TRoute>, RouterEvent<TRoute>>;
159
205
  /** The event a route decision chooses: `ROUTE_<name>`. */
160
206
  declare function routeEventType(route: string): string;
161
207
  /**
@@ -177,7 +223,7 @@ declare function routeEventType(route: string): string;
177
223
  * });
178
224
  * ```
179
225
  */
180
- declare function createRouterMachine(config: CreateRouterMachineConfig): AnyStateMachine;
226
+ declare function createRouterMachine<const TRoutes extends Record<string, PresetEntry>>(config: CreateRouterMachineConfig<TRoutes>): RouterMachine<keyof TRoutes & string>;
181
227
  //#endregion
182
228
  //#region src/machines/parallel.d.ts
183
229
  /** Config for {@link createParallelMachine}. */
@@ -192,6 +238,16 @@ type ParallelContext = {
192
238
  prompt: string;
193
239
  results: Record<string, unknown>;
194
240
  };
241
+ /** Machine input of a {@link createParallelMachine} machine. */
242
+ type ParallelInput = {
243
+ prompt: string;
244
+ };
245
+ /** Machine output of a {@link createParallelMachine} machine. */
246
+ type ParallelOutput = {
247
+ results: Record<string, unknown>;
248
+ };
249
+ /** The machine {@link createParallelMachine} returns. */
250
+ type ParallelMachine = PresetMachine<ParallelContext, ParallelInput, ParallelOutput>;
195
251
  /**
196
252
  * Static fan-out: every branch runs concurrently as its own region of one
197
253
  * parallel state, and the run joins when all of them finish. Results are keyed
@@ -213,7 +269,7 @@ type ParallelContext = {
213
269
  * });
214
270
  * ```
215
271
  */
216
- declare function createParallelMachine(config: CreateParallelMachineConfig): AnyStateMachine;
272
+ declare function createParallelMachine(config: CreateParallelMachineConfig): ParallelMachine;
217
273
  //#endregion
218
274
  //#region src/machines/loop.d.ts
219
275
  /** The accumulated state a {@link CreateLoopMachineConfig.until} predicate reads. */
@@ -247,6 +303,18 @@ type LoopContext = {
247
303
  results: unknown[];
248
304
  last: unknown;
249
305
  };
306
+ /** Machine input of a {@link createLoopMachine} machine. */
307
+ type LoopInput = {
308
+ prompt: string;
309
+ };
310
+ /** Machine output of a {@link createLoopMachine} machine. */
311
+ type LoopOutput = {
312
+ iterations: number;
313
+ results: unknown[];
314
+ last: unknown;
315
+ };
316
+ /** The machine {@link createLoopMachine} returns. */
317
+ type LoopMachine = PresetMachine<LoopContext, LoopInput, LoopOutput>;
250
318
  /**
251
319
  * A bounded repeat: run the body, check `until` over the accumulated state,
252
320
  * and either stop or go again. `maxTurns` is a guard, so the loop cannot
@@ -263,27 +331,44 @@ type LoopContext = {
263
331
  * });
264
332
  * ```
265
333
  */
266
- declare function createLoopMachine(config: CreateLoopMachineConfig): AnyStateMachine;
334
+ declare function createLoopMachine(config: CreateLoopMachineConfig): LoopMachine;
267
335
  //#endregion
268
336
  //#region src/machines/supervisor.d.ts
269
337
  /** Config for {@link createSupervisorMachine}. */
270
- interface CreateSupervisorMachineConfig {
338
+ interface CreateSupervisorMachineConfig<TWorkers extends Record<string, PresetEntry> = Record<string, PresetEntry>> {
271
339
  /** Model ref for the supervising decision, and the default for request workers. */
272
340
  model: string;
273
341
  /** System prompt for the supervising decision. */
274
342
  instructions?: string;
275
343
  /** The workers the supervisor may delegate to, keyed by name. */
276
- workers: Record<string, PresetEntry>;
344
+ workers: TWorkers;
277
345
  /** Hard upper bound on delegations, enforced by a guard. Default 6. */
278
346
  maxTurns?: number;
279
347
  }
280
348
  /** Context of a {@link createSupervisorMachine} machine. */
281
- type SupervisorContext = {
349
+ type SupervisorContext<TWorker extends string = string> = {
282
350
  task: string;
283
351
  results: Record<string, unknown>;
284
352
  turns: number;
285
- worker: string | null;
353
+ worker: TWorker | null;
354
+ };
355
+ /** Machine input of a {@link createSupervisorMachine} machine. */
356
+ type SupervisorInput = {
357
+ task: string;
286
358
  };
359
+ /** Machine output of a {@link createSupervisorMachine} machine. */
360
+ type SupervisorOutput = {
361
+ results: Record<string, unknown>;
362
+ turns: number;
363
+ };
364
+ /** The events a {@link createSupervisorMachine} machine accepts: one per worker, plus `FINISH`. */
365
+ type SupervisorEvent<TWorker extends string = string> = {
366
+ type: `DELEGATE_${TWorker}`;
367
+ } | {
368
+ type: typeof FINISH_EVENT_TYPE;
369
+ };
370
+ /** The machine {@link createSupervisorMachine} returns. */
371
+ type SupervisorMachine<TWorker extends string = string> = PresetMachine<SupervisorContext<TWorker>, SupervisorInput, SupervisorOutput, SupervisorEvent<TWorker>>;
287
372
  /** The event a supervising decision chooses to delegate to a worker: `DELEGATE_<name>`. */
288
373
  declare function delegateEventType(worker: string): string;
289
374
  /** The event a supervising decision chooses to stop. */
@@ -314,24 +399,36 @@ declare const FINISH_EVENT_TYPE = "FINISH";
314
399
  * });
315
400
  * ```
316
401
  */
317
- declare function createSupervisorMachine(config: CreateSupervisorMachineConfig): AnyStateMachine;
402
+ declare function createSupervisorMachine<const TWorkers extends Record<string, PresetEntry>>(config: CreateSupervisorMachineConfig<TWorkers>): SupervisorMachine<keyof TWorkers & string>;
318
403
  //#endregion
319
404
  //#region src/machines/handoff.d.ts
320
405
  /** Config for {@link createHandoffMachine}. */
321
- interface CreateHandoffMachineConfig {
406
+ interface CreateHandoffMachineConfig<TAgents extends Record<string, PresetEntry> = Record<string, PresetEntry>> {
322
407
  /** The peer agents, keyed by name. Each is an inline request or a child machine. */
323
- agents: Record<string, PresetEntry>;
408
+ agents: TAgents;
324
409
  /** Which agent holds the mic at the start of a run. */
325
- defaultActiveAgent: string;
410
+ defaultActiveAgent: keyof TAgents & string;
326
411
  /** Default model ref for request agents that declare none. */
327
412
  model?: string;
328
413
  }
329
414
  /** Context of a {@link createHandoffMachine} machine. */
330
- type HandoffContext = {
415
+ type HandoffContext<TAgent extends string = string> = {
331
416
  message: string;
332
- activeAgent: string;
417
+ activeAgent: TAgent;
333
418
  reply: unknown;
334
419
  };
420
+ /** Machine input of a {@link createHandoffMachine} machine. */
421
+ type HandoffInput<TAgent extends string = string> = {
422
+ message: string;
423
+ activeAgent?: TAgent;
424
+ };
425
+ /** The events a {@link createHandoffMachine} machine accepts: one per peer agent. */
426
+ type HandoffEvent<TAgent extends string = string> = {
427
+ type: `transfer_to_${TAgent}`;
428
+ message?: string;
429
+ };
430
+ /** The machine {@link createHandoffMachine} returns. There is no final state, so it has no output. */
431
+ type HandoffMachine<TAgent extends string = string> = PresetMachine<HandoffContext<TAgent>, HandoffInput<TAgent>, undefined, HandoffEvent<TAgent>>;
335
432
  /** The event that hands the mic to a peer: `transfer_to_<name>`. */
336
433
  declare function transferEventType(agent: string): string;
337
434
  /**
@@ -364,6 +461,6 @@ declare function transferEventType(agent: string): string;
364
461
  * });
365
462
  * ```
366
463
  */
367
- declare function createHandoffMachine(config: CreateHandoffMachineConfig): AnyStateMachine;
464
+ declare function createHandoffMachine<const TAgents extends Record<string, PresetEntry>>(config: CreateHandoffMachineConfig<TAgents>): HandoffMachine<keyof TAgents & string>;
368
465
  //#endregion
369
- export { type CreateHandoffMachineConfig, type CreateLoopMachineConfig, type CreateParallelMachineConfig, type CreateRouterMachineConfig, type CreateSequentialMachineConfig, type CreateSupervisorMachineConfig, type CreateToolLoopMachineConfig, FINISH_EVENT_TYPE, type HandoffContext, type LoopContext, type LoopState, type ParallelContext, type PresetEntry, type PresetMachineEntry, type PresetRequestEntry, type RouterContext, type SequentialContext, type SequentialPromptArgs, type SequentialStep, type SupervisorContext, type ToolLoopContext, createHandoffMachine, createLoopMachine, createParallelMachine, createRouterMachine, createSequentialMachine, createSupervisorMachine, createToolLoopMachine, delegateEventType, routeEventType, transferEventType };
466
+ export { type CreateHandoffMachineConfig, type CreateLoopMachineConfig, type CreateParallelMachineConfig, type CreateRouterMachineConfig, type CreateSequentialMachineConfig, type CreateSupervisorMachineConfig, type CreateToolLoopMachineConfig, FINISH_EVENT_TYPE, type HandoffContext, type HandoffEvent, type HandoffInput, type HandoffMachine, type LoopContext, type LoopInput, type LoopMachine, type LoopOutput, type LoopState, type ParallelContext, type ParallelInput, type ParallelMachine, type ParallelOutput, type PresetEntry, type PresetMachine, type PresetMachineEntry, type PresetRequestEntry, type RouterContext, type RouterEvent, type RouterInput, type RouterMachine, type RouterOutput, type SequentialContext, type SequentialInput, type SequentialMachine, type SequentialOutput, type SequentialPromptArgs, type SequentialStep, type SupervisorContext, type SupervisorEvent, type SupervisorInput, type SupervisorMachine, type SupervisorOutput, type ToolLoopContext, type ToolLoopInput, type ToolLoopMachine, type ToolLoopOutput, createHandoffMachine, createLoopMachine, createParallelMachine, createRouterMachine, createSequentialMachine, createSupervisorMachine, createToolLoopMachine, delegateEventType, routeEventType, transferEventType };
package/dist/machines.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { r as setupAgent } from "./setup-agent-DqqdcdNh.mjs";
1
+ import { r as setupAgent } from "./setup-agent-nSFYrNTP.mjs";
2
2
  //#region src/machines/internal.ts
3
3
  /** The builtin inline text request every preset lowers a request entry to. */
4
4
  const GENERATE_TEXT_SRC = "agent.generateText";
package/dist/otel.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { d as AgentTraceEvent } from "./run-agent-Do094mGu.cjs";
1
+ import { d as AgentTraceEvent } from "./run-agent-Zbkx7XP8.cjs";
2
2
  import { Attributes, Tracer, TracerProvider } from "@opentelemetry/api";
3
3
 
4
4
  //#region src/otel/index.d.ts
package/dist/otel.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { d as AgentTraceEvent } from "./run-agent-DBNI8ETM.mjs";
1
+ import { d as AgentTraceEvent } from "./run-agent-0fpEZ1wJ.mjs";
2
2
  import { Attributes, Tracer, TracerProvider } from "@opentelemetry/api";
3
3
 
4
4
  //#region src/otel/index.d.ts