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