@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.
- package/CHANGELOG.md +44 -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 +1026 -10
- package/dist/testing.d.cts +585 -5
- package/dist/testing.d.mts +585 -5
- package/dist/testing.mjs +1018 -11
- 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-CoCvR1eM.mjs → verify-Bpahmr34.mjs} +14 -4
- package/dist/{verify-BVz4cURF.d.mts → verify-CqeuEmIk.d.mts} +1 -1
- package/dist/{verify-D_llRCTr.cjs → verify-DCLZa9R0.cjs} +19 -3
- 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/add-evals/SKILL.md +8 -2
- package/skills/improve-machine/SKILL.md +1 -1
- package/skills/loop-to-machine/SKILL.md +40 -2
package/dist/openai.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { c as renderDecisionAttempts } from "./decision-
|
|
1
|
+
import { B as providerOutputSchema, M as getAgentOutputMode, Q as getJsonSchemaSync, T as AGENT_USAGE_TOKEN_FIELDS, Z as getJsonSchema, mt as AgentTruncatedError, rt as isStandardSchema, z as parseProviderOutput } from "./event-log-C7jeH0y8.mjs";
|
|
2
|
+
import { c as renderDecisionAttempts } from "./decision-COkJNjOW.mjs";
|
|
3
|
+
import { n as rejectedAnswer, t as withModelCalls } from "./with-model-calls-Bv4V4EHC.mjs";
|
|
3
4
|
//#region src/openai/index.ts
|
|
4
5
|
/** Thrown for a message part Chat Completions cannot carry — dropping it would
|
|
5
6
|
* silently change what the model sees. */
|
|
@@ -135,24 +136,25 @@ function staticDescription(descriptor) {
|
|
|
135
136
|
if (typeof descriptor === "function") return;
|
|
136
137
|
return typeof descriptor.description === "string" ? descriptor.description : void 0;
|
|
137
138
|
}
|
|
138
|
-
/** One
|
|
139
|
+
/** One Responses API function tool per `AgentTools` entry. `strict` is sent as
|
|
140
|
+
* `false` because the Responses API defaults it to `true`, and arbitrary JSON
|
|
141
|
+
* Schema (e.g. from Zod) may fall outside OpenAI's strict-mode subset. */
|
|
139
142
|
function toOpenAiTools(tools) {
|
|
140
143
|
return Object.entries(tools).flatMap(([name, descriptor]) => {
|
|
141
144
|
if (!descriptor) return [];
|
|
142
145
|
const inputSchema = typeof descriptor === "function" ? void 0 : descriptor.inputSchema;
|
|
143
146
|
return [{
|
|
144
147
|
type: "function",
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
148
|
+
name,
|
|
149
|
+
description: staticDescription(descriptor),
|
|
150
|
+
parameters: (isStandardSchema(inputSchema) ? getJsonSchemaSync(inputSchema) : void 0) ?? {},
|
|
151
|
+
strict: false
|
|
150
152
|
}];
|
|
151
153
|
});
|
|
152
154
|
}
|
|
153
155
|
/**
|
|
154
|
-
* Maps an {@link AgentToolChoice} onto
|
|
155
|
-
* `{ type: 'tool', name }` becomes `{ type: 'function',
|
|
156
|
+
* Maps an {@link AgentToolChoice} onto the Responses API's `tool_choice` —
|
|
157
|
+
* `{ type: 'tool', name }` becomes `{ type: 'function', name }`;
|
|
156
158
|
* `'auto'`/`'none'`/`'required'` are OpenAI's own vocabulary and pass through.
|
|
157
159
|
* `undefined` stays `undefined`, so the key is omitted.
|
|
158
160
|
*/
|
|
@@ -160,21 +162,80 @@ function toOpenAiToolChoice(toolChoice) {
|
|
|
160
162
|
if (toolChoice === void 0) return;
|
|
161
163
|
return typeof toolChoice === "object" ? {
|
|
162
164
|
type: "function",
|
|
163
|
-
|
|
165
|
+
name: toolChoice.name
|
|
164
166
|
} : toolChoice;
|
|
165
167
|
}
|
|
166
|
-
/** One
|
|
167
|
-
* + tool_choice: 'required'" recipe, mirroring `toAiSdkEventTools`. */
|
|
168
|
+
/** One Responses API function tool per candidate decision event — the
|
|
169
|
+
* "tool-per-event + tool_choice: 'required'" recipe, mirroring `toAiSdkEventTools`. */
|
|
168
170
|
function toOpenAiEventTools(events) {
|
|
169
171
|
return events.map((event) => ({
|
|
170
172
|
type: "function",
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
173
|
+
name: event.toolName,
|
|
174
|
+
description: `Choose the '${event.type}' move.`,
|
|
175
|
+
parameters: getJsonSchemaSync(event.inputSchema) ?? {},
|
|
176
|
+
strict: false
|
|
176
177
|
}));
|
|
177
178
|
}
|
|
179
|
+
/** A Chat Completions content part as a Responses input part. `toOpenAiMessages`
|
|
180
|
+
* only emits text and image parts. */
|
|
181
|
+
function toResponsesContentPart(part) {
|
|
182
|
+
switch (part.type) {
|
|
183
|
+
case "text": return {
|
|
184
|
+
type: "input_text",
|
|
185
|
+
text: part.text
|
|
186
|
+
};
|
|
187
|
+
case "image_url": return {
|
|
188
|
+
type: "input_image",
|
|
189
|
+
image_url: part.image_url.url,
|
|
190
|
+
detail: part.image_url.detail ?? "auto"
|
|
191
|
+
};
|
|
192
|
+
default: return unsupportedPart("user", part.type);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function contentText(content) {
|
|
196
|
+
if (typeof content === "string" || content == null) return content ?? "";
|
|
197
|
+
return content.map((part) => part.text ?? "").join("");
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Maps the Chat Completions messages {@link toOpenAiMessages} builds onto
|
|
201
|
+
* Responses API input items: assistant `tool_calls` become `function_call`
|
|
202
|
+
* items and tool messages become `function_call_output` items. Reusing the
|
|
203
|
+
* Chat Completions mapping keeps one place that decides what a message part
|
|
204
|
+
* means.
|
|
205
|
+
*/
|
|
206
|
+
function toResponsesInput(messages) {
|
|
207
|
+
return messages.flatMap((message) => {
|
|
208
|
+
switch (message.role) {
|
|
209
|
+
case "system":
|
|
210
|
+
case "developer": return [{
|
|
211
|
+
role: message.role,
|
|
212
|
+
content: contentText(message.content)
|
|
213
|
+
}];
|
|
214
|
+
case "user": return [{
|
|
215
|
+
role: "user",
|
|
216
|
+
content: typeof message.content === "string" ? message.content : message.content.map(toResponsesContentPart)
|
|
217
|
+
}];
|
|
218
|
+
case "assistant": {
|
|
219
|
+
const text = contentText(message.content);
|
|
220
|
+
return [...text ? [{
|
|
221
|
+
role: "assistant",
|
|
222
|
+
content: text
|
|
223
|
+
}] : [], ...(message.tool_calls ?? []).flatMap((call) => call.type === "function" ? [{
|
|
224
|
+
type: "function_call",
|
|
225
|
+
call_id: call.id,
|
|
226
|
+
name: call.function.name,
|
|
227
|
+
arguments: call.function.arguments
|
|
228
|
+
}] : [])];
|
|
229
|
+
}
|
|
230
|
+
case "tool": return [{
|
|
231
|
+
type: "function_call_output",
|
|
232
|
+
call_id: message.tool_call_id,
|
|
233
|
+
output: contentText(message.content)
|
|
234
|
+
}];
|
|
235
|
+
default: return unsupportedPart(message.role, "message");
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
}
|
|
178
239
|
/**
|
|
179
240
|
* Messages for a decision request, with prior failed `attempts` rendered as an
|
|
180
241
|
* appended user message so retries converge. Mirrors the AI SDK adapter's
|
|
@@ -223,6 +284,113 @@ function toAgentFinishReason(finishReason) {
|
|
|
223
284
|
default: return "other";
|
|
224
285
|
}
|
|
225
286
|
}
|
|
287
|
+
function functionCalls(response) {
|
|
288
|
+
return (response.output ?? []).filter((item) => item.type === "function_call");
|
|
289
|
+
}
|
|
290
|
+
function outputText(response) {
|
|
291
|
+
let text = "";
|
|
292
|
+
for (const item of response.output ?? []) if (item.type === "message") {
|
|
293
|
+
for (const part of item.content) if (part.type === "output_text") text += part.text;
|
|
294
|
+
}
|
|
295
|
+
return text;
|
|
296
|
+
}
|
|
297
|
+
/** {@link toAgentCallUsage} for a Responses API usage payload, which names its
|
|
298
|
+
* fields `input_tokens`/`output_tokens`. */
|
|
299
|
+
function toAgentResponseUsage(usage) {
|
|
300
|
+
if (!usage) return;
|
|
301
|
+
const reasoningTokens = usage.output_tokens_details?.reasoning_tokens;
|
|
302
|
+
const cachedInputTokens = usage.input_tokens_details?.cached_tokens;
|
|
303
|
+
return {
|
|
304
|
+
...usage.input_tokens !== void 0 ? { inputTokens: usage.input_tokens } : {},
|
|
305
|
+
...usage.output_tokens !== void 0 ? { outputTokens: usage.output_tokens } : {},
|
|
306
|
+
...usage.total_tokens !== void 0 ? { totalTokens: usage.total_tokens } : {},
|
|
307
|
+
...reasoningTokens !== void 0 ? { reasoningTokens } : {},
|
|
308
|
+
...cachedInputTokens !== void 0 ? { cachedInputTokens } : {}
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* The portable {@link AgentFinishReason} of a Responses API response, which has
|
|
313
|
+
* no `finish_reason`: an `incomplete` status carries the reason, and a
|
|
314
|
+
* completed response that asked for function calls stopped for them.
|
|
315
|
+
*/
|
|
316
|
+
function toAgentResponseFinishReason(response) {
|
|
317
|
+
if (response.status === "incomplete") switch (response.incomplete_details?.reason) {
|
|
318
|
+
case "max_output_tokens": return "length";
|
|
319
|
+
case "content_filter": return "content-filter";
|
|
320
|
+
default: return "other";
|
|
321
|
+
}
|
|
322
|
+
if (functionCalls(response).length > 0) return "tool-calls";
|
|
323
|
+
return response.status === "completed" ? "stop" : "other";
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Throws for a `failed` Responses API response. The SDK returns one without
|
|
327
|
+
* throwing, and passing it on would record an empty result as a success.
|
|
328
|
+
*/
|
|
329
|
+
function assertNotFailed(response, executor) {
|
|
330
|
+
if (response.status === "failed") throw new Error(`createOpenAiExecutors: ${executor} — the Responses API call failed` + (response.error ? ` (${response.error.code}: ${response.error.message})` : "") + ".");
|
|
331
|
+
}
|
|
332
|
+
const REJECTABLE_SETTINGS = [
|
|
333
|
+
"temperature",
|
|
334
|
+
"top_p",
|
|
335
|
+
"top_logprobs",
|
|
336
|
+
"reasoning"
|
|
337
|
+
];
|
|
338
|
+
/** The top-level parameter an OpenAI `unsupported_parameter` 400 names, if any. */
|
|
339
|
+
function unsupportedParameter(error) {
|
|
340
|
+
const { status, param, message } = error ?? {};
|
|
341
|
+
if (status !== 400) return void 0;
|
|
342
|
+
return (typeof param === "string" ? param : /Unsupported parameter: '([\w.]+)'/.exec(String(message))?.[1])?.split(".")[0];
|
|
343
|
+
}
|
|
344
|
+
const RESPONSES_SHARED_SETTINGS = [
|
|
345
|
+
"metadata",
|
|
346
|
+
"moderation",
|
|
347
|
+
"parallel_tool_calls",
|
|
348
|
+
"prompt_cache_key",
|
|
349
|
+
"prompt_cache_options",
|
|
350
|
+
"prompt_cache_retention",
|
|
351
|
+
"safety_identifier",
|
|
352
|
+
"service_tier",
|
|
353
|
+
"store",
|
|
354
|
+
"temperature",
|
|
355
|
+
"top_logprobs",
|
|
356
|
+
"top_p",
|
|
357
|
+
"user"
|
|
358
|
+
];
|
|
359
|
+
/** Chat Completions settings `toResponsesSettings` renames onto the Responses API. */
|
|
360
|
+
const RESPONSES_TRANSLATED_SETTINGS = [
|
|
361
|
+
"max_completion_tokens",
|
|
362
|
+
"max_tokens",
|
|
363
|
+
"reasoning_effort",
|
|
364
|
+
"verbosity"
|
|
365
|
+
];
|
|
366
|
+
/**
|
|
367
|
+
* Translates Chat Completions settings onto the Responses API: the settings it
|
|
368
|
+
* shares by name pass through, `reasoning_effort` becomes `reasoning.effort`,
|
|
369
|
+
* `max_completion_tokens` (or `max_tokens`) becomes `max_output_tokens`, and
|
|
370
|
+
* `verbosity` becomes `text.verbosity`. Everything else is Chat-only (`seed`,
|
|
371
|
+
* `stop`, `logprobs`, `prediction`, …) and is dropped with a warning, the way
|
|
372
|
+
* the AI SDK's Responses provider drops what it cannot send, rather than
|
|
373
|
+
* failing the call. `warned` holds the keys already warned about, so a tool
|
|
374
|
+
* loop warns once per key, not once per step.
|
|
375
|
+
*/
|
|
376
|
+
function toResponsesSettings(settings, warned) {
|
|
377
|
+
const { reasoning_effort, max_completion_tokens, max_tokens, verbosity } = settings;
|
|
378
|
+
const entries = Object.entries(settings).filter(([, value]) => value !== void 0);
|
|
379
|
+
const shared = Object.fromEntries(entries.filter(([key]) => RESPONSES_SHARED_SETTINGS.includes(key)));
|
|
380
|
+
for (const [key] of entries) {
|
|
381
|
+
if (RESPONSES_SHARED_SETTINGS.includes(key) || RESPONSES_TRANSLATED_SETTINGS.includes(key)) continue;
|
|
382
|
+
if (warned.has(key)) continue;
|
|
383
|
+
warned.add(key);
|
|
384
|
+
console.warn(`@statelyai/agent/openai: the Responses API has no \`${key}\` parameter, so it was dropped from this call. A request's \`seed\` and \`stopSequences\` map to \`seed\` and \`stop\`.`);
|
|
385
|
+
}
|
|
386
|
+
const maxOutputTokens = max_completion_tokens ?? max_tokens;
|
|
387
|
+
return {
|
|
388
|
+
...shared,
|
|
389
|
+
...reasoning_effort != null ? { reasoning: { effort: reasoning_effort } } : {},
|
|
390
|
+
...maxOutputTokens != null ? { max_output_tokens: maxOutputTokens } : {},
|
|
391
|
+
...verbosity != null ? { text: { verbosity } } : {}
|
|
392
|
+
};
|
|
393
|
+
}
|
|
226
394
|
/** Resolves the host's `settings` option for one request. */
|
|
227
395
|
function callSettings(options, request) {
|
|
228
396
|
const settings = options.settings;
|
|
@@ -272,12 +440,15 @@ function toToolContent(output) {
|
|
|
272
440
|
}
|
|
273
441
|
/**
|
|
274
442
|
* Builds the `{ generateText, streamText, decide }` executor set for the raw
|
|
275
|
-
* `openai` package
|
|
276
|
-
*
|
|
443
|
+
* `openai` package. Compare `createAiSdkExecutors` — same shape, different SDK
|
|
444
|
+
* underneath.
|
|
277
445
|
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
*
|
|
446
|
+
* Calls with function tools use the Responses API, which reasoning models
|
|
447
|
+
* require for tools: decisions force a call with `tool_choice: 'required'`, one
|
|
448
|
+
* function tool per candidate event, and `generateText` runs its tool loop
|
|
449
|
+
* there. Tool-free calls use Chat Completions. Structured output wraps the
|
|
450
|
+
* `{ result, reasoning? }` provider output schema in a `json_schema` format on
|
|
451
|
+
* either API.
|
|
281
452
|
*
|
|
282
453
|
* @example
|
|
283
454
|
* ```ts
|
|
@@ -289,91 +460,186 @@ function toToolContent(output) {
|
|
|
289
460
|
* ```
|
|
290
461
|
*/
|
|
291
462
|
function createOpenAiExecutors(options) {
|
|
292
|
-
const { client, resolveModel = (modelRef) => modelRef } = options;
|
|
293
|
-
const
|
|
294
|
-
|
|
463
|
+
const { client, api = "chat", resolveModel = (modelRef) => modelRef } = options;
|
|
464
|
+
const warnedSettings = /* @__PURE__ */ new Set();
|
|
465
|
+
const rejectedSettings = /* @__PURE__ */ new Map();
|
|
466
|
+
const createResponse = async (params, signal) => {
|
|
467
|
+
const rejected = rejectedSettings.get(params.model) ?? /* @__PURE__ */ new Set();
|
|
468
|
+
const sent = Object.fromEntries(Object.entries(params).filter(([key]) => !rejected.has(key)));
|
|
469
|
+
try {
|
|
470
|
+
return await client.responses.create(sent, { signal });
|
|
471
|
+
} catch (error) {
|
|
472
|
+
const param = unsupportedParameter(error);
|
|
473
|
+
if (param === void 0 || !REJECTABLE_SETTINGS.includes(param) || !(param in sent)) throw error;
|
|
474
|
+
rejectedSettings.set(params.model, rejected.add(param));
|
|
475
|
+
console.warn(`@statelyai/agent/openai: \`${params.model}\` does not support \`${param}\`, so it is dropped from this executor's calls to that model.`);
|
|
476
|
+
return createResponse(params, signal);
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
const responsesParams = (request, input, extra) => {
|
|
480
|
+
const settings = toResponsesSettings({
|
|
481
|
+
...callSettings(options, request),
|
|
482
|
+
...toOpenAiCallSettings(request)
|
|
483
|
+
}, warnedSettings);
|
|
295
484
|
return {
|
|
296
485
|
model: resolveModel(request.model),
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
...
|
|
300
|
-
...
|
|
301
|
-
...extra
|
|
486
|
+
input,
|
|
487
|
+
store: false,
|
|
488
|
+
...settings,
|
|
489
|
+
...extra,
|
|
490
|
+
...settings.text || extra.text ? { text: {
|
|
491
|
+
...settings.text,
|
|
492
|
+
...extra.text
|
|
493
|
+
} } : {}
|
|
302
494
|
};
|
|
303
495
|
};
|
|
304
496
|
const structuredSchema = async (request) => {
|
|
305
497
|
if (getAgentOutputMode(request.outputSchema) !== "structured") return;
|
|
306
498
|
return getJsonSchema(providerOutputSchema(request.outputSchema, { reasoning: request.includeReasoning }));
|
|
307
499
|
};
|
|
308
|
-
const
|
|
309
|
-
const
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
500
|
+
const chatCompletion = async (request, jsonSchema, info) => {
|
|
501
|
+
const response = await client.chat.completions.create({
|
|
502
|
+
model: resolveModel(request.model),
|
|
503
|
+
messages: toOpenAiMessages(request),
|
|
504
|
+
...callSettings(options, request),
|
|
505
|
+
...toOpenAiCallSettings(request),
|
|
506
|
+
...jsonSchema ? { response_format: {
|
|
507
|
+
type: "json_schema",
|
|
508
|
+
json_schema: {
|
|
509
|
+
name: "output",
|
|
510
|
+
schema: jsonSchema,
|
|
511
|
+
strict: false
|
|
512
|
+
}
|
|
513
|
+
} } : {}
|
|
514
|
+
}, { signal: info?.signal });
|
|
515
|
+
const choice = response.choices[0];
|
|
516
|
+
return {
|
|
517
|
+
content: choice?.message.content,
|
|
518
|
+
finishReason: toAgentFinishReason(choice?.finish_reason),
|
|
519
|
+
usage: toAgentCallUsage(response.usage),
|
|
520
|
+
modelCalls: 1,
|
|
521
|
+
raw: response
|
|
522
|
+
};
|
|
523
|
+
};
|
|
524
|
+
const responsesToolLoop = async (request, tools, jsonSchema, info) => {
|
|
318
525
|
const maxSteps = typeof request.maxSteps === "number" && Number.isFinite(request.maxSteps) ? Math.max(1, Math.floor(request.maxSteps)) : 1;
|
|
319
|
-
const
|
|
526
|
+
const input = toResponsesInput(toOpenAiMessages(request));
|
|
320
527
|
const toolChoice = toOpenAiToolChoice(request.toolChoice);
|
|
321
528
|
let response;
|
|
322
529
|
let usage;
|
|
323
530
|
let step = 0;
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
...
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
tool_call_id: call.id,
|
|
348
|
-
content
|
|
531
|
+
try {
|
|
532
|
+
for (;;) {
|
|
533
|
+
step++;
|
|
534
|
+
response = await createResponse(responsesParams(request, [...input], {
|
|
535
|
+
...tools.length > 0 ? { tools } : {},
|
|
536
|
+
...jsonSchema ? { text: { format: {
|
|
537
|
+
type: "json_schema",
|
|
538
|
+
name: "output",
|
|
539
|
+
schema: jsonSchema,
|
|
540
|
+
strict: false
|
|
541
|
+
} } } : {},
|
|
542
|
+
...step === 1 && toolChoice !== void 0 ? { tool_choice: toolChoice } : {}
|
|
543
|
+
}), info?.signal);
|
|
544
|
+
assertNotFailed(response, "generateText");
|
|
545
|
+
usage = addAgentCallUsage(usage, toAgentResponseUsage(response.usage));
|
|
546
|
+
const calls = functionCalls(response);
|
|
547
|
+
if (calls.length === 0 || step >= maxSteps || response.status === "incomplete") break;
|
|
548
|
+
const offered = (name) => Object.hasOwn(request.tools, name) && request.tools[name] !== void 0;
|
|
549
|
+
if (calls.some((call) => offered(call.name) && !toolExecutor(request.tools, call.name))) break;
|
|
550
|
+
const text = outputText(response);
|
|
551
|
+
if (text) input.push({
|
|
552
|
+
role: "assistant",
|
|
553
|
+
content: text
|
|
349
554
|
});
|
|
555
|
+
for (const { call_id, name, arguments: args } of calls) {
|
|
556
|
+
input.push({
|
|
557
|
+
type: "function_call",
|
|
558
|
+
call_id,
|
|
559
|
+
name,
|
|
560
|
+
arguments: args || "{}"
|
|
561
|
+
});
|
|
562
|
+
let output;
|
|
563
|
+
try {
|
|
564
|
+
if (!offered(name)) throw new Error(`tool '${name}' was not offered; valid tools: ${Object.keys(request.tools).filter(offered).join(", ")}`);
|
|
565
|
+
let toolInput = {};
|
|
566
|
+
if (args) try {
|
|
567
|
+
toolInput = JSON.parse(args);
|
|
568
|
+
} catch {
|
|
569
|
+
throw new Error(`the arguments for tool '${name}' were not valid JSON`);
|
|
570
|
+
}
|
|
571
|
+
output = toToolContent(await toolExecutor(request.tools, name)(toolInput));
|
|
572
|
+
} catch (error) {
|
|
573
|
+
output = `Error: ${error instanceof Error ? error.message : String(error)}`;
|
|
574
|
+
}
|
|
575
|
+
input.push({
|
|
576
|
+
type: "function_call_output",
|
|
577
|
+
call_id,
|
|
578
|
+
output
|
|
579
|
+
});
|
|
580
|
+
}
|
|
350
581
|
}
|
|
582
|
+
} catch (error) {
|
|
583
|
+
throw withModelCalls(error, step);
|
|
351
584
|
}
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
585
|
+
return {
|
|
586
|
+
content: outputText(response),
|
|
587
|
+
finishReason: toAgentResponseFinishReason(response),
|
|
588
|
+
usage,
|
|
589
|
+
modelCalls: step,
|
|
590
|
+
raw: response
|
|
591
|
+
};
|
|
592
|
+
};
|
|
593
|
+
const generateText = async (request, info) => {
|
|
594
|
+
const jsonSchema = await structuredSchema(request);
|
|
595
|
+
const tools = toOpenAiTools(request.tools);
|
|
596
|
+
const { content, finishReason, usage, modelCalls, raw } = tools.length > 0 || api === "responses" ? await responsesToolLoop(request, tools, jsonSchema, info) : await chatCompletion(request, jsonSchema, info);
|
|
597
|
+
try {
|
|
598
|
+
if (jsonSchema) {
|
|
599
|
+
if (finishReason === "length") throw truncated(request, info, content || void 0);
|
|
600
|
+
const parsed = parseProviderOutput(request, content ? JSON.parse(content) : void 0);
|
|
601
|
+
return {
|
|
602
|
+
result: parsed.result,
|
|
603
|
+
...typeof parsed.reasoning === "string" ? { reasoning: parsed.reasoning } : {},
|
|
604
|
+
...usageField(usage),
|
|
605
|
+
finishReason,
|
|
606
|
+
modelCalls,
|
|
607
|
+
raw
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
} catch (error) {
|
|
611
|
+
throw withModelCalls(error, modelCalls);
|
|
366
612
|
}
|
|
367
613
|
return {
|
|
368
614
|
result: content ?? "",
|
|
369
615
|
...usageField(usage),
|
|
370
616
|
finishReason,
|
|
371
|
-
|
|
617
|
+
modelCalls,
|
|
618
|
+
raw
|
|
372
619
|
};
|
|
373
620
|
};
|
|
374
621
|
const streamText = async (request, info) => {
|
|
375
622
|
if (getAgentOutputMode(request.outputSchema) === "structured") throw new Error("createOpenAiExecutors: streamText is text-only — a request declaring a structured output schema must be routed to generateText.");
|
|
376
623
|
if (Object.keys(request.tools ?? {}).length > 0) throw new Error("createOpenAiExecutors: streamText is text-only — a request declaring tools must be routed to generateText.");
|
|
624
|
+
if (api === "responses") {
|
|
625
|
+
const events = await client.responses.create({
|
|
626
|
+
...responsesParams(request, toResponsesInput(toOpenAiMessages(request)), {}),
|
|
627
|
+
stream: true
|
|
628
|
+
}, { signal: info?.signal });
|
|
629
|
+
let text = "";
|
|
630
|
+
let response;
|
|
631
|
+
for await (const event of events) if (event.type === "response.output_text.delta") {
|
|
632
|
+
text += event.delta;
|
|
633
|
+
info?.onChunk?.(event.delta);
|
|
634
|
+
} else if (event.type === "response.completed" || event.type === "response.incomplete" || event.type === "response.failed") response = event.response;
|
|
635
|
+
if (response) assertNotFailed(response, "streamText");
|
|
636
|
+
return {
|
|
637
|
+
result: text,
|
|
638
|
+
...usageField(toAgentResponseUsage(response?.usage)),
|
|
639
|
+
finishReason: response ? toAgentResponseFinishReason(response) : "other",
|
|
640
|
+
raw: response
|
|
641
|
+
};
|
|
642
|
+
}
|
|
377
643
|
const stream = await client.chat.completions.create({
|
|
378
644
|
model: resolveModel(request.model),
|
|
379
645
|
messages: toOpenAiMessages(request),
|
|
@@ -405,26 +671,39 @@ function createOpenAiExecutors(options) {
|
|
|
405
671
|
};
|
|
406
672
|
};
|
|
407
673
|
const decide = async (request, info) => {
|
|
408
|
-
const response = await
|
|
409
|
-
model: resolveModel(request.model),
|
|
410
|
-
messages: toDecisionMessages(request),
|
|
411
|
-
...callSettings(options, request),
|
|
412
|
-
...toOpenAiCallSettings(request),
|
|
674
|
+
const response = await createResponse(responsesParams(request, toResponsesInput(toDecisionMessages(request)), {
|
|
413
675
|
tools: toOpenAiEventTools(request.events),
|
|
414
676
|
tool_choice: "required"
|
|
415
|
-
},
|
|
416
|
-
|
|
417
|
-
if (
|
|
418
|
-
const
|
|
419
|
-
|
|
420
|
-
|
|
677
|
+
}), info?.signal);
|
|
678
|
+
assertNotFailed(response, "decide");
|
|
679
|
+
if (response.status === "incomplete") throw new Error(`createOpenAiExecutors: decide — the response was incomplete (${response.incomplete_details?.reason ?? "unknown reason"}), so no event was chosen.`);
|
|
680
|
+
const toolCall = functionCalls(response)[0];
|
|
681
|
+
const unnamed = (call) => ({
|
|
682
|
+
rejected: rejectedAnswer(call, request.events.map((event) => event.toolName)),
|
|
683
|
+
...usageField(toAgentResponseUsage(response.usage)),
|
|
684
|
+
finishReason: toAgentResponseFinishReason(response),
|
|
685
|
+
raw: response
|
|
686
|
+
});
|
|
687
|
+
if (!toolCall) return unnamed();
|
|
688
|
+
const toolName = toolCall.name;
|
|
689
|
+
const chosenEvent = request.events.find((event) => event.toolName === toolName);
|
|
690
|
+
if (!chosenEvent) return unnamed({ toolName });
|
|
691
|
+
let args = {};
|
|
692
|
+
try {
|
|
693
|
+
if (toolCall.arguments) args = JSON.parse(toolCall.arguments);
|
|
694
|
+
} catch {
|
|
695
|
+
return unnamed({
|
|
696
|
+
toolName,
|
|
697
|
+
invalidJson: true
|
|
698
|
+
});
|
|
699
|
+
}
|
|
421
700
|
return {
|
|
422
701
|
event: {
|
|
423
702
|
...args && typeof args === "object" ? args : {},
|
|
424
703
|
type: chosenEvent.type
|
|
425
704
|
},
|
|
426
|
-
...usageField(
|
|
427
|
-
finishReason:
|
|
705
|
+
...usageField(toAgentResponseUsage(response.usage)),
|
|
706
|
+
finishReason: toAgentResponseFinishReason(response),
|
|
428
707
|
raw: response
|
|
429
708
|
};
|
|
430
709
|
};
|
|
@@ -435,4 +714,4 @@ function createOpenAiExecutors(options) {
|
|
|
435
714
|
};
|
|
436
715
|
}
|
|
437
716
|
//#endregion
|
|
438
|
-
export { addAgentCallUsage, createOpenAiExecutors, toAgentCallUsage, toAgentFinishReason, toDecisionMessages, toOpenAiCallSettings, toOpenAiEventTools, toOpenAiMessages, toOpenAiToolChoice, toOpenAiTools };
|
|
717
|
+
export { addAgentCallUsage, createOpenAiExecutors, toAgentCallUsage, toAgentFinishReason, toAgentResponseFinishReason, toAgentResponseUsage, toDecisionMessages, toOpenAiCallSettings, toOpenAiEventTools, toOpenAiMessages, toOpenAiToolChoice, toOpenAiTools, toResponsesInput };
|
package/dist/otel.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { g as AgentTraceEvent } from "./run-agent-
|
|
1
|
+
import { g as AgentTraceEvent } from "./run-agent-B1-Eqviq.cjs";
|
|
2
2
|
import { Attributes, Tracer, TracerProvider } from "@opentelemetry/api";
|
|
3
3
|
//#region src/otel/index.d.ts
|
|
4
4
|
/** Options for {@link createOtelTraceHandler}. */
|
package/dist/otel.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { g as AgentTraceEvent } from "./run-agent-
|
|
1
|
+
import { g as AgentTraceEvent } from "./run-agent-BGdx5913.mjs";
|
|
2
2
|
import { Attributes, Tracer, TracerProvider } from "@opentelemetry/api";
|
|
3
3
|
//#region src/otel/index.d.ts
|
|
4
4
|
/** Options for {@link createOtelTraceHandler}. */
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { _ as StandardSchemaV1, m as InferInput, s as AgentTools, w as WithAgentInputSchema } from "./types-Dp5_Ru4R.cjs";
|
|
2
|
-
import {
|
|
3
|
-
import { n as AgentUsageEvent } from "./usage-
|
|
4
|
-
import { c as AgentLogEntry, h as JsonValue$1, n as AgentEventLogStore } from "./event-log-store-
|
|
2
|
+
import { $ as AgentError, G as AgentEventDescriptor, J as AgentRequestSource, L as DecisionAttempt, P as AgentDecisionRequest, d as AgentRequestMode, f as AgentTextRequest, m as AgentUsage, r as AgentFinishReason, t as AgentCallUsage, u as AgentRequestExecutors } from "./text-logic-CV_4KGr6.cjs";
|
|
3
|
+
import { n as AgentUsageEvent } from "./usage-BDiJKyv9.cjs";
|
|
4
|
+
import { c as AgentLogEntry, h as JsonValue$1, n as AgentEventLogStore } from "./event-log-store-BzXehP7R.cjs";
|
|
5
5
|
import { AnyActorLogic, AnyActorRef, AnyMachineSnapshot, AnyStateMachine, AsyncActorLogic, EmittedFrom, EventFromLogic, EventObject, InputFrom, InspectionEvent, OutputFrom, Snapshot, SnapshotFrom, StateValue } from "xstate";
|
|
6
6
|
import { DurableExecution } from "xstate/durable";
|
|
7
7
|
//#region src/steps.d.ts
|
|
@@ -334,9 +334,16 @@ interface AgentRuntimeOptions<TMachine extends AnyStateMachine> {
|
|
|
334
334
|
* invoke that would have made the call, as an
|
|
335
335
|
* {@link AgentMaxModelCallsExceededError} with `code: 'max-model-calls'` — so
|
|
336
336
|
* an `onError` can branch on it and route to a degraded state. Unhandled, it
|
|
337
|
-
* settles `{ status: 'error', cause: 'max-model-calls' }`.
|
|
337
|
+
* settles `{ status: 'error', cause: 'max-model-calls' }`. A tool loop
|
|
338
|
+
* counts one call per step, so the call after it is the one refused.
|
|
338
339
|
*/
|
|
339
340
|
maxModelCalls?: number;
|
|
341
|
+
/**
|
|
342
|
+
* Model calls already spent against `maxModelCalls` before this run, such as
|
|
343
|
+
* earlier turns of the same conversation (sum their `usage.modelCalls`).
|
|
344
|
+
* Default 0. Not added to this run's `usage.modelCalls`.
|
|
345
|
+
*/
|
|
346
|
+
modelCallsSpent?: number;
|
|
340
347
|
/** Aborts the run; settles `{ status: 'error', cause: 'aborted' }` with `signal.reason` as the error. */
|
|
341
348
|
signal?: AbortSignal;
|
|
342
349
|
/** Where root `after` timers live. Default `"in-process"`. See {@link AgentTimerScheduler}. */
|
|
@@ -488,7 +495,8 @@ type AgentEffects = Parameters<DurableExecution<AnyStateMachine>["executeEffects
|
|
|
488
495
|
* durable timers (a Temporal timer, a Durable Object alarm, an Inngest sleep)
|
|
489
496
|
* passes `schedule`/`cancel`: the loop then treats a pending timer as the
|
|
490
497
|
* outside world's business, and the host delivers `{ type: "xstate.timer", id }`
|
|
491
|
-
* through {@link AgentRuntime.transition} when it fires.
|
|
498
|
+
* through {@link AgentRuntime.transition} when it fires. Only root timers go
|
|
499
|
+
* to the host: an invoked child machine's timers are always in-process.
|
|
492
500
|
*/
|
|
493
501
|
type AgentTimerScheduler = "in-process" | {
|
|
494
502
|
schedule(timer: {
|