@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.mjs
CHANGED
|
@@ -15,7 +15,6 @@ import {
|
|
|
15
15
|
createEventSourceResponseHandler,
|
|
16
16
|
createJsonResponseHandler,
|
|
17
17
|
generateId,
|
|
18
|
-
isParsableJson,
|
|
19
18
|
parseProviderOptions,
|
|
20
19
|
postJsonToApi
|
|
21
20
|
} from "@ai-sdk/provider-utils";
|
|
@@ -55,6 +54,129 @@ function getOpenAILanguageModelCapabilities(modelId) {
|
|
|
55
54
|
};
|
|
56
55
|
}
|
|
57
56
|
|
|
57
|
+
// src/openai-stream-error.ts
|
|
58
|
+
import { APICallError } from "@ai-sdk/provider";
|
|
59
|
+
async function throwIfOpenAIStreamErrorBeforeOutput({
|
|
60
|
+
stream,
|
|
61
|
+
getError,
|
|
62
|
+
isOutputChunk,
|
|
63
|
+
url,
|
|
64
|
+
requestBodyValues,
|
|
65
|
+
responseHeaders
|
|
66
|
+
}) {
|
|
67
|
+
const [streamForEarlyError, streamForConsumer] = stream.tee();
|
|
68
|
+
const reader = streamForEarlyError.getReader();
|
|
69
|
+
try {
|
|
70
|
+
while (true) {
|
|
71
|
+
const result = await reader.read();
|
|
72
|
+
if (result.done) {
|
|
73
|
+
return streamForConsumer;
|
|
74
|
+
}
|
|
75
|
+
const chunk = result.value;
|
|
76
|
+
if (!chunk.success) {
|
|
77
|
+
return streamForConsumer;
|
|
78
|
+
}
|
|
79
|
+
const errorFrame = getError(chunk.value);
|
|
80
|
+
if (errorFrame != null) {
|
|
81
|
+
streamForConsumer.cancel().catch(() => {
|
|
82
|
+
});
|
|
83
|
+
throw createOpenAIStreamError({
|
|
84
|
+
frame: errorFrame,
|
|
85
|
+
url,
|
|
86
|
+
requestBodyValues,
|
|
87
|
+
responseHeaders
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
if (isOutputChunk(chunk.value)) {
|
|
91
|
+
return streamForConsumer;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
} finally {
|
|
95
|
+
reader.cancel().catch(() => {
|
|
96
|
+
});
|
|
97
|
+
reader.releaseLock();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function createOpenAIStreamError({
|
|
101
|
+
frame,
|
|
102
|
+
url,
|
|
103
|
+
requestBodyValues,
|
|
104
|
+
responseHeaders
|
|
105
|
+
}) {
|
|
106
|
+
var _a;
|
|
107
|
+
const streamError = parseStreamError(frame);
|
|
108
|
+
return new APICallError({
|
|
109
|
+
message: (_a = streamError == null ? void 0 : streamError.message) != null ? _a : "OpenAI stream failed before any output was generated",
|
|
110
|
+
url,
|
|
111
|
+
requestBodyValues,
|
|
112
|
+
statusCode: streamError == null ? 500 : getStatusCode(streamError),
|
|
113
|
+
responseHeaders,
|
|
114
|
+
responseBody: JSON.stringify(frame),
|
|
115
|
+
data: frame
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
function parseStreamError(frame) {
|
|
119
|
+
var _a;
|
|
120
|
+
const value = asRecord(frame);
|
|
121
|
+
if (value == null) {
|
|
122
|
+
return void 0;
|
|
123
|
+
}
|
|
124
|
+
if (value.type === "response.failed") {
|
|
125
|
+
const response = asRecord(value.response);
|
|
126
|
+
const responseError = asRecord(response == null ? void 0 : response.error);
|
|
127
|
+
return typeof (responseError == null ? void 0 : responseError.message) === "string" ? {
|
|
128
|
+
message: responseError.message,
|
|
129
|
+
code: getStringOrNumber(responseError.code),
|
|
130
|
+
type: "response.failed",
|
|
131
|
+
frame
|
|
132
|
+
} : void 0;
|
|
133
|
+
}
|
|
134
|
+
const error = (_a = asRecord(value.error)) != null ? _a : value;
|
|
135
|
+
return typeof error.message === "string" && (asRecord(value.error) != null || typeof error.type === "string" || "code" in error || "param" in error) ? {
|
|
136
|
+
message: error.message,
|
|
137
|
+
code: getStringOrNumber(error.code),
|
|
138
|
+
type: typeof error.type === "string" ? error.type : void 0,
|
|
139
|
+
frame
|
|
140
|
+
} : void 0;
|
|
141
|
+
}
|
|
142
|
+
function getStatusCode(error) {
|
|
143
|
+
if (typeof error.code === "number" && isHttpErrorStatusCode(error.code)) {
|
|
144
|
+
return error.code;
|
|
145
|
+
}
|
|
146
|
+
if (typeof error.code === "string" && /^\d{3}$/.test(error.code)) {
|
|
147
|
+
const numericCode = Number(error.code);
|
|
148
|
+
if (isHttpErrorStatusCode(numericCode)) {
|
|
149
|
+
return numericCode;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const discriminator = [error.code, error.type].filter((value) => typeof value === "string" || typeof value === "number").join(" ").toLowerCase();
|
|
153
|
+
if (["insufficient_quota", "rate_limit"].some(
|
|
154
|
+
(term) => discriminator.includes(term)
|
|
155
|
+
)) {
|
|
156
|
+
return 429;
|
|
157
|
+
}
|
|
158
|
+
if (discriminator.includes("authentication")) return 401;
|
|
159
|
+
if (discriminator.includes("permission")) return 403;
|
|
160
|
+
if (discriminator.includes("not_found")) return 404;
|
|
161
|
+
if (["invalid", "bad_request", "context_length"].some(
|
|
162
|
+
(term) => discriminator.includes(term)
|
|
163
|
+
)) {
|
|
164
|
+
return 400;
|
|
165
|
+
}
|
|
166
|
+
if (discriminator.includes("overload")) return 503;
|
|
167
|
+
if (discriminator.includes("timeout")) return 504;
|
|
168
|
+
return 500;
|
|
169
|
+
}
|
|
170
|
+
function asRecord(value) {
|
|
171
|
+
return typeof value === "object" && value != null ? value : void 0;
|
|
172
|
+
}
|
|
173
|
+
function getStringOrNumber(value) {
|
|
174
|
+
return typeof value === "string" || typeof value === "number" ? value : void 0;
|
|
175
|
+
}
|
|
176
|
+
function isHttpErrorStatusCode(value) {
|
|
177
|
+
return Number.isInteger(value) && value >= 400 && value <= 599;
|
|
178
|
+
}
|
|
179
|
+
|
|
58
180
|
// src/chat/convert-openai-chat-usage.ts
|
|
59
181
|
function convertOpenAIChatUsage(usage) {
|
|
60
182
|
var _a, _b, _c, _d, _e, _f;
|
|
@@ -259,7 +381,7 @@ function convertToOpenAIChatMessages({
|
|
|
259
381
|
contentValue = output.value;
|
|
260
382
|
break;
|
|
261
383
|
case "execution-denied":
|
|
262
|
-
contentValue = (_a = output.reason) != null ? _a : "Tool execution denied.";
|
|
384
|
+
contentValue = (_a = output.reason) != null ? _a : "Tool call execution denied.";
|
|
263
385
|
break;
|
|
264
386
|
case "content":
|
|
265
387
|
case "json":
|
|
@@ -921,11 +1043,12 @@ var OpenAIChatLanguageModel = class {
|
|
|
921
1043
|
include_usage: true
|
|
922
1044
|
}
|
|
923
1045
|
};
|
|
1046
|
+
const url = this.config.url({
|
|
1047
|
+
path: "/chat/completions",
|
|
1048
|
+
modelId: this.modelId
|
|
1049
|
+
});
|
|
924
1050
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
925
|
-
url
|
|
926
|
-
path: "/chat/completions",
|
|
927
|
-
modelId: this.modelId
|
|
928
|
-
}),
|
|
1051
|
+
url,
|
|
929
1052
|
headers: combineHeaders(this.config.headers(), options.headers),
|
|
930
1053
|
body,
|
|
931
1054
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
@@ -935,6 +1058,14 @@ var OpenAIChatLanguageModel = class {
|
|
|
935
1058
|
abortSignal: options.abortSignal,
|
|
936
1059
|
fetch: this.config.fetch
|
|
937
1060
|
});
|
|
1061
|
+
const checkedResponse = await throwIfOpenAIStreamErrorBeforeOutput({
|
|
1062
|
+
stream: response,
|
|
1063
|
+
getError: (chunk) => "error" in chunk ? chunk.error : void 0,
|
|
1064
|
+
isOutputChunk: isOpenAIChatOutputChunk,
|
|
1065
|
+
url,
|
|
1066
|
+
requestBodyValues: body,
|
|
1067
|
+
responseHeaders
|
|
1068
|
+
});
|
|
938
1069
|
const toolCalls = [];
|
|
939
1070
|
let finishReason = {
|
|
940
1071
|
unified: "other",
|
|
@@ -944,14 +1075,14 @@ var OpenAIChatLanguageModel = class {
|
|
|
944
1075
|
let metadataExtracted = false;
|
|
945
1076
|
let isActiveText = false;
|
|
946
1077
|
const providerMetadata = { openai: {} };
|
|
947
|
-
|
|
948
|
-
stream:
|
|
1078
|
+
const result = {
|
|
1079
|
+
stream: checkedResponse.pipeThrough(
|
|
949
1080
|
new TransformStream({
|
|
950
1081
|
start(controller) {
|
|
951
1082
|
controller.enqueue({ type: "stream-start", warnings });
|
|
952
1083
|
},
|
|
953
1084
|
transform(chunk, controller) {
|
|
954
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m
|
|
1085
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
955
1086
|
if (options.includeRawChunks) {
|
|
956
1087
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
957
1088
|
}
|
|
@@ -1055,19 +1186,6 @@ var OpenAIChatLanguageModel = class {
|
|
|
1055
1186
|
delta: toolCall2.function.arguments
|
|
1056
1187
|
});
|
|
1057
1188
|
}
|
|
1058
|
-
if (isParsableJson(toolCall2.function.arguments)) {
|
|
1059
|
-
controller.enqueue({
|
|
1060
|
-
type: "tool-input-end",
|
|
1061
|
-
id: toolCall2.id
|
|
1062
|
-
});
|
|
1063
|
-
controller.enqueue({
|
|
1064
|
-
type: "tool-call",
|
|
1065
|
-
toolCallId: (_j = toolCall2.id) != null ? _j : generateId(),
|
|
1066
|
-
toolName: toolCall2.function.name,
|
|
1067
|
-
input: toolCall2.function.arguments
|
|
1068
|
-
});
|
|
1069
|
-
toolCall2.hasFinished = true;
|
|
1070
|
-
}
|
|
1071
1189
|
}
|
|
1072
1190
|
continue;
|
|
1073
1191
|
}
|
|
@@ -1075,27 +1193,14 @@ var OpenAIChatLanguageModel = class {
|
|
|
1075
1193
|
if (toolCall.hasFinished) {
|
|
1076
1194
|
continue;
|
|
1077
1195
|
}
|
|
1078
|
-
if (((
|
|
1079
|
-
toolCall.function.arguments += (
|
|
1196
|
+
if (((_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null) {
|
|
1197
|
+
toolCall.function.arguments += (_l = (_k = toolCallDelta.function) == null ? void 0 : _k.arguments) != null ? _l : "";
|
|
1080
1198
|
}
|
|
1081
1199
|
controller.enqueue({
|
|
1082
1200
|
type: "tool-input-delta",
|
|
1083
1201
|
id: toolCall.id,
|
|
1084
|
-
delta: (
|
|
1202
|
+
delta: (_m = toolCallDelta.function.arguments) != null ? _m : ""
|
|
1085
1203
|
});
|
|
1086
|
-
if (((_o = toolCall.function) == null ? void 0 : _o.name) != null && ((_p = toolCall.function) == null ? void 0 : _p.arguments) != null && isParsableJson(toolCall.function.arguments)) {
|
|
1087
|
-
controller.enqueue({
|
|
1088
|
-
type: "tool-input-end",
|
|
1089
|
-
id: toolCall.id
|
|
1090
|
-
});
|
|
1091
|
-
controller.enqueue({
|
|
1092
|
-
type: "tool-call",
|
|
1093
|
-
toolCallId: (_q = toolCall.id) != null ? _q : generateId(),
|
|
1094
|
-
toolName: toolCall.function.name,
|
|
1095
|
-
input: toolCall.function.arguments
|
|
1096
|
-
});
|
|
1097
|
-
toolCall.hasFinished = true;
|
|
1098
|
-
}
|
|
1099
1204
|
}
|
|
1100
1205
|
}
|
|
1101
1206
|
if (delta.annotations != null) {
|
|
@@ -1111,9 +1216,25 @@ var OpenAIChatLanguageModel = class {
|
|
|
1111
1216
|
}
|
|
1112
1217
|
},
|
|
1113
1218
|
flush(controller) {
|
|
1219
|
+
var _a;
|
|
1114
1220
|
if (isActiveText) {
|
|
1115
1221
|
controller.enqueue({ type: "text-end", id: "0" });
|
|
1116
1222
|
}
|
|
1223
|
+
for (const toolCall of toolCalls) {
|
|
1224
|
+
if (!toolCall.hasFinished) {
|
|
1225
|
+
controller.enqueue({
|
|
1226
|
+
type: "tool-input-end",
|
|
1227
|
+
id: toolCall.id
|
|
1228
|
+
});
|
|
1229
|
+
controller.enqueue({
|
|
1230
|
+
type: "tool-call",
|
|
1231
|
+
toolCallId: (_a = toolCall.id) != null ? _a : generateId(),
|
|
1232
|
+
toolName: toolCall.function.name,
|
|
1233
|
+
input: toolCall.function.arguments
|
|
1234
|
+
});
|
|
1235
|
+
toolCall.hasFinished = true;
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1117
1238
|
controller.enqueue({
|
|
1118
1239
|
type: "finish",
|
|
1119
1240
|
finishReason,
|
|
@@ -1126,8 +1247,18 @@ var OpenAIChatLanguageModel = class {
|
|
|
1126
1247
|
request: { body },
|
|
1127
1248
|
response: { headers: responseHeaders }
|
|
1128
1249
|
};
|
|
1250
|
+
return result;
|
|
1129
1251
|
}
|
|
1130
1252
|
};
|
|
1253
|
+
function isOpenAIChatOutputChunk(chunk) {
|
|
1254
|
+
if ("error" in chunk) {
|
|
1255
|
+
return false;
|
|
1256
|
+
}
|
|
1257
|
+
return chunk.choices.some((choice) => {
|
|
1258
|
+
const delta = choice.delta;
|
|
1259
|
+
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;
|
|
1260
|
+
});
|
|
1261
|
+
}
|
|
1131
1262
|
|
|
1132
1263
|
// src/completion/openai-completion-language-model.ts
|
|
1133
1264
|
import {
|
|
@@ -1534,11 +1665,12 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1534
1665
|
include_usage: true
|
|
1535
1666
|
}
|
|
1536
1667
|
};
|
|
1668
|
+
const url = this.config.url({
|
|
1669
|
+
path: "/completions",
|
|
1670
|
+
modelId: this.modelId
|
|
1671
|
+
});
|
|
1537
1672
|
const { responseHeaders, value: response } = await postJsonToApi2({
|
|
1538
|
-
url
|
|
1539
|
-
path: "/completions",
|
|
1540
|
-
modelId: this.modelId
|
|
1541
|
-
}),
|
|
1673
|
+
url,
|
|
1542
1674
|
headers: combineHeaders2(this.config.headers(), options.headers),
|
|
1543
1675
|
body,
|
|
1544
1676
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
@@ -1548,6 +1680,14 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1548
1680
|
abortSignal: options.abortSignal,
|
|
1549
1681
|
fetch: this.config.fetch
|
|
1550
1682
|
});
|
|
1683
|
+
const checkedResponse = await throwIfOpenAIStreamErrorBeforeOutput({
|
|
1684
|
+
stream: response,
|
|
1685
|
+
getError: (chunk) => "error" in chunk ? chunk.error : void 0,
|
|
1686
|
+
isOutputChunk: isOpenAICompletionOutputChunk,
|
|
1687
|
+
url,
|
|
1688
|
+
requestBodyValues: body,
|
|
1689
|
+
responseHeaders
|
|
1690
|
+
});
|
|
1551
1691
|
let finishReason = {
|
|
1552
1692
|
unified: "other",
|
|
1553
1693
|
raw: void 0
|
|
@@ -1555,8 +1695,8 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1555
1695
|
const providerMetadata = { openai: {} };
|
|
1556
1696
|
let usage = void 0;
|
|
1557
1697
|
let isFirstChunk = true;
|
|
1558
|
-
|
|
1559
|
-
stream:
|
|
1698
|
+
const result = {
|
|
1699
|
+
stream: checkedResponse.pipeThrough(
|
|
1560
1700
|
new TransformStream({
|
|
1561
1701
|
start(controller) {
|
|
1562
1702
|
controller.enqueue({ type: "stream-start", warnings });
|
|
@@ -1621,8 +1761,12 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1621
1761
|
request: { body },
|
|
1622
1762
|
response: { headers: responseHeaders }
|
|
1623
1763
|
};
|
|
1764
|
+
return result;
|
|
1624
1765
|
}
|
|
1625
1766
|
};
|
|
1767
|
+
function isOpenAICompletionOutputChunk(chunk) {
|
|
1768
|
+
return !("error" in chunk) && chunk.choices.some((choice) => choice.text.length > 0);
|
|
1769
|
+
}
|
|
1626
1770
|
|
|
1627
1771
|
// src/embedding/openai-embedding-model.ts
|
|
1628
1772
|
import {
|
|
@@ -2762,7 +2906,7 @@ var openaiTools = {
|
|
|
2762
2906
|
|
|
2763
2907
|
// src/responses/openai-responses-language-model.ts
|
|
2764
2908
|
import {
|
|
2765
|
-
APICallError
|
|
2909
|
+
APICallError as APICallError2
|
|
2766
2910
|
} from "@ai-sdk/provider";
|
|
2767
2911
|
import {
|
|
2768
2912
|
combineHeaders as combineHeaders5,
|
|
@@ -3300,7 +3444,7 @@ async function convertToOpenAIResponsesInput({
|
|
|
3300
3444
|
outputValue = output.value;
|
|
3301
3445
|
break;
|
|
3302
3446
|
case "execution-denied":
|
|
3303
|
-
outputValue = (_v = output.reason) != null ? _v : "Tool execution denied.";
|
|
3447
|
+
outputValue = (_v = output.reason) != null ? _v : "Tool call execution denied.";
|
|
3304
3448
|
break;
|
|
3305
3449
|
case "json":
|
|
3306
3450
|
case "error-json":
|
|
@@ -3361,7 +3505,7 @@ async function convertToOpenAIResponsesInput({
|
|
|
3361
3505
|
contentValue = output.value;
|
|
3362
3506
|
break;
|
|
3363
3507
|
case "execution-denied":
|
|
3364
|
-
contentValue = (_w = output.reason) != null ? _w : "Tool execution denied.";
|
|
3508
|
+
contentValue = (_w = output.reason) != null ? _w : "Tool call execution denied.";
|
|
3365
3509
|
break;
|
|
3366
3510
|
case "json":
|
|
3367
3511
|
case "error-json":
|
|
@@ -3478,6 +3622,23 @@ var jsonValueSchema2 = z22.lazy(
|
|
|
3478
3622
|
z22.record(z22.string(), jsonValueSchema2.optional())
|
|
3479
3623
|
])
|
|
3480
3624
|
);
|
|
3625
|
+
var openaiResponsesNestedErrorChunkSchema = z22.object({
|
|
3626
|
+
type: z22.literal("error"),
|
|
3627
|
+
sequence_number: z22.number(),
|
|
3628
|
+
error: z22.object({
|
|
3629
|
+
type: z22.string(),
|
|
3630
|
+
code: z22.string(),
|
|
3631
|
+
message: z22.string(),
|
|
3632
|
+
param: z22.string().nullish()
|
|
3633
|
+
})
|
|
3634
|
+
});
|
|
3635
|
+
var openaiResponsesErrorChunkSchema = z22.object({
|
|
3636
|
+
type: z22.literal("error"),
|
|
3637
|
+
sequence_number: z22.number(),
|
|
3638
|
+
code: z22.string().nullish(),
|
|
3639
|
+
message: z22.string(),
|
|
3640
|
+
param: z22.string().nullish()
|
|
3641
|
+
});
|
|
3481
3642
|
var openaiResponsesChunkSchema = lazySchema20(
|
|
3482
3643
|
() => zodSchema20(
|
|
3483
3644
|
z22.union([
|
|
@@ -3520,6 +3681,7 @@ var openaiResponsesChunkSchema = lazySchema20(
|
|
|
3520
3681
|
}),
|
|
3521
3682
|
z22.object({
|
|
3522
3683
|
type: z22.literal("response.failed"),
|
|
3684
|
+
sequence_number: z22.number(),
|
|
3523
3685
|
response: z22.object({
|
|
3524
3686
|
error: z22.object({
|
|
3525
3687
|
code: z22.string().nullish(),
|
|
@@ -4008,16 +4170,8 @@ var openaiResponsesChunkSchema = lazySchema20(
|
|
|
4008
4170
|
output_index: z22.number(),
|
|
4009
4171
|
diff: z22.string()
|
|
4010
4172
|
}),
|
|
4011
|
-
|
|
4012
|
-
|
|
4013
|
-
sequence_number: z22.number(),
|
|
4014
|
-
error: z22.object({
|
|
4015
|
-
type: z22.string(),
|
|
4016
|
-
code: z22.string(),
|
|
4017
|
-
message: z22.string(),
|
|
4018
|
-
param: z22.string().nullish()
|
|
4019
|
-
})
|
|
4020
|
-
}),
|
|
4173
|
+
openaiResponsesNestedErrorChunkSchema,
|
|
4174
|
+
openaiResponsesErrorChunkSchema,
|
|
4021
4175
|
z22.object({ type: z22.string() }).loose().transform((value) => ({
|
|
4022
4176
|
type: "unknown_chunk",
|
|
4023
4177
|
message: value.type
|
|
@@ -5215,7 +5369,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
5215
5369
|
fetch: this.config.fetch
|
|
5216
5370
|
});
|
|
5217
5371
|
if (response.error) {
|
|
5218
|
-
throw new
|
|
5372
|
+
throw new APICallError2({
|
|
5219
5373
|
message: response.error.message,
|
|
5220
5374
|
url,
|
|
5221
5375
|
requestBodyValues: body,
|
|
@@ -5688,6 +5842,14 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
5688
5842
|
abortSignal: options.abortSignal,
|
|
5689
5843
|
fetch: this.config.fetch
|
|
5690
5844
|
});
|
|
5845
|
+
const checkedResponse = await throwIfOpenAIStreamErrorBeforeOutput({
|
|
5846
|
+
stream: response,
|
|
5847
|
+
getError: (chunk) => isErrorChunk(chunk) || isResponseFailedChunk(chunk) && chunk.response.error != null ? chunk : void 0,
|
|
5848
|
+
isOutputChunk: isResponseOutputChunk,
|
|
5849
|
+
url,
|
|
5850
|
+
requestBodyValues: body,
|
|
5851
|
+
responseHeaders
|
|
5852
|
+
});
|
|
5691
5853
|
const self = this;
|
|
5692
5854
|
const approvalRequestIdToDummyToolCallIdFromPrompt = extractApprovalRequestIdToToolCallIdMapping(options.prompt);
|
|
5693
5855
|
const approvalRequestIdToDummyToolCallIdFromStream = /* @__PURE__ */ new Map();
|
|
@@ -5705,8 +5867,9 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
5705
5867
|
const activeReasoning = {};
|
|
5706
5868
|
let serviceTier;
|
|
5707
5869
|
const hostedToolSearchCallIds = [];
|
|
5708
|
-
|
|
5709
|
-
|
|
5870
|
+
let encounteredStreamError = false;
|
|
5871
|
+
const result = {
|
|
5872
|
+
stream: checkedResponse.pipeThrough(
|
|
5710
5873
|
new TransformStream({
|
|
5711
5874
|
start(controller) {
|
|
5712
5875
|
controller.enqueue({ type: "stream-start", warnings });
|
|
@@ -6026,12 +6189,12 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
6026
6189
|
toolName: toolNameMapping.toCustomToolName("file_search"),
|
|
6027
6190
|
result: {
|
|
6028
6191
|
queries: value.item.queries,
|
|
6029
|
-
results: (_f = (_e = value.item.results) == null ? void 0 : _e.map((
|
|
6030
|
-
attributes:
|
|
6031
|
-
fileId:
|
|
6032
|
-
filename:
|
|
6033
|
-
score:
|
|
6034
|
-
text:
|
|
6192
|
+
results: (_f = (_e = value.item.results) == null ? void 0 : _e.map((result2) => ({
|
|
6193
|
+
attributes: result2.attributes,
|
|
6194
|
+
fileId: result2.file_id,
|
|
6195
|
+
filename: result2.filename,
|
|
6196
|
+
score: result2.score,
|
|
6197
|
+
text: result2.text
|
|
6035
6198
|
}))) != null ? _f : null
|
|
6036
6199
|
}
|
|
6037
6200
|
});
|
|
@@ -6464,6 +6627,21 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
6464
6627
|
raw: incompleteReason != null ? incompleteReason : "error"
|
|
6465
6628
|
};
|
|
6466
6629
|
usage = (_z = value.response.usage) != null ? _z : void 0;
|
|
6630
|
+
if (!encounteredStreamError && value.response.error != null) {
|
|
6631
|
+
encounteredStreamError = true;
|
|
6632
|
+
controller.enqueue({
|
|
6633
|
+
type: "error",
|
|
6634
|
+
error: {
|
|
6635
|
+
type: "response.failed",
|
|
6636
|
+
sequence_number: value.sequence_number,
|
|
6637
|
+
response: {
|
|
6638
|
+
error: value.response.error,
|
|
6639
|
+
incomplete_details: value.response.incomplete_details,
|
|
6640
|
+
service_tier: value.response.service_tier
|
|
6641
|
+
}
|
|
6642
|
+
}
|
|
6643
|
+
});
|
|
6644
|
+
}
|
|
6467
6645
|
} else if (isResponseAnnotationAddedChunk(value)) {
|
|
6468
6646
|
ongoingAnnotations.push(value.annotation);
|
|
6469
6647
|
if (value.annotation.type === "url_citation") {
|
|
@@ -6524,6 +6702,8 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
6524
6702
|
});
|
|
6525
6703
|
}
|
|
6526
6704
|
} else if (isErrorChunk(value)) {
|
|
6705
|
+
encounteredStreamError = true;
|
|
6706
|
+
finishReason = { unified: "error", raw: "error" };
|
|
6527
6707
|
controller.enqueue({ type: "error", error: value });
|
|
6528
6708
|
}
|
|
6529
6709
|
},
|
|
@@ -6547,13 +6727,14 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
6547
6727
|
request: { body },
|
|
6548
6728
|
response: { headers: responseHeaders }
|
|
6549
6729
|
};
|
|
6730
|
+
return result;
|
|
6550
6731
|
}
|
|
6551
6732
|
};
|
|
6552
6733
|
function isTextDeltaChunk(chunk) {
|
|
6553
6734
|
return chunk.type === "response.output_text.delta";
|
|
6554
6735
|
}
|
|
6555
6736
|
function isOpenAIChatCompletionChunk(value) {
|
|
6556
|
-
const chunk =
|
|
6737
|
+
const chunk = asRecord2(value);
|
|
6557
6738
|
return chunk != null && Array.isArray(chunk.choices) && typeof chunk.type !== "string";
|
|
6558
6739
|
}
|
|
6559
6740
|
function createOpenAIResponsesChatCompletionsMismatchError({
|
|
@@ -6563,7 +6744,7 @@ function createOpenAIResponsesChatCompletionsMismatchError({
|
|
|
6563
6744
|
requestBodyValues,
|
|
6564
6745
|
responseHeaders
|
|
6565
6746
|
}) {
|
|
6566
|
-
return new
|
|
6747
|
+
return new APICallError2({
|
|
6567
6748
|
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.",
|
|
6568
6749
|
url,
|
|
6569
6750
|
requestBodyValues,
|
|
@@ -6574,7 +6755,7 @@ function createOpenAIResponsesChatCompletionsMismatchError({
|
|
|
6574
6755
|
isRetryable: false
|
|
6575
6756
|
});
|
|
6576
6757
|
}
|
|
6577
|
-
function
|
|
6758
|
+
function asRecord2(value) {
|
|
6578
6759
|
return typeof value === "object" && value != null ? value : void 0;
|
|
6579
6760
|
}
|
|
6580
6761
|
function isResponseOutputItemDoneChunk(chunk) {
|
|
@@ -6619,6 +6800,9 @@ function isResponseAnnotationAddedChunk(chunk) {
|
|
|
6619
6800
|
function isErrorChunk(chunk) {
|
|
6620
6801
|
return chunk.type === "error";
|
|
6621
6802
|
}
|
|
6803
|
+
function isResponseOutputChunk(chunk) {
|
|
6804
|
+
return !(chunk.type === "response.created" || chunk.type === "response.failed" || chunk.type === "error" || chunk.type === "unknown_chunk");
|
|
6805
|
+
}
|
|
6622
6806
|
function mapWebSearchOutput(action) {
|
|
6623
6807
|
var _a;
|
|
6624
6808
|
if (action == null) {
|
|
@@ -7022,7 +7206,7 @@ var OpenAITranscriptionModel = class {
|
|
|
7022
7206
|
};
|
|
7023
7207
|
|
|
7024
7208
|
// src/version.ts
|
|
7025
|
-
var VERSION = true ? "3.0.
|
|
7209
|
+
var VERSION = true ? "3.0.82" : "0.0.0-test";
|
|
7026
7210
|
|
|
7027
7211
|
// src/openai-provider.ts
|
|
7028
7212
|
function createOpenAI(options = {}) {
|