@statelyai/agent 1.1.6 → 2.0.0-alpha.11
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/LICENSE +21 -0
- package/dist/adapter.cjs +15 -0
- package/dist/adapter.d.cts +4 -0
- package/dist/adapter.d.mts +4 -0
- package/dist/adapter.mjs +2 -0
- package/dist/ai-sdk.cjs +306 -0
- package/dist/ai-sdk.d.cts +96 -0
- package/dist/ai-sdk.d.mts +96 -0
- package/dist/ai-sdk.mjs +304 -0
- package/dist/decision-C3k4ve51.mjs +227 -0
- package/dist/decision-D8wJrM8W.cjs +286 -0
- package/dist/events-CRQj3VtP.cjs +1010 -0
- package/dist/events-JiVPYrct.mjs +759 -0
- package/dist/index.cjs +2528 -0
- package/dist/index.d.cts +1232 -0
- package/dist/index.d.mts +1217 -413
- package/dist/index.mjs +2489 -584
- package/dist/openai-compat.cjs +309 -0
- package/dist/openai-compat.d.cts +59 -0
- package/dist/openai-compat.d.mts +59 -0
- package/dist/openai-compat.mjs +308 -0
- package/dist/steps-BALp1eZo.d.mts +198 -0
- package/dist/steps-CVe54GPP.cjs +420 -0
- package/dist/steps-CkyyyuHd.mjs +379 -0
- package/dist/steps-MjnQI4aB.d.cts +198 -0
- package/dist/steps.cjs +12 -0
- package/dist/steps.d.cts +3 -0
- package/dist/steps.d.mts +3 -0
- package/dist/steps.mjs +3 -0
- package/dist/text-logic-CaKqgX4Y.d.mts +710 -0
- package/dist/text-logic-Ckhr2kKC.d.cts +710 -0
- package/dist/types-C9QiMjre.d.cts +219 -0
- package/dist/types-qm00QF91.d.mts +219 -0
- package/dist/utils-BYqT_Dyv.d.cts +108 -0
- package/dist/utils-Do5wIJrh.d.mts +108 -0
- package/dist/zod.cjs +31 -0
- package/dist/zod.d.cts +30 -0
- package/dist/zod.d.mts +30 -0
- package/dist/zod.mjs +30 -0
- package/package.json +132 -28
- package/readme.md +153 -6
- package/schemas/agent-workflow.json +526 -0
- package/.changeset/README.md +0 -8
- package/.changeset/config.json +0 -11
- package/.env.template +0 -3
- package/.github/actions/ci-setup/action.yml +0 -24
- package/.github/workflows/release.yml +0 -46
- package/.vscode/launch.json +0 -28
- package/CHANGELOG.md +0 -222
- package/dist/index.d.ts +0 -428
- package/dist/index.js +0 -621
- package/examples/chatbot.ts +0 -71
- package/examples/cot.ts +0 -89
- package/examples/email.ts +0 -118
- package/examples/example.ts +0 -81
- package/examples/goal.ts +0 -94
- package/examples/helpers/helpers.ts +0 -17
- package/examples/helpers/loader.ts +0 -32
- package/examples/helpers/runner.ts +0 -27
- package/examples/joke.ts +0 -225
- package/examples/multi.ts +0 -103
- package/examples/newspaper.ts +0 -324
- package/examples/number.ts +0 -102
- package/examples/raffle.ts +0 -105
- package/examples/sandbox.ts +0 -28
- package/examples/simple.ts +0 -39
- package/examples/support.ts +0 -147
- package/examples/ticTacToe.ts +0 -224
- package/examples/todo.ts +0 -137
- package/examples/tutor.ts +0 -100
- package/examples/verify.ts +0 -120
- package/examples/weather.ts +0 -178
- package/examples/wiki.ts +0 -30
- package/examples/word.ts +0 -171
- package/src/adapters/vercel.ts +0 -7
- package/src/agent-experimental.ts +0 -221
- package/src/agent.test.ts +0 -506
- package/src/agent.ts +0 -300
- package/src/decision.test.ts +0 -179
- package/src/decision.ts +0 -84
- package/src/index.ts +0 -4
- package/src/memory.ts +0 -25
- package/src/planners/shortestPathPlanner.ts +0 -22
- package/src/planners/simplePlanner.ts +0 -139
- package/src/schemas.ts +0 -11
- package/src/strategies/chain-of-note.ts +0 -155
- package/src/templates/defaultText.ts +0 -18
- package/src/text.ts +0 -241
- package/src/types.ts +0 -499
- package/src/utils.ts +0 -72
- package/tsconfig.json +0 -109
- package/vitest.config.ts +0 -9
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
const require_events = require("./events-CRQj3VtP.cjs");
|
|
2
|
+
const require_decision = require("./decision-D8wJrM8W.cjs");
|
|
3
|
+
let xstate = require("xstate");
|
|
4
|
+
//#region src/steps.ts
|
|
5
|
+
/**
|
|
6
|
+
* The step path: durable, per-model-call-checkpoint hosting of an agent
|
|
7
|
+
* machine. Public vocabulary — `initialAgentStep`, `transitionAgentStep`,
|
|
8
|
+
* `resolveAgentStep`, `getAgentRequests`, `executeAgentRequest`,
|
|
9
|
+
* `resolveAgentRequests`.
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
/** @internal Normalizes current and legacy XState invoke effect shapes. */
|
|
13
|
+
function getInvokeEffectMetadata(action) {
|
|
14
|
+
if (action.type === "@xstate.spawn") return action;
|
|
15
|
+
if (action.type === "xstate.spawnChild") {
|
|
16
|
+
const params = action.params;
|
|
17
|
+
return params ? {
|
|
18
|
+
...params,
|
|
19
|
+
logic: action.logic
|
|
20
|
+
} : void 0;
|
|
21
|
+
}
|
|
22
|
+
if (action.type === "@xstate.start" && typeof action.src === "string") return action;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Scans a set of executable actions (as returned by xstate's `transition`/
|
|
26
|
+
* `initialTransition`) for spawned `TextLogic`/`DecisionLogic` invokes and
|
|
27
|
+
* lowers each into an {@link AgentStepRequest}. The hand-passed-schemas
|
|
28
|
+
* implementation detail behind the public {@link getAgentRequests} — it needs
|
|
29
|
+
* `schemas`/`actorSources` passed explicitly, whereas `getAgentRequests`
|
|
30
|
+
* pre-fills them from the machine's registered `setupAgent` options.
|
|
31
|
+
* `options.snapshot` is required to resolve a decision's candidate events
|
|
32
|
+
* (intersecting declared `allowedEvents` with what's currently legal) — omit
|
|
33
|
+
* it and decision requests report an empty `events` list.
|
|
34
|
+
*
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
function getAgentRequestsWith(actions, options = {}) {
|
|
38
|
+
return [...actions.flatMap((action) => {
|
|
39
|
+
const params = getInvokeEffectMetadata(action);
|
|
40
|
+
if (!params || typeof params.src !== "string") return [];
|
|
41
|
+
if (typeof params.id !== "string" || params.id.length === 0) throw new Error(`Agent invoke '${params.src}' must define a durable string id.`);
|
|
42
|
+
const registeredLogic = require_events.isTextLogic(params.logic) || require_decision.isDecisionLogic(params.logic) ? params.logic : options.actorSources?.[params.src];
|
|
43
|
+
if (require_decision.isDecisionLogic(registeredLogic)) {
|
|
44
|
+
const decisionRequest = registeredLogic.request(params.input);
|
|
45
|
+
const allowedEventTypes = registeredLogic.allowedEventTypes?.(params.input);
|
|
46
|
+
const events = options.snapshot ? require_events.getAcceptedEvents(options.snapshot, {
|
|
47
|
+
events: options.events,
|
|
48
|
+
schemas: options.schemas,
|
|
49
|
+
eventTypes: allowedEventTypes,
|
|
50
|
+
eventToolName: options.eventToolName
|
|
51
|
+
}) : [];
|
|
52
|
+
return [{
|
|
53
|
+
...decisionRequest,
|
|
54
|
+
id: params.id,
|
|
55
|
+
events
|
|
56
|
+
}];
|
|
57
|
+
}
|
|
58
|
+
const textLogic = require_events.isTextLogic(registeredLogic) ? registeredLogic : void 0;
|
|
59
|
+
const input = textLogic ? textLogic.request(params.input) : void 0;
|
|
60
|
+
if (!input) return [];
|
|
61
|
+
return [{
|
|
62
|
+
kind: "text",
|
|
63
|
+
id: params.id,
|
|
64
|
+
src: params.src,
|
|
65
|
+
...textLogic ? { mode: textLogic.mode } : {},
|
|
66
|
+
input,
|
|
67
|
+
tools: input.tools ?? {},
|
|
68
|
+
events: []
|
|
69
|
+
}];
|
|
70
|
+
}), ...getActivePlanRequests(options)];
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Scans the live snapshot's children for active `agent.plan` (plan-logic)
|
|
74
|
+
* invokes and lowers each into an {@link AgentPlanRequest} — the re-surfacing
|
|
75
|
+
* half of plan discovery. Reads the applied-event trail and remaining budget
|
|
76
|
+
* from the child's own ledger `context` ({@link PlanLedgerContext}), recomputes
|
|
77
|
+
* the currently-legal candidates (∩ declared `allowedEvents`) plus the reserved
|
|
78
|
+
* `agent.plan.done` move, and takes `stepsRemaining` from the ledger (falling
|
|
79
|
+
* back to `maxSteps - applied.length` for a snapshot with no context yet).
|
|
80
|
+
* Returns `[]` when no snapshot is available (candidates need a live snapshot).
|
|
81
|
+
*
|
|
82
|
+
* @internal
|
|
83
|
+
*/
|
|
84
|
+
function getActivePlanRequests(options) {
|
|
85
|
+
const snapshot = options.snapshot;
|
|
86
|
+
if (!snapshot) return [];
|
|
87
|
+
const children = snapshot.children;
|
|
88
|
+
if (!children) return [];
|
|
89
|
+
const requests = [];
|
|
90
|
+
for (const [id, child] of Object.entries(children)) {
|
|
91
|
+
const ref = child;
|
|
92
|
+
if (typeof ref?.getSnapshot !== "function") continue;
|
|
93
|
+
const src = typeof ref.src === "string" ? ref.src : void 0;
|
|
94
|
+
const logic = (src ? options.actorSources?.[src] : void 0) ?? ref.logic;
|
|
95
|
+
if (!require_decision.isPlanLogic(logic)) continue;
|
|
96
|
+
const childSnapshot = ref.getSnapshot();
|
|
97
|
+
if (childSnapshot?.status !== "active") continue;
|
|
98
|
+
const input = childSnapshot.input ?? {};
|
|
99
|
+
const maxSteps = input.maxSteps ?? 8;
|
|
100
|
+
const ledger = childSnapshot.context ?? {};
|
|
101
|
+
const applied = ledger.applied ?? [];
|
|
102
|
+
const stepsRemaining = ledger.stepsRemaining ?? maxSteps - applied.length;
|
|
103
|
+
const machineEvents = require_events.getAcceptedEvents(snapshot, {
|
|
104
|
+
events: options.events,
|
|
105
|
+
schemas: options.schemas,
|
|
106
|
+
eventTypes: logic.allowedEventTypes(input) ?? void 0,
|
|
107
|
+
eventToolName: options.eventToolName
|
|
108
|
+
});
|
|
109
|
+
const events = machineEvents.some((event) => event.type === "agent.plan.done") ? machineEvents : [...machineEvents, {
|
|
110
|
+
type: require_decision.PLAN_DONE_EVENT_TYPE,
|
|
111
|
+
toolName: require_events.sanitizeEventToolName(require_decision.PLAN_DONE_EVENT_TYPE)
|
|
112
|
+
}];
|
|
113
|
+
requests.push({
|
|
114
|
+
kind: "plan",
|
|
115
|
+
id,
|
|
116
|
+
src: src ?? "",
|
|
117
|
+
input,
|
|
118
|
+
events,
|
|
119
|
+
applied,
|
|
120
|
+
stepsRemaining: Math.max(0, stepsRemaining)
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
return requests;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Builds the synthetic `xstate.done.actor.<id>` event xstate's `transition()`
|
|
127
|
+
* expects to resolve a spawned invoke — the event {@link resolveAgentStep}
|
|
128
|
+
* applies internally.
|
|
129
|
+
*
|
|
130
|
+
* @internal
|
|
131
|
+
*/
|
|
132
|
+
function doneEvent(request, output) {
|
|
133
|
+
return {
|
|
134
|
+
type: `xstate.done.actor.${typeof request === "string" ? request : request.id}`,
|
|
135
|
+
output
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Applies a request's `output` as a done event via `transition(...)`,
|
|
140
|
+
* returning the raw `[snapshot, actions]` tuple. Lower-level than
|
|
141
|
+
* {@link resolveAgentStep} — that helper wraps this and also runs
|
|
142
|
+
* {@link getAgentRequests} to produce the next {@link AgentStep}.
|
|
143
|
+
*
|
|
144
|
+
* @internal
|
|
145
|
+
*/
|
|
146
|
+
function transitionResult(logic, snapshot, request, output) {
|
|
147
|
+
const event = doneEvent(request, output);
|
|
148
|
+
const result = (0, xstate.transition)(logic, snapshot, event);
|
|
149
|
+
applyFinalStateOutput(logic, result[0], event);
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Starts a machine and returns its first {@link AgentStep} — the step-path
|
|
154
|
+
* equivalent of `initialTransition` plus request discovery. Begins the
|
|
155
|
+
* durable/per-model-call-checkpoint loop: resolve each `step.requests` entry
|
|
156
|
+
* (via {@link executeAgentRequest} for `kind: 'text'`, or
|
|
157
|
+
* {@link resolveDecision} for `kind: 'decision'`), then advance with
|
|
158
|
+
* {@link resolveAgentStep} or {@link transitionAgentStep}.
|
|
159
|
+
*/
|
|
160
|
+
function initialAgentStep(machine, input, options) {
|
|
161
|
+
const [snapshot, actions] = (0, xstate.initialTransition)(machine, input);
|
|
162
|
+
return createAgentStep(machine, snapshot, actions, require_events.getRegisteredAgentExecutionOptions(machine, options));
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Applies an externally-sent event (e.g. a decision's chosen event, or a
|
|
166
|
+
* human's reply) and returns the next {@link AgentStep}. Accepts **either**
|
|
167
|
+
* a raw snapshot **or** a prior `AgentStep` as the second argument —
|
|
168
|
+
* `.snapshot` is unwrapped automatically, so callers can thread the whole
|
|
169
|
+
* step object through without manually plucking the snapshot out.
|
|
170
|
+
*/
|
|
171
|
+
function transitionAgentStep(machine, snapshotOrStep, event, options) {
|
|
172
|
+
const [nextSnapshot, actions] = (0, xstate.transition)(machine, isAgentStep(snapshotOrStep) ? snapshotOrStep.snapshot : snapshotOrStep, event);
|
|
173
|
+
return createAgentStep(machine, nextSnapshot, actions, require_events.getRegisteredAgentExecutionOptions(machine, options));
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Applies a resolved text request's output (a `kind: 'text'`
|
|
177
|
+
* {@link AgentRequest} — not a decision) as a done event and returns the
|
|
178
|
+
* next {@link AgentStep}. For decisions, resolve with `resolveDecision`
|
|
179
|
+
* (which returns a {@link ChosenEvent}) and apply it with
|
|
180
|
+
* {@link transitionAgentStep} instead — a decision has no output value of
|
|
181
|
+
* its own to feed here.
|
|
182
|
+
*/
|
|
183
|
+
function resolveAgentStep(machine, step, request, output, options) {
|
|
184
|
+
const [snapshot, actions] = transitionResult(machine, step.snapshot, request, output);
|
|
185
|
+
return createAgentStep(machine, snapshot, actions, require_events.getRegisteredAgentExecutionOptions(machine, options));
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Snapshot in, requests out: scans executable actions for spawned agent
|
|
189
|
+
* invokes and lowers each into an {@link AgentStepRequest}, pre-filled with
|
|
190
|
+
* the machine's registered `setupAgent` schemas/actorSources (so callers
|
|
191
|
+
* don't pass them by hand each call) — merged with any `options` passed here,
|
|
192
|
+
* which take precedence. The step path's public discovery primitive;
|
|
193
|
+
* `initialAgentStep`/`transitionAgentStep`/`resolveAgentStep` call it
|
|
194
|
+
* internally to populate `AgentStep.requests`.
|
|
195
|
+
*/
|
|
196
|
+
function getAgentRequests(machine, actions, snapshot, options = {}) {
|
|
197
|
+
return getAgentRequestsWith(actions, {
|
|
198
|
+
...require_events.getRegisteredAgentExecutionOptions(machine, options),
|
|
199
|
+
...options,
|
|
200
|
+
snapshot
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
async function executeAgentRequest(request, executors, options) {
|
|
204
|
+
if (request.kind === "decision") throw new Error("executeAgentRequest(...) is text-only. Resolve a 'decision' request with resolveDecision(request, executors.decide, ...) instead.");
|
|
205
|
+
assertTextExecutor(request, executors);
|
|
206
|
+
const { output, raw } = await require_events.executeAgentTextRequest(request.mode ?? "generate", request.id, request.input, executors, request.tools);
|
|
207
|
+
const normalizedOutput = request.input.outputSchema ? require_events.validateSchemaSync(request.input.outputSchema, output) : output;
|
|
208
|
+
return options?.verbose ? {
|
|
209
|
+
output: normalizedOutput,
|
|
210
|
+
raw
|
|
211
|
+
} : normalizedOutput;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Resolves the current step's pending requests and returns the next
|
|
215
|
+
* {@link AgentStep} — one iteration of the durable step loop, collapsing the
|
|
216
|
+
* manual `request.kind` dispatch a host would otherwise write by hand.
|
|
217
|
+
*
|
|
218
|
+
* For each pending request, in order: a `kind: 'text'` request is run with
|
|
219
|
+
* {@link executeAgentRequest} then fed back via {@link resolveAgentStep}; a
|
|
220
|
+
* `kind: 'decision'` request is resolved with `resolveDecision` (wiring
|
|
221
|
+
* `canTake` to `step.snapshot.can` so guard-rejected choices retry) then
|
|
222
|
+
* applied with {@link transitionAgentStep}. The **current** step is re-read
|
|
223
|
+
* after each application — the machine may advance and its `requests` change —
|
|
224
|
+
* so this always resolves against the live step, never a stale list.
|
|
225
|
+
*
|
|
226
|
+
* A `kind: 'plan'` request (`agent.plan`) is resolved natively too: one plan
|
|
227
|
+
* step per call. It resolves a single decision from `request.events` (wiring
|
|
228
|
+
* `canTake` to `step.snapshot.can`, exempting the reserved `agent.plan.done`
|
|
229
|
+
* move and `stopOn` events), then either applies the chosen machine event and
|
|
230
|
+
* lets the next step re-surface the plan, or completes the plan (feeding its
|
|
231
|
+
* `{ steps, stopped }` output back) on the done move / a `stopOn` event / an
|
|
232
|
+
* exhausted budget / no legal events. The plan's applied trail is carried in
|
|
233
|
+
* the invoke child's snapshot, so persisting the step between calls resumes the
|
|
234
|
+
* plan identically.
|
|
235
|
+
*
|
|
236
|
+
* Missing the executor a request needs throws a clear error
|
|
237
|
+
* (`generateText`/`streamText` for text, `decide` for decisions and plans).
|
|
238
|
+
*
|
|
239
|
+
* A complete durable host is two lines:
|
|
240
|
+
*
|
|
241
|
+
* ```ts
|
|
242
|
+
* let step = initialAgentStep(machine, input);
|
|
243
|
+
* while (!step.done) step = await resolveAgentRequests(machine, step, executors);
|
|
244
|
+
* ```
|
|
245
|
+
*
|
|
246
|
+
* All pending **text** requests of a step are resolved in parallel
|
|
247
|
+
* (`Promise.all`) — parallel statechart regions are genuinely concurrent, so
|
|
248
|
+
* their model calls run concurrently — then their outputs apply in
|
|
249
|
+
* **request-array order** (deterministic for durable replay regardless of which
|
|
250
|
+
* call finishes first). Decisions and plans stay **one at a time**: applying
|
|
251
|
+
* either changes the set of legal candidates for what follows, so they cannot be
|
|
252
|
+
* resolved against a stale snapshot. A host that instead wants strictly
|
|
253
|
+
* sequential text resolution loops the manual per-request helpers
|
|
254
|
+
* ({@link executeAgentRequest} + {@link resolveAgentStep}) one at a time.
|
|
255
|
+
*/
|
|
256
|
+
async function resolveAgentRequests(machine, step, executors, options) {
|
|
257
|
+
const [request] = step.requests;
|
|
258
|
+
if (!request) return step;
|
|
259
|
+
if (request.kind === "decision") {
|
|
260
|
+
if (!executors.decide) throw new Error(`this step's decision request '${request.id}' needs a 'decide' executor but none was provided.`);
|
|
261
|
+
return transitionAgentStep(machine, step, await require_decision.resolveDecision(request, executors.decide, {
|
|
262
|
+
canTake: (event) => step.snapshot.can(event),
|
|
263
|
+
maxRetries: options?.maxRetries
|
|
264
|
+
}), options);
|
|
265
|
+
}
|
|
266
|
+
if (request.kind === "plan") return resolvePlanRequest(machine, step, request, executors, options);
|
|
267
|
+
const textRequests = step.requests.filter((candidate) => candidate.kind === "text");
|
|
268
|
+
for (const textRequest of textRequests) assertTextExecutor(textRequest, executors);
|
|
269
|
+
const outputs = await Promise.all(textRequests.map((textRequest) => executeAgentRequest(textRequest, executors)));
|
|
270
|
+
let next = step;
|
|
271
|
+
for (let index = 0; index < textRequests.length; index++) next = resolveAgentStep(machine, next, textRequests[index], outputs[index], options);
|
|
272
|
+
return next;
|
|
273
|
+
}
|
|
274
|
+
function assertTextExecutor(request, executors) {
|
|
275
|
+
const mode = request.mode ?? "generate";
|
|
276
|
+
const kind = mode === "stream" ? "streamText" : "generateText";
|
|
277
|
+
if (!(mode === "stream" ? executors.streamText : executors.generateText)) throw new Error(`this step's text request '${request.src}' needs a '${kind}' executor but none was provided.`);
|
|
278
|
+
}
|
|
279
|
+
async function resolvePlanRequest(machine, step, request, executors, options) {
|
|
280
|
+
if (!executors.decide) throw new Error(`this step's plan request '${request.src}' needs a 'decide' executor but none was provided.`);
|
|
281
|
+
const stopOn = new Set(request.input.stopOn ?? []);
|
|
282
|
+
if (request.stepsRemaining <= 0) return completePlan(machine, step, request.id, request.applied, "max-steps", options);
|
|
283
|
+
if (request.events.filter((event) => event.type !== "agent.plan.done").length === 0) return completePlan(machine, step, request.id, request.applied, "no-legal-events", options);
|
|
284
|
+
const chosen = await require_decision.resolveDecision(planStepDecisionRequest(request), executors.decide, {
|
|
285
|
+
maxRetries: options?.maxRetries,
|
|
286
|
+
canTake: (event) => {
|
|
287
|
+
if (event.type === "agent.plan.done" || stopOn.has(event.type)) return true;
|
|
288
|
+
return step.snapshot.can(event);
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
if (chosen.type === "agent.plan.done") return completePlan(machine, step, request.id, request.applied, "done", options);
|
|
292
|
+
const applied = [...request.applied, chosen];
|
|
293
|
+
advancePlanChildLedger(step.snapshot, request.id, {
|
|
294
|
+
type: "plan.applied",
|
|
295
|
+
event: chosen
|
|
296
|
+
});
|
|
297
|
+
const next = transitionAgentStep(machine, step, chosen, options);
|
|
298
|
+
if (stopOn.has(chosen.type)) {
|
|
299
|
+
if (isPlanActive(next.snapshot, request.id)) return completePlan(machine, next, request.id, applied, "stop-event", options);
|
|
300
|
+
}
|
|
301
|
+
return next;
|
|
302
|
+
}
|
|
303
|
+
function completePlan(machine, step, id, steps, stopped, options) {
|
|
304
|
+
return resolveAgentStep(machine, step, { id }, {
|
|
305
|
+
steps,
|
|
306
|
+
stopped
|
|
307
|
+
}, options);
|
|
308
|
+
}
|
|
309
|
+
function planStepDecisionRequest(request) {
|
|
310
|
+
const { input, applied, events, id } = request;
|
|
311
|
+
const trail = applied.length === 0 ? "" : `\n\nEvents already applied in this plan, in order:\n${applied.map((step) => JSON.stringify(step)).join("\n")}\nContinue from here; do not repeat applied events.`;
|
|
312
|
+
const doneHint = `\n\nWhen the request is fully handled (or no action is needed), choose '${require_decision.PLAN_DONE_EVENT_TYPE}'.`;
|
|
313
|
+
return {
|
|
314
|
+
kind: "decision",
|
|
315
|
+
id: `${id}[${applied.length}]`,
|
|
316
|
+
model: input.model,
|
|
317
|
+
system: input.system,
|
|
318
|
+
prompt: `${input.prompt ?? ""}${trail}${doneHint}`,
|
|
319
|
+
messages: input.messages,
|
|
320
|
+
events,
|
|
321
|
+
attempts: [],
|
|
322
|
+
temperature: input.temperature,
|
|
323
|
+
maxOutputTokens: input.maxOutputTokens,
|
|
324
|
+
topP: input.topP,
|
|
325
|
+
topK: input.topK,
|
|
326
|
+
seed: input.seed,
|
|
327
|
+
stopSequences: input.stopSequences,
|
|
328
|
+
metadata: input.metadata
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
function advancePlanChildLedger(snapshot, id, event) {
|
|
332
|
+
const child = snapshot.children?.[id];
|
|
333
|
+
const childSnapshot = child?.getSnapshot?.();
|
|
334
|
+
if (!require_decision.isPlanLogic(child?.logic) || !childSnapshot || typeof childSnapshot !== "object") return;
|
|
335
|
+
Object.assign(childSnapshot, require_decision.advancePlanLedger(child.logic, childSnapshot, event));
|
|
336
|
+
}
|
|
337
|
+
function isPlanActive(snapshot, id) {
|
|
338
|
+
return (snapshot.children?.[id])?.getSnapshot?.()?.status === "active";
|
|
339
|
+
}
|
|
340
|
+
function createAgentStep(machine, snapshot, actions, options) {
|
|
341
|
+
applyFinalStateOutput(machine, snapshot);
|
|
342
|
+
return {
|
|
343
|
+
snapshot,
|
|
344
|
+
actions,
|
|
345
|
+
requests: getAgentRequestsWith(actions, {
|
|
346
|
+
...options,
|
|
347
|
+
snapshot
|
|
348
|
+
}),
|
|
349
|
+
done: snapshot.status === "done"
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
function resolveStateValueConfig(config, value) {
|
|
353
|
+
if (typeof value === "string") return config.states?.[value];
|
|
354
|
+
if (!value || typeof value !== "object") return;
|
|
355
|
+
for (const [key, childValue] of Object.entries(value)) {
|
|
356
|
+
const childConfig = config.states?.[key];
|
|
357
|
+
if (!childConfig) continue;
|
|
358
|
+
if (childConfig.type === "final") return childConfig;
|
|
359
|
+
const nested = resolveStateValueConfig(childConfig, childValue);
|
|
360
|
+
if (nested) return nested;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
function applyFinalStateOutput(logic, snapshot, event) {
|
|
364
|
+
const machineSnapshot = snapshot;
|
|
365
|
+
if (machineSnapshot.status !== "done" || machineSnapshot.output !== void 0 || !("config" in logic)) return;
|
|
366
|
+
const config = logic.config;
|
|
367
|
+
if (!config) return;
|
|
368
|
+
const output = resolveStateValueConfig(config, machineSnapshot.value)?.output;
|
|
369
|
+
if (output === void 0) return;
|
|
370
|
+
machineSnapshot.output = typeof output === "function" ? output({
|
|
371
|
+
context: machineSnapshot.context,
|
|
372
|
+
event
|
|
373
|
+
}) : output;
|
|
374
|
+
}
|
|
375
|
+
function isAgentStep(value) {
|
|
376
|
+
return !!value && typeof value === "object" && "snapshot" in value && "actions" in value && "requests" in value;
|
|
377
|
+
}
|
|
378
|
+
//#endregion
|
|
379
|
+
Object.defineProperty(exports, "executeAgentRequest", {
|
|
380
|
+
enumerable: true,
|
|
381
|
+
get: function() {
|
|
382
|
+
return executeAgentRequest;
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
Object.defineProperty(exports, "getAgentRequests", {
|
|
386
|
+
enumerable: true,
|
|
387
|
+
get: function() {
|
|
388
|
+
return getAgentRequests;
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
Object.defineProperty(exports, "getInvokeEffectMetadata", {
|
|
392
|
+
enumerable: true,
|
|
393
|
+
get: function() {
|
|
394
|
+
return getInvokeEffectMetadata;
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
Object.defineProperty(exports, "initialAgentStep", {
|
|
398
|
+
enumerable: true,
|
|
399
|
+
get: function() {
|
|
400
|
+
return initialAgentStep;
|
|
401
|
+
}
|
|
402
|
+
});
|
|
403
|
+
Object.defineProperty(exports, "resolveAgentRequests", {
|
|
404
|
+
enumerable: true,
|
|
405
|
+
get: function() {
|
|
406
|
+
return resolveAgentRequests;
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
Object.defineProperty(exports, "resolveAgentStep", {
|
|
410
|
+
enumerable: true,
|
|
411
|
+
get: function() {
|
|
412
|
+
return resolveAgentStep;
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
Object.defineProperty(exports, "transitionAgentStep", {
|
|
416
|
+
enumerable: true,
|
|
417
|
+
get: function() {
|
|
418
|
+
return transitionAgentStep;
|
|
419
|
+
}
|
|
420
|
+
});
|