@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/internal/index.js
CHANGED
|
@@ -64,7 +64,7 @@ __export(index_exports, {
|
|
|
64
64
|
module.exports = __toCommonJS(index_exports);
|
|
65
65
|
|
|
66
66
|
// src/chat/openai-chat-language-model.ts
|
|
67
|
-
var
|
|
67
|
+
var import_provider4 = require("@ai-sdk/provider");
|
|
68
68
|
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
|
69
69
|
|
|
70
70
|
// src/openai-error.ts
|
|
@@ -102,6 +102,129 @@ function getOpenAILanguageModelCapabilities(modelId) {
|
|
|
102
102
|
};
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
+
// src/openai-stream-error.ts
|
|
106
|
+
var import_provider = require("@ai-sdk/provider");
|
|
107
|
+
async function throwIfOpenAIStreamErrorBeforeOutput({
|
|
108
|
+
stream,
|
|
109
|
+
getError,
|
|
110
|
+
isOutputChunk,
|
|
111
|
+
url,
|
|
112
|
+
requestBodyValues,
|
|
113
|
+
responseHeaders
|
|
114
|
+
}) {
|
|
115
|
+
const [streamForEarlyError, streamForConsumer] = stream.tee();
|
|
116
|
+
const reader = streamForEarlyError.getReader();
|
|
117
|
+
try {
|
|
118
|
+
while (true) {
|
|
119
|
+
const result = await reader.read();
|
|
120
|
+
if (result.done) {
|
|
121
|
+
return streamForConsumer;
|
|
122
|
+
}
|
|
123
|
+
const chunk = result.value;
|
|
124
|
+
if (!chunk.success) {
|
|
125
|
+
return streamForConsumer;
|
|
126
|
+
}
|
|
127
|
+
const errorFrame = getError(chunk.value);
|
|
128
|
+
if (errorFrame != null) {
|
|
129
|
+
streamForConsumer.cancel().catch(() => {
|
|
130
|
+
});
|
|
131
|
+
throw createOpenAIStreamError({
|
|
132
|
+
frame: errorFrame,
|
|
133
|
+
url,
|
|
134
|
+
requestBodyValues,
|
|
135
|
+
responseHeaders
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
if (isOutputChunk(chunk.value)) {
|
|
139
|
+
return streamForConsumer;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
} finally {
|
|
143
|
+
reader.cancel().catch(() => {
|
|
144
|
+
});
|
|
145
|
+
reader.releaseLock();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function createOpenAIStreamError({
|
|
149
|
+
frame,
|
|
150
|
+
url,
|
|
151
|
+
requestBodyValues,
|
|
152
|
+
responseHeaders
|
|
153
|
+
}) {
|
|
154
|
+
var _a;
|
|
155
|
+
const streamError = parseStreamError(frame);
|
|
156
|
+
return new import_provider.APICallError({
|
|
157
|
+
message: (_a = streamError == null ? void 0 : streamError.message) != null ? _a : "OpenAI stream failed before any output was generated",
|
|
158
|
+
url,
|
|
159
|
+
requestBodyValues,
|
|
160
|
+
statusCode: streamError == null ? 500 : getStatusCode(streamError),
|
|
161
|
+
responseHeaders,
|
|
162
|
+
responseBody: JSON.stringify(frame),
|
|
163
|
+
data: frame
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
function parseStreamError(frame) {
|
|
167
|
+
var _a;
|
|
168
|
+
const value = asRecord(frame);
|
|
169
|
+
if (value == null) {
|
|
170
|
+
return void 0;
|
|
171
|
+
}
|
|
172
|
+
if (value.type === "response.failed") {
|
|
173
|
+
const response = asRecord(value.response);
|
|
174
|
+
const responseError = asRecord(response == null ? void 0 : response.error);
|
|
175
|
+
return typeof (responseError == null ? void 0 : responseError.message) === "string" ? {
|
|
176
|
+
message: responseError.message,
|
|
177
|
+
code: getStringOrNumber(responseError.code),
|
|
178
|
+
type: "response.failed",
|
|
179
|
+
frame
|
|
180
|
+
} : void 0;
|
|
181
|
+
}
|
|
182
|
+
const error = (_a = asRecord(value.error)) != null ? _a : value;
|
|
183
|
+
return typeof error.message === "string" && (asRecord(value.error) != null || typeof error.type === "string" || "code" in error || "param" in error) ? {
|
|
184
|
+
message: error.message,
|
|
185
|
+
code: getStringOrNumber(error.code),
|
|
186
|
+
type: typeof error.type === "string" ? error.type : void 0,
|
|
187
|
+
frame
|
|
188
|
+
} : void 0;
|
|
189
|
+
}
|
|
190
|
+
function getStatusCode(error) {
|
|
191
|
+
if (typeof error.code === "number" && isHttpErrorStatusCode(error.code)) {
|
|
192
|
+
return error.code;
|
|
193
|
+
}
|
|
194
|
+
if (typeof error.code === "string" && /^\d{3}$/.test(error.code)) {
|
|
195
|
+
const numericCode = Number(error.code);
|
|
196
|
+
if (isHttpErrorStatusCode(numericCode)) {
|
|
197
|
+
return numericCode;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const discriminator = [error.code, error.type].filter((value) => typeof value === "string" || typeof value === "number").join(" ").toLowerCase();
|
|
201
|
+
if (["insufficient_quota", "rate_limit"].some(
|
|
202
|
+
(term) => discriminator.includes(term)
|
|
203
|
+
)) {
|
|
204
|
+
return 429;
|
|
205
|
+
}
|
|
206
|
+
if (discriminator.includes("authentication")) return 401;
|
|
207
|
+
if (discriminator.includes("permission")) return 403;
|
|
208
|
+
if (discriminator.includes("not_found")) return 404;
|
|
209
|
+
if (["invalid", "bad_request", "context_length"].some(
|
|
210
|
+
(term) => discriminator.includes(term)
|
|
211
|
+
)) {
|
|
212
|
+
return 400;
|
|
213
|
+
}
|
|
214
|
+
if (discriminator.includes("overload")) return 503;
|
|
215
|
+
if (discriminator.includes("timeout")) return 504;
|
|
216
|
+
return 500;
|
|
217
|
+
}
|
|
218
|
+
function asRecord(value) {
|
|
219
|
+
return typeof value === "object" && value != null ? value : void 0;
|
|
220
|
+
}
|
|
221
|
+
function getStringOrNumber(value) {
|
|
222
|
+
return typeof value === "string" || typeof value === "number" ? value : void 0;
|
|
223
|
+
}
|
|
224
|
+
function isHttpErrorStatusCode(value) {
|
|
225
|
+
return Number.isInteger(value) && value >= 400 && value <= 599;
|
|
226
|
+
}
|
|
227
|
+
|
|
105
228
|
// src/chat/convert-openai-chat-usage.ts
|
|
106
229
|
function convertOpenAIChatUsage(usage) {
|
|
107
230
|
var _a, _b, _c, _d, _e, _f;
|
|
@@ -142,7 +265,7 @@ function convertOpenAIChatUsage(usage) {
|
|
|
142
265
|
}
|
|
143
266
|
|
|
144
267
|
// src/chat/convert-to-openai-chat-messages.ts
|
|
145
|
-
var
|
|
268
|
+
var import_provider2 = require("@ai-sdk/provider");
|
|
146
269
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
147
270
|
function serializeToolCallArguments(input) {
|
|
148
271
|
return JSON.stringify(input === void 0 ? {} : input);
|
|
@@ -208,7 +331,7 @@ function convertToOpenAIChatMessages({
|
|
|
208
331
|
};
|
|
209
332
|
} else if (part.mediaType.startsWith("audio/")) {
|
|
210
333
|
if (part.data instanceof URL) {
|
|
211
|
-
throw new
|
|
334
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
212
335
|
functionality: "audio file parts with URLs"
|
|
213
336
|
});
|
|
214
337
|
}
|
|
@@ -233,14 +356,14 @@ function convertToOpenAIChatMessages({
|
|
|
233
356
|
};
|
|
234
357
|
}
|
|
235
358
|
default: {
|
|
236
|
-
throw new
|
|
359
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
237
360
|
functionality: `audio content parts with media type ${part.mediaType}`
|
|
238
361
|
});
|
|
239
362
|
}
|
|
240
363
|
}
|
|
241
364
|
} else if (part.mediaType === "application/pdf") {
|
|
242
365
|
if (part.data instanceof URL) {
|
|
243
|
-
throw new
|
|
366
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
244
367
|
functionality: "PDF file parts with URLs"
|
|
245
368
|
});
|
|
246
369
|
}
|
|
@@ -252,7 +375,7 @@ function convertToOpenAIChatMessages({
|
|
|
252
375
|
}
|
|
253
376
|
};
|
|
254
377
|
} else {
|
|
255
|
-
throw new
|
|
378
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
256
379
|
functionality: `file part media type ${part.mediaType}`
|
|
257
380
|
});
|
|
258
381
|
}
|
|
@@ -304,7 +427,7 @@ function convertToOpenAIChatMessages({
|
|
|
304
427
|
contentValue = output.value;
|
|
305
428
|
break;
|
|
306
429
|
case "execution-denied":
|
|
307
|
-
contentValue = (_a = output.reason) != null ? _a : "Tool execution denied.";
|
|
430
|
+
contentValue = (_a = output.reason) != null ? _a : "Tool call execution denied.";
|
|
308
431
|
break;
|
|
309
432
|
case "content":
|
|
310
433
|
case "json":
|
|
@@ -621,7 +744,7 @@ var openaiLanguageModelChatOptions = (0, import_provider_utils4.lazySchema)(
|
|
|
621
744
|
);
|
|
622
745
|
|
|
623
746
|
// src/chat/openai-chat-prepare-tools.ts
|
|
624
|
-
var
|
|
747
|
+
var import_provider3 = require("@ai-sdk/provider");
|
|
625
748
|
function prepareChatTools({
|
|
626
749
|
tools,
|
|
627
750
|
toolChoice
|
|
@@ -675,7 +798,7 @@ function prepareChatTools({
|
|
|
675
798
|
};
|
|
676
799
|
default: {
|
|
677
800
|
const _exhaustiveCheck = type;
|
|
678
|
-
throw new
|
|
801
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
679
802
|
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
680
803
|
});
|
|
681
804
|
}
|
|
@@ -958,11 +1081,12 @@ var OpenAIChatLanguageModel = class {
|
|
|
958
1081
|
include_usage: true
|
|
959
1082
|
}
|
|
960
1083
|
};
|
|
1084
|
+
const url = this.config.url({
|
|
1085
|
+
path: "/chat/completions",
|
|
1086
|
+
modelId: this.modelId
|
|
1087
|
+
});
|
|
961
1088
|
const { responseHeaders, value: response } = await (0, import_provider_utils5.postJsonToApi)({
|
|
962
|
-
url
|
|
963
|
-
path: "/chat/completions",
|
|
964
|
-
modelId: this.modelId
|
|
965
|
-
}),
|
|
1089
|
+
url,
|
|
966
1090
|
headers: (0, import_provider_utils5.combineHeaders)(this.config.headers(), options.headers),
|
|
967
1091
|
body,
|
|
968
1092
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
@@ -972,6 +1096,14 @@ var OpenAIChatLanguageModel = class {
|
|
|
972
1096
|
abortSignal: options.abortSignal,
|
|
973
1097
|
fetch: this.config.fetch
|
|
974
1098
|
});
|
|
1099
|
+
const checkedResponse = await throwIfOpenAIStreamErrorBeforeOutput({
|
|
1100
|
+
stream: response,
|
|
1101
|
+
getError: (chunk) => "error" in chunk ? chunk.error : void 0,
|
|
1102
|
+
isOutputChunk: isOpenAIChatOutputChunk,
|
|
1103
|
+
url,
|
|
1104
|
+
requestBodyValues: body,
|
|
1105
|
+
responseHeaders
|
|
1106
|
+
});
|
|
975
1107
|
const toolCalls = [];
|
|
976
1108
|
let finishReason = {
|
|
977
1109
|
unified: "other",
|
|
@@ -981,14 +1113,14 @@ var OpenAIChatLanguageModel = class {
|
|
|
981
1113
|
let metadataExtracted = false;
|
|
982
1114
|
let isActiveText = false;
|
|
983
1115
|
const providerMetadata = { openai: {} };
|
|
984
|
-
|
|
985
|
-
stream:
|
|
1116
|
+
const result = {
|
|
1117
|
+
stream: checkedResponse.pipeThrough(
|
|
986
1118
|
new TransformStream({
|
|
987
1119
|
start(controller) {
|
|
988
1120
|
controller.enqueue({ type: "stream-start", warnings });
|
|
989
1121
|
},
|
|
990
1122
|
transform(chunk, controller) {
|
|
991
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m
|
|
1123
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
992
1124
|
if (options.includeRawChunks) {
|
|
993
1125
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
994
1126
|
}
|
|
@@ -1052,19 +1184,19 @@ var OpenAIChatLanguageModel = class {
|
|
|
1052
1184
|
const index = toolCallDelta.index;
|
|
1053
1185
|
if (toolCalls[index] == null) {
|
|
1054
1186
|
if (toolCallDelta.type != null && toolCallDelta.type !== "function") {
|
|
1055
|
-
throw new
|
|
1187
|
+
throw new import_provider4.InvalidResponseDataError({
|
|
1056
1188
|
data: toolCallDelta,
|
|
1057
1189
|
message: `Expected 'function' type.`
|
|
1058
1190
|
});
|
|
1059
1191
|
}
|
|
1060
1192
|
if (toolCallDelta.id == null) {
|
|
1061
|
-
throw new
|
|
1193
|
+
throw new import_provider4.InvalidResponseDataError({
|
|
1062
1194
|
data: toolCallDelta,
|
|
1063
1195
|
message: `Expected 'id' to be a string.`
|
|
1064
1196
|
});
|
|
1065
1197
|
}
|
|
1066
1198
|
if (((_f = toolCallDelta.function) == null ? void 0 : _f.name) == null) {
|
|
1067
|
-
throw new
|
|
1199
|
+
throw new import_provider4.InvalidResponseDataError({
|
|
1068
1200
|
data: toolCallDelta,
|
|
1069
1201
|
message: `Expected 'function.name' to be a string.`
|
|
1070
1202
|
});
|
|
@@ -1092,19 +1224,6 @@ var OpenAIChatLanguageModel = class {
|
|
|
1092
1224
|
delta: toolCall2.function.arguments
|
|
1093
1225
|
});
|
|
1094
1226
|
}
|
|
1095
|
-
if ((0, import_provider_utils5.isParsableJson)(toolCall2.function.arguments)) {
|
|
1096
|
-
controller.enqueue({
|
|
1097
|
-
type: "tool-input-end",
|
|
1098
|
-
id: toolCall2.id
|
|
1099
|
-
});
|
|
1100
|
-
controller.enqueue({
|
|
1101
|
-
type: "tool-call",
|
|
1102
|
-
toolCallId: (_j = toolCall2.id) != null ? _j : (0, import_provider_utils5.generateId)(),
|
|
1103
|
-
toolName: toolCall2.function.name,
|
|
1104
|
-
input: toolCall2.function.arguments
|
|
1105
|
-
});
|
|
1106
|
-
toolCall2.hasFinished = true;
|
|
1107
|
-
}
|
|
1108
1227
|
}
|
|
1109
1228
|
continue;
|
|
1110
1229
|
}
|
|
@@ -1112,27 +1231,14 @@ var OpenAIChatLanguageModel = class {
|
|
|
1112
1231
|
if (toolCall.hasFinished) {
|
|
1113
1232
|
continue;
|
|
1114
1233
|
}
|
|
1115
|
-
if (((
|
|
1116
|
-
toolCall.function.arguments += (
|
|
1234
|
+
if (((_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null) {
|
|
1235
|
+
toolCall.function.arguments += (_l = (_k = toolCallDelta.function) == null ? void 0 : _k.arguments) != null ? _l : "";
|
|
1117
1236
|
}
|
|
1118
1237
|
controller.enqueue({
|
|
1119
1238
|
type: "tool-input-delta",
|
|
1120
1239
|
id: toolCall.id,
|
|
1121
|
-
delta: (
|
|
1240
|
+
delta: (_m = toolCallDelta.function.arguments) != null ? _m : ""
|
|
1122
1241
|
});
|
|
1123
|
-
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)) {
|
|
1124
|
-
controller.enqueue({
|
|
1125
|
-
type: "tool-input-end",
|
|
1126
|
-
id: toolCall.id
|
|
1127
|
-
});
|
|
1128
|
-
controller.enqueue({
|
|
1129
|
-
type: "tool-call",
|
|
1130
|
-
toolCallId: (_q = toolCall.id) != null ? _q : (0, import_provider_utils5.generateId)(),
|
|
1131
|
-
toolName: toolCall.function.name,
|
|
1132
|
-
input: toolCall.function.arguments
|
|
1133
|
-
});
|
|
1134
|
-
toolCall.hasFinished = true;
|
|
1135
|
-
}
|
|
1136
1242
|
}
|
|
1137
1243
|
}
|
|
1138
1244
|
if (delta.annotations != null) {
|
|
@@ -1148,9 +1254,25 @@ var OpenAIChatLanguageModel = class {
|
|
|
1148
1254
|
}
|
|
1149
1255
|
},
|
|
1150
1256
|
flush(controller) {
|
|
1257
|
+
var _a;
|
|
1151
1258
|
if (isActiveText) {
|
|
1152
1259
|
controller.enqueue({ type: "text-end", id: "0" });
|
|
1153
1260
|
}
|
|
1261
|
+
for (const toolCall of toolCalls) {
|
|
1262
|
+
if (!toolCall.hasFinished) {
|
|
1263
|
+
controller.enqueue({
|
|
1264
|
+
type: "tool-input-end",
|
|
1265
|
+
id: toolCall.id
|
|
1266
|
+
});
|
|
1267
|
+
controller.enqueue({
|
|
1268
|
+
type: "tool-call",
|
|
1269
|
+
toolCallId: (_a = toolCall.id) != null ? _a : (0, import_provider_utils5.generateId)(),
|
|
1270
|
+
toolName: toolCall.function.name,
|
|
1271
|
+
input: toolCall.function.arguments
|
|
1272
|
+
});
|
|
1273
|
+
toolCall.hasFinished = true;
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1154
1276
|
controller.enqueue({
|
|
1155
1277
|
type: "finish",
|
|
1156
1278
|
finishReason,
|
|
@@ -1163,8 +1285,18 @@ var OpenAIChatLanguageModel = class {
|
|
|
1163
1285
|
request: { body },
|
|
1164
1286
|
response: { headers: responseHeaders }
|
|
1165
1287
|
};
|
|
1288
|
+
return result;
|
|
1166
1289
|
}
|
|
1167
1290
|
};
|
|
1291
|
+
function isOpenAIChatOutputChunk(chunk) {
|
|
1292
|
+
if ("error" in chunk) {
|
|
1293
|
+
return false;
|
|
1294
|
+
}
|
|
1295
|
+
return chunk.choices.some((choice) => {
|
|
1296
|
+
const delta = choice.delta;
|
|
1297
|
+
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;
|
|
1298
|
+
});
|
|
1299
|
+
}
|
|
1168
1300
|
|
|
1169
1301
|
// src/completion/openai-completion-language-model.ts
|
|
1170
1302
|
var import_provider_utils8 = require("@ai-sdk/provider-utils");
|
|
@@ -1207,7 +1339,7 @@ function convertOpenAICompletionUsage(usage) {
|
|
|
1207
1339
|
}
|
|
1208
1340
|
|
|
1209
1341
|
// src/completion/convert-to-openai-completion-prompt.ts
|
|
1210
|
-
var
|
|
1342
|
+
var import_provider5 = require("@ai-sdk/provider");
|
|
1211
1343
|
function convertToOpenAICompletionPrompt({
|
|
1212
1344
|
prompt,
|
|
1213
1345
|
user = "user",
|
|
@@ -1223,7 +1355,7 @@ function convertToOpenAICompletionPrompt({
|
|
|
1223
1355
|
for (const { role, content } of prompt) {
|
|
1224
1356
|
switch (role) {
|
|
1225
1357
|
case "system": {
|
|
1226
|
-
throw new
|
|
1358
|
+
throw new import_provider5.InvalidPromptError({
|
|
1227
1359
|
message: "Unexpected system message in prompt: ${content}",
|
|
1228
1360
|
prompt
|
|
1229
1361
|
});
|
|
@@ -1249,7 +1381,7 @@ ${userMessage}
|
|
|
1249
1381
|
return part.text;
|
|
1250
1382
|
}
|
|
1251
1383
|
case "tool-call": {
|
|
1252
|
-
throw new
|
|
1384
|
+
throw new import_provider5.UnsupportedFunctionalityError({
|
|
1253
1385
|
functionality: "tool-call messages"
|
|
1254
1386
|
});
|
|
1255
1387
|
}
|
|
@@ -1262,7 +1394,7 @@ ${assistantMessage}
|
|
|
1262
1394
|
break;
|
|
1263
1395
|
}
|
|
1264
1396
|
case "tool": {
|
|
1265
|
-
throw new
|
|
1397
|
+
throw new import_provider5.UnsupportedFunctionalityError({
|
|
1266
1398
|
functionality: "tool messages"
|
|
1267
1399
|
});
|
|
1268
1400
|
}
|
|
@@ -1556,11 +1688,12 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1556
1688
|
include_usage: true
|
|
1557
1689
|
}
|
|
1558
1690
|
};
|
|
1691
|
+
const url = this.config.url({
|
|
1692
|
+
path: "/completions",
|
|
1693
|
+
modelId: this.modelId
|
|
1694
|
+
});
|
|
1559
1695
|
const { responseHeaders, value: response } = await (0, import_provider_utils8.postJsonToApi)({
|
|
1560
|
-
url
|
|
1561
|
-
path: "/completions",
|
|
1562
|
-
modelId: this.modelId
|
|
1563
|
-
}),
|
|
1696
|
+
url,
|
|
1564
1697
|
headers: (0, import_provider_utils8.combineHeaders)(this.config.headers(), options.headers),
|
|
1565
1698
|
body,
|
|
1566
1699
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
@@ -1570,6 +1703,14 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1570
1703
|
abortSignal: options.abortSignal,
|
|
1571
1704
|
fetch: this.config.fetch
|
|
1572
1705
|
});
|
|
1706
|
+
const checkedResponse = await throwIfOpenAIStreamErrorBeforeOutput({
|
|
1707
|
+
stream: response,
|
|
1708
|
+
getError: (chunk) => "error" in chunk ? chunk.error : void 0,
|
|
1709
|
+
isOutputChunk: isOpenAICompletionOutputChunk,
|
|
1710
|
+
url,
|
|
1711
|
+
requestBodyValues: body,
|
|
1712
|
+
responseHeaders
|
|
1713
|
+
});
|
|
1573
1714
|
let finishReason = {
|
|
1574
1715
|
unified: "other",
|
|
1575
1716
|
raw: void 0
|
|
@@ -1577,8 +1718,8 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1577
1718
|
const providerMetadata = { openai: {} };
|
|
1578
1719
|
let usage = void 0;
|
|
1579
1720
|
let isFirstChunk = true;
|
|
1580
|
-
|
|
1581
|
-
stream:
|
|
1721
|
+
const result = {
|
|
1722
|
+
stream: checkedResponse.pipeThrough(
|
|
1582
1723
|
new TransformStream({
|
|
1583
1724
|
start(controller) {
|
|
1584
1725
|
controller.enqueue({ type: "stream-start", warnings });
|
|
@@ -1643,11 +1784,15 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1643
1784
|
request: { body },
|
|
1644
1785
|
response: { headers: responseHeaders }
|
|
1645
1786
|
};
|
|
1787
|
+
return result;
|
|
1646
1788
|
}
|
|
1647
1789
|
};
|
|
1790
|
+
function isOpenAICompletionOutputChunk(chunk) {
|
|
1791
|
+
return !("error" in chunk) && chunk.choices.some((choice) => choice.text.length > 0);
|
|
1792
|
+
}
|
|
1648
1793
|
|
|
1649
1794
|
// src/embedding/openai-embedding-model.ts
|
|
1650
|
-
var
|
|
1795
|
+
var import_provider6 = require("@ai-sdk/provider");
|
|
1651
1796
|
var import_provider_utils11 = require("@ai-sdk/provider-utils");
|
|
1652
1797
|
|
|
1653
1798
|
// src/embedding/openai-embedding-options.ts
|
|
@@ -1702,7 +1847,7 @@ var OpenAIEmbeddingModel = class {
|
|
|
1702
1847
|
}) {
|
|
1703
1848
|
var _a;
|
|
1704
1849
|
if (values.length > this.maxEmbeddingsPerCall) {
|
|
1705
|
-
throw new
|
|
1850
|
+
throw new import_provider6.TooManyEmbeddingValuesForCallError({
|
|
1706
1851
|
provider: this.provider,
|
|
1707
1852
|
modelId: this.modelId,
|
|
1708
1853
|
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
@@ -2431,7 +2576,7 @@ var OpenAISpeechModel = class {
|
|
|
2431
2576
|
};
|
|
2432
2577
|
|
|
2433
2578
|
// src/responses/openai-responses-language-model.ts
|
|
2434
|
-
var
|
|
2579
|
+
var import_provider9 = require("@ai-sdk/provider");
|
|
2435
2580
|
var import_provider_utils35 = require("@ai-sdk/provider-utils");
|
|
2436
2581
|
|
|
2437
2582
|
// src/responses/convert-openai-responses-usage.ts
|
|
@@ -2474,7 +2619,7 @@ function convertOpenAIResponsesUsage(usage) {
|
|
|
2474
2619
|
}
|
|
2475
2620
|
|
|
2476
2621
|
// src/responses/convert-to-openai-responses-input.ts
|
|
2477
|
-
var
|
|
2622
|
+
var import_provider7 = require("@ai-sdk/provider");
|
|
2478
2623
|
var import_provider_utils24 = require("@ai-sdk/provider-utils");
|
|
2479
2624
|
var import_v417 = require("zod/v4");
|
|
2480
2625
|
|
|
@@ -2758,7 +2903,7 @@ async function convertToOpenAIResponsesInput({
|
|
|
2758
2903
|
};
|
|
2759
2904
|
}
|
|
2760
2905
|
if (mediaType !== "application/pdf" && !passThroughUnsupportedFiles) {
|
|
2761
|
-
throw new
|
|
2906
|
+
throw new import_provider7.UnsupportedFunctionalityError({
|
|
2762
2907
|
functionality: `file part media type ${mediaType}`
|
|
2763
2908
|
});
|
|
2764
2909
|
}
|
|
@@ -3152,7 +3297,7 @@ async function convertToOpenAIResponsesInput({
|
|
|
3152
3297
|
outputValue = output.value;
|
|
3153
3298
|
break;
|
|
3154
3299
|
case "execution-denied":
|
|
3155
|
-
outputValue = (_v = output.reason) != null ? _v : "Tool execution denied.";
|
|
3300
|
+
outputValue = (_v = output.reason) != null ? _v : "Tool call execution denied.";
|
|
3156
3301
|
break;
|
|
3157
3302
|
case "json":
|
|
3158
3303
|
case "error-json":
|
|
@@ -3213,7 +3358,7 @@ async function convertToOpenAIResponsesInput({
|
|
|
3213
3358
|
contentValue = output.value;
|
|
3214
3359
|
break;
|
|
3215
3360
|
case "execution-denied":
|
|
3216
|
-
contentValue = (_w = output.reason) != null ? _w : "Tool execution denied.";
|
|
3361
|
+
contentValue = (_w = output.reason) != null ? _w : "Tool call execution denied.";
|
|
3217
3362
|
break;
|
|
3218
3363
|
case "json":
|
|
3219
3364
|
case "error-json":
|
|
@@ -3327,6 +3472,23 @@ var jsonValueSchema = import_v418.z.lazy(
|
|
|
3327
3472
|
import_v418.z.record(import_v418.z.string(), jsonValueSchema.optional())
|
|
3328
3473
|
])
|
|
3329
3474
|
);
|
|
3475
|
+
var openaiResponsesNestedErrorChunkSchema = import_v418.z.object({
|
|
3476
|
+
type: import_v418.z.literal("error"),
|
|
3477
|
+
sequence_number: import_v418.z.number(),
|
|
3478
|
+
error: import_v418.z.object({
|
|
3479
|
+
type: import_v418.z.string(),
|
|
3480
|
+
code: import_v418.z.string(),
|
|
3481
|
+
message: import_v418.z.string(),
|
|
3482
|
+
param: import_v418.z.string().nullish()
|
|
3483
|
+
})
|
|
3484
|
+
});
|
|
3485
|
+
var openaiResponsesErrorChunkSchema = import_v418.z.object({
|
|
3486
|
+
type: import_v418.z.literal("error"),
|
|
3487
|
+
sequence_number: import_v418.z.number(),
|
|
3488
|
+
code: import_v418.z.string().nullish(),
|
|
3489
|
+
message: import_v418.z.string(),
|
|
3490
|
+
param: import_v418.z.string().nullish()
|
|
3491
|
+
});
|
|
3330
3492
|
var openaiResponsesChunkSchema = (0, import_provider_utils25.lazySchema)(
|
|
3331
3493
|
() => (0, import_provider_utils25.zodSchema)(
|
|
3332
3494
|
import_v418.z.union([
|
|
@@ -3369,6 +3531,7 @@ var openaiResponsesChunkSchema = (0, import_provider_utils25.lazySchema)(
|
|
|
3369
3531
|
}),
|
|
3370
3532
|
import_v418.z.object({
|
|
3371
3533
|
type: import_v418.z.literal("response.failed"),
|
|
3534
|
+
sequence_number: import_v418.z.number(),
|
|
3372
3535
|
response: import_v418.z.object({
|
|
3373
3536
|
error: import_v418.z.object({
|
|
3374
3537
|
code: import_v418.z.string().nullish(),
|
|
@@ -3857,16 +4020,8 @@ var openaiResponsesChunkSchema = (0, import_provider_utils25.lazySchema)(
|
|
|
3857
4020
|
output_index: import_v418.z.number(),
|
|
3858
4021
|
diff: import_v418.z.string()
|
|
3859
4022
|
}),
|
|
3860
|
-
|
|
3861
|
-
|
|
3862
|
-
sequence_number: import_v418.z.number(),
|
|
3863
|
-
error: import_v418.z.object({
|
|
3864
|
-
type: import_v418.z.string(),
|
|
3865
|
-
code: import_v418.z.string(),
|
|
3866
|
-
message: import_v418.z.string(),
|
|
3867
|
-
param: import_v418.z.string().nullish()
|
|
3868
|
-
})
|
|
3869
|
-
}),
|
|
4023
|
+
openaiResponsesNestedErrorChunkSchema,
|
|
4024
|
+
openaiResponsesErrorChunkSchema,
|
|
3870
4025
|
import_v418.z.object({ type: import_v418.z.string() }).loose().transform((value) => ({
|
|
3871
4026
|
type: "unknown_chunk",
|
|
3872
4027
|
message: value.type
|
|
@@ -4433,7 +4588,7 @@ var openaiLanguageModelResponsesOptionsSchema = (0, import_provider_utils26.lazy
|
|
|
4433
4588
|
);
|
|
4434
4589
|
|
|
4435
4590
|
// src/responses/openai-responses-prepare-tools.ts
|
|
4436
|
-
var
|
|
4591
|
+
var import_provider8 = require("@ai-sdk/provider");
|
|
4437
4592
|
var import_provider_utils34 = require("@ai-sdk/provider-utils");
|
|
4438
4593
|
|
|
4439
4594
|
// src/tool/code-interpreter.ts
|
|
@@ -4795,7 +4950,7 @@ async function prepareResponsesTools({
|
|
|
4795
4950
|
namespaceTools.set(namespace.name, namespaceTool);
|
|
4796
4951
|
openaiTools.push(namespaceTool);
|
|
4797
4952
|
} else if (namespaceTool.description !== namespace.description) {
|
|
4798
|
-
throw new
|
|
4953
|
+
throw new import_provider8.UnsupportedFunctionalityError({
|
|
4799
4954
|
functionality: `conflicting descriptions for OpenAI tool namespace "${namespace.name}"`
|
|
4800
4955
|
});
|
|
4801
4956
|
}
|
|
@@ -5007,7 +5162,7 @@ async function prepareResponsesTools({
|
|
|
5007
5162
|
}
|
|
5008
5163
|
default: {
|
|
5009
5164
|
const _exhaustiveCheck = type;
|
|
5010
|
-
throw new
|
|
5165
|
+
throw new import_provider8.UnsupportedFunctionalityError({
|
|
5011
5166
|
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
5012
5167
|
});
|
|
5013
5168
|
}
|
|
@@ -5380,7 +5535,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
5380
5535
|
fetch: this.config.fetch
|
|
5381
5536
|
});
|
|
5382
5537
|
if (response.error) {
|
|
5383
|
-
throw new
|
|
5538
|
+
throw new import_provider9.APICallError({
|
|
5384
5539
|
message: response.error.message,
|
|
5385
5540
|
url,
|
|
5386
5541
|
requestBodyValues: body,
|
|
@@ -5853,6 +6008,14 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
5853
6008
|
abortSignal: options.abortSignal,
|
|
5854
6009
|
fetch: this.config.fetch
|
|
5855
6010
|
});
|
|
6011
|
+
const checkedResponse = await throwIfOpenAIStreamErrorBeforeOutput({
|
|
6012
|
+
stream: response,
|
|
6013
|
+
getError: (chunk) => isErrorChunk(chunk) || isResponseFailedChunk(chunk) && chunk.response.error != null ? chunk : void 0,
|
|
6014
|
+
isOutputChunk: isResponseOutputChunk,
|
|
6015
|
+
url,
|
|
6016
|
+
requestBodyValues: body,
|
|
6017
|
+
responseHeaders
|
|
6018
|
+
});
|
|
5856
6019
|
const self = this;
|
|
5857
6020
|
const approvalRequestIdToDummyToolCallIdFromPrompt = extractApprovalRequestIdToToolCallIdMapping(options.prompt);
|
|
5858
6021
|
const approvalRequestIdToDummyToolCallIdFromStream = /* @__PURE__ */ new Map();
|
|
@@ -5870,8 +6033,9 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
5870
6033
|
const activeReasoning = {};
|
|
5871
6034
|
let serviceTier;
|
|
5872
6035
|
const hostedToolSearchCallIds = [];
|
|
5873
|
-
|
|
5874
|
-
|
|
6036
|
+
let encounteredStreamError = false;
|
|
6037
|
+
const result = {
|
|
6038
|
+
stream: checkedResponse.pipeThrough(
|
|
5875
6039
|
new TransformStream({
|
|
5876
6040
|
start(controller) {
|
|
5877
6041
|
controller.enqueue({ type: "stream-start", warnings });
|
|
@@ -6191,12 +6355,12 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
6191
6355
|
toolName: toolNameMapping.toCustomToolName("file_search"),
|
|
6192
6356
|
result: {
|
|
6193
6357
|
queries: value.item.queries,
|
|
6194
|
-
results: (_f = (_e = value.item.results) == null ? void 0 : _e.map((
|
|
6195
|
-
attributes:
|
|
6196
|
-
fileId:
|
|
6197
|
-
filename:
|
|
6198
|
-
score:
|
|
6199
|
-
text:
|
|
6358
|
+
results: (_f = (_e = value.item.results) == null ? void 0 : _e.map((result2) => ({
|
|
6359
|
+
attributes: result2.attributes,
|
|
6360
|
+
fileId: result2.file_id,
|
|
6361
|
+
filename: result2.filename,
|
|
6362
|
+
score: result2.score,
|
|
6363
|
+
text: result2.text
|
|
6200
6364
|
}))) != null ? _f : null
|
|
6201
6365
|
}
|
|
6202
6366
|
});
|
|
@@ -6629,6 +6793,21 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
6629
6793
|
raw: incompleteReason != null ? incompleteReason : "error"
|
|
6630
6794
|
};
|
|
6631
6795
|
usage = (_z = value.response.usage) != null ? _z : void 0;
|
|
6796
|
+
if (!encounteredStreamError && value.response.error != null) {
|
|
6797
|
+
encounteredStreamError = true;
|
|
6798
|
+
controller.enqueue({
|
|
6799
|
+
type: "error",
|
|
6800
|
+
error: {
|
|
6801
|
+
type: "response.failed",
|
|
6802
|
+
sequence_number: value.sequence_number,
|
|
6803
|
+
response: {
|
|
6804
|
+
error: value.response.error,
|
|
6805
|
+
incomplete_details: value.response.incomplete_details,
|
|
6806
|
+
service_tier: value.response.service_tier
|
|
6807
|
+
}
|
|
6808
|
+
}
|
|
6809
|
+
});
|
|
6810
|
+
}
|
|
6632
6811
|
} else if (isResponseAnnotationAddedChunk(value)) {
|
|
6633
6812
|
ongoingAnnotations.push(value.annotation);
|
|
6634
6813
|
if (value.annotation.type === "url_citation") {
|
|
@@ -6689,6 +6868,8 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
6689
6868
|
});
|
|
6690
6869
|
}
|
|
6691
6870
|
} else if (isErrorChunk(value)) {
|
|
6871
|
+
encounteredStreamError = true;
|
|
6872
|
+
finishReason = { unified: "error", raw: "error" };
|
|
6692
6873
|
controller.enqueue({ type: "error", error: value });
|
|
6693
6874
|
}
|
|
6694
6875
|
},
|
|
@@ -6712,13 +6893,14 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
6712
6893
|
request: { body },
|
|
6713
6894
|
response: { headers: responseHeaders }
|
|
6714
6895
|
};
|
|
6896
|
+
return result;
|
|
6715
6897
|
}
|
|
6716
6898
|
};
|
|
6717
6899
|
function isTextDeltaChunk(chunk) {
|
|
6718
6900
|
return chunk.type === "response.output_text.delta";
|
|
6719
6901
|
}
|
|
6720
6902
|
function isOpenAIChatCompletionChunk(value) {
|
|
6721
|
-
const chunk =
|
|
6903
|
+
const chunk = asRecord2(value);
|
|
6722
6904
|
return chunk != null && Array.isArray(chunk.choices) && typeof chunk.type !== "string";
|
|
6723
6905
|
}
|
|
6724
6906
|
function createOpenAIResponsesChatCompletionsMismatchError({
|
|
@@ -6728,7 +6910,7 @@ function createOpenAIResponsesChatCompletionsMismatchError({
|
|
|
6728
6910
|
requestBodyValues,
|
|
6729
6911
|
responseHeaders
|
|
6730
6912
|
}) {
|
|
6731
|
-
return new
|
|
6913
|
+
return new import_provider9.APICallError({
|
|
6732
6914
|
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.",
|
|
6733
6915
|
url,
|
|
6734
6916
|
requestBodyValues,
|
|
@@ -6739,7 +6921,7 @@ function createOpenAIResponsesChatCompletionsMismatchError({
|
|
|
6739
6921
|
isRetryable: false
|
|
6740
6922
|
});
|
|
6741
6923
|
}
|
|
6742
|
-
function
|
|
6924
|
+
function asRecord2(value) {
|
|
6743
6925
|
return typeof value === "object" && value != null ? value : void 0;
|
|
6744
6926
|
}
|
|
6745
6927
|
function isResponseOutputItemDoneChunk(chunk) {
|
|
@@ -6784,6 +6966,9 @@ function isResponseAnnotationAddedChunk(chunk) {
|
|
|
6784
6966
|
function isErrorChunk(chunk) {
|
|
6785
6967
|
return chunk.type === "error";
|
|
6786
6968
|
}
|
|
6969
|
+
function isResponseOutputChunk(chunk) {
|
|
6970
|
+
return !(chunk.type === "response.created" || chunk.type === "response.failed" || chunk.type === "error" || chunk.type === "unknown_chunk");
|
|
6971
|
+
}
|
|
6787
6972
|
function mapWebSearchOutput(action) {
|
|
6788
6973
|
var _a;
|
|
6789
6974
|
if (action == null) {
|