@statelyai/agent 2.0.0-alpha.22 → 2.0.0-alpha.23
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 +53 -25
- package/dist/ai-sdk.d.cts +20 -12
- package/dist/ai-sdk.d.mts +20 -12
- package/dist/ai-sdk.mjs +53 -25
- package/dist/{decision-DxSTqzgZ.mjs → decision-BBZ2jMO1.mjs} +444 -79
- package/dist/{decision-1o45_ZrS.cjs → decision-Cv6SfNWl.cjs} +525 -94
- package/dist/index.cjs +2224 -1032
- package/dist/index.d.cts +413 -275
- package/dist/index.d.mts +413 -275
- package/dist/index.mjs +2163 -983
- package/dist/machines.cjs +30 -2
- package/dist/machines.d.cts +1 -1
- package/dist/machines.d.mts +1 -1
- package/dist/machines.mjs +30 -2
- package/dist/openai.cjs +447 -0
- package/dist/openai.d.cts +177 -0
- package/dist/openai.d.mts +177 -0
- package/dist/openai.mjs +437 -0
- package/dist/otel.cjs +3 -6
- package/dist/otel.d.cts +1 -1
- package/dist/otel.d.mts +1 -1
- package/dist/otel.mjs +3 -6
- package/dist/run-agent-Ck7Sa3Tb.d.mts +968 -0
- package/dist/run-agent-DWqA8KiI.d.cts +968 -0
- package/dist/setup-agent-CeO2fARv.mjs +639 -0
- package/dist/setup-agent-Dl2VELLi.cjs +663 -0
- package/dist/{text-logic-BaxPrcLk.d.cts → text-logic-C7bSnH_B.d.cts} +195 -44
- package/dist/{text-logic-DhfFWzu9.d.mts → text-logic-DJBK3LbG.d.mts} +195 -44
- package/dist/{types-DSdj2tGs.d.cts → types-CjHk5OdH.d.cts} +4 -19
- package/dist/{types-CvWRGFxP.d.mts → types-DWA6X9R-.d.mts} +4 -19
- package/package.json +17 -21
- package/readme.md +32 -17
- package/dist/errors-BQRk9eiZ.d.cts +0 -19
- package/dist/errors-C9rxnWbX.d.mts +0 -19
- package/dist/errors-CeSXQx0v.mjs +0 -23
- package/dist/errors-DUBBzRLP.cjs +0 -28
- package/dist/event-log-store-Bz7HDBkE.d.cts +0 -145
- package/dist/event-log-store-DmIDosD6.mjs +0 -427
- package/dist/event-log-store-a_TKy1gk.cjs +0 -468
- package/dist/event-log-store-hrA1vqtN.d.mts +0 -145
- package/dist/run-agent-0fpEZ1wJ.d.mts +0 -1098
- package/dist/run-agent-Zbkx7XP8.d.cts +0 -1098
- package/dist/setup-agent-DkCTy5Eu.cjs +0 -1707
- package/dist/setup-agent-nSFYrNTP.mjs +0 -1587
- package/dist/sqlite.cjs +0 -129
- package/dist/sqlite.d.cts +0 -57
- package/dist/sqlite.d.mts +0 -57
- package/dist/sqlite.mjs +0 -127
|
@@ -1,8 +1,71 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { AnyMachineSnapshot, AsyncActorLogic, EventObject, MachineSnapshot } from "xstate";
|
|
1
|
+
import { _ as StandardSchemaV1, c as AllowedEvents, h as InferOutput, i as AgentToolChoice, n as AgentMessage, s as AgentTools, u as ChosenEvent } from "./types-DWA6X9R-.mjs";
|
|
2
|
+
import { AnyMachineSnapshot, AnyStateMachine, AsyncActorLogic, EventFromLogic, EventObject, MachineSnapshot } from "xstate";
|
|
4
3
|
|
|
4
|
+
//#region src/errors.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Base class for every error this package throws. Carries a stable, kebab-case
|
|
7
|
+
* `code` so hosts can branch on the failure without `instanceof` (useful across
|
|
8
|
+
* bundle/realm boundaries and after serialization).
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* catch (error) {
|
|
13
|
+
* if (error instanceof AgentError && error.code === 'agent-idle') { ... }
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
declare class AgentError extends Error {
|
|
18
|
+
readonly code: string;
|
|
19
|
+
constructor(code: string, message: string, options?: ErrorOptions);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A model call ran out of output tokens before it produced anything usable.
|
|
23
|
+
* Core never throws this: it is the adapter's signal that a `'length'` finish
|
|
24
|
+
* reason left nothing to return — a structured request whose envelope never
|
|
25
|
+
* closed, say. A text request that WAS cut off still returns its text, with
|
|
26
|
+
* `finishReason: 'length'`.
|
|
27
|
+
*
|
|
28
|
+
* The code is `'truncated'`, so an invoke's `onError` can branch on it without
|
|
29
|
+
* `instanceof`:
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* onError: [
|
|
34
|
+
* { guard: ({ event }) => event.error.code === 'truncated', target: 'retryShorter' },
|
|
35
|
+
* { target: 'failed' },
|
|
36
|
+
* ]
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare class AgentTruncatedError extends AgentError {
|
|
40
|
+
/** The request that was cut off — `AgentTextRequest.name`. */
|
|
41
|
+
readonly requestName: string;
|
|
42
|
+
/** The durable invoke id of the request, when the host knows it. */
|
|
43
|
+
readonly requestId?: string;
|
|
44
|
+
/** Whatever the model did produce before it hit the limit, if anything. */
|
|
45
|
+
readonly partialOutput?: unknown;
|
|
46
|
+
constructor(message: string, options: {
|
|
47
|
+
requestName: string;
|
|
48
|
+
requestId?: string;
|
|
49
|
+
partialOutput?: unknown;
|
|
50
|
+
cause?: unknown;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
5
54
|
//#region src/events.d.ts
|
|
55
|
+
/**
|
|
56
|
+
* Thrown by {@link parseAgentEvent} (and {@link eventFromInteraction}) when a
|
|
57
|
+
* wire payload is not a usable event: it is not an object with a string
|
|
58
|
+
* `type`, it names a reserved `@agent.*` type, or its fields fail the schema
|
|
59
|
+
* the machine registered for that type. A host turns it into a 400.
|
|
60
|
+
*
|
|
61
|
+
* Not handling an event is NOT this error — a state machine ignores events it
|
|
62
|
+
* has no transition for, so an event the current state does not handle parses
|
|
63
|
+
* fine and settles the run with `result.ignored` set.
|
|
64
|
+
*/
|
|
65
|
+
declare class AgentInvalidEventPayloadError extends AgentError {
|
|
66
|
+
readonly eventType: string;
|
|
67
|
+
constructor(eventType: string, detail: string);
|
|
68
|
+
}
|
|
6
69
|
/** The invoke `src` of an {@link AgentRequest}/{@link AgentDecisionRequest} — a plain string, widened so literal `src` values still narrow in editor hints. */
|
|
7
70
|
type AgentRequestSource = string & {};
|
|
8
71
|
/** Customizes the tool name generated for a candidate event; see {@link AgentRequestOptions.eventToolName}. */
|
|
@@ -33,30 +96,32 @@ interface AgentRequestOptions {
|
|
|
33
96
|
}
|
|
34
97
|
/** Recovers a machine's event union from its snapshot type, so {@link parseAgentEvent} returns the machine-typed event without a downstream cast. @internal */
|
|
35
98
|
type EventFromSnapshot<TSnapshot> = TSnapshot extends MachineSnapshot<any, infer TEvent, any, any, any, any, any, any> ? TEvent : EventObject;
|
|
99
|
+
/** The machine event union {@link parseAgentEvent} returns, from a machine or from a snapshot of one. @internal */
|
|
100
|
+
type ParsedAgentEvent<TSource> = TSource extends AnyStateMachine ? EventFromLogic<TSource> : EventFromSnapshot<TSource>;
|
|
36
101
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
102
|
+
* Parses a wire payload (a request body, a socket frame) into an
|
|
103
|
+
* event typed as the machine's event union, so it can go straight to
|
|
104
|
+
* `runAgent({ event })` / `actor.send(...)` without a cast. Pass the machine
|
|
105
|
+
* itself, or any snapshot of it.
|
|
106
|
+
*
|
|
107
|
+
* It checks exactly what a schema can check: the payload is an object with a
|
|
108
|
+
* string `type`, that type is not one of the library-reserved `@agent.*` types,
|
|
109
|
+
* and — when the machine registered a schema for it — the fields satisfy that
|
|
110
|
+
* schema (defaults filled, transforms applied). Anything else throws
|
|
111
|
+
* {@link AgentInvalidEventPayloadError}, which a host answers with a 400.
|
|
44
112
|
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* the live snapshot; the schemas only add payload validation.
|
|
113
|
+
* It deliberately does NOT check whether the current state handles the event.
|
|
114
|
+
* State machines ignore events they have no transition for; that is normal
|
|
115
|
+
* behavior, not a validation failure. Send the parsed event and read
|
|
116
|
+
* `result.ignored` if the host wants to tell the client nothing happened.
|
|
50
117
|
*
|
|
51
118
|
* @example
|
|
52
119
|
* ```ts
|
|
53
|
-
* const event = parseAgentEvent(
|
|
54
|
-
* result = await runAgent(machine, {
|
|
120
|
+
* const event = parseAgentEvent(machine, await request.json());
|
|
121
|
+
* const result = await runAgent(machine, { store, threadId, event, executors });
|
|
55
122
|
* ```
|
|
56
123
|
*/
|
|
57
|
-
declare function parseAgentEvent<
|
|
58
|
-
type: string;
|
|
59
|
-
} & Record<string, unknown>, options?: Pick<AgentRequestOptions, "events" | "schemas" | "eventToolName">): EventFromSnapshot<TSnapshot>;
|
|
124
|
+
declare function parseAgentEvent<TSource extends AnyStateMachine | AnyMachineSnapshot>(source: TSource, payload: unknown, options?: Pick<AgentRequestOptions, "events" | "schemas">): ParsedAgentEvent<TSource>;
|
|
60
125
|
/**
|
|
61
126
|
* Lists the events a snapshot can currently accept, as {@link AgentEventDescriptor}s
|
|
62
127
|
* a model can be offered (via `resolveDecision`/an adapter's tool-per-event
|
|
@@ -85,10 +150,13 @@ declare function getAcceptedEvents(snapshot: AnyMachineSnapshot, options?: Pick<
|
|
|
85
150
|
* See {@link AgentDecisionRequest} for the request shape this lowers to.
|
|
86
151
|
*/
|
|
87
152
|
interface AgentDecisionInput<TEvent extends string = string, TMetadata = Record<string, unknown>, TModel extends string = string> {
|
|
153
|
+
/** Semantic request identity. Defaults to the invoke occurrence id. */
|
|
154
|
+
name?: string;
|
|
88
155
|
model: TModel;
|
|
89
156
|
system?: string;
|
|
90
157
|
prompt?: string;
|
|
91
|
-
messages
|
|
158
|
+
/** Framework-native messages, passed through without conversion. */
|
|
159
|
+
messages?: any[];
|
|
92
160
|
allowedEvents?: AllowedEvents<TEvent>;
|
|
93
161
|
maxRetries?: number;
|
|
94
162
|
temperature?: number;
|
|
@@ -107,13 +175,16 @@ interface AgentDecisionInput<TEvent extends string = string, TMetadata = Record<
|
|
|
107
175
|
* {@link AllowedEvents}) and `maxRetries` for {@link resolveDecision}.
|
|
108
176
|
*/
|
|
109
177
|
interface DecisionLogicConfig<TInputSchema extends StandardSchemaV1 = StandardSchemaV1, TEvent extends string = string, TMetadata extends Record<string, unknown> = Record<string, unknown>, TModel extends string = string> {
|
|
178
|
+
/** Semantic request identity. Defaults to the invoke occurrence id. */
|
|
179
|
+
name?: ResolveTextLogicValue<string | undefined, InferOutput<TInputSchema>>;
|
|
110
180
|
schemas?: {
|
|
111
181
|
input: TInputSchema;
|
|
112
182
|
};
|
|
113
183
|
model: ResolveTextLogicValue<TModel, InferOutput<TInputSchema>>;
|
|
114
184
|
system?: ResolveTextLogicValue<string | undefined, InferOutput<TInputSchema>>;
|
|
115
185
|
prompt?: ResolveTextLogicValue<string | undefined, InferOutput<TInputSchema>>;
|
|
116
|
-
messages
|
|
186
|
+
/** Framework-native messages, passed through without conversion. */
|
|
187
|
+
messages?: ResolveTextLogicValue<any[] | undefined, InferOutput<TInputSchema>>;
|
|
117
188
|
allowedEvents?: AllowedEvents<TEvent, InferOutput<TInputSchema>>;
|
|
118
189
|
maxRetries?: number;
|
|
119
190
|
temperature?: ResolveTextLogicValue<number | undefined, InferOutput<TInputSchema>>;
|
|
@@ -175,10 +246,15 @@ interface AgentDecisionRequest {
|
|
|
175
246
|
kind: "decision";
|
|
176
247
|
/** Durable invoke id. */
|
|
177
248
|
id: string;
|
|
249
|
+
/** Semantic request identity; defaults to {@link id}. */
|
|
250
|
+
name?: string;
|
|
251
|
+
/** The schema-validated invoke input that produced this request. */
|
|
252
|
+
input?: unknown;
|
|
178
253
|
model: string;
|
|
179
254
|
system?: string;
|
|
180
255
|
prompt?: string;
|
|
181
|
-
messages
|
|
256
|
+
/** Framework-native messages, passed through without conversion. */
|
|
257
|
+
messages?: any[];
|
|
182
258
|
/** Candidate events: declared `allowedEvents` ∩ snapshot-legal events. */
|
|
183
259
|
events: AgentEventDescriptor[];
|
|
184
260
|
/**
|
|
@@ -194,13 +270,6 @@ interface AgentDecisionRequest {
|
|
|
194
270
|
seed?: number;
|
|
195
271
|
stopSequences?: string[];
|
|
196
272
|
metadata?: Record<string, unknown>;
|
|
197
|
-
/**
|
|
198
|
-
* Abort signal for the underlying model call, threaded through by
|
|
199
|
-
* {@link resolveDecision} from its `options.signal` so a `decide` executor
|
|
200
|
-
* can cancel the in-flight request (symmetric with the text executors'
|
|
201
|
-
* `info.signal`). Runtime-only — never serialized into a provider request.
|
|
202
|
-
*/
|
|
203
|
-
signal?: AbortSignal;
|
|
204
273
|
/**
|
|
205
274
|
* The `runAgent` run this decision belongs to (`run_<n>`, matching trace
|
|
206
275
|
* events), injected by runAgent like `signal` (symmetric with the text
|
|
@@ -211,6 +280,42 @@ interface AgentDecisionRequest {
|
|
|
211
280
|
*/
|
|
212
281
|
runId?: string;
|
|
213
282
|
}
|
|
283
|
+
/**
|
|
284
|
+
* Options for {@link createDecisionRequest}. Everything except `model` and
|
|
285
|
+
* `events` is optional; `events` also accepts bare event types.
|
|
286
|
+
*/
|
|
287
|
+
interface CreateDecisionRequestOptions extends Omit<AgentDecisionRequest, "kind" | "id" | "events" | "attempts"> {
|
|
288
|
+
/** Durable invoke id. Defaults to `name`, then `'decision'`. */
|
|
289
|
+
id?: string;
|
|
290
|
+
/** Candidate events: descriptors (from `getAcceptedEvents`) or bare types. */
|
|
291
|
+
events: readonly (string | AgentEventDescriptor)[];
|
|
292
|
+
/** Prior failed attempts. Defaults to `[]` (a first attempt). */
|
|
293
|
+
attempts?: DecisionAttempt[];
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Builds an {@link AgentDecisionRequest} for {@link resolveDecision}, filling
|
|
297
|
+
* in the fields a hand-written literal has to restate every time: `kind`, an
|
|
298
|
+
* `id`, an empty `attempts` list, and each candidate event's `toolName`.
|
|
299
|
+
*
|
|
300
|
+
* @example
|
|
301
|
+
* ```ts
|
|
302
|
+
* const chosen = await resolveDecision(
|
|
303
|
+
* createDecisionRequest({
|
|
304
|
+
* name: 'judge',
|
|
305
|
+
* model: 'reviewer',
|
|
306
|
+
* prompt: `Judge this draft.\n\n${snapshot.context.draft}`,
|
|
307
|
+
* events: getAcceptedEvents(snapshot),
|
|
308
|
+
* }),
|
|
309
|
+
* { decide },
|
|
310
|
+
* { canTake: (event) => snapshot.can(event) },
|
|
311
|
+
* );
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
314
|
+
declare function createDecisionRequest(options: CreateDecisionRequestOptions): AgentDecisionRequest;
|
|
315
|
+
/** Decision request after core has resolved its semantic identity. */
|
|
316
|
+
type AgentExecutorDecisionRequest = AgentDecisionRequest & {
|
|
317
|
+
name: string;
|
|
318
|
+
};
|
|
214
319
|
/**
|
|
215
320
|
* A single failed decision attempt, recorded by {@link resolveDecision} and
|
|
216
321
|
* fed back to the executor on the next attempt via
|
|
@@ -267,7 +372,7 @@ declare function renderDecisionAttempts(request: Pick<AgentDecisionRequest, "eve
|
|
|
267
372
|
* report this attempt's tokens there and `runAgent` folds them into the run's
|
|
268
373
|
* aggregated {@link AgentUsage}.
|
|
269
374
|
*/
|
|
270
|
-
type AgentDecisionExecutor = (request:
|
|
375
|
+
type AgentDecisionExecutor = (request: AgentExecutorDecisionRequest, info?: AgentRequestExecutorInfo) => PromiseLike<{
|
|
271
376
|
event: ChosenEvent;
|
|
272
377
|
reason?: string; /** This attempt's token usage, aggregated into the run result's {@link AgentUsage}. */
|
|
273
378
|
usage?: AgentCallUsage;
|
|
@@ -337,7 +442,7 @@ type AgentModelMap = Record<string, unknown>;
|
|
|
337
442
|
* autocomplete. Refs are opaque routing keys — the host/executor (or the AI SDK
|
|
338
443
|
* adapter's models map / `resolveModel`) resolves them to a real model.
|
|
339
444
|
*/
|
|
340
|
-
type AgentModelRef<TModels extends AgentModelMap = {}> = [keyof TModels] extends [never] ? string :
|
|
445
|
+
type AgentModelRef<TModels extends AgentModelMap = {}> = [keyof TModels] extends [never] ? string : keyof TModels & string;
|
|
341
446
|
/**
|
|
342
447
|
* Splits a portable `"provider/model-id"` model ref (the convention JSON
|
|
343
448
|
* workflows and registry-less hosts use, e.g. `"openai/gpt-5.4-mini"`) into
|
|
@@ -361,7 +466,7 @@ declare function parseModelRef(modelRef: string): {
|
|
|
361
466
|
* `createAiSdkExecutors`) map this shape onto their provider's call
|
|
362
467
|
* settings.
|
|
363
468
|
*/
|
|
364
|
-
interface AgentTextRequest<TMetadata = Record<string, unknown
|
|
469
|
+
interface AgentTextRequest<TMetadata = Record<string, unknown>, TMessage = any> {
|
|
365
470
|
/**
|
|
366
471
|
* The registered name of the request that produced this call — the
|
|
367
472
|
* `setupAgent({ requests })` key (also set by `setupAgent.fromConfig`), or
|
|
@@ -371,10 +476,12 @@ interface AgentTextRequest<TMetadata = Record<string, unknown>> {
|
|
|
371
476
|
* the caller sets it on the inline input.
|
|
372
477
|
*/
|
|
373
478
|
name?: string;
|
|
479
|
+
/** The schema-validated invoke input that produced this request. */
|
|
480
|
+
input?: unknown;
|
|
374
481
|
model: string;
|
|
375
482
|
system?: string;
|
|
376
483
|
prompt?: string;
|
|
377
|
-
messages?:
|
|
484
|
+
messages?: TMessage[];
|
|
378
485
|
/** Host/model tools that are always available to this text call. */
|
|
379
486
|
tools?: AgentTools;
|
|
380
487
|
toolChoice?: AgentToolChoice;
|
|
@@ -424,7 +531,7 @@ interface AgentTextRequest<TMetadata = Record<string, unknown>> {
|
|
|
424
531
|
/**
|
|
425
532
|
* Aggregated model-call usage for ONE `runAgent` call — the run-level total
|
|
426
533
|
* attached to every settled {@link RunAgentResult} (and therefore to
|
|
427
|
-
* `
|
|
534
|
+
* `runAgent`'s `{ output, snapshot, persist, usage }`).
|
|
428
535
|
*
|
|
429
536
|
* - `modelCalls` counts every model/decision call this run made (each decision
|
|
430
537
|
* retry counts separately) — the same seam `maxModelCalls` budgets. Always a
|
|
@@ -449,6 +556,29 @@ interface AgentUsage {
|
|
|
449
556
|
}
|
|
450
557
|
/** One model call's reported usage — {@link AgentUsage} without the run-level `modelCalls` count. What an executor puts on its result's `usage` field. */
|
|
451
558
|
type AgentCallUsage = Omit<AgentUsage, "modelCalls">;
|
|
559
|
+
/**
|
|
560
|
+
* Why a model call stopped, normalized across providers:
|
|
561
|
+
*
|
|
562
|
+
* - `'stop'` — the model finished on its own.
|
|
563
|
+
* - `'length'` — the output token limit cut it off.
|
|
564
|
+
* - `'tool-calls'` — it stopped to call tools.
|
|
565
|
+
* - `'content-filter'` — a safety filter stopped it.
|
|
566
|
+
* - `'other'` — anything else, including a provider error the adapter mapped.
|
|
567
|
+
*
|
|
568
|
+
* Adapters set it on their executor result; `runAgent` copies it onto the
|
|
569
|
+
* `request.end` trace event. A provider's own string stays on the result's
|
|
570
|
+
* `raw`.
|
|
571
|
+
*/
|
|
572
|
+
type AgentFinishReason = "stop" | "length" | "tool-calls" | "content-filter" | "other";
|
|
573
|
+
/**
|
|
574
|
+
* Reads a settled call's {@link AgentFinishReason} off a RAW executor result's
|
|
575
|
+
* `finishReason` field — the finish-reason sibling of {@link getCallUsage},
|
|
576
|
+
* applied by `runAgent` before it puts the reason on the `request.end` trace.
|
|
577
|
+
* A provider's `'error'` normalizes to `'other'`; anything else the union does
|
|
578
|
+
* not name (an un-normalized provider string, a missing field) returns
|
|
579
|
+
* `undefined`.
|
|
580
|
+
*/
|
|
581
|
+
declare function getCallFinishReason(raw: unknown): AgentFinishReason | undefined;
|
|
452
582
|
/**
|
|
453
583
|
* Reads a settled call's per-call {@link AgentCallUsage} off a RAW executor
|
|
454
584
|
* result's `usage` field, keeping only finite numbers — the same normalization
|
|
@@ -522,7 +652,8 @@ interface TextLogicConfig<TInputSchema extends StandardSchemaV1 = StandardSchema
|
|
|
522
652
|
model: ResolveTextLogicValue<TModel, InferOutput<TInputSchema>>;
|
|
523
653
|
system?: ResolveTextLogicValue<string | undefined, InferOutput<TInputSchema>>;
|
|
524
654
|
prompt?: ResolveTextLogicValue<string | undefined, InferOutput<TInputSchema>>;
|
|
525
|
-
messages
|
|
655
|
+
/** Framework-native messages, passed through without conversion. */
|
|
656
|
+
messages?: ResolveTextLogicValue<any[] | undefined, InferOutput<TInputSchema>>;
|
|
526
657
|
tools?: ResolveTextLogicValue<AgentTools | undefined, InferOutput<TInputSchema>>;
|
|
527
658
|
toolChoice?: ResolveTextLogicValue<AgentToolChoice | undefined, InferOutput<TInputSchema>>;
|
|
528
659
|
/** Opt into the structured-output envelope's `reasoning` field (see {@link AgentTextRequest.includeReasoning}). */
|
|
@@ -618,9 +749,11 @@ declare function bindRequestExecutor<TInputSchema extends StandardSchemaV1, TOut
|
|
|
618
749
|
* {@link AgentUsage}. Optional — an executor that reports nothing still counts
|
|
619
750
|
* toward `modelCalls`.
|
|
620
751
|
*/
|
|
621
|
-
type AgentRequestExecutorResult<TOutput = unknown> = {
|
|
622
|
-
output: TOutput; /**
|
|
623
|
-
|
|
752
|
+
type AgentRequestExecutorResult<TOutput = unknown, TMessage = unknown> = {
|
|
753
|
+
output: TOutput; /** Framework-native response messages, preserved without normalization. */
|
|
754
|
+
messages?: TMessage[]; /** This call's token usage, aggregated into the run result's {@link AgentUsage}. */
|
|
755
|
+
usage?: AgentCallUsage; /** Why this call stopped, normalized (see {@link AgentFinishReason}). Surfaced on the `request.end` trace event. */
|
|
756
|
+
finishReason?: AgentFinishReason;
|
|
624
757
|
[key: string]: unknown;
|
|
625
758
|
};
|
|
626
759
|
/**
|
|
@@ -645,6 +778,24 @@ interface AgentRequestExecutorInfo {
|
|
|
645
778
|
* call has no invoking actor.
|
|
646
779
|
*/
|
|
647
780
|
requestId?: string;
|
|
781
|
+
/**
|
|
782
|
+
* A per-call idempotency key, `${executionId}:${requestId}#${n}` — the event
|
|
783
|
+
* log's lineage id plus the occurrence of this call at this invoke site.
|
|
784
|
+
* IDENTICAL across a crash re-execution: a run resumed from a log whose last
|
|
785
|
+
* call was still in flight re-issues that call under the same key, so an
|
|
786
|
+
* executor-level cache (or a provider's own idempotency header) can return
|
|
787
|
+
* the first attempt's result instead of paying for it twice. A decision
|
|
788
|
+
* retry appends its attempt ordinal (`${executionId}:${requestId}#${n}.${a}`,
|
|
789
|
+
* `a` being the number of prior failed attempts), so a retry never collides
|
|
790
|
+
* with the rejected attempt it is replacing.
|
|
791
|
+
*
|
|
792
|
+
* It identifies the CALL SITE, not the request: a fork inherits its parent's
|
|
793
|
+
* lineage id, so cache on `callKey` together with a fingerprint of the
|
|
794
|
+
* request and reuse a cached result only when the request also matches.
|
|
795
|
+
*
|
|
796
|
+
* Undefined off the `runAgent` path, and for a log with no `executionId`.
|
|
797
|
+
*/
|
|
798
|
+
callKey?: string;
|
|
648
799
|
}
|
|
649
800
|
/**
|
|
650
801
|
* A raw Vercel AI SDK `generateText` result shape: resolves `{ text }` (a
|
|
@@ -694,15 +845,15 @@ type AiSdkShapedStreamResult = {
|
|
|
694
845
|
* - `prompt`/`messages` are mutually exclusive, matching `ai`'s `Prompt`
|
|
695
846
|
* union (core always sets exactly one).
|
|
696
847
|
* - `tools`, `toolChoice`, and `messages` are widened to `any` — their
|
|
697
|
-
*
|
|
698
|
-
*
|
|
699
|
-
* and would otherwise fail the contravariant check.
|
|
848
|
+
* framework-native shapes may be branded and would otherwise fail the
|
|
849
|
+
* contravariant check.
|
|
700
850
|
*
|
|
701
851
|
* Hand-written executors that want the precise shapes can annotate their
|
|
702
852
|
* parameter as `AgentTextRequest & { tools: AgentTools }` — that wider
|
|
703
853
|
* parameter type keeps the executor assignable.
|
|
704
854
|
*/
|
|
705
|
-
type AgentExecutorTextRequest<TMetadata = Record<string, unknown>> = Omit<AgentTextRequest<TMetadata>, "prompt" | "messages" | "tools" | "toolChoice"> & {
|
|
855
|
+
type AgentExecutorTextRequest<TMetadata = Record<string, unknown>> = Omit<AgentTextRequest<TMetadata>, "name" | "prompt" | "messages" | "tools" | "toolChoice"> & {
|
|
856
|
+
/** Semantic request identity; always resolved before an executor is called. */name: string;
|
|
706
857
|
tools: any;
|
|
707
858
|
toolChoice?: any;
|
|
708
859
|
} & ({
|
|
@@ -787,4 +938,4 @@ declare function buildEnvelopeSchema(inner: StandardSchemaV1, options?: {
|
|
|
787
938
|
*/
|
|
788
939
|
declare function parseStructuredEnvelope(request: Pick<AgentTextRequest, "outputSchema" | "includeReasoning">, value: unknown): StructuredOutputEnvelope;
|
|
789
940
|
//#endregion
|
|
790
|
-
export {
|
|
941
|
+
export { getAcceptedEvents as $, parseOutput as A, DecisionLogicConfig as B, bindRequestExecutor as C, getCallFinishReason as D, getAgentOutputMode as E, AgentDecisionRequest as F, resolveDecision as G, createDecisionLogic as H, AgentExecutorDecisionRequest as I, AgentInvalidEventPayloadError as J, AgentEventDescriptor as K, CreateDecisionRequestOptions as L, AgentDecisionExecutor as M, AgentDecisionExhaustedError as N, getCallUsage as O, AgentDecisionInput as P, EventFromSnapshot as Q, DecisionAttempt as R, TextLogicExecutor as S, createTextLogic as T, createDecisionRequest as U, ResolveDecisionOptions as V, renderDecisionAttempts as W, AgentRequestSource as X, AgentRequestOptions as Y, AgentSchemas as Z, BuiltinAgentActors as _, AgentModelRef as a, TextLogicConfig as b, AgentRequestExecutorInfo as c, AgentRequestMode as d, parseAgentEvent as et, AgentTextRequest as f, AiSdkShapedTextResult as g, AiSdkShapedStreamResult as h, AgentModelMap as i, parseStructuredEnvelope as j, parseModelRef as k, AgentRequestExecutorResult as l, AgentUserInput as m, AgentExecutorTextRequest as n, AgentTruncatedError as nt, AgentOutputMode as o, AgentUsage as p, AgentEventToolNameResolver as q, AgentFinishReason as r, AgentRequestExecutor as s, AgentCallUsage as t, AgentError as tt, AgentRequestExecutors as u, StructuredOutputEnvelope as v, buildEnvelopeSchema as w, TextLogicExecuteArgs as x, TextLogic as y, DecisionLogic as z };
|
|
@@ -1,17 +1,4 @@
|
|
|
1
|
-
import { Snapshot } from "xstate";
|
|
2
|
-
|
|
3
1
|
//#region src/types.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* A minimal, type-only contract for a snapshot store: persist a `runAgent`
|
|
6
|
-
* idle/settled snapshot under an id and load it back. It exists purely so
|
|
7
|
-
* userland stores (a file, a SQLite table, a KV row, …) share one shape and
|
|
8
|
-
* interoperate — there is **zero runtime** behind it; the library ships no
|
|
9
|
-
* implementation. See `examples/file-snapshot-store` for a `node:fs` store.
|
|
10
|
-
*/
|
|
11
|
-
interface AgentSnapshotStore {
|
|
12
|
-
load(id: string): Promise<Snapshot<unknown> | undefined>;
|
|
13
|
-
save(id: string, snapshot: Snapshot<unknown>): Promise<void>;
|
|
14
|
-
}
|
|
15
2
|
/**
|
|
16
3
|
* The [Standard Schema](https://standardschema.dev) interface. Every schema
|
|
17
4
|
* this library accepts (context, events, input/output, tool schemas, …) is a
|
|
@@ -176,11 +163,9 @@ type ToolMessage = {
|
|
|
176
163
|
providerOptions?: ProviderOptions;
|
|
177
164
|
};
|
|
178
165
|
/**
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
* plain context state — see {@link appendMessages}
|
|
182
|
-
* decision requests via `messages`. Validate a context field with
|
|
183
|
-
* {@link messagesSchema}.
|
|
166
|
+
* Optional framework-neutral message helpers. Requests also accept native
|
|
167
|
+
* framework message values directly; core never converts between formats.
|
|
168
|
+
* Store messages as plain context state — see {@link appendMessages}.
|
|
184
169
|
*/
|
|
185
170
|
type AgentMessage = SystemMessage | UserMessage | AssistantMessage | ToolMessage;
|
|
186
171
|
/**
|
|
@@ -249,4 +234,4 @@ type AllowedEvents<TEvent extends string = string, TInput = unknown> = AllowedEv
|
|
|
249
234
|
input: TInput;
|
|
250
235
|
}) => AllowedEventPattern<TEvent> | readonly AllowedEventPattern<TEvent>[]);
|
|
251
236
|
//#endregion
|
|
252
|
-
export {
|
|
237
|
+
export { UserMessage as C, ToolResultPart as S, StandardSchemaV1 as _, AgentToolDescriptor as a, ToolCallPart as b, AllowedEvents as c, EventUnion as d, FilePart as f, NormalizedEventSchemas as g, InferOutput as h, AgentToolChoice as i, AssistantMessage as l, InferInput as m, AgentMessage as n, AgentToolExecute as o, ImagePart as p, AgentTool as r, AgentTools as s, AgentEventSchemaInputMap as t, ChosenEvent as u, SystemMessage as v, WithAgentInputSchema as w, ToolMessage as x, TextPart as y };
|
|
@@ -1,17 +1,4 @@
|
|
|
1
|
-
import { Snapshot } from "xstate";
|
|
2
|
-
|
|
3
1
|
//#region src/types.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* A minimal, type-only contract for a snapshot store: persist a `runAgent`
|
|
6
|
-
* idle/settled snapshot under an id and load it back. It exists purely so
|
|
7
|
-
* userland stores (a file, a SQLite table, a KV row, …) share one shape and
|
|
8
|
-
* interoperate — there is **zero runtime** behind it; the library ships no
|
|
9
|
-
* implementation. See `examples/file-snapshot-store` for a `node:fs` store.
|
|
10
|
-
*/
|
|
11
|
-
interface AgentSnapshotStore {
|
|
12
|
-
load(id: string): Promise<Snapshot<unknown> | undefined>;
|
|
13
|
-
save(id: string, snapshot: Snapshot<unknown>): Promise<void>;
|
|
14
|
-
}
|
|
15
2
|
/**
|
|
16
3
|
* The [Standard Schema](https://standardschema.dev) interface. Every schema
|
|
17
4
|
* this library accepts (context, events, input/output, tool schemas, …) is a
|
|
@@ -176,11 +163,9 @@ type ToolMessage = {
|
|
|
176
163
|
providerOptions?: ProviderOptions;
|
|
177
164
|
};
|
|
178
165
|
/**
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
* plain context state — see {@link appendMessages}
|
|
182
|
-
* decision requests via `messages`. Validate a context field with
|
|
183
|
-
* {@link messagesSchema}.
|
|
166
|
+
* Optional framework-neutral message helpers. Requests also accept native
|
|
167
|
+
* framework message values directly; core never converts between formats.
|
|
168
|
+
* Store messages as plain context state — see {@link appendMessages}.
|
|
184
169
|
*/
|
|
185
170
|
type AgentMessage = SystemMessage | UserMessage | AssistantMessage | ToolMessage;
|
|
186
171
|
/**
|
|
@@ -249,4 +234,4 @@ type AllowedEvents<TEvent extends string = string, TInput = unknown> = AllowedEv
|
|
|
249
234
|
input: TInput;
|
|
250
235
|
}) => AllowedEventPattern<TEvent> | readonly AllowedEventPattern<TEvent>[]);
|
|
251
236
|
//#endregion
|
|
252
|
-
export {
|
|
237
|
+
export { UserMessage as C, ToolResultPart as S, StandardSchemaV1 as _, AgentToolDescriptor as a, ToolCallPart as b, AllowedEvents as c, EventUnion as d, FilePart as f, NormalizedEventSchemas as g, InferOutput as h, AgentToolChoice as i, AssistantMessage as l, InferInput as m, AgentMessage as n, AgentToolExecute as o, ImagePart as p, AgentTool as r, AgentTools as s, AgentEventSchemaInputMap as t, ChosenEvent as u, SystemMessage as v, WithAgentInputSchema as w, ToolMessage as x, TextPart as y };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@statelyai/agent",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.23",
|
|
4
4
|
"description": "Make invalid agent actions impossible. Agent logic as state machines: deterministic, inspectable, resumable, runs anywhere.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agent",
|
|
@@ -58,6 +58,16 @@
|
|
|
58
58
|
"default": "./dist/machines.cjs"
|
|
59
59
|
}
|
|
60
60
|
},
|
|
61
|
+
"./openai": {
|
|
62
|
+
"import": {
|
|
63
|
+
"types": "./dist/openai.d.mts",
|
|
64
|
+
"default": "./dist/openai.mjs"
|
|
65
|
+
},
|
|
66
|
+
"require": {
|
|
67
|
+
"types": "./dist/openai.d.cts",
|
|
68
|
+
"default": "./dist/openai.cjs"
|
|
69
|
+
}
|
|
70
|
+
},
|
|
61
71
|
"./otel": {
|
|
62
72
|
"import": {
|
|
63
73
|
"types": "./dist/otel.d.mts",
|
|
@@ -78,16 +88,6 @@
|
|
|
78
88
|
"default": "./dist/validate.cjs"
|
|
79
89
|
}
|
|
80
90
|
},
|
|
81
|
-
"./sqlite": {
|
|
82
|
-
"import": {
|
|
83
|
-
"types": "./dist/sqlite.d.mts",
|
|
84
|
-
"default": "./dist/sqlite.mjs"
|
|
85
|
-
},
|
|
86
|
-
"require": {
|
|
87
|
-
"types": "./dist/sqlite.d.cts",
|
|
88
|
-
"default": "./dist/sqlite.cjs"
|
|
89
|
-
}
|
|
90
|
-
},
|
|
91
91
|
"./agent-workflow.json": "./schemas/agent-workflow.json"
|
|
92
92
|
},
|
|
93
93
|
"publishConfig": {
|
|
@@ -100,31 +100,23 @@
|
|
|
100
100
|
"@changesets/cli": "^2.31.1",
|
|
101
101
|
"@earendil-works/pi-ai": "0.83.0",
|
|
102
102
|
"@flue/runtime": "2.0.3",
|
|
103
|
-
"@inquirer/prompts": "^8.6.0",
|
|
104
103
|
"@langchain/core": "^1.2.9",
|
|
105
104
|
"@langchain/openai": "^1.5.10",
|
|
106
105
|
"@mastra/core": "^1.61.0",
|
|
107
106
|
"@opentelemetry/api": "^1.9.1",
|
|
108
107
|
"@opentelemetry/context-async-hooks": "^2.10.0",
|
|
109
|
-
"@opentelemetry/exporter-trace-otlp-http": "^0.221.0",
|
|
110
108
|
"@opentelemetry/sdk-trace-base": "^2.10.0",
|
|
111
|
-
"@opentelemetry/sdk-trace-node": "^2.10.0",
|
|
112
|
-
"@statelyai/sdk": "^0.22.1",
|
|
113
|
-
"@types/express": "^5.0.6",
|
|
114
109
|
"@types/node": "^20.19.43",
|
|
115
110
|
"@types/react": "^19.2.18",
|
|
116
111
|
"ai": "^7.0.0",
|
|
117
112
|
"ajv": "^8.20.0",
|
|
118
113
|
"braintrust": "^3.28.0",
|
|
119
114
|
"dotenv": "^16.6.1",
|
|
120
|
-
"express": "^5.2.1",
|
|
121
|
-
"hono": "^4.13.3",
|
|
122
115
|
"knip": "6.24.0",
|
|
123
116
|
"langchain": "^1.5.10",
|
|
124
117
|
"openai": "^7.5.0",
|
|
125
118
|
"oxfmt": "0.57.0",
|
|
126
119
|
"oxlint": "1.72.0",
|
|
127
|
-
"react": "^19.2.8",
|
|
128
120
|
"tsdown": "^0.21.10",
|
|
129
121
|
"tsx": "^4.23.12",
|
|
130
122
|
"twoslash": "^0.3.9",
|
|
@@ -138,6 +130,7 @@
|
|
|
138
130
|
"@opentelemetry/api": "^1",
|
|
139
131
|
"ai": "^7.0.0",
|
|
140
132
|
"ajv": "^8.20.0",
|
|
133
|
+
"openai": ">=5.0.0 <8",
|
|
141
134
|
"xstate": ">=6.0.0-alpha.46 <6.0.0"
|
|
142
135
|
},
|
|
143
136
|
"peerDependenciesMeta": {
|
|
@@ -149,6 +142,9 @@
|
|
|
149
142
|
},
|
|
150
143
|
"ajv": {
|
|
151
144
|
"optional": true
|
|
145
|
+
},
|
|
146
|
+
"openai": {
|
|
147
|
+
"optional": true
|
|
152
148
|
}
|
|
153
149
|
},
|
|
154
150
|
"engines": {
|
|
@@ -162,7 +158,7 @@
|
|
|
162
158
|
"typecheck": "pnpm run typecheck:src && pnpm run typecheck:examples && pnpm run typecheck:example-packages",
|
|
163
159
|
"typecheck:src": "tsc --noEmit",
|
|
164
160
|
"typecheck:examples": "tsc -p examples/tsconfig.json --noEmit",
|
|
165
|
-
"typecheck:example-packages": "pnpm run build && tsc -p examples/next-host --noEmit && tsc -p examples/tanstack-
|
|
161
|
+
"typecheck:example-packages": "pnpm run build && tsc -p examples/next-host --noEmit && tsc -p examples/tanstack-ai-stream --noEmit && tsc -p examples/cloudflare-agent-host --noEmit && tsc -p examples/cloudflare-workers-ai-host --noEmit && tsc -p demo --noEmit",
|
|
166
162
|
"test:cloudflare": "pnpm --filter @statelyai/example-cloudflare-agent-host test && pnpm --filter @statelyai/example-cloudflare-workers-ai-host test",
|
|
167
163
|
"lint": "oxlint",
|
|
168
164
|
"format": "oxfmt src examples",
|
|
@@ -172,7 +168,7 @@
|
|
|
172
168
|
"check:dts": "tsc -p fixtures/dts-consumer/tsconfig.json",
|
|
173
169
|
"test": "vitest",
|
|
174
170
|
"test:live": "LIVE_TESTS=1 vitest --run",
|
|
175
|
-
"test:ci": "vitest --run && pnpm run test:cloudflare",
|
|
171
|
+
"test:ci": "vitest --run && pnpm run test:cloudflare && pnpm run build && pnpm --filter @statelyai/agent-demo test",
|
|
176
172
|
"changeset": "changeset",
|
|
177
173
|
"release": "changeset publish",
|
|
178
174
|
"version": "changeset version"
|