@statelyai/agent 2.0.0-alpha.39 → 2.0.0-alpha.41
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/CHANGELOG.md +35 -0
- package/dist/evals.cjs +1 -1
- package/dist/evals.d.cts +1 -1
- package/dist/evals.d.mts +1 -1
- package/dist/evals.mjs +1 -1
- package/dist/index.cjs +17 -10
- package/dist/index.d.cts +29 -12
- package/dist/index.d.mts +29 -12
- package/dist/index.mjs +17 -10
- package/dist/machine-input-0YTPcnCm.mjs +27 -0
- package/dist/machine-input-DU3owLUr.cjs +32 -0
- package/dist/otel.d.cts +1 -1
- package/dist/otel.d.mts +1 -1
- package/dist/policy.cjs +1 -1
- package/dist/policy.d.cts +1 -1
- package/dist/policy.d.mts +1 -1
- package/dist/policy.mjs +1 -1
- package/dist/{run-agent-Dq0oSFRm.d.cts → run-agent-BO9RSb7i.d.cts} +16 -7
- package/dist/{run-agent-DHlq87G7.d.mts → run-agent-BxkeuAYR.d.mts} +16 -7
- package/dist/{run-agent-De3xVFGw.cjs → run-agent-COdR9FpM.cjs} +26 -27
- package/dist/{run-agent-BE7DOWD6.mjs → run-agent-D3gW9wfd.mjs} +27 -28
- package/dist/testing.cjs +377 -18
- package/dist/testing.d.cts +181 -5
- package/dist/testing.d.mts +181 -5
- package/dist/testing.mjs +377 -20
- package/dist/{utils-DAclF-kw.d.cts → utils-BMxUcjay.d.cts} +57 -1
- package/dist/{utils-B8k-YPYI.d.mts → utils-BV7pxhD-.d.mts} +57 -1
- package/dist/validate.cjs +1 -1
- package/dist/validate.d.cts +1 -1
- package/dist/validate.d.mts +1 -1
- package/dist/validate.mjs +1 -1
- package/dist/{verify-H3Pr2gNw.d.mts → verify-C1UU84eq.d.mts} +22 -2
- package/dist/{verify-BM0sGmsf.d.cts → verify-DlN6tK8_.d.cts} +22 -2
- package/dist/{verify-Dek6gHCS.cjs → verify-IVYsI026.cjs} +84 -17
- package/dist/{verify-CWlg3j7B.mjs → verify-lKyu3buS.mjs} +84 -17
- package/docs/evals.md +1 -0
- package/docs/hosts.md +5 -3
- package/docs/index.md +2 -2
- package/docs/meta.json +1 -0
- package/docs/strangler-fig.md +36 -29
- package/docs/verify-in-place.md +326 -0
- package/docs/verify.md +70 -4
- package/llms.txt +1 -0
- package/package.json +1 -1
- package/readme.md +1 -0
|
@@ -369,9 +369,12 @@ interface AgentRuntimeOptions<TMachine extends AnyStateMachine> {
|
|
|
369
369
|
* when the machine has no transition for `event`, when the loop never
|
|
370
370
|
* applies it, when the machine settles still `active` after handling it,
|
|
371
371
|
* or when `timeout` milliseconds pass while it is still working.
|
|
372
|
+
*
|
|
373
|
+
* `event` can be a function of `signal.reason`, so aborts with different
|
|
374
|
+
* reasons (a pause and a stop) send different events.
|
|
372
375
|
*/
|
|
373
376
|
abortWith?: {
|
|
374
|
-
event: EventFromLogic<TMachine
|
|
377
|
+
event: EventFromLogic<TMachine> | ((reason: unknown) => EventFromLogic<TMachine>);
|
|
375
378
|
timeout?: number;
|
|
376
379
|
};
|
|
377
380
|
/** Where root `after` timers live. Default `"in-process"`. See {@link AgentTimerScheduler}. */
|
|
@@ -617,7 +620,8 @@ interface AgentRuntime<TMachine extends AnyStateMachine> {
|
|
|
617
620
|
nextEvent(): Promise<EventFromLogic<TMachine> | undefined>;
|
|
618
621
|
/**
|
|
619
622
|
* Delivers a host event (a cancel, a user message, a host-owned timer) into
|
|
620
|
-
* the run
|
|
623
|
+
* the run, behind earlier host sends and ahead of completions not yet handed
|
|
624
|
+
* out. Asynchronous: `send`
|
|
621
625
|
* only enqueues; the machine moves when the host loop's next `nextEvent()`
|
|
622
626
|
* hands the event out (a parked `nextEvent()` resolves with it) and the loop
|
|
623
627
|
* calls `transition`. A host that needs the event applied before it returns
|
|
@@ -631,8 +635,9 @@ interface AgentRuntime<TMachine extends AnyStateMachine> {
|
|
|
631
635
|
*
|
|
632
636
|
* - Before `start`: queued; the first `nextEvent()` hands it out (after the
|
|
633
637
|
* init event a host delivers itself). Returns `true`.
|
|
634
|
-
* - While an effect, child or timer is in flight: queued
|
|
635
|
-
*
|
|
638
|
+
* - While an effect, child or timer is in flight: queued behind earlier
|
|
639
|
+
* host sends, and handed out before any completion the run holds, so
|
|
640
|
+
* actors that settle in microtasks cannot keep it waiting. Returns `true`.
|
|
636
641
|
* - After the run stopped (cancelled, aborted, journal failure) or the
|
|
637
642
|
* machine left `active` (done, error): dropped. Returns `false`.
|
|
638
643
|
* - After `finish`: throws {@link AgentError} `runtime-finished`.
|
|
@@ -682,10 +687,13 @@ interface AgentRunOptions<TMachine extends AnyStateMachine> extends AgentRuntime
|
|
|
682
687
|
* A host that cannot block for the answer (a request handler, a durable
|
|
683
688
|
* object) leaves this out, stores `result.persist()`, and calls
|
|
684
689
|
* {@link runAgent} again later with `{ snapshot, event }`.
|
|
690
|
+
*
|
|
691
|
+
* `"inbox"`: each idle state waits for the next `inbox.send` (a resume
|
|
692
|
+
* after a pause), with no answer of its own. Needs `inbox`.
|
|
685
693
|
*/
|
|
686
|
-
onIdle?: (snapshot: SnapshotFrom<TMachine>, options: {
|
|
694
|
+
onIdle?: ((snapshot: SnapshotFrom<TMachine>, options: {
|
|
687
695
|
signal: AbortSignal | undefined;
|
|
688
|
-
}) => Promise<EventFromLogic<TMachine> | AgentTimerEvent
|
|
696
|
+
}) => Promise<EventFromLogic<TMachine> | AgentTimerEvent>) | "inbox";
|
|
689
697
|
/**
|
|
690
698
|
* A send handle for events pushed mid-run (a cancel, a user message), from
|
|
691
699
|
* {@link createAgentInbox}. Each `inbox.send` goes to the current pass's
|
|
@@ -695,7 +703,8 @@ interface AgentRunOptions<TMachine extends AnyStateMachine> extends AgentRuntime
|
|
|
695
703
|
* dropped). Without `onIdle`, the run returns at the first idle state
|
|
696
704
|
* (an event that lands while that pass is finishing still resumes it), and
|
|
697
705
|
* a `send` after `runAgent` returns gets `false`: a pause that waits for a
|
|
698
|
-
* resume needs `onIdle` as well
|
|
706
|
+
* resume needs `onIdle` as well (`onIdle: "inbox"` when only the inbox
|
|
707
|
+
* answers). One inbox serves one `runAgent` call.
|
|
699
708
|
*/
|
|
700
709
|
inbox?: AgentInbox<EventFromLogic<TMachine>>;
|
|
701
710
|
}
|
|
@@ -369,9 +369,12 @@ interface AgentRuntimeOptions<TMachine extends AnyStateMachine> {
|
|
|
369
369
|
* when the machine has no transition for `event`, when the loop never
|
|
370
370
|
* applies it, when the machine settles still `active` after handling it,
|
|
371
371
|
* or when `timeout` milliseconds pass while it is still working.
|
|
372
|
+
*
|
|
373
|
+
* `event` can be a function of `signal.reason`, so aborts with different
|
|
374
|
+
* reasons (a pause and a stop) send different events.
|
|
372
375
|
*/
|
|
373
376
|
abortWith?: {
|
|
374
|
-
event: EventFromLogic<TMachine
|
|
377
|
+
event: EventFromLogic<TMachine> | ((reason: unknown) => EventFromLogic<TMachine>);
|
|
375
378
|
timeout?: number;
|
|
376
379
|
};
|
|
377
380
|
/** Where root `after` timers live. Default `"in-process"`. See {@link AgentTimerScheduler}. */
|
|
@@ -617,7 +620,8 @@ interface AgentRuntime<TMachine extends AnyStateMachine> {
|
|
|
617
620
|
nextEvent(): Promise<EventFromLogic<TMachine> | undefined>;
|
|
618
621
|
/**
|
|
619
622
|
* Delivers a host event (a cancel, a user message, a host-owned timer) into
|
|
620
|
-
* the run
|
|
623
|
+
* the run, behind earlier host sends and ahead of completions not yet handed
|
|
624
|
+
* out. Asynchronous: `send`
|
|
621
625
|
* only enqueues; the machine moves when the host loop's next `nextEvent()`
|
|
622
626
|
* hands the event out (a parked `nextEvent()` resolves with it) and the loop
|
|
623
627
|
* calls `transition`. A host that needs the event applied before it returns
|
|
@@ -631,8 +635,9 @@ interface AgentRuntime<TMachine extends AnyStateMachine> {
|
|
|
631
635
|
*
|
|
632
636
|
* - Before `start`: queued; the first `nextEvent()` hands it out (after the
|
|
633
637
|
* init event a host delivers itself). Returns `true`.
|
|
634
|
-
* - While an effect, child or timer is in flight: queued
|
|
635
|
-
*
|
|
638
|
+
* - While an effect, child or timer is in flight: queued behind earlier
|
|
639
|
+
* host sends, and handed out before any completion the run holds, so
|
|
640
|
+
* actors that settle in microtasks cannot keep it waiting. Returns `true`.
|
|
636
641
|
* - After the run stopped (cancelled, aborted, journal failure) or the
|
|
637
642
|
* machine left `active` (done, error): dropped. Returns `false`.
|
|
638
643
|
* - After `finish`: throws {@link AgentError} `runtime-finished`.
|
|
@@ -682,10 +687,13 @@ interface AgentRunOptions<TMachine extends AnyStateMachine> extends AgentRuntime
|
|
|
682
687
|
* A host that cannot block for the answer (a request handler, a durable
|
|
683
688
|
* object) leaves this out, stores `result.persist()`, and calls
|
|
684
689
|
* {@link runAgent} again later with `{ snapshot, event }`.
|
|
690
|
+
*
|
|
691
|
+
* `"inbox"`: each idle state waits for the next `inbox.send` (a resume
|
|
692
|
+
* after a pause), with no answer of its own. Needs `inbox`.
|
|
685
693
|
*/
|
|
686
|
-
onIdle?: (snapshot: SnapshotFrom<TMachine>, options: {
|
|
694
|
+
onIdle?: ((snapshot: SnapshotFrom<TMachine>, options: {
|
|
687
695
|
signal: AbortSignal | undefined;
|
|
688
|
-
}) => Promise<EventFromLogic<TMachine> | AgentTimerEvent
|
|
696
|
+
}) => Promise<EventFromLogic<TMachine> | AgentTimerEvent>) | "inbox";
|
|
689
697
|
/**
|
|
690
698
|
* A send handle for events pushed mid-run (a cancel, a user message), from
|
|
691
699
|
* {@link createAgentInbox}. Each `inbox.send` goes to the current pass's
|
|
@@ -695,7 +703,8 @@ interface AgentRunOptions<TMachine extends AnyStateMachine> extends AgentRuntime
|
|
|
695
703
|
* dropped). Without `onIdle`, the run returns at the first idle state
|
|
696
704
|
* (an event that lands while that pass is finishing still resumes it), and
|
|
697
705
|
* a `send` after `runAgent` returns gets `false`: a pause that waits for a
|
|
698
|
-
* resume needs `onIdle` as well
|
|
706
|
+
* resume needs `onIdle` as well (`onIdle: "inbox"` when only the inbox
|
|
707
|
+
* answers). One inbox serves one `runAgent` call.
|
|
699
708
|
*/
|
|
700
709
|
inbox?: AgentInbox<EventFromLogic<TMachine>>;
|
|
701
710
|
}
|
|
@@ -2,6 +2,7 @@ const require_event_log = require("./event-log-dSOnXHuG.cjs");
|
|
|
2
2
|
const require_decision = require("./decision-D8bcIGXQ.cjs");
|
|
3
3
|
const require_act = require("./act-B0CVbmo4.cjs");
|
|
4
4
|
const require_event_log_store = require("./event-log-store-Dey9ufA4.cjs");
|
|
5
|
+
const require_machine_input = require("./machine-input-DU3owLUr.cjs");
|
|
5
6
|
let xstate = require("xstate");
|
|
6
7
|
let xstate_durable = require("xstate/durable");
|
|
7
8
|
//#region src/internal/stable-inspection.ts
|
|
@@ -1282,26 +1283,6 @@ function bindChildMachineForProvide(childMachine, executors, options, visited) {
|
|
|
1282
1283
|
return rebindChildMachine(childMachine, ctxFor(childMachine), visited, ctxFor);
|
|
1283
1284
|
}
|
|
1284
1285
|
/**
|
|
1285
|
-
* Validates `input` against the machine's registered input schema, returning
|
|
1286
|
-
* the schema's output — so defaults are filled and transforms applied before
|
|
1287
|
-
* the value reaches `createActor` or the replayable event log.
|
|
1288
|
-
*
|
|
1289
|
-
* Standard Schema only (no validation library is referenced), so this works for
|
|
1290
|
-
* whatever the machine was declared with. Omitted input stays omitted rather
|
|
1291
|
-
* than being validated as `{}`: "started with no input" keeps meaning what it
|
|
1292
|
-
* has always meant, instead of newly failing schemas with required fields.
|
|
1293
|
-
*/
|
|
1294
|
-
function resolveMachineInput(machine, input) {
|
|
1295
|
-
if (input === void 0) return input;
|
|
1296
|
-
const schema = require_event_log.getRegisteredAgentExecutionOptions(machine).schemas?.input;
|
|
1297
|
-
if (!require_event_log.isStandardSchema(schema)) return input;
|
|
1298
|
-
try {
|
|
1299
|
-
return require_event_log.validateSchemaSync(schema, input);
|
|
1300
|
-
} catch (error) {
|
|
1301
|
-
throw new require_event_log.AgentError("invalid-machine-input", `createAgentRuntime: machine input failed validation against the declared input schema: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
1302
|
-
}
|
|
1303
|
-
}
|
|
1304
|
-
/**
|
|
1305
1286
|
* Recursively rebinds an invoked child machine's own agent sources with the
|
|
1306
1287
|
* SAME host-backed wrappers the runtime applies to the top-level machine, so a
|
|
1307
1288
|
* child's text/stream/decision requests inherit the runtime's executors and
|
|
@@ -1569,7 +1550,9 @@ async function waitForIdleAnswer(snapshot, onIdle, inbox, signal, aborted) {
|
|
|
1569
1550
|
* ```
|
|
1570
1551
|
*/
|
|
1571
1552
|
async function runAgent(machine, options = {}) {
|
|
1572
|
-
const { onIdle, inbox, ...rest } = options;
|
|
1553
|
+
const { onIdle: idle, inbox, ...rest } = options;
|
|
1554
|
+
if (idle === "inbox" && inbox === void 0) throw new require_event_log.AgentError("inbox-required", "runAgent: `onIdle: \"inbox\"` needs an `inbox`.");
|
|
1555
|
+
const onIdle = idle === "inbox" ? () => new Promise(() => {}) : idle;
|
|
1573
1556
|
const inboxState = inbox === void 0 ? void 0 : inboxStates.get(inbox);
|
|
1574
1557
|
if (inbox !== void 0) {
|
|
1575
1558
|
if (inboxState === void 0 || inboxState.claimed) throw new require_event_log.AgentError("inbox-in-use", "runAgent: `inbox` must come from createAgentInbox() and serves one runAgent call.");
|
|
@@ -2001,6 +1984,7 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
2001
1984
|
}
|
|
2002
1985
|
const boundMachine = provided.provide({ actors: wrappedSources });
|
|
2003
1986
|
const mailbox = [];
|
|
1987
|
+
const hostQueue = [];
|
|
2004
1988
|
let wake;
|
|
2005
1989
|
const fromMailbox = /* @__PURE__ */ new WeakSet();
|
|
2006
1990
|
let stopReason;
|
|
@@ -2014,7 +1998,8 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
2014
1998
|
if (waiting) {
|
|
2015
1999
|
wake = void 0;
|
|
2016
2000
|
waiting(event);
|
|
2017
|
-
} else
|
|
2001
|
+
} else if (fromHost) hostQueue.push(event);
|
|
2002
|
+
else mailbox.push(event);
|
|
2018
2003
|
};
|
|
2019
2004
|
const inProcessTimers = options.timers === void 0 || options.timers === "in-process";
|
|
2020
2005
|
const timerHandles = /* @__PURE__ */ new Map();
|
|
@@ -2089,11 +2074,25 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
2089
2074
|
let abortTimer;
|
|
2090
2075
|
const onAbort = () => {
|
|
2091
2076
|
const abortWith = options.abortWith;
|
|
2092
|
-
if (abortWith === void 0
|
|
2077
|
+
if (abortWith === void 0) {
|
|
2078
|
+
abortNow();
|
|
2079
|
+
return;
|
|
2080
|
+
}
|
|
2081
|
+
let event;
|
|
2082
|
+
try {
|
|
2083
|
+
event = typeof abortWith.event === "function" ? abortWith.event(options.signal?.reason) : abortWith.event;
|
|
2084
|
+
} catch (error) {
|
|
2085
|
+
stopRun({
|
|
2086
|
+
cause: "aborted",
|
|
2087
|
+
error
|
|
2088
|
+
});
|
|
2089
|
+
return;
|
|
2090
|
+
}
|
|
2091
|
+
if (runtime.send(event) === false) {
|
|
2093
2092
|
abortNow();
|
|
2094
2093
|
return;
|
|
2095
2094
|
}
|
|
2096
|
-
pendingAbort =
|
|
2095
|
+
pendingAbort = event;
|
|
2097
2096
|
if (abortWith.timeout !== void 0) abortTimer = setTimeout(() => {
|
|
2098
2097
|
if (current?.status === "active") abortNow();
|
|
2099
2098
|
}, abortWith.timeout);
|
|
@@ -2151,7 +2150,7 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
2151
2150
|
enqueueRootEvent: (_source, event) => enqueue(event),
|
|
2152
2151
|
executeAction: (action, _metadata, runtime) => action.exec(runtime),
|
|
2153
2152
|
waitForEvent: () => {
|
|
2154
|
-
const queued = mailbox.shift();
|
|
2153
|
+
const queued = hostQueue.shift() ?? mailbox.shift();
|
|
2155
2154
|
if (queued !== void 0) return queued;
|
|
2156
2155
|
if (resultOwed) {
|
|
2157
2156
|
resultOwed = false;
|
|
@@ -2224,7 +2223,7 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
2224
2223
|
resumedFromStore = true;
|
|
2225
2224
|
}
|
|
2226
2225
|
}
|
|
2227
|
-
const resolvedInput = resolveMachineInput(machine, init.input);
|
|
2226
|
+
const resolvedInput = require_machine_input.resolveMachineInput(machine, init.input, "createAgentRuntime");
|
|
2228
2227
|
const threadInit = resumeEvents?.[0]?.event;
|
|
2229
2228
|
if (resumedFromStore && init.input !== void 0 && threadInit !== void 0 && Object.hasOwn(threadInit, "input")) {
|
|
2230
2229
|
if (require_event_log.canonicalJson(threadInit.input) !== require_event_log.canonicalJson(resolvedInput)) throw new require_event_log.AgentError("thread-input-mismatch", `createAgentRuntime: thread '${threadId}' was started with different input. A thread that already has entries is resumed, not restarted; give a new run a new threadId.`);
|
|
@@ -2471,7 +2470,7 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
2471
2470
|
async nextEvent() {
|
|
2472
2471
|
for (;;) {
|
|
2473
2472
|
if (finished || stopReason !== void 0 || current?.status !== "active") return;
|
|
2474
|
-
const event = await execution.waitForEvent();
|
|
2473
|
+
const event = hostQueue.shift() ?? await execution.waitForEvent();
|
|
2475
2474
|
if (event === QUIESCENT || stopReason !== void 0) return;
|
|
2476
2475
|
if (isStaleChildEvent(event)) continue;
|
|
2477
2476
|
return event;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { C as AGENT_USAGE_EVENT_TYPE, F as getCallModelCalls, I as getCallUsage, K as getRegisteredAgentExecutionOptions, L as isTextLogic, P as getCallFinishReason, R as normalizeGeneratorResult, S as validateReplayEntries, T as ACT_ACTOR, U as responseMessagesOf, W as executorBoundLogics, Z as findNonSerializableContextPaths, _ as getSnapshotStateHash, c as agentCallSite, ct as validateSchemaSync, d as canonicalEventJson, dt as isPlainObject, f as createReplayEntry, ft as isRecord, g as getLogExecutionId, i as AgentMachineVersionMismatchError, it as resolveMachineVersion, lt as canonicalJson, p as foldLog, pt as AgentError, q as isUnboundPlaceholder,
|
|
1
|
+
import { C as AGENT_USAGE_EVENT_TYPE, F as getCallModelCalls, I as getCallUsage, K as getRegisteredAgentExecutionOptions, L as isTextLogic, P as getCallFinishReason, R as normalizeGeneratorResult, S as validateReplayEntries, T as ACT_ACTOR, U as responseMessagesOf, W as executorBoundLogics, Z as findNonSerializableContextPaths, _ as getSnapshotStateHash, c as agentCallSite, ct as validateSchemaSync, d as canonicalEventJson, dt as isPlainObject, f as createReplayEntry, ft as isRecord, g as getLogExecutionId, i as AgentMachineVersionMismatchError, it as resolveMachineVersion, lt as canonicalJson, p as foldLog, pt as AgentError, q as isUnboundPlaceholder, s as agentCallOccurrence, u as assertJsonSerializable, v as getUsageFromEvents, w as restoreMachineSnapshot, x as replay, y as initEntry } from "./event-log-CEq_bVXQ.mjs";
|
|
2
2
|
import { _ as isEventPattern, a as createDecisionRequest, g as getHumanEventTypes, h as getAcceptedEvents, l as resolveAllowedEventTypes, n as acceptedDecisionOptions, s as isDecisionLogic, t as AgentDecisionExhaustedError, u as resolveDecision } from "./decision-B41YBx5U.mjs";
|
|
3
3
|
import { i as isActLogic, t as ACT_STATE_EXITED } from "./act-Bsz-hUfD.mjs";
|
|
4
4
|
import { t as AgentEventLogConflictError } from "./event-log-store-DLAfNM34.mjs";
|
|
5
|
+
import { t as resolveMachineInput } from "./machine-input-0YTPcnCm.mjs";
|
|
5
6
|
import { createAsyncLogic, deliverEvent, getNextTransitions, isMachineSnapshot, stopActor, transition } from "xstate";
|
|
6
7
|
import { createDurable } from "xstate/durable";
|
|
7
8
|
//#region src/internal/stable-inspection.ts
|
|
@@ -1282,26 +1283,6 @@ function bindChildMachineForProvide(childMachine, executors, options, visited) {
|
|
|
1282
1283
|
return rebindChildMachine(childMachine, ctxFor(childMachine), visited, ctxFor);
|
|
1283
1284
|
}
|
|
1284
1285
|
/**
|
|
1285
|
-
* Validates `input` against the machine's registered input schema, returning
|
|
1286
|
-
* the schema's output — so defaults are filled and transforms applied before
|
|
1287
|
-
* the value reaches `createActor` or the replayable event log.
|
|
1288
|
-
*
|
|
1289
|
-
* Standard Schema only (no validation library is referenced), so this works for
|
|
1290
|
-
* whatever the machine was declared with. Omitted input stays omitted rather
|
|
1291
|
-
* than being validated as `{}`: "started with no input" keeps meaning what it
|
|
1292
|
-
* has always meant, instead of newly failing schemas with required fields.
|
|
1293
|
-
*/
|
|
1294
|
-
function resolveMachineInput(machine, input) {
|
|
1295
|
-
if (input === void 0) return input;
|
|
1296
|
-
const schema = getRegisteredAgentExecutionOptions(machine).schemas?.input;
|
|
1297
|
-
if (!isStandardSchema(schema)) return input;
|
|
1298
|
-
try {
|
|
1299
|
-
return validateSchemaSync(schema, input);
|
|
1300
|
-
} catch (error) {
|
|
1301
|
-
throw new AgentError("invalid-machine-input", `createAgentRuntime: machine input failed validation against the declared input schema: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
1302
|
-
}
|
|
1303
|
-
}
|
|
1304
|
-
/**
|
|
1305
1286
|
* Recursively rebinds an invoked child machine's own agent sources with the
|
|
1306
1287
|
* SAME host-backed wrappers the runtime applies to the top-level machine, so a
|
|
1307
1288
|
* child's text/stream/decision requests inherit the runtime's executors and
|
|
@@ -1569,7 +1550,9 @@ async function waitForIdleAnswer(snapshot, onIdle, inbox, signal, aborted) {
|
|
|
1569
1550
|
* ```
|
|
1570
1551
|
*/
|
|
1571
1552
|
async function runAgent(machine, options = {}) {
|
|
1572
|
-
const { onIdle, inbox, ...rest } = options;
|
|
1553
|
+
const { onIdle: idle, inbox, ...rest } = options;
|
|
1554
|
+
if (idle === "inbox" && inbox === void 0) throw new AgentError("inbox-required", "runAgent: `onIdle: \"inbox\"` needs an `inbox`.");
|
|
1555
|
+
const onIdle = idle === "inbox" ? () => new Promise(() => {}) : idle;
|
|
1573
1556
|
const inboxState = inbox === void 0 ? void 0 : inboxStates.get(inbox);
|
|
1574
1557
|
if (inbox !== void 0) {
|
|
1575
1558
|
if (inboxState === void 0 || inboxState.claimed) throw new AgentError("inbox-in-use", "runAgent: `inbox` must come from createAgentInbox() and serves one runAgent call.");
|
|
@@ -2001,6 +1984,7 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
2001
1984
|
}
|
|
2002
1985
|
const boundMachine = provided.provide({ actors: wrappedSources });
|
|
2003
1986
|
const mailbox = [];
|
|
1987
|
+
const hostQueue = [];
|
|
2004
1988
|
let wake;
|
|
2005
1989
|
const fromMailbox = /* @__PURE__ */ new WeakSet();
|
|
2006
1990
|
let stopReason;
|
|
@@ -2014,7 +1998,8 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
2014
1998
|
if (waiting) {
|
|
2015
1999
|
wake = void 0;
|
|
2016
2000
|
waiting(event);
|
|
2017
|
-
} else
|
|
2001
|
+
} else if (fromHost) hostQueue.push(event);
|
|
2002
|
+
else mailbox.push(event);
|
|
2018
2003
|
};
|
|
2019
2004
|
const inProcessTimers = options.timers === void 0 || options.timers === "in-process";
|
|
2020
2005
|
const timerHandles = /* @__PURE__ */ new Map();
|
|
@@ -2089,11 +2074,25 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
2089
2074
|
let abortTimer;
|
|
2090
2075
|
const onAbort = () => {
|
|
2091
2076
|
const abortWith = options.abortWith;
|
|
2092
|
-
if (abortWith === void 0
|
|
2077
|
+
if (abortWith === void 0) {
|
|
2078
|
+
abortNow();
|
|
2079
|
+
return;
|
|
2080
|
+
}
|
|
2081
|
+
let event;
|
|
2082
|
+
try {
|
|
2083
|
+
event = typeof abortWith.event === "function" ? abortWith.event(options.signal?.reason) : abortWith.event;
|
|
2084
|
+
} catch (error) {
|
|
2085
|
+
stopRun({
|
|
2086
|
+
cause: "aborted",
|
|
2087
|
+
error
|
|
2088
|
+
});
|
|
2089
|
+
return;
|
|
2090
|
+
}
|
|
2091
|
+
if (runtime.send(event) === false) {
|
|
2093
2092
|
abortNow();
|
|
2094
2093
|
return;
|
|
2095
2094
|
}
|
|
2096
|
-
pendingAbort =
|
|
2095
|
+
pendingAbort = event;
|
|
2097
2096
|
if (abortWith.timeout !== void 0) abortTimer = setTimeout(() => {
|
|
2098
2097
|
if (current?.status === "active") abortNow();
|
|
2099
2098
|
}, abortWith.timeout);
|
|
@@ -2151,7 +2150,7 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
2151
2150
|
enqueueRootEvent: (_source, event) => enqueue(event),
|
|
2152
2151
|
executeAction: (action, _metadata, runtime) => action.exec(runtime),
|
|
2153
2152
|
waitForEvent: () => {
|
|
2154
|
-
const queued = mailbox.shift();
|
|
2153
|
+
const queued = hostQueue.shift() ?? mailbox.shift();
|
|
2155
2154
|
if (queued !== void 0) return queued;
|
|
2156
2155
|
if (resultOwed) {
|
|
2157
2156
|
resultOwed = false;
|
|
@@ -2224,7 +2223,7 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
2224
2223
|
resumedFromStore = true;
|
|
2225
2224
|
}
|
|
2226
2225
|
}
|
|
2227
|
-
const resolvedInput = resolveMachineInput(machine, init.input);
|
|
2226
|
+
const resolvedInput = resolveMachineInput(machine, init.input, "createAgentRuntime");
|
|
2228
2227
|
const threadInit = resumeEvents?.[0]?.event;
|
|
2229
2228
|
if (resumedFromStore && init.input !== void 0 && threadInit !== void 0 && Object.hasOwn(threadInit, "input")) {
|
|
2230
2229
|
if (canonicalJson(threadInit.input) !== canonicalJson(resolvedInput)) throw new AgentError("thread-input-mismatch", `createAgentRuntime: thread '${threadId}' was started with different input. A thread that already has entries is resumed, not restarted; give a new run a new threadId.`);
|
|
@@ -2471,7 +2470,7 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
2471
2470
|
async nextEvent() {
|
|
2472
2471
|
for (;;) {
|
|
2473
2472
|
if (finished || stopReason !== void 0 || current?.status !== "active") return;
|
|
2474
|
-
const event = await execution.waitForEvent();
|
|
2473
|
+
const event = hostQueue.shift() ?? await execution.waitForEvent();
|
|
2475
2474
|
if (event === QUIESCENT || stopReason !== void 0) return;
|
|
2476
2475
|
if (isStaleChildEvent(event)) continue;
|
|
2477
2476
|
return event;
|