@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/ai-sdk.cjs
CHANGED
package/dist/ai-sdk.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { d as ChosenEvent } from "./types-
|
|
2
|
-
import { k as AgentDecisionExecutor, l as AgentRequestExecutors, o as AgentRequestExecutor } from "./text-logic-
|
|
1
|
+
import { d as ChosenEvent } from "./types-rMe7x6NR.cjs";
|
|
2
|
+
import { k as AgentDecisionExecutor, l as AgentRequestExecutors, o as AgentRequestExecutor } from "./text-logic-Jkilp1Ie.cjs";
|
|
3
3
|
import { FinishReason, LanguageModel, LanguageModelUsage, ToolSet, TypedToolCall, TypedToolResult } from "ai";
|
|
4
4
|
|
|
5
5
|
//#region src/ai-sdk/index.d.ts
|
package/dist/ai-sdk.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { d as ChosenEvent } from "./types-
|
|
2
|
-
import { k as AgentDecisionExecutor, l as AgentRequestExecutors, o as AgentRequestExecutor } from "./text-logic-
|
|
1
|
+
import { d as ChosenEvent } from "./types-9Bqg5rZB.mjs";
|
|
2
|
+
import { k as AgentDecisionExecutor, l as AgentRequestExecutors, o as AgentRequestExecutor } from "./text-logic-C5kbjaDz.mjs";
|
|
3
3
|
import { FinishReason, LanguageModel, LanguageModelUsage, ToolSet, TypedToolCall, TypedToolResult } from "ai";
|
|
4
4
|
|
|
5
5
|
//#region src/ai-sdk/index.d.ts
|
package/dist/ai-sdk.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { V as isStandardSchema, i as renderDecisionAttempts, p as buildEnvelopeSchema, v as getAgentOutputMode } from "./decision-D9Zi7Xi5.mjs";
|
|
2
2
|
import { NoObjectGeneratedError, Output, generateText, stepCountIs, streamText, tool } from "ai";
|
|
3
3
|
//#region src/ai-sdk/mappers.ts
|
|
4
4
|
/**
|
|
@@ -281,7 +281,6 @@ const USER_INPUT_ACTOR = "agent.userInput";
|
|
|
281
281
|
const GENERATE_TEXT_ACTOR = "agent.generateText";
|
|
282
282
|
const STREAM_TEXT_ACTOR = "agent.streamText";
|
|
283
283
|
const DECIDE_ACTOR = "agent.decide";
|
|
284
|
-
const PLAN_ACTOR = "agent.plan";
|
|
285
284
|
/** Synthetic `src` stamped on trace requests produced by `getRequests` state interpretation — NOT a registered actor source (nothing is invoked; the pass makes the call directly). */
|
|
286
285
|
const INTERPRET_SOURCE = "agent.interpret";
|
|
287
286
|
/**
|
|
@@ -882,68 +881,6 @@ function decideActorWithExecutor(execute) {
|
|
|
882
881
|
function createDecideActor() {
|
|
883
882
|
return decideActorWithExecutor();
|
|
884
883
|
}
|
|
885
|
-
/**
|
|
886
|
-
* Reserved event type the `agent.plan` builtin adds to every step's
|
|
887
|
-
* candidates as the explicit "no further action needed" move. Choosing it
|
|
888
|
-
* ends the plan (`stopped: 'done'`); it is never sent to the machine, so
|
|
889
|
-
* machines need no no-op sentinel event of their own.
|
|
890
|
-
*/
|
|
891
|
-
const PLAN_DONE_EVENT_TYPE = "agent.plan.done";
|
|
892
|
-
function createPlanActor() {
|
|
893
|
-
const logic = (0, xstate.createLogic)({
|
|
894
|
-
context: ({ input }) => ({
|
|
895
|
-
applied: [],
|
|
896
|
-
stepsRemaining: input.maxSteps ?? 8,
|
|
897
|
-
stopped: null
|
|
898
|
-
}),
|
|
899
|
-
run: ({ context, event }) => event.type === "plan.applied" ? { context: {
|
|
900
|
-
...context,
|
|
901
|
-
applied: [...context.applied, event.event],
|
|
902
|
-
stepsRemaining: context.stepsRemaining - 1
|
|
903
|
-
} } : event.type === "plan.ended" ? {
|
|
904
|
-
context: {
|
|
905
|
-
...context,
|
|
906
|
-
stopped: event.stopped
|
|
907
|
-
},
|
|
908
|
-
status: "done",
|
|
909
|
-
output: {
|
|
910
|
-
steps: context.applied,
|
|
911
|
-
stopped: event.stopped
|
|
912
|
-
}
|
|
913
|
-
} : void 0
|
|
914
|
-
});
|
|
915
|
-
return Object.assign(logic, {
|
|
916
|
-
kind: "statelyai.planLogic",
|
|
917
|
-
maxRetries: 2,
|
|
918
|
-
request: decideRequestFromInput,
|
|
919
|
-
allowedEventTypes: (input) => resolveAllowedEventTypes(input.allowedEvents, input)
|
|
920
|
-
});
|
|
921
|
-
}
|
|
922
|
-
const PLAN_LEDGER_SCOPE = {
|
|
923
|
-
emit: () => {},
|
|
924
|
-
self: { id: "agent.plan.ledger" }
|
|
925
|
-
};
|
|
926
|
-
/**
|
|
927
|
-
* Builds a fresh plan ledger snapshot from resolved plan input — the shared
|
|
928
|
-
* starting point for BOTH hosts (the step path reads the invoke child's own
|
|
929
|
-
* initial snapshot; runAgent seeds a local ledger with this). @internal
|
|
930
|
-
*/
|
|
931
|
-
function initialPlanLedger(logic, input) {
|
|
932
|
-
return logic.getInitialSnapshot(PLAN_LEDGER_SCOPE, input);
|
|
933
|
-
}
|
|
934
|
-
/**
|
|
935
|
-
* Advances a plan ledger by one {@link PlanLedgerEvent}, returning the next
|
|
936
|
-
* snapshot (unwrapping `createLogic`'s `[snapshot, effects]` tuple). Pure — no
|
|
937
|
-
* mutation of the input snapshot. @internal
|
|
938
|
-
*/
|
|
939
|
-
function advancePlanLedger(logic, snapshot, event) {
|
|
940
|
-
const result = logic.transition(snapshot, event, PLAN_LEDGER_SCOPE);
|
|
941
|
-
return Array.isArray(result) ? result[0] : result;
|
|
942
|
-
}
|
|
943
|
-
/** Type guard: true for the `agent.plan` builtin logic (checks the `kind` marker). @internal */
|
|
944
|
-
function isPlanLogic(logic) {
|
|
945
|
-
return !!logic && logic.kind === "statelyai.planLogic";
|
|
946
|
-
}
|
|
947
884
|
function resolveAllowedEventTypes(allowedEvents, input) {
|
|
948
885
|
if (allowedEvents === void 0) return;
|
|
949
886
|
const resolved = typeof allowedEvents === "function" ? allowedEvents({ input }) : allowedEvents;
|
|
@@ -1087,30 +1024,12 @@ Object.defineProperty(exports, "INTERPRET_SOURCE", {
|
|
|
1087
1024
|
return INTERPRET_SOURCE;
|
|
1088
1025
|
}
|
|
1089
1026
|
});
|
|
1090
|
-
Object.defineProperty(exports, "PLAN_ACTOR", {
|
|
1091
|
-
enumerable: true,
|
|
1092
|
-
get: function() {
|
|
1093
|
-
return PLAN_ACTOR;
|
|
1094
|
-
}
|
|
1095
|
-
});
|
|
1096
|
-
Object.defineProperty(exports, "PLAN_DONE_EVENT_TYPE", {
|
|
1097
|
-
enumerable: true,
|
|
1098
|
-
get: function() {
|
|
1099
|
-
return PLAN_DONE_EVENT_TYPE;
|
|
1100
|
-
}
|
|
1101
|
-
});
|
|
1102
1027
|
Object.defineProperty(exports, "USER_INPUT_ACTOR", {
|
|
1103
1028
|
enumerable: true,
|
|
1104
1029
|
get: function() {
|
|
1105
1030
|
return USER_INPUT_ACTOR;
|
|
1106
1031
|
}
|
|
1107
1032
|
});
|
|
1108
|
-
Object.defineProperty(exports, "advancePlanLedger", {
|
|
1109
|
-
enumerable: true,
|
|
1110
|
-
get: function() {
|
|
1111
|
-
return advancePlanLedger;
|
|
1112
|
-
}
|
|
1113
|
-
});
|
|
1114
1033
|
Object.defineProperty(exports, "agentExecutionOptions", {
|
|
1115
1034
|
enumerable: true,
|
|
1116
1035
|
get: function() {
|
|
@@ -1147,12 +1066,6 @@ Object.defineProperty(exports, "createDecideActor", {
|
|
|
1147
1066
|
return createDecideActor;
|
|
1148
1067
|
}
|
|
1149
1068
|
});
|
|
1150
|
-
Object.defineProperty(exports, "createPlanActor", {
|
|
1151
|
-
enumerable: true,
|
|
1152
|
-
get: function() {
|
|
1153
|
-
return createPlanActor;
|
|
1154
|
-
}
|
|
1155
|
-
});
|
|
1156
1069
|
Object.defineProperty(exports, "createTextLogic", {
|
|
1157
1070
|
enumerable: true,
|
|
1158
1071
|
get: function() {
|
|
@@ -1243,24 +1156,12 @@ Object.defineProperty(exports, "getStateMeta", {
|
|
|
1243
1156
|
return getStateMeta;
|
|
1244
1157
|
}
|
|
1245
1158
|
});
|
|
1246
|
-
Object.defineProperty(exports, "initialPlanLedger", {
|
|
1247
|
-
enumerable: true,
|
|
1248
|
-
get: function() {
|
|
1249
|
-
return initialPlanLedger;
|
|
1250
|
-
}
|
|
1251
|
-
});
|
|
1252
1159
|
Object.defineProperty(exports, "isDecisionLogic", {
|
|
1253
1160
|
enumerable: true,
|
|
1254
1161
|
get: function() {
|
|
1255
1162
|
return isDecisionLogic;
|
|
1256
1163
|
}
|
|
1257
1164
|
});
|
|
1258
|
-
Object.defineProperty(exports, "isPlanLogic", {
|
|
1259
|
-
enumerable: true,
|
|
1260
|
-
get: function() {
|
|
1261
|
-
return isPlanLogic;
|
|
1262
|
-
}
|
|
1263
|
-
});
|
|
1264
1165
|
Object.defineProperty(exports, "isStandardSchema", {
|
|
1265
1166
|
enumerable: true,
|
|
1266
1167
|
get: function() {
|
|
@@ -1345,12 +1246,6 @@ Object.defineProperty(exports, "resolveDecision", {
|
|
|
1345
1246
|
return resolveDecision;
|
|
1346
1247
|
}
|
|
1347
1248
|
});
|
|
1348
|
-
Object.defineProperty(exports, "sanitizeEventToolName", {
|
|
1349
|
-
enumerable: true,
|
|
1350
|
-
get: function() {
|
|
1351
|
-
return sanitizeEventToolName;
|
|
1352
|
-
}
|
|
1353
|
-
});
|
|
1354
1249
|
Object.defineProperty(exports, "systemMessage", {
|
|
1355
1250
|
enumerable: true,
|
|
1356
1251
|
get: function() {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as AgentError } from "./errors-CeSXQx0v.mjs";
|
|
2
|
-
import { createAsyncLogic,
|
|
2
|
+
import { createAsyncLogic, getNextTransitions } from "xstate";
|
|
3
3
|
//#region src/utils.ts
|
|
4
4
|
/**
|
|
5
5
|
* Deep-clones a snapshot to a plain-JSON value via a `JSON` round-trip, the
|
|
@@ -281,7 +281,6 @@ const USER_INPUT_ACTOR = "agent.userInput";
|
|
|
281
281
|
const GENERATE_TEXT_ACTOR = "agent.generateText";
|
|
282
282
|
const STREAM_TEXT_ACTOR = "agent.streamText";
|
|
283
283
|
const DECIDE_ACTOR = "agent.decide";
|
|
284
|
-
const PLAN_ACTOR = "agent.plan";
|
|
285
284
|
/** Synthetic `src` stamped on trace requests produced by `getRequests` state interpretation — NOT a registered actor source (nothing is invoked; the pass makes the call directly). */
|
|
286
285
|
const INTERPRET_SOURCE = "agent.interpret";
|
|
287
286
|
/**
|
|
@@ -882,68 +881,6 @@ function decideActorWithExecutor(execute) {
|
|
|
882
881
|
function createDecideActor() {
|
|
883
882
|
return decideActorWithExecutor();
|
|
884
883
|
}
|
|
885
|
-
/**
|
|
886
|
-
* Reserved event type the `agent.plan` builtin adds to every step's
|
|
887
|
-
* candidates as the explicit "no further action needed" move. Choosing it
|
|
888
|
-
* ends the plan (`stopped: 'done'`); it is never sent to the machine, so
|
|
889
|
-
* machines need no no-op sentinel event of their own.
|
|
890
|
-
*/
|
|
891
|
-
const PLAN_DONE_EVENT_TYPE = "agent.plan.done";
|
|
892
|
-
function createPlanActor() {
|
|
893
|
-
const logic = createLogic({
|
|
894
|
-
context: ({ input }) => ({
|
|
895
|
-
applied: [],
|
|
896
|
-
stepsRemaining: input.maxSteps ?? 8,
|
|
897
|
-
stopped: null
|
|
898
|
-
}),
|
|
899
|
-
run: ({ context, event }) => event.type === "plan.applied" ? { context: {
|
|
900
|
-
...context,
|
|
901
|
-
applied: [...context.applied, event.event],
|
|
902
|
-
stepsRemaining: context.stepsRemaining - 1
|
|
903
|
-
} } : event.type === "plan.ended" ? {
|
|
904
|
-
context: {
|
|
905
|
-
...context,
|
|
906
|
-
stopped: event.stopped
|
|
907
|
-
},
|
|
908
|
-
status: "done",
|
|
909
|
-
output: {
|
|
910
|
-
steps: context.applied,
|
|
911
|
-
stopped: event.stopped
|
|
912
|
-
}
|
|
913
|
-
} : void 0
|
|
914
|
-
});
|
|
915
|
-
return Object.assign(logic, {
|
|
916
|
-
kind: "statelyai.planLogic",
|
|
917
|
-
maxRetries: 2,
|
|
918
|
-
request: decideRequestFromInput,
|
|
919
|
-
allowedEventTypes: (input) => resolveAllowedEventTypes(input.allowedEvents, input)
|
|
920
|
-
});
|
|
921
|
-
}
|
|
922
|
-
const PLAN_LEDGER_SCOPE = {
|
|
923
|
-
emit: () => {},
|
|
924
|
-
self: { id: "agent.plan.ledger" }
|
|
925
|
-
};
|
|
926
|
-
/**
|
|
927
|
-
* Builds a fresh plan ledger snapshot from resolved plan input — the shared
|
|
928
|
-
* starting point for BOTH hosts (the step path reads the invoke child's own
|
|
929
|
-
* initial snapshot; runAgent seeds a local ledger with this). @internal
|
|
930
|
-
*/
|
|
931
|
-
function initialPlanLedger(logic, input) {
|
|
932
|
-
return logic.getInitialSnapshot(PLAN_LEDGER_SCOPE, input);
|
|
933
|
-
}
|
|
934
|
-
/**
|
|
935
|
-
* Advances a plan ledger by one {@link PlanLedgerEvent}, returning the next
|
|
936
|
-
* snapshot (unwrapping `createLogic`'s `[snapshot, effects]` tuple). Pure — no
|
|
937
|
-
* mutation of the input snapshot. @internal
|
|
938
|
-
*/
|
|
939
|
-
function advancePlanLedger(logic, snapshot, event) {
|
|
940
|
-
const result = logic.transition(snapshot, event, PLAN_LEDGER_SCOPE);
|
|
941
|
-
return Array.isArray(result) ? result[0] : result;
|
|
942
|
-
}
|
|
943
|
-
/** Type guard: true for the `agent.plan` builtin logic (checks the `kind` marker). @internal */
|
|
944
|
-
function isPlanLogic(logic) {
|
|
945
|
-
return !!logic && logic.kind === "statelyai.planLogic";
|
|
946
|
-
}
|
|
947
884
|
function resolveAllowedEventTypes(allowedEvents, input) {
|
|
948
885
|
if (allowedEvents === void 0) return;
|
|
949
886
|
const resolved = typeof allowedEvents === "function" ? allowedEvents({ input }) : allowedEvents;
|
|
@@ -1063,4 +1000,4 @@ async function resolveDecision(request, executor, options = {}) {
|
|
|
1063
1000
|
throw new AgentDecisionExhaustedError(attempts);
|
|
1064
1001
|
}
|
|
1065
1002
|
//#endregion
|
|
1066
|
-
export {
|
|
1003
|
+
export { isUnboundPlaceholder as A, getStateMeta as B, parseStructuredEnvelope as C, getMachineStaticTransitionTargets as D, executorBoundLogics as E, findNonSerializableContextPaths as F, userMessage as G, persistSnapshot as H, getAgentMessages as I, validateSchemaSync as K, getJsonSchema as L, machineSuspensionPredicates as M, missingActor as N, getMachineSuspensionPredicate as O, assistantMessage as P, getJsonSchemaSync as R, parseOutput as S, agentExecutionOptions as T, systemMessage as U, isStandardSchema as V, toolMessage as W, extractCallUsage as _, resolveDecision as a, normalizeGeneratorResult as b, AGENT_USAGE_TOKEN_FIELDS as c, USER_INPUT_ACTOR as d, bindRequestExecutor as f, executeAgentTextRequest as g, createTextLogic as h, renderDecisionAttempts as i, machineStaticTransitionTargets as j, getRegisteredAgentExecutionOptions as k, DECIDE_ACTOR as l, builtinTextActors as m, createDecideActor as n, getAcceptedEvents as o, buildEnvelopeSchema as p, isDecisionLogic as r, parseAgentEvent as s, AgentDecisionExhaustedError as t, INTERPRET_SOURCE as u, getAgentOutputMode as v, userInputActor as w, parseModelRef as x, isTextLogic as y, getMachineStructuralHash as z };
|
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_errors = require("./errors-DUBBzRLP.cjs");
|
|
3
|
-
const require_setup_agent = require("./setup-agent-
|
|
4
|
-
const require_decision = require("./decision-
|
|
3
|
+
const require_setup_agent = require("./setup-agent-CpK0ZRWV.cjs");
|
|
4
|
+
const require_decision = require("./decision-C0cUKvNt.cjs");
|
|
5
5
|
const require_event_log_store = require("./event-log-store-CNT_7F0V.cjs");
|
|
6
6
|
let xstate = require("xstate");
|
|
7
7
|
//#region src/internal/state-request-pass.ts
|
|
@@ -365,7 +365,7 @@ function assertMachineBindable(machine, effectiveSources, options, ctx) {
|
|
|
365
365
|
assertChildMachineBindable(src, src, stateName, options, ctx);
|
|
366
366
|
continue;
|
|
367
367
|
}
|
|
368
|
-
if ((require_decision.isTextLogic(src) || require_decision.isDecisionLogic(src)
|
|
368
|
+
if ((require_decision.isTextLogic(src) || require_decision.isDecisionLogic(src)) && !require_decision.executorBoundLogics.has(src)) throw new Error(`runAgent: ${where} '${stateName}' invokes a direct-object actor logic (kind: '${src.kind}'). Direct-object invoke srcs cannot be rebound by runAgent — either call '.withExecutor(...)' on the logic before invoking it, or register it as a string-keyed actor source instead (machine.provide({ actors: { name: logic } })) and invoke it by name.`);
|
|
369
369
|
continue;
|
|
370
370
|
}
|
|
371
371
|
const logic = effectiveSources[src];
|
|
@@ -381,11 +381,6 @@ function assertMachineBindable(machine, effectiveSources, options, ctx) {
|
|
|
381
381
|
if (!options.hasDecide) throw new Error(`runAgent: ${where} '${stateName}' invokes decision source '${src}' but no 'decide' executor was provided to runAgent(...).`);
|
|
382
382
|
continue;
|
|
383
383
|
}
|
|
384
|
-
if (require_decision.isPlanLogic(logic)) {
|
|
385
|
-
if (!ctx.rebindable) throw unrebindableChildRequestError(ctx.childPath, stateName, src, "plan");
|
|
386
|
-
if (!options.hasDecide) throw new Error(`runAgent: ${where} '${stateName}' invokes plan source '${src}' but no 'decide' executor was provided to runAgent(...).`);
|
|
387
|
-
continue;
|
|
388
|
-
}
|
|
389
384
|
if (require_decision.isTextLogic(logic)) {
|
|
390
385
|
if (require_decision.executorBoundLogics.has(logic)) continue;
|
|
391
386
|
if (!ctx.rebindable) throw unrebindableChildRequestError(ctx.childPath, stateName, src, logic.mode === "stream" ? "streaming text" : "text");
|
|
@@ -443,11 +438,11 @@ function selfIdAndSrc(self) {
|
|
|
443
438
|
};
|
|
444
439
|
}
|
|
445
440
|
/**
|
|
446
|
-
* The machine actor that INVOKED a decision
|
|
441
|
+
* The machine actor that INVOKED a decision request — the actor whose
|
|
447
442
|
* live snapshot supplies the candidate events and drives `canTake`/`send`.
|
|
448
443
|
* For a top-level request this is the root actor (identity-equal to
|
|
449
444
|
* `runCtx.actorHolder.actorRef`); for a request inside an invoked child
|
|
450
|
-
* machine it is that child's actor, so a child decision
|
|
445
|
+
* machine it is that child's actor, so a child decision reads and drives
|
|
451
446
|
* the CHILD's snapshot — not the root's. Read off `self._parent`, with the
|
|
452
447
|
* root actor as a fallback.
|
|
453
448
|
*/
|
|
@@ -542,7 +537,7 @@ function wrapTextLogicForRunAgent(logic, runCtx) {
|
|
|
542
537
|
}
|
|
543
538
|
});
|
|
544
539
|
}
|
|
545
|
-
function createCountingDecide(runCtx, self
|
|
540
|
+
function createCountingDecide(runCtx, self) {
|
|
546
541
|
return async (attemptRequest) => {
|
|
547
542
|
runCtx.consumeModelCall();
|
|
548
543
|
runCtx.onTrace?.({
|
|
@@ -558,7 +553,7 @@ function createCountingDecide(runCtx, self, kind = "decision") {
|
|
|
558
553
|
if (usage) {
|
|
559
554
|
const { src } = selfIdAndSrc(self);
|
|
560
555
|
runCtx.recordUsage?.(usage, {
|
|
561
|
-
kind,
|
|
556
|
+
kind: "decision",
|
|
562
557
|
...attemptRequest.id ? { id: attemptRequest.id } : {},
|
|
563
558
|
...src !== "" ? { src } : {},
|
|
564
559
|
model: attemptRequest.model
|
|
@@ -595,8 +590,8 @@ function createCountingDecide(runCtx, self, kind = "decision") {
|
|
|
595
590
|
* logic here that calls `resolveDecision` itself, reusing `logic.request(...)`
|
|
596
591
|
* to build the request the same way the original logic would have.
|
|
597
592
|
*
|
|
598
|
-
* On success it SENDS the chosen event to the invoking actor (auto-delivery
|
|
599
|
-
*
|
|
593
|
+
* On success it SENDS the chosen event to the invoking actor (auto-delivery)
|
|
594
|
+
* and then completes with that event
|
|
600
595
|
* as its output — so callers never wire an `onDone` to deliver it. See the
|
|
601
596
|
* send-then-complete note inside `run` for how exit-cancels-invoke interacts
|
|
602
597
|
* with `onDone`.
|
|
@@ -633,85 +628,6 @@ function createRunAgentDecisionLogic(logic, runCtx) {
|
|
|
633
628
|
});
|
|
634
629
|
}
|
|
635
630
|
/**
|
|
636
|
-
* Builds the plan actor logic runAgent installs in place of the `agent.plan`
|
|
637
|
-
* builtin: iterated {@link resolveDecision}. Each step re-reads the live
|
|
638
|
-
* snapshot (so the candidate set reflects everything applied so far), asks
|
|
639
|
-
* the `decide` executor for one legal event with the same validation/retry
|
|
640
|
-
* loop a decision gets, and sends it to the machine. The loop ends on a
|
|
641
|
-
* `stopOn` event, at `maxSteps`, when no legal candidate remains, or when an
|
|
642
|
-
* applied event exits the invoking state (xstate cancels this invoke — the
|
|
643
|
-
* machine simply moves on and the pending output is discarded).
|
|
644
|
-
*
|
|
645
|
-
* The invoke's `input` is resolved once, so the prompt cannot re-render
|
|
646
|
-
* context between steps; instead the applied trail is appended to the prompt
|
|
647
|
-
* each step so the model can see plan progress.
|
|
648
|
-
*/
|
|
649
|
-
function createRunAgentPlanLogic(logic, runCtx) {
|
|
650
|
-
const planLogic = (0, xstate.createAsyncLogic)({ run: async ({ input, signal, self }) => {
|
|
651
|
-
if (!runCtx.decide) throw new Error("runAgent: no 'decide' executor provided.");
|
|
652
|
-
const { id } = selfIdAndSrc(self);
|
|
653
|
-
const stopOn = new Set(input.stopOn ?? []);
|
|
654
|
-
const countingDecide = createCountingDecide(runCtx, self, "plan");
|
|
655
|
-
const base = logic.request(input);
|
|
656
|
-
let ledger = require_decision.initialPlanLedger(logic, input);
|
|
657
|
-
const end = (stopped) => {
|
|
658
|
-
ledger = require_decision.advancePlanLedger(logic, ledger, {
|
|
659
|
-
type: "plan.ended",
|
|
660
|
-
stopped
|
|
661
|
-
});
|
|
662
|
-
return ledger.output;
|
|
663
|
-
};
|
|
664
|
-
await Promise.resolve();
|
|
665
|
-
const invokingActor = invokingActorOf(self, runCtx);
|
|
666
|
-
while (ledger.context.stepsRemaining > 0) {
|
|
667
|
-
const actorRef = invokingActor;
|
|
668
|
-
if (!actorRef || signal.aborted) break;
|
|
669
|
-
const machineEvents = require_decision.getAcceptedEvents(actorRef.getSnapshot(), {
|
|
670
|
-
schemas: runCtx.schemas,
|
|
671
|
-
eventTypes: logic.allowedEventTypes(input)
|
|
672
|
-
});
|
|
673
|
-
if (machineEvents.length === 0) return end("no-legal-events");
|
|
674
|
-
const doneDescriptor = {
|
|
675
|
-
type: require_decision.PLAN_DONE_EVENT_TYPE,
|
|
676
|
-
toolName: require_decision.sanitizeEventToolName(require_decision.PLAN_DONE_EVENT_TYPE)
|
|
677
|
-
};
|
|
678
|
-
const events = machineEvents.some((event) => event.type === "agent.plan.done") ? machineEvents : [...machineEvents, doneDescriptor];
|
|
679
|
-
const applied = ledger.context.applied;
|
|
680
|
-
const trail = applied.length === 0 ? "" : `\n\nEvents already applied in this plan, in order:\n${applied.map((step) => JSON.stringify(step)).join("\n")}\nContinue from here; do not repeat applied events.`;
|
|
681
|
-
const doneHint = `\n\nWhen the request is fully handled (or no action is needed), choose '${require_decision.PLAN_DONE_EVENT_TYPE}'.`;
|
|
682
|
-
const chosen = await require_decision.resolveDecision({
|
|
683
|
-
...base,
|
|
684
|
-
id: `${id}[${applied.length}]`,
|
|
685
|
-
events,
|
|
686
|
-
prompt: `${base.prompt ?? ""}${trail}${doneHint}`,
|
|
687
|
-
attempts: []
|
|
688
|
-
}, countingDecide, {
|
|
689
|
-
maxRetries: input.maxRetries ?? logic.maxRetries,
|
|
690
|
-
signal,
|
|
691
|
-
canTake: (event) => {
|
|
692
|
-
if (event.type === "agent.plan.done" || stopOn.has(event.type)) return true;
|
|
693
|
-
return invokingActor ? invokingActor.getSnapshot().can(event) : true;
|
|
694
|
-
}
|
|
695
|
-
});
|
|
696
|
-
if (chosen.type === "agent.plan.done") return end("done");
|
|
697
|
-
actorRef.send(chosen);
|
|
698
|
-
ledger = require_decision.advancePlanLedger(logic, ledger, {
|
|
699
|
-
type: "plan.applied",
|
|
700
|
-
event: chosen
|
|
701
|
-
});
|
|
702
|
-
await Promise.resolve();
|
|
703
|
-
if (stopOn.has(chosen.type)) return end("stop-event");
|
|
704
|
-
}
|
|
705
|
-
return end("max-steps");
|
|
706
|
-
} });
|
|
707
|
-
return Object.assign(planLogic, {
|
|
708
|
-
kind: "statelyai.planLogic",
|
|
709
|
-
maxRetries: logic.maxRetries,
|
|
710
|
-
request: logic.request,
|
|
711
|
-
allowedEventTypes: logic.allowedEventTypes
|
|
712
|
-
});
|
|
713
|
-
}
|
|
714
|
-
/**
|
|
715
631
|
* The set of string-keyed actor `src`s the machine's own config invokes
|
|
716
632
|
* (top-level, recursing into child STATES but not into invoked child
|
|
717
633
|
* machines). {@link provideExecutors} uses it to require an executor only for a
|
|
@@ -851,17 +767,9 @@ function bindDecisionForProvide(machine, logic, executors, options) {
|
|
|
851
767
|
return createRunAgentDecisionLogic(logic, provideBindContext(machine, executors, options));
|
|
852
768
|
}
|
|
853
769
|
/**
|
|
854
|
-
* Host-binds one `agent.plan` source for {@link provideExecutors}: runAgent's
|
|
855
|
-
* plan wrapper (iterated snapshot-driven decisions, auto-delivery) with the same
|
|
856
|
-
* request-level tracing runAgent emits, minus run-scoped counting. @internal
|
|
857
|
-
*/
|
|
858
|
-
function bindPlanForProvide(machine, logic, executors, options) {
|
|
859
|
-
return createRunAgentPlanLogic(logic, provideBindContext(machine, executors, options));
|
|
860
|
-
}
|
|
861
|
-
/**
|
|
862
770
|
* Recursively rebinds an invoked child machine's own agent sources with the
|
|
863
771
|
* SAME host-backed wrappers runAgent applies to the top-level machine, so a
|
|
864
|
-
* child's text/stream/decision
|
|
772
|
+
* child's text/stream/decision requests inherit runAgent's executors and
|
|
865
773
|
* participate in maxModelCalls counting, onTrace/onChunk/onResult exactly like
|
|
866
774
|
* parent requests. Returns the child machine to invoke: a `.provide`-rebound
|
|
867
775
|
* copy when any inner source needed wrapping, else the original untouched.
|
|
@@ -882,10 +790,6 @@ function rebindChildMachine(childMachine, runCtx, visited) {
|
|
|
882
790
|
if (!require_decision.executorBoundLogics.has(logic)) wrapped[key] = createRunAgentDecisionLogic(logic, runCtx);
|
|
883
791
|
continue;
|
|
884
792
|
}
|
|
885
|
-
if (require_decision.isPlanLogic(logic)) {
|
|
886
|
-
wrapped[key] = createRunAgentPlanLogic(logic, runCtx);
|
|
887
|
-
continue;
|
|
888
|
-
}
|
|
889
793
|
if (require_decision.isTextLogic(logic)) {
|
|
890
794
|
if (!require_decision.executorBoundLogics.has(logic)) wrapped[key] = wrapTextLogicForRunAgent(logic, runCtx);
|
|
891
795
|
continue;
|
|
@@ -1085,10 +989,6 @@ function createAgentSession(machine, options, lifecycle) {
|
|
|
1085
989
|
wrappedSources[key] = createRunAgentDecisionLogic(logic, runCtx);
|
|
1086
990
|
continue;
|
|
1087
991
|
}
|
|
1088
|
-
if (require_decision.isPlanLogic(logic)) {
|
|
1089
|
-
wrappedSources[key] = createRunAgentPlanLogic(logic, runCtx);
|
|
1090
|
-
continue;
|
|
1091
|
-
}
|
|
1092
992
|
if (require_decision.isTextLogic(logic)) {
|
|
1093
993
|
if (!require_decision.executorBoundLogics.has(logic)) wrappedSources[key] = wrapTextLogicForRunAgent(logic, runCtx);
|
|
1094
994
|
continue;
|
|
@@ -1225,10 +1125,8 @@ function createAgentSession(machine, options, lifecycle) {
|
|
|
1225
1125
|
settle({
|
|
1226
1126
|
status: "idle",
|
|
1227
1127
|
snapshot: current,
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
persistedSnapshot: actor.getPersistedSnapshot()
|
|
1231
|
-
} : {}
|
|
1128
|
+
persistedSnapshot: actor.getPersistedSnapshot(),
|
|
1129
|
+
...pendingUserInputs.length > 0 ? { pendingUserInputs } : {}
|
|
1232
1130
|
});
|
|
1233
1131
|
};
|
|
1234
1132
|
let interpreting = false;
|
|
@@ -1359,7 +1257,13 @@ function createAgentSession(machine, options, lifecycle) {
|
|
|
1359
1257
|
return;
|
|
1360
1258
|
}
|
|
1361
1259
|
if (!deliveringResumeEvent && isSuspended(snapshot) && isIdleSnapshot(snapshot, { ignoreUserInputChildren: userInputIsPlaceholder })) {
|
|
1362
|
-
|
|
1260
|
+
queueMicrotask(() => {
|
|
1261
|
+
if (settled) return;
|
|
1262
|
+
const current = actor.getSnapshot();
|
|
1263
|
+
if (isSuspended(current) && isIdleSnapshot(current, { ignoreUserInputChildren: userInputIsPlaceholder })) {
|
|
1264
|
+
if (!maybeInterpret(current)) settleIdle(current);
|
|
1265
|
+
} else scheduleIdleCheck();
|
|
1266
|
+
});
|
|
1363
1267
|
return;
|
|
1364
1268
|
}
|
|
1365
1269
|
scheduleIdleCheck();
|
|
@@ -1499,7 +1403,10 @@ function isIdleSnapshot(snapshot, { ignoreUserInputChildren }) {
|
|
|
1499
1403
|
if (Object.values(snapshot.children ?? {}).some((child) => {
|
|
1500
1404
|
const ref = child;
|
|
1501
1405
|
if (ignoreUserInputChildren && ref?.src === "agent.userInput") return false;
|
|
1502
|
-
|
|
1406
|
+
const childSnapshot = ref?.getSnapshot?.();
|
|
1407
|
+
if (childSnapshot?.status !== "active") return false;
|
|
1408
|
+
if ((0, xstate.isMachineSnapshot)(childSnapshot)) return !isIdleSnapshot(childSnapshot, { ignoreUserInputChildren });
|
|
1409
|
+
return true;
|
|
1503
1410
|
})) return false;
|
|
1504
1411
|
return !(0, xstate.getNextTransitions)(snapshot).some((transitionDef) => transitionDef.eventType === "" || transitionDef.eventType.startsWith("xstate.after"));
|
|
1505
1412
|
}
|
|
@@ -1638,7 +1545,6 @@ function createAgentRun(machine, options) {
|
|
|
1638
1545
|
* - decision / `agent.decide` source → `executors.decide` (snapshot-driven
|
|
1639
1546
|
* candidate events, guard `canTake`, and auto-delivery of the chosen event,
|
|
1640
1547
|
* mirroring `runAgent` but without its model-call counting)
|
|
1641
|
-
* - `agent.plan` source → `executors.decide` (iterated, same semantics)
|
|
1642
1548
|
*
|
|
1643
1549
|
* Pass `options.onTrace` to observe request-level trace events (identical in
|
|
1644
1550
|
* shape to `runAgent`'s); pair it with {@link traceTransitions} on the actor's
|
|
@@ -1676,14 +1582,6 @@ function provideExecutors(machine, executors, options = {}) {
|
|
|
1676
1582
|
wrappedSources[key] = bindDecisionForProvide(provided, logic, executors, bindOptions);
|
|
1677
1583
|
continue;
|
|
1678
1584
|
}
|
|
1679
|
-
if (require_decision.isPlanLogic(logic)) {
|
|
1680
|
-
if (!executors.decide) {
|
|
1681
|
-
if (invokedSrcs.has(key)) throw missingExecutorError(key, "plan", "decide");
|
|
1682
|
-
continue;
|
|
1683
|
-
}
|
|
1684
|
-
wrappedSources[key] = bindPlanForProvide(provided, logic, executors, bindOptions);
|
|
1685
|
-
continue;
|
|
1686
|
-
}
|
|
1687
1585
|
if (require_decision.isTextLogic(logic)) {
|
|
1688
1586
|
if (require_decision.executorBoundLogics.has(logic)) continue;
|
|
1689
1587
|
if (!(logic.mode === "stream" ? executors.streamText : executors.generateText)) {
|
|
@@ -1702,7 +1600,6 @@ function missingExecutorError(src, kind, executor) {
|
|
|
1702
1600
|
//#endregion
|
|
1703
1601
|
//#region src/verify.ts
|
|
1704
1602
|
const DECIDE_SRC = "agent.decide";
|
|
1705
|
-
const PLAN_SRC = "agent.plan";
|
|
1706
1603
|
function normalizeInvokes(invoke) {
|
|
1707
1604
|
if (invoke === void 0 || invoke === null) return [];
|
|
1708
1605
|
return Array.isArray(invoke) ? invoke : [invoke];
|
|
@@ -1839,17 +1736,12 @@ function hasNonEmptyOn(config) {
|
|
|
1839
1736
|
function decisionKindOf(src, actors) {
|
|
1840
1737
|
if (typeof src === "string") {
|
|
1841
1738
|
if (src === DECIDE_SRC) return "decision";
|
|
1842
|
-
|
|
1843
|
-
const logic = actors[src];
|
|
1844
|
-
if (require_decision.isDecisionLogic(logic)) return "decision";
|
|
1845
|
-
if (require_decision.isPlanLogic(logic)) return "plan";
|
|
1846
|
-
return;
|
|
1739
|
+
return require_decision.isDecisionLogic(actors[src]) ? "decision" : void 0;
|
|
1847
1740
|
}
|
|
1848
|
-
|
|
1849
|
-
if (require_decision.isPlanLogic(src)) return "plan";
|
|
1741
|
+
return require_decision.isDecisionLogic(src) ? "decision" : void 0;
|
|
1850
1742
|
}
|
|
1851
1743
|
function isAgentLogicNeedingBinding(src) {
|
|
1852
|
-
return (require_decision.isTextLogic(src) || require_decision.isDecisionLogic(src)
|
|
1744
|
+
return (require_decision.isTextLogic(src) || require_decision.isDecisionLogic(src)) && !require_decision.executorBoundLogics.has(src);
|
|
1853
1745
|
}
|
|
1854
1746
|
function schemaExposesJson(schema) {
|
|
1855
1747
|
if (!schema) return false;
|
|
@@ -2130,17 +2022,6 @@ async function simulateAgent(machine, options) {
|
|
|
2130
2022
|
});
|
|
2131
2023
|
continue;
|
|
2132
2024
|
}
|
|
2133
|
-
if (request.kind === "plan") {
|
|
2134
|
-
const taken = takeFromQueue(script.decisions, request.src);
|
|
2135
|
-
if (!taken.found) throw scriptDryError("decision", request.src, request.id, request);
|
|
2136
|
-
const event = taken.value;
|
|
2137
|
-
step = await require_setup_agent.resolveAgentRequests(machine, step, { decide: async () => ({ event }) });
|
|
2138
|
-
trail.push({
|
|
2139
|
-
state: step.snapshot.value,
|
|
2140
|
-
appliedEvent: taken.value
|
|
2141
|
-
});
|
|
2142
|
-
continue;
|
|
2143
|
-
}
|
|
2144
2025
|
const taken = takeFromQueue(script.text, request.src);
|
|
2145
2026
|
if (!taken.found) throw scriptDryError("text", request.src, request.id);
|
|
2146
2027
|
step = require_setup_agent.resolveAgentStep(machine, step, request, taken.value);
|
|
@@ -2224,7 +2105,7 @@ async function explore(machine, options, stopWhen) {
|
|
|
2224
2105
|
recordState(current.snapshot);
|
|
2225
2106
|
continue;
|
|
2226
2107
|
}
|
|
2227
|
-
if (request &&
|
|
2108
|
+
if (request && request.kind === "decision") return { step: current };
|
|
2228
2109
|
const [invoke] = pendingInvokes(current);
|
|
2229
2110
|
if (invoke) {
|
|
2230
2111
|
if (!(invoke.src in textOutputs)) return {
|
|
@@ -2271,8 +2152,7 @@ async function explore(machine, options, stopWhen) {
|
|
|
2271
2152
|
return;
|
|
2272
2153
|
}
|
|
2273
2154
|
const request = settled.requests[0];
|
|
2274
|
-
const
|
|
2275
|
-
const branchEvents = request?.kind === "decision" || request?.kind === "plan" ? request.events.map((descriptor) => ({ type: descriptor.type })) : require_decision.getAcceptedEvents(settled.snapshot).map((descriptor) => ({ type: descriptor.type }));
|
|
2155
|
+
const branchEvents = request?.kind === "decision" ? request.events.map((descriptor) => ({ type: descriptor.type })) : require_decision.getAcceptedEvents(settled.snapshot).map((descriptor) => ({ type: descriptor.type }));
|
|
2276
2156
|
if (branchEvents.length === 0) {
|
|
2277
2157
|
pathsExplored++;
|
|
2278
2158
|
terminals.push({
|
|
@@ -2297,11 +2177,11 @@ async function explore(machine, options, stopWhen) {
|
|
|
2297
2177
|
if (pathsExplored >= maxPaths) hitPathCap = true;
|
|
2298
2178
|
return;
|
|
2299
2179
|
}
|
|
2300
|
-
if (!
|
|
2180
|
+
if (!settled.snapshot.can(event)) {
|
|
2301
2181
|
prunedByGuard++;
|
|
2302
2182
|
continue;
|
|
2303
2183
|
}
|
|
2304
|
-
const next =
|
|
2184
|
+
const next = require_setup_agent.transitionAgentStep(machine, settled, event);
|
|
2305
2185
|
recordState(next.snapshot);
|
|
2306
2186
|
await visit(next, [...path, event], depth + 1);
|
|
2307
2187
|
}
|
|
@@ -2324,10 +2204,7 @@ async function explore(machine, options, stopWhen) {
|
|
|
2324
2204
|
* depth, model-free, and reports which states are reached and how each path
|
|
2325
2205
|
* terminates. At each decision request it forks one branch per candidate event
|
|
2326
2206
|
* (guard-rejected candidates are counted in `prunedByGuard`, not explored); at
|
|
2327
|
-
* an idle wait it forks per externally-accepted event.
|
|
2328
|
-
* forks the same way — one branch per candidate, including the reserved
|
|
2329
|
-
* `agent.plan.done` move — advancing each branch through the real plan protocol
|
|
2330
|
-
* (`resolveAgentRequests`), so a plan can consume several depth units. Text/`userInput` invokes
|
|
2207
|
+
* an idle wait it forks per externally-accepted event. Text/`userInput` invokes
|
|
2331
2208
|
* are resolved from `textOutputs` (a by-src canned-output map) — a missing src
|
|
2332
2209
|
* halts that branch with a `needs-output` terminal rather than throwing.
|
|
2333
2210
|
*
|
|
@@ -2811,7 +2688,6 @@ exports.AgentReplayDivergenceError = require_setup_agent.AgentReplayDivergenceEr
|
|
|
2811
2688
|
exports.AgentReplayMachineMismatchError = require_setup_agent.AgentReplayMachineMismatchError;
|
|
2812
2689
|
exports.AgentSnapshotVersionMismatchError = AgentSnapshotVersionMismatchError;
|
|
2813
2690
|
exports.NonSerializableAgentEventError = require_event_log_store.NonSerializableAgentEventError;
|
|
2814
|
-
exports.PLAN_DONE_EVENT_TYPE = require_decision.PLAN_DONE_EVENT_TYPE;
|
|
2815
2691
|
exports.appendMessages = require_setup_agent.appendMessages;
|
|
2816
2692
|
exports.assertAgentLogEntry = require_event_log_store.assertAgentLogEntry;
|
|
2817
2693
|
exports.assertAgentMachine = assertAgentMachine;
|