@statelyai/agent 2.0.0-alpha.13 → 2.0.0-alpha.14
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 +31 -155
- package/dist/index.d.cts +8 -12
- package/dist/index.d.mts +8 -12
- package/dist/index.mjs +33 -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-2MnlQTkB.d.mts} +60 -107
- package/dist/{run-agent-DnvtcnTZ.d.cts → run-agent-BlqKwHIF.d.cts} +60 -107
- 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-C5kbjaDz.d.mts} +5 -93
- package/dist/{text-logic-BDxwQNsD.d.cts → text-logic-Jkilp1Ie.d.cts} +5 -93
- package/dist/{types-QbEfCVny.d.cts → types-9Bqg5rZB.d.mts} +2 -2
- package/dist/{types-_FXoFBGO.d.mts → types-rMe7x6NR.d.cts} +2 -2
- 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
package/dist/otel.d.mts
CHANGED
package/dist/otel.mjs
CHANGED
|
@@ -32,23 +32,15 @@ function usageAttributes(usage) {
|
|
|
32
32
|
}
|
|
33
33
|
return attributes;
|
|
34
34
|
}
|
|
35
|
-
/** The model ref a request targets: text
|
|
35
|
+
/** The model ref a request targets: text carries it on `input`, a decision inline. */
|
|
36
36
|
function requestModel(request) {
|
|
37
37
|
const model = request.kind === "decision" ? request.model : request.input?.model;
|
|
38
38
|
return typeof model === "string" ? model : void 0;
|
|
39
39
|
}
|
|
40
|
-
/** The `src` a text
|
|
40
|
+
/** The `src` a text invoke was declared with; a decision has none. */
|
|
41
41
|
function requestSrc(request) {
|
|
42
42
|
return "src" in request && typeof request.src === "string" ? request.src : void 0;
|
|
43
43
|
}
|
|
44
|
-
/**
|
|
45
|
-
* `gen_ai.operation.name` for a request. A plan request drives the machine
|
|
46
|
-
* through a sequence of events, which semconv names `plan`; text and decision
|
|
47
|
-
* requests are both single chat completions.
|
|
48
|
-
*/
|
|
49
|
-
function operationName(request) {
|
|
50
|
-
return request.kind === "plan" ? "plan" : "chat";
|
|
51
|
-
}
|
|
52
44
|
/** Best-effort JSON, never throws — content capture must not break a run. */
|
|
53
45
|
function toJson(value) {
|
|
54
46
|
try {
|
|
@@ -161,10 +153,9 @@ function createOtelTraceHandler(options) {
|
|
|
161
153
|
const request = event.request;
|
|
162
154
|
endRequest(request.id, () => {});
|
|
163
155
|
const model = requestModel(request);
|
|
164
|
-
const operation = operationName(request);
|
|
165
156
|
const attributes = {
|
|
166
157
|
...baseAttributes,
|
|
167
|
-
"gen_ai.operation.name":
|
|
158
|
+
"gen_ai.operation.name": "chat",
|
|
168
159
|
"agent.run_id": event.runId,
|
|
169
160
|
"agent.request_id": request.id,
|
|
170
161
|
"agent.request_kind": request.kind
|
|
@@ -181,9 +172,8 @@ function createOtelTraceHandler(options) {
|
|
|
181
172
|
const json = toJson(input.messages ?? input.prompt);
|
|
182
173
|
if (json !== void 0) attributes["gen_ai.input.messages"] = json;
|
|
183
174
|
}
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
kind: operation === "plan" ? SpanKind.INTERNAL : SpanKind.CLIENT,
|
|
175
|
+
const span = tracer.startSpan(`chat ${model ?? request.kind}`, {
|
|
176
|
+
kind: SpanKind.CLIENT,
|
|
187
177
|
attributes
|
|
188
178
|
}, runFor(event).context);
|
|
189
179
|
requests.set(request.id, {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { c as AgentTools, d as ChosenEvent, n as AgentMessage } from "./types-
|
|
1
|
+
import { c as AgentTools, d as ChosenEvent, n as AgentMessage } from "./types-9Bqg5rZB.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-C5kbjaDz.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
|
|
@@ -749,7 +701,7 @@ interface RunAgentOptions<TMachine extends AnyStateMachine> {
|
|
|
749
701
|
* runAgent uses to settle idle. Resolution order: this option (host override)
|
|
750
702
|
* → the machine-carried predicate declared via `setupAgent({ isSuspended })`
|
|
751
703
|
* → the timing heuristic (when neither is present). When the resolved
|
|
752
|
-
* predicate returns true and nothing is in flight (no live requests/
|
|
704
|
+
* predicate returns true and nothing is in flight (no live requests/
|
|
753
705
|
* invokes; the `agent.userInput` placeholder exemption still applies), runAgent
|
|
754
706
|
* settles idle immediately, without the `setTimeout` heuristic. It does NOT
|
|
755
707
|
* force-settle while agent work is in flight, and whole-machine idle semantics
|
|
@@ -902,12 +854,13 @@ type RunAgentOutcome<TMachine extends AnyStateMachine> = {
|
|
|
902
854
|
snapshot: SnapshotFrom<TMachine>; /** Present when the machine is waiting on unhandled `agent.userInput` invokes: one entry per pending invoke. */
|
|
903
855
|
pendingUserInputs?: PendingUserInput[];
|
|
904
856
|
/**
|
|
905
|
-
*
|
|
906
|
-
*
|
|
907
|
-
*
|
|
908
|
-
*
|
|
857
|
+
* The JSON-serializable persisted snapshot (in-flight children included,
|
|
858
|
+
* WITH their own state). Persist THIS one and resume with
|
|
859
|
+
* `runAgent(machine, { snapshot: persistedSnapshot, ... })` — the live
|
|
860
|
+
* `snapshot` above cannot round-trip active children, so resuming from
|
|
861
|
+
* it restarts every invoked child from scratch.
|
|
909
862
|
*/
|
|
910
|
-
persistedSnapshot
|
|
863
|
+
persistedSnapshot: Snapshot<unknown>;
|
|
911
864
|
} | {
|
|
912
865
|
status: "error";
|
|
913
866
|
cause: RunAgentErrorCause;
|
|
@@ -1108,4 +1061,4 @@ declare function inspectTransitions(handler: (snapshot: AnyMachineSnapshot, acto
|
|
|
1108
1061
|
*/
|
|
1109
1062
|
declare function traceTransitions<TMachine extends AnyStateMachine = AnyStateMachine>(onTrace: (event: AgentTraceEvent<TMachine>) => void): (inspectionEvent: InspectionEvent) => void;
|
|
1110
1063
|
//#endregion
|
|
1111
|
-
export {
|
|
1064
|
+
export { AgentEffectDiff as A, createReplayEntry as B, AgentStateRequest as C, AGENT_INIT_EVENT_TYPE as D, executeAgentRequest as E, AgentUsageEvent as F, replay as G, getAgentEffects as H, CreateReplayEntryOptions as I, verifyReplay as K, GetAgentEffectsOptions as L, AgentLogPatchOperation as M, AgentReplayDivergenceError as N, AGENT_USAGE_EVENT_TYPE as O, AgentReplayMachineMismatchError as P, ReplayOptions as R, traceTransitions as S, AgentStepRequest as T, getCallUsage as U, diffEventLogs as V, initEntry as W, createAgentActor as _, AgentMessageInfo as a, runAgent as b, AgentTraceEvent as c, InspectedActorRef as d, JsonSerializableTraceEvent as f, RunAgentResult as g, RunAgentOptions as h, AgentIllegalResumeEventError as i, AgentEventLogDiff as j, AgentEffect as k, AgentUserInputExecutor as l, RunAgentErrorCause as m, AgentActorSession as n, AgentRunMeta as o, PendingUserInput as p, AgentIdleError as r, AgentSnapshotVersionMismatchError as s, AGENT_TRACE_SCHEMA_VERSION as t, GenerateResult as u, generateResult as v, AgentRequest as w, serializeTraceEvent as x, inspectTransitions as y, ReplayResult as z };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { c as AgentTools, d as ChosenEvent, n as AgentMessage } from "./types-
|
|
1
|
+
import { c as AgentTools, d as ChosenEvent, n as AgentMessage } from "./types-rMe7x6NR.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-Jkilp1Ie.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
|
|
@@ -749,7 +701,7 @@ interface RunAgentOptions<TMachine extends AnyStateMachine> {
|
|
|
749
701
|
* runAgent uses to settle idle. Resolution order: this option (host override)
|
|
750
702
|
* → the machine-carried predicate declared via `setupAgent({ isSuspended })`
|
|
751
703
|
* → the timing heuristic (when neither is present). When the resolved
|
|
752
|
-
* predicate returns true and nothing is in flight (no live requests/
|
|
704
|
+
* predicate returns true and nothing is in flight (no live requests/
|
|
753
705
|
* invokes; the `agent.userInput` placeholder exemption still applies), runAgent
|
|
754
706
|
* settles idle immediately, without the `setTimeout` heuristic. It does NOT
|
|
755
707
|
* force-settle while agent work is in flight, and whole-machine idle semantics
|
|
@@ -902,12 +854,13 @@ type RunAgentOutcome<TMachine extends AnyStateMachine> = {
|
|
|
902
854
|
snapshot: SnapshotFrom<TMachine>; /** Present when the machine is waiting on unhandled `agent.userInput` invokes: one entry per pending invoke. */
|
|
903
855
|
pendingUserInputs?: PendingUserInput[];
|
|
904
856
|
/**
|
|
905
|
-
*
|
|
906
|
-
*
|
|
907
|
-
*
|
|
908
|
-
*
|
|
857
|
+
* The JSON-serializable persisted snapshot (in-flight children included,
|
|
858
|
+
* WITH their own state). Persist THIS one and resume with
|
|
859
|
+
* `runAgent(machine, { snapshot: persistedSnapshot, ... })` — the live
|
|
860
|
+
* `snapshot` above cannot round-trip active children, so resuming from
|
|
861
|
+
* it restarts every invoked child from scratch.
|
|
909
862
|
*/
|
|
910
|
-
persistedSnapshot
|
|
863
|
+
persistedSnapshot: Snapshot<unknown>;
|
|
911
864
|
} | {
|
|
912
865
|
status: "error";
|
|
913
866
|
cause: RunAgentErrorCause;
|
|
@@ -1108,4 +1061,4 @@ declare function inspectTransitions(handler: (snapshot: AnyMachineSnapshot, acto
|
|
|
1108
1061
|
*/
|
|
1109
1062
|
declare function traceTransitions<TMachine extends AnyStateMachine = AnyStateMachine>(onTrace: (event: AgentTraceEvent<TMachine>) => void): (inspectionEvent: InspectionEvent) => void;
|
|
1110
1063
|
//#endregion
|
|
1111
|
-
export {
|
|
1064
|
+
export { AgentEffectDiff as A, createReplayEntry as B, AgentStateRequest as C, AGENT_INIT_EVENT_TYPE as D, executeAgentRequest as E, AgentUsageEvent as F, replay as G, getAgentEffects as H, CreateReplayEntryOptions as I, verifyReplay as K, GetAgentEffectsOptions as L, AgentLogPatchOperation as M, AgentReplayDivergenceError as N, AGENT_USAGE_EVENT_TYPE as O, AgentReplayMachineMismatchError as P, ReplayOptions as R, traceTransitions as S, AgentStepRequest as T, getCallUsage as U, diffEventLogs as V, initEntry as W, createAgentActor as _, AgentMessageInfo as a, runAgent as b, AgentTraceEvent as c, InspectedActorRef as d, JsonSerializableTraceEvent as f, RunAgentResult as g, RunAgentOptions as h, AgentIllegalResumeEventError as i, AgentEventLogDiff as j, AgentEffect as k, AgentUserInputExecutor as l, RunAgentErrorCause as m, AgentActorSession as n, AgentRunMeta as o, PendingUserInput as p, AgentIdleError as r, AgentSnapshotVersionMismatchError as s, AGENT_TRACE_SCHEMA_VERSION as t, GenerateResult as u, generateResult as v, AgentRequest as w, serializeTraceEvent as x, inspectTransitions as y, ReplayResult as z };
|