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

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 (75) hide show
  1. package/dist/ai-sdk.cjs +149 -26
  2. package/dist/ai-sdk.d.cts +57 -34
  3. package/dist/ai-sdk.d.mts +57 -34
  4. package/dist/ai-sdk.mjs +147 -23
  5. package/dist/{decision-RQ0iv0oZ.mjs → decision-Bl_rKQnU.mjs} +73 -7
  6. package/dist/{decision-CmM8-HHz.cjs → decision-oevZnkBe.cjs} +112 -29
  7. package/dist/{text-logic-f5F5sCdc.cjs → event-log-Dtb16p1p.cjs} +813 -6
  8. package/dist/{text-logic-CIqHAmBi.mjs → event-log-GPZO9p7_.mjs} +679 -3
  9. package/dist/{event-log-store-BIPOz-di.d.mts → event-log-store-CN9ygggF.d.cts} +90 -5
  10. package/dist/{event-log-store-2xzFmgr2.d.cts → event-log-store-DS836wch.d.mts} +90 -5
  11. package/dist/event-log-store-bCdskPNS.cjs +95 -0
  12. package/dist/event-log-store-yalq8C77.mjs +84 -0
  13. package/dist/index.cjs +571 -58
  14. package/dist/index.d.cts +104 -510
  15. package/dist/index.d.mts +104 -510
  16. package/dist/index.mjs +542 -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-C7FFP2Sj.mjs +39 -0
  26. package/dist/model-source-DjJPjGSP.cjs +44 -0
  27. package/dist/openai.cjs +10 -10
  28. package/dist/openai.d.cts +27 -25
  29. package/dist/openai.d.mts +27 -25
  30. package/dist/openai.mjs +2 -2
  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-COXXc0RW.d.mts → run-agent-C9ubX8Tx.d.cts} +87 -30
  36. package/dist/{run-agent-xNTxpt82.mjs → run-agent-CuQT1xSS.mjs} +170 -42
  37. package/dist/{run-agent-ByO-Bxyd.d.cts → run-agent-CxSyZmIE.d.mts} +87 -30
  38. package/dist/{run-agent-Dmf8GV2d.cjs → run-agent-cToZ3K42.cjs} +219 -86
  39. package/dist/{setup-agent-DV4cTou-.cjs → setup-agent-DTF3NQg3.cjs} +31 -20
  40. package/dist/setup-agent-DlbgwsIJ.d.cts +577 -0
  41. package/dist/{setup-agent-BuanDXeh.mjs → setup-agent-DniLvDu7.mjs} +15 -9
  42. package/dist/setup-agent-tsc3vm5I.d.mts +577 -0
  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 +98 -985
  48. package/dist/testing.d.cts +51 -347
  49. package/dist/testing.d.mts +51 -347
  50. package/dist/testing.mjs +86 -973
  51. package/dist/{text-logic-IjfYy9DP.d.cts → text-logic-9fv1i9aK.d.cts} +50 -10
  52. package/dist/{text-logic-D-czVPYp.d.mts → text-logic-BDbGjYMh.d.mts} +50 -10
  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-D8UphgEA.d.mts +17 -0
  56. package/dist/usage-R1NTmLxY.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-B5qGC5WN.mjs +1181 -0
  64. package/dist/verify-CBuYIpsa.d.mts +414 -0
  65. package/dist/verify-Cqghu4gz.cjs +1222 -0
  66. package/dist/verify-DYH_2VzU.d.cts +414 -0
  67. package/package.json +44 -31
  68. package/readme.md +38 -26
  69. package/skills/generate-machine/SKILL.md +140 -66
  70. package/skills/improve-machine/SKILL.md +190 -0
  71. package/skills/improve-machine/reference.md +96 -0
  72. package/dist/event-log-store-2pAFiWuc.mjs +0 -576
  73. package/dist/event-log-store-C-COhIXP.cjs +0 -702
  74. package/dist/usage-BdEjFuIi.mjs +0 -5
  75. 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.mjs";
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-BDbGjYMh.mjs";
3
+ import { n as AgentUsageEvent } from "./usage-D8UphgEA.mjs";
4
+ import { c as AgentLogEntry, n as AgentEventLogStore } from "./event-log-store-DS836wch.mjs";
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 —
@@ -292,7 +290,7 @@ interface AgentRuntimeOptions<TMachine extends AnyStateMachine> {
292
290
  */
293
291
  on?: { [TType in EmittedFrom<TMachine>["type"] | "*"]?: (emitted: EmittedFrom<TMachine> & (TType extends "*" ? unknown : {
294
292
  type: TType;
295
- })) => void };
293
+ })) => void; };
296
294
  /**
297
295
  * Raw xstate inspection passthrough: fires for every inspection event in
298
296
  * the whole actor system — root machine, invoked child machines, spawned
@@ -549,8 +547,15 @@ interface AgentRunStart<TMachine extends AnyStateMachine> {
549
547
  interface AgentRuntime<TMachine extends AnyStateMachine> {
550
548
  /** Opens the run: a fresh start from `input`, a resume from `snapshot`, or recovery from a log. */
551
549
  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];
550
+ /**
551
+ * Applies one event: journals it, traces it, and returns the next state plus
552
+ * its effects. `options.attempts` records the failed attempts of a decider
553
+ * the host ran itself (e.g. `decideNext`'s `attempts`) on the event's entry
554
+ * as `entry.attempts`. Replay ignores them; an empty list is not journaled.
555
+ */
556
+ transition(state: SnapshotFrom<TMachine>, event: EventFromLogic<TMachine> | AgentTimerEvent, options?: {
557
+ attempts?: DecisionAttempt[];
558
+ }): [SnapshotFrom<TMachine>, AgentEffects];
554
559
  /** Starts the effects' work and returns once it is accepted, not once it finishes. */
555
560
  execute(effects: AgentEffects): Promise<void>;
556
561
  /** The next event that arrived, or `undefined` once the run is quiescent. */
@@ -570,6 +575,12 @@ interface AgentRunInit<TMachine extends AnyStateMachine> extends AgentRunStart<T
570
575
  * due is delivered the same way, as an {@link AgentTimerEvent}.
571
576
  */
572
577
  event?: EventFromLogic<TMachine> | AgentTimerEvent;
578
+ /**
579
+ * The failed attempts of the decider the host ran to choose `event` (e.g.
580
+ * `decideNext`'s `attempts`), journaled on `event`'s entry as
581
+ * `entry.attempts`. Replay ignores them.
582
+ */
583
+ attempts?: DecisionAttempt[];
573
584
  }
574
585
  /**
575
586
  * The blocking host: opens the run, delivers `event` if given, then applies
@@ -578,6 +589,51 @@ interface AgentRunInit<TMachine extends AnyStateMachine> extends AgentRunStart<T
578
589
  * own stop policy; this one is for scripts and tests.
579
590
  */
580
591
  declare function runToQuiescence<TMachine extends AnyStateMachine>(runtime: AgentRuntime<TMachine>, init?: AgentRunInit<TMachine>): Promise<AgentRunResult<TMachine>>;
592
+ /** Everything {@link runAgent} takes: the runtime's options plus how to open the run. */
593
+ interface AgentRunOptions<TMachine extends AnyStateMachine> extends AgentRuntimeOptions<TMachine>, AgentRunInit<TMachine> {
594
+ /**
595
+ * Answers an idle state for a host that can block for it. When given,
596
+ * {@link runAgent} loops: each time the run settles `idle`, it calls
597
+ * `onIdle` with the idle snapshot, persists the run, and resumes from that
598
+ * persisted snapshot with the returned event — until the run is `done` or
599
+ * `error`. Each pass is a fresh {@link createAgentRuntime} over the same
600
+ * options, so nothing of the earlier pass is held across the wait; the log
601
+ * travels with the result (or the store's thread), so the final `events`
602
+ * replays every pass, and `maxModelCalls` bounds the whole call. An answer
603
+ * the idle state does not handle ends the loop: the result is `idle` with
604
+ * {@link AgentRunResult.ignored} set, not another call to `onIdle`.
605
+ *
606
+ * A host that cannot block for the answer (a request handler, a durable
607
+ * object) leaves this out, stores `result.persist()`, and calls
608
+ * {@link runAgent} again later with `{ snapshot, event }`.
609
+ */
610
+ onIdle?: (snapshot: SnapshotFrom<TMachine>) => Promise<EventFromLogic<TMachine>>;
611
+ }
612
+ /**
613
+ * Runs `machine` to quiescence in one call: {@link createAgentRuntime} over
614
+ * `options`, then {@link runToQuiescence} with the same object as its init.
615
+ * One options object carries both halves (`executors`, `store`, callbacks,
616
+ * and `input` | `snapshot` + `event`), and the result is the same
617
+ * {@link AgentRunResult}. With {@link AgentRunOptions.onIdle}, idle states are
618
+ * answered in place and the call returns only `done` or `error`.
619
+ *
620
+ * Reach for {@link createAgentRuntime} directly when the host needs the loop
621
+ * itself: a non-blocking durable host, events pushed mid-run, or a custom stop
622
+ * or checkpoint policy.
623
+ *
624
+ * @example
625
+ * ```ts
626
+ * const result = await runAgent(machine, { input: { topic }, executors });
627
+ * if (result.status === "done") console.log(result.output);
628
+ * ```
629
+ *
630
+ * @example
631
+ * ```ts
632
+ * // Resume a run a request handler left idle.
633
+ * const result = await runAgent(machine, { executors, snapshot, event });
634
+ * ```
635
+ */
636
+ declare function runAgent<TMachine extends AnyStateMachine>(machine: TMachine, options?: AgentRunOptions<TMachine>): Promise<AgentRunResult<TMachine>>;
581
637
  /**
582
638
  * Creates the helpers for one run of `machine`: executors bound to its
583
639
  * requests, the write-ahead log, traces, the budget, and a mailbox over
@@ -601,7 +657,7 @@ declare function runToQuiescence<TMachine extends AnyStateMachine>(runtime: Agen
601
657
  * while (r.status === 'idle') {
602
658
  * const event = await promptUser(getAcceptedEvents(r.snapshot));
603
659
  * r = await runToQuiescence(createAgentRuntime(machine, { executors }), {
604
- * snapshot: r.snapshot,
660
+ * snapshot: r.persist(),
605
661
  * event,
606
662
  * });
607
663
  * }
@@ -647,7 +703,8 @@ declare function inspectTransitions(handler: (snapshot: AnyMachineSnapshot, acto
647
703
  * ```
648
704
  *
649
705
  * Only ROOT-actor transitions are traced (matching the runtime's
650
- * `machine.transition`); child-actor transitions are ignored. Attribute the
706
+ * `machine.transition`); child-actor transitions are ignored. Each event's
707
+ * `from.value` is the root's previous state value, tracked per root actor. Attribute the
651
708
  * event via its envelope `runId`.
652
709
  *
653
710
  * By design this path has NO `run.start`/`run.end` events: `createActor` has no
@@ -669,4 +726,4 @@ declare function traceTransitions<TMachine extends AnyStateMachine = AnyStateMac
669
726
  */
670
727
  declare function isAgentIdle(snapshot: AnyMachineSnapshot): boolean;
671
728
  //#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 };
729
+ 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 };