@statelyai/agent 2.0.0-alpha.30 → 2.0.0-alpha.32
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/CHANGELOG.md +1743 -0
- package/dist/ai-sdk.cjs +8 -8
- package/dist/ai-sdk.d.cts +1 -1
- package/dist/ai-sdk.d.mts +1 -1
- package/dist/ai-sdk.mjs +3 -3
- package/dist/{decision-CIb7ddIv.cjs → decision-BPdEaxPf.cjs} +30 -17
- package/dist/{decision-BHkvuImD.mjs → decision-CV7WiV5C.mjs} +22 -9
- package/dist/{event-log-BZRfBRd7.cjs → event-log-CKWeC7H-.cjs} +125 -27
- package/dist/{event-log-DJCMHek_.mjs → event-log-DAqsalT1.mjs} +108 -28
- package/dist/{event-log-store-DsP6GCRh.mjs → event-log-store-BGehvVTb.mjs} +2 -2
- package/dist/{event-log-store-CIIJohjj.d.mts → event-log-store-C2NjVlWU.d.mts} +13 -5
- package/dist/{event-log-store-DZR4IIWe.cjs → event-log-store-DKyE7JKr.cjs} +2 -2
- package/dist/{event-log-store-CLAC9q2Y.d.cts → event-log-store-s73r1Hxf.d.cts} +13 -5
- package/dist/index.cjs +129 -21
- package/dist/index.d.cts +59 -11
- package/dist/index.d.mts +59 -11
- package/dist/index.mjs +126 -19
- package/dist/log.cjs +40 -5
- package/dist/log.d.cts +1 -1
- package/dist/log.d.mts +1 -1
- package/dist/log.mjs +40 -5
- package/dist/machines.cjs +1 -1
- package/dist/machines.mjs +1 -1
- package/dist/{model-source-BCKg4qJ1.cjs → model-source-BQ97_l1-.cjs} +4 -5
- package/dist/{model-source-BaHwJ1wK.mjs → model-source-BilDEyqk.mjs} +1 -2
- package/dist/openai.cjs +2 -2
- package/dist/openai.d.cts +1 -1
- package/dist/openai.d.mts +1 -1
- package/dist/openai.mjs +2 -2
- package/dist/otel.d.cts +1 -1
- package/dist/otel.d.mts +1 -1
- package/dist/{run-agent-BpHqkaZw.mjs → run-agent-2UnSUhDb.mjs} +93 -37
- package/dist/{run-agent-CAVt5R-r.d.mts → run-agent-Atu-wEk7.d.mts} +17 -6
- package/dist/{run-agent-CTlMc3Jb.d.cts → run-agent-B5QhabdY.d.cts} +17 -6
- package/dist/{run-agent-ctLCvNgL.cjs → run-agent-Cg0xd3K-.cjs} +93 -37
- package/dist/{setup-agent-CLPCkXsu.d.cts → setup-agent-BiF1N3td.d.cts} +11 -3
- package/dist/{setup-agent-7rV_cujV.mjs → setup-agent-CmhB-U5P.mjs} +38 -3
- package/dist/{setup-agent-DoUk7EdC.cjs → setup-agent-CwUJyqKB.cjs} +38 -3
- package/dist/{setup-agent-CGELn1qZ.d.mts → setup-agent-J9f_Wu5T.d.mts} +11 -3
- package/dist/tanstack-ai.cjs +3 -3
- package/dist/tanstack-ai.d.cts +1 -1
- package/dist/tanstack-ai.d.mts +1 -1
- package/dist/tanstack-ai.mjs +3 -3
- package/dist/testing.cjs +73 -29
- package/dist/testing.d.cts +47 -10
- package/dist/testing.d.mts +47 -10
- package/dist/testing.mjs +65 -22
- package/dist/{text-logic-DmciuVUQ.d.cts → text-logic-CYP502VT.d.cts} +9 -3
- package/dist/{text-logic-BRYOPWvY.d.mts → text-logic-DuK9HIFM.d.mts} +9 -3
- package/dist/{usage-BkM3z02Y.d.mts → usage-D7JKAxLc.d.mts} +1 -1
- package/dist/{usage-Cm_ovz9V.d.cts → usage-DVw-646x.d.cts} +1 -1
- package/dist/validate.cjs +3 -3
- package/dist/validate.d.cts +2 -2
- package/dist/validate.d.mts +2 -2
- package/dist/validate.mjs +3 -3
- package/dist/{verify-Cn86rT8l.d.mts → verify-BVz4cURF.d.mts} +14 -6
- package/dist/{verify-COFWyprJ.mjs → verify-CoCvR1eM.mjs} +55 -5
- package/dist/{verify-CVlsTZ-V.cjs → verify-D_llRCTr.cjs} +57 -7
- package/dist/{verify-Dzwd4Mvk.d.cts → verify-Dk3Vwnha.d.cts} +14 -6
- package/package.json +34 -4
- package/readme.md +70 -40
- package/skills/add-evals/SKILL.md +23 -8
- package/skills/add-traces/SKILL.md +23 -11
- package/skills/generate-machine/SKILL.md +2 -2
- package/skills/graph-engineering/SKILL.md +1 -1
- package/skills/improve-machine/SKILL.md +7 -6
- package/skills/loop-to-machine/SKILL.md +34 -7
- package/skills/loop-to-machine/policy.md +14 -18
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
const require_event_log = require("./event-log-
|
|
2
|
-
const require_decision = require("./decision-
|
|
3
|
-
const require_event_log_store = require("./event-log-store-
|
|
1
|
+
const require_event_log = require("./event-log-CKWeC7H-.cjs");
|
|
2
|
+
const require_decision = require("./decision-BPdEaxPf.cjs");
|
|
3
|
+
const require_event_log_store = require("./event-log-store-DKyE7JKr.cjs");
|
|
4
4
|
let xstate = require("xstate");
|
|
5
5
|
let xstate_durable = require("xstate/durable");
|
|
6
6
|
//#region src/run-agent.ts
|
|
@@ -290,6 +290,13 @@ function unrebindableChildRequestError(childPath, stateName, requestSrc, kind) {
|
|
|
290
290
|
function declaresUsageTransition(snapshot) {
|
|
291
291
|
return (0, xstate.getNextTransitions)(snapshot).some((transition) => transition.eventType === "@agent.usage" || transition.eventType === "*");
|
|
292
292
|
}
|
|
293
|
+
function observe(name, call) {
|
|
294
|
+
try {
|
|
295
|
+
call();
|
|
296
|
+
} catch (error) {
|
|
297
|
+
console.error(`@statelyai/agent: ${name} threw; the run continues.`, error);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
293
300
|
/**
|
|
294
301
|
* Builds the ONE place a trace payload is emitted: it hands the payload to the
|
|
295
302
|
* envelope-stamping trace sink and, from that same payload, invokes the sugar
|
|
@@ -306,21 +313,21 @@ function createTraceDispatch(sinks) {
|
|
|
306
313
|
return (payload, self) => {
|
|
307
314
|
switch (payload.type) {
|
|
308
315
|
case "stream.chunk":
|
|
309
|
-
sinks.onTrace?.(payload, self);
|
|
310
|
-
sinks.onChunk?.(payload.chunk, { request: payload.request });
|
|
316
|
+
observe("onTrace", () => sinks.onTrace?.(payload, self));
|
|
317
|
+
observe("onChunk", () => sinks.onChunk?.(payload.chunk, { request: payload.request }));
|
|
311
318
|
return;
|
|
312
319
|
case "request.end":
|
|
313
|
-
sinks.onResult?.(payload.request, {
|
|
320
|
+
observe("onResult", () => sinks.onResult?.(payload.request, {
|
|
314
321
|
result: payload.output,
|
|
315
322
|
raw: payload.raw
|
|
316
|
-
});
|
|
317
|
-
sinks.onTrace?.(payload, self);
|
|
323
|
+
}));
|
|
324
|
+
observe("onTrace", () => sinks.onTrace?.(payload, self));
|
|
318
325
|
return;
|
|
319
326
|
case "machine.transition":
|
|
320
|
-
sinks.onTrace?.(payload, self);
|
|
321
|
-
sinks.onTransition?.(payload.snapshot, payload.event);
|
|
327
|
+
observe("onTrace", () => sinks.onTrace?.(payload, self));
|
|
328
|
+
observe("onTransition", () => sinks.onTransition?.(payload.snapshot, payload.event));
|
|
322
329
|
return;
|
|
323
|
-
default: sinks.onTrace?.(payload, self);
|
|
330
|
+
default: observe("onTrace", () => sinks.onTrace?.(payload, self));
|
|
324
331
|
}
|
|
325
332
|
};
|
|
326
333
|
}
|
|
@@ -509,6 +516,11 @@ function bindDecisionLogic(logic, runCtx) {
|
|
|
509
516
|
schemas: runCtx.schemas,
|
|
510
517
|
eventTypes: declaredEventTypes
|
|
511
518
|
}) : [];
|
|
519
|
+
const agentCan = (event) => {
|
|
520
|
+
if (!actorRef) return true;
|
|
521
|
+
const snapshot = actorRef.getSnapshot();
|
|
522
|
+
return !require_decision.getHumanEventTypes(snapshot).has(event.type) && snapshot.can(event);
|
|
523
|
+
};
|
|
512
524
|
const lowered = logic.request(input);
|
|
513
525
|
const request = {
|
|
514
526
|
...lowered,
|
|
@@ -521,7 +533,7 @@ function bindDecisionLogic(logic, runCtx) {
|
|
|
521
533
|
const options = require_decision.acceptedDecisionOptions(request, (event) => {
|
|
522
534
|
let accepted;
|
|
523
535
|
try {
|
|
524
|
-
accepted =
|
|
536
|
+
accepted = agentCan(event);
|
|
525
537
|
} catch {
|
|
526
538
|
accepted = false;
|
|
527
539
|
}
|
|
@@ -532,6 +544,10 @@ function bindDecisionLogic(logic, runCtx) {
|
|
|
532
544
|
request.options = options;
|
|
533
545
|
request.withheld = withheld;
|
|
534
546
|
}
|
|
547
|
+
if (actorRef) {
|
|
548
|
+
const humanEventTypes = require_decision.getHumanEventTypes(actorRef.getSnapshot());
|
|
549
|
+
request.events = events.filter((event) => !humanEventTypes.has(event.type));
|
|
550
|
+
}
|
|
535
551
|
if (options?.length === 0) {
|
|
536
552
|
const error = new require_decision.AgentDecisionExhaustedError([]);
|
|
537
553
|
error.message = `Decision '${request.name}' has no offered option the current snapshot accepts; no model call was made.`;
|
|
@@ -554,7 +570,7 @@ function bindDecisionLogic(logic, runCtx) {
|
|
|
554
570
|
} }, {
|
|
555
571
|
maxRetries: lowered.maxRetries ?? logic.maxRetries,
|
|
556
572
|
signal,
|
|
557
|
-
canTake:
|
|
573
|
+
canTake: agentCan
|
|
558
574
|
});
|
|
559
575
|
if (priorAttempts.length > 0) runCtx.noteDecision?.(chosen, priorAttempts);
|
|
560
576
|
actorRef?.send(chosen);
|
|
@@ -870,7 +886,7 @@ function assertThreadMatchesEvents(threadId, stored, events) {
|
|
|
870
886
|
const givenEntry = events[index];
|
|
871
887
|
const storedHash = storedEntry.verification?.stateHash;
|
|
872
888
|
const givenHash = givenEntry.verification?.stateHash;
|
|
873
|
-
if (!(storedEntry.id === givenEntry.id && storedEntry.index === givenEntry.index && require_event_log.canonicalEventJson(storedEntry.event) === require_event_log.canonicalEventJson(givenEntry.event) && (storedHash === void 0 || givenHash === void 0 || storedHash === givenHash))) throw new require_event_log.AgentError("event-log-conflict", `createAgentRuntime: the given \`events\` diverge from thread "${threadId}" at index ${index} (stored entry '${storedEntry.id}', given '${givenEntry.id}') — the log passed in is not this thread's log.`);
|
|
889
|
+
if (!(storedEntry.id === givenEntry.id && storedEntry.index === givenEntry.index && require_event_log.canonicalEventJson(storedEntry.event) === require_event_log.canonicalEventJson(givenEntry.event) && (storedHash === void 0 || givenHash === void 0 || storedHash === givenHash) && (index > 0 || require_event_log.getLogExecutionId([storedEntry]) === require_event_log.getLogExecutionId([givenEntry])))) throw new require_event_log.AgentError("event-log-conflict", `createAgentRuntime: the given \`events\` diverge from thread "${threadId}" at index ${index} (stored entry '${storedEntry.id}', given '${givenEntry.id}') — the log passed in is not this thread's log.`);
|
|
874
890
|
}
|
|
875
891
|
}
|
|
876
892
|
const QUIESCENT = Symbol("quiescent");
|
|
@@ -1000,13 +1016,32 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
1000
1016
|
let loggingEnabled = true;
|
|
1001
1017
|
const attemptsByEvent = /* @__PURE__ */ new WeakMap();
|
|
1002
1018
|
let logFold;
|
|
1019
|
+
const stopLogging = (error) => {
|
|
1020
|
+
if (!loggingEnabled) return;
|
|
1021
|
+
loggingEnabled = false;
|
|
1022
|
+
if (store !== void 0) {
|
|
1023
|
+
journalError ??= error;
|
|
1024
|
+
stopRun({
|
|
1025
|
+
cause: "journal",
|
|
1026
|
+
error
|
|
1027
|
+
});
|
|
1028
|
+
return;
|
|
1029
|
+
}
|
|
1030
|
+
console.warn(`@statelyai/agent: this run's log stopped at entry ${logEntries.length}, so result.events is incomplete: ${error instanceof Error ? error.message : String(error)}. Pass input and events that JSON can hold.`);
|
|
1031
|
+
};
|
|
1003
1032
|
const appendLogEntry = (event, attempts) => {
|
|
1004
1033
|
if (!loggingEnabled) return;
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1034
|
+
let entry;
|
|
1035
|
+
try {
|
|
1036
|
+
entry = require_event_log.createReplayEntry(logMachine, logEntries, event, {
|
|
1037
|
+
machineVersion,
|
|
1038
|
+
verification: false,
|
|
1039
|
+
...attempts !== void 0 ? { attempts } : {}
|
|
1040
|
+
});
|
|
1041
|
+
} catch (error) {
|
|
1042
|
+
stopLogging(error);
|
|
1043
|
+
return;
|
|
1044
|
+
}
|
|
1010
1045
|
if (verificationEnabled && logFold !== null) try {
|
|
1011
1046
|
logFold ??= require_event_log.foldLog(logMachine, logEntries, {
|
|
1012
1047
|
machineVersion,
|
|
@@ -1018,7 +1053,7 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
1018
1053
|
}
|
|
1019
1054
|
logEntries.push(entry);
|
|
1020
1055
|
journalEntry(entry);
|
|
1021
|
-
options.onEvent?.(entry);
|
|
1056
|
+
observe("onEvent", () => options.onEvent?.(entry));
|
|
1022
1057
|
return entry;
|
|
1023
1058
|
};
|
|
1024
1059
|
const recordUsage = (usage, source = {}) => {
|
|
@@ -1193,8 +1228,8 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
1193
1228
|
event
|
|
1194
1229
|
});
|
|
1195
1230
|
const on = options.on ?? {};
|
|
1196
|
-
on[event.type]?.(event);
|
|
1197
|
-
if (event.type !== "*") on["*"]?.(event);
|
|
1231
|
+
observe(`on['${event.type}']`, () => on[event.type]?.(event));
|
|
1232
|
+
if (event.type !== "*") observe("on['*']", () => on["*"]?.(event));
|
|
1198
1233
|
};
|
|
1199
1234
|
const inspect = options.inspect === void 0 ? void 0 : (event) => {
|
|
1200
1235
|
if (typeof options.inspect === "function") options.inspect(event);
|
|
@@ -1248,14 +1283,15 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
1248
1283
|
}
|
|
1249
1284
|
}, inspect ? { inspect } : void 0);
|
|
1250
1285
|
const runErrorCause = (error) => budgetExceeded ? "max-model-calls" : wrapsDecisionExhausted(error) ? "decision-exhausted" : "machine";
|
|
1251
|
-
const commit = (snapshot, event) => {
|
|
1286
|
+
const commit = (snapshot, event, ignored = false) => {
|
|
1252
1287
|
const from = { value: (current ?? snapshot).value };
|
|
1253
1288
|
current = snapshot;
|
|
1254
1289
|
onTrace({
|
|
1255
1290
|
type: "machine.transition",
|
|
1256
1291
|
from,
|
|
1257
1292
|
snapshot,
|
|
1258
|
-
event
|
|
1293
|
+
event,
|
|
1294
|
+
...ignored ? { ignored: true } : {}
|
|
1259
1295
|
});
|
|
1260
1296
|
};
|
|
1261
1297
|
/** True for a child completion/failure whose child is no longer running under the current state. */
|
|
@@ -1269,14 +1305,23 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
1269
1305
|
if (started) throw new require_event_log.AgentError("runtime-started", "createAgentRuntime: `start` was already called.");
|
|
1270
1306
|
started = true;
|
|
1271
1307
|
let resumeEvents = init.events;
|
|
1308
|
+
let resumedFromStore = false;
|
|
1272
1309
|
if (store !== void 0) {
|
|
1273
1310
|
const storedThread = await store.read(threadId);
|
|
1274
1311
|
if (resumeEvents !== void 0) {
|
|
1275
1312
|
if (storedThread.length !== resumeEvents.length) throw new require_event_log_store.AgentEventLogConflictError(threadId, resumeEvents.length, storedThread.length);
|
|
1276
1313
|
assertThreadMatchesEvents(threadId, storedThread, resumeEvents);
|
|
1277
|
-
|
|
1314
|
+
resumedFromStore = storedThread.length > 0;
|
|
1315
|
+
} else if (storedThread.length > 0) {
|
|
1316
|
+
resumeEvents = storedThread;
|
|
1317
|
+
resumedFromStore = true;
|
|
1318
|
+
}
|
|
1278
1319
|
}
|
|
1279
1320
|
const resolvedInput = resolveMachineInput(machine, init.input);
|
|
1321
|
+
const threadInit = resumeEvents?.[0]?.event;
|
|
1322
|
+
if (resumedFromStore && init.input !== void 0 && threadInit !== void 0 && Object.hasOwn(threadInit, "input")) {
|
|
1323
|
+
if (require_event_log.canonicalJson(threadInit.input) !== require_event_log.canonicalJson(resolvedInput)) throw new require_event_log.AgentError("thread-input-mismatch", `createAgentRuntime: thread '${threadId}' was started with different input. A thread that already has entries is resumed, not restarted; give a new run a new threadId.`);
|
|
1324
|
+
}
|
|
1280
1325
|
let effectiveSnapshot = init.snapshot;
|
|
1281
1326
|
let seedInitEntry;
|
|
1282
1327
|
const restoredPersistedSnapshot = () => {
|
|
@@ -1294,18 +1339,25 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
1294
1339
|
* run WITHOUT a log rather than failing it.
|
|
1295
1340
|
*/
|
|
1296
1341
|
const appendInitEntry = (candidates, metadata) => {
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1342
|
+
let lastError;
|
|
1343
|
+
for (const candidate of candidates) for (const verification of verificationEnabled ? [true, false] : [false]) {
|
|
1344
|
+
let entry;
|
|
1345
|
+
try {
|
|
1346
|
+
entry = require_event_log.initEntry(logMachine, candidate, {
|
|
1347
|
+
machineVersion,
|
|
1348
|
+
verification,
|
|
1349
|
+
metadata
|
|
1350
|
+
});
|
|
1351
|
+
} catch (error) {
|
|
1352
|
+
lastError = error;
|
|
1353
|
+
continue;
|
|
1354
|
+
}
|
|
1303
1355
|
logEntries.push(entry);
|
|
1304
1356
|
journalEntry(entry);
|
|
1305
|
-
options.onEvent?.(entry);
|
|
1357
|
+
observe("onEvent", () => options.onEvent?.(entry));
|
|
1306
1358
|
return;
|
|
1307
|
-
}
|
|
1308
|
-
|
|
1359
|
+
}
|
|
1360
|
+
stopLogging(lastError);
|
|
1309
1361
|
};
|
|
1310
1362
|
/** Init forms for a snapshot resume, most faithful first. */
|
|
1311
1363
|
const snapshotInitCandidates = () => {
|
|
@@ -1355,7 +1407,9 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
1355
1407
|
machineVersion: tail.machineVersion
|
|
1356
1408
|
});
|
|
1357
1409
|
const bridgeMeta = readAgentMeta(init.snapshot);
|
|
1358
|
-
|
|
1410
|
+
const forkedFrom = resumeEvents[0].metadata?.forkedFrom;
|
|
1411
|
+
const parentCache = forkedFrom?.executionId !== void 0 && bridgeMeta?.logId === forkedFrom.executionId && forkedFrom.logIndex === resumeEvents.length;
|
|
1412
|
+
if (inheritedExecutionId === void 0 || bridgeMeta?.logId !== inheritedExecutionId && !parentCache) throw new require_event_log.AgentMachineVersionMismatchError(tail.id, tail.index, {
|
|
1359
1413
|
machineId,
|
|
1360
1414
|
machineVersion
|
|
1361
1415
|
}, {
|
|
@@ -1456,9 +1510,11 @@ function createAgentRuntime(machine, options = {}) {
|
|
|
1456
1510
|
const attempts = transitionOptions?.attempts ?? attemptsByEvent.get(event);
|
|
1457
1511
|
if (transitionOptions?.attempts !== void 0) require_event_log.assertJsonSerializable(transitionOptions.attempts, "attempts");
|
|
1458
1512
|
const [next, effects] = execution.transition(state, event);
|
|
1459
|
-
|
|
1513
|
+
const ignored = !fromMailbox.has(event) && next === state && effects.length === 0;
|
|
1514
|
+
if (ignored) ignoredEvent = event;
|
|
1460
1515
|
appendLogEntry(event, attempts);
|
|
1461
|
-
|
|
1516
|
+
if (stopReason !== void 0) return [state, []];
|
|
1517
|
+
commit(next, event, ignored);
|
|
1462
1518
|
return [next, effects];
|
|
1463
1519
|
},
|
|
1464
1520
|
async execute(effects) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { _ as StandardSchemaV1, d as EventUnion, g as NormalizedEventSchemas, h as InferOutput, i as AgentToolChoice, m as InferInput, s as AgentTools, t as AgentEventSchemaInputMap, w as WithAgentInputSchema } from "./types-Dp5_Ru4R.cjs";
|
|
2
|
-
import { J as AgentSchemas, Y as EventFromSnapshot, _ as TextLogic, a as AgentModelRef, d as AgentRequestMode, h as BuiltinAgentActors, i as AgentModelMap, v as TextLogicConfig } from "./text-logic-
|
|
3
|
-
import { n as AgentUsageEvent, t as AGENT_USAGE_EVENT_TYPE } from "./usage-
|
|
2
|
+
import { J as AgentSchemas, Y as EventFromSnapshot, _ as TextLogic, a as AgentModelRef, d as AgentRequestMode, h as BuiltinAgentActors, i as AgentModelMap, v as TextLogicConfig } from "./text-logic-CYP502VT.cjs";
|
|
3
|
+
import { n as AgentUsageEvent, t as AGENT_USAGE_EVENT_TYPE } from "./usage-DVw-646x.cjs";
|
|
4
4
|
import { AnyActorLogic, AnyMachineSnapshot, AnySetupConfig, AnyStateMachine, AsyncActorLogic, EventObject, MachineContext, MetaObject, NonReducibleUnknown, SetupReturnFromConfig, SetupStateSchema } from "xstate";
|
|
5
5
|
//#region src/interaction.d.ts
|
|
6
6
|
interface AgentInteractionEvent<TType extends string = string> {
|
|
@@ -84,13 +84,21 @@ interface AgentInteractionDescriptor<TEvent extends string = string> {
|
|
|
84
84
|
*/
|
|
85
85
|
interface AgentInteractionMeta<TEvent extends string = string> {
|
|
86
86
|
interaction?: AgentInteractionDescriptor<TEvent>;
|
|
87
|
+
/**
|
|
88
|
+
* On a transition: `"human"` means a person sends its event, never a model.
|
|
89
|
+
* Model-facing decisions (`agent.decide`, `decideNext`) withhold an event a
|
|
90
|
+
* state handles through a transition tagged this way. Read from object
|
|
91
|
+
* transitions only; a transition written as a function cannot be tagged.
|
|
92
|
+
*/
|
|
93
|
+
actor?: "human";
|
|
87
94
|
}
|
|
88
95
|
/**
|
|
89
96
|
* A {@link StandardSchemaV1} for the state metadata {@link getInteraction}
|
|
90
97
|
* reads: an object whose optional `interaction` declares a `label` (a string
|
|
91
98
|
* with `{context.path}` interpolation, or a function of the context), an
|
|
92
99
|
* `events` map of choices, payload `choices` (an array, or a context path to
|
|
93
|
-
* one), and a `textEvent` (a type, or `{ type, field }`) for free-text answers
|
|
100
|
+
* one), and a `textEvent` (a type, or `{ type, field }`) for free-text answers; and
|
|
101
|
+
* an optional `actor: "human"`, which tags a transition as sent by a person. This is
|
|
94
102
|
* the default `meta` type `setupAgent` gives a machine; pass it to
|
|
95
103
|
* `createAgentSchemas({ meta: interactionMetaSchema })` only when building a
|
|
96
104
|
* schema pack by hand.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { A as createTextLogic, C as AGENT_USAGE_EVENT_TYPE, G as missingActor, H as getAgentExecutionOptions, K as setAgentExecutionOptions, T as DECIDE_ACTOR, k as builtinTextActors, ot as validateSchemaSync } from "./event-log-
|
|
2
|
-
import { r as createDecideActor } from "./decision-
|
|
1
|
+
import { A as createTextLogic, C as AGENT_USAGE_EVENT_TYPE, G as missingActor, H as getAgentExecutionOptions, K as setAgentExecutionOptions, T as DECIDE_ACTOR, ct as isPlainObject, k as builtinTextActors, ot as validateSchemaSync, ut as AgentError } from "./event-log-DAqsalT1.mjs";
|
|
2
|
+
import { r as createDecideActor } from "./decision-CV7WiV5C.mjs";
|
|
3
3
|
import { machineFromJSON, setup } from "xstate";
|
|
4
4
|
//#region src/workflow-config.ts
|
|
5
5
|
/**
|
|
@@ -387,7 +387,27 @@ function withAgentUsageEventSchema(events) {
|
|
|
387
387
|
};
|
|
388
388
|
}
|
|
389
389
|
function normalizeEventSchemas(events) {
|
|
390
|
-
return Object.fromEntries(Object.entries(events).map(([type, schema]) =>
|
|
390
|
+
return Object.fromEntries(Object.entries(events).map(([type, schema]) => {
|
|
391
|
+
if (schema && typeof schema === "object" && "~standard" in schema) return [type, schema];
|
|
392
|
+
if (!(isPlainObject(schema) && Object.keys(schema).length === 0)) assertStandardSchema(schema, `events.${type}`);
|
|
393
|
+
return [type, emptyEventSchema];
|
|
394
|
+
}));
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Throws for a declared schema without the Standard Schema `~standard`
|
|
398
|
+
* property. Such a schema would otherwise be skipped, so values it should
|
|
399
|
+
* reject would pass unchecked. Zod 3.23 and earlier are the usual cause.
|
|
400
|
+
*/
|
|
401
|
+
function assertStandardSchema(schema, where) {
|
|
402
|
+
if (schema === void 0 || typeof schema === "object" && schema !== null && "~standard" in schema) return;
|
|
403
|
+
throw new AgentError("invalid-schema", `setupAgent: '${where}' is not a Standard Schema (it has no '~standard' property), so it cannot be used to validate. Use zod 4 or zod 3.24 and later, Valibot, ArkType, or any other Standard Schema library; zod 3.23 and earlier do not implement it.`);
|
|
404
|
+
}
|
|
405
|
+
function assertStateSchemasAreStandard(states, path = []) {
|
|
406
|
+
for (const [key, state] of Object.entries(states ?? {})) {
|
|
407
|
+
const statePath = [...path, key];
|
|
408
|
+
for (const [name, schema] of Object.entries(state?.schemas ?? {})) assertStandardSchema(schema, `states.${statePath.join(".states.")}.schemas.${name}`);
|
|
409
|
+
assertStateSchemasAreStandard(state?.states, statePath);
|
|
410
|
+
}
|
|
391
411
|
}
|
|
392
412
|
/**
|
|
393
413
|
* Builds a machine's {@link AgentSchemaPack} from a partial schema
|
|
@@ -454,8 +474,23 @@ function createAgentSchemas(schemas) {
|
|
|
454
474
|
*/
|
|
455
475
|
function setupAgent(config) {
|
|
456
476
|
const schemas = normalizeAgentSchemas(config);
|
|
477
|
+
for (const key of [
|
|
478
|
+
"context",
|
|
479
|
+
"input",
|
|
480
|
+
"output",
|
|
481
|
+
"meta"
|
|
482
|
+
]) assertStandardSchema(schemas[key], key);
|
|
483
|
+
for (const [type, schema] of Object.entries(schemas.emitted ?? {})) assertStandardSchema(schema, `emitted.${type}`);
|
|
484
|
+
assertStateSchemasAreStandard(config.states);
|
|
457
485
|
const requestActors = createRequestActors(config.requests ?? {});
|
|
458
486
|
const actors = createAgentActors(config.actors, requestActors);
|
|
487
|
+
for (const [name, request] of Object.entries(config.requests ?? {})) {
|
|
488
|
+
const source = request;
|
|
489
|
+
if (source.prompt === void 0 && source.messages === void 0) throw new AgentError("invalid-request", `setupAgent: request '${name}' has neither \`prompt\` nor \`messages\`, so every call to it fails. A request whose executor builds its own model call still needs one: pass the conversation as \`messages: ({ input }) => input.messages\` (or a \`prompt\`) and read it in the executor.`);
|
|
490
|
+
const requestSchemas = request.schemas;
|
|
491
|
+
assertStandardSchema(requestSchemas?.input, `requests.${name}.schemas.input`);
|
|
492
|
+
assertStandardSchema(requestSchemas?.output, `requests.${name}.schemas.output`);
|
|
493
|
+
}
|
|
459
494
|
const setupConfig = {
|
|
460
495
|
schemas: {
|
|
461
496
|
context: schemas.context,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const require_event_log = require("./event-log-
|
|
2
|
-
const require_decision = require("./decision-
|
|
1
|
+
const require_event_log = require("./event-log-CKWeC7H-.cjs");
|
|
2
|
+
const require_decision = require("./decision-BPdEaxPf.cjs");
|
|
3
3
|
let xstate = require("xstate");
|
|
4
4
|
//#region src/workflow-config.ts
|
|
5
5
|
/**
|
|
@@ -387,7 +387,27 @@ function withAgentUsageEventSchema(events) {
|
|
|
387
387
|
};
|
|
388
388
|
}
|
|
389
389
|
function normalizeEventSchemas(events) {
|
|
390
|
-
return Object.fromEntries(Object.entries(events).map(([type, schema]) =>
|
|
390
|
+
return Object.fromEntries(Object.entries(events).map(([type, schema]) => {
|
|
391
|
+
if (schema && typeof schema === "object" && "~standard" in schema) return [type, schema];
|
|
392
|
+
if (!(require_event_log.isPlainObject(schema) && Object.keys(schema).length === 0)) assertStandardSchema(schema, `events.${type}`);
|
|
393
|
+
return [type, emptyEventSchema];
|
|
394
|
+
}));
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Throws for a declared schema without the Standard Schema `~standard`
|
|
398
|
+
* property. Such a schema would otherwise be skipped, so values it should
|
|
399
|
+
* reject would pass unchecked. Zod 3.23 and earlier are the usual cause.
|
|
400
|
+
*/
|
|
401
|
+
function assertStandardSchema(schema, where) {
|
|
402
|
+
if (schema === void 0 || typeof schema === "object" && schema !== null && "~standard" in schema) return;
|
|
403
|
+
throw new require_event_log.AgentError("invalid-schema", `setupAgent: '${where}' is not a Standard Schema (it has no '~standard' property), so it cannot be used to validate. Use zod 4 or zod 3.24 and later, Valibot, ArkType, or any other Standard Schema library; zod 3.23 and earlier do not implement it.`);
|
|
404
|
+
}
|
|
405
|
+
function assertStateSchemasAreStandard(states, path = []) {
|
|
406
|
+
for (const [key, state] of Object.entries(states ?? {})) {
|
|
407
|
+
const statePath = [...path, key];
|
|
408
|
+
for (const [name, schema] of Object.entries(state?.schemas ?? {})) assertStandardSchema(schema, `states.${statePath.join(".states.")}.schemas.${name}`);
|
|
409
|
+
assertStateSchemasAreStandard(state?.states, statePath);
|
|
410
|
+
}
|
|
391
411
|
}
|
|
392
412
|
/**
|
|
393
413
|
* Builds a machine's {@link AgentSchemaPack} from a partial schema
|
|
@@ -454,8 +474,23 @@ function createAgentSchemas(schemas) {
|
|
|
454
474
|
*/
|
|
455
475
|
function setupAgent(config) {
|
|
456
476
|
const schemas = normalizeAgentSchemas(config);
|
|
477
|
+
for (const key of [
|
|
478
|
+
"context",
|
|
479
|
+
"input",
|
|
480
|
+
"output",
|
|
481
|
+
"meta"
|
|
482
|
+
]) assertStandardSchema(schemas[key], key);
|
|
483
|
+
for (const [type, schema] of Object.entries(schemas.emitted ?? {})) assertStandardSchema(schema, `emitted.${type}`);
|
|
484
|
+
assertStateSchemasAreStandard(config.states);
|
|
457
485
|
const requestActors = createRequestActors(config.requests ?? {});
|
|
458
486
|
const actors = createAgentActors(config.actors, requestActors);
|
|
487
|
+
for (const [name, request] of Object.entries(config.requests ?? {})) {
|
|
488
|
+
const source = request;
|
|
489
|
+
if (source.prompt === void 0 && source.messages === void 0) throw new require_event_log.AgentError("invalid-request", `setupAgent: request '${name}' has neither \`prompt\` nor \`messages\`, so every call to it fails. A request whose executor builds its own model call still needs one: pass the conversation as \`messages: ({ input }) => input.messages\` (or a \`prompt\`) and read it in the executor.`);
|
|
490
|
+
const requestSchemas = request.schemas;
|
|
491
|
+
assertStandardSchema(requestSchemas?.input, `requests.${name}.schemas.input`);
|
|
492
|
+
assertStandardSchema(requestSchemas?.output, `requests.${name}.schemas.output`);
|
|
493
|
+
}
|
|
459
494
|
const setupConfig = {
|
|
460
495
|
schemas: {
|
|
461
496
|
context: schemas.context,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { _ as StandardSchemaV1, d as EventUnion, g as NormalizedEventSchemas, h as InferOutput, i as AgentToolChoice, m as InferInput, s as AgentTools, t as AgentEventSchemaInputMap, w as WithAgentInputSchema } from "./types-Dp5_Ru4R.mjs";
|
|
2
|
-
import { J as AgentSchemas, Y as EventFromSnapshot, _ as TextLogic, a as AgentModelRef, d as AgentRequestMode, h as BuiltinAgentActors, i as AgentModelMap, v as TextLogicConfig } from "./text-logic-
|
|
3
|
-
import { n as AgentUsageEvent, t as AGENT_USAGE_EVENT_TYPE } from "./usage-
|
|
2
|
+
import { J as AgentSchemas, Y as EventFromSnapshot, _ as TextLogic, a as AgentModelRef, d as AgentRequestMode, h as BuiltinAgentActors, i as AgentModelMap, v as TextLogicConfig } from "./text-logic-DuK9HIFM.mjs";
|
|
3
|
+
import { n as AgentUsageEvent, t as AGENT_USAGE_EVENT_TYPE } from "./usage-D7JKAxLc.mjs";
|
|
4
4
|
import { AnyActorLogic, AnyMachineSnapshot, AnySetupConfig, AnyStateMachine, AsyncActorLogic, EventObject, MachineContext, MetaObject, NonReducibleUnknown, SetupReturnFromConfig, SetupStateSchema } from "xstate";
|
|
5
5
|
//#region src/interaction.d.ts
|
|
6
6
|
interface AgentInteractionEvent<TType extends string = string> {
|
|
@@ -84,13 +84,21 @@ interface AgentInteractionDescriptor<TEvent extends string = string> {
|
|
|
84
84
|
*/
|
|
85
85
|
interface AgentInteractionMeta<TEvent extends string = string> {
|
|
86
86
|
interaction?: AgentInteractionDescriptor<TEvent>;
|
|
87
|
+
/**
|
|
88
|
+
* On a transition: `"human"` means a person sends its event, never a model.
|
|
89
|
+
* Model-facing decisions (`agent.decide`, `decideNext`) withhold an event a
|
|
90
|
+
* state handles through a transition tagged this way. Read from object
|
|
91
|
+
* transitions only; a transition written as a function cannot be tagged.
|
|
92
|
+
*/
|
|
93
|
+
actor?: "human";
|
|
87
94
|
}
|
|
88
95
|
/**
|
|
89
96
|
* A {@link StandardSchemaV1} for the state metadata {@link getInteraction}
|
|
90
97
|
* reads: an object whose optional `interaction` declares a `label` (a string
|
|
91
98
|
* with `{context.path}` interpolation, or a function of the context), an
|
|
92
99
|
* `events` map of choices, payload `choices` (an array, or a context path to
|
|
93
|
-
* one), and a `textEvent` (a type, or `{ type, field }`) for free-text answers
|
|
100
|
+
* one), and a `textEvent` (a type, or `{ type, field }`) for free-text answers; and
|
|
101
|
+
* an optional `actor: "human"`, which tags a transition as sent by a person. This is
|
|
94
102
|
* the default `meta` type `setupAgent` gives a machine; pass it to
|
|
95
103
|
* `createAgentSchemas({ meta: interactionMetaSchema })` only when building a
|
|
96
104
|
* schema pack by hand.
|
package/dist/tanstack-ai.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_event_log = require("./event-log-
|
|
3
|
-
const require_decision = require("./decision-
|
|
4
|
-
const require_model_source = require("./model-source-
|
|
2
|
+
const require_event_log = require("./event-log-CKWeC7H-.cjs");
|
|
3
|
+
const require_decision = require("./decision-BPdEaxPf.cjs");
|
|
4
|
+
const require_model_source = require("./model-source-BQ97_l1-.cjs");
|
|
5
5
|
let _tanstack_ai = require("@tanstack/ai");
|
|
6
6
|
//#region src/tanstack-ai/index.ts
|
|
7
7
|
/**
|
package/dist/tanstack-ai.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { i as AgentToolChoice, n as AgentMessage, s as AgentTools, u as ChosenEvent } from "./types-Dp5_Ru4R.cjs";
|
|
2
|
-
import { N as AgentDecisionRequest, W as AgentEventDescriptor, c as AgentRequestExecutorInfo, f as AgentTextRequest, r as AgentFinishReason, t as AgentCallUsage } from "./text-logic-
|
|
2
|
+
import { N as AgentDecisionRequest, W as AgentEventDescriptor, c as AgentRequestExecutorInfo, f as AgentTextRequest, r as AgentFinishReason, t as AgentCallUsage } from "./text-logic-CYP502VT.cjs";
|
|
3
3
|
import { AnyStateMachine } from "xstate";
|
|
4
4
|
import { AnyTextAdapter, ModelMessage, StreamChunk, TokenUsage, Tool } from "@tanstack/ai";
|
|
5
5
|
import { TextActivityOptions } from "@tanstack/ai/adapters";
|
package/dist/tanstack-ai.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { i as AgentToolChoice, n as AgentMessage, s as AgentTools, u as ChosenEvent } from "./types-Dp5_Ru4R.mjs";
|
|
2
|
-
import { N as AgentDecisionRequest, W as AgentEventDescriptor, c as AgentRequestExecutorInfo, f as AgentTextRequest, r as AgentFinishReason, t as AgentCallUsage } from "./text-logic-
|
|
2
|
+
import { N as AgentDecisionRequest, W as AgentEventDescriptor, c as AgentRequestExecutorInfo, f as AgentTextRequest, r as AgentFinishReason, t as AgentCallUsage } from "./text-logic-DuK9HIFM.mjs";
|
|
3
3
|
import { AnyStateMachine } from "xstate";
|
|
4
4
|
import { AnyTextAdapter, ModelMessage, StreamChunk, TokenUsage, Tool } from "@tanstack/ai";
|
|
5
5
|
import { TextActivityOptions } from "@tanstack/ai/adapters";
|
package/dist/tanstack-ai.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { L as parseProviderOutput, R as providerOutputSchema, X as getJsonSchemaSync, Y as getJsonSchema,
|
|
2
|
-
import { c as renderDecisionAttempts } from "./decision-
|
|
3
|
-
import { t as resolveModelSource } from "./model-source-
|
|
1
|
+
import { L as parseProviderOutput, R as providerOutputSchema, X as getJsonSchemaSync, Y as getJsonSchema, dt as AgentTruncatedError, j as getAgentOutputMode, tt as isStandardSchema, w as AGENT_USAGE_TOKEN_FIELDS } from "./event-log-DAqsalT1.mjs";
|
|
2
|
+
import { c as renderDecisionAttempts } from "./decision-CV7WiV5C.mjs";
|
|
3
|
+
import { t as resolveModelSource } from "./model-source-BilDEyqk.mjs";
|
|
4
4
|
import { chat, maxIterations } from "@tanstack/ai";
|
|
5
5
|
//#region src/tanstack-ai/index.ts
|
|
6
6
|
/**
|