@statelyai/agent 2.0.0-alpha.13 → 2.0.0-alpha.15
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 +1 -1
- package/dist/ai-sdk.d.cts +2 -2
- package/dist/ai-sdk.d.mts +2 -2
- package/dist/ai-sdk.mjs +1 -1
- package/dist/{decision-dWGhBh0P.cjs → decision-C0cUKvNt.cjs} +0 -105
- package/dist/{decision-BezSD_YC.mjs → decision-D9Zi7Xi5.mjs} +2 -65
- package/dist/index.cjs +52 -155
- package/dist/index.d.cts +26 -13
- package/dist/index.d.mts +26 -13
- package/dist/index.mjs +54 -156
- package/dist/machines.cjs +1 -1
- package/dist/machines.d.cts +1 -1
- package/dist/machines.d.mts +1 -1
- package/dist/machines.mjs +1 -1
- package/dist/otel.cjs +5 -15
- package/dist/otel.d.cts +1 -1
- package/dist/otel.d.mts +1 -1
- package/dist/otel.mjs +5 -15
- package/dist/{run-agent-C3mFDGTf.d.mts → run-agent-BWzo4FLv.d.mts} +83 -109
- package/dist/{run-agent-DnvtcnTZ.d.cts → run-agent-B_n4Qxye.d.cts} +83 -109
- package/dist/{setup-agent-DP95MFrI.cjs → setup-agent-CpK0ZRWV.cjs} +10 -210
- package/dist/{setup-agent-DAZZSjDS.mjs → setup-agent-DeHRW-qX.mjs} +11 -205
- package/dist/sqlite.d.cts +2 -2
- package/dist/sqlite.d.mts +2 -2
- package/dist/{text-logic-TkKPw8Aq.d.mts → text-logic-C7anC7qX.d.mts} +8 -94
- package/dist/{text-logic-BDxwQNsD.d.cts → text-logic-DZW7XWy9.d.cts} +8 -94
- package/dist/{types-QbEfCVny.d.cts → types-CTBhMnFu.d.mts} +30 -3
- package/dist/{types-_FXoFBGO.d.mts → types-DFD28AWe.d.cts} +30 -3
- package/package.json +1 -1
- /package/dist/{event-log-store-CriMgX1D.d.mts → event-log-store-BkUNtyOF.d.mts} +0 -0
- /package/dist/{event-log-store-Ruq18mGp.d.cts → event-log-store-CVd2eyRy.d.cts} +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { c as AgentTools, d as ChosenEvent, n as AgentMessage } from "./types-
|
|
1
|
+
import { T as WithAgentInputSchema, c as AgentTools, d as ChosenEvent, h as InferInput, n as AgentMessage, v as StandardSchemaV1 } from "./types-CTBhMnFu.mjs";
|
|
2
2
|
import { t as AgentError } from "./errors-C9rxnWbX.mjs";
|
|
3
|
-
import {
|
|
4
|
-
import { i as AgentLogEntry, o as JsonValue } from "./event-log-store-
|
|
3
|
+
import { B as AgentRequestOptions, M as AgentDecisionRequest, R as AgentEventDescriptor, V as AgentRequestSource, d as AgentTextRequest, f as AgentUsage, l as AgentRequestExecutors, p as AgentUserInput, t as AgentCallUsage, u as AgentRequestMode } from "./text-logic-C7anC7qX.mjs";
|
|
4
|
+
import { i as AgentLogEntry, o as JsonValue } from "./event-log-store-BkUNtyOF.mjs";
|
|
5
5
|
import { AnyActorLogic, AnyActorRef, AnyMachineSnapshot, AnyStateMachine, AsyncActorLogic, EmittedFrom, EventFromLogic, EventObject, ExecutableActionObject, InputFrom, InspectionEvent, OutputFrom, Snapshot, SnapshotFrom, createActor } from "xstate";
|
|
6
6
|
|
|
7
7
|
//#region src/internal/registry.d.ts
|
|
@@ -9,94 +9,6 @@ type AgentExecutionOptions = Pick<AgentRequestOptions, "schemas" | "actors"> & {
|
|
|
9
9
|
models?: object;
|
|
10
10
|
};
|
|
11
11
|
//#endregion
|
|
12
|
-
//#region src/steps.d.ts
|
|
13
|
-
/**
|
|
14
|
-
* A pending text request surfaced by step discovery ({@link getAgentRequests}
|
|
15
|
-
* / {@link AgentStep.requests}): the machine has spawned a
|
|
16
|
-
* `TextLogic`-backed invoke and is waiting on its result. Resolve it with
|
|
17
|
-
* {@link executeAgentRequest} (or by hand, then feed the output into
|
|
18
|
-
* {@link resolveAgentStep} via `xstate.done.actor` + `actorId`).
|
|
19
|
-
*/
|
|
20
|
-
interface AgentRequest<TInput extends AgentTextRequest = AgentTextRequest> {
|
|
21
|
-
kind: "text";
|
|
22
|
-
id: string;
|
|
23
|
-
src: AgentRequestSource;
|
|
24
|
-
mode?: AgentRequestMode;
|
|
25
|
-
input: TInput;
|
|
26
|
-
tools: AgentTools;
|
|
27
|
-
events: AgentEventDescriptor[];
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* A pending **plan** request re-surfaced by step discovery: the machine
|
|
31
|
-
* invoked `agent.plan`, which applies an ordered sequence of legal events
|
|
32
|
-
* (each one a decision) rather than a single one. Unlike text/decision
|
|
33
|
-
* requests — surfaced once and resolved once — a plan request **re-surfaces on
|
|
34
|
-
* every step** while the plan is in flight, its `events`/`applied`/
|
|
35
|
-
* `stepsRemaining` updated each time, until it terminates.
|
|
36
|
-
*
|
|
37
|
-
* All fields are plain serializable data. Resolve ONE decision per step from
|
|
38
|
-
* `events` (via {@link resolveDecision}, wiring `canTake` to
|
|
39
|
-
* `snapshot.can` exactly like a single decision) then apply it: a real machine
|
|
40
|
-
* event advances the plan (the next step re-surfaces this request); the
|
|
41
|
-
* reserved `agent.plan.done` move, a `stopOn` event, an exhausted budget, or no
|
|
42
|
-
* legal events completes it (its invoke resolves with `{ steps, stopped }`).
|
|
43
|
-
* {@link resolveAgentRequests} does all of this natively — one decision (or one
|
|
44
|
-
* completion) per call.
|
|
45
|
-
*
|
|
46
|
-
* The in-progress plan state (`applied` trail + remaining budget) lives in the
|
|
47
|
-
* plan invoke child's own `createLogic` snapshot `context`
|
|
48
|
-
* (`children.<id>.snapshot.context`), so it survives a full JSON
|
|
49
|
-
* `getPersistedSnapshot` → restore round-trip: a host that persists the step
|
|
50
|
-
* after every event and reloads resumes the plan identically.
|
|
51
|
-
*/
|
|
52
|
-
interface AgentPlanRequest {
|
|
53
|
-
kind: "plan";
|
|
54
|
-
/** Durable invoke id of the `agent.plan` invoke. */
|
|
55
|
-
id: string;
|
|
56
|
-
/** Invoke src (`'agent.plan'` or a registered plan-logic source name). */
|
|
57
|
-
src: AgentRequestSource;
|
|
58
|
-
/** The resolved plan input (`model`/`system`/`prompt`/`allowedEvents`/`stopOn`/`maxSteps`/…). */
|
|
59
|
-
input: AgentPlanInput;
|
|
60
|
-
/**
|
|
61
|
-
* The legal candidates for the NEXT plan step: the currently
|
|
62
|
-
* snapshot-legal machine events (∩ declared `allowedEvents`) plus the
|
|
63
|
-
* reserved `agent.plan.done` move.
|
|
64
|
-
*/
|
|
65
|
-
events: AgentEventDescriptor[];
|
|
66
|
-
/** The events applied so far in this plan, in order (the trail). */
|
|
67
|
-
applied: ChosenEvent[];
|
|
68
|
-
/** How many more events the plan may apply (`maxSteps - applied.length`). */
|
|
69
|
-
stepsRemaining: number;
|
|
70
|
-
}
|
|
71
|
-
/** `AgentStep.requests` element: a text, decision, or plan request. */
|
|
72
|
-
type AgentStepRequest = AgentRequest | AgentDecisionRequest | AgentPlanRequest;
|
|
73
|
-
/**
|
|
74
|
-
* Resolves one **text** request against a host's {@link AgentRequestExecutors}
|
|
75
|
-
* — merges the request's tools, dispatches to `generateText`/`streamText` per
|
|
76
|
-
* `mode`, and validates the result against the request's `outputSchema` if
|
|
77
|
-
* present. Accepts either a `kind: 'text'` `AgentEffect` (the thin-loop shape
|
|
78
|
-
* from `getAgentEffects`) or a legacy {@link AgentRequest} envelope.
|
|
79
|
-
* **Text-only**: passing a `kind: 'decision'` request throws, directing the
|
|
80
|
-
* caller to `resolveDecision(request, executors.decide, ...)` instead. By default
|
|
81
|
-
* returns the normalized output; pass `{ verbose: true }` to also get the
|
|
82
|
-
* raw executor result (tool calls, usage, finish reason — needed for
|
|
83
|
-
* observability and event-sourced replay).
|
|
84
|
-
*/
|
|
85
|
-
/** A `kind: 'text'` `AgentEffect` (structural, to avoid an import cycle with effects.ts). */
|
|
86
|
-
interface TextEffectLike {
|
|
87
|
-
kind: "text";
|
|
88
|
-
requestId: string;
|
|
89
|
-
request: AgentTextRequest;
|
|
90
|
-
mode?: AgentRequestMode;
|
|
91
|
-
}
|
|
92
|
-
declare function executeAgentRequest(request: AgentRequest | TextEffectLike, executors: Partial<AgentRequestExecutors>): Promise<unknown>;
|
|
93
|
-
declare function executeAgentRequest(request: AgentRequest | TextEffectLike, executors: Partial<AgentRequestExecutors>, options: {
|
|
94
|
-
verbose: true;
|
|
95
|
-
}): Promise<{
|
|
96
|
-
output: unknown;
|
|
97
|
-
raw: unknown;
|
|
98
|
-
}>;
|
|
99
|
-
//#endregion
|
|
100
12
|
//#region src/effects.d.ts
|
|
101
13
|
/**
|
|
102
14
|
* One thing a host does at the frontier. Effect kinds mirror what a machine
|
|
@@ -109,8 +21,6 @@ declare function executeAgentRequest(request: AgentRequest | TextEffectLike, exe
|
|
|
109
21
|
* - `decision` — an `agent.decide`/`DecisionLogic` invoke. Resolve with
|
|
110
22
|
* `resolveDecision` and journal the CHOSEN machine event directly (a decision
|
|
111
23
|
* has no output value of its own; the chosen event advances the machine).
|
|
112
|
-
* - `plan` — an `agent.plan` invoke (multi-event). Drive it a step at a time
|
|
113
|
-
* (see `resolveAgentRequests`), journaling each applied event.
|
|
114
24
|
* - `task` — any other invoke/spawn: a plain host-run task keyed by `src` +
|
|
115
25
|
* `input`. Run it, then journal `toDoneEvent`/`toErrorEvent`.
|
|
116
26
|
* - `delay` — an `after(...)` timer. Schedule it; when it fires, journal
|
|
@@ -130,10 +40,6 @@ type AgentEffect = {
|
|
|
130
40
|
kind: "decision";
|
|
131
41
|
requestId: string;
|
|
132
42
|
request: AgentDecisionRequest;
|
|
133
|
-
} | {
|
|
134
|
-
kind: "plan";
|
|
135
|
-
requestId: string;
|
|
136
|
-
request: AgentPlanRequest;
|
|
137
43
|
} | {
|
|
138
44
|
kind: "task";
|
|
139
45
|
requestId: string;
|
|
@@ -195,7 +101,7 @@ interface AgentUsageEvent extends EventObject {
|
|
|
195
101
|
/** The settled call's token usage, as reported by the executor. */
|
|
196
102
|
usage: AgentCallUsage;
|
|
197
103
|
/** Which kind of request reported it. */
|
|
198
|
-
kind?: "text" | "decision"
|
|
104
|
+
kind?: "text" | "decision";
|
|
199
105
|
/** The reporting request's durable invoke id. */
|
|
200
106
|
id?: string;
|
|
201
107
|
/** The reporting request's invoke `src`. */
|
|
@@ -261,8 +167,8 @@ interface GetAgentEffectsOptions extends Partial<AgentExecutionOptions> {
|
|
|
261
167
|
* Ordering is load-bearing: a single transition's actions are emitted in
|
|
262
168
|
* document order (a custom entry action, a spawn, and a `sendTo` in that order
|
|
263
169
|
* yield `execute`, then `task`/agent effect, then `execute` — never a
|
|
264
|
-
* reordered set). Effects visible only on the snapshot (
|
|
265
|
-
*
|
|
170
|
+
* reordered set). Effects visible only on the snapshot (children spawned by
|
|
171
|
+
* an EARLIER transition that
|
|
266
172
|
* have not completed yet — the fan-out / crash-resume case) are appended after
|
|
267
173
|
* the action-derived effects, deduped by site id.
|
|
268
174
|
*
|
|
@@ -370,6 +276,52 @@ declare function verifyReplay<TMachine extends AnyStateMachine>(machine: TMachin
|
|
|
370
276
|
/** Structural event-tail, logical-state, and owed-effect comparison. */
|
|
371
277
|
declare function diffEventLogs<TMachine extends AnyStateMachine>(machine: TMachine, parentEntries: readonly AgentLogEntry[], forkEntries: readonly AgentLogEntry[], options?: ReplayOptions): AgentEventLogDiff<TMachine>;
|
|
372
278
|
//#endregion
|
|
279
|
+
//#region src/steps.d.ts
|
|
280
|
+
/**
|
|
281
|
+
* A pending text request surfaced by step discovery ({@link getAgentRequests}
|
|
282
|
+
* / {@link AgentStep.requests}): the machine has spawned a
|
|
283
|
+
* `TextLogic`-backed invoke and is waiting on its result. Resolve it with
|
|
284
|
+
* {@link executeAgentRequest} (or by hand, then feed the output into
|
|
285
|
+
* {@link resolveAgentStep} via `xstate.done.actor` + `actorId`).
|
|
286
|
+
*/
|
|
287
|
+
interface AgentRequest<TInput extends AgentTextRequest = AgentTextRequest> {
|
|
288
|
+
kind: "text";
|
|
289
|
+
id: string;
|
|
290
|
+
src: AgentRequestSource;
|
|
291
|
+
mode?: AgentRequestMode;
|
|
292
|
+
input: TInput;
|
|
293
|
+
tools: AgentTools;
|
|
294
|
+
events: AgentEventDescriptor[];
|
|
295
|
+
}
|
|
296
|
+
/** `AgentStep.requests` element: a text or decision request. */
|
|
297
|
+
type AgentStepRequest = AgentRequest | AgentDecisionRequest;
|
|
298
|
+
/**
|
|
299
|
+
* Resolves one **text** request against a host's {@link AgentRequestExecutors}
|
|
300
|
+
* — merges the request's tools, dispatches to `generateText`/`streamText` per
|
|
301
|
+
* `mode`, and validates the result against the request's `outputSchema` if
|
|
302
|
+
* present. Accepts either a `kind: 'text'` `AgentEffect` (the thin-loop shape
|
|
303
|
+
* from `getAgentEffects`) or a legacy {@link AgentRequest} envelope.
|
|
304
|
+
* **Text-only**: passing a `kind: 'decision'` request throws, directing the
|
|
305
|
+
* caller to `resolveDecision(request, executors.decide, ...)` instead. By default
|
|
306
|
+
* returns the normalized output; pass `{ verbose: true }` to also get the
|
|
307
|
+
* raw executor result (tool calls, usage, finish reason — needed for
|
|
308
|
+
* observability and event-sourced replay).
|
|
309
|
+
*/
|
|
310
|
+
/** A `kind: 'text'` `AgentEffect` (structural, to avoid an import cycle with effects.ts). */
|
|
311
|
+
interface TextEffectLike {
|
|
312
|
+
kind: "text";
|
|
313
|
+
requestId: string;
|
|
314
|
+
request: AgentTextRequest;
|
|
315
|
+
mode?: AgentRequestMode;
|
|
316
|
+
}
|
|
317
|
+
declare function executeAgentRequest(request: AgentRequest | TextEffectLike, executors: Partial<AgentRequestExecutors>): Promise<unknown>;
|
|
318
|
+
declare function executeAgentRequest(request: AgentRequest | TextEffectLike, executors: Partial<AgentRequestExecutors>, options: {
|
|
319
|
+
verbose: true;
|
|
320
|
+
}): Promise<{
|
|
321
|
+
output: unknown;
|
|
322
|
+
raw: unknown;
|
|
323
|
+
}>;
|
|
324
|
+
//#endregion
|
|
373
325
|
//#region src/internal/state-request-pass.d.ts
|
|
374
326
|
/**
|
|
375
327
|
* One model request read off the machine's CURRENT snapshot by a
|
|
@@ -672,8 +624,15 @@ interface RunAgentOptions<TMachine extends AnyStateMachine> {
|
|
|
672
624
|
* kind, so e.g. a stream-only machine may pass `{ streamText }` alone.
|
|
673
625
|
*/
|
|
674
626
|
executors?: Partial<AgentRequestExecutors>;
|
|
675
|
-
/**
|
|
676
|
-
|
|
627
|
+
/**
|
|
628
|
+
* Machine input. Validated against the machine's declared input schema —
|
|
629
|
+
* defaults filled, transforms applied — before it reaches
|
|
630
|
+
* `createActor(machine, { input })` and the replayable event log; invalid
|
|
631
|
+
* input throws an {@link AgentError} with code `invalid-machine-input`.
|
|
632
|
+
* Typed as {@link AgentInputFrom}, so fields the schema defaults are optional
|
|
633
|
+
* here. Omit when resuming via `snapshot`.
|
|
634
|
+
*/
|
|
635
|
+
input?: AgentInputFrom<TMachine>;
|
|
677
636
|
/** A previously-settled run's `result.snapshot`, to resume from instead of starting fresh. Pair with `event` to deliver the event that unblocks the resumed idle state. */
|
|
678
637
|
snapshot?: Snapshot<unknown>;
|
|
679
638
|
/** An event to send immediately after starting/resuming the actor (e.g. the human's answer to an idle-state prompt). */
|
|
@@ -749,7 +708,7 @@ interface RunAgentOptions<TMachine extends AnyStateMachine> {
|
|
|
749
708
|
* runAgent uses to settle idle. Resolution order: this option (host override)
|
|
750
709
|
* → the machine-carried predicate declared via `setupAgent({ isSuspended })`
|
|
751
710
|
* → the timing heuristic (when neither is present). When the resolved
|
|
752
|
-
* predicate returns true and nothing is in flight (no live requests/
|
|
711
|
+
* predicate returns true and nothing is in flight (no live requests/
|
|
753
712
|
* invokes; the `agent.userInput` placeholder exemption still applies), runAgent
|
|
754
713
|
* settles idle immediately, without the `setTimeout` heuristic. It does NOT
|
|
755
714
|
* force-settle while agent work is in flight, and whole-machine idle semantics
|
|
@@ -902,12 +861,13 @@ type RunAgentOutcome<TMachine extends AnyStateMachine> = {
|
|
|
902
861
|
snapshot: SnapshotFrom<TMachine>; /** Present when the machine is waiting on unhandled `agent.userInput` invokes: one entry per pending invoke. */
|
|
903
862
|
pendingUserInputs?: PendingUserInput[];
|
|
904
863
|
/**
|
|
905
|
-
*
|
|
906
|
-
*
|
|
907
|
-
*
|
|
908
|
-
*
|
|
864
|
+
* The JSON-serializable persisted snapshot (in-flight children included,
|
|
865
|
+
* WITH their own state). Persist THIS one and resume with
|
|
866
|
+
* `runAgent(machine, { snapshot: persistedSnapshot, ... })` — the live
|
|
867
|
+
* `snapshot` above cannot round-trip active children, so resuming from
|
|
868
|
+
* it restarts every invoked child from scratch.
|
|
909
869
|
*/
|
|
910
|
-
persistedSnapshot
|
|
870
|
+
persistedSnapshot: Snapshot<unknown>;
|
|
911
871
|
} | {
|
|
912
872
|
status: "error";
|
|
913
873
|
cause: RunAgentErrorCause;
|
|
@@ -948,6 +908,20 @@ type RunAgentResult<TMachine extends AnyStateMachine> = RunAgentOutcome<TMachine
|
|
|
948
908
|
* - `'stopped'` — the actor was stopped externally (`status === 'stopped'`).
|
|
949
909
|
*/
|
|
950
910
|
type RunAgentErrorCause = "aborted" | "max-model-calls" | "decision-exhausted" | "machine" | "stopped";
|
|
911
|
+
/**
|
|
912
|
+
* The machine input a run accepts, which is the schema's *pre*-validation side.
|
|
913
|
+
*
|
|
914
|
+
* XState's `schemas` are types only — it never validates, and it resolves
|
|
915
|
+
* `schemas.input` to one type shared by `createActor`'s `input` option and the
|
|
916
|
+
* `context: ({ input })` factory. A schema field declared with a default
|
|
917
|
+
* therefore reads as required at the call site even though the caller is meant
|
|
918
|
+
* to omit it. `setupAgent` brands the machine's input type with its own schema
|
|
919
|
+
* ({@link WithAgentInputSchema}), so this recovers the looser caller-facing
|
|
920
|
+
* side while the factory keeps seeing the validated one. Machines with no
|
|
921
|
+
* declared input schema — and machines reached through `.provide(...)`, which
|
|
922
|
+
* drops the brand — fall back to xstate's `InputFrom`.
|
|
923
|
+
*/
|
|
924
|
+
type AgentInputFrom<TMachine extends AnyStateMachine> = InputFrom<TMachine> extends WithAgentInputSchema<infer TInputSchema> ? [TInputSchema] extends [StandardSchemaV1] ? InferInput<TInputSchema> : InputFrom<TMachine> : InputFrom<TMachine>;
|
|
951
925
|
/**
|
|
952
926
|
* Runs an agent machine to completion or idle: a `createActor` host that
|
|
953
927
|
* binds `options`' host executors onto the machine's `agent.*`/`TextLogic`/
|
|
@@ -1108,4 +1082,4 @@ declare function inspectTransitions(handler: (snapshot: AnyMachineSnapshot, acto
|
|
|
1108
1082
|
*/
|
|
1109
1083
|
declare function traceTransitions<TMachine extends AnyStateMachine = AnyStateMachine>(onTrace: (event: AgentTraceEvent<TMachine>) => void): (inspectionEvent: InspectionEvent) => void;
|
|
1110
1084
|
//#endregion
|
|
1111
|
-
export {
|
|
1085
|
+
export { AgentEffect as A, ReplayResult as B, traceTransitions as C, executeAgentRequest as D, AgentStepRequest as E, AgentReplayMachineMismatchError as F, initEntry as G, diffEventLogs as H, AgentUsageEvent as I, replay as K, CreateReplayEntryOptions as L, AgentEventLogDiff as M, AgentLogPatchOperation as N, AGENT_INIT_EVENT_TYPE as O, AgentReplayDivergenceError as P, GetAgentEffectsOptions as R, serializeTraceEvent as S, AgentRequest as T, getAgentEffects as U, createReplayEntry as V, getCallUsage as W, RunAgentResult as _, AgentInputFrom as a, inspectTransitions as b, AgentSnapshotVersionMismatchError as c, GenerateResult as d, InspectedActorRef as f, RunAgentOptions as g, RunAgentErrorCause as h, AgentIllegalResumeEventError as i, AgentEffectDiff as j, AGENT_USAGE_EVENT_TYPE as k, AgentTraceEvent as l, PendingUserInput as m, AgentActorSession as n, AgentMessageInfo as o, JsonSerializableTraceEvent as p, verifyReplay as q, AgentIdleError as r, AgentRunMeta as s, AGENT_TRACE_SCHEMA_VERSION as t, AgentUserInputExecutor as u, createAgentActor as v, AgentStateRequest as w, runAgent as x, generateResult as y, ReplayOptions as z };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { c as AgentTools, d as ChosenEvent, n as AgentMessage } from "./types-
|
|
1
|
+
import { T as WithAgentInputSchema, c as AgentTools, d as ChosenEvent, h as InferInput, n as AgentMessage, v as StandardSchemaV1 } from "./types-DFD28AWe.cjs";
|
|
2
2
|
import { t as AgentError } from "./errors-BQRk9eiZ.cjs";
|
|
3
|
-
import {
|
|
4
|
-
import { i as AgentLogEntry, o as JsonValue } from "./event-log-store-
|
|
3
|
+
import { B as AgentRequestOptions, M as AgentDecisionRequest, R as AgentEventDescriptor, V as AgentRequestSource, d as AgentTextRequest, f as AgentUsage, l as AgentRequestExecutors, p as AgentUserInput, t as AgentCallUsage, u as AgentRequestMode } from "./text-logic-DZW7XWy9.cjs";
|
|
4
|
+
import { i as AgentLogEntry, o as JsonValue } from "./event-log-store-CVd2eyRy.cjs";
|
|
5
5
|
import { AnyActorLogic, AnyActorRef, AnyMachineSnapshot, AnyStateMachine, AsyncActorLogic, EmittedFrom, EventFromLogic, EventObject, ExecutableActionObject, InputFrom, InspectionEvent, OutputFrom, Snapshot, SnapshotFrom, createActor } from "xstate";
|
|
6
6
|
|
|
7
7
|
//#region src/internal/registry.d.ts
|
|
@@ -9,94 +9,6 @@ type AgentExecutionOptions = Pick<AgentRequestOptions, "schemas" | "actors"> & {
|
|
|
9
9
|
models?: object;
|
|
10
10
|
};
|
|
11
11
|
//#endregion
|
|
12
|
-
//#region src/steps.d.ts
|
|
13
|
-
/**
|
|
14
|
-
* A pending text request surfaced by step discovery ({@link getAgentRequests}
|
|
15
|
-
* / {@link AgentStep.requests}): the machine has spawned a
|
|
16
|
-
* `TextLogic`-backed invoke and is waiting on its result. Resolve it with
|
|
17
|
-
* {@link executeAgentRequest} (or by hand, then feed the output into
|
|
18
|
-
* {@link resolveAgentStep} via `xstate.done.actor` + `actorId`).
|
|
19
|
-
*/
|
|
20
|
-
interface AgentRequest<TInput extends AgentTextRequest = AgentTextRequest> {
|
|
21
|
-
kind: "text";
|
|
22
|
-
id: string;
|
|
23
|
-
src: AgentRequestSource;
|
|
24
|
-
mode?: AgentRequestMode;
|
|
25
|
-
input: TInput;
|
|
26
|
-
tools: AgentTools;
|
|
27
|
-
events: AgentEventDescriptor[];
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* A pending **plan** request re-surfaced by step discovery: the machine
|
|
31
|
-
* invoked `agent.plan`, which applies an ordered sequence of legal events
|
|
32
|
-
* (each one a decision) rather than a single one. Unlike text/decision
|
|
33
|
-
* requests — surfaced once and resolved once — a plan request **re-surfaces on
|
|
34
|
-
* every step** while the plan is in flight, its `events`/`applied`/
|
|
35
|
-
* `stepsRemaining` updated each time, until it terminates.
|
|
36
|
-
*
|
|
37
|
-
* All fields are plain serializable data. Resolve ONE decision per step from
|
|
38
|
-
* `events` (via {@link resolveDecision}, wiring `canTake` to
|
|
39
|
-
* `snapshot.can` exactly like a single decision) then apply it: a real machine
|
|
40
|
-
* event advances the plan (the next step re-surfaces this request); the
|
|
41
|
-
* reserved `agent.plan.done` move, a `stopOn` event, an exhausted budget, or no
|
|
42
|
-
* legal events completes it (its invoke resolves with `{ steps, stopped }`).
|
|
43
|
-
* {@link resolveAgentRequests} does all of this natively — one decision (or one
|
|
44
|
-
* completion) per call.
|
|
45
|
-
*
|
|
46
|
-
* The in-progress plan state (`applied` trail + remaining budget) lives in the
|
|
47
|
-
* plan invoke child's own `createLogic` snapshot `context`
|
|
48
|
-
* (`children.<id>.snapshot.context`), so it survives a full JSON
|
|
49
|
-
* `getPersistedSnapshot` → restore round-trip: a host that persists the step
|
|
50
|
-
* after every event and reloads resumes the plan identically.
|
|
51
|
-
*/
|
|
52
|
-
interface AgentPlanRequest {
|
|
53
|
-
kind: "plan";
|
|
54
|
-
/** Durable invoke id of the `agent.plan` invoke. */
|
|
55
|
-
id: string;
|
|
56
|
-
/** Invoke src (`'agent.plan'` or a registered plan-logic source name). */
|
|
57
|
-
src: AgentRequestSource;
|
|
58
|
-
/** The resolved plan input (`model`/`system`/`prompt`/`allowedEvents`/`stopOn`/`maxSteps`/…). */
|
|
59
|
-
input: AgentPlanInput;
|
|
60
|
-
/**
|
|
61
|
-
* The legal candidates for the NEXT plan step: the currently
|
|
62
|
-
* snapshot-legal machine events (∩ declared `allowedEvents`) plus the
|
|
63
|
-
* reserved `agent.plan.done` move.
|
|
64
|
-
*/
|
|
65
|
-
events: AgentEventDescriptor[];
|
|
66
|
-
/** The events applied so far in this plan, in order (the trail). */
|
|
67
|
-
applied: ChosenEvent[];
|
|
68
|
-
/** How many more events the plan may apply (`maxSteps - applied.length`). */
|
|
69
|
-
stepsRemaining: number;
|
|
70
|
-
}
|
|
71
|
-
/** `AgentStep.requests` element: a text, decision, or plan request. */
|
|
72
|
-
type AgentStepRequest = AgentRequest | AgentDecisionRequest | AgentPlanRequest;
|
|
73
|
-
/**
|
|
74
|
-
* Resolves one **text** request against a host's {@link AgentRequestExecutors}
|
|
75
|
-
* — merges the request's tools, dispatches to `generateText`/`streamText` per
|
|
76
|
-
* `mode`, and validates the result against the request's `outputSchema` if
|
|
77
|
-
* present. Accepts either a `kind: 'text'` `AgentEffect` (the thin-loop shape
|
|
78
|
-
* from `getAgentEffects`) or a legacy {@link AgentRequest} envelope.
|
|
79
|
-
* **Text-only**: passing a `kind: 'decision'` request throws, directing the
|
|
80
|
-
* caller to `resolveDecision(request, executors.decide, ...)` instead. By default
|
|
81
|
-
* returns the normalized output; pass `{ verbose: true }` to also get the
|
|
82
|
-
* raw executor result (tool calls, usage, finish reason — needed for
|
|
83
|
-
* observability and event-sourced replay).
|
|
84
|
-
*/
|
|
85
|
-
/** A `kind: 'text'` `AgentEffect` (structural, to avoid an import cycle with effects.ts). */
|
|
86
|
-
interface TextEffectLike {
|
|
87
|
-
kind: "text";
|
|
88
|
-
requestId: string;
|
|
89
|
-
request: AgentTextRequest;
|
|
90
|
-
mode?: AgentRequestMode;
|
|
91
|
-
}
|
|
92
|
-
declare function executeAgentRequest(request: AgentRequest | TextEffectLike, executors: Partial<AgentRequestExecutors>): Promise<unknown>;
|
|
93
|
-
declare function executeAgentRequest(request: AgentRequest | TextEffectLike, executors: Partial<AgentRequestExecutors>, options: {
|
|
94
|
-
verbose: true;
|
|
95
|
-
}): Promise<{
|
|
96
|
-
output: unknown;
|
|
97
|
-
raw: unknown;
|
|
98
|
-
}>;
|
|
99
|
-
//#endregion
|
|
100
12
|
//#region src/effects.d.ts
|
|
101
13
|
/**
|
|
102
14
|
* One thing a host does at the frontier. Effect kinds mirror what a machine
|
|
@@ -109,8 +21,6 @@ declare function executeAgentRequest(request: AgentRequest | TextEffectLike, exe
|
|
|
109
21
|
* - `decision` — an `agent.decide`/`DecisionLogic` invoke. Resolve with
|
|
110
22
|
* `resolveDecision` and journal the CHOSEN machine event directly (a decision
|
|
111
23
|
* has no output value of its own; the chosen event advances the machine).
|
|
112
|
-
* - `plan` — an `agent.plan` invoke (multi-event). Drive it a step at a time
|
|
113
|
-
* (see `resolveAgentRequests`), journaling each applied event.
|
|
114
24
|
* - `task` — any other invoke/spawn: a plain host-run task keyed by `src` +
|
|
115
25
|
* `input`. Run it, then journal `toDoneEvent`/`toErrorEvent`.
|
|
116
26
|
* - `delay` — an `after(...)` timer. Schedule it; when it fires, journal
|
|
@@ -130,10 +40,6 @@ type AgentEffect = {
|
|
|
130
40
|
kind: "decision";
|
|
131
41
|
requestId: string;
|
|
132
42
|
request: AgentDecisionRequest;
|
|
133
|
-
} | {
|
|
134
|
-
kind: "plan";
|
|
135
|
-
requestId: string;
|
|
136
|
-
request: AgentPlanRequest;
|
|
137
43
|
} | {
|
|
138
44
|
kind: "task";
|
|
139
45
|
requestId: string;
|
|
@@ -195,7 +101,7 @@ interface AgentUsageEvent extends EventObject {
|
|
|
195
101
|
/** The settled call's token usage, as reported by the executor. */
|
|
196
102
|
usage: AgentCallUsage;
|
|
197
103
|
/** Which kind of request reported it. */
|
|
198
|
-
kind?: "text" | "decision"
|
|
104
|
+
kind?: "text" | "decision";
|
|
199
105
|
/** The reporting request's durable invoke id. */
|
|
200
106
|
id?: string;
|
|
201
107
|
/** The reporting request's invoke `src`. */
|
|
@@ -261,8 +167,8 @@ interface GetAgentEffectsOptions extends Partial<AgentExecutionOptions> {
|
|
|
261
167
|
* Ordering is load-bearing: a single transition's actions are emitted in
|
|
262
168
|
* document order (a custom entry action, a spawn, and a `sendTo` in that order
|
|
263
169
|
* yield `execute`, then `task`/agent effect, then `execute` — never a
|
|
264
|
-
* reordered set). Effects visible only on the snapshot (
|
|
265
|
-
*
|
|
170
|
+
* reordered set). Effects visible only on the snapshot (children spawned by
|
|
171
|
+
* an EARLIER transition that
|
|
266
172
|
* have not completed yet — the fan-out / crash-resume case) are appended after
|
|
267
173
|
* the action-derived effects, deduped by site id.
|
|
268
174
|
*
|
|
@@ -370,6 +276,52 @@ declare function verifyReplay<TMachine extends AnyStateMachine>(machine: TMachin
|
|
|
370
276
|
/** Structural event-tail, logical-state, and owed-effect comparison. */
|
|
371
277
|
declare function diffEventLogs<TMachine extends AnyStateMachine>(machine: TMachine, parentEntries: readonly AgentLogEntry[], forkEntries: readonly AgentLogEntry[], options?: ReplayOptions): AgentEventLogDiff<TMachine>;
|
|
372
278
|
//#endregion
|
|
279
|
+
//#region src/steps.d.ts
|
|
280
|
+
/**
|
|
281
|
+
* A pending text request surfaced by step discovery ({@link getAgentRequests}
|
|
282
|
+
* / {@link AgentStep.requests}): the machine has spawned a
|
|
283
|
+
* `TextLogic`-backed invoke and is waiting on its result. Resolve it with
|
|
284
|
+
* {@link executeAgentRequest} (or by hand, then feed the output into
|
|
285
|
+
* {@link resolveAgentStep} via `xstate.done.actor` + `actorId`).
|
|
286
|
+
*/
|
|
287
|
+
interface AgentRequest<TInput extends AgentTextRequest = AgentTextRequest> {
|
|
288
|
+
kind: "text";
|
|
289
|
+
id: string;
|
|
290
|
+
src: AgentRequestSource;
|
|
291
|
+
mode?: AgentRequestMode;
|
|
292
|
+
input: TInput;
|
|
293
|
+
tools: AgentTools;
|
|
294
|
+
events: AgentEventDescriptor[];
|
|
295
|
+
}
|
|
296
|
+
/** `AgentStep.requests` element: a text or decision request. */
|
|
297
|
+
type AgentStepRequest = AgentRequest | AgentDecisionRequest;
|
|
298
|
+
/**
|
|
299
|
+
* Resolves one **text** request against a host's {@link AgentRequestExecutors}
|
|
300
|
+
* — merges the request's tools, dispatches to `generateText`/`streamText` per
|
|
301
|
+
* `mode`, and validates the result against the request's `outputSchema` if
|
|
302
|
+
* present. Accepts either a `kind: 'text'` `AgentEffect` (the thin-loop shape
|
|
303
|
+
* from `getAgentEffects`) or a legacy {@link AgentRequest} envelope.
|
|
304
|
+
* **Text-only**: passing a `kind: 'decision'` request throws, directing the
|
|
305
|
+
* caller to `resolveDecision(request, executors.decide, ...)` instead. By default
|
|
306
|
+
* returns the normalized output; pass `{ verbose: true }` to also get the
|
|
307
|
+
* raw executor result (tool calls, usage, finish reason — needed for
|
|
308
|
+
* observability and event-sourced replay).
|
|
309
|
+
*/
|
|
310
|
+
/** A `kind: 'text'` `AgentEffect` (structural, to avoid an import cycle with effects.ts). */
|
|
311
|
+
interface TextEffectLike {
|
|
312
|
+
kind: "text";
|
|
313
|
+
requestId: string;
|
|
314
|
+
request: AgentTextRequest;
|
|
315
|
+
mode?: AgentRequestMode;
|
|
316
|
+
}
|
|
317
|
+
declare function executeAgentRequest(request: AgentRequest | TextEffectLike, executors: Partial<AgentRequestExecutors>): Promise<unknown>;
|
|
318
|
+
declare function executeAgentRequest(request: AgentRequest | TextEffectLike, executors: Partial<AgentRequestExecutors>, options: {
|
|
319
|
+
verbose: true;
|
|
320
|
+
}): Promise<{
|
|
321
|
+
output: unknown;
|
|
322
|
+
raw: unknown;
|
|
323
|
+
}>;
|
|
324
|
+
//#endregion
|
|
373
325
|
//#region src/internal/state-request-pass.d.ts
|
|
374
326
|
/**
|
|
375
327
|
* One model request read off the machine's CURRENT snapshot by a
|
|
@@ -672,8 +624,15 @@ interface RunAgentOptions<TMachine extends AnyStateMachine> {
|
|
|
672
624
|
* kind, so e.g. a stream-only machine may pass `{ streamText }` alone.
|
|
673
625
|
*/
|
|
674
626
|
executors?: Partial<AgentRequestExecutors>;
|
|
675
|
-
/**
|
|
676
|
-
|
|
627
|
+
/**
|
|
628
|
+
* Machine input. Validated against the machine's declared input schema —
|
|
629
|
+
* defaults filled, transforms applied — before it reaches
|
|
630
|
+
* `createActor(machine, { input })` and the replayable event log; invalid
|
|
631
|
+
* input throws an {@link AgentError} with code `invalid-machine-input`.
|
|
632
|
+
* Typed as {@link AgentInputFrom}, so fields the schema defaults are optional
|
|
633
|
+
* here. Omit when resuming via `snapshot`.
|
|
634
|
+
*/
|
|
635
|
+
input?: AgentInputFrom<TMachine>;
|
|
677
636
|
/** A previously-settled run's `result.snapshot`, to resume from instead of starting fresh. Pair with `event` to deliver the event that unblocks the resumed idle state. */
|
|
678
637
|
snapshot?: Snapshot<unknown>;
|
|
679
638
|
/** An event to send immediately after starting/resuming the actor (e.g. the human's answer to an idle-state prompt). */
|
|
@@ -749,7 +708,7 @@ interface RunAgentOptions<TMachine extends AnyStateMachine> {
|
|
|
749
708
|
* runAgent uses to settle idle. Resolution order: this option (host override)
|
|
750
709
|
* → the machine-carried predicate declared via `setupAgent({ isSuspended })`
|
|
751
710
|
* → the timing heuristic (when neither is present). When the resolved
|
|
752
|
-
* predicate returns true and nothing is in flight (no live requests/
|
|
711
|
+
* predicate returns true and nothing is in flight (no live requests/
|
|
753
712
|
* invokes; the `agent.userInput` placeholder exemption still applies), runAgent
|
|
754
713
|
* settles idle immediately, without the `setTimeout` heuristic. It does NOT
|
|
755
714
|
* force-settle while agent work is in flight, and whole-machine idle semantics
|
|
@@ -902,12 +861,13 @@ type RunAgentOutcome<TMachine extends AnyStateMachine> = {
|
|
|
902
861
|
snapshot: SnapshotFrom<TMachine>; /** Present when the machine is waiting on unhandled `agent.userInput` invokes: one entry per pending invoke. */
|
|
903
862
|
pendingUserInputs?: PendingUserInput[];
|
|
904
863
|
/**
|
|
905
|
-
*
|
|
906
|
-
*
|
|
907
|
-
*
|
|
908
|
-
*
|
|
864
|
+
* The JSON-serializable persisted snapshot (in-flight children included,
|
|
865
|
+
* WITH their own state). Persist THIS one and resume with
|
|
866
|
+
* `runAgent(machine, { snapshot: persistedSnapshot, ... })` — the live
|
|
867
|
+
* `snapshot` above cannot round-trip active children, so resuming from
|
|
868
|
+
* it restarts every invoked child from scratch.
|
|
909
869
|
*/
|
|
910
|
-
persistedSnapshot
|
|
870
|
+
persistedSnapshot: Snapshot<unknown>;
|
|
911
871
|
} | {
|
|
912
872
|
status: "error";
|
|
913
873
|
cause: RunAgentErrorCause;
|
|
@@ -948,6 +908,20 @@ type RunAgentResult<TMachine extends AnyStateMachine> = RunAgentOutcome<TMachine
|
|
|
948
908
|
* - `'stopped'` — the actor was stopped externally (`status === 'stopped'`).
|
|
949
909
|
*/
|
|
950
910
|
type RunAgentErrorCause = "aborted" | "max-model-calls" | "decision-exhausted" | "machine" | "stopped";
|
|
911
|
+
/**
|
|
912
|
+
* The machine input a run accepts, which is the schema's *pre*-validation side.
|
|
913
|
+
*
|
|
914
|
+
* XState's `schemas` are types only — it never validates, and it resolves
|
|
915
|
+
* `schemas.input` to one type shared by `createActor`'s `input` option and the
|
|
916
|
+
* `context: ({ input })` factory. A schema field declared with a default
|
|
917
|
+
* therefore reads as required at the call site even though the caller is meant
|
|
918
|
+
* to omit it. `setupAgent` brands the machine's input type with its own schema
|
|
919
|
+
* ({@link WithAgentInputSchema}), so this recovers the looser caller-facing
|
|
920
|
+
* side while the factory keeps seeing the validated one. Machines with no
|
|
921
|
+
* declared input schema — and machines reached through `.provide(...)`, which
|
|
922
|
+
* drops the brand — fall back to xstate's `InputFrom`.
|
|
923
|
+
*/
|
|
924
|
+
type AgentInputFrom<TMachine extends AnyStateMachine> = InputFrom<TMachine> extends WithAgentInputSchema<infer TInputSchema> ? [TInputSchema] extends [StandardSchemaV1] ? InferInput<TInputSchema> : InputFrom<TMachine> : InputFrom<TMachine>;
|
|
951
925
|
/**
|
|
952
926
|
* Runs an agent machine to completion or idle: a `createActor` host that
|
|
953
927
|
* binds `options`' host executors onto the machine's `agent.*`/`TextLogic`/
|
|
@@ -1108,4 +1082,4 @@ declare function inspectTransitions(handler: (snapshot: AnyMachineSnapshot, acto
|
|
|
1108
1082
|
*/
|
|
1109
1083
|
declare function traceTransitions<TMachine extends AnyStateMachine = AnyStateMachine>(onTrace: (event: AgentTraceEvent<TMachine>) => void): (inspectionEvent: InspectionEvent) => void;
|
|
1110
1084
|
//#endregion
|
|
1111
|
-
export {
|
|
1085
|
+
export { AgentEffect as A, ReplayResult as B, traceTransitions as C, executeAgentRequest as D, AgentStepRequest as E, AgentReplayMachineMismatchError as F, initEntry as G, diffEventLogs as H, AgentUsageEvent as I, replay as K, CreateReplayEntryOptions as L, AgentEventLogDiff as M, AgentLogPatchOperation as N, AGENT_INIT_EVENT_TYPE as O, AgentReplayDivergenceError as P, GetAgentEffectsOptions as R, serializeTraceEvent as S, AgentRequest as T, getAgentEffects as U, createReplayEntry as V, getCallUsage as W, RunAgentResult as _, AgentInputFrom as a, inspectTransitions as b, AgentSnapshotVersionMismatchError as c, GenerateResult as d, InspectedActorRef as f, RunAgentOptions as g, RunAgentErrorCause as h, AgentIllegalResumeEventError as i, AgentEffectDiff as j, AGENT_USAGE_EVENT_TYPE as k, AgentTraceEvent as l, PendingUserInput as m, AgentActorSession as n, AgentMessageInfo as o, JsonSerializableTraceEvent as p, verifyReplay as q, AgentIdleError as r, AgentRunMeta as s, AGENT_TRACE_SCHEMA_VERSION as t, AgentUserInputExecutor as u, createAgentActor as v, AgentStateRequest as w, runAgent as x, generateResult as y, ReplayOptions as z };
|