@statelyai/agent 2.0.0-alpha.32 → 2.0.0-alpha.34

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 (66) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/dist/ai-sdk.cjs +114 -63
  3. package/dist/ai-sdk.d.cts +11 -2
  4. package/dist/ai-sdk.d.mts +11 -2
  5. package/dist/ai-sdk.mjs +115 -64
  6. package/dist/{decision-BPdEaxPf.cjs → decision-BEsATcr1.cjs} +18 -2
  7. package/dist/{decision-CV7WiV5C.mjs → decision-COkJNjOW.mjs} +18 -2
  8. package/dist/{event-log-CKWeC7H-.cjs → event-log-BWSSLUff.cjs} +139 -10
  9. package/dist/{event-log-DAqsalT1.mjs → event-log-C7jeH0y8.mjs} +123 -12
  10. package/dist/{event-log-store-C2NjVlWU.d.mts → event-log-store-BzXehP7R.d.cts} +28 -3
  11. package/dist/{event-log-store-s73r1Hxf.d.cts → event-log-store-CWjN6lmM.d.mts} +28 -3
  12. package/dist/{event-log-store-DKyE7JKr.cjs → event-log-store-CleWpsp5.cjs} +1 -1
  13. package/dist/{event-log-store-BGehvVTb.mjs → event-log-store-W49TCJXi.mjs} +1 -1
  14. package/dist/index.cjs +5 -5
  15. package/dist/index.d.cts +7 -7
  16. package/dist/index.d.mts +7 -7
  17. package/dist/index.mjs +5 -5
  18. package/dist/log.cjs +2 -2
  19. package/dist/log.d.cts +1 -1
  20. package/dist/log.d.mts +1 -1
  21. package/dist/log.mjs +2 -2
  22. package/dist/machines.cjs +1 -1
  23. package/dist/machines.mjs +1 -1
  24. package/dist/{model-source-BilDEyqk.mjs → model-source-CK3F4YuU.mjs} +1 -1
  25. package/dist/{model-source-BQ97_l1-.cjs → model-source-DmhTMK39.cjs} +1 -1
  26. package/dist/openai.cjs +377 -95
  27. package/dist/openai.d.cts +55 -20
  28. package/dist/openai.d.mts +55 -20
  29. package/dist/openai.mjs +375 -96
  30. package/dist/otel.d.cts +1 -1
  31. package/dist/otel.d.mts +1 -1
  32. package/dist/{run-agent-B5QhabdY.d.cts → run-agent-B1-Eqviq.d.cts} +13 -5
  33. package/dist/{run-agent-Atu-wEk7.d.mts → run-agent-BGdx5913.d.mts} +13 -5
  34. package/dist/{run-agent-2UnSUhDb.mjs → run-agent-Ct_GZw_f.mjs} +124 -38
  35. package/dist/{run-agent-Cg0xd3K-.cjs → run-agent-s5D32w99.cjs} +123 -37
  36. package/dist/{setup-agent-CmhB-U5P.mjs → setup-agent-05uw4dPZ.mjs} +2 -2
  37. package/dist/{setup-agent-BiF1N3td.d.cts → setup-agent-BtfeFfF6.d.cts} +2 -2
  38. package/dist/{setup-agent-J9f_Wu5T.d.mts → setup-agent-C6nqzv_X.d.mts} +2 -2
  39. package/dist/{setup-agent-CwUJyqKB.cjs → setup-agent-CXEBURdo.cjs} +2 -2
  40. package/dist/tanstack-ai.cjs +91 -59
  41. package/dist/tanstack-ai.d.cts +13 -2
  42. package/dist/tanstack-ai.d.mts +13 -2
  43. package/dist/tanstack-ai.mjs +91 -59
  44. package/dist/testing.cjs +1026 -10
  45. package/dist/testing.d.cts +585 -5
  46. package/dist/testing.d.mts +585 -5
  47. package/dist/testing.mjs +1018 -11
  48. package/dist/{text-logic-CYP502VT.d.cts → text-logic-CV_4KGr6.d.cts} +28 -3
  49. package/dist/{text-logic-DuK9HIFM.d.mts → text-logic-Ckh-rJFV.d.mts} +28 -3
  50. package/dist/{usage-D7JKAxLc.d.mts → usage-BDiJKyv9.d.cts} +11 -1
  51. package/dist/{usage-DVw-646x.d.cts → usage-DOoVUGVq.d.mts} +11 -1
  52. package/dist/validate.cjs +3 -3
  53. package/dist/validate.d.cts +2 -2
  54. package/dist/validate.d.mts +2 -2
  55. package/dist/validate.mjs +3 -3
  56. package/dist/{verify-Dk3Vwnha.d.cts → verify-BQpE8hUz.d.cts} +1 -1
  57. package/dist/{verify-CoCvR1eM.mjs → verify-Bpahmr34.mjs} +14 -4
  58. package/dist/{verify-BVz4cURF.d.mts → verify-CqeuEmIk.d.mts} +1 -1
  59. package/dist/{verify-D_llRCTr.cjs → verify-DCLZa9R0.cjs} +19 -3
  60. package/dist/with-model-calls-BXYdBdcu.cjs +45 -0
  61. package/dist/with-model-calls-Bv4V4EHC.mjs +34 -0
  62. package/package.json +5 -5
  63. package/readme.md +14 -13
  64. package/skills/add-evals/SKILL.md +8 -2
  65. package/skills/improve-machine/SKILL.md +1 -1
  66. package/skills/loop-to-machine/SKILL.md +40 -2
package/CHANGELOG.md CHANGED
@@ -1,5 +1,49 @@
1
1
  # @statelyai/agent
2
2
 
3
+ ## 2.0.0-alpha.34
4
+
5
+ ### Patch Changes
6
+
7
+ - [#185](https://github.com/statelyai/agent/pull/185) [`3fffe8a`](https://github.com/statelyai/agent/commit/3fffe8ad81854b5f669d35313ec94f8ace759122) Thanks [@davidkpiano](https://github.com/davidkpiano)! - `info.callKey` is distinct for calls inside an invoked child machine. Such a call names the child's path (`<executionId>:<childId>/<requestId>#<n>`), so a resumed thread no longer restarts every child call at `[#1](https://github.com/statelyai/agent/issues/1)`. A call key's `n` is now the invocation's ordinal at its site in a fold of the log, cancelled invocations included, so a call re-entered after a cancel no longer takes the cancelled call's key after a crash.
8
+
9
+ - [#190](https://github.com/statelyai/agent/pull/190) [`cb6cf39`](https://github.com/statelyai/agent/commit/cb6cf39799f257bebe4024e48b707bbb516ae80c) Thanks [@davidkpiano](https://github.com/davidkpiano)! - An invoked child machine's `after` timers fire under `createAgentRuntime`. They were dropped as internal events sent from outside, so the child never moved. A run also no longer settles `idle` while a child timer is pending when `timers: { schedule, cancel }` is passed: child timers always run in process and count as work in flight.
10
+
11
+ - [#189](https://github.com/statelyai/agent/pull/189) [`d3cc5bb`](https://github.com/statelyai/agent/commit/d3cc5bbbc6a1a5f554a69b405c208e5e597ba392) Thanks [@davidkpiano](https://github.com/davidkpiano)! - An invoked child machine keeps its state across a resume. The runtime journals what each child machine processes as an entry with a new optional `actorPath`, and `replay` steps that child through those entries, so a long-lived child no longer restarts from its initial state on every turn, and a `persist()` snapshot holding its progress resumes instead of being rejected as diverged. A nested machine's `machineVersion` also no longer changes once one of its transitions has run. A child step that throws no longer makes `replay` throw.
12
+
13
+ - [#183](https://github.com/statelyai/agent/pull/183) [`4f26708`](https://github.com/statelyai/agent/commit/4f26708d52992113c10f0feabded62f6b991b81b) Thanks [@davidkpiano](https://github.com/davidkpiano)! - **Fixes from porting five real agent repos.**
14
+
15
+ - **Adapter SDKs install next to any version.** The optional peers `ai`, `openai`, `@tanstack/ai`, and `ajv` have no version range, so an app on `ai` 4 or `openai` 4 installs the core without `--legacy-peer-deps`. The adapters still need the versions in the readme, and fail when imported or called with older ones.
16
+ - **A decision answer that names no event is retried.** The AI SDK, OpenAI, and TanStack AI adapters no longer throw when the model calls no tool, calls a tool that was not offered, or sends arguments that are not JSON. A decide executor now returns such an answer as `rejected: { failure, reason }` (type `AgentDecisionRejection`) instead of `event`; `resolveDecision` records it as a failed attempt (`'unknown-event'`, or `'invalid-payload'` for arguments that are not JSON), feeds the reason back as written, and asks again. Exhausting retries throws `AgentDecisionExhaustedError` as before. The AI SDK closed-choice and decision-model paths report an unoffered option the same way, so reasons are no longer wrapped twice. Code that reads `event` off a decide executor's result directly now checks for `rejected` first. TanStack AI's `generateText` now returns a tool call with malformed arguments (marked `invalid: true`) instead of throwing a `SyntaxError`.
17
+ - **Tool-loop steps count as model calls.** An executor result may report `modelCalls`, and the adapters report one per step. `usage.modelCalls` and `maxModelCalls` count them; the loop's request is admitted as one call, so the call after it is the one refused.
18
+ - **The machine can read its budget.** `@agent.usage` carries `budget: { modelCalls, maxModelCalls }`.
19
+ - **`modelCallsSpent`** counts earlier turns of a conversation against `maxModelCalls` without adding them to this run's `usage`. `runAgent` with `onIdle` uses it between passes.
20
+ - **`firstDivergence(actual, reference, { key: "payload" })`** also compares event payloads, so a fault that changes an output but no state is found. For deterministic or mocked runs.
21
+ - **A decision's `@agent.usage` arrives after the event it chose,** so a budget guard reading it can no longer reject the chosen event and leave the run idle. `firstDivergence` and `foldTraces` no longer give a decision's name to an `@agent.usage` step.
22
+ - **A tool loop that throws after some steps still counts them,** and `key: "payload"` compares errors anywhere in a payload by name, message, `code`, and `cause`.
23
+
24
+ - [#184](https://github.com/statelyai/agent/pull/184) [`cc1f370`](https://github.com/statelyai/agent/commit/cc1f370540b45f2ad6a455263b2f17948d3e41f9) Thanks [@davidkpiano](https://github.com/davidkpiano)! - **Fixes from a second round of real-repo ports.**
25
+
26
+ - **Each decision gets its own `callKey`.** A decision usually ends by sending its chosen event, which exits the state before its invoke completes, so the log held no completion to count. Every turn at a decision site got `[#1](https://github.com/statelyai/agent/issues/1)`, and a crash re-run got the completed call's key. The chosen event's entry now records the call it settles as `entry.decidedBy` (`<requestId>#<n>`), and `agentCallOccurrence` counts it.
27
+ - **`traceCoverage` counts states an `always` transition passes through as visited.** It re-derives each step's microsteps with XState's `getMicrosteps`; a step it cannot recompute adds none.
28
+ - **`NormalizedEventSchemas` is exported,** so a module that exports a machine factory built on `createAgentSchemas` events emits declarations without TS2742/TS2883.
29
+ - **The OpenAI text tool loop no longer throws or returns `""` on a bad tool call.** A call to a tool the request did not offer, or arguments that are not JSON, goes back to the model as an error result, as a thrown tool does.
30
+
31
+ - [#186](https://github.com/statelyai/agent/pull/186) [`d0d52ad`](https://github.com/statelyai/agent/commit/d0d52ad5bc4be997921f765ee28fbf9e61ea08ed) Thanks [@davidkpiano](https://github.com/davidkpiano)! - **Verified equivalence for ports.** `@statelyai/agent/testing` adds `createRecorder` and `compareRecordings`. Record a run of the code you are replacing by wrapping its AI SDK model (`recorder.model`) or `openai` client (`recorder.openai`) and its tools (`recorder.tool`, `recorder.tools`). Then replay the recording against the machine: `createRecorder({ replay })` answers every model and tool call from the recording, with no API key and no side effects. `compareRecordings` reports the first effect that differs: a model call (count and shape), a tool call or its arguments, or the output. The `loop-to-machine` skill records golden runs and proves equivalence with them.
32
+
33
+ - `createOpenAiExecutors` accepts `api: "responses"` to send tool-free text calls and streams to the OpenAI Responses API (`client.responses.create`) too, so a port of Responses-based code makes the same calls and replays its recordings. `compareRecordings` lists every divergence (`divergences`), aligned so one extra call does not shift the rest. A `modelCalls` function allows a model call's shape to change on purpose, per call, in replay and in comparison. A tool the original ran synchronously replays synchronously. Only concurrent tool calls may finish in either order; sequential ones must keep theirs. A model call that fails on one side only, or changes method, diverges. `createRecorder({ replay, normalize })` matches tool calls through `normalize`, so a generated id still replays. A wrapped `openai` client keeps `create(...).withResponse()`.
34
+
35
+ ## 2.0.0-alpha.33
36
+
37
+ ### Minor Changes
38
+
39
+ - [#181](https://github.com/statelyai/agent/pull/181) [`407e2a6`](https://github.com/statelyai/agent/commit/407e2a6c03e463d4b8d92997cf59e72b30301511) Thanks [@davidkpiano](https://github.com/davidkpiano)! - **Four eval helpers in `@statelyai/agent/testing`.**
40
+
41
+ - `traceCoverage(machine, traces, { reachable? })` reports which states and handlers no trace exercised. Coverage is per handler: a state and an event it handles, with the targets it declares and the ones runs reached. `always` handlers are listed as unobservable, because traces record one step per macrostep.
42
+ - `repeatRuns({ scenarios, k, run, passed })` runs each scenario `k` times and reports passes, pass@k, pass^k, distinct paths, and the state where the runs diverged. `report.passHat(j)` estimates the suite's pass^j. `summarizeRepeats` builds the same report from runs collected elsewhere.
43
+ - `judgeAgreement(rows, { positive?, groupBy? })` compares a judge's labels with confirmed human labels: accuracy, per-label recall, the confusion matrix, disagreements, the true positive and true negative rates, and a correction for the judge's bias.
44
+ - `firstDivergence(actual, reference)` finds the first step where a run departs from a reference trace or an expected trajectory, and names the state it was in.
45
+ - `foldTraces` takes a `blame` option and adds a `blamed` column: failed runs counted once, against the state their failure started in. Rows now always include `blamed`, which is 0 without `blame`.
46
+
3
47
  ## 2.0.0-alpha.32
4
48
 
5
49
  ### Patch Changes
package/dist/ai-sdk.cjs CHANGED
@@ -1,7 +1,8 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
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");
2
+ const require_event_log = require("./event-log-BWSSLUff.cjs");
3
+ const require_decision = require("./decision-BEsATcr1.cjs");
4
+ const require_model_source = require("./model-source-DmhTMK39.cjs");
5
+ const require_with_model_calls = require("./with-model-calls-BXYdBdcu.cjs");
5
6
  let ai = require("ai");
6
7
  //#region src/ai-sdk/mappers.ts
7
8
  /**
@@ -204,23 +205,24 @@ function toAiSdkOptionTool(options) {
204
205
  }
205
206
  /**
206
207
  * The event a closed-choice tool call names: the offered option whose id is
207
- * `input.option`. A call to another tool, or an id that is not offered, maps
208
- * to {@link unofferedOption}, so the choice is retried instead of thrown.
208
+ * `input.option`. A call to another tool, or an id that is not offered, is
209
+ * rejected with {@link unofferedOption}, so the choice is retried instead of thrown.
209
210
  */
210
211
  function fromAiSdkOptionCall(toolCall, options, ids) {
211
- if (toolCall.toolName !== OPTION_TOOL_NAME) return unofferedOption(`tool '${toolCall.toolName}'`, ids);
212
+ if (toolCall.toolName !== OPTION_TOOL_NAME) return { rejected: unofferedOption(`tool '${toolCall.toolName}'`, ids) };
212
213
  const id = require_event_log.isRecord(toolCall.input) ? toolCall.input.option : void 0;
213
214
  const option = typeof id === "string" ? options[ids.indexOf(id)] : void 0;
214
- return option ? option.event : unofferedOption(`option ${JSON.stringify(id)}`, ids);
215
+ return option ? { event: option.event } : { rejected: unofferedOption(`option ${JSON.stringify(id)}`, ids) };
215
216
  }
216
217
  /**
217
- * A stand-in event for a closed-choice answer that names no offered option.
218
- * Its `type` is no candidate event, so `resolveDecision` records it as a
219
- * failed `'unknown-event'` attempt (the type, which names the answer and the
220
- * valid ids, is the attempt's reason) and asks again.
218
+ * The rejection for a closed-choice answer that names no offered option, so
219
+ * `resolveDecision` records a failed `'unknown-event'` attempt and asks again.
221
220
  */
222
221
  function unofferedOption(answer, ids) {
223
- return { type: `${answer} was not offered (valid ids: ${ids.join(", ") || "none"})` };
222
+ return {
223
+ failure: "unknown-event",
224
+ reason: `${answer} was not offered; valid ids: ${ids.join(", ") || "none"}`
225
+ };
224
226
  }
225
227
  /**
226
228
  * Messages for a decision request, with prior failed `attempts` (§2.6)
@@ -332,71 +334,98 @@ function createAiSdkExecutors(source) {
332
334
  const options = require_model_source.resolveModelSource("createAiSdkExecutors", source);
333
335
  const generateText = async (request, info) => {
334
336
  const { model, settings: modelSettings } = resolveLanguageModel(options, request);
337
+ const settings = {
338
+ ...callSettings(options, request),
339
+ ...defined(modelSettings ?? {})
340
+ };
341
+ let calls = 0;
335
342
  const common = {
336
343
  model,
337
344
  abortSignal: info?.signal,
338
- ...callSettings(options, request),
339
- ...defined(modelSettings ?? {}),
345
+ ...settings,
346
+ onLanguageModelCallStart: (event) => {
347
+ calls++;
348
+ return settings.onLanguageModelCallStart?.(event);
349
+ },
340
350
  ...toAiSdkCallSettings(request),
341
351
  ...maxStepsSetting(request)
342
352
  };
343
- if (isStructuredOutputRequest(request)) {
344
- const outputSchema = require_event_log.providerOutputSchema(request.outputSchema, { reasoning: request.includeReasoning });
345
- const structuredOutput = withJsonRepair(ai.Output.object({ schema: outputSchema }));
346
- const result = await (0, ai.generateText)({
347
- ...common,
348
- output: structuredOutput
349
- }).catch((error) => {
350
- throw ai.NoObjectGeneratedError.isInstance(error) && error.finishReason === "length" ? truncated(request, info, error.text, error) : error;
351
- });
352
- const parsed = (() => {
353
- try {
354
- return result.output;
355
- } catch (error) {
356
- throw result.finishReason === "length" ? truncated(request, info, result.text, error) : error;
357
- }
358
- })();
359
- if (result.finishReason === "length") throw truncated(request, info, parsed.result);
353
+ try {
354
+ if (isStructuredOutputRequest(request)) {
355
+ const outputSchema = require_event_log.providerOutputSchema(request.outputSchema, { reasoning: request.includeReasoning });
356
+ const structuredOutput = withJsonRepair(ai.Output.object({ schema: outputSchema }));
357
+ const result = await (0, ai.generateText)({
358
+ ...common,
359
+ output: structuredOutput
360
+ }).catch((error) => {
361
+ throw ai.NoObjectGeneratedError.isInstance(error) && error.finishReason === "length" ? truncated(request, info, error.text, error) : error;
362
+ });
363
+ const parsed = (() => {
364
+ try {
365
+ return result.output;
366
+ } catch (error) {
367
+ throw result.finishReason === "length" ? truncated(request, info, result.text, error) : error;
368
+ }
369
+ })();
370
+ if (result.finishReason === "length") throw truncated(request, info, parsed.result);
371
+ return {
372
+ result: parsed.result,
373
+ ...parsed.reasoning !== void 0 ? { reasoning: parsed.reasoning } : {},
374
+ usage: toAgentCallUsage(result.usage),
375
+ finishReason: toAgentFinishReason(result.finishReason),
376
+ modelCalls: result.steps.length,
377
+ toolCalls: result.toolCalls,
378
+ toolResults: result.toolResults,
379
+ messages: result.responseMessages,
380
+ raw: result
381
+ };
382
+ }
383
+ const result = await (0, ai.generateText)(common);
360
384
  return {
361
- result: parsed.result,
362
- ...parsed.reasoning !== void 0 ? { reasoning: parsed.reasoning } : {},
385
+ result: result.text,
363
386
  usage: toAgentCallUsage(result.usage),
364
387
  finishReason: toAgentFinishReason(result.finishReason),
388
+ modelCalls: result.steps.length,
365
389
  toolCalls: result.toolCalls,
366
390
  toolResults: result.toolResults,
367
391
  messages: result.responseMessages,
368
392
  raw: result
369
393
  };
394
+ } catch (error) {
395
+ throw require_with_model_calls.withModelCalls(error, calls);
370
396
  }
371
- const result = await (0, ai.generateText)(common);
372
- return {
373
- result: result.text,
374
- usage: toAgentCallUsage(result.usage),
375
- finishReason: toAgentFinishReason(result.finishReason),
376
- toolCalls: result.toolCalls,
377
- toolResults: result.toolResults,
378
- messages: result.responseMessages,
379
- raw: result
380
- };
381
397
  };
382
398
  const streamText = async (request, info) => {
383
399
  const { model, settings: modelSettings } = resolveLanguageModel(options, request);
400
+ const settings = {
401
+ ...callSettings(options, request),
402
+ ...defined(modelSettings ?? {})
403
+ };
404
+ let calls = 0;
384
405
  const result = (0, ai.streamText)({
385
406
  model,
386
407
  abortSignal: info?.signal,
387
- ...callSettings(options, request),
388
- ...defined(modelSettings ?? {}),
408
+ ...settings,
409
+ onLanguageModelCallStart: (event) => {
410
+ calls++;
411
+ return settings.onLanguageModelCallStart?.(event);
412
+ },
389
413
  ...toAiSdkCallSettings(request),
390
414
  ...maxStepsSetting(request)
391
415
  });
392
- for await (const chunk of result.textStream) info?.onChunk?.(chunk);
393
- return {
394
- result: await result.text,
395
- usage: toAgentCallUsage(await result.usage),
396
- finishReason: toAgentFinishReason(await result.finishReason),
397
- messages: await result.responseMessages,
398
- raw: result
399
- };
416
+ try {
417
+ for await (const chunk of result.textStream) info?.onChunk?.(chunk);
418
+ return {
419
+ result: await result.text,
420
+ usage: toAgentCallUsage(await result.usage),
421
+ finishReason: toAgentFinishReason(await result.finishReason),
422
+ modelCalls: (await result.steps).length,
423
+ messages: await result.responseMessages,
424
+ raw: result
425
+ };
426
+ } catch (error) {
427
+ throw require_with_model_calls.withModelCalls(error, calls);
428
+ }
400
429
  };
401
430
  const decide = async (request, info) => {
402
431
  const { model, settings: modelSettings } = resolveAiSdkModel(options, request.model);
@@ -404,11 +433,25 @@ function createAiSdkExecutors(source) {
404
433
  const choice = request.options ? toAiSdkOptionTool(request.options) : void 0;
405
434
  const tools = choice ? choice.tools : toAiSdkEventTools(request.events);
406
435
  const messages = toDecisionMessages(request);
436
+ const settings = {
437
+ ...callSettings(options, request),
438
+ ...defined(modelSettings ?? {})
439
+ };
440
+ const toolNames = Object.keys(tools);
441
+ let callEnd;
442
+ const unnamed = (ended, call) => ({
443
+ rejected: require_with_model_calls.rejectedAnswer(call, toolNames),
444
+ usage: toAgentCallUsage(ended.usage),
445
+ finishReason: ended.finishReason
446
+ });
407
447
  const result = await (0, ai.generateText)({
408
448
  model,
409
449
  abortSignal: info?.signal,
410
- ...callSettings(options, request),
411
- ...defined(modelSettings ?? {}),
450
+ ...settings,
451
+ onLanguageModelCallEnd: (event) => {
452
+ callEnd = event;
453
+ return settings.onLanguageModelCallEnd?.(event);
454
+ },
412
455
  system: request.system,
413
456
  ...messages ? {
414
457
  messages,
@@ -418,16 +461,24 @@ function createAiSdkExecutors(source) {
418
461
  toolChoice: "required",
419
462
  stopWhen: (0, ai.stepCountIs)(1),
420
463
  ...toAiSdkGenerationSettings(request)
464
+ }).catch((error) => {
465
+ if (ai.ToolChoiceViolationError.isInstance(error) && callEnd) return void 0;
466
+ throw error;
421
467
  });
468
+ if (!result) return unnamed(callEnd);
422
469
  const toolCall = result.toolCalls[0];
423
- if (!toolCall) throw new Error("createAiSdkExecutors: decide — model did not call an event tool.");
470
+ if (!toolCall) return unnamed(result);
471
+ if (toolCall.invalid && typeof toolCall.input === "string" && toolNames.includes(toolCall.toolName)) return unnamed(result, {
472
+ toolName: toolCall.toolName,
473
+ invalidJson: true
474
+ });
424
475
  if (choice) return {
425
- event: fromAiSdkOptionCall(toolCall, request.options, choice.ids),
476
+ ...fromAiSdkOptionCall(toolCall, request.options, choice.ids),
426
477
  usage: toAgentCallUsage(result.usage),
427
478
  finishReason: result.finishReason
428
479
  };
429
480
  const chosenEvent = request.events.find((event) => event.toolName === toolCall.toolName);
430
- if (!chosenEvent) throw new Error(`createAiSdkExecutors: decide — model called unknown tool '${toolCall.toolName}'.`);
481
+ if (!chosenEvent) return unnamed(result, toolCall);
431
482
  return {
432
483
  event: {
433
484
  ...toolCall.input && typeof toolCall.input === "object" ? toolCall.input : {},
@@ -467,18 +518,18 @@ async function decideWithDecisionModel(model, request, info) {
467
518
  } catch (error) {
468
519
  const chosen = unofferedChoice(error, ids);
469
520
  if (chosen === void 0) throw error;
470
- return decisionResult(unofferedOption(`option ${JSON.stringify(chosen)}`, ids));
521
+ return decisionResult({ rejected: unofferedOption(`option ${JSON.stringify(chosen)}`, ids) });
471
522
  }
472
523
  const answer = result.answers[DECISION_QUESTION_ID];
473
524
  const option = offered[ids.indexOf(answer.choice)];
474
- if (!option) return decisionResult(unofferedOption(`option ${JSON.stringify(answer.choice)}`, ids));
525
+ if (!option) return decisionResult({ rejected: unofferedOption(`option ${JSON.stringify(answer.choice)}`, ids) });
475
526
  const reported = (result.providerMetadata?.typesafe?.confidence)?.[DECISION_QUESTION_ID];
476
527
  const confidence = typeof reported === "number" ? reported : answer.probabilities?.[answer.choice];
477
- return decisionResult(option.event, result.usage, confidence);
528
+ return decisionResult({ event: option.event }, result.usage, confidence);
478
529
  }
479
- function decisionResult(event, usage = {}, confidence) {
530
+ function decisionResult(answer, usage = {}, confidence) {
480
531
  return {
481
- event,
532
+ ...answer,
482
533
  usage: toAgentCallUsage({
483
534
  inputTokens: usage.inputTokens,
484
535
  outputTokens: usage.outputTokens,
package/dist/ai-sdk.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { s as AgentTools, u as ChosenEvent } from "./types-Dp5_Ru4R.cjs";
2
- import { N as AgentDecisionRequest, c as AgentRequestExecutorInfo, f as AgentTextRequest, r as AgentFinishReason, t as AgentCallUsage } from "./text-logic-CYP502VT.cjs";
2
+ import { N as AgentDecisionRejection, P as AgentDecisionRequest, c as AgentRequestExecutorInfo, f as AgentTextRequest, r as AgentFinishReason, t as AgentCallUsage } from "./text-logic-CV_4KGr6.cjs";
3
3
  import { Experimental_DecisionModel, FinishReason, LanguageModel, LanguageModelUsage, ModelMessage, ToolSet, TypedToolCall, TypedToolResult, generateText } from "ai";
4
4
  import { AnyStateMachine } from "xstate";
5
5
  //#region src/ai-sdk/mappers.d.ts
@@ -104,6 +104,8 @@ export type AiSdkGenerateResult = {
104
104
  usage: AiSdkCallUsage;
105
105
  /** Why the call stopped, normalized to the portable {@link AgentFinishReason}. The SDK's own value stays on `raw`. */
106
106
  finishReason: AgentFinishReason;
107
+ /** Model calls the SDK made: one per tool-loop step. */
108
+ modelCalls: number;
107
109
  toolCalls: TypedToolCall<ToolSet>[];
108
110
  toolResults: TypedToolResult<ToolSet>[];
109
111
  /** AI SDK response messages, including tool calls/results from every step. */
@@ -118,14 +120,21 @@ export type AiSdkStreamResult = {
118
120
  usage: AiSdkCallUsage;
119
121
  /** Why the stream stopped, normalized to the portable {@link AgentFinishReason}. The SDK's own value stays on `raw`. */
120
122
  finishReason: AgentFinishReason;
123
+ /** Model calls the SDK made: one per tool-loop step. */
124
+ modelCalls: number;
121
125
  /** AI SDK response messages, including tool calls/results from every step. */
122
126
  messages: ModelMessage[];
123
127
  /** The untouched AI SDK result. */
124
128
  raw: unknown;
125
129
  };
126
130
  /** Raw result shape from {@link AiSdkExecutors.decide} — the chosen event plus the AI SDK call metadata, delivered per decision attempt to `onResult`. */
127
- export type AiSdkDecideResult = {
131
+ export type AiSdkDecideResult = ({
128
132
  event: ChosenEvent;
133
+ rejected?: undefined;
134
+ } | {
135
+ event?: undefined;
136
+ rejected: AgentDecisionRejection;
137
+ }) & {
129
138
  /** This attempt's usage; aggregated into the run result's `AgentUsage`. */
130
139
  usage: AiSdkCallUsage;
131
140
  /** `'stop'` for a decision model, which reports no finish reason. */
package/dist/ai-sdk.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { s as AgentTools, u as ChosenEvent } from "./types-Dp5_Ru4R.mjs";
2
- import { N as AgentDecisionRequest, c as AgentRequestExecutorInfo, f as AgentTextRequest, r as AgentFinishReason, t as AgentCallUsage } from "./text-logic-DuK9HIFM.mjs";
2
+ import { N as AgentDecisionRejection, P as AgentDecisionRequest, c as AgentRequestExecutorInfo, f as AgentTextRequest, r as AgentFinishReason, t as AgentCallUsage } from "./text-logic-Ckh-rJFV.mjs";
3
3
  import { AnyStateMachine } from "xstate";
4
4
  import { Experimental_DecisionModel, FinishReason, LanguageModel, LanguageModelUsage, ModelMessage, ToolSet, TypedToolCall, TypedToolResult, generateText } from "ai";
5
5
  //#region src/ai-sdk/mappers.d.ts
@@ -104,6 +104,8 @@ export type AiSdkGenerateResult = {
104
104
  usage: AiSdkCallUsage;
105
105
  /** Why the call stopped, normalized to the portable {@link AgentFinishReason}. The SDK's own value stays on `raw`. */
106
106
  finishReason: AgentFinishReason;
107
+ /** Model calls the SDK made: one per tool-loop step. */
108
+ modelCalls: number;
107
109
  toolCalls: TypedToolCall<ToolSet>[];
108
110
  toolResults: TypedToolResult<ToolSet>[];
109
111
  /** AI SDK response messages, including tool calls/results from every step. */
@@ -118,14 +120,21 @@ export type AiSdkStreamResult = {
118
120
  usage: AiSdkCallUsage;
119
121
  /** Why the stream stopped, normalized to the portable {@link AgentFinishReason}. The SDK's own value stays on `raw`. */
120
122
  finishReason: AgentFinishReason;
123
+ /** Model calls the SDK made: one per tool-loop step. */
124
+ modelCalls: number;
121
125
  /** AI SDK response messages, including tool calls/results from every step. */
122
126
  messages: ModelMessage[];
123
127
  /** The untouched AI SDK result. */
124
128
  raw: unknown;
125
129
  };
126
130
  /** Raw result shape from {@link AiSdkExecutors.decide} — the chosen event plus the AI SDK call metadata, delivered per decision attempt to `onResult`. */
127
- export type AiSdkDecideResult = {
131
+ export type AiSdkDecideResult = ({
128
132
  event: ChosenEvent;
133
+ rejected?: undefined;
134
+ } | {
135
+ event?: undefined;
136
+ rejected: AgentDecisionRejection;
137
+ }) & {
129
138
  /** This attempt's usage; aggregated into the run result's `AgentUsage`. */
130
139
  usage: AiSdkCallUsage;
131
140
  /** `'stop'` for a decision model, which reports no finish reason. */