@statelyai/agent 2.0.0-alpha.33 → 2.0.0-alpha.35
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 +45 -0
- package/dist/ai-sdk.cjs +114 -63
- package/dist/ai-sdk.d.cts +11 -2
- package/dist/ai-sdk.d.mts +11 -2
- package/dist/ai-sdk.mjs +115 -64
- package/dist/{decision-BPdEaxPf.cjs → decision-BEsATcr1.cjs} +18 -2
- package/dist/{decision-CV7WiV5C.mjs → decision-COkJNjOW.mjs} +18 -2
- package/dist/{event-log-CKWeC7H-.cjs → event-log-BWSSLUff.cjs} +139 -10
- package/dist/{event-log-DAqsalT1.mjs → event-log-C7jeH0y8.mjs} +123 -12
- package/dist/{event-log-store-C2NjVlWU.d.mts → event-log-store-BzXehP7R.d.cts} +28 -3
- package/dist/{event-log-store-s73r1Hxf.d.cts → event-log-store-CWjN6lmM.d.mts} +28 -3
- package/dist/{event-log-store-DKyE7JKr.cjs → event-log-store-CleWpsp5.cjs} +1 -1
- package/dist/{event-log-store-BGehvVTb.mjs → event-log-store-W49TCJXi.mjs} +1 -1
- package/dist/index.cjs +5 -5
- package/dist/index.d.cts +7 -7
- package/dist/index.d.mts +7 -7
- package/dist/index.mjs +5 -5
- package/dist/log.cjs +2 -2
- package/dist/log.d.cts +1 -1
- package/dist/log.d.mts +1 -1
- package/dist/log.mjs +2 -2
- package/dist/machines.cjs +1 -1
- package/dist/machines.mjs +1 -1
- package/dist/{model-source-BilDEyqk.mjs → model-source-CK3F4YuU.mjs} +1 -1
- package/dist/{model-source-BQ97_l1-.cjs → model-source-DmhTMK39.cjs} +1 -1
- package/dist/openai.cjs +377 -95
- package/dist/openai.d.cts +55 -20
- package/dist/openai.d.mts +55 -20
- package/dist/openai.mjs +375 -96
- package/dist/otel.d.cts +1 -1
- package/dist/otel.d.mts +1 -1
- package/dist/{run-agent-B5QhabdY.d.cts → run-agent-B1-Eqviq.d.cts} +13 -5
- package/dist/{run-agent-Atu-wEk7.d.mts → run-agent-BGdx5913.d.mts} +13 -5
- package/dist/{run-agent-2UnSUhDb.mjs → run-agent-Ct_GZw_f.mjs} +124 -38
- package/dist/{run-agent-Cg0xd3K-.cjs → run-agent-s5D32w99.cjs} +123 -37
- package/dist/{setup-agent-CmhB-U5P.mjs → setup-agent-05uw4dPZ.mjs} +2 -2
- package/dist/{setup-agent-BiF1N3td.d.cts → setup-agent-BtfeFfF6.d.cts} +2 -2
- package/dist/{setup-agent-J9f_Wu5T.d.mts → setup-agent-C6nqzv_X.d.mts} +2 -2
- package/dist/{setup-agent-CwUJyqKB.cjs → setup-agent-CXEBURdo.cjs} +2 -2
- package/dist/tanstack-ai.cjs +91 -59
- package/dist/tanstack-ai.d.cts +13 -2
- package/dist/tanstack-ai.d.mts +13 -2
- package/dist/tanstack-ai.mjs +91 -59
- package/dist/testing.cjs +629 -13
- package/dist/testing.d.cts +278 -6
- package/dist/testing.d.mts +278 -6
- package/dist/testing.mjs +626 -14
- package/dist/{text-logic-CYP502VT.d.cts → text-logic-CV_4KGr6.d.cts} +28 -3
- package/dist/{text-logic-DuK9HIFM.d.mts → text-logic-Ckh-rJFV.d.mts} +28 -3
- package/dist/{usage-D7JKAxLc.d.mts → usage-BDiJKyv9.d.cts} +11 -1
- package/dist/{usage-DVw-646x.d.cts → usage-DOoVUGVq.d.mts} +11 -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-Dk3Vwnha.d.cts → verify-BQpE8hUz.d.cts} +1 -1
- package/dist/{verify-DgB8P-Wx.mjs → verify-Bpahmr34.mjs} +2 -2
- package/dist/{verify-BVz4cURF.d.mts → verify-CqeuEmIk.d.mts} +1 -1
- package/dist/{verify-CS_WLRXn.cjs → verify-DCLZa9R0.cjs} +2 -2
- package/dist/with-model-calls-BXYdBdcu.cjs +45 -0
- package/dist/with-model-calls-Bv4V4EHC.mjs +34 -0
- package/package.json +5 -5
- package/readme.md +14 -13
- package/skills/loop-to-machine/SKILL.md +42 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,50 @@
|
|
|
1
1
|
# @statelyai/agent
|
|
2
2
|
|
|
3
|
+
## 2.0.0-alpha.35
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#191](https://github.com/statelyai/agent/pull/191) [`8dcb0c8`](https://github.com/statelyai/agent/commit/8dcb0c8643b1bd38e53538999c3d27c58c4b096f) Thanks [@davidkpiano](https://github.com/davidkpiano)! - **Replay ports that change on purpose.** `createRecorder({ replay })` now answers each model call with the earliest unused recorded call of the same method and shape, and each tool call with the earliest unused recorded call of the same name and input, instead of by position. A call the port added or dropped no longer shifts the answers after it, unless another call has the same shape (or tool name and input): those are answered in recorded order.
|
|
8
|
+
|
|
9
|
+
- `translate(recorded, actual)` adapts a recorded answer to a call you changed on purpose (a stream that became `doGenerate`, text that became a decision). `compareRecordings` counts the pair as the same call.
|
|
10
|
+
- `fallback: { model, openai, tools }` answers what the recording cannot: a stand-in model or client answers the call, and the wrapped tool runs. Those effects are marked `unrecorded` and still count as extra.
|
|
11
|
+
- `compareRecordings(a, b, { kinds: ["tool", "output"] })` compares only what a port did, for a port that calls the model another way.
|
|
12
|
+
- `normalize(effect, index, effects)` can return `undefined` to leave an effect out of the comparison, such as a repeated idempotent write. Replay still answers from it.
|
|
13
|
+
- `recorder.model` wraps a decision model's `doDecide` (and `doEvaluate`). Its shape is the questions, their choice options, and their score levels.
|
|
14
|
+
- A recorded error keeps its number and boolean fields (`statusCode`, `isRetryable`) and its `Symbol.for` markers. In replay, `APICallError.isInstance` and retry behavior hold. Comparisons still check only an error's name, message, and code.
|
|
15
|
+
|
|
16
|
+
## 2.0.0-alpha.34
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- [#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.
|
|
21
|
+
|
|
22
|
+
- [#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.
|
|
23
|
+
|
|
24
|
+
- [#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.
|
|
25
|
+
|
|
26
|
+
- [#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.**
|
|
27
|
+
|
|
28
|
+
- **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.
|
|
29
|
+
- **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`.
|
|
30
|
+
- **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.
|
|
31
|
+
- **The machine can read its budget.** `@agent.usage` carries `budget: { modelCalls, maxModelCalls }`.
|
|
32
|
+
- **`modelCallsSpent`** counts earlier turns of a conversation against `maxModelCalls` without adding them to this run's `usage`. `runAgent` with `onIdle` uses it between passes.
|
|
33
|
+
- **`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.
|
|
34
|
+
- **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.
|
|
35
|
+
- **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`.
|
|
36
|
+
|
|
37
|
+
- [#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.**
|
|
38
|
+
|
|
39
|
+
- **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.
|
|
40
|
+
- **`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.
|
|
41
|
+
- **`NormalizedEventSchemas` is exported,** so a module that exports a machine factory built on `createAgentSchemas` events emits declarations without TS2742/TS2883.
|
|
42
|
+
- **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.
|
|
43
|
+
|
|
44
|
+
- [#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.
|
|
45
|
+
|
|
46
|
+
- `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()`.
|
|
47
|
+
|
|
3
48
|
## 2.0.0-alpha.33
|
|
4
49
|
|
|
5
50
|
### Minor 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-
|
|
3
|
-
const require_decision = require("./decision-
|
|
4
|
-
const require_model_source = require("./model-source-
|
|
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,
|
|
208
|
-
*
|
|
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
|
-
*
|
|
218
|
-
*
|
|
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 {
|
|
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
|
-
...
|
|
339
|
-
|
|
345
|
+
...settings,
|
|
346
|
+
onLanguageModelCallStart: (event) => {
|
|
347
|
+
calls++;
|
|
348
|
+
return settings.onLanguageModelCallStart?.(event);
|
|
349
|
+
},
|
|
340
350
|
...toAiSdkCallSettings(request),
|
|
341
351
|
...maxStepsSetting(request)
|
|
342
352
|
};
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
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:
|
|
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
|
-
...
|
|
388
|
-
|
|
408
|
+
...settings,
|
|
409
|
+
onLanguageModelCallStart: (event) => {
|
|
410
|
+
calls++;
|
|
411
|
+
return settings.onLanguageModelCallStart?.(event);
|
|
412
|
+
},
|
|
389
413
|
...toAiSdkCallSettings(request),
|
|
390
414
|
...maxStepsSetting(request)
|
|
391
415
|
});
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
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
|
-
...
|
|
411
|
-
|
|
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)
|
|
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
|
-
|
|
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)
|
|
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(
|
|
530
|
+
function decisionResult(answer, usage = {}, confidence) {
|
|
480
531
|
return {
|
|
481
|
-
|
|
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-
|
|
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-
|
|
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. */
|