@statelyai/agent 2.0.0-alpha.28 → 2.0.0-alpha.30
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.
- package/dist/ai-sdk.cjs +150 -27
- package/dist/ai-sdk.d.cts +59 -36
- package/dist/ai-sdk.d.mts +59 -36
- package/dist/ai-sdk.mjs +148 -24
- package/dist/{decision-RQ0iv0oZ.mjs → decision-BHkvuImD.mjs} +74 -8
- package/dist/{decision-CmM8-HHz.cjs → decision-CIb7ddIv.cjs} +113 -30
- package/dist/{text-logic-f5F5sCdc.cjs → event-log-BZRfBRd7.cjs} +814 -7
- package/dist/{text-logic-CIqHAmBi.mjs → event-log-DJCMHek_.mjs} +680 -4
- package/dist/{event-log-store-BIPOz-di.d.mts → event-log-store-CIIJohjj.d.mts} +90 -5
- package/dist/{event-log-store-2xzFmgr2.d.cts → event-log-store-CLAC9q2Y.d.cts} +90 -5
- package/dist/event-log-store-DZR4IIWe.cjs +95 -0
- package/dist/event-log-store-DsP6GCRh.mjs +84 -0
- package/dist/index.cjs +576 -58
- package/dist/index.d.cts +104 -510
- package/dist/index.d.mts +104 -510
- package/dist/index.mjs +547 -32
- package/dist/log.cjs +77 -20
- package/dist/log.d.cts +5 -5
- package/dist/log.d.mts +5 -5
- package/dist/log.mjs +58 -3
- package/dist/machines.cjs +1 -1
- package/dist/machines.d.cts +15 -15
- package/dist/machines.d.mts +15 -15
- package/dist/machines.mjs +1 -1
- package/dist/model-source-BCKg4qJ1.cjs +44 -0
- package/dist/model-source-BaHwJ1wK.mjs +39 -0
- package/dist/openai.cjs +11 -11
- package/dist/openai.d.cts +28 -26
- package/dist/openai.d.mts +28 -26
- package/dist/openai.mjs +3 -3
- package/dist/otel.cjs +5 -4
- package/dist/otel.d.cts +5 -7
- package/dist/otel.d.mts +5 -7
- package/dist/otel.mjs +5 -3
- package/dist/{run-agent-xNTxpt82.mjs → run-agent-BpHqkaZw.mjs} +181 -44
- package/dist/{run-agent-COXXc0RW.d.mts → run-agent-CAVt5R-r.d.mts} +95 -30
- package/dist/{run-agent-ByO-Bxyd.d.cts → run-agent-CTlMc3Jb.d.cts} +95 -30
- package/dist/{run-agent-Dmf8GV2d.cjs → run-agent-ctLCvNgL.cjs} +230 -88
- package/dist/{setup-agent-BuanDXeh.mjs → setup-agent-7rV_cujV.mjs} +15 -9
- package/dist/setup-agent-CGELn1qZ.d.mts +577 -0
- package/dist/setup-agent-CLPCkXsu.d.cts +577 -0
- package/dist/{setup-agent-DV4cTou-.cjs → setup-agent-DoUk7EdC.cjs} +31 -20
- package/dist/tanstack-ai.cjs +627 -0
- package/dist/tanstack-ai.d.cts +204 -0
- package/dist/tanstack-ai.d.mts +204 -0
- package/dist/tanstack-ai.mjs +618 -0
- package/dist/testing.cjs +204 -985
- package/dist/testing.d.cts +132 -347
- package/dist/testing.d.mts +132 -347
- package/dist/testing.mjs +191 -973
- package/dist/{text-logic-IjfYy9DP.d.cts → text-logic-BRYOPWvY.d.mts} +52 -12
- package/dist/{text-logic-D-czVPYp.d.mts → text-logic-DmciuVUQ.d.cts} +52 -12
- package/dist/{types-BgNbNKnJ.d.mts → types-Dp5_Ru4R.d.cts} +2 -2
- package/dist/{types-hR5f5YXa.d.cts → types-Dp5_Ru4R.d.mts} +2 -2
- package/dist/usage-BkM3z02Y.d.mts +17 -0
- package/dist/usage-Cm_ovz9V.d.cts +17 -0
- package/dist/{utils-_q7F-AZu.d.mts → utils-BnXdWih3.d.mts} +1 -2
- package/dist/{utils-o4YRdHJX.d.cts → utils-ebVpCYoF.d.cts} +1 -2
- package/dist/validate.cjs +239 -8
- package/dist/validate.d.cts +110 -3
- package/dist/validate.d.mts +110 -3
- package/dist/validate.mjs +238 -7
- package/dist/verify-COFWyprJ.mjs +1181 -0
- package/dist/verify-CVlsTZ-V.cjs +1222 -0
- package/dist/verify-Cn86rT8l.d.mts +414 -0
- package/dist/verify-Dzwd4Mvk.d.cts +414 -0
- package/package.json +44 -31
- package/readme.md +40 -28
- package/skills/add-evals/SKILL.md +129 -0
- package/skills/add-traces/SKILL.md +135 -0
- package/skills/generate-machine/SKILL.md +143 -69
- package/skills/graph-engineering/SKILL.md +33 -0
- package/skills/improve-machine/SKILL.md +241 -0
- package/skills/improve-machine/reference.md +101 -0
- package/skills/loop-to-machine/SKILL.md +183 -0
- package/skills/loop-to-machine/policy.md +158 -0
- package/dist/event-log-store-2pAFiWuc.mjs +0 -576
- package/dist/event-log-store-C-COhIXP.cjs +0 -702
- package/dist/usage-BdEjFuIi.mjs +0 -5
- package/dist/usage-efOQPdf_.cjs +0 -10
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { createMachineFromConfig, setup } from "xstate";
|
|
1
|
+
import { A as createTextLogic, C as AGENT_USAGE_EVENT_TYPE, G as missingActor, H as getAgentExecutionOptions, K as setAgentExecutionOptions, T as DECIDE_ACTOR, k as builtinTextActors, ot as validateSchemaSync } from "./event-log-DJCMHek_.mjs";
|
|
2
|
+
import { r as createDecideActor } from "./decision-BHkvuImD.mjs";
|
|
3
|
+
import { machineFromJSON, setup } from "xstate";
|
|
5
4
|
//#region src/workflow-config.ts
|
|
6
5
|
/**
|
|
7
6
|
* The machines-as-data lowering: `setupAgent(...).fromConfig(config, options)`.
|
|
@@ -285,7 +284,8 @@ function setupAgentFromConfig(config, options) {
|
|
|
285
284
|
syntheticGuards: {},
|
|
286
285
|
transitionTargets: {}
|
|
287
286
|
};
|
|
288
|
-
const
|
|
287
|
+
const json = translateWorkflowConfig(config, translation);
|
|
288
|
+
const machine = machineFromJSON(json, {
|
|
289
289
|
guards: Object.fromEntries(Object.entries({
|
|
290
290
|
...translation.guards,
|
|
291
291
|
...translation.syntheticGuards
|
|
@@ -314,7 +314,12 @@ const emptyEventSchema = { "~standard": {
|
|
|
314
314
|
vendor: "statelyai-agent",
|
|
315
315
|
validate(value) {
|
|
316
316
|
return value !== null && typeof value === "object" && Object.keys(value).length === 0 ? { value: {} } : { issues: [{ message: "Expected an empty event payload" }] };
|
|
317
|
-
}
|
|
317
|
+
},
|
|
318
|
+
jsonSchema: { input: () => ({
|
|
319
|
+
type: "object",
|
|
320
|
+
properties: {},
|
|
321
|
+
additionalProperties: false
|
|
322
|
+
}) }
|
|
318
323
|
} };
|
|
319
324
|
const USAGE_TOKEN_FIELDS = [
|
|
320
325
|
"inputTokens",
|
|
@@ -451,7 +456,7 @@ function setupAgent(config) {
|
|
|
451
456
|
const schemas = normalizeAgentSchemas(config);
|
|
452
457
|
const requestActors = createRequestActors(config.requests ?? {});
|
|
453
458
|
const actors = createAgentActors(config.actors, requestActors);
|
|
454
|
-
const
|
|
459
|
+
const setupConfig = {
|
|
455
460
|
schemas: {
|
|
456
461
|
context: schemas.context,
|
|
457
462
|
events: schemas.events,
|
|
@@ -465,7 +470,8 @@ function setupAgent(config) {
|
|
|
465
470
|
actions: config.actions,
|
|
466
471
|
guards: config.guards,
|
|
467
472
|
delays: config.delays
|
|
468
|
-
}
|
|
473
|
+
};
|
|
474
|
+
const base = setup(setupConfig);
|
|
469
475
|
const createBaseMachine = base.createMachine.bind(base);
|
|
470
476
|
const models = config.models ?? {};
|
|
471
477
|
const machineOptions = {
|
|
@@ -603,4 +609,4 @@ function createAgentActors(actors, requestActors) {
|
|
|
603
609
|
};
|
|
604
610
|
}
|
|
605
611
|
//#endregion
|
|
606
|
-
export { getAgentSchemas as n, setupAgent as r, createAgentSchemas as t };
|
|
612
|
+
export { setupAgentFromConfig as i, getAgentSchemas as n, setupAgent as r, createAgentSchemas as t };
|
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
import { _ as StandardSchemaV1, d as EventUnion, g as NormalizedEventSchemas, h as InferOutput, i as AgentToolChoice, m as InferInput, s as AgentTools, t as AgentEventSchemaInputMap, w as WithAgentInputSchema } from "./types-Dp5_Ru4R.mjs";
|
|
2
|
+
import { J as AgentSchemas, Y as EventFromSnapshot, _ as TextLogic, a as AgentModelRef, d as AgentRequestMode, h as BuiltinAgentActors, i as AgentModelMap, v as TextLogicConfig } from "./text-logic-BRYOPWvY.mjs";
|
|
3
|
+
import { n as AgentUsageEvent, t as AGENT_USAGE_EVENT_TYPE } from "./usage-BkM3z02Y.mjs";
|
|
4
|
+
import { AnyActorLogic, AnyMachineSnapshot, AnySetupConfig, AnyStateMachine, AsyncActorLogic, EventObject, MachineContext, MetaObject, NonReducibleUnknown, SetupReturnFromConfig, SetupStateSchema } from "xstate";
|
|
5
|
+
//#region src/interaction.d.ts
|
|
6
|
+
interface AgentInteractionEvent<TType extends string = string> {
|
|
7
|
+
type: TType;
|
|
8
|
+
label: string;
|
|
9
|
+
style?: string;
|
|
10
|
+
/** Optional fixed fields merged into the event when this choice is selected. */
|
|
11
|
+
event?: Record<string, unknown>;
|
|
12
|
+
}
|
|
13
|
+
/** One payload choice {@link getInteraction} resolved: a label and the full event it sends. */
|
|
14
|
+
interface AgentInteractionChoice<TEvent extends EventObject = EventObject> {
|
|
15
|
+
label: string;
|
|
16
|
+
event: TEvent;
|
|
17
|
+
style?: string;
|
|
18
|
+
}
|
|
19
|
+
interface AgentInteraction<TEvent extends EventObject = EventObject> {
|
|
20
|
+
label: string;
|
|
21
|
+
events: AgentInteractionEvent<TEvent["type"]>[];
|
|
22
|
+
/**
|
|
23
|
+
* Payload choices from `choices`, each a full event the snapshot accepts
|
|
24
|
+
* right now. Present when the state declares `choices`.
|
|
25
|
+
*/
|
|
26
|
+
choices?: AgentInteractionChoice<TEvent>[];
|
|
27
|
+
/** Event type used for a free-text response. */
|
|
28
|
+
textEvent?: TEvent["type"];
|
|
29
|
+
/** The `textEvent` field the text fills, when it is not `text`. */
|
|
30
|
+
textField?: string;
|
|
31
|
+
}
|
|
32
|
+
/** One payload choice as authored in `meta.interaction.choices`, inline or in a context array. */
|
|
33
|
+
interface AgentInteractionChoiceMeta<TEvent extends string = string> {
|
|
34
|
+
/** Button text. `{path.to.field}` interpolates from the context. */
|
|
35
|
+
label: string;
|
|
36
|
+
/** The full event the choice sends. */
|
|
37
|
+
event: {
|
|
38
|
+
type: TEvent;
|
|
39
|
+
[key: string]: unknown;
|
|
40
|
+
};
|
|
41
|
+
style?: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* One choice as authored in `meta.interaction.events`: a bare label, or a
|
|
45
|
+
* descriptor carrying a `style` and the fixed `event` fields merged in when the
|
|
46
|
+
* choice is selected.
|
|
47
|
+
*/
|
|
48
|
+
type AgentInteractionEventMeta = string | {
|
|
49
|
+
label?: string;
|
|
50
|
+
style?: string;
|
|
51
|
+
event?: Record<string, unknown>;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* The `interaction` descriptor {@link getInteraction} reads off a state's meta.
|
|
55
|
+
* `TEvent` is the machine's event-type union, so a choice or `textEvent` that
|
|
56
|
+
* names an event the machine never declared is a compile error.
|
|
57
|
+
*/
|
|
58
|
+
interface AgentInteractionDescriptor<TEvent extends string = string> {
|
|
59
|
+
/** Label text, or a function of the machine context. `{path.to.field}` interpolates. */
|
|
60
|
+
label?: string | ((args: {
|
|
61
|
+
context: any;
|
|
62
|
+
}) => string);
|
|
63
|
+
events?: { [K in TEvent]?: AgentInteractionEventMeta; };
|
|
64
|
+
/**
|
|
65
|
+
* Payload choices: one button per full event, such as one per seat or per
|
|
66
|
+
* amount. A string is a context path to an array of the same shape,
|
|
67
|
+
* computed at runtime.
|
|
68
|
+
*/
|
|
69
|
+
choices?: AgentInteractionChoiceMeta<TEvent>[] | string;
|
|
70
|
+
/**
|
|
71
|
+
* The free-text event. A bare type puts the text in a `text` field;
|
|
72
|
+
* `{ type, field }` names the field.
|
|
73
|
+
*/
|
|
74
|
+
textEvent?: TEvent | {
|
|
75
|
+
type: TEvent;
|
|
76
|
+
field: string;
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The state-meta shape the interaction protocol reads. `setupAgent` types
|
|
81
|
+
* every state's `meta` as this by default, with `TEvent` narrowed to the
|
|
82
|
+
* machine's declared events; see {@link interactionMetaSchema} for the runtime
|
|
83
|
+
* schema when a machine declares its own `meta` schema.
|
|
84
|
+
*/
|
|
85
|
+
interface AgentInteractionMeta<TEvent extends string = string> {
|
|
86
|
+
interaction?: AgentInteractionDescriptor<TEvent>;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* A {@link StandardSchemaV1} for the state metadata {@link getInteraction}
|
|
90
|
+
* reads: an object whose optional `interaction` declares a `label` (a string
|
|
91
|
+
* with `{context.path}` interpolation, or a function of the context), an
|
|
92
|
+
* `events` map of choices, payload `choices` (an array, or a context path to
|
|
93
|
+
* one), and a `textEvent` (a type, or `{ type, field }`) for free-text answers. This is
|
|
94
|
+
* the default `meta` type `setupAgent` gives a machine; pass it to
|
|
95
|
+
* `createAgentSchemas({ meta: interactionMetaSchema })` only when building a
|
|
96
|
+
* schema pack by hand.
|
|
97
|
+
*/
|
|
98
|
+
declare const interactionMetaSchema: StandardSchemaV1<AgentInteractionMeta>;
|
|
99
|
+
/** Options for {@link getInteraction}. */
|
|
100
|
+
interface GetInteractionOptions {
|
|
101
|
+
/**
|
|
102
|
+
* Keep label whitespace exactly as authored. Off by default: labels collapse
|
|
103
|
+
* runs of whitespace to single spaces and trim, so a multi-line template
|
|
104
|
+
* literal renders as one line and interpolated context cannot smuggle in
|
|
105
|
+
* layout. Turn it on when the label is deliberately multi-line (a rendered
|
|
106
|
+
* diff, a preformatted block).
|
|
107
|
+
*/
|
|
108
|
+
preserveWhitespace?: boolean;
|
|
109
|
+
}
|
|
110
|
+
/** Reads the active state's interaction metadata and filters it through the
|
|
111
|
+
* events XState currently accepts. Returns `undefined` when none is declared. */
|
|
112
|
+
declare function getInteraction<TSnapshot extends AnyMachineSnapshot>(snapshot: TSnapshot, options?: GetInteractionOptions): AgentInteraction<EventFromSnapshot<TSnapshot>> | undefined;
|
|
113
|
+
/**
|
|
114
|
+
* Converts a rendered interaction answer back into a schema-validated machine
|
|
115
|
+
* event:
|
|
116
|
+
*
|
|
117
|
+
* - `{ type, ...fields }` — an `events` choice; fixed fields declared in
|
|
118
|
+
* metadata win over user fields.
|
|
119
|
+
* - `{ text }` — free text, placed in the `textEvent` field.
|
|
120
|
+
* - `{ choice }` — a payload choice, by its index in `getInteraction(snapshot).choices`
|
|
121
|
+
* or by its label. A label that several choices share throws; pass the index.
|
|
122
|
+
*/
|
|
123
|
+
declare function eventFromInteraction<TSnapshot extends AnyMachineSnapshot>(snapshot: TSnapshot, choice: ({
|
|
124
|
+
type: string;
|
|
125
|
+
} & Record<string, unknown>) | {
|
|
126
|
+
text: string;
|
|
127
|
+
} | {
|
|
128
|
+
choice: number | string;
|
|
129
|
+
}): EventFromSnapshot<TSnapshot>;
|
|
130
|
+
//#endregion
|
|
131
|
+
//#region src/workflow-config.d.ts
|
|
132
|
+
type JsonSchemaObject = {
|
|
133
|
+
type?: string | string[];
|
|
134
|
+
properties?: Record<string, JsonSchemaObject>;
|
|
135
|
+
required?: string[];
|
|
136
|
+
items?: JsonSchemaObject;
|
|
137
|
+
enum?: unknown[];
|
|
138
|
+
const?: unknown;
|
|
139
|
+
additionalProperties?: unknown;
|
|
140
|
+
[key: string]: unknown;
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Compiles a JSON Schema object (from an `AgentWorkflowConfig`) into a
|
|
144
|
+
* runtime `StandardSchemaV1` validator. `setupAgent.fromConfig(...)` calls
|
|
145
|
+
* this once per schema in the config (context/events/input/output/meta,
|
|
146
|
+
* request input/output) — bring your own engine (Ajv, @cfworker/json-schema,
|
|
147
|
+
* a compiled-Zod-from-JSON-Schema pipeline, ...). Core intentionally ships no
|
|
148
|
+
* JSON Schema engine.
|
|
149
|
+
*/
|
|
150
|
+
type SchemaCompiler = (jsonSchema: Record<string, unknown>, name: string) => StandardSchemaV1;
|
|
151
|
+
/**
|
|
152
|
+
* Serializable JSON/YAML machine definition — the config a database, visual
|
|
153
|
+
* editor, or LLM could produce and hand to `setupAgent.fromConfig(config, {
|
|
154
|
+
* compileSchema })` to get back the same kind of `AnyStateMachine`
|
|
155
|
+
* TypeScript `setupAgent(...)` authoring would build. JS/TS authoring should
|
|
156
|
+
* use `setupAgent(...)` directly instead of this JSON form. Any `unknown`-
|
|
157
|
+
* typed field here (`model`, `guard`, action `params`, …) accepts either a
|
|
158
|
+
* literal JSON value or a `"{{ path.to.value }}"` template-expression string
|
|
159
|
+
* resolved against `{ context, event, input, output }` at machine-build/
|
|
160
|
+
* transition time — see the sibling `evaluateWorkflowConfigValue` lowering.
|
|
161
|
+
*/
|
|
162
|
+
interface AgentWorkflowConfig {
|
|
163
|
+
/** JSON Schema reference (`"$schema"`) editors attach to a config file. Ignored by the lowering. */
|
|
164
|
+
$schema?: string;
|
|
165
|
+
key?: string;
|
|
166
|
+
id?: string;
|
|
167
|
+
version?: string;
|
|
168
|
+
description?: string;
|
|
169
|
+
schemas?: {
|
|
170
|
+
input?: JsonSchemaObject;
|
|
171
|
+
context?: JsonSchemaObject;
|
|
172
|
+
events?: Record<string, JsonSchemaObject>;
|
|
173
|
+
emitted?: Record<string, JsonSchemaObject>;
|
|
174
|
+
output?: JsonSchemaObject;
|
|
175
|
+
meta?: JsonSchemaObject;
|
|
176
|
+
};
|
|
177
|
+
context?: Record<string, unknown>;
|
|
178
|
+
requests?: Record<string, AgentWorkflowRequestConfig>;
|
|
179
|
+
actors?: Record<string, AgentWorkflowActorConfig>;
|
|
180
|
+
initial: string;
|
|
181
|
+
states: Record<string, AgentWorkflowStateConfig>;
|
|
182
|
+
meta?: Record<string, unknown>;
|
|
183
|
+
}
|
|
184
|
+
/** A `requests` entry in {@link AgentWorkflowConfig} — the JSON equivalent of a `setupAgent({ requests })` `TextLogicConfig`. Fields beyond `input`/`output`/`tools`/`mode`/`description` are `unknown` because they accept template-expression strings (see {@link AgentWorkflowConfig}). */
|
|
185
|
+
interface AgentWorkflowRequestConfig {
|
|
186
|
+
mode?: AgentRequestMode;
|
|
187
|
+
description?: string;
|
|
188
|
+
model: unknown;
|
|
189
|
+
system?: unknown;
|
|
190
|
+
prompt?: unknown;
|
|
191
|
+
messages?: unknown;
|
|
192
|
+
input: JsonSchemaObject;
|
|
193
|
+
output: JsonSchemaObject;
|
|
194
|
+
tools?: AgentTools;
|
|
195
|
+
toolChoice?: AgentToolChoice | unknown;
|
|
196
|
+
/** Opt into the provider output's `reasoning` field (see `AgentTextRequest.includeReasoning`). */
|
|
197
|
+
includeReasoning?: boolean;
|
|
198
|
+
temperature?: unknown;
|
|
199
|
+
maxOutputTokens?: unknown;
|
|
200
|
+
topP?: unknown;
|
|
201
|
+
topK?: unknown;
|
|
202
|
+
seed?: unknown;
|
|
203
|
+
stopSequences?: unknown;
|
|
204
|
+
metadata?: unknown;
|
|
205
|
+
}
|
|
206
|
+
/** An `actors` entry in {@link AgentWorkflowConfig} — declares a placeholder actor source (by key) with no host execution wired from JSON; provide it via `machine.provide({ actors })` after `setupAgent.fromConfig(...)`. */
|
|
207
|
+
interface AgentWorkflowActorConfig {
|
|
208
|
+
input?: JsonSchemaObject;
|
|
209
|
+
output?: JsonSchemaObject;
|
|
210
|
+
description?: string;
|
|
211
|
+
}
|
|
212
|
+
/** A `states` entry in {@link AgentWorkflowConfig} — the JSON equivalent of an XState state node config. */
|
|
213
|
+
interface AgentWorkflowStateConfig {
|
|
214
|
+
description?: string;
|
|
215
|
+
type?: "parallel" | "history" | "final" | "choice";
|
|
216
|
+
initial?: string;
|
|
217
|
+
states?: Record<string, AgentWorkflowStateConfig>;
|
|
218
|
+
choice?: AgentWorkflowTransitionConfig | AgentWorkflowTransitionConfig[];
|
|
219
|
+
invoke?: AgentWorkflowInvokeConfig | AgentWorkflowInvokeConfig[];
|
|
220
|
+
on?: Record<string, AgentWorkflowTransitionConfig | AgentWorkflowTransitionConfig[]>;
|
|
221
|
+
always?: AgentWorkflowTransitionConfig | AgentWorkflowTransitionConfig[];
|
|
222
|
+
onDone?: AgentWorkflowTransitionConfig | AgentWorkflowTransitionConfig[];
|
|
223
|
+
after?: Record<string, AgentWorkflowTransitionConfig | AgentWorkflowTransitionConfig[]>;
|
|
224
|
+
entry?: AgentWorkflowActionConfig | AgentWorkflowActionConfig[];
|
|
225
|
+
exit?: AgentWorkflowActionConfig | AgentWorkflowActionConfig[];
|
|
226
|
+
tags?: string[];
|
|
227
|
+
output?: unknown;
|
|
228
|
+
meta?: Record<string, unknown>;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* An `invoke` entry in {@link AgentWorkflowStateConfig}. For `src:
|
|
232
|
+
* 'agent.decide'`, the chosen event is delivered automatically and handled by
|
|
233
|
+
* the state's `on` transitions — a decision has no output of its own, so an
|
|
234
|
+
* `onDone` there is always a config error and is rejected at
|
|
235
|
+
* `setupAgent.fromConfig(...)` time. `onError` handles retries-exhausted.
|
|
236
|
+
*/
|
|
237
|
+
interface AgentWorkflowInvokeConfig {
|
|
238
|
+
id?: string;
|
|
239
|
+
src: string;
|
|
240
|
+
input?: unknown;
|
|
241
|
+
onDone?: AgentWorkflowTransitionConfig | AgentWorkflowTransitionConfig[];
|
|
242
|
+
onError?: AgentWorkflowTransitionConfig | AgentWorkflowTransitionConfig[];
|
|
243
|
+
meta?: Record<string, unknown>;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* A transition target in {@link AgentWorkflowConfig}
|
|
247
|
+
* (`on`/`always`/`onDone`/`after`/invoke `onDone`/`onError`) — the JSON
|
|
248
|
+
* equivalent of an XState transition config. A string `guard` containing
|
|
249
|
+
* `{{ ... }}` is a template expression evaluated as truthy/falsy; any other
|
|
250
|
+
* string is a named guard reference resolved against the `guards` passed to
|
|
251
|
+
* `setupAgent.fromConfig(config, { guards })` (unresolvable references throw
|
|
252
|
+
* at build time).
|
|
253
|
+
*/
|
|
254
|
+
interface AgentWorkflowTransitionConfig {
|
|
255
|
+
target?: string | string[];
|
|
256
|
+
guard?: unknown;
|
|
257
|
+
assign?: Record<string, unknown>;
|
|
258
|
+
actions?: AgentWorkflowActionConfig | AgentWorkflowActionConfig[];
|
|
259
|
+
description?: string;
|
|
260
|
+
reenter?: boolean;
|
|
261
|
+
meta?: Record<string, unknown>;
|
|
262
|
+
}
|
|
263
|
+
/** An `entry`/`exit`/transition `actions` entry in {@link AgentWorkflowConfig} — a bare context `assign`, an `emit`, or a named action `type` (with template-expression `params`) resolved against the `actions` passed to `setupAgent.fromConfig(config, { actions })`. */
|
|
264
|
+
interface AgentWorkflowActionConfig {
|
|
265
|
+
type?: string;
|
|
266
|
+
params?: unknown;
|
|
267
|
+
assign?: Record<string, unknown>;
|
|
268
|
+
emit?: unknown;
|
|
269
|
+
[key: string]: unknown;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* What `setupAgent.fromConfig(...)` returns: the built machine plus the
|
|
273
|
+
* schema pack compiled from the config's `schemas` (with `compileSchema`).
|
|
274
|
+
* The pack is what hosts need for the runtime work the machine's types can't
|
|
275
|
+
* do for a JSON-authored agent — e.g. validating an inbound raw event:
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* ```ts
|
|
279
|
+
* const { machine, schemas } = setupAgent.fromConfig(config, { compileSchema });
|
|
280
|
+
* const event = parseAgentEvent(snapshot, raw, { events: schemas.events });
|
|
281
|
+
* ```
|
|
282
|
+
*/
|
|
283
|
+
interface FromConfigResult {
|
|
284
|
+
machine: AnyStateMachine;
|
|
285
|
+
/** Compiled `context`/`events`/`input`/`output`/`meta`/`emitted` schemas — the same pack `createAgentRuntime` executes this machine with. */
|
|
286
|
+
schemas: AgentSchemaPack<StandardSchemaV1<Record<string, unknown>>, Record<string, StandardSchemaV1>, StandardSchemaV1, StandardSchemaV1, StandardSchemaV1<MetaObject>>;
|
|
287
|
+
}
|
|
288
|
+
/** Options for `setupAgent.fromConfig(...)`. */
|
|
289
|
+
interface FromConfigOptions {
|
|
290
|
+
/**
|
|
291
|
+
* Compile a JSON Schema from the config into a runtime validator. Bring
|
|
292
|
+
* your own engine (Ajv, @cfworker/json-schema, a compiled-Zod-from-JSON-Schema
|
|
293
|
+
* pipeline, ...). Core intentionally ships no JSON Schema engine.
|
|
294
|
+
*/
|
|
295
|
+
compileSchema: SchemaCompiler;
|
|
296
|
+
/**
|
|
297
|
+
* Host guard implementations for named guard references in the config
|
|
298
|
+
* (`guard: "isFromHuman"`). Each is called with `{ context, event }` and
|
|
299
|
+
* returns a boolean. A named guard reference with no implementation here is
|
|
300
|
+
* a build-time error — a guard is never silently dropped.
|
|
301
|
+
*/
|
|
302
|
+
guards?: Record<string, (args: {
|
|
303
|
+
context: any;
|
|
304
|
+
event: any;
|
|
305
|
+
}) => boolean>;
|
|
306
|
+
/**
|
|
307
|
+
* Host action implementations for named action types in the config
|
|
308
|
+
* (`{ type: "notify", params: ... }`). Each is called with the
|
|
309
|
+
* template-resolved `params` as its only argument — pull context/event data
|
|
310
|
+
* into `params` via `{{ ... }}` templates. A named action type with no
|
|
311
|
+
* implementation here is a build-time error.
|
|
312
|
+
*/
|
|
313
|
+
actions?: Record<string, (params: any) => unknown>;
|
|
314
|
+
}
|
|
315
|
+
//#endregion
|
|
316
|
+
//#region src/setup-agent.d.ts
|
|
317
|
+
type Constrain<T, TConstraint> = T extends TConstraint ? T : TConstraint;
|
|
318
|
+
type ContextOf<TContextSchema extends StandardSchemaV1> = Constrain<InferOutput<TContextSchema>, MachineContext>;
|
|
319
|
+
type EventsOf<TEventSchemas extends AgentEventSchemaInputMap> = Constrain<EventUnion<TEventSchemas>, EventObject>;
|
|
320
|
+
/**
|
|
321
|
+
* The first argument a guard or delay source receives. `context` and `event`
|
|
322
|
+
* come from the agent's own schemas, so an inline source is contextually typed
|
|
323
|
+
* and an annotated one stays assignable. XState's remaining fields (`self`,
|
|
324
|
+
* `parent`, `value`, `children`, `stateNode`) are deliberately not modeled:
|
|
325
|
+
* `setupAgent` only forwards these sources to `setup()`, which types them for
|
|
326
|
+
* real where the machine is built.
|
|
327
|
+
*/
|
|
328
|
+
type AgentSourceArgs<TContextSchema extends StandardSchemaV1<Record<string, unknown>>, TEventSchemas extends AgentEventSchemaInputMap> = {
|
|
329
|
+
context: ContextOf<TContextSchema>;
|
|
330
|
+
event: EventsOf<TEventSchemas>;
|
|
331
|
+
};
|
|
332
|
+
/** Guard sources for `setupAgent({ guards })`, typed against the agent's own context and events. */
|
|
333
|
+
type AgentGuardSources<TContextSchema extends StandardSchemaV1<Record<string, unknown>>, TEventSchemas extends AgentEventSchemaInputMap> = Record<string, (args: AgentSourceArgs<TContextSchema, TEventSchemas>, ...params: any[]) => boolean>;
|
|
334
|
+
/** Delay sources for `setupAgent({ delays })`, typed the same way as {@link AgentGuardSources}. */
|
|
335
|
+
type AgentDelaySources<TContextSchema extends StandardSchemaV1<Record<string, unknown>>, TEventSchemas extends AgentEventSchemaInputMap> = Record<string, number | ((args: AgentSourceArgs<TContextSchema, TEventSchemas>) => number)>;
|
|
336
|
+
type SetupActors<TActors extends { [K in keyof TActors]: AnyActorLogic; }> = { [K in keyof TActors]: TActors[K] extends AsyncActorLogic<infer TOutput, infer TInput> ? AsyncActorLogic<TOutput, TInput> : TActors[K]; };
|
|
337
|
+
type AgentSetupActors<TActors extends { [K in keyof TActors]: AnyActorLogic; }, TEvent extends string = string, TModel extends string = string> = TActors & BuiltinAgentActors<TEvent, TModel>;
|
|
338
|
+
/**
|
|
339
|
+
* The default `meta` type of an agent machine: the interaction descriptor
|
|
340
|
+
* `getInteraction` reads, keyed by the machine's own event types (the
|
|
341
|
+
* reserved `@agent.*` events excluded). A machine that declares `meta`
|
|
342
|
+
* replaces this default.
|
|
343
|
+
*/
|
|
344
|
+
type AgentDefaultMeta<TEventSchemas extends AgentEventSchemaInputMap> = AgentInteractionMeta<Exclude<keyof TEventSchemas & string, typeof AGENT_USAGE_EVENT_TYPE>>;
|
|
345
|
+
/**
|
|
346
|
+
* A machine's full schema set — context, event payloads, machine input/
|
|
347
|
+
* output, and state/transition meta — as returned by {@link createAgentSchemas}
|
|
348
|
+
* and retained on `setupAgent(...)`'s `result.schemas` for runtime
|
|
349
|
+
* validation (e.g. by `getAcceptedEvents` to attach event payload schemas). Unlike
|
|
350
|
+
* `AgentSchemaConfig` (the input to `createAgentSchemas`), every field here
|
|
351
|
+
* is required — `events`/`input`/`output`/`meta` default to empty/unknown
|
|
352
|
+
* schemas when not supplied.
|
|
353
|
+
*/
|
|
354
|
+
interface AgentSchemaPack<TContextSchema extends StandardSchemaV1<Record<string, unknown>> = StandardSchemaV1<Record<string, unknown>>, TEventSchemas extends AgentEventSchemaInputMap = AgentEventSchemaInputMap, TInputSchema extends StandardSchemaV1 = StandardSchemaV1<NonReducibleUnknown>, TOutputSchema extends StandardSchemaV1 = StandardSchemaV1<NonReducibleUnknown>, TMetaSchema extends StandardSchemaV1 = StandardSchemaV1<AgentDefaultMeta<TEventSchemas>>, TEmittedSchemas extends Record<string, StandardSchemaV1> = Record<string, StandardSchemaV1>> {
|
|
355
|
+
context: TContextSchema;
|
|
356
|
+
events: NormalizedEventSchemas<TEventSchemas>;
|
|
357
|
+
input: TInputSchema;
|
|
358
|
+
output: TOutputSchema;
|
|
359
|
+
meta: TMetaSchema;
|
|
360
|
+
/** Schemas for events the machine emits (`enq.emit(...)`), keyed by event type — they type `enq.emit` in the machine and the `on` handlers of {@link createAgentRuntime}. Optional: omitted means emitted events stay untyped. */
|
|
361
|
+
emitted?: TEmittedSchemas;
|
|
362
|
+
}
|
|
363
|
+
type AgentSchemaConfig<TContextSchema extends StandardSchemaV1<Record<string, unknown>>, TEventSchemas extends AgentEventSchemaInputMap, TInputSchema extends StandardSchemaV1, TOutputSchema extends StandardSchemaV1, TMetaSchema extends StandardSchemaV1, TEmittedSchemas extends Record<string, StandardSchemaV1> = Record<string, StandardSchemaV1>> = {
|
|
364
|
+
context: TContextSchema;
|
|
365
|
+
events?: TEventSchemas;
|
|
366
|
+
input?: TInputSchema;
|
|
367
|
+
output?: TOutputSchema;
|
|
368
|
+
meta?: TMetaSchema;
|
|
369
|
+
emitted?: TEmittedSchemas;
|
|
370
|
+
};
|
|
371
|
+
/**
|
|
372
|
+
* The payload of the reserved `'@agent.usage'` event — {@link AgentUsageEvent}
|
|
373
|
+
* without its `type`. It is what a machine's `'@agent.usage'` handler receives
|
|
374
|
+
* alongside `type`, since event schemas describe payloads only.
|
|
375
|
+
*/
|
|
376
|
+
type AgentUsageEventPayload = Omit<AgentUsageEvent, "type">;
|
|
377
|
+
/**
|
|
378
|
+
* An authored event schema map with the reserved `'@agent.usage'` entry added —
|
|
379
|
+
* every agent machine's event union includes it, so `on: { '@agent.usage': … }`
|
|
380
|
+
* is typed (and autocompletes) without the machine declaring anything.
|
|
381
|
+
* Idempotent: re-applying it to an already-registered map is a no-op.
|
|
382
|
+
*/
|
|
383
|
+
type WithAgentEvents<T extends AgentEventSchemaInputMap> = Omit<T, typeof AGENT_USAGE_EVENT_TYPE> & {
|
|
384
|
+
[AGENT_USAGE_EVENT_TYPE]: StandardSchemaV1<AgentUsageEventPayload>;
|
|
385
|
+
};
|
|
386
|
+
/** @deprecated Use {@link WithAgentEvents}. */
|
|
387
|
+
type WithAgentUsageEvent<T extends AgentEventSchemaInputMap> = WithAgentEvents<T>;
|
|
388
|
+
/**
|
|
389
|
+
* Builds a machine's {@link AgentSchemaPack} from a partial schema
|
|
390
|
+
* declaration — only `context` is required; `events`/`input`/`output`/`meta`
|
|
391
|
+
* default to empty/unknown schemas when omitted. Pass the result as
|
|
392
|
+
* `setupAgent({ schemas })`'s `schemas` (or spread the individual fields
|
|
393
|
+
* directly into `setupAgent({ context, events, ... })` — both forms are
|
|
394
|
+
* accepted).
|
|
395
|
+
*/
|
|
396
|
+
declare function createAgentSchemas<TContextSchema extends StandardSchemaV1<Record<string, unknown>>, TEventSchemas extends AgentEventSchemaInputMap = {}, TInputSchema extends StandardSchemaV1 = StandardSchemaV1<NonReducibleUnknown>, TOutputSchema extends StandardSchemaV1 = StandardSchemaV1<NonReducibleUnknown>, TMetaSchema extends StandardSchemaV1 = StandardSchemaV1<AgentDefaultMeta<TEventSchemas>>, TEmittedSchemas extends Record<string, StandardSchemaV1> = {}>(schemas: AgentSchemaConfig<TContextSchema, TEventSchemas, TInputSchema, TOutputSchema, TMetaSchema, TEmittedSchemas>): AgentSchemaPack<TContextSchema, WithAgentUsageEvent<TEventSchemas>, TInputSchema, TOutputSchema, TMetaSchema, TEmittedSchemas>;
|
|
397
|
+
type AgentRequestSchemaMap = Record<string, {
|
|
398
|
+
input?: StandardSchemaV1;
|
|
399
|
+
output?: StandardSchemaV1;
|
|
400
|
+
}>;
|
|
401
|
+
type ResolvedRequestSchema<TSchema, TFallback> = [NonNullable<TSchema>] extends [never] ? TFallback : [NonNullable<TSchema>] extends [StandardSchemaV1] ? NonNullable<TSchema> : TFallback;
|
|
402
|
+
type RequestInputSchema<TSchemas extends {
|
|
403
|
+
input?: StandardSchemaV1;
|
|
404
|
+
}> = ResolvedRequestSchema<TSchemas["input"], StandardSchemaV1<undefined>>;
|
|
405
|
+
type RequestOutputSchema<TSchemas extends {
|
|
406
|
+
output?: StandardSchemaV1;
|
|
407
|
+
}> = ResolvedRequestSchema<TSchemas["output"], StandardSchemaV1<string>>;
|
|
408
|
+
type AgentRequestInput<TRequestSchemas extends AgentRequestSchemaMap, TModel extends string = string> = { [K in keyof TRequestSchemas]: Omit<TextLogicConfig<RequestInputSchema<TRequestSchemas[K]>, RequestOutputSchema<TRequestSchemas[K]>, Record<string, unknown>, TModel>, "schemas"> & {
|
|
409
|
+
schemas: TRequestSchemas[K];
|
|
410
|
+
}; };
|
|
411
|
+
type RequestActors<TRequestSchemas extends AgentRequestSchemaMap> = { [K in keyof TRequestSchemas]: TextLogic<RequestInputSchema<TRequestSchemas[K]>, RequestOutputSchema<TRequestSchemas[K]>>; };
|
|
412
|
+
type AgentAllActors<TActors extends { [K in keyof TActors]: AnyActorLogic; }, TRequestSchemas extends AgentRequestSchemaMap> = TActors & RequestActors<TRequestSchemas>;
|
|
413
|
+
type AgentSetupEventsSchema<TEventSchemas extends AgentEventSchemaInputMap> = {
|
|
414
|
+
events: NormalizedEventSchemas<WithAgentUsageEvent<TEventSchemas>>;
|
|
415
|
+
};
|
|
416
|
+
type AgentSetupEmittedSchema<TEmittedSchemas extends Record<string, StandardSchemaV1>> = [keyof TEmittedSchemas] extends [never] ? {} : {
|
|
417
|
+
emitted: TEmittedSchemas;
|
|
418
|
+
};
|
|
419
|
+
/**
|
|
420
|
+
* The input schema as handed to xstate's `setup(...)`, with the machine's input
|
|
421
|
+
* type branded by the schema it came from.
|
|
422
|
+
*
|
|
423
|
+
* XState resolves `schemas.input` to a single type used both by
|
|
424
|
+
* `createActor`'s `input` option and by the `context: ({ input })` factory —
|
|
425
|
+
* and it never validates, so a schema default reads as a required field at the
|
|
426
|
+
* call site while being absent at runtime. The runtime validates the input
|
|
427
|
+
* (filling defaults) and reads this brand back through `AgentInputFrom` to
|
|
428
|
+
* accept the schema's looser *input* side, while the factory keeps seeing the
|
|
429
|
+
* validated *output* side.
|
|
430
|
+
*
|
|
431
|
+
* Only object-shaped input is branded: with no declared input schema the
|
|
432
|
+
* resolved type is xstate's `NonReducibleUnknown` (a union including `null`),
|
|
433
|
+
* and intersecting a brand into that collapses members to `never`.
|
|
434
|
+
*/
|
|
435
|
+
type BrandedInputSchema<TInputSchema extends StandardSchemaV1> = InferOutput<TInputSchema> extends Record<string, unknown> ? StandardSchemaV1<InferInput<TInputSchema>, InferOutput<TInputSchema> & WithAgentInputSchema<TInputSchema>> : TInputSchema;
|
|
436
|
+
type AgentSetupXStateConfig<TContextSchema extends StandardSchemaV1<Record<string, unknown>>, TEventSchemas extends AgentEventSchemaInputMap, TActors extends { [K in keyof TActors]: AnyActorLogic; }, TRequestSchemas extends AgentRequestSchemaMap, TInputSchema extends StandardSchemaV1, TOutputSchema extends StandardSchemaV1, TMetaSchema extends StandardSchemaV1, TModels extends AgentModelMap, TEmittedSchemas extends Record<string, StandardSchemaV1> = {}, TStateSchemas extends Record<string, SetupStateSchema> = Record<string, SetupStateSchema>> = {
|
|
437
|
+
schemas: {
|
|
438
|
+
context: TContextSchema;
|
|
439
|
+
input: BrandedInputSchema<TInputSchema>;
|
|
440
|
+
output: TOutputSchema;
|
|
441
|
+
meta: TMetaSchema;
|
|
442
|
+
} & AgentSetupEventsSchema<TEventSchemas> & AgentSetupEmittedSchema<TEmittedSchemas>;
|
|
443
|
+
states?: TStateSchemas;
|
|
444
|
+
actors: SetupActors<AgentSetupActors<AgentAllActors<TActors, TRequestSchemas>, Exclude<keyof TEventSchemas & string, typeof AGENT_USAGE_EVENT_TYPE>, AgentModelRef<TModels>>>;
|
|
445
|
+
actions?: NonNullable<AnySetupConfig["actions"]>;
|
|
446
|
+
guards?: NonNullable<AnySetupConfig["guards"]>;
|
|
447
|
+
delays?: NonNullable<AnySetupConfig["delays"]>;
|
|
448
|
+
};
|
|
449
|
+
type SetupAgentBaseConfig<TContextSchema extends StandardSchemaV1<Record<string, unknown>>, TEventSchemas extends AgentEventSchemaInputMap, TActors extends { [K in keyof TActors]: AnyActorLogic; }, TInputSchema extends StandardSchemaV1, TOutputSchema extends StandardSchemaV1, TMetaSchema extends StandardSchemaV1, TRequestSchemas extends AgentRequestSchemaMap, TModels extends AgentModelMap, TEmittedSchemas extends Record<string, StandardSchemaV1> = {}, TStateSchemas extends Record<string, SetupStateSchema> = Record<string, SetupStateSchema>> = ({
|
|
450
|
+
schemas: AgentSchemaPack<TContextSchema, TEventSchemas, TInputSchema, TOutputSchema, TMetaSchema, TEmittedSchemas>;
|
|
451
|
+
} | AgentSchemaConfig<TContextSchema, TEventSchemas, TInputSchema, TOutputSchema, TMetaSchema, TEmittedSchemas>) & {
|
|
452
|
+
models?: TModels;
|
|
453
|
+
actors?: TActors;
|
|
454
|
+
/**
|
|
455
|
+
* Per-state schemas, mirroring xstate's `setup({ states })`: narrow
|
|
456
|
+
* `context` inside a state (invoke `input`, transition fns, final `output`)
|
|
457
|
+
* — e.g. mark a field non-null in states only reachable after it is set.
|
|
458
|
+
* Use xstate's native `{ schemas: { context } }` form with a complete
|
|
459
|
+
* context schema.
|
|
460
|
+
*/
|
|
461
|
+
states?: TStateSchemas;
|
|
462
|
+
requests?: AgentRequestInput<TRequestSchemas, AgentModelRef<TModels>>;
|
|
463
|
+
actions?: NonNullable<AnySetupConfig["actions"]>;
|
|
464
|
+
guards?: AgentGuardSources<TContextSchema, TEventSchemas>;
|
|
465
|
+
delays?: AgentDelaySources<TContextSchema, TEventSchemas>;
|
|
466
|
+
};
|
|
467
|
+
type SetupAgentXStateResult<TContextSchema extends StandardSchemaV1<Record<string, unknown>>, TEventSchemas extends AgentEventSchemaInputMap, TActors extends { [K in keyof TActors]: AnyActorLogic; }, TRequestSchemas extends AgentRequestSchemaMap, TInputSchema extends StandardSchemaV1, TOutputSchema extends StandardSchemaV1, TMetaSchema extends StandardSchemaV1, TModels extends AgentModelMap, TEmittedSchemas extends Record<string, StandardSchemaV1> = {}, TStateSchemas extends Record<string, SetupStateSchema> = Record<string, SetupStateSchema>> = SetupReturnFromConfig<AgentSetupXStateConfig<TContextSchema, TEventSchemas, TActors, TRequestSchemas, TInputSchema, TOutputSchema, TMetaSchema, TModels, TEmittedSchemas, TStateSchemas>>;
|
|
468
|
+
/**
|
|
469
|
+
* The object returned by {@link setupAgent}: an xstate `setup(...)` result
|
|
470
|
+
* (`createMachine`, `assign`, …) extended with `schemas` (the resolved
|
|
471
|
+
* {@link AgentSchemaPack}), `models`, and `requests` (the built request
|
|
472
|
+
* actors). Machines created here are registered so
|
|
473
|
+
* `createAgentRuntime` can resolve their schemas/actors
|
|
474
|
+
* without re-passing them each call.
|
|
475
|
+
*/
|
|
476
|
+
type SetupAgentResult<TContextSchema extends StandardSchemaV1<Record<string, unknown>>, TEventSchemas extends AgentEventSchemaInputMap, TActors extends { [K in keyof TActors]: AnyActorLogic; }, TRequestSchemas extends AgentRequestSchemaMap, TInputSchema extends StandardSchemaV1, TOutputSchema extends StandardSchemaV1, TMetaSchema extends StandardSchemaV1, TModels extends AgentModelMap, TEmittedSchemas extends Record<string, StandardSchemaV1> = {}, TStateSchemas extends Record<string, SetupStateSchema> = Record<string, SetupStateSchema>> = Omit<SetupAgentXStateResult<TContextSchema, TEventSchemas, TActors, TRequestSchemas, TInputSchema, TOutputSchema, TMetaSchema, TModels, TEmittedSchemas, TStateSchemas>, "createMachine"> & {
|
|
477
|
+
/**
|
|
478
|
+
* Creates the agent machine — XState's own `createMachine`, plus: the
|
|
479
|
+
* machine is registered so {@link createAgentRuntime} can resolve
|
|
480
|
+
* its schemas/actors without re-passing them, and a single final state's
|
|
481
|
+
* `output` is copied to the machine root when the root declares none.
|
|
482
|
+
*/
|
|
483
|
+
createMachine: SetupAgentXStateResult<TContextSchema, TEventSchemas, TActors, TRequestSchemas, TInputSchema, TOutputSchema, TMetaSchema, TModels, TEmittedSchemas, TStateSchemas>["createMachine"];
|
|
484
|
+
/** The retained schema pack ({@link AgentSchemaPack}) for host-side validation and tooling. Its `events` include the reserved `'@agent.usage'` entry setupAgent registers by default. */
|
|
485
|
+
schemas: AgentSchemaPack<TContextSchema, WithAgentUsageEvent<TEventSchemas>, TInputSchema, TOutputSchema, TMetaSchema, TEmittedSchemas>;
|
|
486
|
+
/** The `models` registry passed to `setupAgent(...)`, if any (used to type-narrow `AgentModelRef`). */
|
|
487
|
+
readonly models: TModels;
|
|
488
|
+
/** The {@link TextLogic} actors built from `setupAgent({ requests })`, keyed the same way. */
|
|
489
|
+
readonly requests: RequestActors<TRequestSchemas>;
|
|
490
|
+
};
|
|
491
|
+
/**
|
|
492
|
+
* Schema-first `setup(...)` for agent machines — the standard entry point
|
|
493
|
+
* for authoring a machine (the blueprint) that this library then runs (via
|
|
494
|
+
* {@link createAgentRuntime}) against host-supplied model/decision
|
|
495
|
+
* executors. Context, events, machine input, machine output, and
|
|
496
|
+
* state/transition meta are all standard schemas — no `{} as Type` casts —
|
|
497
|
+
* and are retained on `result.schemas` for runtime validation. Also
|
|
498
|
+
* registers the `agent.generateText`/`agent.streamText`/
|
|
499
|
+
* `agent.decide` builtin actors and lowers `requests`/`actors` into the
|
|
500
|
+
* machine's actor sources. The result is the xstate `setup(...)` object with
|
|
501
|
+
* a wrapped `result.createMachine(...)` plus `result.schemas`/`models`/
|
|
502
|
+
* `requests` attached. Also has a
|
|
503
|
+
* `setupAgent.fromConfig(...)` namespace member for building a machine from
|
|
504
|
+
* a serializable {@link AgentWorkflowConfig} instead of this TS API.
|
|
505
|
+
*
|
|
506
|
+
* @example
|
|
507
|
+
* ```ts
|
|
508
|
+
* const schemas = createAgentSchemas({
|
|
509
|
+
* context: z.object({ topic: z.string(), joke: z.string().nullable() }),
|
|
510
|
+
* input: z.object({ topic: z.string() }),
|
|
511
|
+
* output: z.object({ joke: z.string() }),
|
|
512
|
+
* });
|
|
513
|
+
*
|
|
514
|
+
* const agent = setupAgent({
|
|
515
|
+
* schemas,
|
|
516
|
+
* actors: { tellJoke },
|
|
517
|
+
* });
|
|
518
|
+
*
|
|
519
|
+
* const jokeMachine = agent.createMachine({
|
|
520
|
+
* context: ({ input }) => ({ topic: input.topic, joke: null }),
|
|
521
|
+
* initial: 'telling',
|
|
522
|
+
* states: {
|
|
523
|
+
* telling: {
|
|
524
|
+
* invoke: {
|
|
525
|
+
* id: 'joke',
|
|
526
|
+
* src: 'tellJoke',
|
|
527
|
+
* input: ({ context }) => ({ topic: context.topic }),
|
|
528
|
+
* onDone: ({ output }) => ({ target: 'done', context: { joke: output } }),
|
|
529
|
+
* },
|
|
530
|
+
* },
|
|
531
|
+
* done: { type: 'final', output: ({ context }) => ({ joke: context.joke ?? '' }) },
|
|
532
|
+
* },
|
|
533
|
+
* });
|
|
534
|
+
* ```
|
|
535
|
+
*/
|
|
536
|
+
declare function setupAgent<TContextSchema extends StandardSchemaV1<Record<string, unknown>>, TEventSchemas extends AgentEventSchemaInputMap, TActors extends { [K in keyof TActors]: AnyActorLogic; }, TRequestSchemas extends AgentRequestSchemaMap = {}, TInputSchema extends StandardSchemaV1 = StandardSchemaV1<NonReducibleUnknown>, TOutputSchema extends StandardSchemaV1 = StandardSchemaV1<NonReducibleUnknown>, TMetaSchema extends StandardSchemaV1 = StandardSchemaV1<AgentDefaultMeta<TEventSchemas>>, TModels extends AgentModelMap = {}, TEmittedSchemas extends Record<string, StandardSchemaV1> = {}, const TStateSchemas extends Record<string, SetupStateSchema> = Record<string, SetupStateSchema>>(config: SetupAgentBaseConfig<TContextSchema, TEventSchemas, TActors, TInputSchema, TOutputSchema, TMetaSchema, TRequestSchemas, TModels, TEmittedSchemas, TStateSchemas>): SetupAgentResult<TContextSchema, TEventSchemas, TActors, TRequestSchemas, TInputSchema, TOutputSchema, TMetaSchema, TModels, TEmittedSchemas, TStateSchemas>;
|
|
537
|
+
declare namespace setupAgent {
|
|
538
|
+
/**
|
|
539
|
+
* Builds a state machine from a serializable {@link AgentWorkflowConfig}
|
|
540
|
+
* (JSON/YAML) instead of the TypeScript `setupAgent(...)` API — the same
|
|
541
|
+
* kind of machine a database, visual editor, or LLM could produce and hand
|
|
542
|
+
* back. Requires a `compileSchema` (see {@link FromConfigOptions}) since
|
|
543
|
+
* the library bundles no JSON Schema engine itself; bring Ajv,
|
|
544
|
+
* @cfworker/json-schema, or another compiler that returns Standard Schema.
|
|
545
|
+
*
|
|
546
|
+
* Returns the built `machine` plus the compiled `schemas` pack (context,
|
|
547
|
+
* events, input, output, meta, emitted) — a JSON-authored agent has no
|
|
548
|
+
* TypeScript types, so hosts need the runtime schemas for things like
|
|
549
|
+
* validating an inbound raw event with `parseAgentEvent`.
|
|
550
|
+
*
|
|
551
|
+
* @example
|
|
552
|
+
* ```ts
|
|
553
|
+
* const { machine, schemas } = setupAgent.fromConfig(workflowConfig, {
|
|
554
|
+
* compileSchema,
|
|
555
|
+
* });
|
|
556
|
+
* const result = await runToQuiescence(
|
|
557
|
+
* createAgentRuntime(machine, { executors: { generateText, decide } }),
|
|
558
|
+
* { input: { ticket } },
|
|
559
|
+
* );
|
|
560
|
+
* const event = parseAgentEvent(result.snapshot, raw, { events: schemas.events });
|
|
561
|
+
* ```
|
|
562
|
+
*/
|
|
563
|
+
function fromConfig(config: AgentWorkflowConfig, options: FromConfigOptions): FromConfigResult;
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* The schema pack a machine was built with — `setupAgent(...).createMachine`
|
|
567
|
+
* and `setupAgent.fromConfig(...)` both register one, so hosts can read a
|
|
568
|
+
* machine's input/event schemas at runtime without knowing how it was
|
|
569
|
+
* authored. Returns `undefined` for machines not built by either (a plain
|
|
570
|
+
* xstate `createMachine`/`setup` machine).
|
|
571
|
+
*
|
|
572
|
+
* Registration also travels through `machine.provide(...)`: the pack is keyed
|
|
573
|
+
* on the machine's root `config`, which `.provide` shares by reference.
|
|
574
|
+
*/
|
|
575
|
+
declare function getAgentSchemas(machine: AnyStateMachine): AgentSchemas | undefined;
|
|
576
|
+
//#endregion
|
|
577
|
+
export { AgentInteractionEventMeta as C, getInteraction as D, eventFromInteraction as E, interactionMetaSchema as O, AgentInteractionEvent as S, GetInteractionOptions as T, FromConfigResult as _, WithAgentUsageEvent as a, AgentInteractionChoice as b, setupAgent as c, AgentWorkflowConfig as d, AgentWorkflowInvokeConfig as f, FromConfigOptions as g, AgentWorkflowTransitionConfig as h, WithAgentEvents as i, AgentWorkflowActionConfig as l, AgentWorkflowStateConfig as m, AgentSchemaPack as n, createAgentSchemas as o, AgentWorkflowRequestConfig as p, AgentUsageEventPayload as r, getAgentSchemas as s, AgentDefaultMeta as t, AgentWorkflowActorConfig as u, SchemaCompiler as v, AgentInteractionMeta as w, AgentInteractionDescriptor as x, AgentInteraction as y };
|