@statelyai/agent 2.0.0-alpha.34 → 2.0.0-alpha.36

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.
Files changed (130) hide show
  1. package/CHANGELOG.md +124 -0
  2. package/dist/act-B9hLQls6.mjs +19 -0
  3. package/dist/act-CrX0JwWY.cjs +42 -0
  4. package/dist/ai-sdk.cjs +63 -7
  5. package/dist/ai-sdk.d.cts +13 -3
  6. package/dist/ai-sdk.d.mts +13 -3
  7. package/dist/ai-sdk.mjs +63 -7
  8. package/dist/{decision-BEsATcr1.cjs → decision-CMeuNZ1g.cjs} +262 -4
  9. package/dist/{decision-COkJNjOW.mjs → decision-m5hGmHpF.mjs} +239 -5
  10. package/dist/decision-object-B5zGSPFp.d.mts +15 -0
  11. package/dist/decision-object-DBcLWVvv.d.cts +15 -0
  12. package/dist/{run-agent-s5D32w99.cjs → effects-B23bFZEs.cjs} +948 -102
  13. package/dist/{run-agent-Ct_GZw_f.mjs → effects-CuOUNYtZ.mjs} +914 -104
  14. package/dist/{event-log-C7jeH0y8.mjs → event-log-DkVlk3Rh.mjs} +30 -15
  15. package/dist/{event-log-BWSSLUff.cjs → event-log-Dkc-yIYu.cjs} +41 -14
  16. package/dist/{event-log-store-BzXehP7R.d.cts → event-log-store-BBS8so8a.d.mts} +2 -2
  17. package/dist/{event-log-store-W49TCJXi.mjs → event-log-store-Brx9tOHt.mjs} +1 -1
  18. package/dist/{event-log-store-CleWpsp5.cjs → event-log-store-Cl-YWDnq.cjs} +1 -1
  19. package/dist/{event-log-store-CWjN6lmM.d.mts → event-log-store-bc4ICGcS.d.cts} +2 -2
  20. package/dist/index.cjs +660 -286
  21. package/dist/index.d.cts +168 -9
  22. package/dist/index.d.mts +168 -9
  23. package/dist/index.mjs +639 -271
  24. package/dist/internal-C3Lqu538.d.cts +43 -0
  25. package/dist/internal-DUcdhk-i.d.mts +43 -0
  26. package/dist/log.cjs +2 -2
  27. package/dist/log.d.cts +1 -1
  28. package/dist/log.d.mts +1 -1
  29. package/dist/log.mjs +2 -2
  30. package/dist/machines.cjs +457 -41
  31. package/dist/machines.d.cts +114 -43
  32. package/dist/machines.d.mts +114 -43
  33. package/dist/machines.mjs +457 -42
  34. package/dist/{model-source-DmhTMK39.cjs → model-source-B4WQ8Idp.cjs} +1 -1
  35. package/dist/{model-source-CK3F4YuU.mjs → model-source-kjGwqAV8.mjs} +1 -1
  36. package/dist/openai.cjs +149 -36
  37. package/dist/openai.d.cts +28 -10
  38. package/dist/openai.d.mts +28 -10
  39. package/dist/openai.mjs +149 -36
  40. package/dist/otel.d.cts +1 -1
  41. package/dist/otel.d.mts +1 -1
  42. package/dist/{run-agent-BGdx5913.d.mts → run-agent-BcQ-9NRT.d.cts} +91 -9
  43. package/dist/{run-agent-B1-Eqviq.d.cts → run-agent-C86drZtN.d.mts} +91 -9
  44. package/dist/{setup-agent-05uw4dPZ.mjs → setup-agent-BDmNr6PC.mjs} +9 -3
  45. package/dist/{setup-agent-BtfeFfF6.d.cts → setup-agent-BIKcbVXV.d.mts} +4 -4
  46. package/dist/{setup-agent-CXEBURdo.cjs → setup-agent-CmEToB8y.cjs} +9 -3
  47. package/dist/{setup-agent-C6nqzv_X.d.mts → setup-agent-jlo4vtaZ.d.cts} +4 -4
  48. package/dist/tanstack-ai.cjs +62 -4
  49. package/dist/tanstack-ai.d.cts +18 -6
  50. package/dist/tanstack-ai.d.mts +18 -6
  51. package/dist/tanstack-ai.mjs +62 -4
  52. package/dist/testing.cjs +536 -67
  53. package/dist/testing.d.cts +197 -180
  54. package/dist/testing.d.mts +197 -180
  55. package/dist/testing.mjs +532 -67
  56. package/dist/{text-logic-CV_4KGr6.d.cts → text-logic-BqxalgMr.d.mts} +87 -4
  57. package/dist/{text-logic-Ckh-rJFV.d.mts → text-logic-CSf9bqpP.d.cts} +87 -4
  58. package/dist/{types-Dp5_Ru4R.d.cts → types-DQUOOaZO.d.cts} +1 -1
  59. package/dist/{types-Dp5_Ru4R.d.mts → types-DQUOOaZO.d.mts} +1 -1
  60. package/dist/{usage-BDiJKyv9.d.cts → usage-DoP0Oi84.d.mts} +1 -1
  61. package/dist/{usage-DOoVUGVq.d.mts → usage-HtnrUsl9.d.cts} +1 -1
  62. package/dist/utils-7lSa4H-i.d.cts +403 -0
  63. package/dist/utils-CBbKw2fM.d.mts +403 -0
  64. package/dist/validate.cjs +44 -12
  65. package/dist/validate.d.cts +6 -6
  66. package/dist/validate.d.mts +6 -6
  67. package/dist/validate.mjs +44 -12
  68. package/dist/verify-Bink-Drw.cjs +2304 -0
  69. package/dist/{verify-BQpE8hUz.d.cts → verify-Buh8XSUr.d.mts} +208 -14
  70. package/dist/{verify-CqeuEmIk.d.mts → verify-CciG8UP-.d.cts} +208 -14
  71. package/dist/verify-Dpz1u5Pe.mjs +2251 -0
  72. package/dist/with-model-calls-B_WLmOSl.cjs +248 -0
  73. package/dist/with-model-calls-BkOwdroS.mjs +213 -0
  74. package/docs/act.md +102 -0
  75. package/docs/advanced.md +105 -0
  76. package/docs/any-stack.md +62 -0
  77. package/docs/choosing-a-run-mode.md +55 -0
  78. package/docs/debugging.md +29 -0
  79. package/docs/decisions.md +473 -0
  80. package/docs/environment-and-policy.md +188 -0
  81. package/docs/evals.md +509 -0
  82. package/docs/event-log.md +311 -0
  83. package/docs/from-a-loop.md +528 -0
  84. package/docs/generate-machines.md +110 -0
  85. package/docs/hosts.md +414 -0
  86. package/docs/human-in-the-loop.md +177 -0
  87. package/docs/index.md +48 -0
  88. package/docs/judgments.md +281 -0
  89. package/docs/langgraph-comparison.md +198 -0
  90. package/docs/machines-as-data.md +454 -0
  91. package/docs/machines-presets.md +198 -0
  92. package/docs/machines.md +497 -0
  93. package/docs/messages.md +67 -0
  94. package/docs/meta.json +52 -0
  95. package/docs/models-and-providers.md +268 -0
  96. package/docs/multi-agent.md +68 -0
  97. package/docs/observability.md +109 -0
  98. package/docs/patterns.md +31 -0
  99. package/docs/persistence.md +136 -0
  100. package/docs/quickstart.md +121 -0
  101. package/docs/roadmap.md +22 -0
  102. package/docs/scope.md +75 -0
  103. package/docs/statechart-policy-examples.md +69 -0
  104. package/docs/steps.md +54 -0
  105. package/docs/strangler-fig.md +323 -0
  106. package/docs/text-requests.md +250 -0
  107. package/docs/thinking-in-state-machines.md +324 -0
  108. package/docs/tools.md +249 -0
  109. package/docs/upgrading.md +37 -0
  110. package/docs/usage-and-budgets.md +516 -0
  111. package/docs/validate-and-repair.md +253 -0
  112. package/docs/verify.md +487 -0
  113. package/docs/xstate-4-and-5-apps.md +161 -0
  114. package/llms.txt +53 -0
  115. package/package.json +10 -3
  116. package/readme.md +30 -15
  117. package/skills/add-evals/SKILL.md +3 -1
  118. package/skills/add-traces/SKILL.md +6 -1
  119. package/skills/build-machine/SKILL.md +81 -0
  120. package/skills/generate-machine/SKILL.md +1 -1
  121. package/skills/graph-engineering/SKILL.md +13 -3
  122. package/skills/improve-machine/SKILL.md +21 -2
  123. package/skills/loop-to-machine/SKILL.md +147 -190
  124. package/skills/loop-to-machine/reference.md +260 -0
  125. package/dist/utils-BnXdWih3.d.mts +0 -108
  126. package/dist/utils-ebVpCYoF.d.cts +0 -108
  127. package/dist/verify-Bpahmr34.mjs +0 -1241
  128. package/dist/verify-DCLZa9R0.cjs +0 -1288
  129. package/dist/with-model-calls-BXYdBdcu.cjs +0 -45
  130. package/dist/with-model-calls-Bv4V4EHC.mjs +0 -34
package/dist/machines.mjs CHANGED
@@ -1,4 +1,5 @@
1
- import { r as setupAgent } from "./setup-agent-05uw4dPZ.mjs";
1
+ import { r as setupAgent } from "./setup-agent-BDmNr6PC.mjs";
2
+ import { createAsyncLogic } from "xstate";
2
3
  //#region src/machines/internal.ts
3
4
  /** The builtin inline text request every preset lowers a request entry to. */
4
5
  const GENERATE_TEXT_SRC = "agent.generateText";
@@ -127,12 +128,12 @@ function renderEntryList(entries) {
127
128
  }
128
129
  //#endregion
129
130
  //#region src/machines/tool-loop.ts
130
- const contextSchema$6 = objectSchema({
131
+ const contextSchema$7 = objectSchema({
131
132
  prompt: jsonString,
132
133
  result: jsonAny
133
134
  }, ["prompt"]);
134
- const inputSchema$6 = objectSchema({ prompt: jsonString }, ["prompt"]);
135
- const outputSchema$5 = objectSchema({ result: jsonAny }, ["result"]);
135
+ const inputSchema$7 = objectSchema({ prompt: jsonString }, ["prompt"]);
136
+ const outputSchema$6 = objectSchema({ result: jsonAny }, ["result"]);
136
137
  /**
137
138
  * The single-state tool loop: one text request carries the `tools`, and the
138
139
  * host runs the tool loop inside it (`maxSteps` bounds it). Selecting and
@@ -157,9 +158,9 @@ const outputSchema$5 = objectSchema({ result: jsonAny }, ["result"]);
157
158
  function createToolLoopMachine(config) {
158
159
  const { model, instructions, tools, outputSchema: resultSchema, maxSteps } = config;
159
160
  return setupAgent({
160
- context: contextSchema$6,
161
- input: inputSchema$6,
162
- output: outputSchema$5
161
+ context: contextSchema$7,
162
+ input: inputSchema$7,
163
+ output: outputSchema$6
163
164
  }).createMachine({
164
165
  id: "tool-loop",
165
166
  version: "1",
@@ -195,13 +196,13 @@ function createToolLoopMachine(config) {
195
196
  }
196
197
  //#endregion
197
198
  //#region src/machines/sequential.ts
198
- const contextSchema$5 = objectSchema({
199
+ const contextSchema$6 = objectSchema({
199
200
  prompt: jsonString,
200
201
  results: jsonRecord,
201
202
  previous: jsonAny
202
203
  }, ["prompt", "results"]);
203
- const inputSchema$5 = objectSchema({ prompt: jsonString }, ["prompt"]);
204
- const outputSchema$4 = objectSchema({
204
+ const inputSchema$6 = objectSchema({ prompt: jsonString }, ["prompt"]);
205
+ const outputSchema$5 = objectSchema({
205
206
  results: jsonRecord,
206
207
  output: jsonAny
207
208
  }, ["results"]);
@@ -227,9 +228,9 @@ function createSequentialMachine(config) {
227
228
  const { model, steps } = config;
228
229
  assertEntryNames("step", steps.map((step) => step.name), ["done"]);
229
230
  const agentSetup = setupAgent({
230
- context: contextSchema$5,
231
- input: inputSchema$5,
232
- output: outputSchema$4
231
+ context: contextSchema$6,
232
+ input: inputSchema$6,
233
+ output: outputSchema$5
233
234
  });
234
235
  const states = {};
235
236
  steps.forEach((step, index) => {
@@ -282,13 +283,13 @@ function createSequentialMachine(config) {
282
283
  }
283
284
  //#endregion
284
285
  //#region src/machines/router.ts
285
- const contextSchema$4 = objectSchema({
286
+ const contextSchema$5 = objectSchema({
286
287
  prompt: jsonString,
287
288
  route: { type: ["string", "null"] },
288
289
  result: jsonAny
289
290
  }, ["prompt"]);
290
- const inputSchema$4 = objectSchema({ prompt: jsonString }, ["prompt"]);
291
- const outputSchema$3 = objectSchema({
291
+ const inputSchema$5 = objectSchema({ prompt: jsonString }, ["prompt"]);
292
+ const outputSchema$4 = objectSchema({
292
293
  route: { type: ["string", "null"] },
293
294
  result: jsonAny
294
295
  }, ["route"]);
@@ -321,9 +322,9 @@ function createRouterMachine(config) {
321
322
  assertEntryNames("route", names, ["routing", "done"]);
322
323
  if (fallback !== void 0 && !names.includes(fallback)) throw new Error(`createRouterMachine: fallback '${fallback}' is not a declared route (${names.join(", ")}).`);
323
324
  const agentSetup = setupAgent({
324
- context: contextSchema$4,
325
- input: inputSchema$4,
326
- output: outputSchema$3,
325
+ context: contextSchema$5,
326
+ input: inputSchema$5,
327
+ output: outputSchema$4,
327
328
  events: Object.fromEntries(names.map((name) => [routeEventType(name), emptyPayload])),
328
329
  actors: machineActors(routes)
329
330
  });
@@ -381,12 +382,12 @@ function createRouterMachine(config) {
381
382
  }
382
383
  //#endregion
383
384
  //#region src/machines/parallel.ts
384
- const contextSchema$3 = objectSchema({
385
+ const contextSchema$4 = objectSchema({
385
386
  prompt: jsonString,
386
387
  results: jsonRecord
387
388
  }, ["prompt", "results"]);
388
- const inputSchema$3 = objectSchema({ prompt: jsonString }, ["prompt"]);
389
- const outputSchema$2 = objectSchema({ results: jsonRecord }, ["results"]);
389
+ const inputSchema$4 = objectSchema({ prompt: jsonString }, ["prompt"]);
390
+ const outputSchema$3 = objectSchema({ results: jsonRecord }, ["results"]);
390
391
  /**
391
392
  * Static fan-out: every branch runs concurrently as its own region of one
392
393
  * parallel state, and the run joins when all of them finish. Results are keyed
@@ -412,9 +413,9 @@ function createParallelMachine(config) {
412
413
  const { model, branches } = config;
413
414
  assertEntryNames("branch", Object.keys(branches), ["running", "done"]);
414
415
  const agentSetup = setupAgent({
415
- context: contextSchema$3,
416
- input: inputSchema$3,
417
- output: outputSchema$2,
416
+ context: contextSchema$4,
417
+ input: inputSchema$4,
418
+ output: outputSchema$3,
418
419
  actors: machineActors(branches)
419
420
  });
420
421
  const regions = {};
@@ -460,7 +461,7 @@ function createParallelMachine(config) {
460
461
  }
461
462
  //#endregion
462
463
  //#region src/machines/loop.ts
463
- const contextSchema$2 = objectSchema({
464
+ const contextSchema$3 = objectSchema({
464
465
  prompt: jsonString,
465
466
  iterations: jsonNumber,
466
467
  results: jsonArray,
@@ -470,8 +471,8 @@ const contextSchema$2 = objectSchema({
470
471
  "iterations",
471
472
  "results"
472
473
  ]);
473
- const inputSchema$2 = objectSchema({ prompt: jsonString }, ["prompt"]);
474
- const outputSchema$1 = objectSchema({
474
+ const inputSchema$3 = objectSchema({ prompt: jsonString }, ["prompt"]);
475
+ const outputSchema$2 = objectSchema({
475
476
  iterations: jsonNumber,
476
477
  results: jsonArray,
477
478
  last: jsonAny
@@ -496,9 +497,9 @@ function createLoopMachine(config) {
496
497
  const { model, body, until, maxTurns } = config;
497
498
  if (!Number.isInteger(maxTurns) || maxTurns < 1) throw new Error("createLoopMachine: maxTurns must be an integer >= 1.");
498
499
  const agentSetup = setupAgent({
499
- context: contextSchema$2,
500
- input: inputSchema$2,
501
- output: outputSchema$1,
500
+ context: contextSchema$3,
501
+ input: inputSchema$3,
502
+ output: outputSchema$2,
502
503
  actors: isMachineEntry(body) ? { body: body.machine } : {}
503
504
  });
504
505
  const loopState = (context) => ({
@@ -552,7 +553,7 @@ function createLoopMachine(config) {
552
553
  }
553
554
  //#endregion
554
555
  //#region src/machines/supervisor.ts
555
- const contextSchema$1 = objectSchema({
556
+ const contextSchema$2 = objectSchema({
556
557
  task: jsonString,
557
558
  results: jsonRecord,
558
559
  turns: jsonNumber,
@@ -562,8 +563,8 @@ const contextSchema$1 = objectSchema({
562
563
  "results",
563
564
  "turns"
564
565
  ]);
565
- const inputSchema$1 = objectSchema({ task: jsonString }, ["task"]);
566
- const outputSchema = objectSchema({
566
+ const inputSchema$2 = objectSchema({ task: jsonString }, ["task"]);
567
+ const outputSchema$1 = objectSchema({
567
568
  results: jsonRecord,
568
569
  turns: jsonNumber
569
570
  }, ["results", "turns"]);
@@ -605,9 +606,9 @@ function createSupervisorMachine(config) {
605
606
  assertEntryNames("worker", names, ["supervising", "done"]);
606
607
  if (!Number.isInteger(maxTurns) || maxTurns < 1) throw new Error("createSupervisorMachine: maxTurns must be an integer >= 1.");
607
608
  const agentSetup = setupAgent({
608
- context: contextSchema$1,
609
- input: inputSchema$1,
610
- output: outputSchema,
609
+ context: contextSchema$2,
610
+ input: inputSchema$2,
611
+ output: outputSchema$1,
611
612
  events: {
612
613
  ...Object.fromEntries(names.map((name) => [delegateEventType(name), emptyPayload])),
613
614
  [FINISH_EVENT_TYPE]: emptyPayload
@@ -679,12 +680,12 @@ function renderResults(results) {
679
680
  }
680
681
  //#endregion
681
682
  //#region src/machines/handoff.ts
682
- const contextSchema = objectSchema({
683
+ const contextSchema$1 = objectSchema({
683
684
  message: jsonString,
684
685
  activeAgent: jsonString,
685
686
  reply: jsonAny
686
687
  }, ["message", "activeAgent"]);
687
- const inputSchema = objectSchema({
688
+ const inputSchema$1 = objectSchema({
688
689
  message: jsonString,
689
690
  activeAgent: jsonString
690
691
  }, ["message"]);
@@ -728,8 +729,8 @@ function createHandoffMachine(config) {
728
729
  assertEntryNames("agent", names, ["routing", "waiting"]);
729
730
  if (!names.includes(defaultActiveAgent)) throw new Error(`createHandoffMachine: defaultActiveAgent '${defaultActiveAgent}' is not a declared agent (${names.join(", ")}).`);
730
731
  const agentSetup = setupAgent({
731
- context: contextSchema,
732
- input: inputSchema,
732
+ context: contextSchema$1,
733
+ input: inputSchema$1,
733
734
  events: Object.fromEntries(names.map((name) => [transferEventType(name), transferPayload])),
734
735
  actors: machineActors(agents)
735
736
  });
@@ -770,4 +771,418 @@ function createHandoffMachine(config) {
770
771
  return agentSetup.createMachine(machineConfig);
771
772
  }
772
773
  //#endregion
773
- export { FINISH_EVENT_TYPE, createHandoffMachine, createLoopMachine, createParallelMachine, createRouterMachine, createSequentialMachine, createSupervisorMachine, createToolLoopMachine, delegateEventType, routeEventType, transferEventType };
774
+ //#region src/machines/approval-batch.ts
775
+ const CHILD_PREFIX = "call:";
776
+ /** The actor source key the per-call child machine is registered under. */
777
+ const CALL_SRC = "call";
778
+ const askEveryCall = createAsyncLogic({ run: async () => "ask" });
779
+ const callSchema = {
780
+ type: "object",
781
+ properties: {
782
+ id: jsonString,
783
+ name: jsonString
784
+ }
785
+ };
786
+ const callIdPayload = objectSchema({ callId: jsonString }, ["callId"]);
787
+ function settledResult(results, id) {
788
+ return Object.hasOwn(results, id) ? results[id] : void 0;
789
+ }
790
+ function errorMessage(error) {
791
+ return error instanceof Error ? error.message : String(error);
792
+ }
793
+ function createCallMachine(config) {
794
+ const callSetup = setupAgent({
795
+ context: objectSchema({
796
+ call: callSchema,
797
+ decision: { type: ["string", "null"] },
798
+ approvedBy: { type: ["string", "null"] },
799
+ result: jsonAny,
800
+ error: { type: ["string", "null"] },
801
+ reason: { type: ["string", "null"] }
802
+ }, ["call"]),
803
+ input: objectSchema({ call: callSchema }, ["call"]),
804
+ output: objectSchema({
805
+ id: jsonString,
806
+ status: jsonString
807
+ }, ["id", "status"]),
808
+ events: {
809
+ APPROVE: emptyPayload,
810
+ APPROVE_ALWAYS: emptyPayload,
811
+ REJECT: objectSchema({ reason: jsonString }),
812
+ RELEASE: emptyPayload
813
+ },
814
+ actors: {
815
+ policy: config.policy ?? askEveryCall,
816
+ execute: config.execute
817
+ }
818
+ });
819
+ const toParent = ({ context, parent }, enq, type) => {
820
+ if (parent) enq.sendTo(parent, {
821
+ type,
822
+ callId: context.call.id
823
+ });
824
+ };
825
+ return callSetup.createMachine({
826
+ id: "approval-call",
827
+ version: "1",
828
+ context: ({ input }) => ({
829
+ call: input.call,
830
+ decision: null,
831
+ approvedBy: null,
832
+ result: null,
833
+ error: null,
834
+ reason: null
835
+ }),
836
+ initial: "checking",
837
+ states: {
838
+ checking: { invoke: {
839
+ id: "policy",
840
+ src: "policy",
841
+ input: ({ context }) => ({ call: context.call }),
842
+ onDone: ({ output }) => {
843
+ switch (output) {
844
+ case "allow": return {
845
+ target: "approved",
846
+ context: {
847
+ decision: "allow",
848
+ approvedBy: "policy"
849
+ }
850
+ };
851
+ case "ask": return {
852
+ target: "awaitingApproval",
853
+ context: { decision: "ask" }
854
+ };
855
+ case "deny": return {
856
+ target: "rejected",
857
+ context: {
858
+ decision: "deny",
859
+ reason: "denied by policy"
860
+ }
861
+ };
862
+ default: return {
863
+ target: "failed",
864
+ context: { error: `policy returned ${JSON.stringify(output)}` }
865
+ };
866
+ }
867
+ },
868
+ onError: ({ event }) => ({
869
+ target: "failed",
870
+ context: { error: errorMessage(event.error) }
871
+ })
872
+ } },
873
+ awaitingApproval: {
874
+ entry: (args, enq) => toParent(args, enq, "CALL_ASKING"),
875
+ meta: { interaction: {
876
+ label: "Run {call.name}?",
877
+ events: {
878
+ APPROVE: {
879
+ label: "Approve",
880
+ style: "primary"
881
+ },
882
+ APPROVE_ALWAYS: "Always approve",
883
+ REJECT: {
884
+ label: "Reject",
885
+ style: "danger"
886
+ }
887
+ },
888
+ textEvent: {
889
+ type: "REJECT",
890
+ field: "reason"
891
+ }
892
+ } },
893
+ on: {
894
+ APPROVE: {
895
+ target: "approved",
896
+ context: { approvedBy: "user" }
897
+ },
898
+ APPROVE_ALWAYS: {
899
+ target: "approved",
900
+ context: { approvedBy: "user" }
901
+ },
902
+ REJECT: ({ event }) => ({
903
+ target: "rejected",
904
+ context: { reason: event.reason ?? "rejected" }
905
+ })
906
+ }
907
+ },
908
+ approved: {
909
+ entry: (args, enq) => toParent(args, enq, "CALL_READY"),
910
+ on: { RELEASE: { target: "executing" } }
911
+ },
912
+ executing: { invoke: {
913
+ id: "execute",
914
+ src: "execute",
915
+ input: ({ context }) => ({ call: context.call }),
916
+ onDone: ({ output }) => ({
917
+ target: "executed",
918
+ context: { result: output }
919
+ }),
920
+ onError: ({ event }) => ({
921
+ target: "failed",
922
+ context: { error: errorMessage(event.error) }
923
+ })
924
+ } },
925
+ executed: {
926
+ type: "final",
927
+ output: ({ context }) => ({
928
+ id: context.call.id,
929
+ status: "executed",
930
+ result: context.result
931
+ })
932
+ },
933
+ rejected: {
934
+ type: "final",
935
+ output: ({ context }) => ({
936
+ id: context.call.id,
937
+ status: "rejected",
938
+ reason: context.reason ?? "rejected"
939
+ })
940
+ },
941
+ failed: {
942
+ type: "final",
943
+ output: ({ context }) => ({
944
+ id: context.call.id,
945
+ status: "failed",
946
+ error: context.error ?? "failed"
947
+ })
948
+ }
949
+ }
950
+ });
951
+ }
952
+ function choicesFor(awaiting) {
953
+ return awaiting.flatMap((call) => [
954
+ {
955
+ label: `Approve ${call.name} (${call.id})`,
956
+ event: {
957
+ type: "APPROVE",
958
+ callId: call.id
959
+ }
960
+ },
961
+ {
962
+ label: `Always approve ${call.name} (${call.id})`,
963
+ event: {
964
+ type: "APPROVE_ALWAYS",
965
+ callId: call.id
966
+ }
967
+ },
968
+ {
969
+ label: `Reject ${call.name} (${call.id})`,
970
+ event: {
971
+ type: "REJECT",
972
+ callId: call.id
973
+ }
974
+ }
975
+ ]);
976
+ }
977
+ const contextSchema = objectSchema({
978
+ calls: jsonArray,
979
+ awaiting: jsonArray,
980
+ choices: jsonArray,
981
+ ready: jsonArray,
982
+ results: jsonRecord,
983
+ approveAlways: jsonArray
984
+ }, [
985
+ "calls",
986
+ "awaiting",
987
+ "choices",
988
+ "ready",
989
+ "results",
990
+ "approveAlways"
991
+ ]);
992
+ const inputSchema = objectSchema({ calls: jsonArray }, ["calls"]);
993
+ const outputSchema = objectSchema({
994
+ results: jsonArray,
995
+ approveAlways: jsonArray
996
+ }, ["results", "approveAlways"]);
997
+ /**
998
+ * One model turn's tool calls, each approved or rejected individually; only
999
+ * approved calls execute. One child actor per call:
1000
+ *
1001
+ * `checking` (policy) → `awaitingApproval` (person) → `approved` → `executing`
1002
+ * → `executed` | `rejected` | `failed`.
1003
+ *
1004
+ * The parent routes `APPROVE` / `APPROVE_ALWAYS` / `REJECT` to the child by
1005
+ * `callId`, holds approved calls at the `executeWhen` barrier, and collects
1006
+ * outputs. `CANCEL_ALL` stops every unfinished child and ends the batch; those
1007
+ * calls report `cancelled`, including one whose execute was in flight.
1008
+ *
1009
+ * States: `running` → `done` | `cancelled`.
1010
+ *
1011
+ * ```ts
1012
+ * const machine = createApprovalBatchMachine({
1013
+ * policy: createAsyncLogic({
1014
+ * run: async ({ input }) => (input.call.name === "search" ? "allow" : "ask"),
1015
+ * }),
1016
+ * execute: createAsyncLogic({
1017
+ * run: async ({ input }) => tools[input.call.name](input.call.args),
1018
+ * }),
1019
+ * });
1020
+ * ```
1021
+ */
1022
+ function createApprovalBatchMachine(config) {
1023
+ const executeWhen = config.executeWhen ?? "allReady";
1024
+ const callMachine = createCallMachine(config);
1025
+ const agentSetup = setupAgent({
1026
+ context: contextSchema,
1027
+ input: inputSchema,
1028
+ output: outputSchema,
1029
+ events: {
1030
+ APPROVE: callIdPayload,
1031
+ APPROVE_ALWAYS: callIdPayload,
1032
+ REJECT: objectSchema({
1033
+ callId: jsonString,
1034
+ reason: jsonString
1035
+ }, ["callId"]),
1036
+ CANCEL_ALL: emptyPayload,
1037
+ CALL_ASKING: callIdPayload,
1038
+ CALL_READY: callIdPayload
1039
+ },
1040
+ actors: { [CALL_SRC]: callMachine }
1041
+ });
1042
+ const child = (children, id) => children[`${CHILD_PREFIX}${id}`];
1043
+ const settle = (args, enq, result) => {
1044
+ const { context } = args;
1045
+ const results = {
1046
+ ...context.results,
1047
+ [result.id]: result
1048
+ };
1049
+ const awaiting = context.awaiting.filter((call) => call.id !== result.id);
1050
+ const next = {
1051
+ results,
1052
+ awaiting,
1053
+ ready: context.ready.filter((id) => id !== result.id),
1054
+ choices: choicesFor(awaiting)
1055
+ };
1056
+ if (Object.keys(results).length === context.calls.length) return {
1057
+ target: "done",
1058
+ context: next
1059
+ };
1060
+ return { context: {
1061
+ ...next,
1062
+ ready: release({
1063
+ ...args,
1064
+ context: {
1065
+ ...context,
1066
+ ...next
1067
+ }
1068
+ }, enq)
1069
+ } };
1070
+ };
1071
+ const release = ({ context, children }, enq) => {
1072
+ const settled = Object.keys(context.results).length + context.ready.length;
1073
+ if (context.ready.length === 0 || settled < context.calls.length) return context.ready;
1074
+ for (const id of context.ready) enq.sendTo(child(children, id), { type: "RELEASE" });
1075
+ return [];
1076
+ };
1077
+ const forward = (type) => ({ context, event, children }, enq) => {
1078
+ const call = context.awaiting.find((candidate) => candidate.id === event.callId);
1079
+ if (!call) return void 0;
1080
+ enq.sendTo(child(children, call.id), type === "REJECT" && event.reason !== void 0 ? {
1081
+ type,
1082
+ reason: event.reason
1083
+ } : { type });
1084
+ const awaiting = context.awaiting.filter((candidate) => candidate.id !== call.id);
1085
+ const approveAlways = type === "APPROVE_ALWAYS" && !context.approveAlways.some((rule) => rule.name === call.name) ? [...context.approveAlways, { name: call.name }] : context.approveAlways;
1086
+ return { context: {
1087
+ awaiting,
1088
+ choices: choicesFor(awaiting),
1089
+ approveAlways
1090
+ } };
1091
+ };
1092
+ const output = ({ context }) => ({
1093
+ results: context.calls.map((call) => settledResult(context.results, call.id) ?? {
1094
+ id: call.id,
1095
+ status: "cancelled"
1096
+ }),
1097
+ approveAlways: context.approveAlways
1098
+ });
1099
+ const machineConfig = {
1100
+ id: "approval-batch",
1101
+ version: "1",
1102
+ context: ({ input }) => {
1103
+ if (new Set(input.calls.map((call) => call.id)).size !== input.calls.length) throw new Error("createApprovalBatchMachine: call ids must be unique.");
1104
+ return {
1105
+ calls: input.calls,
1106
+ awaiting: [],
1107
+ choices: [],
1108
+ ready: [],
1109
+ results: {},
1110
+ approveAlways: []
1111
+ };
1112
+ },
1113
+ initial: "running",
1114
+ states: {
1115
+ running: {
1116
+ entry: ({ context }, enq) => {
1117
+ for (const call of context.calls) enq.spawn(CALL_SRC, {
1118
+ id: `${CHILD_PREFIX}${call.id}`,
1119
+ input: { call }
1120
+ });
1121
+ },
1122
+ always: ({ context }) => context.calls.length === 0 ? { target: "done" } : void 0,
1123
+ meta: { interaction: {
1124
+ label: "Approve the proposed tool calls.",
1125
+ choices: "choices"
1126
+ } },
1127
+ on: {
1128
+ CALL_ASKING: ({ context, event }) => {
1129
+ const call = context.calls.find((candidate) => candidate.id === event.callId);
1130
+ if (!call || settledResult(context.results, call.id) || context.awaiting.some((candidate) => candidate.id === call.id)) return;
1131
+ const awaiting = [...context.awaiting, call];
1132
+ return { context: {
1133
+ awaiting,
1134
+ choices: choicesFor(awaiting)
1135
+ } };
1136
+ },
1137
+ CALL_READY: (args, enq) => {
1138
+ const { context, event, children } = args;
1139
+ const id = event.callId;
1140
+ if (!id || !context.calls.some((call) => call.id === id)) return void 0;
1141
+ if (context.ready.includes(id) || settledResult(context.results, id)) return void 0;
1142
+ if (executeWhen === "eachReady") {
1143
+ enq.sendTo(child(children, id), { type: "RELEASE" });
1144
+ return {};
1145
+ }
1146
+ const ready = [...context.ready, id];
1147
+ return { context: { ready: release({
1148
+ ...args,
1149
+ context: {
1150
+ ...context,
1151
+ ready
1152
+ }
1153
+ }, enq) } };
1154
+ },
1155
+ APPROVE: forward("APPROVE"),
1156
+ APPROVE_ALWAYS: forward("APPROVE_ALWAYS"),
1157
+ REJECT: forward("REJECT"),
1158
+ CANCEL_ALL: { target: "cancelled" },
1159
+ "xstate.done.actor": (args, enq) => {
1160
+ const { actorId, output: result } = args.event;
1161
+ if (!actorId?.startsWith(CHILD_PREFIX)) return void 0;
1162
+ return settle(args, enq, result);
1163
+ },
1164
+ "xstate.error.actor": (args, enq) => {
1165
+ const { actorId } = args.event;
1166
+ if (!actorId?.startsWith(CHILD_PREFIX)) return void 0;
1167
+ return settle(args, enq, {
1168
+ id: actorId.slice(5),
1169
+ status: "failed",
1170
+ error: errorMessage(args.event.error)
1171
+ });
1172
+ }
1173
+ }
1174
+ },
1175
+ done: {
1176
+ type: "final",
1177
+ output
1178
+ },
1179
+ cancelled: {
1180
+ type: "final",
1181
+ output
1182
+ }
1183
+ }
1184
+ };
1185
+ return agentSetup.createMachine(machineConfig);
1186
+ }
1187
+ //#endregion
1188
+ export { FINISH_EVENT_TYPE, createApprovalBatchMachine, createHandoffMachine, createLoopMachine, createParallelMachine, createRouterMachine, createSequentialMachine, createSupervisorMachine, createToolLoopMachine, delegateEventType, routeEventType, transferEventType };
@@ -1,4 +1,4 @@
1
- const require_event_log = require("./event-log-BWSSLUff.cjs");
1
+ const require_event_log = require("./event-log-Dkc-yIYu.cjs");
2
2
  //#region src/internal/model-source.ts
3
3
  function hasEntries(value) {
4
4
  return require_event_log.isRecord(value) && Object.keys(value).length > 0;
@@ -1,4 +1,4 @@
1
- import { W as getAgentExecutionOptions, ft as isRecord } from "./event-log-C7jeH0y8.mjs";
1
+ import { K as getAgentExecutionOptions, mt as isRecord } from "./event-log-DkVlk3Rh.mjs";
2
2
  //#region src/internal/model-source.ts
3
3
  function hasEntries(value) {
4
4
  return isRecord(value) && Object.keys(value).length > 0;