@statelyai/agent 2.0.0-alpha.7 → 2.0.0-alpha.9

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.
@@ -1,4 +1,4 @@
1
- import { b as StandardSchemaV1, c as AgentTools, f as ChosenEvent, i as AgentToolChoice, t as AgentMessage, u as AllowedEvents, v as InferOutput } from "./types-Cq1YlAQ6.mjs";
1
+ import { C as StandardSchemaV1, b as InferOutput, f as AllowedEvents, m as ChosenEvent, o as AgentToolChoice, r as AgentMessage, u as AgentTools } from "./types-qm00QF91.mjs";
2
2
  import { AnyMachineSnapshot, AsyncActorLogic, EventObject, LogicActorLogic, MachineSnapshot } from "xstate";
3
3
 
4
4
  //#region src/events.d.ts
@@ -1,4 +1,4 @@
1
- import { b as StandardSchemaV1, c as AgentTools, f as ChosenEvent, i as AgentToolChoice, t as AgentMessage, u as AllowedEvents, v as InferOutput } from "./types-BHjeDdch.cjs";
1
+ import { C as StandardSchemaV1, b as InferOutput, f as AllowedEvents, m as ChosenEvent, o as AgentToolChoice, r as AgentMessage, u as AgentTools } from "./types-C9QiMjre.cjs";
2
2
  import { AnyMachineSnapshot, AsyncActorLogic, EventObject, LogicActorLogic, MachineSnapshot } from "xstate";
3
3
 
4
4
  //#region src/events.d.ts
@@ -38,15 +38,26 @@ interface StandardSchemaV1<Input = unknown, Output = Input> {
38
38
  type InferOutput<T> = T extends StandardSchemaV1<any, infer O> ? O : never;
39
39
  /** An event schema's output, widened to `unknown` when it validates an empty object (no payload fields). */
40
40
  type EventPayload<T> = T extends Record<string, never> ? unknown : T;
41
+ /**
42
+ * One entry in an event schema map: a Standard Schema for the event's
43
+ * payload, or the `{}` shorthand for a payload-less event
44
+ * (`events: { CONFIRM: {} }` ≡ `events: { CONFIRM: z.object({}) }`).
45
+ */
46
+ type AgentEventSchemaInput = StandardSchemaV1 | Record<string, never>;
47
+ /** An event schema map as authored: payload schemas and/or `{}` payload-less shorthands, keyed by event type. */
48
+ type AgentEventSchemaInputMap = Record<string, AgentEventSchemaInput>;
49
+ /** Resolves an authored event schema map's `{}` shorthands to real (empty-payload) schemas — the type-level counterpart of the runtime normalization in `createAgentSchemas`. */
50
+ type NormalizedEventSchemas<T extends AgentEventSchemaInputMap> = { [K in keyof T]: T[K] extends StandardSchemaV1 ? T[K] : StandardSchemaV1<{}> };
41
51
  /**
42
52
  * The discriminated event union derived from a machine's event schema map
43
53
  * (e.g. `{ ASK: z.object({ question: z.string() }) }` → `{ type: 'ASK';
44
- * question: string }`). Used internally by {@link createAgentSchemas} and
45
- * `setupAgent` to type a machine's `TEvent`.
54
+ * question: string }`; a `{}` shorthand entry yields its bare `{ type: K }`).
55
+ * Used internally by {@link createAgentSchemas} and `setupAgent` to type a
56
+ * machine's `TEvent`.
46
57
  */
47
- type EventUnion<T extends Record<string, StandardSchemaV1>> = { [K in keyof T & string]: {
58
+ type EventUnion<T extends AgentEventSchemaInputMap> = { [K in keyof T & string]: {
48
59
  type: K;
49
- } & EventPayload<InferOutput<T[K]>> }[keyof T & string];
60
+ } & (T[K] extends StandardSchemaV1 ? EventPayload<InferOutput<T[K]>> : unknown) }[keyof T & string];
50
61
  /** Raw binary or string content for an {@link ImagePart}/{@link FilePart}. */
51
62
  type DataContent = string | Uint8Array | ArrayBuffer;
52
63
  /** Provider-specific passthrough options, keyed by provider name (e.g. `{ anthropic: { cacheControl: ... } }`). */
@@ -205,4 +216,4 @@ type AllowedEvents<TEvent extends string = string, TInput = unknown> = AllowedEv
205
216
  input: TInput;
206
217
  }) => AllowedEventPattern<TEvent> | readonly AllowedEventPattern<TEvent>[]);
207
218
  //#endregion
208
- export { ToolCallPart as C, UserMessage as D, ToolResultPart as E, TextPart as S, ToolResultOutput as T, ImagePart as _, AgentToolDescriptor as a, StandardSchemaV1 as b, AgentTools as c, AssistantMessage as d, ChosenEvent as f, FilePart as g, EventUnion as h, AgentToolChoice as i, AllowedEventPattern as l, EventPayload as m, AgentSnapshotStore as n, AgentToolExecute as o, DataContent as p, AgentTool as r, AgentToolSchema as s, AgentMessage as t, AllowedEvents as u, InferOutput as v, ToolMessage as w, SystemMessage as x, ProviderOptions as y };
219
+ export { UserMessage as A, StandardSchemaV1 as C, ToolMessage as D, ToolCallPart as E, ToolResultOutput as O, ProviderOptions as S, TextPart as T, EventUnion as _, AgentTool as a, InferOutput as b, AgentToolExecute as c, AllowedEventPattern as d, AllowedEvents as f, EventPayload as g, DataContent as h, AgentSnapshotStore as i, ToolResultPart as k, AgentToolSchema as l, ChosenEvent as m, AgentEventSchemaInputMap as n, AgentToolChoice as o, AssistantMessage as p, AgentMessage as r, AgentToolDescriptor as s, AgentEventSchemaInput as t, AgentTools as u, FilePart as v, SystemMessage as w, NormalizedEventSchemas as x, ImagePart as y };
@@ -38,15 +38,26 @@ interface StandardSchemaV1<Input = unknown, Output = Input> {
38
38
  type InferOutput<T> = T extends StandardSchemaV1<any, infer O> ? O : never;
39
39
  /** An event schema's output, widened to `unknown` when it validates an empty object (no payload fields). */
40
40
  type EventPayload<T> = T extends Record<string, never> ? unknown : T;
41
+ /**
42
+ * One entry in an event schema map: a Standard Schema for the event's
43
+ * payload, or the `{}` shorthand for a payload-less event
44
+ * (`events: { CONFIRM: {} }` ≡ `events: { CONFIRM: z.object({}) }`).
45
+ */
46
+ type AgentEventSchemaInput = StandardSchemaV1 | Record<string, never>;
47
+ /** An event schema map as authored: payload schemas and/or `{}` payload-less shorthands, keyed by event type. */
48
+ type AgentEventSchemaInputMap = Record<string, AgentEventSchemaInput>;
49
+ /** Resolves an authored event schema map's `{}` shorthands to real (empty-payload) schemas — the type-level counterpart of the runtime normalization in `createAgentSchemas`. */
50
+ type NormalizedEventSchemas<T extends AgentEventSchemaInputMap> = { [K in keyof T]: T[K] extends StandardSchemaV1 ? T[K] : StandardSchemaV1<{}> };
41
51
  /**
42
52
  * The discriminated event union derived from a machine's event schema map
43
53
  * (e.g. `{ ASK: z.object({ question: z.string() }) }` → `{ type: 'ASK';
44
- * question: string }`). Used internally by {@link createAgentSchemas} and
45
- * `setupAgent` to type a machine's `TEvent`.
54
+ * question: string }`; a `{}` shorthand entry yields its bare `{ type: K }`).
55
+ * Used internally by {@link createAgentSchemas} and `setupAgent` to type a
56
+ * machine's `TEvent`.
46
57
  */
47
- type EventUnion<T extends Record<string, StandardSchemaV1>> = { [K in keyof T & string]: {
58
+ type EventUnion<T extends AgentEventSchemaInputMap> = { [K in keyof T & string]: {
48
59
  type: K;
49
- } & EventPayload<InferOutput<T[K]>> }[keyof T & string];
60
+ } & (T[K] extends StandardSchemaV1 ? EventPayload<InferOutput<T[K]>> : unknown) }[keyof T & string];
50
61
  /** Raw binary or string content for an {@link ImagePart}/{@link FilePart}. */
51
62
  type DataContent = string | Uint8Array | ArrayBuffer;
52
63
  /** Provider-specific passthrough options, keyed by provider name (e.g. `{ anthropic: { cacheControl: ... } }`). */
@@ -205,4 +216,4 @@ type AllowedEvents<TEvent extends string = string, TInput = unknown> = AllowedEv
205
216
  input: TInput;
206
217
  }) => AllowedEventPattern<TEvent> | readonly AllowedEventPattern<TEvent>[]);
207
218
  //#endregion
208
- export { ToolCallPart as C, UserMessage as D, ToolResultPart as E, TextPart as S, ToolResultOutput as T, ImagePart as _, AgentToolDescriptor as a, StandardSchemaV1 as b, AgentTools as c, AssistantMessage as d, ChosenEvent as f, FilePart as g, EventUnion as h, AgentToolChoice as i, AllowedEventPattern as l, EventPayload as m, AgentSnapshotStore as n, AgentToolExecute as o, DataContent as p, AgentTool as r, AgentToolSchema as s, AgentMessage as t, AllowedEvents as u, InferOutput as v, ToolMessage as w, SystemMessage as x, ProviderOptions as y };
219
+ export { UserMessage as A, StandardSchemaV1 as C, ToolMessage as D, ToolCallPart as E, ToolResultOutput as O, ProviderOptions as S, TextPart as T, EventUnion as _, AgentTool as a, InferOutput as b, AgentToolExecute as c, AllowedEventPattern as d, AllowedEvents as f, EventPayload as g, DataContent as h, AgentSnapshotStore as i, ToolResultPart as k, AgentToolSchema as l, ChosenEvent as m, AgentEventSchemaInputMap as n, AgentToolChoice as o, AssistantMessage as p, AgentMessage as r, AgentToolDescriptor as s, AgentEventSchemaInput as t, AgentTools as u, FilePart as v, SystemMessage as w, NormalizedEventSchemas as x, ImagePart as y };
@@ -1,4 +1,4 @@
1
- import { C as ToolCallPart, D as UserMessage, E as ToolResultPart, S as TextPart, _ as ImagePart, b as StandardSchemaV1, d as AssistantMessage, g as FilePart, t as AgentMessage, w as ToolMessage, x as SystemMessage } from "./types-Cq1YlAQ6.mjs";
1
+ import { A as UserMessage, C as StandardSchemaV1, D as ToolMessage, E as ToolCallPart, T as TextPart, k as ToolResultPart, p as AssistantMessage, r as AgentMessage, v as FilePart, w as SystemMessage, y as ImagePart } from "./types-C9QiMjre.cjs";
2
2
  import { AnyMachineSnapshot, AnyStateMachine } from "xstate";
3
3
 
4
4
  //#region src/utils.d.ts
@@ -1,4 +1,4 @@
1
- import { C as ToolCallPart, D as UserMessage, E as ToolResultPart, S as TextPart, _ as ImagePart, b as StandardSchemaV1, d as AssistantMessage, g as FilePart, t as AgentMessage, w as ToolMessage, x as SystemMessage } from "./types-BHjeDdch.cjs";
1
+ import { A as UserMessage, C as StandardSchemaV1, D as ToolMessage, E as ToolCallPart, T as TextPart, k as ToolResultPart, p as AssistantMessage, r as AgentMessage, v as FilePart, w as SystemMessage, y as ImagePart } from "./types-qm00QF91.mjs";
2
2
  import { AnyMachineSnapshot, AnyStateMachine } from "xstate";
3
3
 
4
4
  //#region src/utils.d.ts
package/dist/zod.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { t as AgentMessage } from "./types-BHjeDdch.cjs";
1
+ import { r as AgentMessage } from "./types-C9QiMjre.cjs";
2
2
  import { z } from "zod";
3
3
 
4
4
  //#region src/zod/index.d.ts
package/dist/zod.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { t as AgentMessage } from "./types-Cq1YlAQ6.mjs";
1
+ import { r as AgentMessage } from "./types-qm00QF91.mjs";
2
2
  import { z } from "zod";
3
3
 
4
4
  //#region src/zod/index.d.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statelyai/agent",
3
- "version": "2.0.0-alpha.7",
3
+ "version": "2.0.0-alpha.9",
4
4
  "description": "State-machine authoring layer for AI agents",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
package/readme.md CHANGED
@@ -24,18 +24,18 @@ Node 22.18 or newer is required.
24
24
 
25
25
  ## Quick start
26
26
 
27
- <!-- refund decision example using setupAgent, agent.decide, a machine guard, and runAgent -->
27
+ <!-- refund decision example using setupAgent, agent.decide, a machine guard, and the AI SDK runAgent host -->
28
28
 
29
29
  This agent reviews refund requests. The model may propose an automatic refund, but the state machine owns the $100 limit.
30
30
 
31
31
  ```ts
32
- import { openai } from '@ai-sdk/openai';
33
- import { createAiSdkExecutors, defineModels } from '@statelyai/agent/ai-sdk';
34
- import { runAgent, setupAgent } from '@statelyai/agent';
35
- import { z } from 'zod';
32
+ import { openai } from "@ai-sdk/openai";
33
+ import { defineModels, runAgent } from "@statelyai/agent/ai-sdk";
34
+ import { setupAgent } from "@statelyai/agent";
35
+ import { z } from "zod";
36
36
 
37
37
  const models = defineModels({
38
- fast: openai('gpt-5.4-mini'),
38
+ fast: openai("gpt-5.4-mini"),
39
39
  });
40
40
 
41
41
  const agent = setupAgent({
@@ -49,54 +49,52 @@ const agent = setupAgent({
49
49
  amount: z.number(),
50
50
  }),
51
51
  output: z.object({
52
- outcome: z.enum(['refunded', 'review']),
52
+ outcome: z.enum(["refunded", "review"]),
53
53
  }),
54
54
  events: {
55
- AUTO_REFUND: z.object({}),
55
+ AUTO_REFUND: {},
56
56
  REVIEW: z.object({ reason: z.string() }),
57
57
  },
58
58
  });
59
59
 
60
60
  const refundMachine = agent.createMachine({
61
61
  context: ({ input }) => input,
62
- initial: 'deciding',
62
+ initial: "deciding",
63
63
  states: {
64
64
  deciding: {
65
65
  invoke: {
66
- src: 'agent.decide',
66
+ src: "agent.decide",
67
67
  input: ({ context }) => ({
68
- model: 'fast',
69
- system: 'Choose AUTO_REFUND for eligible requests. Otherwise choose REVIEW.',
68
+ model: "fast",
69
+ system: "Choose AUTO_REFUND for eligible requests. Otherwise choose REVIEW.",
70
70
  prompt: `${context.request}\nAmount: $${context.amount}`,
71
- allowedEvents: ['AUTO_REFUND', 'REVIEW'],
71
+ allowedEvents: ["AUTO_REFUND", "REVIEW"],
72
72
  }),
73
73
  },
74
74
  on: {
75
- AUTO_REFUND: ({ context }) =>
76
- context.amount <= 100 ? { target: 'refunded' } : undefined,
77
- REVIEW: { target: 'review' },
75
+ AUTO_REFUND: ({ context }) => (context.amount <= 100 ? { target: "refunded" } : undefined),
76
+ REVIEW: { target: "review" },
78
77
  },
79
78
  },
80
79
  refunded: {
81
- type: 'final',
82
- output: () => ({ outcome: 'refunded' }),
80
+ type: "final",
81
+ output: () => ({ outcome: "refunded" }),
83
82
  },
84
83
  review: {
85
- type: 'final',
86
- output: () => ({ outcome: 'review' }),
84
+ type: "final",
85
+ output: () => ({ outcome: "review" }),
87
86
  },
88
87
  },
89
88
  });
90
89
 
91
90
  const result = await runAgent(refundMachine, {
92
91
  input: {
93
- request: 'I was charged twice for the same order.',
92
+ request: "I was charged twice for the same order.",
94
93
  amount: 75,
95
94
  },
96
- executors: createAiSdkExecutors({ models }),
97
95
  });
98
96
 
99
- if (result.status === 'done') {
97
+ if (result.status === "done") {
100
98
  console.log(result.output);
101
99
  }
102
100
  ```