@ai-sdk/openai 3.0.81 → 3.0.82
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 +13 -0
- package/dist/index.d.mts +16 -9
- package/dist/index.d.ts +16 -9
- package/dist/index.js +276 -91
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +254 -70
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +16 -9
- package/dist/internal/index.d.ts +16 -9
- package/dist/internal/index.js +275 -90
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +253 -69
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/chat/convert-to-openai-chat-messages.ts +1 -1
- package/src/chat/openai-chat-language-model.ts +56 -44
- package/src/completion/openai-completion-language-model.ts +26 -6
- package/src/openai-stream-error.ts +181 -0
- package/src/responses/convert-to-openai-responses-input.ts +2 -2
- package/src/responses/openai-responses-api.ts +27 -10
- package/src/responses/openai-responses-language-model.ts +46 -2
package/dist/index.js
CHANGED
|
@@ -30,7 +30,7 @@ module.exports = __toCommonJS(index_exports);
|
|
|
30
30
|
var import_provider_utils36 = require("@ai-sdk/provider-utils");
|
|
31
31
|
|
|
32
32
|
// src/chat/openai-chat-language-model.ts
|
|
33
|
-
var
|
|
33
|
+
var import_provider4 = require("@ai-sdk/provider");
|
|
34
34
|
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
|
35
35
|
|
|
36
36
|
// src/openai-error.ts
|
|
@@ -68,6 +68,129 @@ function getOpenAILanguageModelCapabilities(modelId) {
|
|
|
68
68
|
};
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
// src/openai-stream-error.ts
|
|
72
|
+
var import_provider = require("@ai-sdk/provider");
|
|
73
|
+
async function throwIfOpenAIStreamErrorBeforeOutput({
|
|
74
|
+
stream,
|
|
75
|
+
getError,
|
|
76
|
+
isOutputChunk,
|
|
77
|
+
url,
|
|
78
|
+
requestBodyValues,
|
|
79
|
+
responseHeaders
|
|
80
|
+
}) {
|
|
81
|
+
const [streamForEarlyError, streamForConsumer] = stream.tee();
|
|
82
|
+
const reader = streamForEarlyError.getReader();
|
|
83
|
+
try {
|
|
84
|
+
while (true) {
|
|
85
|
+
const result = await reader.read();
|
|
86
|
+
if (result.done) {
|
|
87
|
+
return streamForConsumer;
|
|
88
|
+
}
|
|
89
|
+
const chunk = result.value;
|
|
90
|
+
if (!chunk.success) {
|
|
91
|
+
return streamForConsumer;
|
|
92
|
+
}
|
|
93
|
+
const errorFrame = getError(chunk.value);
|
|
94
|
+
if (errorFrame != null) {
|
|
95
|
+
streamForConsumer.cancel().catch(() => {
|
|
96
|
+
});
|
|
97
|
+
throw createOpenAIStreamError({
|
|
98
|
+
frame: errorFrame,
|
|
99
|
+
url,
|
|
100
|
+
requestBodyValues,
|
|
101
|
+
responseHeaders
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
if (isOutputChunk(chunk.value)) {
|
|
105
|
+
return streamForConsumer;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
} finally {
|
|
109
|
+
reader.cancel().catch(() => {
|
|
110
|
+
});
|
|
111
|
+
reader.releaseLock();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function createOpenAIStreamError({
|
|
115
|
+
frame,
|
|
116
|
+
url,
|
|
117
|
+
requestBodyValues,
|
|
118
|
+
responseHeaders
|
|
119
|
+
}) {
|
|
120
|
+
var _a;
|
|
121
|
+
const streamError = parseStreamError(frame);
|
|
122
|
+
return new import_provider.APICallError({
|
|
123
|
+
message: (_a = streamError == null ? void 0 : streamError.message) != null ? _a : "OpenAI stream failed before any output was generated",
|
|
124
|
+
url,
|
|
125
|
+
requestBodyValues,
|
|
126
|
+
statusCode: streamError == null ? 500 : getStatusCode(streamError),
|
|
127
|
+
responseHeaders,
|
|
128
|
+
responseBody: JSON.stringify(frame),
|
|
129
|
+
data: frame
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
function parseStreamError(frame) {
|
|
133
|
+
var _a;
|
|
134
|
+
const value = asRecord(frame);
|
|
135
|
+
if (value == null) {
|
|
136
|
+
return void 0;
|
|
137
|
+
}
|
|
138
|
+
if (value.type === "response.failed") {
|
|
139
|
+
const response = asRecord(value.response);
|
|
140
|
+
const responseError = asRecord(response == null ? void 0 : response.error);
|
|
141
|
+
return typeof (responseError == null ? void 0 : responseError.message) === "string" ? {
|
|
142
|
+
message: responseError.message,
|
|
143
|
+
code: getStringOrNumber(responseError.code),
|
|
144
|
+
type: "response.failed",
|
|
145
|
+
frame
|
|
146
|
+
} : void 0;
|
|
147
|
+
}
|
|
148
|
+
const error = (_a = asRecord(value.error)) != null ? _a : value;
|
|
149
|
+
return typeof error.message === "string" && (asRecord(value.error) != null || typeof error.type === "string" || "code" in error || "param" in error) ? {
|
|
150
|
+
message: error.message,
|
|
151
|
+
code: getStringOrNumber(error.code),
|
|
152
|
+
type: typeof error.type === "string" ? error.type : void 0,
|
|
153
|
+
frame
|
|
154
|
+
} : void 0;
|
|
155
|
+
}
|
|
156
|
+
function getStatusCode(error) {
|
|
157
|
+
if (typeof error.code === "number" && isHttpErrorStatusCode(error.code)) {
|
|
158
|
+
return error.code;
|
|
159
|
+
}
|
|
160
|
+
if (typeof error.code === "string" && /^\d{3}$/.test(error.code)) {
|
|
161
|
+
const numericCode = Number(error.code);
|
|
162
|
+
if (isHttpErrorStatusCode(numericCode)) {
|
|
163
|
+
return numericCode;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const discriminator = [error.code, error.type].filter((value) => typeof value === "string" || typeof value === "number").join(" ").toLowerCase();
|
|
167
|
+
if (["insufficient_quota", "rate_limit"].some(
|
|
168
|
+
(term) => discriminator.includes(term)
|
|
169
|
+
)) {
|
|
170
|
+
return 429;
|
|
171
|
+
}
|
|
172
|
+
if (discriminator.includes("authentication")) return 401;
|
|
173
|
+
if (discriminator.includes("permission")) return 403;
|
|
174
|
+
if (discriminator.includes("not_found")) return 404;
|
|
175
|
+
if (["invalid", "bad_request", "context_length"].some(
|
|
176
|
+
(term) => discriminator.includes(term)
|
|
177
|
+
)) {
|
|
178
|
+
return 400;
|
|
179
|
+
}
|
|
180
|
+
if (discriminator.includes("overload")) return 503;
|
|
181
|
+
if (discriminator.includes("timeout")) return 504;
|
|
182
|
+
return 500;
|
|
183
|
+
}
|
|
184
|
+
function asRecord(value) {
|
|
185
|
+
return typeof value === "object" && value != null ? value : void 0;
|
|
186
|
+
}
|
|
187
|
+
function getStringOrNumber(value) {
|
|
188
|
+
return typeof value === "string" || typeof value === "number" ? value : void 0;
|
|
189
|
+
}
|
|
190
|
+
function isHttpErrorStatusCode(value) {
|
|
191
|
+
return Number.isInteger(value) && value >= 400 && value <= 599;
|
|
192
|
+
}
|
|
193
|
+
|
|
71
194
|
// src/chat/convert-openai-chat-usage.ts
|
|
72
195
|
function convertOpenAIChatUsage(usage) {
|
|
73
196
|
var _a, _b, _c, _d, _e, _f;
|
|
@@ -108,7 +231,7 @@ function convertOpenAIChatUsage(usage) {
|
|
|
108
231
|
}
|
|
109
232
|
|
|
110
233
|
// src/chat/convert-to-openai-chat-messages.ts
|
|
111
|
-
var
|
|
234
|
+
var import_provider2 = require("@ai-sdk/provider");
|
|
112
235
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
113
236
|
function serializeToolCallArguments(input) {
|
|
114
237
|
return JSON.stringify(input === void 0 ? {} : input);
|
|
@@ -174,7 +297,7 @@ function convertToOpenAIChatMessages({
|
|
|
174
297
|
};
|
|
175
298
|
} else if (part.mediaType.startsWith("audio/")) {
|
|
176
299
|
if (part.data instanceof URL) {
|
|
177
|
-
throw new
|
|
300
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
178
301
|
functionality: "audio file parts with URLs"
|
|
179
302
|
});
|
|
180
303
|
}
|
|
@@ -199,14 +322,14 @@ function convertToOpenAIChatMessages({
|
|
|
199
322
|
};
|
|
200
323
|
}
|
|
201
324
|
default: {
|
|
202
|
-
throw new
|
|
325
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
203
326
|
functionality: `audio content parts with media type ${part.mediaType}`
|
|
204
327
|
});
|
|
205
328
|
}
|
|
206
329
|
}
|
|
207
330
|
} else if (part.mediaType === "application/pdf") {
|
|
208
331
|
if (part.data instanceof URL) {
|
|
209
|
-
throw new
|
|
332
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
210
333
|
functionality: "PDF file parts with URLs"
|
|
211
334
|
});
|
|
212
335
|
}
|
|
@@ -218,7 +341,7 @@ function convertToOpenAIChatMessages({
|
|
|
218
341
|
}
|
|
219
342
|
};
|
|
220
343
|
} else {
|
|
221
|
-
throw new
|
|
344
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
222
345
|
functionality: `file part media type ${part.mediaType}`
|
|
223
346
|
});
|
|
224
347
|
}
|
|
@@ -270,7 +393,7 @@ function convertToOpenAIChatMessages({
|
|
|
270
393
|
contentValue = output.value;
|
|
271
394
|
break;
|
|
272
395
|
case "execution-denied":
|
|
273
|
-
contentValue = (_a = output.reason) != null ? _a : "Tool execution denied.";
|
|
396
|
+
contentValue = (_a = output.reason) != null ? _a : "Tool call execution denied.";
|
|
274
397
|
break;
|
|
275
398
|
case "content":
|
|
276
399
|
case "json":
|
|
@@ -587,7 +710,7 @@ var openaiLanguageModelChatOptions = (0, import_provider_utils4.lazySchema)(
|
|
|
587
710
|
);
|
|
588
711
|
|
|
589
712
|
// src/chat/openai-chat-prepare-tools.ts
|
|
590
|
-
var
|
|
713
|
+
var import_provider3 = require("@ai-sdk/provider");
|
|
591
714
|
function prepareChatTools({
|
|
592
715
|
tools,
|
|
593
716
|
toolChoice
|
|
@@ -641,7 +764,7 @@ function prepareChatTools({
|
|
|
641
764
|
};
|
|
642
765
|
default: {
|
|
643
766
|
const _exhaustiveCheck = type;
|
|
644
|
-
throw new
|
|
767
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
645
768
|
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
646
769
|
});
|
|
647
770
|
}
|
|
@@ -924,11 +1047,12 @@ var OpenAIChatLanguageModel = class {
|
|
|
924
1047
|
include_usage: true
|
|
925
1048
|
}
|
|
926
1049
|
};
|
|
1050
|
+
const url = this.config.url({
|
|
1051
|
+
path: "/chat/completions",
|
|
1052
|
+
modelId: this.modelId
|
|
1053
|
+
});
|
|
927
1054
|
const { responseHeaders, value: response } = await (0, import_provider_utils5.postJsonToApi)({
|
|
928
|
-
url
|
|
929
|
-
path: "/chat/completions",
|
|
930
|
-
modelId: this.modelId
|
|
931
|
-
}),
|
|
1055
|
+
url,
|
|
932
1056
|
headers: (0, import_provider_utils5.combineHeaders)(this.config.headers(), options.headers),
|
|
933
1057
|
body,
|
|
934
1058
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
@@ -938,6 +1062,14 @@ var OpenAIChatLanguageModel = class {
|
|
|
938
1062
|
abortSignal: options.abortSignal,
|
|
939
1063
|
fetch: this.config.fetch
|
|
940
1064
|
});
|
|
1065
|
+
const checkedResponse = await throwIfOpenAIStreamErrorBeforeOutput({
|
|
1066
|
+
stream: response,
|
|
1067
|
+
getError: (chunk) => "error" in chunk ? chunk.error : void 0,
|
|
1068
|
+
isOutputChunk: isOpenAIChatOutputChunk,
|
|
1069
|
+
url,
|
|
1070
|
+
requestBodyValues: body,
|
|
1071
|
+
responseHeaders
|
|
1072
|
+
});
|
|
941
1073
|
const toolCalls = [];
|
|
942
1074
|
let finishReason = {
|
|
943
1075
|
unified: "other",
|
|
@@ -947,14 +1079,14 @@ var OpenAIChatLanguageModel = class {
|
|
|
947
1079
|
let metadataExtracted = false;
|
|
948
1080
|
let isActiveText = false;
|
|
949
1081
|
const providerMetadata = { openai: {} };
|
|
950
|
-
|
|
951
|
-
stream:
|
|
1082
|
+
const result = {
|
|
1083
|
+
stream: checkedResponse.pipeThrough(
|
|
952
1084
|
new TransformStream({
|
|
953
1085
|
start(controller) {
|
|
954
1086
|
controller.enqueue({ type: "stream-start", warnings });
|
|
955
1087
|
},
|
|
956
1088
|
transform(chunk, controller) {
|
|
957
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m
|
|
1089
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
958
1090
|
if (options.includeRawChunks) {
|
|
959
1091
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
960
1092
|
}
|
|
@@ -1018,19 +1150,19 @@ var OpenAIChatLanguageModel = class {
|
|
|
1018
1150
|
const index = toolCallDelta.index;
|
|
1019
1151
|
if (toolCalls[index] == null) {
|
|
1020
1152
|
if (toolCallDelta.type != null && toolCallDelta.type !== "function") {
|
|
1021
|
-
throw new
|
|
1153
|
+
throw new import_provider4.InvalidResponseDataError({
|
|
1022
1154
|
data: toolCallDelta,
|
|
1023
1155
|
message: `Expected 'function' type.`
|
|
1024
1156
|
});
|
|
1025
1157
|
}
|
|
1026
1158
|
if (toolCallDelta.id == null) {
|
|
1027
|
-
throw new
|
|
1159
|
+
throw new import_provider4.InvalidResponseDataError({
|
|
1028
1160
|
data: toolCallDelta,
|
|
1029
1161
|
message: `Expected 'id' to be a string.`
|
|
1030
1162
|
});
|
|
1031
1163
|
}
|
|
1032
1164
|
if (((_f = toolCallDelta.function) == null ? void 0 : _f.name) == null) {
|
|
1033
|
-
throw new
|
|
1165
|
+
throw new import_provider4.InvalidResponseDataError({
|
|
1034
1166
|
data: toolCallDelta,
|
|
1035
1167
|
message: `Expected 'function.name' to be a string.`
|
|
1036
1168
|
});
|
|
@@ -1058,19 +1190,6 @@ var OpenAIChatLanguageModel = class {
|
|
|
1058
1190
|
delta: toolCall2.function.arguments
|
|
1059
1191
|
});
|
|
1060
1192
|
}
|
|
1061
|
-
if ((0, import_provider_utils5.isParsableJson)(toolCall2.function.arguments)) {
|
|
1062
|
-
controller.enqueue({
|
|
1063
|
-
type: "tool-input-end",
|
|
1064
|
-
id: toolCall2.id
|
|
1065
|
-
});
|
|
1066
|
-
controller.enqueue({
|
|
1067
|
-
type: "tool-call",
|
|
1068
|
-
toolCallId: (_j = toolCall2.id) != null ? _j : (0, import_provider_utils5.generateId)(),
|
|
1069
|
-
toolName: toolCall2.function.name,
|
|
1070
|
-
input: toolCall2.function.arguments
|
|
1071
|
-
});
|
|
1072
|
-
toolCall2.hasFinished = true;
|
|
1073
|
-
}
|
|
1074
1193
|
}
|
|
1075
1194
|
continue;
|
|
1076
1195
|
}
|
|
@@ -1078,27 +1197,14 @@ var OpenAIChatLanguageModel = class {
|
|
|
1078
1197
|
if (toolCall.hasFinished) {
|
|
1079
1198
|
continue;
|
|
1080
1199
|
}
|
|
1081
|
-
if (((
|
|
1082
|
-
toolCall.function.arguments += (
|
|
1200
|
+
if (((_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null) {
|
|
1201
|
+
toolCall.function.arguments += (_l = (_k = toolCallDelta.function) == null ? void 0 : _k.arguments) != null ? _l : "";
|
|
1083
1202
|
}
|
|
1084
1203
|
controller.enqueue({
|
|
1085
1204
|
type: "tool-input-delta",
|
|
1086
1205
|
id: toolCall.id,
|
|
1087
|
-
delta: (
|
|
1206
|
+
delta: (_m = toolCallDelta.function.arguments) != null ? _m : ""
|
|
1088
1207
|
});
|
|
1089
|
-
if (((_o = toolCall.function) == null ? void 0 : _o.name) != null && ((_p = toolCall.function) == null ? void 0 : _p.arguments) != null && (0, import_provider_utils5.isParsableJson)(toolCall.function.arguments)) {
|
|
1090
|
-
controller.enqueue({
|
|
1091
|
-
type: "tool-input-end",
|
|
1092
|
-
id: toolCall.id
|
|
1093
|
-
});
|
|
1094
|
-
controller.enqueue({
|
|
1095
|
-
type: "tool-call",
|
|
1096
|
-
toolCallId: (_q = toolCall.id) != null ? _q : (0, import_provider_utils5.generateId)(),
|
|
1097
|
-
toolName: toolCall.function.name,
|
|
1098
|
-
input: toolCall.function.arguments
|
|
1099
|
-
});
|
|
1100
|
-
toolCall.hasFinished = true;
|
|
1101
|
-
}
|
|
1102
1208
|
}
|
|
1103
1209
|
}
|
|
1104
1210
|
if (delta.annotations != null) {
|
|
@@ -1114,9 +1220,25 @@ var OpenAIChatLanguageModel = class {
|
|
|
1114
1220
|
}
|
|
1115
1221
|
},
|
|
1116
1222
|
flush(controller) {
|
|
1223
|
+
var _a;
|
|
1117
1224
|
if (isActiveText) {
|
|
1118
1225
|
controller.enqueue({ type: "text-end", id: "0" });
|
|
1119
1226
|
}
|
|
1227
|
+
for (const toolCall of toolCalls) {
|
|
1228
|
+
if (!toolCall.hasFinished) {
|
|
1229
|
+
controller.enqueue({
|
|
1230
|
+
type: "tool-input-end",
|
|
1231
|
+
id: toolCall.id
|
|
1232
|
+
});
|
|
1233
|
+
controller.enqueue({
|
|
1234
|
+
type: "tool-call",
|
|
1235
|
+
toolCallId: (_a = toolCall.id) != null ? _a : (0, import_provider_utils5.generateId)(),
|
|
1236
|
+
toolName: toolCall.function.name,
|
|
1237
|
+
input: toolCall.function.arguments
|
|
1238
|
+
});
|
|
1239
|
+
toolCall.hasFinished = true;
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1120
1242
|
controller.enqueue({
|
|
1121
1243
|
type: "finish",
|
|
1122
1244
|
finishReason,
|
|
@@ -1129,8 +1251,18 @@ var OpenAIChatLanguageModel = class {
|
|
|
1129
1251
|
request: { body },
|
|
1130
1252
|
response: { headers: responseHeaders }
|
|
1131
1253
|
};
|
|
1254
|
+
return result;
|
|
1132
1255
|
}
|
|
1133
1256
|
};
|
|
1257
|
+
function isOpenAIChatOutputChunk(chunk) {
|
|
1258
|
+
if ("error" in chunk) {
|
|
1259
|
+
return false;
|
|
1260
|
+
}
|
|
1261
|
+
return chunk.choices.some((choice) => {
|
|
1262
|
+
const delta = choice.delta;
|
|
1263
|
+
return (delta == null ? void 0 : delta.content) != null && delta.content.length > 0 || (delta == null ? void 0 : delta.tool_calls) != null && delta.tool_calls.length > 0 || (delta == null ? void 0 : delta.annotations) != null && delta.annotations.length > 0;
|
|
1264
|
+
});
|
|
1265
|
+
}
|
|
1134
1266
|
|
|
1135
1267
|
// src/completion/openai-completion-language-model.ts
|
|
1136
1268
|
var import_provider_utils8 = require("@ai-sdk/provider-utils");
|
|
@@ -1173,7 +1305,7 @@ function convertOpenAICompletionUsage(usage) {
|
|
|
1173
1305
|
}
|
|
1174
1306
|
|
|
1175
1307
|
// src/completion/convert-to-openai-completion-prompt.ts
|
|
1176
|
-
var
|
|
1308
|
+
var import_provider5 = require("@ai-sdk/provider");
|
|
1177
1309
|
function convertToOpenAICompletionPrompt({
|
|
1178
1310
|
prompt,
|
|
1179
1311
|
user = "user",
|
|
@@ -1189,7 +1321,7 @@ function convertToOpenAICompletionPrompt({
|
|
|
1189
1321
|
for (const { role, content } of prompt) {
|
|
1190
1322
|
switch (role) {
|
|
1191
1323
|
case "system": {
|
|
1192
|
-
throw new
|
|
1324
|
+
throw new import_provider5.InvalidPromptError({
|
|
1193
1325
|
message: "Unexpected system message in prompt: ${content}",
|
|
1194
1326
|
prompt
|
|
1195
1327
|
});
|
|
@@ -1215,7 +1347,7 @@ ${userMessage}
|
|
|
1215
1347
|
return part.text;
|
|
1216
1348
|
}
|
|
1217
1349
|
case "tool-call": {
|
|
1218
|
-
throw new
|
|
1350
|
+
throw new import_provider5.UnsupportedFunctionalityError({
|
|
1219
1351
|
functionality: "tool-call messages"
|
|
1220
1352
|
});
|
|
1221
1353
|
}
|
|
@@ -1228,7 +1360,7 @@ ${assistantMessage}
|
|
|
1228
1360
|
break;
|
|
1229
1361
|
}
|
|
1230
1362
|
case "tool": {
|
|
1231
|
-
throw new
|
|
1363
|
+
throw new import_provider5.UnsupportedFunctionalityError({
|
|
1232
1364
|
functionality: "tool messages"
|
|
1233
1365
|
});
|
|
1234
1366
|
}
|
|
@@ -1522,11 +1654,12 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1522
1654
|
include_usage: true
|
|
1523
1655
|
}
|
|
1524
1656
|
};
|
|
1657
|
+
const url = this.config.url({
|
|
1658
|
+
path: "/completions",
|
|
1659
|
+
modelId: this.modelId
|
|
1660
|
+
});
|
|
1525
1661
|
const { responseHeaders, value: response } = await (0, import_provider_utils8.postJsonToApi)({
|
|
1526
|
-
url
|
|
1527
|
-
path: "/completions",
|
|
1528
|
-
modelId: this.modelId
|
|
1529
|
-
}),
|
|
1662
|
+
url,
|
|
1530
1663
|
headers: (0, import_provider_utils8.combineHeaders)(this.config.headers(), options.headers),
|
|
1531
1664
|
body,
|
|
1532
1665
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
@@ -1536,6 +1669,14 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1536
1669
|
abortSignal: options.abortSignal,
|
|
1537
1670
|
fetch: this.config.fetch
|
|
1538
1671
|
});
|
|
1672
|
+
const checkedResponse = await throwIfOpenAIStreamErrorBeforeOutput({
|
|
1673
|
+
stream: response,
|
|
1674
|
+
getError: (chunk) => "error" in chunk ? chunk.error : void 0,
|
|
1675
|
+
isOutputChunk: isOpenAICompletionOutputChunk,
|
|
1676
|
+
url,
|
|
1677
|
+
requestBodyValues: body,
|
|
1678
|
+
responseHeaders
|
|
1679
|
+
});
|
|
1539
1680
|
let finishReason = {
|
|
1540
1681
|
unified: "other",
|
|
1541
1682
|
raw: void 0
|
|
@@ -1543,8 +1684,8 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1543
1684
|
const providerMetadata = { openai: {} };
|
|
1544
1685
|
let usage = void 0;
|
|
1545
1686
|
let isFirstChunk = true;
|
|
1546
|
-
|
|
1547
|
-
stream:
|
|
1687
|
+
const result = {
|
|
1688
|
+
stream: checkedResponse.pipeThrough(
|
|
1548
1689
|
new TransformStream({
|
|
1549
1690
|
start(controller) {
|
|
1550
1691
|
controller.enqueue({ type: "stream-start", warnings });
|
|
@@ -1609,11 +1750,15 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1609
1750
|
request: { body },
|
|
1610
1751
|
response: { headers: responseHeaders }
|
|
1611
1752
|
};
|
|
1753
|
+
return result;
|
|
1612
1754
|
}
|
|
1613
1755
|
};
|
|
1756
|
+
function isOpenAICompletionOutputChunk(chunk) {
|
|
1757
|
+
return !("error" in chunk) && chunk.choices.some((choice) => choice.text.length > 0);
|
|
1758
|
+
}
|
|
1614
1759
|
|
|
1615
1760
|
// src/embedding/openai-embedding-model.ts
|
|
1616
|
-
var
|
|
1761
|
+
var import_provider6 = require("@ai-sdk/provider");
|
|
1617
1762
|
var import_provider_utils11 = require("@ai-sdk/provider-utils");
|
|
1618
1763
|
|
|
1619
1764
|
// src/embedding/openai-embedding-options.ts
|
|
@@ -1668,7 +1813,7 @@ var OpenAIEmbeddingModel = class {
|
|
|
1668
1813
|
}) {
|
|
1669
1814
|
var _a;
|
|
1670
1815
|
if (values.length > this.maxEmbeddingsPerCall) {
|
|
1671
|
-
throw new
|
|
1816
|
+
throw new import_provider6.TooManyEmbeddingValuesForCallError({
|
|
1672
1817
|
provider: this.provider,
|
|
1673
1818
|
modelId: this.modelId,
|
|
1674
1819
|
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
@@ -2683,7 +2828,7 @@ var openaiTools = {
|
|
|
2683
2828
|
};
|
|
2684
2829
|
|
|
2685
2830
|
// src/responses/openai-responses-language-model.ts
|
|
2686
|
-
var
|
|
2831
|
+
var import_provider9 = require("@ai-sdk/provider");
|
|
2687
2832
|
var import_provider_utils30 = require("@ai-sdk/provider-utils");
|
|
2688
2833
|
|
|
2689
2834
|
// src/responses/convert-openai-responses-usage.ts
|
|
@@ -2726,7 +2871,7 @@ function convertOpenAIResponsesUsage(usage) {
|
|
|
2726
2871
|
}
|
|
2727
2872
|
|
|
2728
2873
|
// src/responses/convert-to-openai-responses-input.ts
|
|
2729
|
-
var
|
|
2874
|
+
var import_provider7 = require("@ai-sdk/provider");
|
|
2730
2875
|
var import_provider_utils26 = require("@ai-sdk/provider-utils");
|
|
2731
2876
|
var import_v421 = require("zod/v4");
|
|
2732
2877
|
function serializeToolCallArguments2(input) {
|
|
@@ -2810,7 +2955,7 @@ async function convertToOpenAIResponsesInput({
|
|
|
2810
2955
|
};
|
|
2811
2956
|
}
|
|
2812
2957
|
if (mediaType !== "application/pdf" && !passThroughUnsupportedFiles) {
|
|
2813
|
-
throw new
|
|
2958
|
+
throw new import_provider7.UnsupportedFunctionalityError({
|
|
2814
2959
|
functionality: `file part media type ${mediaType}`
|
|
2815
2960
|
});
|
|
2816
2961
|
}
|
|
@@ -3204,7 +3349,7 @@ async function convertToOpenAIResponsesInput({
|
|
|
3204
3349
|
outputValue = output.value;
|
|
3205
3350
|
break;
|
|
3206
3351
|
case "execution-denied":
|
|
3207
|
-
outputValue = (_v = output.reason) != null ? _v : "Tool execution denied.";
|
|
3352
|
+
outputValue = (_v = output.reason) != null ? _v : "Tool call execution denied.";
|
|
3208
3353
|
break;
|
|
3209
3354
|
case "json":
|
|
3210
3355
|
case "error-json":
|
|
@@ -3265,7 +3410,7 @@ async function convertToOpenAIResponsesInput({
|
|
|
3265
3410
|
contentValue = output.value;
|
|
3266
3411
|
break;
|
|
3267
3412
|
case "execution-denied":
|
|
3268
|
-
contentValue = (_w = output.reason) != null ? _w : "Tool execution denied.";
|
|
3413
|
+
contentValue = (_w = output.reason) != null ? _w : "Tool call execution denied.";
|
|
3269
3414
|
break;
|
|
3270
3415
|
case "json":
|
|
3271
3416
|
case "error-json":
|
|
@@ -3379,6 +3524,23 @@ var jsonValueSchema2 = import_v422.z.lazy(
|
|
|
3379
3524
|
import_v422.z.record(import_v422.z.string(), jsonValueSchema2.optional())
|
|
3380
3525
|
])
|
|
3381
3526
|
);
|
|
3527
|
+
var openaiResponsesNestedErrorChunkSchema = import_v422.z.object({
|
|
3528
|
+
type: import_v422.z.literal("error"),
|
|
3529
|
+
sequence_number: import_v422.z.number(),
|
|
3530
|
+
error: import_v422.z.object({
|
|
3531
|
+
type: import_v422.z.string(),
|
|
3532
|
+
code: import_v422.z.string(),
|
|
3533
|
+
message: import_v422.z.string(),
|
|
3534
|
+
param: import_v422.z.string().nullish()
|
|
3535
|
+
})
|
|
3536
|
+
});
|
|
3537
|
+
var openaiResponsesErrorChunkSchema = import_v422.z.object({
|
|
3538
|
+
type: import_v422.z.literal("error"),
|
|
3539
|
+
sequence_number: import_v422.z.number(),
|
|
3540
|
+
code: import_v422.z.string().nullish(),
|
|
3541
|
+
message: import_v422.z.string(),
|
|
3542
|
+
param: import_v422.z.string().nullish()
|
|
3543
|
+
});
|
|
3382
3544
|
var openaiResponsesChunkSchema = (0, import_provider_utils27.lazySchema)(
|
|
3383
3545
|
() => (0, import_provider_utils27.zodSchema)(
|
|
3384
3546
|
import_v422.z.union([
|
|
@@ -3421,6 +3583,7 @@ var openaiResponsesChunkSchema = (0, import_provider_utils27.lazySchema)(
|
|
|
3421
3583
|
}),
|
|
3422
3584
|
import_v422.z.object({
|
|
3423
3585
|
type: import_v422.z.literal("response.failed"),
|
|
3586
|
+
sequence_number: import_v422.z.number(),
|
|
3424
3587
|
response: import_v422.z.object({
|
|
3425
3588
|
error: import_v422.z.object({
|
|
3426
3589
|
code: import_v422.z.string().nullish(),
|
|
@@ -3909,16 +4072,8 @@ var openaiResponsesChunkSchema = (0, import_provider_utils27.lazySchema)(
|
|
|
3909
4072
|
output_index: import_v422.z.number(),
|
|
3910
4073
|
diff: import_v422.z.string()
|
|
3911
4074
|
}),
|
|
3912
|
-
|
|
3913
|
-
|
|
3914
|
-
sequence_number: import_v422.z.number(),
|
|
3915
|
-
error: import_v422.z.object({
|
|
3916
|
-
type: import_v422.z.string(),
|
|
3917
|
-
code: import_v422.z.string(),
|
|
3918
|
-
message: import_v422.z.string(),
|
|
3919
|
-
param: import_v422.z.string().nullish()
|
|
3920
|
-
})
|
|
3921
|
-
}),
|
|
4075
|
+
openaiResponsesNestedErrorChunkSchema,
|
|
4076
|
+
openaiResponsesErrorChunkSchema,
|
|
3922
4077
|
import_v422.z.object({ type: import_v422.z.string() }).loose().transform((value) => ({
|
|
3923
4078
|
type: "unknown_chunk",
|
|
3924
4079
|
message: value.type
|
|
@@ -4485,7 +4640,7 @@ var openaiLanguageModelResponsesOptionsSchema = (0, import_provider_utils28.lazy
|
|
|
4485
4640
|
);
|
|
4486
4641
|
|
|
4487
4642
|
// src/responses/openai-responses-prepare-tools.ts
|
|
4488
|
-
var
|
|
4643
|
+
var import_provider8 = require("@ai-sdk/provider");
|
|
4489
4644
|
var import_provider_utils29 = require("@ai-sdk/provider-utils");
|
|
4490
4645
|
async function prepareResponsesTools({
|
|
4491
4646
|
tools,
|
|
@@ -4526,7 +4681,7 @@ async function prepareResponsesTools({
|
|
|
4526
4681
|
namespaceTools.set(namespace.name, namespaceTool);
|
|
4527
4682
|
openaiTools2.push(namespaceTool);
|
|
4528
4683
|
} else if (namespaceTool.description !== namespace.description) {
|
|
4529
|
-
throw new
|
|
4684
|
+
throw new import_provider8.UnsupportedFunctionalityError({
|
|
4530
4685
|
functionality: `conflicting descriptions for OpenAI tool namespace "${namespace.name}"`
|
|
4531
4686
|
});
|
|
4532
4687
|
}
|
|
@@ -4738,7 +4893,7 @@ async function prepareResponsesTools({
|
|
|
4738
4893
|
}
|
|
4739
4894
|
default: {
|
|
4740
4895
|
const _exhaustiveCheck = type;
|
|
4741
|
-
throw new
|
|
4896
|
+
throw new import_provider8.UnsupportedFunctionalityError({
|
|
4742
4897
|
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
4743
4898
|
});
|
|
4744
4899
|
}
|
|
@@ -5111,7 +5266,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
5111
5266
|
fetch: this.config.fetch
|
|
5112
5267
|
});
|
|
5113
5268
|
if (response.error) {
|
|
5114
|
-
throw new
|
|
5269
|
+
throw new import_provider9.APICallError({
|
|
5115
5270
|
message: response.error.message,
|
|
5116
5271
|
url,
|
|
5117
5272
|
requestBodyValues: body,
|
|
@@ -5584,6 +5739,14 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
5584
5739
|
abortSignal: options.abortSignal,
|
|
5585
5740
|
fetch: this.config.fetch
|
|
5586
5741
|
});
|
|
5742
|
+
const checkedResponse = await throwIfOpenAIStreamErrorBeforeOutput({
|
|
5743
|
+
stream: response,
|
|
5744
|
+
getError: (chunk) => isErrorChunk(chunk) || isResponseFailedChunk(chunk) && chunk.response.error != null ? chunk : void 0,
|
|
5745
|
+
isOutputChunk: isResponseOutputChunk,
|
|
5746
|
+
url,
|
|
5747
|
+
requestBodyValues: body,
|
|
5748
|
+
responseHeaders
|
|
5749
|
+
});
|
|
5587
5750
|
const self = this;
|
|
5588
5751
|
const approvalRequestIdToDummyToolCallIdFromPrompt = extractApprovalRequestIdToToolCallIdMapping(options.prompt);
|
|
5589
5752
|
const approvalRequestIdToDummyToolCallIdFromStream = /* @__PURE__ */ new Map();
|
|
@@ -5601,8 +5764,9 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
5601
5764
|
const activeReasoning = {};
|
|
5602
5765
|
let serviceTier;
|
|
5603
5766
|
const hostedToolSearchCallIds = [];
|
|
5604
|
-
|
|
5605
|
-
|
|
5767
|
+
let encounteredStreamError = false;
|
|
5768
|
+
const result = {
|
|
5769
|
+
stream: checkedResponse.pipeThrough(
|
|
5606
5770
|
new TransformStream({
|
|
5607
5771
|
start(controller) {
|
|
5608
5772
|
controller.enqueue({ type: "stream-start", warnings });
|
|
@@ -5922,12 +6086,12 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
5922
6086
|
toolName: toolNameMapping.toCustomToolName("file_search"),
|
|
5923
6087
|
result: {
|
|
5924
6088
|
queries: value.item.queries,
|
|
5925
|
-
results: (_f = (_e = value.item.results) == null ? void 0 : _e.map((
|
|
5926
|
-
attributes:
|
|
5927
|
-
fileId:
|
|
5928
|
-
filename:
|
|
5929
|
-
score:
|
|
5930
|
-
text:
|
|
6089
|
+
results: (_f = (_e = value.item.results) == null ? void 0 : _e.map((result2) => ({
|
|
6090
|
+
attributes: result2.attributes,
|
|
6091
|
+
fileId: result2.file_id,
|
|
6092
|
+
filename: result2.filename,
|
|
6093
|
+
score: result2.score,
|
|
6094
|
+
text: result2.text
|
|
5931
6095
|
}))) != null ? _f : null
|
|
5932
6096
|
}
|
|
5933
6097
|
});
|
|
@@ -6360,6 +6524,21 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
6360
6524
|
raw: incompleteReason != null ? incompleteReason : "error"
|
|
6361
6525
|
};
|
|
6362
6526
|
usage = (_z = value.response.usage) != null ? _z : void 0;
|
|
6527
|
+
if (!encounteredStreamError && value.response.error != null) {
|
|
6528
|
+
encounteredStreamError = true;
|
|
6529
|
+
controller.enqueue({
|
|
6530
|
+
type: "error",
|
|
6531
|
+
error: {
|
|
6532
|
+
type: "response.failed",
|
|
6533
|
+
sequence_number: value.sequence_number,
|
|
6534
|
+
response: {
|
|
6535
|
+
error: value.response.error,
|
|
6536
|
+
incomplete_details: value.response.incomplete_details,
|
|
6537
|
+
service_tier: value.response.service_tier
|
|
6538
|
+
}
|
|
6539
|
+
}
|
|
6540
|
+
});
|
|
6541
|
+
}
|
|
6363
6542
|
} else if (isResponseAnnotationAddedChunk(value)) {
|
|
6364
6543
|
ongoingAnnotations.push(value.annotation);
|
|
6365
6544
|
if (value.annotation.type === "url_citation") {
|
|
@@ -6420,6 +6599,8 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
6420
6599
|
});
|
|
6421
6600
|
}
|
|
6422
6601
|
} else if (isErrorChunk(value)) {
|
|
6602
|
+
encounteredStreamError = true;
|
|
6603
|
+
finishReason = { unified: "error", raw: "error" };
|
|
6423
6604
|
controller.enqueue({ type: "error", error: value });
|
|
6424
6605
|
}
|
|
6425
6606
|
},
|
|
@@ -6443,13 +6624,14 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
6443
6624
|
request: { body },
|
|
6444
6625
|
response: { headers: responseHeaders }
|
|
6445
6626
|
};
|
|
6627
|
+
return result;
|
|
6446
6628
|
}
|
|
6447
6629
|
};
|
|
6448
6630
|
function isTextDeltaChunk(chunk) {
|
|
6449
6631
|
return chunk.type === "response.output_text.delta";
|
|
6450
6632
|
}
|
|
6451
6633
|
function isOpenAIChatCompletionChunk(value) {
|
|
6452
|
-
const chunk =
|
|
6634
|
+
const chunk = asRecord2(value);
|
|
6453
6635
|
return chunk != null && Array.isArray(chunk.choices) && typeof chunk.type !== "string";
|
|
6454
6636
|
}
|
|
6455
6637
|
function createOpenAIResponsesChatCompletionsMismatchError({
|
|
@@ -6459,7 +6641,7 @@ function createOpenAIResponsesChatCompletionsMismatchError({
|
|
|
6459
6641
|
requestBodyValues,
|
|
6460
6642
|
responseHeaders
|
|
6461
6643
|
}) {
|
|
6462
|
-
return new
|
|
6644
|
+
return new import_provider9.APICallError({
|
|
6463
6645
|
message: "Received a Chat Completions stream while using the OpenAI Responses API. The default OpenAI provider model uses the Responses API. If your custom baseURL targets a Chat Completions-compatible endpoint, use openai.chat('model-id') or createOpenAI(...).chat('model-id') instead. You can also use @ai-sdk/openai-compatible for OpenAI-compatible providers.",
|
|
6464
6646
|
url,
|
|
6465
6647
|
requestBodyValues,
|
|
@@ -6470,7 +6652,7 @@ function createOpenAIResponsesChatCompletionsMismatchError({
|
|
|
6470
6652
|
isRetryable: false
|
|
6471
6653
|
});
|
|
6472
6654
|
}
|
|
6473
|
-
function
|
|
6655
|
+
function asRecord2(value) {
|
|
6474
6656
|
return typeof value === "object" && value != null ? value : void 0;
|
|
6475
6657
|
}
|
|
6476
6658
|
function isResponseOutputItemDoneChunk(chunk) {
|
|
@@ -6515,6 +6697,9 @@ function isResponseAnnotationAddedChunk(chunk) {
|
|
|
6515
6697
|
function isErrorChunk(chunk) {
|
|
6516
6698
|
return chunk.type === "error";
|
|
6517
6699
|
}
|
|
6700
|
+
function isResponseOutputChunk(chunk) {
|
|
6701
|
+
return !(chunk.type === "response.created" || chunk.type === "response.failed" || chunk.type === "error" || chunk.type === "unknown_chunk");
|
|
6702
|
+
}
|
|
6518
6703
|
function mapWebSearchOutput(action) {
|
|
6519
6704
|
var _a;
|
|
6520
6705
|
if (action == null) {
|
|
@@ -6900,7 +7085,7 @@ var OpenAITranscriptionModel = class {
|
|
|
6900
7085
|
};
|
|
6901
7086
|
|
|
6902
7087
|
// src/version.ts
|
|
6903
|
-
var VERSION = true ? "3.0.
|
|
7088
|
+
var VERSION = true ? "3.0.82" : "0.0.0-test";
|
|
6904
7089
|
|
|
6905
7090
|
// src/openai-provider.ts
|
|
6906
7091
|
function createOpenAI(options = {}) {
|