@ai-sdk/openai 4.0.41 → 4.0.43
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 +26 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +148 -11
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +21 -5
- package/dist/internal/index.js +147 -10
- package/dist/internal/index.js.map +1 -1
- package/docs/03-openai.mdx +93 -0
- package/package.json +3 -3
- package/src/responses/openai-responses-api.ts +21 -1
- package/src/responses/openai-responses-language-model-options.ts +6 -0
- package/src/responses/openai-responses-language-model.ts +7 -0
- package/src/responses/openai-responses-prepare-tools.ts +188 -5
package/dist/internal/index.d.ts
CHANGED
|
@@ -391,7 +391,7 @@ declare const openaiResponsesComputerCallSchema: z.ZodObject<{
|
|
|
391
391
|
}, z.core.$strip>>>>;
|
|
392
392
|
}, z.core.$strip>;
|
|
393
393
|
type OpenAIResponsesInput = Array<OpenAIResponsesInputItem>;
|
|
394
|
-
type OpenAIResponsesInputItem = OpenAIResponsesSystemMessage | OpenAIResponsesUserMessage | OpenAIResponsesAssistantMessage | OpenAIResponsesFunctionCall | OpenAIResponsesFunctionCallOutput | OpenAIResponsesProgram | OpenAIResponsesProgramOutput | OpenAIResponsesCustomToolCall | OpenAIResponsesCustomToolCallOutput | OpenAIResponsesMcpApprovalResponse | OpenAIResponsesComputerCall | OpenAIResponsesComputerCallOutput | OpenAIResponsesLocalShellCall | OpenAIResponsesLocalShellCallOutput | OpenAIResponsesShellCall | OpenAIResponsesShellCallOutput | OpenAIResponsesApplyPatchCall | OpenAIResponsesApplyPatchCallOutput | OpenAIResponsesToolSearchCall | OpenAIResponsesToolSearchOutput | OpenAIResponsesReasoning | OpenAIResponsesItemReference | OpenAIResponsesCompactionItem;
|
|
394
|
+
type OpenAIResponsesInputItem = OpenAIResponsesSystemMessage | OpenAIResponsesUserMessage | OpenAIResponsesAssistantMessage | OpenAIResponsesFunctionCall | OpenAIResponsesFunctionCallOutput | OpenAIResponsesProgram | OpenAIResponsesProgramOutput | OpenAIResponsesCustomToolCall | OpenAIResponsesCustomToolCallOutput | OpenAIResponsesMcpApprovalResponse | OpenAIResponsesComputerCall | OpenAIResponsesComputerCallOutput | OpenAIResponsesLocalShellCall | OpenAIResponsesLocalShellCallOutput | OpenAIResponsesShellCall | OpenAIResponsesShellCallOutput | OpenAIResponsesApplyPatchCall | OpenAIResponsesApplyPatchCallOutput | OpenAIResponsesToolSearchCall | OpenAIResponsesToolSearchOutput | OpenAIResponsesReasoning | OpenAIResponsesItemReference | OpenAIResponsesCompactionItem | OpenAIResponsesCompactionTrigger;
|
|
395
395
|
type OpenAIResponsesIncludeValue = 'web_search_call.action.sources' | 'web_search_call.results' | 'code_interpreter_call.outputs' | 'computer_call_output.output.image_url' | 'file_search_call.results' | 'message.input_image.image_url' | 'message.output_text.logprobs' | 'reasoning.encrypted_content';
|
|
396
396
|
type OpenAIResponsesIncludeOptions = Array<OpenAIResponsesIncludeValue> | undefined | null;
|
|
397
397
|
type OpenAIResponsesSystemMessage = {
|
|
@@ -645,6 +645,9 @@ type OpenAIResponsesCompactionItem = {
|
|
|
645
645
|
id: string;
|
|
646
646
|
encrypted_content: string;
|
|
647
647
|
};
|
|
648
|
+
type OpenAIResponsesCompactionTrigger = {
|
|
649
|
+
type: 'compaction_trigger';
|
|
650
|
+
};
|
|
648
651
|
/**
|
|
649
652
|
* A filter used to compare a specified attribute key to a given value using a defined comparison operation.
|
|
650
653
|
*/
|
|
@@ -685,6 +688,22 @@ type OpenAIResponsesFunctionTool = {
|
|
|
685
688
|
allowed_callers?: Array<'direct' | 'programmatic'>;
|
|
686
689
|
output_schema?: JSONSchema7;
|
|
687
690
|
};
|
|
691
|
+
/**
|
|
692
|
+
* Entry in `tool_choice.allowed_tools.tools`. OpenAI identifies most built-in
|
|
693
|
+
* tools by type alone; only `function`, `custom` and `mcp` carry an identifier.
|
|
694
|
+
*/
|
|
695
|
+
type OpenAIResponsesAllowedTool = {
|
|
696
|
+
type: 'function';
|
|
697
|
+
name: string;
|
|
698
|
+
} | {
|
|
699
|
+
type: 'custom';
|
|
700
|
+
name: string;
|
|
701
|
+
} | {
|
|
702
|
+
type: 'mcp';
|
|
703
|
+
server_label: string;
|
|
704
|
+
} | {
|
|
705
|
+
type: Exclude<OpenAIResponsesTool['type'], 'function' | 'custom' | 'mcp' | 'namespace' | 'tool_search'>;
|
|
706
|
+
};
|
|
688
707
|
type OpenAIResponsesTool = OpenAIResponsesFunctionTool | {
|
|
689
708
|
type: 'namespace';
|
|
690
709
|
name: string;
|
|
@@ -1597,10 +1616,7 @@ declare class OpenAIResponsesLanguageModel implements LanguageModelV4 {
|
|
|
1597
1616
|
} | {
|
|
1598
1617
|
type: "allowed_tools";
|
|
1599
1618
|
mode: "auto" | "required";
|
|
1600
|
-
tools: Array<
|
|
1601
|
-
type: "function";
|
|
1602
|
-
name: string;
|
|
1603
|
-
}>;
|
|
1619
|
+
tools: Array<OpenAIResponsesAllowedTool>;
|
|
1604
1620
|
} | undefined;
|
|
1605
1621
|
reasoning?: {
|
|
1606
1622
|
context?: "auto" | "current_turn" | "all_turns" | undefined;
|
package/dist/internal/index.js
CHANGED
|
@@ -5650,6 +5650,11 @@ var openaiLanguageModelResponsesOptionsSchema = lazySchema19(
|
|
|
5650
5650
|
compactThreshold: z21.number()
|
|
5651
5651
|
})
|
|
5652
5652
|
).nullish(),
|
|
5653
|
+
/**
|
|
5654
|
+
* Request explicit server-side compaction by appending a
|
|
5655
|
+
* `compaction_trigger` item to the Responses input.
|
|
5656
|
+
*/
|
|
5657
|
+
compactionTrigger: z21.boolean().optional(),
|
|
5653
5658
|
/**
|
|
5654
5659
|
* Restrict the callable tools to a subset while keeping the full tools
|
|
5655
5660
|
* list intact, so prompt caching is preserved across requests with
|
|
@@ -6035,7 +6040,7 @@ async function prepareResponsesTools({
|
|
|
6035
6040
|
customProviderToolNames,
|
|
6036
6041
|
outputSchemaToolNames
|
|
6037
6042
|
}) {
|
|
6038
|
-
var _a, _b, _c;
|
|
6043
|
+
var _a, _b, _c, _d;
|
|
6039
6044
|
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
6040
6045
|
const toolWarnings = [];
|
|
6041
6046
|
if (tools == null) {
|
|
@@ -6044,6 +6049,20 @@ async function prepareResponsesTools({
|
|
|
6044
6049
|
const openaiTools = [];
|
|
6045
6050
|
const namespaceTools = /* @__PURE__ */ new Map();
|
|
6046
6051
|
const resolvedCustomProviderToolNames = customProviderToolNames != null ? customProviderToolNames : /* @__PURE__ */ new Set();
|
|
6052
|
+
const allowedToolResolutions = /* @__PURE__ */ new Map();
|
|
6053
|
+
const allowedToolAliases = /* @__PURE__ */ new Map();
|
|
6054
|
+
const recordAllowedTool = (toolName, resolution, canonicalName) => {
|
|
6055
|
+
allowedToolResolutions.set(toolName, resolution);
|
|
6056
|
+
if (canonicalName == null || canonicalName === toolName) {
|
|
6057
|
+
return;
|
|
6058
|
+
}
|
|
6059
|
+
const existingAlias = allowedToolAliases.get(canonicalName);
|
|
6060
|
+
if (existingAlias == null) {
|
|
6061
|
+
allowedToolAliases.set(canonicalName, resolution);
|
|
6062
|
+
} else if (existingAlias !== "ambiguous" && !isSameAllowedTool(existingAlias, resolution)) {
|
|
6063
|
+
allowedToolAliases.set(canonicalName, "ambiguous");
|
|
6064
|
+
}
|
|
6065
|
+
};
|
|
6047
6066
|
for (const tool of tools) {
|
|
6048
6067
|
switch (tool.type) {
|
|
6049
6068
|
case "function": {
|
|
@@ -6076,9 +6095,24 @@ async function prepareResponsesTools({
|
|
|
6076
6095
|
}
|
|
6077
6096
|
namespaceTool.tools.push(openaiFunctionTool);
|
|
6078
6097
|
}
|
|
6098
|
+
recordAllowedTool(
|
|
6099
|
+
tool.name,
|
|
6100
|
+
namespace != null ? {
|
|
6101
|
+
supported: false,
|
|
6102
|
+
reason: "tools inside an OpenAI tool namespace are not visible to tool_choice.allowed_tools"
|
|
6103
|
+
} : (openaiOptions == null ? void 0 : openaiOptions.deferLoading) === true ? {
|
|
6104
|
+
supported: false,
|
|
6105
|
+
reason: "deferred tools are not visible to tool_choice.allowed_tools"
|
|
6106
|
+
} : {
|
|
6107
|
+
supported: true,
|
|
6108
|
+
entry: { type: "function", name: tool.name }
|
|
6109
|
+
},
|
|
6110
|
+
void 0
|
|
6111
|
+
);
|
|
6079
6112
|
break;
|
|
6080
6113
|
}
|
|
6081
6114
|
case "provider": {
|
|
6115
|
+
const openaiToolCountBefore = openaiTools.length;
|
|
6082
6116
|
switch (tool.id) {
|
|
6083
6117
|
case "openai.file_search": {
|
|
6084
6118
|
const args = await validateTypes2({
|
|
@@ -6251,6 +6285,14 @@ async function prepareResponsesTools({
|
|
|
6251
6285
|
break;
|
|
6252
6286
|
}
|
|
6253
6287
|
}
|
|
6288
|
+
if (openaiTools.length > openaiToolCountBefore) {
|
|
6289
|
+
const openaiTool = openaiTools[openaiToolCountBefore];
|
|
6290
|
+
recordAllowedTool(
|
|
6291
|
+
tool.name,
|
|
6292
|
+
toAllowedToolResolution(openaiTool),
|
|
6293
|
+
toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(tool.name)
|
|
6294
|
+
);
|
|
6295
|
+
}
|
|
6254
6296
|
break;
|
|
6255
6297
|
}
|
|
6256
6298
|
default:
|
|
@@ -6262,18 +6304,63 @@ async function prepareResponsesTools({
|
|
|
6262
6304
|
}
|
|
6263
6305
|
}
|
|
6264
6306
|
if (allowedTools != null) {
|
|
6307
|
+
const allowedToolEntries = [];
|
|
6308
|
+
const droppedToolNames = [];
|
|
6309
|
+
for (const name of allowedTools.toolNames) {
|
|
6310
|
+
const directResolution = allowedToolResolutions.get(name);
|
|
6311
|
+
const resolution = directResolution != null ? directResolution : allowedToolAliases.get(name);
|
|
6312
|
+
if (directResolution != null && allowedToolAliases.has(name)) {
|
|
6313
|
+
toolWarnings.push({
|
|
6314
|
+
type: "unsupported",
|
|
6315
|
+
feature: `allowedTools entry "${name}"`,
|
|
6316
|
+
details: "this name is both a tool name and the provider tool name of another tool in this request; the tool with this name is allowed"
|
|
6317
|
+
});
|
|
6318
|
+
}
|
|
6319
|
+
if (resolution === "ambiguous") {
|
|
6320
|
+
toolWarnings.push({
|
|
6321
|
+
type: "unsupported",
|
|
6322
|
+
feature: `allowedTools entry "${name}"`,
|
|
6323
|
+
details: "several tools in this request share this provider tool name; use the tool name from the tools for this request instead"
|
|
6324
|
+
});
|
|
6325
|
+
droppedToolNames.push(name);
|
|
6326
|
+
continue;
|
|
6327
|
+
}
|
|
6328
|
+
if (resolution == null) {
|
|
6329
|
+
toolWarnings.push({
|
|
6330
|
+
type: "unsupported",
|
|
6331
|
+
feature: `allowedTools entry "${name}"`,
|
|
6332
|
+
details: "the tool is not part of the tools for this request and is sent as a function tool"
|
|
6333
|
+
});
|
|
6334
|
+
allowedToolEntries.push({
|
|
6335
|
+
type: "function",
|
|
6336
|
+
name: (_b = toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(name)) != null ? _b : name
|
|
6337
|
+
});
|
|
6338
|
+
continue;
|
|
6339
|
+
}
|
|
6340
|
+
if (!resolution.supported) {
|
|
6341
|
+
toolWarnings.push({
|
|
6342
|
+
type: "unsupported",
|
|
6343
|
+
feature: `allowedTools entry "${name}"`,
|
|
6344
|
+
details: `${resolution.reason}; the tool is removed from the allowed tools`
|
|
6345
|
+
});
|
|
6346
|
+
droppedToolNames.push(name);
|
|
6347
|
+
continue;
|
|
6348
|
+
}
|
|
6349
|
+
allowedToolEntries.push(resolution.entry);
|
|
6350
|
+
}
|
|
6351
|
+
if (allowedToolEntries.length === 0) {
|
|
6352
|
+
throw new UnsupportedFunctionalityError6({
|
|
6353
|
+
functionality: `allowedTools with only tools that cannot be allow-listed (${droppedToolNames.join(
|
|
6354
|
+
", "
|
|
6355
|
+
)})`
|
|
6356
|
+
});
|
|
6357
|
+
}
|
|
6265
6358
|
return {
|
|
6266
6359
|
tools: openaiTools,
|
|
6267
6360
|
toolChoice: {
|
|
6268
6361
|
type: "allowed_tools",
|
|
6269
|
-
mode: (
|
|
6270
|
-
tools:
|
|
6271
|
-
var _a2;
|
|
6272
|
-
return {
|
|
6273
|
-
type: "function",
|
|
6274
|
-
name: (_a2 = toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(name)) != null ? _a2 : name
|
|
6275
|
-
};
|
|
6276
|
-
})
|
|
6362
|
+
mode: (_c = allowedTools.mode) != null ? _c : "auto",
|
|
6363
|
+
tools: allowedToolEntries
|
|
6277
6364
|
},
|
|
6278
6365
|
toolWarnings
|
|
6279
6366
|
};
|
|
@@ -6288,7 +6375,7 @@ async function prepareResponsesTools({
|
|
|
6288
6375
|
case "required":
|
|
6289
6376
|
return { tools: openaiTools, toolChoice: type, toolWarnings };
|
|
6290
6377
|
case "tool": {
|
|
6291
|
-
const resolvedToolName = (
|
|
6378
|
+
const resolvedToolName = (_d = toolNameMapping == null ? void 0 : toolNameMapping.toProviderToolName(toolChoice.toolName)) != null ? _d : toolChoice.toolName;
|
|
6292
6379
|
return {
|
|
6293
6380
|
tools: openaiTools,
|
|
6294
6381
|
toolChoice: resolvedToolName === "code_interpreter" || resolvedToolName === "file_search" || resolvedToolName === "image_generation" || resolvedToolName === "web_search_preview" || resolvedToolName === "web_search" || resolvedToolName === "mcp" || resolvedToolName === "apply_patch" || resolvedToolName === "computer" || resolvedToolName === "programmatic_tool_calling" ? { type: resolvedToolName } : resolvedCustomProviderToolNames.has(resolvedToolName) ? { type: "custom", name: resolvedToolName } : { type: "function", name: resolvedToolName },
|
|
@@ -6303,6 +6390,53 @@ async function prepareResponsesTools({
|
|
|
6303
6390
|
}
|
|
6304
6391
|
}
|
|
6305
6392
|
}
|
|
6393
|
+
function allowedToolKey(entry) {
|
|
6394
|
+
switch (entry.type) {
|
|
6395
|
+
case "mcp":
|
|
6396
|
+
return `mcp:${entry.server_label}`;
|
|
6397
|
+
case "function":
|
|
6398
|
+
case "custom":
|
|
6399
|
+
return `${entry.type}:${entry.name}`;
|
|
6400
|
+
default:
|
|
6401
|
+
return entry.type;
|
|
6402
|
+
}
|
|
6403
|
+
}
|
|
6404
|
+
function isSameAllowedTool(a, b) {
|
|
6405
|
+
if (a.supported && b.supported) {
|
|
6406
|
+
return allowedToolKey(a.entry) === allowedToolKey(b.entry);
|
|
6407
|
+
}
|
|
6408
|
+
if (!a.supported && !b.supported) {
|
|
6409
|
+
return a.reason === b.reason;
|
|
6410
|
+
}
|
|
6411
|
+
return false;
|
|
6412
|
+
}
|
|
6413
|
+
function toAllowedToolResolution(tool) {
|
|
6414
|
+
switch (tool.type) {
|
|
6415
|
+
case "custom":
|
|
6416
|
+
return { supported: true, entry: { type: "custom", name: tool.name } };
|
|
6417
|
+
case "mcp":
|
|
6418
|
+
return {
|
|
6419
|
+
supported: true,
|
|
6420
|
+
entry: { type: "mcp", server_label: tool.server_label }
|
|
6421
|
+
};
|
|
6422
|
+
case "file_search":
|
|
6423
|
+
case "web_search":
|
|
6424
|
+
case "web_search_preview":
|
|
6425
|
+
case "image_generation":
|
|
6426
|
+
case "code_interpreter":
|
|
6427
|
+
case "computer":
|
|
6428
|
+
case "apply_patch":
|
|
6429
|
+
case "shell":
|
|
6430
|
+
case "local_shell":
|
|
6431
|
+
case "programmatic_tool_calling":
|
|
6432
|
+
return { supported: true, entry: { type: tool.type } };
|
|
6433
|
+
default:
|
|
6434
|
+
return {
|
|
6435
|
+
supported: false,
|
|
6436
|
+
reason: `OpenAI does not support ${tool.type} tools in tool_choice.allowed_tools`
|
|
6437
|
+
};
|
|
6438
|
+
}
|
|
6439
|
+
}
|
|
6306
6440
|
function prepareFunctionTool({
|
|
6307
6441
|
tool,
|
|
6308
6442
|
options
|
|
@@ -6584,6 +6718,9 @@ var OpenAIResponsesLanguageModel = class _OpenAIResponsesLanguageModel {
|
|
|
6584
6718
|
outputSchemaToolNames: outputSchemaToolNames.size > 0 ? outputSchemaToolNames : void 0
|
|
6585
6719
|
});
|
|
6586
6720
|
warnings.push(...inputWarnings);
|
|
6721
|
+
if (openaiOptions == null ? void 0 : openaiOptions.compactionTrigger) {
|
|
6722
|
+
input.push({ type: "compaction_trigger" });
|
|
6723
|
+
}
|
|
6587
6724
|
const strictJsonSchema = (_g = openaiOptions == null ? void 0 : openaiOptions.strictJsonSchema) != null ? _g : true;
|
|
6588
6725
|
let include = openaiOptions == null ? void 0 : openaiOptions.include;
|
|
6589
6726
|
function addInclude(key) {
|