@ai-sdk/openai 4.0.24 → 4.0.25
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.ts +10 -2
- package/dist/index.js +88 -7
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +10 -2
- package/dist/internal/index.js +87 -6
- package/dist/internal/index.js.map +1 -1
- package/package.json +4 -4
- package/src/openai-stream-error.ts +84 -3
- package/src/responses/openai-responses-api.ts +9 -0
- package/src/responses/openai-responses-language-model.ts +8 -0
- package/src/tool/programmatic-tool-calling.ts +23 -1
package/dist/internal/index.d.ts
CHANGED
|
@@ -425,6 +425,14 @@ declare const openaiResponsesChunkSchema: _ai_sdk_provider_utils.LazySchema<{
|
|
|
425
425
|
model: string;
|
|
426
426
|
service_tier?: string | null | undefined;
|
|
427
427
|
};
|
|
428
|
+
} | {
|
|
429
|
+
type: "response.in_progress";
|
|
430
|
+
response: {
|
|
431
|
+
id: string;
|
|
432
|
+
created_at: number;
|
|
433
|
+
model: string;
|
|
434
|
+
service_tier?: string | null | undefined;
|
|
435
|
+
};
|
|
428
436
|
} | {
|
|
429
437
|
type: "response.output_item.added";
|
|
430
438
|
output_index: number;
|
|
@@ -1462,7 +1470,7 @@ declare const programmaticToolCallingOutputSchema: _ai_sdk_provider_utils.LazySc
|
|
|
1462
1470
|
result: string;
|
|
1463
1471
|
status: "completed" | "incomplete";
|
|
1464
1472
|
}>;
|
|
1465
|
-
declare const programmaticToolCalling: () => _ai_sdk_provider_utils.ProviderExecutedTool<{
|
|
1473
|
+
declare const programmaticToolCalling: () => _ai_sdk_provider_utils.Experimental_ToolCallerTool<_ai_sdk_provider_utils.ProviderExecutedTool<{
|
|
1466
1474
|
/**
|
|
1467
1475
|
* JavaScript source generated and executed by OpenAI.
|
|
1468
1476
|
*/
|
|
@@ -1480,7 +1488,7 @@ declare const programmaticToolCalling: () => _ai_sdk_provider_utils.ProviderExec
|
|
|
1480
1488
|
* Whether the program completed or stopped before producing a final result.
|
|
1481
1489
|
*/
|
|
1482
1490
|
status: "completed" | "incomplete";
|
|
1483
|
-
}, {}
|
|
1491
|
+
}, {}>>;
|
|
1484
1492
|
|
|
1485
1493
|
declare const webSearchArgsSchema: _ai_sdk_provider_utils.LazySchema<{
|
|
1486
1494
|
externalWebAccess?: boolean | undefined;
|
package/dist/internal/index.js
CHANGED
|
@@ -74,15 +74,28 @@ async function throwIfOpenAIStreamErrorBeforeOutput({
|
|
|
74
74
|
stream,
|
|
75
75
|
getError,
|
|
76
76
|
isOutputChunk,
|
|
77
|
+
isAcceptedChunk,
|
|
78
|
+
acceptedGraceMs = 50,
|
|
77
79
|
url,
|
|
78
80
|
requestBodyValues,
|
|
79
81
|
responseHeaders
|
|
80
82
|
}) {
|
|
81
83
|
const [streamForEarlyError, streamForConsumer] = stream.tee();
|
|
82
84
|
const reader = streamForEarlyError.getReader();
|
|
85
|
+
let drainAfterError = false;
|
|
83
86
|
try {
|
|
87
|
+
let accepted = false;
|
|
84
88
|
while (true) {
|
|
85
|
-
|
|
89
|
+
let result;
|
|
90
|
+
if (accepted) {
|
|
91
|
+
const raced = await raceWithTimeout(reader.read(), acceptedGraceMs);
|
|
92
|
+
if (raced.timedOut) {
|
|
93
|
+
return streamForConsumer;
|
|
94
|
+
}
|
|
95
|
+
result = raced.value;
|
|
96
|
+
} else {
|
|
97
|
+
result = await reader.read();
|
|
98
|
+
}
|
|
86
99
|
if (result.done) {
|
|
87
100
|
return streamForConsumer;
|
|
88
101
|
}
|
|
@@ -92,7 +105,10 @@ async function throwIfOpenAIStreamErrorBeforeOutput({
|
|
|
92
105
|
}
|
|
93
106
|
const errorFrame = getError(chunk.value);
|
|
94
107
|
if (errorFrame != null) {
|
|
95
|
-
|
|
108
|
+
drainAfterError = true;
|
|
109
|
+
drainReader(reader).catch(() => {
|
|
110
|
+
});
|
|
111
|
+
drainReader(streamForConsumer.getReader()).catch(() => {
|
|
96
112
|
});
|
|
97
113
|
throw createOpenAIStreamError({
|
|
98
114
|
frame: errorFrame,
|
|
@@ -104,13 +120,46 @@ async function throwIfOpenAIStreamErrorBeforeOutput({
|
|
|
104
120
|
if (isOutputChunk(chunk.value)) {
|
|
105
121
|
return streamForConsumer;
|
|
106
122
|
}
|
|
123
|
+
if (!accepted && (isAcceptedChunk == null ? void 0 : isAcceptedChunk(chunk.value)) === true) {
|
|
124
|
+
accepted = true;
|
|
125
|
+
}
|
|
107
126
|
}
|
|
108
127
|
} finally {
|
|
109
|
-
|
|
110
|
-
|
|
128
|
+
if (!drainAfterError) {
|
|
129
|
+
reader.cancel().catch(() => {
|
|
130
|
+
});
|
|
131
|
+
reader.releaseLock();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
async function drainReader(reader) {
|
|
136
|
+
try {
|
|
137
|
+
while (!(await reader.read()).done) {
|
|
138
|
+
}
|
|
139
|
+
} catch (e) {
|
|
140
|
+
} finally {
|
|
111
141
|
reader.releaseLock();
|
|
112
142
|
}
|
|
113
143
|
}
|
|
144
|
+
async function raceWithTimeout(promise, timeoutMs) {
|
|
145
|
+
let timer;
|
|
146
|
+
const wrapped = promise.then((value) => ({ timedOut: false, value }));
|
|
147
|
+
try {
|
|
148
|
+
const raced = await Promise.race([
|
|
149
|
+
wrapped,
|
|
150
|
+
new Promise((resolve) => {
|
|
151
|
+
timer = setTimeout(() => resolve({ timedOut: true }), timeoutMs);
|
|
152
|
+
})
|
|
153
|
+
]);
|
|
154
|
+
if (raced.timedOut) {
|
|
155
|
+
wrapped.catch(() => {
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
return raced;
|
|
159
|
+
} finally {
|
|
160
|
+
clearTimeout(timer);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
114
163
|
function createOpenAIStreamError({
|
|
115
164
|
frame,
|
|
116
165
|
url,
|
|
@@ -3384,6 +3433,7 @@ var toolSearchToolFactory = createProviderDefinedToolFactoryWithOutputSchema5({
|
|
|
3384
3433
|
// src/tool/programmatic-tool-calling.ts
|
|
3385
3434
|
import {
|
|
3386
3435
|
createProviderExecutedToolFactory,
|
|
3436
|
+
experimental_toolCaller,
|
|
3387
3437
|
lazySchema as lazySchema17,
|
|
3388
3438
|
zodSchema as zodSchema17
|
|
3389
3439
|
} from "@ai-sdk/provider-utils";
|
|
@@ -3410,7 +3460,25 @@ var programmaticToolCallingFactory = createProviderExecutedToolFactory({
|
|
|
3410
3460
|
outputSchema: programmaticToolCallingOutputSchema,
|
|
3411
3461
|
supportsDeferredResults: true
|
|
3412
3462
|
});
|
|
3413
|
-
var programmaticToolCalling = () => programmaticToolCallingFactory({})
|
|
3463
|
+
var programmaticToolCalling = () => experimental_toolCaller(programmaticToolCallingFactory({}), {
|
|
3464
|
+
type: "provider",
|
|
3465
|
+
prepareProviderOptions: (providerOptions) => {
|
|
3466
|
+
var _a;
|
|
3467
|
+
const openaiOptions = providerOptions == null ? void 0 : providerOptions.openai;
|
|
3468
|
+
return {
|
|
3469
|
+
...providerOptions,
|
|
3470
|
+
openai: {
|
|
3471
|
+
...openaiOptions,
|
|
3472
|
+
allowedCallers: [
|
|
3473
|
+
.../* @__PURE__ */ new Set([
|
|
3474
|
+
...(_a = openaiOptions == null ? void 0 : openaiOptions.allowedCallers) != null ? _a : [],
|
|
3475
|
+
"programmatic"
|
|
3476
|
+
])
|
|
3477
|
+
]
|
|
3478
|
+
}
|
|
3479
|
+
};
|
|
3480
|
+
}
|
|
3481
|
+
});
|
|
3414
3482
|
|
|
3415
3483
|
// src/responses/convert-to-openai-responses-input.ts
|
|
3416
3484
|
function serializeToolCallArguments2(input) {
|
|
@@ -4548,6 +4616,15 @@ var openaiResponsesChunkSchema = lazySchema18(
|
|
|
4548
4616
|
service_tier: z20.string().nullish()
|
|
4549
4617
|
})
|
|
4550
4618
|
}),
|
|
4619
|
+
z20.object({
|
|
4620
|
+
type: z20.literal("response.in_progress"),
|
|
4621
|
+
response: z20.object({
|
|
4622
|
+
id: z20.string(),
|
|
4623
|
+
created_at: z20.number(),
|
|
4624
|
+
model: z20.string(),
|
|
4625
|
+
service_tier: z20.string().nullish()
|
|
4626
|
+
})
|
|
4627
|
+
}),
|
|
4551
4628
|
z20.object({
|
|
4552
4629
|
type: z20.literal("response.output_item.added"),
|
|
4553
4630
|
output_index: z20.number(),
|
|
@@ -7304,6 +7381,7 @@ var OpenAIResponsesLanguageModel = class _OpenAIResponsesLanguageModel {
|
|
|
7304
7381
|
stream: response,
|
|
7305
7382
|
getError: (chunk) => isErrorChunk(chunk) || isResponseFailedChunk(chunk) && chunk.response.error != null ? chunk : void 0,
|
|
7306
7383
|
isOutputChunk: isResponseOutputChunk,
|
|
7384
|
+
isAcceptedChunk: isResponseInProgressChunk,
|
|
7307
7385
|
url,
|
|
7308
7386
|
requestBodyValues: body,
|
|
7309
7387
|
responseHeaders
|
|
@@ -8345,8 +8423,11 @@ function isResponseAnnotationAddedChunk(chunk) {
|
|
|
8345
8423
|
function isErrorChunk(chunk) {
|
|
8346
8424
|
return chunk.type === "error";
|
|
8347
8425
|
}
|
|
8426
|
+
function isResponseInProgressChunk(chunk) {
|
|
8427
|
+
return chunk.type === "response.in_progress";
|
|
8428
|
+
}
|
|
8348
8429
|
function isResponseOutputChunk(chunk) {
|
|
8349
|
-
return !(chunk.type === "response.created" || chunk.type === "response.failed" || chunk.type === "error" || chunk.type === "unknown_chunk");
|
|
8430
|
+
return !(chunk.type === "response.created" || chunk.type === "response.in_progress" || chunk.type === "response.failed" || chunk.type === "error" || chunk.type === "unknown_chunk");
|
|
8350
8431
|
}
|
|
8351
8432
|
function mapWebSearchOutput(action) {
|
|
8352
8433
|
var _a;
|