@ai-sdk/openai
Advanced tools
Comparing version 0.0.68 to 0.0.70
@@ -33,3 +33,3 @@ "use strict"; | ||
// src/openai-chat-language-model.ts | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
var import_provider3 = require("@ai-sdk/provider"); | ||
var import_provider_utils3 = require("@ai-sdk/provider-utils"); | ||
@@ -242,2 +242,101 @@ var import_zod2 = require("zod"); | ||
// src/openai-prepare-tools.ts | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
function prepareTools({ | ||
mode, | ||
useLegacyFunctionCalling = false, | ||
structuredOutputs = false | ||
}) { | ||
var _a; | ||
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0; | ||
const toolWarnings = []; | ||
if (tools == null) { | ||
return { tools: void 0, tool_choice: void 0, toolWarnings }; | ||
} | ||
const toolChoice = mode.toolChoice; | ||
if (useLegacyFunctionCalling) { | ||
const openaiFunctions = []; | ||
for (const tool of tools) { | ||
if (tool.type === "provider-defined") { | ||
toolWarnings.push({ type: "unsupported-tool", tool }); | ||
} else { | ||
openaiFunctions.push({ | ||
name: tool.name, | ||
description: tool.description, | ||
parameters: tool.parameters | ||
}); | ||
} | ||
} | ||
if (toolChoice == null) { | ||
return { | ||
functions: openaiFunctions, | ||
function_call: void 0, | ||
toolWarnings | ||
}; | ||
} | ||
const type2 = toolChoice.type; | ||
switch (type2) { | ||
case "auto": | ||
case "none": | ||
case void 0: | ||
return { | ||
functions: openaiFunctions, | ||
function_call: void 0, | ||
toolWarnings | ||
}; | ||
case "required": | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: "useLegacyFunctionCalling and toolChoice: required" | ||
}); | ||
default: | ||
return { | ||
functions: openaiFunctions, | ||
function_call: { name: toolChoice.toolName }, | ||
toolWarnings | ||
}; | ||
} | ||
} | ||
const openaiTools = []; | ||
for (const tool of tools) { | ||
if (tool.type === "provider-defined") { | ||
toolWarnings.push({ type: "unsupported-tool", tool }); | ||
} else { | ||
openaiTools.push({ | ||
type: "function", | ||
function: { | ||
name: tool.name, | ||
description: tool.description, | ||
parameters: tool.parameters, | ||
strict: structuredOutputs === true ? true : void 0 | ||
} | ||
}); | ||
} | ||
} | ||
if (toolChoice == null) { | ||
return { tools: openaiTools, tool_choice: void 0, toolWarnings }; | ||
} | ||
const type = toolChoice.type; | ||
switch (type) { | ||
case "auto": | ||
case "none": | ||
case "required": | ||
return { tools: openaiTools, tool_choice: type, toolWarnings }; | ||
case "tool": | ||
return { | ||
tools: openaiTools, | ||
tool_choice: { | ||
type: "function", | ||
function: { | ||
name: toolChoice.toolName | ||
} | ||
}, | ||
toolWarnings | ||
}; | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
// src/openai-chat-language-model.ts | ||
@@ -298,3 +397,3 @@ var OpenAIChatLanguageModel = class { | ||
if (useLegacyFunctionCalling && this.settings.parallelToolCalls === true) { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
functionality: "useLegacyFunctionCalling with parallelToolCalls" | ||
@@ -304,3 +403,3 @@ }); | ||
if (useLegacyFunctionCalling && this.settings.structuredOutputs === true) { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
functionality: "structuredOutputs with useLegacyFunctionCalling" | ||
@@ -346,12 +445,16 @@ }); | ||
case "regular": { | ||
const { tools, tool_choice, functions, function_call, toolWarnings } = prepareTools({ | ||
mode, | ||
useLegacyFunctionCalling, | ||
structuredOutputs: this.settings.structuredOutputs | ||
}); | ||
return { | ||
args: { | ||
...baseArgs, | ||
...prepareToolsAndToolChoice({ | ||
mode, | ||
useLegacyFunctionCalling, | ||
structuredOutputs: this.settings.structuredOutputs | ||
}) | ||
tools, | ||
tool_choice, | ||
functions, | ||
function_call | ||
}, | ||
warnings | ||
warnings: [...warnings, ...toolWarnings] | ||
}; | ||
@@ -419,3 +522,3 @@ } | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r; | ||
const { args, warnings } = this.getArgs(options); | ||
const { args: body, warnings } = this.getArgs(options); | ||
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({ | ||
@@ -427,3 +530,3 @@ url: this.config.url({ | ||
headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers), | ||
body: args, | ||
body, | ||
failedResponseHandler: openaiFailedResponseHandler, | ||
@@ -436,3 +539,3 @@ successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)( | ||
}); | ||
const { messages: rawPrompt, ...rawSettings } = args; | ||
const { messages: rawPrompt, ...rawSettings } = body; | ||
const choice = response.choices[0]; | ||
@@ -474,2 +577,3 @@ let providerMetadata; | ||
rawResponse: { headers: responseHeaders }, | ||
request: { body: JSON.stringify(body) }, | ||
response: getResponseMetadata(response), | ||
@@ -519,2 +623,8 @@ warnings, | ||
const { args, warnings } = this.getArgs(options); | ||
const body = { | ||
...args, | ||
stream: true, | ||
// only include stream_options when in strict compatibility mode: | ||
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0 | ||
}; | ||
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({ | ||
@@ -526,8 +636,3 @@ url: this.config.url({ | ||
headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers), | ||
body: { | ||
...args, | ||
stream: true, | ||
// only include stream_options when in strict compatibility mode: | ||
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0 | ||
}, | ||
body, | ||
failedResponseHandler: openaiFailedResponseHandler, | ||
@@ -621,3 +726,3 @@ successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)( | ||
if (toolCallDelta.type !== "function") { | ||
throw new import_provider2.InvalidResponseDataError({ | ||
throw new import_provider3.InvalidResponseDataError({ | ||
data: toolCallDelta, | ||
@@ -628,3 +733,3 @@ message: `Expected 'function' type.` | ||
if (toolCallDelta.id == null) { | ||
throw new import_provider2.InvalidResponseDataError({ | ||
throw new import_provider3.InvalidResponseDataError({ | ||
data: toolCallDelta, | ||
@@ -635,3 +740,3 @@ message: `Expected 'id' to be a string.` | ||
if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) { | ||
throw new import_provider2.InvalidResponseDataError({ | ||
throw new import_provider3.InvalidResponseDataError({ | ||
data: toolCallDelta, | ||
@@ -712,2 +817,3 @@ message: `Expected 'function.name' to be a string.` | ||
rawResponse: { headers: responseHeaders }, | ||
request: { body: JSON.stringify(body) }, | ||
warnings | ||
@@ -819,76 +925,2 @@ }; | ||
]); | ||
function prepareToolsAndToolChoice({ | ||
mode, | ||
useLegacyFunctionCalling = false, | ||
structuredOutputs = false | ||
}) { | ||
var _a; | ||
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0; | ||
if (tools == null) { | ||
return { tools: void 0, tool_choice: void 0 }; | ||
} | ||
const toolChoice = mode.toolChoice; | ||
if (useLegacyFunctionCalling) { | ||
const mappedFunctions = tools.map((tool) => ({ | ||
name: tool.name, | ||
description: tool.description, | ||
parameters: tool.parameters | ||
})); | ||
if (toolChoice == null) { | ||
return { functions: mappedFunctions, function_call: void 0 }; | ||
} | ||
const type2 = toolChoice.type; | ||
switch (type2) { | ||
case "auto": | ||
case "none": | ||
case void 0: | ||
return { | ||
functions: mappedFunctions, | ||
function_call: void 0 | ||
}; | ||
case "required": | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: "useLegacyFunctionCalling and toolChoice: required" | ||
}); | ||
default: | ||
return { | ||
functions: mappedFunctions, | ||
function_call: { name: toolChoice.toolName } | ||
}; | ||
} | ||
} | ||
const mappedTools = tools.map((tool) => ({ | ||
type: "function", | ||
function: { | ||
name: tool.name, | ||
description: tool.description, | ||
parameters: tool.parameters, | ||
strict: structuredOutputs === true ? true : void 0 | ||
} | ||
})); | ||
if (toolChoice == null) { | ||
return { tools: mappedTools, tool_choice: void 0 }; | ||
} | ||
const type = toolChoice.type; | ||
switch (type) { | ||
case "auto": | ||
case "none": | ||
case "required": | ||
return { tools: mappedTools, tool_choice: type }; | ||
case "tool": | ||
return { | ||
tools: mappedTools, | ||
tool_choice: { | ||
type: "function", | ||
function: { | ||
name: toolChoice.toolName | ||
} | ||
} | ||
}; | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
function isReasoningModel(modelId) { | ||
@@ -902,3 +934,3 @@ return modelId.startsWith("o1-"); | ||
// src/openai-completion-language-model.ts | ||
var import_provider4 = require("@ai-sdk/provider"); | ||
var import_provider5 = require("@ai-sdk/provider"); | ||
var import_provider_utils4 = require("@ai-sdk/provider-utils"); | ||
@@ -908,3 +940,3 @@ var import_zod3 = require("zod"); | ||
// src/convert-to-openai-completion-prompt.ts | ||
var import_provider3 = require("@ai-sdk/provider"); | ||
var import_provider4 = require("@ai-sdk/provider"); | ||
function convertToOpenAICompletionPrompt({ | ||
@@ -929,3 +961,3 @@ prompt, | ||
case "system": { | ||
throw new import_provider3.InvalidPromptError({ | ||
throw new import_provider4.InvalidPromptError({ | ||
message: "Unexpected system message in prompt: ${content}", | ||
@@ -942,3 +974,3 @@ prompt | ||
case "image": { | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
functionality: "images" | ||
@@ -962,3 +994,3 @@ }); | ||
case "tool-call": { | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
functionality: "tool-call messages" | ||
@@ -976,3 +1008,3 @@ }); | ||
case "tool": { | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
functionality: "tool messages" | ||
@@ -1078,3 +1110,3 @@ }); | ||
if ((_a = mode.tools) == null ? void 0 : _a.length) { | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
throw new import_provider5.UnsupportedFunctionalityError({ | ||
functionality: "tools" | ||
@@ -1084,3 +1116,3 @@ }); | ||
if (mode.toolChoice) { | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
throw new import_provider5.UnsupportedFunctionalityError({ | ||
functionality: "toolChoice" | ||
@@ -1092,3 +1124,3 @@ }); | ||
case "object-json": { | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
throw new import_provider5.UnsupportedFunctionalityError({ | ||
functionality: "object-json mode" | ||
@@ -1098,3 +1130,3 @@ }); | ||
case "object-tool": { | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
throw new import_provider5.UnsupportedFunctionalityError({ | ||
functionality: "object-tool mode" | ||
@@ -1138,3 +1170,4 @@ }); | ||
response: getResponseMetadata(response), | ||
warnings | ||
warnings, | ||
request: { body: JSON.stringify(args) } | ||
}; | ||
@@ -1144,2 +1177,8 @@ } | ||
const { args, warnings } = this.getArgs(options); | ||
const body = { | ||
...args, | ||
stream: true, | ||
// only include stream_options when in strict compatibility mode: | ||
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0 | ||
}; | ||
const { responseHeaders, value: response } = await (0, import_provider_utils4.postJsonToApi)({ | ||
@@ -1151,8 +1190,3 @@ url: this.config.url({ | ||
headers: (0, import_provider_utils4.combineHeaders)(this.config.headers(), options.headers), | ||
body: { | ||
...args, | ||
stream: true, | ||
// only include stream_options when in strict compatibility mode: | ||
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0 | ||
}, | ||
body, | ||
failedResponseHandler: openaiFailedResponseHandler, | ||
@@ -1231,3 +1265,4 @@ successfulResponseHandler: (0, import_provider_utils4.createEventSourceResponseHandler)( | ||
rawResponse: { headers: responseHeaders }, | ||
warnings | ||
warnings, | ||
request: { body: JSON.stringify(body) } | ||
}; | ||
@@ -1332,3 +1367,3 @@ } | ||
// src/openai-embedding-model.ts | ||
var import_provider5 = require("@ai-sdk/provider"); | ||
var import_provider6 = require("@ai-sdk/provider"); | ||
var import_provider_utils6 = require("@ai-sdk/provider-utils"); | ||
@@ -1360,3 +1395,3 @@ var import_zod4 = require("zod"); | ||
if (values.length > this.maxEmbeddingsPerCall) { | ||
throw new import_provider5.TooManyEmbeddingValuesForCallError({ | ||
throw new import_provider6.TooManyEmbeddingValuesForCallError({ | ||
provider: this.provider, | ||
@@ -1363,0 +1398,0 @@ modelId: this.modelId, |
# @ai-sdk/openai | ||
## 0.0.70 | ||
### Patch Changes | ||
- 3b1b69a: feat: provider-defined tools | ||
- Updated dependencies [aa98cdb] | ||
- Updated dependencies [1486128] | ||
- Updated dependencies [7b937c5] | ||
- Updated dependencies [3b1b69a] | ||
- Updated dependencies [811a317] | ||
- @ai-sdk/provider-utils@1.0.22 | ||
- @ai-sdk/provider@0.0.26 | ||
## 0.0.69 | ||
### Patch Changes | ||
- b9b0d7b: feat (ai): access raw request body | ||
- Updated dependencies [b9b0d7b] | ||
- @ai-sdk/provider@0.0.25 | ||
- @ai-sdk/provider-utils@1.0.21 | ||
## 0.0.68 | ||
@@ -4,0 +26,0 @@ |
@@ -33,3 +33,3 @@ "use strict"; | ||
// src/openai-chat-language-model.ts | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
var import_provider3 = require("@ai-sdk/provider"); | ||
var import_provider_utils3 = require("@ai-sdk/provider-utils"); | ||
@@ -242,2 +242,101 @@ var import_zod2 = require("zod"); | ||
// src/openai-prepare-tools.ts | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
function prepareTools({ | ||
mode, | ||
useLegacyFunctionCalling = false, | ||
structuredOutputs = false | ||
}) { | ||
var _a; | ||
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0; | ||
const toolWarnings = []; | ||
if (tools == null) { | ||
return { tools: void 0, tool_choice: void 0, toolWarnings }; | ||
} | ||
const toolChoice = mode.toolChoice; | ||
if (useLegacyFunctionCalling) { | ||
const openaiFunctions = []; | ||
for (const tool of tools) { | ||
if (tool.type === "provider-defined") { | ||
toolWarnings.push({ type: "unsupported-tool", tool }); | ||
} else { | ||
openaiFunctions.push({ | ||
name: tool.name, | ||
description: tool.description, | ||
parameters: tool.parameters | ||
}); | ||
} | ||
} | ||
if (toolChoice == null) { | ||
return { | ||
functions: openaiFunctions, | ||
function_call: void 0, | ||
toolWarnings | ||
}; | ||
} | ||
const type2 = toolChoice.type; | ||
switch (type2) { | ||
case "auto": | ||
case "none": | ||
case void 0: | ||
return { | ||
functions: openaiFunctions, | ||
function_call: void 0, | ||
toolWarnings | ||
}; | ||
case "required": | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: "useLegacyFunctionCalling and toolChoice: required" | ||
}); | ||
default: | ||
return { | ||
functions: openaiFunctions, | ||
function_call: { name: toolChoice.toolName }, | ||
toolWarnings | ||
}; | ||
} | ||
} | ||
const openaiTools = []; | ||
for (const tool of tools) { | ||
if (tool.type === "provider-defined") { | ||
toolWarnings.push({ type: "unsupported-tool", tool }); | ||
} else { | ||
openaiTools.push({ | ||
type: "function", | ||
function: { | ||
name: tool.name, | ||
description: tool.description, | ||
parameters: tool.parameters, | ||
strict: structuredOutputs === true ? true : void 0 | ||
} | ||
}); | ||
} | ||
} | ||
if (toolChoice == null) { | ||
return { tools: openaiTools, tool_choice: void 0, toolWarnings }; | ||
} | ||
const type = toolChoice.type; | ||
switch (type) { | ||
case "auto": | ||
case "none": | ||
case "required": | ||
return { tools: openaiTools, tool_choice: type, toolWarnings }; | ||
case "tool": | ||
return { | ||
tools: openaiTools, | ||
tool_choice: { | ||
type: "function", | ||
function: { | ||
name: toolChoice.toolName | ||
} | ||
}, | ||
toolWarnings | ||
}; | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
// src/openai-chat-language-model.ts | ||
@@ -298,3 +397,3 @@ var OpenAIChatLanguageModel = class { | ||
if (useLegacyFunctionCalling && this.settings.parallelToolCalls === true) { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
functionality: "useLegacyFunctionCalling with parallelToolCalls" | ||
@@ -304,3 +403,3 @@ }); | ||
if (useLegacyFunctionCalling && this.settings.structuredOutputs === true) { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
functionality: "structuredOutputs with useLegacyFunctionCalling" | ||
@@ -346,12 +445,16 @@ }); | ||
case "regular": { | ||
const { tools, tool_choice, functions, function_call, toolWarnings } = prepareTools({ | ||
mode, | ||
useLegacyFunctionCalling, | ||
structuredOutputs: this.settings.structuredOutputs | ||
}); | ||
return { | ||
args: { | ||
...baseArgs, | ||
...prepareToolsAndToolChoice({ | ||
mode, | ||
useLegacyFunctionCalling, | ||
structuredOutputs: this.settings.structuredOutputs | ||
}) | ||
tools, | ||
tool_choice, | ||
functions, | ||
function_call | ||
}, | ||
warnings | ||
warnings: [...warnings, ...toolWarnings] | ||
}; | ||
@@ -419,3 +522,3 @@ } | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r; | ||
const { args, warnings } = this.getArgs(options); | ||
const { args: body, warnings } = this.getArgs(options); | ||
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({ | ||
@@ -427,3 +530,3 @@ url: this.config.url({ | ||
headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers), | ||
body: args, | ||
body, | ||
failedResponseHandler: openaiFailedResponseHandler, | ||
@@ -436,3 +539,3 @@ successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)( | ||
}); | ||
const { messages: rawPrompt, ...rawSettings } = args; | ||
const { messages: rawPrompt, ...rawSettings } = body; | ||
const choice = response.choices[0]; | ||
@@ -474,2 +577,3 @@ let providerMetadata; | ||
rawResponse: { headers: responseHeaders }, | ||
request: { body: JSON.stringify(body) }, | ||
response: getResponseMetadata(response), | ||
@@ -519,2 +623,8 @@ warnings, | ||
const { args, warnings } = this.getArgs(options); | ||
const body = { | ||
...args, | ||
stream: true, | ||
// only include stream_options when in strict compatibility mode: | ||
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0 | ||
}; | ||
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({ | ||
@@ -526,8 +636,3 @@ url: this.config.url({ | ||
headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers), | ||
body: { | ||
...args, | ||
stream: true, | ||
// only include stream_options when in strict compatibility mode: | ||
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0 | ||
}, | ||
body, | ||
failedResponseHandler: openaiFailedResponseHandler, | ||
@@ -621,3 +726,3 @@ successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)( | ||
if (toolCallDelta.type !== "function") { | ||
throw new import_provider2.InvalidResponseDataError({ | ||
throw new import_provider3.InvalidResponseDataError({ | ||
data: toolCallDelta, | ||
@@ -628,3 +733,3 @@ message: `Expected 'function' type.` | ||
if (toolCallDelta.id == null) { | ||
throw new import_provider2.InvalidResponseDataError({ | ||
throw new import_provider3.InvalidResponseDataError({ | ||
data: toolCallDelta, | ||
@@ -635,3 +740,3 @@ message: `Expected 'id' to be a string.` | ||
if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) { | ||
throw new import_provider2.InvalidResponseDataError({ | ||
throw new import_provider3.InvalidResponseDataError({ | ||
data: toolCallDelta, | ||
@@ -712,2 +817,3 @@ message: `Expected 'function.name' to be a string.` | ||
rawResponse: { headers: responseHeaders }, | ||
request: { body: JSON.stringify(body) }, | ||
warnings | ||
@@ -819,76 +925,2 @@ }; | ||
]); | ||
function prepareToolsAndToolChoice({ | ||
mode, | ||
useLegacyFunctionCalling = false, | ||
structuredOutputs = false | ||
}) { | ||
var _a; | ||
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0; | ||
if (tools == null) { | ||
return { tools: void 0, tool_choice: void 0 }; | ||
} | ||
const toolChoice = mode.toolChoice; | ||
if (useLegacyFunctionCalling) { | ||
const mappedFunctions = tools.map((tool) => ({ | ||
name: tool.name, | ||
description: tool.description, | ||
parameters: tool.parameters | ||
})); | ||
if (toolChoice == null) { | ||
return { functions: mappedFunctions, function_call: void 0 }; | ||
} | ||
const type2 = toolChoice.type; | ||
switch (type2) { | ||
case "auto": | ||
case "none": | ||
case void 0: | ||
return { | ||
functions: mappedFunctions, | ||
function_call: void 0 | ||
}; | ||
case "required": | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: "useLegacyFunctionCalling and toolChoice: required" | ||
}); | ||
default: | ||
return { | ||
functions: mappedFunctions, | ||
function_call: { name: toolChoice.toolName } | ||
}; | ||
} | ||
} | ||
const mappedTools = tools.map((tool) => ({ | ||
type: "function", | ||
function: { | ||
name: tool.name, | ||
description: tool.description, | ||
parameters: tool.parameters, | ||
strict: structuredOutputs === true ? true : void 0 | ||
} | ||
})); | ||
if (toolChoice == null) { | ||
return { tools: mappedTools, tool_choice: void 0 }; | ||
} | ||
const type = toolChoice.type; | ||
switch (type) { | ||
case "auto": | ||
case "none": | ||
case "required": | ||
return { tools: mappedTools, tool_choice: type }; | ||
case "tool": | ||
return { | ||
tools: mappedTools, | ||
tool_choice: { | ||
type: "function", | ||
function: { | ||
name: toolChoice.toolName | ||
} | ||
} | ||
}; | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
function isReasoningModel(modelId) { | ||
@@ -902,3 +934,3 @@ return modelId.startsWith("o1-"); | ||
// src/openai-completion-language-model.ts | ||
var import_provider4 = require("@ai-sdk/provider"); | ||
var import_provider5 = require("@ai-sdk/provider"); | ||
var import_provider_utils4 = require("@ai-sdk/provider-utils"); | ||
@@ -908,3 +940,3 @@ var import_zod3 = require("zod"); | ||
// src/convert-to-openai-completion-prompt.ts | ||
var import_provider3 = require("@ai-sdk/provider"); | ||
var import_provider4 = require("@ai-sdk/provider"); | ||
function convertToOpenAICompletionPrompt({ | ||
@@ -929,3 +961,3 @@ prompt, | ||
case "system": { | ||
throw new import_provider3.InvalidPromptError({ | ||
throw new import_provider4.InvalidPromptError({ | ||
message: "Unexpected system message in prompt: ${content}", | ||
@@ -942,3 +974,3 @@ prompt | ||
case "image": { | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
functionality: "images" | ||
@@ -962,3 +994,3 @@ }); | ||
case "tool-call": { | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
functionality: "tool-call messages" | ||
@@ -976,3 +1008,3 @@ }); | ||
case "tool": { | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
functionality: "tool messages" | ||
@@ -1078,3 +1110,3 @@ }); | ||
if ((_a = mode.tools) == null ? void 0 : _a.length) { | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
throw new import_provider5.UnsupportedFunctionalityError({ | ||
functionality: "tools" | ||
@@ -1084,3 +1116,3 @@ }); | ||
if (mode.toolChoice) { | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
throw new import_provider5.UnsupportedFunctionalityError({ | ||
functionality: "toolChoice" | ||
@@ -1092,3 +1124,3 @@ }); | ||
case "object-json": { | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
throw new import_provider5.UnsupportedFunctionalityError({ | ||
functionality: "object-json mode" | ||
@@ -1098,3 +1130,3 @@ }); | ||
case "object-tool": { | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
throw new import_provider5.UnsupportedFunctionalityError({ | ||
functionality: "object-tool mode" | ||
@@ -1138,3 +1170,4 @@ }); | ||
response: getResponseMetadata(response), | ||
warnings | ||
warnings, | ||
request: { body: JSON.stringify(args) } | ||
}; | ||
@@ -1144,2 +1177,8 @@ } | ||
const { args, warnings } = this.getArgs(options); | ||
const body = { | ||
...args, | ||
stream: true, | ||
// only include stream_options when in strict compatibility mode: | ||
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0 | ||
}; | ||
const { responseHeaders, value: response } = await (0, import_provider_utils4.postJsonToApi)({ | ||
@@ -1151,8 +1190,3 @@ url: this.config.url({ | ||
headers: (0, import_provider_utils4.combineHeaders)(this.config.headers(), options.headers), | ||
body: { | ||
...args, | ||
stream: true, | ||
// only include stream_options when in strict compatibility mode: | ||
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0 | ||
}, | ||
body, | ||
failedResponseHandler: openaiFailedResponseHandler, | ||
@@ -1231,3 +1265,4 @@ successfulResponseHandler: (0, import_provider_utils4.createEventSourceResponseHandler)( | ||
rawResponse: { headers: responseHeaders }, | ||
warnings | ||
warnings, | ||
request: { body: JSON.stringify(body) } | ||
}; | ||
@@ -1332,3 +1367,3 @@ } | ||
// src/openai-embedding-model.ts | ||
var import_provider5 = require("@ai-sdk/provider"); | ||
var import_provider6 = require("@ai-sdk/provider"); | ||
var import_provider_utils6 = require("@ai-sdk/provider-utils"); | ||
@@ -1360,3 +1395,3 @@ var import_zod4 = require("zod"); | ||
if (values.length > this.maxEmbeddingsPerCall) { | ||
throw new import_provider5.TooManyEmbeddingValuesForCallError({ | ||
throw new import_provider6.TooManyEmbeddingValuesForCallError({ | ||
provider: this.provider, | ||
@@ -1363,0 +1398,0 @@ modelId: this.modelId, |
@@ -30,3 +30,3 @@ "use strict"; | ||
// src/openai-chat-language-model.ts | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
var import_provider3 = require("@ai-sdk/provider"); | ||
var import_provider_utils3 = require("@ai-sdk/provider-utils"); | ||
@@ -239,2 +239,101 @@ var import_zod2 = require("zod"); | ||
// src/openai-prepare-tools.ts | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
function prepareTools({ | ||
mode, | ||
useLegacyFunctionCalling = false, | ||
structuredOutputs = false | ||
}) { | ||
var _a; | ||
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0; | ||
const toolWarnings = []; | ||
if (tools == null) { | ||
return { tools: void 0, tool_choice: void 0, toolWarnings }; | ||
} | ||
const toolChoice = mode.toolChoice; | ||
if (useLegacyFunctionCalling) { | ||
const openaiFunctions = []; | ||
for (const tool of tools) { | ||
if (tool.type === "provider-defined") { | ||
toolWarnings.push({ type: "unsupported-tool", tool }); | ||
} else { | ||
openaiFunctions.push({ | ||
name: tool.name, | ||
description: tool.description, | ||
parameters: tool.parameters | ||
}); | ||
} | ||
} | ||
if (toolChoice == null) { | ||
return { | ||
functions: openaiFunctions, | ||
function_call: void 0, | ||
toolWarnings | ||
}; | ||
} | ||
const type2 = toolChoice.type; | ||
switch (type2) { | ||
case "auto": | ||
case "none": | ||
case void 0: | ||
return { | ||
functions: openaiFunctions, | ||
function_call: void 0, | ||
toolWarnings | ||
}; | ||
case "required": | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: "useLegacyFunctionCalling and toolChoice: required" | ||
}); | ||
default: | ||
return { | ||
functions: openaiFunctions, | ||
function_call: { name: toolChoice.toolName }, | ||
toolWarnings | ||
}; | ||
} | ||
} | ||
const openaiTools = []; | ||
for (const tool of tools) { | ||
if (tool.type === "provider-defined") { | ||
toolWarnings.push({ type: "unsupported-tool", tool }); | ||
} else { | ||
openaiTools.push({ | ||
type: "function", | ||
function: { | ||
name: tool.name, | ||
description: tool.description, | ||
parameters: tool.parameters, | ||
strict: structuredOutputs === true ? true : void 0 | ||
} | ||
}); | ||
} | ||
} | ||
if (toolChoice == null) { | ||
return { tools: openaiTools, tool_choice: void 0, toolWarnings }; | ||
} | ||
const type = toolChoice.type; | ||
switch (type) { | ||
case "auto": | ||
case "none": | ||
case "required": | ||
return { tools: openaiTools, tool_choice: type, toolWarnings }; | ||
case "tool": | ||
return { | ||
tools: openaiTools, | ||
tool_choice: { | ||
type: "function", | ||
function: { | ||
name: toolChoice.toolName | ||
} | ||
}, | ||
toolWarnings | ||
}; | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
// src/openai-chat-language-model.ts | ||
@@ -295,3 +394,3 @@ var OpenAIChatLanguageModel = class { | ||
if (useLegacyFunctionCalling && this.settings.parallelToolCalls === true) { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
functionality: "useLegacyFunctionCalling with parallelToolCalls" | ||
@@ -301,3 +400,3 @@ }); | ||
if (useLegacyFunctionCalling && this.settings.structuredOutputs === true) { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
functionality: "structuredOutputs with useLegacyFunctionCalling" | ||
@@ -343,12 +442,16 @@ }); | ||
case "regular": { | ||
const { tools, tool_choice, functions, function_call, toolWarnings } = prepareTools({ | ||
mode, | ||
useLegacyFunctionCalling, | ||
structuredOutputs: this.settings.structuredOutputs | ||
}); | ||
return { | ||
args: { | ||
...baseArgs, | ||
...prepareToolsAndToolChoice({ | ||
mode, | ||
useLegacyFunctionCalling, | ||
structuredOutputs: this.settings.structuredOutputs | ||
}) | ||
tools, | ||
tool_choice, | ||
functions, | ||
function_call | ||
}, | ||
warnings | ||
warnings: [...warnings, ...toolWarnings] | ||
}; | ||
@@ -416,3 +519,3 @@ } | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r; | ||
const { args, warnings } = this.getArgs(options); | ||
const { args: body, warnings } = this.getArgs(options); | ||
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({ | ||
@@ -424,3 +527,3 @@ url: this.config.url({ | ||
headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers), | ||
body: args, | ||
body, | ||
failedResponseHandler: openaiFailedResponseHandler, | ||
@@ -433,3 +536,3 @@ successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)( | ||
}); | ||
const { messages: rawPrompt, ...rawSettings } = args; | ||
const { messages: rawPrompt, ...rawSettings } = body; | ||
const choice = response.choices[0]; | ||
@@ -471,2 +574,3 @@ let providerMetadata; | ||
rawResponse: { headers: responseHeaders }, | ||
request: { body: JSON.stringify(body) }, | ||
response: getResponseMetadata(response), | ||
@@ -516,2 +620,8 @@ warnings, | ||
const { args, warnings } = this.getArgs(options); | ||
const body = { | ||
...args, | ||
stream: true, | ||
// only include stream_options when in strict compatibility mode: | ||
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0 | ||
}; | ||
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({ | ||
@@ -523,8 +633,3 @@ url: this.config.url({ | ||
headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers), | ||
body: { | ||
...args, | ||
stream: true, | ||
// only include stream_options when in strict compatibility mode: | ||
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0 | ||
}, | ||
body, | ||
failedResponseHandler: openaiFailedResponseHandler, | ||
@@ -618,3 +723,3 @@ successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)( | ||
if (toolCallDelta.type !== "function") { | ||
throw new import_provider2.InvalidResponseDataError({ | ||
throw new import_provider3.InvalidResponseDataError({ | ||
data: toolCallDelta, | ||
@@ -625,3 +730,3 @@ message: `Expected 'function' type.` | ||
if (toolCallDelta.id == null) { | ||
throw new import_provider2.InvalidResponseDataError({ | ||
throw new import_provider3.InvalidResponseDataError({ | ||
data: toolCallDelta, | ||
@@ -632,3 +737,3 @@ message: `Expected 'id' to be a string.` | ||
if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) { | ||
throw new import_provider2.InvalidResponseDataError({ | ||
throw new import_provider3.InvalidResponseDataError({ | ||
data: toolCallDelta, | ||
@@ -709,2 +814,3 @@ message: `Expected 'function.name' to be a string.` | ||
rawResponse: { headers: responseHeaders }, | ||
request: { body: JSON.stringify(body) }, | ||
warnings | ||
@@ -816,76 +922,2 @@ }; | ||
]); | ||
function prepareToolsAndToolChoice({ | ||
mode, | ||
useLegacyFunctionCalling = false, | ||
structuredOutputs = false | ||
}) { | ||
var _a; | ||
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0; | ||
if (tools == null) { | ||
return { tools: void 0, tool_choice: void 0 }; | ||
} | ||
const toolChoice = mode.toolChoice; | ||
if (useLegacyFunctionCalling) { | ||
const mappedFunctions = tools.map((tool) => ({ | ||
name: tool.name, | ||
description: tool.description, | ||
parameters: tool.parameters | ||
})); | ||
if (toolChoice == null) { | ||
return { functions: mappedFunctions, function_call: void 0 }; | ||
} | ||
const type2 = toolChoice.type; | ||
switch (type2) { | ||
case "auto": | ||
case "none": | ||
case void 0: | ||
return { | ||
functions: mappedFunctions, | ||
function_call: void 0 | ||
}; | ||
case "required": | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: "useLegacyFunctionCalling and toolChoice: required" | ||
}); | ||
default: | ||
return { | ||
functions: mappedFunctions, | ||
function_call: { name: toolChoice.toolName } | ||
}; | ||
} | ||
} | ||
const mappedTools = tools.map((tool) => ({ | ||
type: "function", | ||
function: { | ||
name: tool.name, | ||
description: tool.description, | ||
parameters: tool.parameters, | ||
strict: structuredOutputs === true ? true : void 0 | ||
} | ||
})); | ||
if (toolChoice == null) { | ||
return { tools: mappedTools, tool_choice: void 0 }; | ||
} | ||
const type = toolChoice.type; | ||
switch (type) { | ||
case "auto": | ||
case "none": | ||
case "required": | ||
return { tools: mappedTools, tool_choice: type }; | ||
case "tool": | ||
return { | ||
tools: mappedTools, | ||
tool_choice: { | ||
type: "function", | ||
function: { | ||
name: toolChoice.toolName | ||
} | ||
} | ||
}; | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
function isReasoningModel(modelId) { | ||
@@ -899,3 +931,3 @@ return modelId.startsWith("o1-"); | ||
// src/openai-completion-language-model.ts | ||
var import_provider4 = require("@ai-sdk/provider"); | ||
var import_provider5 = require("@ai-sdk/provider"); | ||
var import_provider_utils4 = require("@ai-sdk/provider-utils"); | ||
@@ -905,3 +937,3 @@ var import_zod3 = require("zod"); | ||
// src/convert-to-openai-completion-prompt.ts | ||
var import_provider3 = require("@ai-sdk/provider"); | ||
var import_provider4 = require("@ai-sdk/provider"); | ||
function convertToOpenAICompletionPrompt({ | ||
@@ -926,3 +958,3 @@ prompt, | ||
case "system": { | ||
throw new import_provider3.InvalidPromptError({ | ||
throw new import_provider4.InvalidPromptError({ | ||
message: "Unexpected system message in prompt: ${content}", | ||
@@ -939,3 +971,3 @@ prompt | ||
case "image": { | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
functionality: "images" | ||
@@ -959,3 +991,3 @@ }); | ||
case "tool-call": { | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
functionality: "tool-call messages" | ||
@@ -973,3 +1005,3 @@ }); | ||
case "tool": { | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
functionality: "tool messages" | ||
@@ -1075,3 +1107,3 @@ }); | ||
if ((_a = mode.tools) == null ? void 0 : _a.length) { | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
throw new import_provider5.UnsupportedFunctionalityError({ | ||
functionality: "tools" | ||
@@ -1081,3 +1113,3 @@ }); | ||
if (mode.toolChoice) { | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
throw new import_provider5.UnsupportedFunctionalityError({ | ||
functionality: "toolChoice" | ||
@@ -1089,3 +1121,3 @@ }); | ||
case "object-json": { | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
throw new import_provider5.UnsupportedFunctionalityError({ | ||
functionality: "object-json mode" | ||
@@ -1095,3 +1127,3 @@ }); | ||
case "object-tool": { | ||
throw new import_provider4.UnsupportedFunctionalityError({ | ||
throw new import_provider5.UnsupportedFunctionalityError({ | ||
functionality: "object-tool mode" | ||
@@ -1135,3 +1167,4 @@ }); | ||
response: getResponseMetadata(response), | ||
warnings | ||
warnings, | ||
request: { body: JSON.stringify(args) } | ||
}; | ||
@@ -1141,2 +1174,8 @@ } | ||
const { args, warnings } = this.getArgs(options); | ||
const body = { | ||
...args, | ||
stream: true, | ||
// only include stream_options when in strict compatibility mode: | ||
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0 | ||
}; | ||
const { responseHeaders, value: response } = await (0, import_provider_utils4.postJsonToApi)({ | ||
@@ -1148,8 +1187,3 @@ url: this.config.url({ | ||
headers: (0, import_provider_utils4.combineHeaders)(this.config.headers(), options.headers), | ||
body: { | ||
...args, | ||
stream: true, | ||
// only include stream_options when in strict compatibility mode: | ||
stream_options: this.config.compatibility === "strict" ? { include_usage: true } : void 0 | ||
}, | ||
body, | ||
failedResponseHandler: openaiFailedResponseHandler, | ||
@@ -1228,3 +1262,4 @@ successfulResponseHandler: (0, import_provider_utils4.createEventSourceResponseHandler)( | ||
rawResponse: { headers: responseHeaders }, | ||
warnings | ||
warnings, | ||
request: { body: JSON.stringify(body) } | ||
}; | ||
@@ -1279,3 +1314,3 @@ } | ||
// src/openai-embedding-model.ts | ||
var import_provider5 = require("@ai-sdk/provider"); | ||
var import_provider6 = require("@ai-sdk/provider"); | ||
var import_provider_utils5 = require("@ai-sdk/provider-utils"); | ||
@@ -1307,3 +1342,3 @@ var import_zod4 = require("zod"); | ||
if (values.length > this.maxEmbeddingsPerCall) { | ||
throw new import_provider5.TooManyEmbeddingValuesForCallError({ | ||
throw new import_provider6.TooManyEmbeddingValuesForCallError({ | ||
provider: this.provider, | ||
@@ -1310,0 +1345,0 @@ modelId: this.modelId, |
{ | ||
"name": "@ai-sdk/openai", | ||
"version": "0.0.68", | ||
"version": "0.0.70", | ||
"license": "Apache-2.0", | ||
@@ -29,4 +29,4 @@ "sideEffects": false, | ||
"dependencies": { | ||
"@ai-sdk/provider": "0.0.24", | ||
"@ai-sdk/provider-utils": "1.0.20" | ||
"@ai-sdk/provider": "0.0.26", | ||
"@ai-sdk/provider-utils": "1.0.22" | ||
}, | ||
@@ -33,0 +33,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
647551
7504
+ Added@ai-sdk/provider@0.0.26(transitive)
+ Added@ai-sdk/provider-utils@1.0.22(transitive)
+ Addednanoid@3.3.7(transitive)
- Removed@ai-sdk/provider@0.0.24(transitive)
- Removed@ai-sdk/provider-utils@1.0.20(transitive)
- Removednanoid@3.3.6(transitive)
Updated@ai-sdk/provider@0.0.26