@statelyai/agent 2.0.0-alpha.28 → 2.0.0-alpha.30

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 (80) hide show
  1. package/dist/ai-sdk.cjs +150 -27
  2. package/dist/ai-sdk.d.cts +59 -36
  3. package/dist/ai-sdk.d.mts +59 -36
  4. package/dist/ai-sdk.mjs +148 -24
  5. package/dist/{decision-RQ0iv0oZ.mjs → decision-BHkvuImD.mjs} +74 -8
  6. package/dist/{decision-CmM8-HHz.cjs → decision-CIb7ddIv.cjs} +113 -30
  7. package/dist/{text-logic-f5F5sCdc.cjs → event-log-BZRfBRd7.cjs} +814 -7
  8. package/dist/{text-logic-CIqHAmBi.mjs → event-log-DJCMHek_.mjs} +680 -4
  9. package/dist/{event-log-store-BIPOz-di.d.mts → event-log-store-CIIJohjj.d.mts} +90 -5
  10. package/dist/{event-log-store-2xzFmgr2.d.cts → event-log-store-CLAC9q2Y.d.cts} +90 -5
  11. package/dist/event-log-store-DZR4IIWe.cjs +95 -0
  12. package/dist/event-log-store-DsP6GCRh.mjs +84 -0
  13. package/dist/index.cjs +576 -58
  14. package/dist/index.d.cts +104 -510
  15. package/dist/index.d.mts +104 -510
  16. package/dist/index.mjs +547 -32
  17. package/dist/log.cjs +77 -20
  18. package/dist/log.d.cts +5 -5
  19. package/dist/log.d.mts +5 -5
  20. package/dist/log.mjs +58 -3
  21. package/dist/machines.cjs +1 -1
  22. package/dist/machines.d.cts +15 -15
  23. package/dist/machines.d.mts +15 -15
  24. package/dist/machines.mjs +1 -1
  25. package/dist/model-source-BCKg4qJ1.cjs +44 -0
  26. package/dist/model-source-BaHwJ1wK.mjs +39 -0
  27. package/dist/openai.cjs +11 -11
  28. package/dist/openai.d.cts +28 -26
  29. package/dist/openai.d.mts +28 -26
  30. package/dist/openai.mjs +3 -3
  31. package/dist/otel.cjs +5 -4
  32. package/dist/otel.d.cts +5 -7
  33. package/dist/otel.d.mts +5 -7
  34. package/dist/otel.mjs +5 -3
  35. package/dist/{run-agent-xNTxpt82.mjs → run-agent-BpHqkaZw.mjs} +181 -44
  36. package/dist/{run-agent-COXXc0RW.d.mts → run-agent-CAVt5R-r.d.mts} +95 -30
  37. package/dist/{run-agent-ByO-Bxyd.d.cts → run-agent-CTlMc3Jb.d.cts} +95 -30
  38. package/dist/{run-agent-Dmf8GV2d.cjs → run-agent-ctLCvNgL.cjs} +230 -88
  39. package/dist/{setup-agent-BuanDXeh.mjs → setup-agent-7rV_cujV.mjs} +15 -9
  40. package/dist/setup-agent-CGELn1qZ.d.mts +577 -0
  41. package/dist/setup-agent-CLPCkXsu.d.cts +577 -0
  42. package/dist/{setup-agent-DV4cTou-.cjs → setup-agent-DoUk7EdC.cjs} +31 -20
  43. package/dist/tanstack-ai.cjs +627 -0
  44. package/dist/tanstack-ai.d.cts +204 -0
  45. package/dist/tanstack-ai.d.mts +204 -0
  46. package/dist/tanstack-ai.mjs +618 -0
  47. package/dist/testing.cjs +204 -985
  48. package/dist/testing.d.cts +132 -347
  49. package/dist/testing.d.mts +132 -347
  50. package/dist/testing.mjs +191 -973
  51. package/dist/{text-logic-IjfYy9DP.d.cts → text-logic-BRYOPWvY.d.mts} +52 -12
  52. package/dist/{text-logic-D-czVPYp.d.mts → text-logic-DmciuVUQ.d.cts} +52 -12
  53. package/dist/{types-BgNbNKnJ.d.mts → types-Dp5_Ru4R.d.cts} +2 -2
  54. package/dist/{types-hR5f5YXa.d.cts → types-Dp5_Ru4R.d.mts} +2 -2
  55. package/dist/usage-BkM3z02Y.d.mts +17 -0
  56. package/dist/usage-Cm_ovz9V.d.cts +17 -0
  57. package/dist/{utils-_q7F-AZu.d.mts → utils-BnXdWih3.d.mts} +1 -2
  58. package/dist/{utils-o4YRdHJX.d.cts → utils-ebVpCYoF.d.cts} +1 -2
  59. package/dist/validate.cjs +239 -8
  60. package/dist/validate.d.cts +110 -3
  61. package/dist/validate.d.mts +110 -3
  62. package/dist/validate.mjs +238 -7
  63. package/dist/verify-COFWyprJ.mjs +1181 -0
  64. package/dist/verify-CVlsTZ-V.cjs +1222 -0
  65. package/dist/verify-Cn86rT8l.d.mts +414 -0
  66. package/dist/verify-Dzwd4Mvk.d.cts +414 -0
  67. package/package.json +44 -31
  68. package/readme.md +40 -28
  69. package/skills/add-evals/SKILL.md +129 -0
  70. package/skills/add-traces/SKILL.md +135 -0
  71. package/skills/generate-machine/SKILL.md +143 -69
  72. package/skills/graph-engineering/SKILL.md +33 -0
  73. package/skills/improve-machine/SKILL.md +241 -0
  74. package/skills/improve-machine/reference.md +101 -0
  75. package/skills/loop-to-machine/SKILL.md +183 -0
  76. package/skills/loop-to-machine/policy.md +158 -0
  77. package/dist/event-log-store-2pAFiWuc.mjs +0 -576
  78. package/dist/event-log-store-C-COhIXP.cjs +0 -702
  79. package/dist/usage-BdEjFuIi.mjs +0 -5
  80. package/dist/usage-efOQPdf_.cjs +0 -10
@@ -1,23 +1,9 @@
1
- import { _ as StandardSchemaV1, m as InferInput, s as AgentTools, w as WithAgentInputSchema } from "./types-hR5f5YXa.cjs";
2
- import { K as AgentRequestSource, M as AgentDecisionRequest, U as AgentEventDescriptor, Z as AgentError, d as AgentRequestMode, f as AgentTextRequest, m as AgentUsage, r as AgentFinishReason, t as AgentCallUsage, u as AgentRequestExecutors } from "./text-logic-IjfYy9DP.cjs";
3
- import { c as AgentLogEntry, n as AgentEventLogStore } from "./event-log-store-2xzFmgr2.cjs";
4
- import { AnyActorLogic, AnyActorRef, AnyMachineSnapshot, AnyStateMachine, AsyncActorLogic, EmittedFrom, EventFromLogic, EventObject, InputFrom, InspectionEvent, OutputFrom, Snapshot, SnapshotFrom } from "xstate";
1
+ import { _ as StandardSchemaV1, m as InferInput, s as AgentTools, w as WithAgentInputSchema } from "./types-Dp5_Ru4R.cjs";
2
+ import { I as DecisionAttempt, N as AgentDecisionRequest, Q as AgentError, W as AgentEventDescriptor, d as AgentRequestMode, f as AgentTextRequest, m as AgentUsage, q as AgentRequestSource, r as AgentFinishReason, t as AgentCallUsage, u as AgentRequestExecutors } from "./text-logic-DmciuVUQ.cjs";
3
+ import { n as AgentUsageEvent } from "./usage-Cm_ovz9V.cjs";
4
+ import { c as AgentLogEntry, h as JsonValue$1, n as AgentEventLogStore } from "./event-log-store-CLAC9q2Y.cjs";
5
+ import { AnyActorLogic, AnyActorRef, AnyMachineSnapshot, AnyStateMachine, AsyncActorLogic, EmittedFrom, EventFromLogic, EventObject, InputFrom, InspectionEvent, OutputFrom, Snapshot, SnapshotFrom, StateValue } from "xstate";
5
6
  import { DurableExecution } from "xstate/durable";
6
-
7
- //#region src/usage.d.ts
8
- /** Reserved event delivered after a model call reports token usage. */
9
- declare const AGENT_USAGE_EVENT_TYPE: "@agent.usage";
10
- /** Machine-owned usage signal. Handle it with an ordinary XState transition. */
11
- interface AgentUsageEvent extends EventObject {
12
- type: typeof AGENT_USAGE_EVENT_TYPE;
13
- usage: AgentCallUsage;
14
- kind?: "text" | "decision";
15
- id?: string;
16
- src?: string;
17
- model?: string;
18
- name?: string;
19
- }
20
- //#endregion
21
7
  //#region src/steps.d.ts
22
8
  /**
23
9
  * A pending text request surfaced by step discovery ({@link getAgentRequests}
@@ -81,11 +67,13 @@ type AgentTransitionHandler<TMachine extends AnyStateMachine> = (snapshot: Snaps
81
67
  */
82
68
  declare const AGENT_TRACE_SCHEMA_VERSION = 1;
83
69
  type AgentTraceEvent<TMachine extends AnyStateMachine = AnyStateMachine> = {
84
- /** The {@link AGENT_TRACE_SCHEMA_VERSION} the event was produced with. */schemaVersion: typeof AGENT_TRACE_SCHEMA_VERSION;
70
+ /** The {@link AGENT_TRACE_SCHEMA_VERSION} the event was produced with. */
71
+ schemaVersion: typeof AGENT_TRACE_SCHEMA_VERSION;
85
72
  runId: string;
86
73
  seq: number;
87
74
  timestamp: string;
88
- machineId: string; /** The machine's own `version`, else its structural hash. */
75
+ machineId: string;
76
+ /** The machine's own `version`, else its structural hash. */
89
77
  machineVersion: string;
90
78
  } & ({
91
79
  type: "run.start";
@@ -122,6 +110,15 @@ type AgentTraceEvent<TMachine extends AnyStateMachine = AnyStateMachine> = {
122
110
  chunk: string;
123
111
  } | {
124
112
  type: "machine.transition";
113
+ /**
114
+ * Where the event arrived: the state value before this transition. The
115
+ * first transition of a stream (the init event, or the restore on
116
+ * resume) has no earlier state, so its `from.value` is its own
117
+ * `snapshot.value`. An object so more of the prior snapshot can join it.
118
+ */
119
+ from: {
120
+ value: StateValue;
121
+ };
125
122
  snapshot: SnapshotFrom<TMachine>;
126
123
  event: EventFromLogic<TMachine>;
127
124
  } | {
@@ -132,7 +129,8 @@ type AgentTraceEvent<TMachine extends AnyStateMachine = AnyStateMachine> = {
132
129
  * tokens still fold into {@link AgentRunResult.usage}; only the machine
133
130
  * event is dropped. */
134
131
  type: "usage.dropped";
135
- event: AgentUsageEvent; /** `'settled'`: the call settled after the run's cycle had resolved. */
132
+ event: AgentUsageEvent;
133
+ /** `'settled'`: the call settled after the run's cycle had resolved. */
136
134
  reason: "settled";
137
135
  } | ({
138
136
  type: "run.end";
@@ -159,7 +157,7 @@ type AgentTraceEvent<TMachine extends AnyStateMachine = AnyStateMachine> = {
159
157
  */
160
158
  type TraceVerbatimKey = (typeof TRACE_ENVELOPE_KEYS)[number] | "reason";
161
159
  /** One trace variant's JSON projection. Homomorphic, so `?` modifiers survive. @internal */
162
- type JsonProjectedTraceVariant<TEvent> = { [K in keyof TEvent]: K extends TraceVerbatimKey ? TEvent[K] : JsonValue };
160
+ type JsonProjectedTraceVariant<TEvent> = { [K in keyof TEvent]: K extends TraceVerbatimKey ? TEvent[K] : JsonValue; };
163
161
  /**
164
162
  * `raw` is written only when `includeRaw` was set, so it is optional on the
165
163
  * JSON side even though the live trace always carries it. @internal
@@ -178,7 +176,7 @@ type WithOptionalRaw<T> = "raw" extends keyof T ? Omit<T, "raw"> & {
178
176
  * miss the JSON side. `src/serialize-trace-event.test.ts` pins the result to
179
177
  * the shape this type had when it was hand-maintained.
180
178
  */
181
- type JsonSerializableTraceEvent = AgentTraceEvent extends infer TEvent ? TEvent extends unknown ? WithOptionalRaw<JsonProjectedTraceVariant<TEvent>> : never : never;
179
+ type JsonSerializableTraceEvent = AgentTraceEvent extends (infer TEvent) ? TEvent extends unknown ? WithOptionalRaw<JsonProjectedTraceVariant<TEvent>> : never : never;
182
180
  declare const TRACE_ENVELOPE_KEYS: readonly ["schemaVersion", "runId", "seq", "timestamp", "machineId", "machineVersion", "type", "status", "cause", "reasoning", "finishReason", "chunk"];
183
181
  /**
184
182
  * Projects an {@link AgentTraceEvent} into a guaranteed JSON-safe envelope —
@@ -253,6 +251,14 @@ interface AgentRuntimeOptions<TMachine extends AnyStateMachine> {
253
251
  * the same state. Set `false` for a smaller, unverifiable log.
254
252
  */
255
253
  verification?: boolean;
254
+ /**
255
+ * Host-owned JSON stored on the log's `@agent.init` entry when this run
256
+ * starts a log: what produced the run, such as a policy version, the model
257
+ * ids, or the scenario. A resume keeps the stamp of the run that started
258
+ * the log; a version bridge, which opens a new log segment, takes this
259
+ * run's. `executionId` and `migratedFrom` are reserved and cannot be set here.
260
+ */
261
+ metadata?: Record<string, JsonValue$1>;
256
262
  /** Actor source implementations, merged onto the machine before binding — sugar for `machine.provide({ actors })` ahead of the run. */
257
263
  actors?: Record<string, AnyActorLogic>;
258
264
  /**
@@ -292,7 +298,7 @@ interface AgentRuntimeOptions<TMachine extends AnyStateMachine> {
292
298
  */
293
299
  on?: { [TType in EmittedFrom<TMachine>["type"] | "*"]?: (emitted: EmittedFrom<TMachine> & (TType extends "*" ? unknown : {
294
300
  type: TType;
295
- })) => void };
301
+ })) => void; };
296
302
  /**
297
303
  * Raw xstate inspection passthrough: fires for every inspection event in
298
304
  * the whole actor system — root machine, invoked child machines, spawned
@@ -549,8 +555,15 @@ interface AgentRunStart<TMachine extends AnyStateMachine> {
549
555
  interface AgentRuntime<TMachine extends AnyStateMachine> {
550
556
  /** Opens the run: a fresh start from `input`, a resume from `snapshot`, or recovery from a log. */
551
557
  start(init?: AgentRunStart<TMachine>): Promise<[SnapshotFrom<TMachine>, AgentEffects]>;
552
- /** Applies one event: journals it, traces it, and returns the next state plus its effects. */
553
- transition(state: SnapshotFrom<TMachine>, event: EventFromLogic<TMachine> | AgentTimerEvent): [SnapshotFrom<TMachine>, AgentEffects];
558
+ /**
559
+ * Applies one event: journals it, traces it, and returns the next state plus
560
+ * its effects. `options.attempts` records the failed attempts of a decider
561
+ * the host ran itself (e.g. `decideNext`'s `attempts`) on the event's entry
562
+ * as `entry.attempts`. Replay ignores them; an empty list is not journaled.
563
+ */
564
+ transition(state: SnapshotFrom<TMachine>, event: EventFromLogic<TMachine> | AgentTimerEvent, options?: {
565
+ attempts?: DecisionAttempt[];
566
+ }): [SnapshotFrom<TMachine>, AgentEffects];
554
567
  /** Starts the effects' work and returns once it is accepted, not once it finishes. */
555
568
  execute(effects: AgentEffects): Promise<void>;
556
569
  /** The next event that arrived, or `undefined` once the run is quiescent. */
@@ -570,6 +583,12 @@ interface AgentRunInit<TMachine extends AnyStateMachine> extends AgentRunStart<T
570
583
  * due is delivered the same way, as an {@link AgentTimerEvent}.
571
584
  */
572
585
  event?: EventFromLogic<TMachine> | AgentTimerEvent;
586
+ /**
587
+ * The failed attempts of the decider the host ran to choose `event` (e.g.
588
+ * `decideNext`'s `attempts`), journaled on `event`'s entry as
589
+ * `entry.attempts`. Replay ignores them.
590
+ */
591
+ attempts?: DecisionAttempt[];
573
592
  }
574
593
  /**
575
594
  * The blocking host: opens the run, delivers `event` if given, then applies
@@ -578,6 +597,51 @@ interface AgentRunInit<TMachine extends AnyStateMachine> extends AgentRunStart<T
578
597
  * own stop policy; this one is for scripts and tests.
579
598
  */
580
599
  declare function runToQuiescence<TMachine extends AnyStateMachine>(runtime: AgentRuntime<TMachine>, init?: AgentRunInit<TMachine>): Promise<AgentRunResult<TMachine>>;
600
+ /** Everything {@link runAgent} takes: the runtime's options plus how to open the run. */
601
+ interface AgentRunOptions<TMachine extends AnyStateMachine> extends AgentRuntimeOptions<TMachine>, AgentRunInit<TMachine> {
602
+ /**
603
+ * Answers an idle state for a host that can block for it. When given,
604
+ * {@link runAgent} loops: each time the run settles `idle`, it calls
605
+ * `onIdle` with the idle snapshot, persists the run, and resumes from that
606
+ * persisted snapshot with the returned event — until the run is `done` or
607
+ * `error`. Each pass is a fresh {@link createAgentRuntime} over the same
608
+ * options, so nothing of the earlier pass is held across the wait; the log
609
+ * travels with the result (or the store's thread), so the final `events`
610
+ * replays every pass, and `maxModelCalls` bounds the whole call. An answer
611
+ * the idle state does not handle ends the loop: the result is `idle` with
612
+ * {@link AgentRunResult.ignored} set, not another call to `onIdle`.
613
+ *
614
+ * A host that cannot block for the answer (a request handler, a durable
615
+ * object) leaves this out, stores `result.persist()`, and calls
616
+ * {@link runAgent} again later with `{ snapshot, event }`.
617
+ */
618
+ onIdle?: (snapshot: SnapshotFrom<TMachine>) => Promise<EventFromLogic<TMachine>>;
619
+ }
620
+ /**
621
+ * Runs `machine` to quiescence in one call: {@link createAgentRuntime} over
622
+ * `options`, then {@link runToQuiescence} with the same object as its init.
623
+ * One options object carries both halves (`executors`, `store`, callbacks,
624
+ * and `input` | `snapshot` + `event`), and the result is the same
625
+ * {@link AgentRunResult}. With {@link AgentRunOptions.onIdle}, idle states are
626
+ * answered in place and the call returns only `done` or `error`.
627
+ *
628
+ * Reach for {@link createAgentRuntime} directly when the host needs the loop
629
+ * itself: a non-blocking durable host, events pushed mid-run, or a custom stop
630
+ * or checkpoint policy.
631
+ *
632
+ * @example
633
+ * ```ts
634
+ * const result = await runAgent(machine, { input: { topic }, executors });
635
+ * if (result.status === "done") console.log(result.output);
636
+ * ```
637
+ *
638
+ * @example
639
+ * ```ts
640
+ * // Resume a run a request handler left idle.
641
+ * const result = await runAgent(machine, { executors, snapshot, event });
642
+ * ```
643
+ */
644
+ declare function runAgent<TMachine extends AnyStateMachine>(machine: TMachine, options?: AgentRunOptions<TMachine>): Promise<AgentRunResult<TMachine>>;
581
645
  /**
582
646
  * Creates the helpers for one run of `machine`: executors bound to its
583
647
  * requests, the write-ahead log, traces, the budget, and a mailbox over
@@ -601,7 +665,7 @@ declare function runToQuiescence<TMachine extends AnyStateMachine>(runtime: Agen
601
665
  * while (r.status === 'idle') {
602
666
  * const event = await promptUser(getAcceptedEvents(r.snapshot));
603
667
  * r = await runToQuiescence(createAgentRuntime(machine, { executors }), {
604
- * snapshot: r.snapshot,
668
+ * snapshot: r.persist(),
605
669
  * event,
606
670
  * });
607
671
  * }
@@ -647,7 +711,8 @@ declare function inspectTransitions(handler: (snapshot: AnyMachineSnapshot, acto
647
711
  * ```
648
712
  *
649
713
  * Only ROOT-actor transitions are traced (matching the runtime's
650
- * `machine.transition`); child-actor transitions are ignored. Attribute the
714
+ * `machine.transition`); child-actor transitions are ignored. Each event's
715
+ * `from.value` is the root's previous state value, tracked per root actor. Attribute the
651
716
  * event via its envelope `runId`.
652
717
  *
653
718
  * By design this path has NO `run.start`/`run.end` events: `createActor` has no
@@ -669,4 +734,4 @@ declare function traceTransitions<TMachine extends AnyStateMachine = AnyStateMac
669
734
  */
670
735
  declare function isAgentIdle(snapshot: AnyMachineSnapshot): boolean;
671
736
  //#endregion
672
- export { serializeTraceEvent as C, AGENT_USAGE_EVENT_TYPE as D, AgentStepRequest as E, AgentUsageEvent as O, runToQuiescence as S, AgentRequest as T, InspectedActorRef as _, AgentRunErrorCause as a, inspectTransitions as b, AgentRunResult as c, AgentRuntimeOptions as d, AgentSnapshotDivergedError as f, AgentTransitionHandler as g, AgentTraceEvent as h, AgentMaxModelCallsExceededError as i, AgentRunStart as l, AgentTimerScheduler as m, AgentEffects as n, AgentRunInit as o, AgentTimerEvent as p, AgentInputFrom as r, AgentRunMeta as s, AGENT_TRACE_SCHEMA_VERSION as t, AgentRuntime as u, JsonSerializableTraceEvent as v, traceTransitions as w, isAgentIdle as x, createAgentRuntime as y };
737
+ export { runAgent as C, AgentRequest as D, traceTransitions as E, AgentStepRequest as O, isAgentIdle as S, serializeTraceEvent as T, AgentTransitionHandler as _, AgentRunErrorCause as a, createAgentRuntime as b, AgentRunOptions as c, AgentRuntime as d, AgentRuntimeOptions as f, AgentTraceEvent as g, AgentTimerScheduler as h, AgentMaxModelCallsExceededError as i, AgentRunResult as l, AgentTimerEvent as m, AgentEffects as n, AgentRunInit as o, AgentSnapshotDivergedError as p, AgentInputFrom as r, AgentRunMeta as s, AGENT_TRACE_SCHEMA_VERSION as t, AgentRunStart as u, InspectedActorRef as v, runToQuiescence as w, inspectTransitions as x, JsonSerializableTraceEvent as y };