@ai-sdk/openai 2.0.0-canary.1 → 2.0.0-canary.11
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 +120 -0
- package/dist/index.d.mts +27 -103
- package/dist/index.d.ts +27 -103
- package/dist/index.js +967 -942
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +987 -952
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +369 -0
- package/dist/internal/index.d.ts +369 -0
- package/{internal/dist → dist/internal}/index.js +964 -947
- package/dist/internal/index.js.map +1 -0
- package/{internal/dist → dist/internal}/index.mjs +977 -954
- package/dist/internal/index.mjs.map +1 -0
- package/package.json +15 -15
- package/internal/dist/index.d.mts +0 -290
- package/internal/dist/index.d.ts +0 -290
- package/internal/dist/index.js.map +0 -1
- package/internal/dist/index.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -6,8 +6,7 @@ import {
|
|
|
6
6
|
|
|
7
7
|
// src/openai-chat-language-model.ts
|
|
8
8
|
import {
|
|
9
|
-
InvalidResponseDataError
|
|
10
|
-
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
9
|
+
InvalidResponseDataError
|
|
11
10
|
} from "@ai-sdk/provider";
|
|
12
11
|
import {
|
|
13
12
|
combineHeaders,
|
|
@@ -15,18 +14,18 @@ import {
|
|
|
15
14
|
createJsonResponseHandler,
|
|
16
15
|
generateId,
|
|
17
16
|
isParsableJson,
|
|
17
|
+
parseProviderOptions,
|
|
18
18
|
postJsonToApi
|
|
19
19
|
} from "@ai-sdk/provider-utils";
|
|
20
|
-
import { z as
|
|
20
|
+
import { z as z3 } from "zod";
|
|
21
21
|
|
|
22
22
|
// src/convert-to-openai-chat-messages.ts
|
|
23
23
|
import {
|
|
24
24
|
UnsupportedFunctionalityError
|
|
25
25
|
} from "@ai-sdk/provider";
|
|
26
|
-
import {
|
|
26
|
+
import { convertToBase64 } from "@ai-sdk/provider-utils";
|
|
27
27
|
function convertToOpenAIChatMessages({
|
|
28
28
|
prompt,
|
|
29
|
-
useLegacyFunctionCalling = false,
|
|
30
29
|
systemMessageMode = "system"
|
|
31
30
|
}) {
|
|
32
31
|
const messages = [];
|
|
@@ -67,55 +66,71 @@ function convertToOpenAIChatMessages({
|
|
|
67
66
|
messages.push({
|
|
68
67
|
role: "user",
|
|
69
68
|
content: content.map((part, index) => {
|
|
70
|
-
var _a, _b, _c
|
|
69
|
+
var _a, _b, _c;
|
|
71
70
|
switch (part.type) {
|
|
72
71
|
case "text": {
|
|
73
72
|
return { type: "text", text: part.text };
|
|
74
73
|
}
|
|
75
|
-
case "image": {
|
|
76
|
-
return {
|
|
77
|
-
type: "image_url",
|
|
78
|
-
image_url: {
|
|
79
|
-
url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase64(part.image)}`,
|
|
80
|
-
// OpenAI specific extension: image detail
|
|
81
|
-
detail: (_c = (_b = part.providerMetadata) == null ? void 0 : _b.openai) == null ? void 0 : _c.imageDetail
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
74
|
case "file": {
|
|
86
|
-
if (part.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
type: "input_audio",
|
|
102
|
-
input_audio: { data: part.data, format: "mp3" }
|
|
103
|
-
};
|
|
75
|
+
if (part.mediaType.startsWith("image/")) {
|
|
76
|
+
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
|
77
|
+
return {
|
|
78
|
+
type: "image_url",
|
|
79
|
+
image_url: {
|
|
80
|
+
url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${convertToBase64(part.data)}`,
|
|
81
|
+
// OpenAI specific extension: image detail
|
|
82
|
+
detail: (_b = (_a = part.providerOptions) == null ? void 0 : _a.openai) == null ? void 0 : _b.imageDetail
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
} else if (part.mediaType.startsWith("audio/")) {
|
|
86
|
+
if (part.data instanceof URL) {
|
|
87
|
+
throw new UnsupportedFunctionalityError({
|
|
88
|
+
functionality: "audio file parts with URLs"
|
|
89
|
+
});
|
|
104
90
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
91
|
+
switch (part.mediaType) {
|
|
92
|
+
case "audio/wav": {
|
|
93
|
+
return {
|
|
94
|
+
type: "input_audio",
|
|
95
|
+
input_audio: {
|
|
96
|
+
data: convertToBase64(part.data),
|
|
97
|
+
format: "wav"
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
case "audio/mp3":
|
|
102
|
+
case "audio/mpeg": {
|
|
103
|
+
return {
|
|
104
|
+
type: "input_audio",
|
|
105
|
+
input_audio: {
|
|
106
|
+
data: convertToBase64(part.data),
|
|
107
|
+
format: "mp3"
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
default: {
|
|
112
|
+
throw new UnsupportedFunctionalityError({
|
|
113
|
+
functionality: `audio content parts with media type ${part.mediaType}`
|
|
114
|
+
});
|
|
115
|
+
}
|
|
113
116
|
}
|
|
114
|
-
|
|
117
|
+
} else if (part.mediaType === "application/pdf") {
|
|
118
|
+
if (part.data instanceof URL) {
|
|
115
119
|
throw new UnsupportedFunctionalityError({
|
|
116
|
-
functionality:
|
|
120
|
+
functionality: "PDF file parts with URLs"
|
|
117
121
|
});
|
|
118
122
|
}
|
|
123
|
+
return {
|
|
124
|
+
type: "file",
|
|
125
|
+
file: {
|
|
126
|
+
filename: (_c = part.filename) != null ? _c : `part-${index}.pdf`,
|
|
127
|
+
file_data: `data:application/pdf;base64,${part.data}`
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
} else {
|
|
131
|
+
throw new UnsupportedFunctionalityError({
|
|
132
|
+
functionality: `file part media type ${part.mediaType}`
|
|
133
|
+
});
|
|
119
134
|
}
|
|
120
135
|
}
|
|
121
136
|
}
|
|
@@ -145,41 +160,20 @@ function convertToOpenAIChatMessages({
|
|
|
145
160
|
}
|
|
146
161
|
}
|
|
147
162
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
messages.push({
|
|
155
|
-
role: "assistant",
|
|
156
|
-
content: text,
|
|
157
|
-
function_call: toolCalls.length > 0 ? toolCalls[0].function : void 0
|
|
158
|
-
});
|
|
159
|
-
} else {
|
|
160
|
-
messages.push({
|
|
161
|
-
role: "assistant",
|
|
162
|
-
content: text,
|
|
163
|
-
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
164
|
-
});
|
|
165
|
-
}
|
|
163
|
+
messages.push({
|
|
164
|
+
role: "assistant",
|
|
165
|
+
content: text,
|
|
166
|
+
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
167
|
+
});
|
|
166
168
|
break;
|
|
167
169
|
}
|
|
168
170
|
case "tool": {
|
|
169
171
|
for (const toolResponse of content) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
});
|
|
176
|
-
} else {
|
|
177
|
-
messages.push({
|
|
178
|
-
role: "tool",
|
|
179
|
-
tool_call_id: toolResponse.toolCallId,
|
|
180
|
-
content: JSON.stringify(toolResponse.result)
|
|
181
|
-
});
|
|
182
|
-
}
|
|
172
|
+
messages.push({
|
|
173
|
+
role: "tool",
|
|
174
|
+
tool_call_id: toolResponse.toolCallId,
|
|
175
|
+
content: JSON.stringify(toolResponse.result)
|
|
176
|
+
});
|
|
183
177
|
}
|
|
184
178
|
break;
|
|
185
179
|
}
|
|
@@ -192,17 +186,17 @@ function convertToOpenAIChatMessages({
|
|
|
192
186
|
return { messages, warnings };
|
|
193
187
|
}
|
|
194
188
|
|
|
195
|
-
// src/
|
|
196
|
-
function
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}
|
|
189
|
+
// src/get-response-metadata.ts
|
|
190
|
+
function getResponseMetadata({
|
|
191
|
+
id,
|
|
192
|
+
model,
|
|
193
|
+
created
|
|
194
|
+
}) {
|
|
195
|
+
return {
|
|
196
|
+
id: id != null ? id : void 0,
|
|
197
|
+
modelId: model != null ? model : void 0,
|
|
198
|
+
timestamp: created != null ? new Date(created * 1e3) : void 0
|
|
199
|
+
};
|
|
206
200
|
}
|
|
207
201
|
|
|
208
202
|
// src/map-openai-finish-reason.ts
|
|
@@ -222,18 +216,59 @@ function mapOpenAIFinishReason(finishReason) {
|
|
|
222
216
|
}
|
|
223
217
|
}
|
|
224
218
|
|
|
225
|
-
// src/openai-
|
|
219
|
+
// src/openai-chat-options.ts
|
|
226
220
|
import { z } from "zod";
|
|
221
|
+
var openaiProviderOptions = z.object({
|
|
222
|
+
/**
|
|
223
|
+
* Modify the likelihood of specified tokens appearing in the completion.
|
|
224
|
+
*
|
|
225
|
+
* Accepts a JSON object that maps tokens (specified by their token ID in
|
|
226
|
+
* the GPT tokenizer) to an associated bias value from -100 to 100.
|
|
227
|
+
*/
|
|
228
|
+
logitBias: z.record(z.coerce.number(), z.number()).optional(),
|
|
229
|
+
/**
|
|
230
|
+
* Whether to enable parallel function calling during tool use. Default to true.
|
|
231
|
+
*/
|
|
232
|
+
parallelToolCalls: z.boolean().optional(),
|
|
233
|
+
/**
|
|
234
|
+
* A unique identifier representing your end-user, which can help OpenAI to
|
|
235
|
+
* monitor and detect abuse.
|
|
236
|
+
*/
|
|
237
|
+
user: z.string().optional(),
|
|
238
|
+
/**
|
|
239
|
+
* Reasoning effort for reasoning models. Defaults to `medium`.
|
|
240
|
+
*/
|
|
241
|
+
reasoningEffort: z.enum(["low", "medium", "high"]).optional(),
|
|
242
|
+
/**
|
|
243
|
+
* Maximum number of completion tokens to generate. Useful for reasoning models.
|
|
244
|
+
*/
|
|
245
|
+
maxCompletionTokens: z.number().optional(),
|
|
246
|
+
/**
|
|
247
|
+
* Whether to enable persistence in responses API.
|
|
248
|
+
*/
|
|
249
|
+
store: z.boolean().optional(),
|
|
250
|
+
/**
|
|
251
|
+
* Metadata to associate with the request.
|
|
252
|
+
*/
|
|
253
|
+
metadata: z.record(z.string()).optional(),
|
|
254
|
+
/**
|
|
255
|
+
* Parameters for prediction mode.
|
|
256
|
+
*/
|
|
257
|
+
prediction: z.record(z.any()).optional()
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// src/openai-error.ts
|
|
261
|
+
import { z as z2 } from "zod";
|
|
227
262
|
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
|
228
|
-
var openaiErrorDataSchema =
|
|
229
|
-
error:
|
|
230
|
-
message:
|
|
263
|
+
var openaiErrorDataSchema = z2.object({
|
|
264
|
+
error: z2.object({
|
|
265
|
+
message: z2.string(),
|
|
231
266
|
// The additional information below is handled loosely to support
|
|
232
267
|
// OpenAI-compatible providers that have slightly different error
|
|
233
268
|
// responses:
|
|
234
|
-
type:
|
|
235
|
-
param:
|
|
236
|
-
code:
|
|
269
|
+
type: z2.string().nullish(),
|
|
270
|
+
param: z2.any().nullish(),
|
|
271
|
+
code: z2.union([z2.string(), z2.number()]).nullish()
|
|
237
272
|
})
|
|
238
273
|
});
|
|
239
274
|
var openaiFailedResponseHandler = createJsonErrorResponseHandler({
|
|
@@ -241,76 +276,19 @@ var openaiFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
241
276
|
errorToMessage: (data) => data.error.message
|
|
242
277
|
});
|
|
243
278
|
|
|
244
|
-
// src/get-response-metadata.ts
|
|
245
|
-
function getResponseMetadata({
|
|
246
|
-
id,
|
|
247
|
-
model,
|
|
248
|
-
created
|
|
249
|
-
}) {
|
|
250
|
-
return {
|
|
251
|
-
id: id != null ? id : void 0,
|
|
252
|
-
modelId: model != null ? model : void 0,
|
|
253
|
-
timestamp: created != null ? new Date(created * 1e3) : void 0
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
|
|
257
279
|
// src/openai-prepare-tools.ts
|
|
258
280
|
import {
|
|
259
281
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
260
282
|
} from "@ai-sdk/provider";
|
|
261
283
|
function prepareTools({
|
|
262
|
-
|
|
263
|
-
|
|
284
|
+
tools,
|
|
285
|
+
toolChoice,
|
|
264
286
|
structuredOutputs
|
|
265
287
|
}) {
|
|
266
|
-
|
|
267
|
-
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
|
288
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
268
289
|
const toolWarnings = [];
|
|
269
290
|
if (tools == null) {
|
|
270
|
-
return { tools: void 0,
|
|
271
|
-
}
|
|
272
|
-
const toolChoice = mode.toolChoice;
|
|
273
|
-
if (useLegacyFunctionCalling) {
|
|
274
|
-
const openaiFunctions = [];
|
|
275
|
-
for (const tool of tools) {
|
|
276
|
-
if (tool.type === "provider-defined") {
|
|
277
|
-
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
278
|
-
} else {
|
|
279
|
-
openaiFunctions.push({
|
|
280
|
-
name: tool.name,
|
|
281
|
-
description: tool.description,
|
|
282
|
-
parameters: tool.parameters
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
if (toolChoice == null) {
|
|
287
|
-
return {
|
|
288
|
-
functions: openaiFunctions,
|
|
289
|
-
function_call: void 0,
|
|
290
|
-
toolWarnings
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
const type2 = toolChoice.type;
|
|
294
|
-
switch (type2) {
|
|
295
|
-
case "auto":
|
|
296
|
-
case "none":
|
|
297
|
-
case void 0:
|
|
298
|
-
return {
|
|
299
|
-
functions: openaiFunctions,
|
|
300
|
-
function_call: void 0,
|
|
301
|
-
toolWarnings
|
|
302
|
-
};
|
|
303
|
-
case "required":
|
|
304
|
-
throw new UnsupportedFunctionalityError2({
|
|
305
|
-
functionality: "useLegacyFunctionCalling and toolChoice: required"
|
|
306
|
-
});
|
|
307
|
-
default:
|
|
308
|
-
return {
|
|
309
|
-
functions: openaiFunctions,
|
|
310
|
-
function_call: { name: toolChoice.toolName },
|
|
311
|
-
toolWarnings
|
|
312
|
-
};
|
|
313
|
-
}
|
|
291
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
314
292
|
}
|
|
315
293
|
const openaiTools2 = [];
|
|
316
294
|
for (const tool of tools) {
|
|
@@ -329,18 +307,18 @@ function prepareTools({
|
|
|
329
307
|
}
|
|
330
308
|
}
|
|
331
309
|
if (toolChoice == null) {
|
|
332
|
-
return { tools: openaiTools2,
|
|
310
|
+
return { tools: openaiTools2, toolChoice: void 0, toolWarnings };
|
|
333
311
|
}
|
|
334
312
|
const type = toolChoice.type;
|
|
335
313
|
switch (type) {
|
|
336
314
|
case "auto":
|
|
337
315
|
case "none":
|
|
338
316
|
case "required":
|
|
339
|
-
return { tools: openaiTools2,
|
|
317
|
+
return { tools: openaiTools2, toolChoice: type, toolWarnings };
|
|
340
318
|
case "tool":
|
|
341
319
|
return {
|
|
342
320
|
tools: openaiTools2,
|
|
343
|
-
|
|
321
|
+
toolChoice: {
|
|
344
322
|
type: "function",
|
|
345
323
|
function: {
|
|
346
324
|
name: toolChoice.toolName
|
|
@@ -351,7 +329,7 @@ function prepareTools({
|
|
|
351
329
|
default: {
|
|
352
330
|
const _exhaustiveCheck = type;
|
|
353
331
|
throw new UnsupportedFunctionalityError2({
|
|
354
|
-
functionality: `
|
|
332
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
355
333
|
});
|
|
356
334
|
}
|
|
357
335
|
}
|
|
@@ -365,26 +343,17 @@ var OpenAIChatLanguageModel = class {
|
|
|
365
343
|
this.settings = settings;
|
|
366
344
|
this.config = config;
|
|
367
345
|
}
|
|
368
|
-
get supportsStructuredOutputs() {
|
|
369
|
-
var _a;
|
|
370
|
-
return (_a = this.settings.structuredOutputs) != null ? _a : isReasoningModel(this.modelId);
|
|
371
|
-
}
|
|
372
|
-
get defaultObjectGenerationMode() {
|
|
373
|
-
if (isAudioModel(this.modelId)) {
|
|
374
|
-
return "tool";
|
|
375
|
-
}
|
|
376
|
-
return this.supportsStructuredOutputs ? "json" : "tool";
|
|
377
|
-
}
|
|
378
346
|
get provider() {
|
|
379
347
|
return this.config.provider;
|
|
380
348
|
}
|
|
381
|
-
|
|
382
|
-
return
|
|
349
|
+
async getSupportedUrls() {
|
|
350
|
+
return {
|
|
351
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
352
|
+
};
|
|
383
353
|
}
|
|
384
354
|
getArgs({
|
|
385
|
-
mode,
|
|
386
355
|
prompt,
|
|
387
|
-
|
|
356
|
+
maxOutputTokens,
|
|
388
357
|
temperature,
|
|
389
358
|
topP,
|
|
390
359
|
topK,
|
|
@@ -393,39 +362,33 @@ var OpenAIChatLanguageModel = class {
|
|
|
393
362
|
stopSequences,
|
|
394
363
|
responseFormat,
|
|
395
364
|
seed,
|
|
396
|
-
|
|
365
|
+
tools,
|
|
366
|
+
toolChoice,
|
|
367
|
+
providerOptions
|
|
397
368
|
}) {
|
|
398
|
-
var _a, _b, _c
|
|
399
|
-
const type = mode.type;
|
|
369
|
+
var _a, _b, _c;
|
|
400
370
|
const warnings = [];
|
|
371
|
+
const openaiOptions = (_a = parseProviderOptions({
|
|
372
|
+
provider: "openai",
|
|
373
|
+
providerOptions,
|
|
374
|
+
schema: openaiProviderOptions
|
|
375
|
+
})) != null ? _a : {};
|
|
401
376
|
if (topK != null) {
|
|
402
377
|
warnings.push({
|
|
403
378
|
type: "unsupported-setting",
|
|
404
379
|
setting: "topK"
|
|
405
380
|
});
|
|
406
381
|
}
|
|
407
|
-
if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !this.
|
|
382
|
+
if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !this.settings.structuredOutputs) {
|
|
408
383
|
warnings.push({
|
|
409
384
|
type: "unsupported-setting",
|
|
410
385
|
setting: "responseFormat",
|
|
411
386
|
details: "JSON response format schema is only supported with structuredOutputs"
|
|
412
387
|
});
|
|
413
388
|
}
|
|
414
|
-
const useLegacyFunctionCalling = this.settings.useLegacyFunctionCalling;
|
|
415
|
-
if (useLegacyFunctionCalling && this.settings.parallelToolCalls === true) {
|
|
416
|
-
throw new UnsupportedFunctionalityError3({
|
|
417
|
-
functionality: "useLegacyFunctionCalling with parallelToolCalls"
|
|
418
|
-
});
|
|
419
|
-
}
|
|
420
|
-
if (useLegacyFunctionCalling && this.supportsStructuredOutputs) {
|
|
421
|
-
throw new UnsupportedFunctionalityError3({
|
|
422
|
-
functionality: "structuredOutputs with useLegacyFunctionCalling"
|
|
423
|
-
});
|
|
424
|
-
}
|
|
425
389
|
const { messages, warnings: messageWarnings } = convertToOpenAIChatMessages(
|
|
426
390
|
{
|
|
427
391
|
prompt,
|
|
428
|
-
useLegacyFunctionCalling,
|
|
429
392
|
systemMessageMode: getSystemMessageMode(this.modelId)
|
|
430
393
|
}
|
|
431
394
|
);
|
|
@@ -434,35 +397,36 @@ var OpenAIChatLanguageModel = class {
|
|
|
434
397
|
// model id:
|
|
435
398
|
model: this.modelId,
|
|
436
399
|
// model specific settings:
|
|
437
|
-
logit_bias:
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
user: this.settings.user,
|
|
441
|
-
parallel_tool_calls: this.settings.parallelToolCalls,
|
|
400
|
+
logit_bias: openaiOptions.logitBias,
|
|
401
|
+
user: openaiOptions.user,
|
|
402
|
+
parallel_tool_calls: openaiOptions.parallelToolCalls,
|
|
442
403
|
// standardized settings:
|
|
443
|
-
max_tokens:
|
|
404
|
+
max_tokens: maxOutputTokens,
|
|
444
405
|
temperature,
|
|
445
406
|
top_p: topP,
|
|
446
407
|
frequency_penalty: frequencyPenalty,
|
|
447
408
|
presence_penalty: presencePenalty,
|
|
448
|
-
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ?
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
409
|
+
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? (
|
|
410
|
+
// TODO convert into provider option
|
|
411
|
+
this.settings.structuredOutputs && responseFormat.schema != null ? {
|
|
412
|
+
type: "json_schema",
|
|
413
|
+
json_schema: {
|
|
414
|
+
schema: responseFormat.schema,
|
|
415
|
+
strict: true,
|
|
416
|
+
name: (_b = responseFormat.name) != null ? _b : "response",
|
|
417
|
+
description: responseFormat.description
|
|
418
|
+
}
|
|
419
|
+
} : { type: "json_object" }
|
|
420
|
+
) : void 0,
|
|
457
421
|
stop: stopSequences,
|
|
458
422
|
seed,
|
|
459
423
|
// openai specific settings:
|
|
460
|
-
// TODO remove in next major version; we auto-map
|
|
461
|
-
max_completion_tokens:
|
|
462
|
-
store:
|
|
463
|
-
metadata:
|
|
464
|
-
prediction:
|
|
465
|
-
reasoning_effort:
|
|
424
|
+
// TODO remove in next major version; we auto-map maxOutputTokens now
|
|
425
|
+
max_completion_tokens: openaiOptions.maxCompletionTokens,
|
|
426
|
+
store: openaiOptions.store,
|
|
427
|
+
metadata: openaiOptions.metadata,
|
|
428
|
+
prediction: openaiOptions.prediction,
|
|
429
|
+
reasoning_effort: openaiOptions.reasoningEffort,
|
|
466
430
|
// messages:
|
|
467
431
|
messages
|
|
468
432
|
};
|
|
@@ -506,102 +470,39 @@ var OpenAIChatLanguageModel = class {
|
|
|
506
470
|
message: "logitBias is not supported for reasoning models"
|
|
507
471
|
});
|
|
508
472
|
}
|
|
509
|
-
if (baseArgs.logprobs != null) {
|
|
510
|
-
baseArgs.logprobs = void 0;
|
|
511
|
-
warnings.push({
|
|
512
|
-
type: "other",
|
|
513
|
-
message: "logprobs is not supported for reasoning models"
|
|
514
|
-
});
|
|
515
|
-
}
|
|
516
|
-
if (baseArgs.top_logprobs != null) {
|
|
517
|
-
baseArgs.top_logprobs = void 0;
|
|
518
|
-
warnings.push({
|
|
519
|
-
type: "other",
|
|
520
|
-
message: "topLogprobs is not supported for reasoning models"
|
|
521
|
-
});
|
|
522
|
-
}
|
|
523
473
|
if (baseArgs.max_tokens != null) {
|
|
524
474
|
if (baseArgs.max_completion_tokens == null) {
|
|
525
475
|
baseArgs.max_completion_tokens = baseArgs.max_tokens;
|
|
526
476
|
}
|
|
527
477
|
baseArgs.max_tokens = void 0;
|
|
528
478
|
}
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
479
|
+
} else if (this.modelId.startsWith("gpt-4o-search-preview") || this.modelId.startsWith("gpt-4o-mini-search-preview")) {
|
|
480
|
+
if (baseArgs.temperature != null) {
|
|
481
|
+
baseArgs.temperature = void 0;
|
|
482
|
+
warnings.push({
|
|
483
|
+
type: "unsupported-setting",
|
|
484
|
+
setting: "temperature",
|
|
485
|
+
details: "temperature is not supported for the search preview models and has been removed."
|
|
536
486
|
});
|
|
537
|
-
return {
|
|
538
|
-
args: {
|
|
539
|
-
...baseArgs,
|
|
540
|
-
tools,
|
|
541
|
-
tool_choice,
|
|
542
|
-
functions,
|
|
543
|
-
function_call
|
|
544
|
-
},
|
|
545
|
-
warnings: [...warnings, ...toolWarnings]
|
|
546
|
-
};
|
|
547
|
-
}
|
|
548
|
-
case "object-json": {
|
|
549
|
-
return {
|
|
550
|
-
args: {
|
|
551
|
-
...baseArgs,
|
|
552
|
-
response_format: this.supportsStructuredOutputs && mode.schema != null ? {
|
|
553
|
-
type: "json_schema",
|
|
554
|
-
json_schema: {
|
|
555
|
-
schema: mode.schema,
|
|
556
|
-
strict: true,
|
|
557
|
-
name: (_h = mode.name) != null ? _h : "response",
|
|
558
|
-
description: mode.description
|
|
559
|
-
}
|
|
560
|
-
} : { type: "json_object" }
|
|
561
|
-
},
|
|
562
|
-
warnings
|
|
563
|
-
};
|
|
564
|
-
}
|
|
565
|
-
case "object-tool": {
|
|
566
|
-
return {
|
|
567
|
-
args: useLegacyFunctionCalling ? {
|
|
568
|
-
...baseArgs,
|
|
569
|
-
function_call: {
|
|
570
|
-
name: mode.tool.name
|
|
571
|
-
},
|
|
572
|
-
functions: [
|
|
573
|
-
{
|
|
574
|
-
name: mode.tool.name,
|
|
575
|
-
description: mode.tool.description,
|
|
576
|
-
parameters: mode.tool.parameters
|
|
577
|
-
}
|
|
578
|
-
]
|
|
579
|
-
} : {
|
|
580
|
-
...baseArgs,
|
|
581
|
-
tool_choice: {
|
|
582
|
-
type: "function",
|
|
583
|
-
function: { name: mode.tool.name }
|
|
584
|
-
},
|
|
585
|
-
tools: [
|
|
586
|
-
{
|
|
587
|
-
type: "function",
|
|
588
|
-
function: {
|
|
589
|
-
name: mode.tool.name,
|
|
590
|
-
description: mode.tool.description,
|
|
591
|
-
parameters: mode.tool.parameters,
|
|
592
|
-
strict: this.supportsStructuredOutputs ? true : void 0
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
]
|
|
596
|
-
},
|
|
597
|
-
warnings
|
|
598
|
-
};
|
|
599
|
-
}
|
|
600
|
-
default: {
|
|
601
|
-
const _exhaustiveCheck = type;
|
|
602
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
603
487
|
}
|
|
604
488
|
}
|
|
489
|
+
const {
|
|
490
|
+
tools: openaiTools2,
|
|
491
|
+
toolChoice: openaiToolChoice,
|
|
492
|
+
toolWarnings
|
|
493
|
+
} = prepareTools({
|
|
494
|
+
tools,
|
|
495
|
+
toolChoice,
|
|
496
|
+
structuredOutputs: (_c = this.settings.structuredOutputs) != null ? _c : false
|
|
497
|
+
});
|
|
498
|
+
return {
|
|
499
|
+
args: {
|
|
500
|
+
...baseArgs,
|
|
501
|
+
tools: openaiTools2,
|
|
502
|
+
tool_choice: openaiToolChoice
|
|
503
|
+
},
|
|
504
|
+
warnings: [...warnings, ...toolWarnings]
|
|
505
|
+
};
|
|
605
506
|
}
|
|
606
507
|
async doGenerate(options) {
|
|
607
508
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
@@ -624,10 +525,23 @@ var OpenAIChatLanguageModel = class {
|
|
|
624
525
|
abortSignal: options.abortSignal,
|
|
625
526
|
fetch: this.config.fetch
|
|
626
527
|
});
|
|
627
|
-
const { messages: rawPrompt, ...rawSettings } = body;
|
|
628
528
|
const choice = response.choices[0];
|
|
629
|
-
const
|
|
630
|
-
const
|
|
529
|
+
const content = [];
|
|
530
|
+
const text = choice.message.content;
|
|
531
|
+
if (text != null && text.length > 0) {
|
|
532
|
+
content.push({ type: "text", text });
|
|
533
|
+
}
|
|
534
|
+
for (const toolCall of (_a = choice.message.tool_calls) != null ? _a : []) {
|
|
535
|
+
content.push({
|
|
536
|
+
type: "tool-call",
|
|
537
|
+
toolCallType: "function",
|
|
538
|
+
toolCallId: (_b = toolCall.id) != null ? _b : generateId(),
|
|
539
|
+
toolName: toolCall.function.name,
|
|
540
|
+
args: toolCall.function.arguments
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
const completionTokenDetails = (_c = response.usage) == null ? void 0 : _c.completion_tokens_details;
|
|
544
|
+
const promptTokenDetails = (_d = response.usage) == null ? void 0 : _d.prompt_tokens_details;
|
|
631
545
|
const providerMetadata = { openai: {} };
|
|
632
546
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens) != null) {
|
|
633
547
|
providerMetadata.openai.reasoningTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens;
|
|
@@ -642,81 +556,23 @@ var OpenAIChatLanguageModel = class {
|
|
|
642
556
|
providerMetadata.openai.cachedPromptTokens = promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens;
|
|
643
557
|
}
|
|
644
558
|
return {
|
|
645
|
-
|
|
646
|
-
toolCalls: this.settings.useLegacyFunctionCalling && choice.message.function_call ? [
|
|
647
|
-
{
|
|
648
|
-
toolCallType: "function",
|
|
649
|
-
toolCallId: generateId(),
|
|
650
|
-
toolName: choice.message.function_call.name,
|
|
651
|
-
args: choice.message.function_call.arguments
|
|
652
|
-
}
|
|
653
|
-
] : (_d = choice.message.tool_calls) == null ? void 0 : _d.map((toolCall) => {
|
|
654
|
-
var _a2;
|
|
655
|
-
return {
|
|
656
|
-
toolCallType: "function",
|
|
657
|
-
toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
|
|
658
|
-
toolName: toolCall.function.name,
|
|
659
|
-
args: toolCall.function.arguments
|
|
660
|
-
};
|
|
661
|
-
}),
|
|
559
|
+
content,
|
|
662
560
|
finishReason: mapOpenAIFinishReason(choice.finish_reason),
|
|
663
561
|
usage: {
|
|
664
|
-
|
|
665
|
-
|
|
562
|
+
inputTokens: (_f = (_e = response.usage) == null ? void 0 : _e.prompt_tokens) != null ? _f : void 0,
|
|
563
|
+
outputTokens: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : void 0
|
|
564
|
+
},
|
|
565
|
+
request: { body },
|
|
566
|
+
response: {
|
|
567
|
+
...getResponseMetadata(response),
|
|
568
|
+
headers: responseHeaders,
|
|
569
|
+
body: rawResponse
|
|
666
570
|
},
|
|
667
|
-
rawCall: { rawPrompt, rawSettings },
|
|
668
|
-
rawResponse: { headers: responseHeaders, body: rawResponse },
|
|
669
|
-
request: { body: JSON.stringify(body) },
|
|
670
|
-
response: getResponseMetadata(response),
|
|
671
571
|
warnings,
|
|
672
|
-
logprobs: mapOpenAIChatLogProbsOutput(choice.logprobs),
|
|
673
572
|
providerMetadata
|
|
674
573
|
};
|
|
675
574
|
}
|
|
676
575
|
async doStream(options) {
|
|
677
|
-
if (this.settings.simulateStreaming) {
|
|
678
|
-
const result = await this.doGenerate(options);
|
|
679
|
-
const simulatedStream = new ReadableStream({
|
|
680
|
-
start(controller) {
|
|
681
|
-
controller.enqueue({ type: "response-metadata", ...result.response });
|
|
682
|
-
if (result.text) {
|
|
683
|
-
controller.enqueue({
|
|
684
|
-
type: "text-delta",
|
|
685
|
-
textDelta: result.text
|
|
686
|
-
});
|
|
687
|
-
}
|
|
688
|
-
if (result.toolCalls) {
|
|
689
|
-
for (const toolCall of result.toolCalls) {
|
|
690
|
-
controller.enqueue({
|
|
691
|
-
type: "tool-call-delta",
|
|
692
|
-
toolCallType: "function",
|
|
693
|
-
toolCallId: toolCall.toolCallId,
|
|
694
|
-
toolName: toolCall.toolName,
|
|
695
|
-
argsTextDelta: toolCall.args
|
|
696
|
-
});
|
|
697
|
-
controller.enqueue({
|
|
698
|
-
type: "tool-call",
|
|
699
|
-
...toolCall
|
|
700
|
-
});
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
controller.enqueue({
|
|
704
|
-
type: "finish",
|
|
705
|
-
finishReason: result.finishReason,
|
|
706
|
-
usage: result.usage,
|
|
707
|
-
logprobs: result.logprobs,
|
|
708
|
-
providerMetadata: result.providerMetadata
|
|
709
|
-
});
|
|
710
|
-
controller.close();
|
|
711
|
-
}
|
|
712
|
-
});
|
|
713
|
-
return {
|
|
714
|
-
stream: simulatedStream,
|
|
715
|
-
rawCall: result.rawCall,
|
|
716
|
-
rawResponse: result.rawResponse,
|
|
717
|
-
warnings: result.warnings
|
|
718
|
-
};
|
|
719
|
-
}
|
|
720
576
|
const { args, warnings } = this.getArgs(options);
|
|
721
577
|
const body = {
|
|
722
578
|
...args,
|
|
@@ -741,17 +597,18 @@ var OpenAIChatLanguageModel = class {
|
|
|
741
597
|
const { messages: rawPrompt, ...rawSettings } = args;
|
|
742
598
|
const toolCalls = [];
|
|
743
599
|
let finishReason = "unknown";
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
600
|
+
const usage = {
|
|
601
|
+
inputTokens: void 0,
|
|
602
|
+
outputTokens: void 0
|
|
747
603
|
};
|
|
748
|
-
let logprobs;
|
|
749
604
|
let isFirstChunk = true;
|
|
750
|
-
const { useLegacyFunctionCalling } = this.settings;
|
|
751
605
|
const providerMetadata = { openai: {} };
|
|
752
606
|
return {
|
|
753
607
|
stream: response.pipeThrough(
|
|
754
608
|
new TransformStream({
|
|
609
|
+
start(controller) {
|
|
610
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
611
|
+
},
|
|
755
612
|
transform(chunk, controller) {
|
|
756
613
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
757
614
|
if (!chunk.success) {
|
|
@@ -779,10 +636,8 @@ var OpenAIChatLanguageModel = class {
|
|
|
779
636
|
prompt_tokens_details,
|
|
780
637
|
completion_tokens_details
|
|
781
638
|
} = value.usage;
|
|
782
|
-
usage =
|
|
783
|
-
|
|
784
|
-
completionTokens: completion_tokens != null ? completion_tokens : void 0
|
|
785
|
-
};
|
|
639
|
+
usage.inputTokens = prompt_tokens != null ? prompt_tokens : void 0;
|
|
640
|
+
usage.outputTokens = completion_tokens != null ? completion_tokens : void 0;
|
|
786
641
|
if ((completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens) != null) {
|
|
787
642
|
providerMetadata.openai.reasoningTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens;
|
|
788
643
|
}
|
|
@@ -806,27 +661,12 @@ var OpenAIChatLanguageModel = class {
|
|
|
806
661
|
const delta = choice.delta;
|
|
807
662
|
if (delta.content != null) {
|
|
808
663
|
controller.enqueue({
|
|
809
|
-
type: "text
|
|
810
|
-
|
|
664
|
+
type: "text",
|
|
665
|
+
text: delta.content
|
|
811
666
|
});
|
|
812
667
|
}
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
);
|
|
816
|
-
if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
|
|
817
|
-
if (logprobs === void 0) logprobs = [];
|
|
818
|
-
logprobs.push(...mappedLogprobs);
|
|
819
|
-
}
|
|
820
|
-
const mappedToolCalls = useLegacyFunctionCalling && delta.function_call != null ? [
|
|
821
|
-
{
|
|
822
|
-
type: "function",
|
|
823
|
-
id: generateId(),
|
|
824
|
-
function: delta.function_call,
|
|
825
|
-
index: 0
|
|
826
|
-
}
|
|
827
|
-
] : delta.tool_calls;
|
|
828
|
-
if (mappedToolCalls != null) {
|
|
829
|
-
for (const toolCallDelta of mappedToolCalls) {
|
|
668
|
+
if (delta.tool_calls != null) {
|
|
669
|
+
for (const toolCallDelta of delta.tool_calls) {
|
|
830
670
|
const index = toolCallDelta.index;
|
|
831
671
|
if (toolCalls[index] == null) {
|
|
832
672
|
if (toolCallDelta.type !== "function") {
|
|
@@ -908,125 +748,82 @@ var OpenAIChatLanguageModel = class {
|
|
|
908
748
|
}
|
|
909
749
|
},
|
|
910
750
|
flush(controller) {
|
|
911
|
-
var _a, _b;
|
|
912
751
|
controller.enqueue({
|
|
913
752
|
type: "finish",
|
|
914
753
|
finishReason,
|
|
915
|
-
|
|
916
|
-
usage: {
|
|
917
|
-
promptTokens: (_a = usage.promptTokens) != null ? _a : NaN,
|
|
918
|
-
completionTokens: (_b = usage.completionTokens) != null ? _b : NaN
|
|
919
|
-
},
|
|
754
|
+
usage,
|
|
920
755
|
...providerMetadata != null ? { providerMetadata } : {}
|
|
921
756
|
});
|
|
922
757
|
}
|
|
923
758
|
})
|
|
924
759
|
),
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
request: { body: JSON.stringify(body) },
|
|
928
|
-
warnings
|
|
760
|
+
request: { body },
|
|
761
|
+
response: { headers: responseHeaders }
|
|
929
762
|
};
|
|
930
763
|
}
|
|
931
764
|
};
|
|
932
|
-
var openaiTokenUsageSchema =
|
|
933
|
-
prompt_tokens:
|
|
934
|
-
completion_tokens:
|
|
935
|
-
prompt_tokens_details:
|
|
936
|
-
cached_tokens:
|
|
765
|
+
var openaiTokenUsageSchema = z3.object({
|
|
766
|
+
prompt_tokens: z3.number().nullish(),
|
|
767
|
+
completion_tokens: z3.number().nullish(),
|
|
768
|
+
prompt_tokens_details: z3.object({
|
|
769
|
+
cached_tokens: z3.number().nullish()
|
|
937
770
|
}).nullish(),
|
|
938
|
-
completion_tokens_details:
|
|
939
|
-
reasoning_tokens:
|
|
940
|
-
accepted_prediction_tokens:
|
|
941
|
-
rejected_prediction_tokens:
|
|
771
|
+
completion_tokens_details: z3.object({
|
|
772
|
+
reasoning_tokens: z3.number().nullish(),
|
|
773
|
+
accepted_prediction_tokens: z3.number().nullish(),
|
|
774
|
+
rejected_prediction_tokens: z3.number().nullish()
|
|
942
775
|
}).nullish()
|
|
943
776
|
}).nullish();
|
|
944
|
-
var openaiChatResponseSchema =
|
|
945
|
-
id:
|
|
946
|
-
created:
|
|
947
|
-
model:
|
|
948
|
-
choices:
|
|
949
|
-
|
|
950
|
-
message:
|
|
951
|
-
role:
|
|
952
|
-
content:
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
type: z2.literal("function"),
|
|
961
|
-
function: z2.object({
|
|
962
|
-
name: z2.string(),
|
|
963
|
-
arguments: z2.string()
|
|
777
|
+
var openaiChatResponseSchema = z3.object({
|
|
778
|
+
id: z3.string().nullish(),
|
|
779
|
+
created: z3.number().nullish(),
|
|
780
|
+
model: z3.string().nullish(),
|
|
781
|
+
choices: z3.array(
|
|
782
|
+
z3.object({
|
|
783
|
+
message: z3.object({
|
|
784
|
+
role: z3.literal("assistant").nullish(),
|
|
785
|
+
content: z3.string().nullish(),
|
|
786
|
+
tool_calls: z3.array(
|
|
787
|
+
z3.object({
|
|
788
|
+
id: z3.string().nullish(),
|
|
789
|
+
type: z3.literal("function"),
|
|
790
|
+
function: z3.object({
|
|
791
|
+
name: z3.string(),
|
|
792
|
+
arguments: z3.string()
|
|
964
793
|
})
|
|
965
794
|
})
|
|
966
795
|
).nullish()
|
|
967
796
|
}),
|
|
968
|
-
index:
|
|
969
|
-
|
|
970
|
-
content: z2.array(
|
|
971
|
-
z2.object({
|
|
972
|
-
token: z2.string(),
|
|
973
|
-
logprob: z2.number(),
|
|
974
|
-
top_logprobs: z2.array(
|
|
975
|
-
z2.object({
|
|
976
|
-
token: z2.string(),
|
|
977
|
-
logprob: z2.number()
|
|
978
|
-
})
|
|
979
|
-
)
|
|
980
|
-
})
|
|
981
|
-
).nullable()
|
|
982
|
-
}).nullish(),
|
|
983
|
-
finish_reason: z2.string().nullish()
|
|
797
|
+
index: z3.number(),
|
|
798
|
+
finish_reason: z3.string().nullish()
|
|
984
799
|
})
|
|
985
800
|
),
|
|
986
801
|
usage: openaiTokenUsageSchema
|
|
987
802
|
});
|
|
988
|
-
var openaiChatChunkSchema =
|
|
989
|
-
|
|
990
|
-
id:
|
|
991
|
-
created:
|
|
992
|
-
model:
|
|
993
|
-
choices:
|
|
994
|
-
|
|
995
|
-
delta:
|
|
996
|
-
role:
|
|
997
|
-
content:
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
type: z2.literal("function").optional(),
|
|
1007
|
-
function: z2.object({
|
|
1008
|
-
name: z2.string().nullish(),
|
|
1009
|
-
arguments: z2.string().nullish()
|
|
803
|
+
var openaiChatChunkSchema = z3.union([
|
|
804
|
+
z3.object({
|
|
805
|
+
id: z3.string().nullish(),
|
|
806
|
+
created: z3.number().nullish(),
|
|
807
|
+
model: z3.string().nullish(),
|
|
808
|
+
choices: z3.array(
|
|
809
|
+
z3.object({
|
|
810
|
+
delta: z3.object({
|
|
811
|
+
role: z3.enum(["assistant"]).nullish(),
|
|
812
|
+
content: z3.string().nullish(),
|
|
813
|
+
tool_calls: z3.array(
|
|
814
|
+
z3.object({
|
|
815
|
+
index: z3.number(),
|
|
816
|
+
id: z3.string().nullish(),
|
|
817
|
+
type: z3.literal("function").optional(),
|
|
818
|
+
function: z3.object({
|
|
819
|
+
name: z3.string().nullish(),
|
|
820
|
+
arguments: z3.string().nullish()
|
|
1010
821
|
})
|
|
1011
822
|
})
|
|
1012
823
|
).nullish()
|
|
1013
824
|
}).nullish(),
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
z2.object({
|
|
1017
|
-
token: z2.string(),
|
|
1018
|
-
logprob: z2.number(),
|
|
1019
|
-
top_logprobs: z2.array(
|
|
1020
|
-
z2.object({
|
|
1021
|
-
token: z2.string(),
|
|
1022
|
-
logprob: z2.number()
|
|
1023
|
-
})
|
|
1024
|
-
)
|
|
1025
|
-
})
|
|
1026
|
-
).nullable()
|
|
1027
|
-
}).nullish(),
|
|
1028
|
-
finish_reason: z2.string().nullable().optional(),
|
|
1029
|
-
index: z2.number()
|
|
825
|
+
finish_reason: z3.string().nullable().optional(),
|
|
826
|
+
index: z3.number()
|
|
1030
827
|
})
|
|
1031
828
|
),
|
|
1032
829
|
usage: openaiTokenUsageSchema
|
|
@@ -1034,10 +831,7 @@ var openaiChatChunkSchema = z2.union([
|
|
|
1034
831
|
openaiErrorDataSchema
|
|
1035
832
|
]);
|
|
1036
833
|
function isReasoningModel(modelId) {
|
|
1037
|
-
return modelId
|
|
1038
|
-
}
|
|
1039
|
-
function isAudioModel(modelId) {
|
|
1040
|
-
return modelId.startsWith("gpt-4o-audio-preview");
|
|
834
|
+
return modelId.startsWith("o");
|
|
1041
835
|
}
|
|
1042
836
|
function getSystemMessageMode(modelId) {
|
|
1043
837
|
var _a, _b;
|
|
@@ -1068,21 +862,18 @@ var reasoningModels = {
|
|
|
1068
862
|
};
|
|
1069
863
|
|
|
1070
864
|
// src/openai-completion-language-model.ts
|
|
1071
|
-
import {
|
|
1072
|
-
UnsupportedFunctionalityError as UnsupportedFunctionalityError5
|
|
1073
|
-
} from "@ai-sdk/provider";
|
|
1074
865
|
import {
|
|
1075
866
|
combineHeaders as combineHeaders2,
|
|
1076
867
|
createEventSourceResponseHandler as createEventSourceResponseHandler2,
|
|
1077
868
|
createJsonResponseHandler as createJsonResponseHandler2,
|
|
1078
869
|
postJsonToApi as postJsonToApi2
|
|
1079
870
|
} from "@ai-sdk/provider-utils";
|
|
1080
|
-
import { z as
|
|
871
|
+
import { z as z4 } from "zod";
|
|
1081
872
|
|
|
1082
873
|
// src/convert-to-openai-completion-prompt.ts
|
|
1083
874
|
import {
|
|
1084
875
|
InvalidPromptError,
|
|
1085
|
-
UnsupportedFunctionalityError as
|
|
876
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
1086
877
|
} from "@ai-sdk/provider";
|
|
1087
878
|
function convertToOpenAICompletionPrompt({
|
|
1088
879
|
prompt,
|
|
@@ -1114,13 +905,8 @@ function convertToOpenAICompletionPrompt({
|
|
|
1114
905
|
case "text": {
|
|
1115
906
|
return part.text;
|
|
1116
907
|
}
|
|
1117
|
-
case "image": {
|
|
1118
|
-
throw new UnsupportedFunctionalityError4({
|
|
1119
|
-
functionality: "images"
|
|
1120
|
-
});
|
|
1121
|
-
}
|
|
1122
908
|
}
|
|
1123
|
-
}).join("");
|
|
909
|
+
}).filter(Boolean).join("");
|
|
1124
910
|
text += `${user}:
|
|
1125
911
|
${userMessage}
|
|
1126
912
|
|
|
@@ -1134,7 +920,7 @@ ${userMessage}
|
|
|
1134
920
|
return part.text;
|
|
1135
921
|
}
|
|
1136
922
|
case "tool-call": {
|
|
1137
|
-
throw new
|
|
923
|
+
throw new UnsupportedFunctionalityError3({
|
|
1138
924
|
functionality: "tool-call messages"
|
|
1139
925
|
});
|
|
1140
926
|
}
|
|
@@ -1147,7 +933,7 @@ ${assistantMessage}
|
|
|
1147
933
|
break;
|
|
1148
934
|
}
|
|
1149
935
|
case "tool": {
|
|
1150
|
-
throw new
|
|
936
|
+
throw new UnsupportedFunctionalityError3({
|
|
1151
937
|
functionality: "tool messages"
|
|
1152
938
|
});
|
|
1153
939
|
}
|
|
@@ -1166,25 +952,10 @@ ${user}:`]
|
|
|
1166
952
|
};
|
|
1167
953
|
}
|
|
1168
954
|
|
|
1169
|
-
// src/map-openai-completion-logprobs.ts
|
|
1170
|
-
function mapOpenAICompletionLogProbs(logprobs) {
|
|
1171
|
-
return logprobs == null ? void 0 : logprobs.tokens.map((token, index) => ({
|
|
1172
|
-
token,
|
|
1173
|
-
logprob: logprobs.token_logprobs[index],
|
|
1174
|
-
topLogprobs: logprobs.top_logprobs ? Object.entries(logprobs.top_logprobs[index]).map(
|
|
1175
|
-
([token2, logprob]) => ({
|
|
1176
|
-
token: token2,
|
|
1177
|
-
logprob
|
|
1178
|
-
})
|
|
1179
|
-
) : []
|
|
1180
|
-
}));
|
|
1181
|
-
}
|
|
1182
|
-
|
|
1183
955
|
// src/openai-completion-language-model.ts
|
|
1184
956
|
var OpenAICompletionLanguageModel = class {
|
|
1185
957
|
constructor(modelId, settings, config) {
|
|
1186
958
|
this.specificationVersion = "v2";
|
|
1187
|
-
this.defaultObjectGenerationMode = void 0;
|
|
1188
959
|
this.modelId = modelId;
|
|
1189
960
|
this.settings = settings;
|
|
1190
961
|
this.config = config;
|
|
@@ -1192,11 +963,15 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1192
963
|
get provider() {
|
|
1193
964
|
return this.config.provider;
|
|
1194
965
|
}
|
|
966
|
+
async getSupportedUrls() {
|
|
967
|
+
return {
|
|
968
|
+
// no supported urls for completion models
|
|
969
|
+
};
|
|
970
|
+
}
|
|
1195
971
|
getArgs({
|
|
1196
|
-
mode,
|
|
1197
972
|
inputFormat,
|
|
1198
973
|
prompt,
|
|
1199
|
-
|
|
974
|
+
maxOutputTokens,
|
|
1200
975
|
temperature,
|
|
1201
976
|
topP,
|
|
1202
977
|
topK,
|
|
@@ -1204,16 +979,19 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1204
979
|
presencePenalty,
|
|
1205
980
|
stopSequences: userStopSequences,
|
|
1206
981
|
responseFormat,
|
|
982
|
+
tools,
|
|
983
|
+
toolChoice,
|
|
1207
984
|
seed
|
|
1208
985
|
}) {
|
|
1209
|
-
var _a;
|
|
1210
|
-
const type = mode.type;
|
|
1211
986
|
const warnings = [];
|
|
1212
987
|
if (topK != null) {
|
|
1213
|
-
warnings.push({
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
});
|
|
988
|
+
warnings.push({ type: "unsupported-setting", setting: "topK" });
|
|
989
|
+
}
|
|
990
|
+
if (tools == null ? void 0 : tools.length) {
|
|
991
|
+
warnings.push({ type: "unsupported-setting", setting: "tools" });
|
|
992
|
+
}
|
|
993
|
+
if (toolChoice != null) {
|
|
994
|
+
warnings.push({ type: "unsupported-setting", setting: "toolChoice" });
|
|
1217
995
|
}
|
|
1218
996
|
if (responseFormat != null && responseFormat.type !== "text") {
|
|
1219
997
|
warnings.push({
|
|
@@ -1224,56 +1002,29 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1224
1002
|
}
|
|
1225
1003
|
const { prompt: completionPrompt, stopSequences } = convertToOpenAICompletionPrompt({ prompt, inputFormat });
|
|
1226
1004
|
const stop = [...stopSequences != null ? stopSequences : [], ...userStopSequences != null ? userStopSequences : []];
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1005
|
+
return {
|
|
1006
|
+
args: {
|
|
1007
|
+
// model id:
|
|
1008
|
+
model: this.modelId,
|
|
1009
|
+
// model specific settings:
|
|
1010
|
+
echo: this.settings.echo,
|
|
1011
|
+
logit_bias: this.settings.logitBias,
|
|
1012
|
+
suffix: this.settings.suffix,
|
|
1013
|
+
user: this.settings.user,
|
|
1014
|
+
// standardized settings:
|
|
1015
|
+
max_tokens: maxOutputTokens,
|
|
1016
|
+
temperature,
|
|
1017
|
+
top_p: topP,
|
|
1018
|
+
frequency_penalty: frequencyPenalty,
|
|
1019
|
+
presence_penalty: presencePenalty,
|
|
1020
|
+
seed,
|
|
1021
|
+
// prompt:
|
|
1022
|
+
prompt: completionPrompt,
|
|
1023
|
+
// stop sequences:
|
|
1024
|
+
stop: stop.length > 0 ? stop : void 0
|
|
1025
|
+
},
|
|
1026
|
+
warnings
|
|
1247
1027
|
};
|
|
1248
|
-
switch (type) {
|
|
1249
|
-
case "regular": {
|
|
1250
|
-
if ((_a = mode.tools) == null ? void 0 : _a.length) {
|
|
1251
|
-
throw new UnsupportedFunctionalityError5({
|
|
1252
|
-
functionality: "tools"
|
|
1253
|
-
});
|
|
1254
|
-
}
|
|
1255
|
-
if (mode.toolChoice) {
|
|
1256
|
-
throw new UnsupportedFunctionalityError5({
|
|
1257
|
-
functionality: "toolChoice"
|
|
1258
|
-
});
|
|
1259
|
-
}
|
|
1260
|
-
return { args: baseArgs, warnings };
|
|
1261
|
-
}
|
|
1262
|
-
case "object-json": {
|
|
1263
|
-
throw new UnsupportedFunctionalityError5({
|
|
1264
|
-
functionality: "object-json mode"
|
|
1265
|
-
});
|
|
1266
|
-
}
|
|
1267
|
-
case "object-tool": {
|
|
1268
|
-
throw new UnsupportedFunctionalityError5({
|
|
1269
|
-
functionality: "object-tool mode"
|
|
1270
|
-
});
|
|
1271
|
-
}
|
|
1272
|
-
default: {
|
|
1273
|
-
const _exhaustiveCheck = type;
|
|
1274
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
1275
|
-
}
|
|
1276
|
-
}
|
|
1277
1028
|
}
|
|
1278
1029
|
async doGenerate(options) {
|
|
1279
1030
|
const { args, warnings } = this.getArgs(options);
|
|
@@ -1295,21 +1046,21 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1295
1046
|
abortSignal: options.abortSignal,
|
|
1296
1047
|
fetch: this.config.fetch
|
|
1297
1048
|
});
|
|
1298
|
-
const { prompt: rawPrompt, ...rawSettings } = args;
|
|
1299
1049
|
const choice = response.choices[0];
|
|
1300
1050
|
return {
|
|
1301
|
-
text: choice.text,
|
|
1051
|
+
content: [{ type: "text", text: choice.text }],
|
|
1302
1052
|
usage: {
|
|
1303
|
-
|
|
1304
|
-
|
|
1053
|
+
inputTokens: response.usage.prompt_tokens,
|
|
1054
|
+
outputTokens: response.usage.completion_tokens
|
|
1305
1055
|
},
|
|
1306
1056
|
finishReason: mapOpenAIFinishReason(choice.finish_reason),
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1057
|
+
request: { body: args },
|
|
1058
|
+
response: {
|
|
1059
|
+
...getResponseMetadata(response),
|
|
1060
|
+
headers: responseHeaders,
|
|
1061
|
+
body: rawResponse
|
|
1062
|
+
},
|
|
1063
|
+
warnings
|
|
1313
1064
|
};
|
|
1314
1065
|
}
|
|
1315
1066
|
async doStream(options) {
|
|
@@ -1334,17 +1085,18 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1334
1085
|
abortSignal: options.abortSignal,
|
|
1335
1086
|
fetch: this.config.fetch
|
|
1336
1087
|
});
|
|
1337
|
-
const { prompt: rawPrompt, ...rawSettings } = args;
|
|
1338
1088
|
let finishReason = "unknown";
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1089
|
+
const usage = {
|
|
1090
|
+
inputTokens: void 0,
|
|
1091
|
+
outputTokens: void 0
|
|
1342
1092
|
};
|
|
1343
|
-
let logprobs;
|
|
1344
1093
|
let isFirstChunk = true;
|
|
1345
1094
|
return {
|
|
1346
1095
|
stream: response.pipeThrough(
|
|
1347
1096
|
new TransformStream({
|
|
1097
|
+
start(controller) {
|
|
1098
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
1099
|
+
},
|
|
1348
1100
|
transform(chunk, controller) {
|
|
1349
1101
|
if (!chunk.success) {
|
|
1350
1102
|
finishReason = "error";
|
|
@@ -1365,10 +1117,8 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1365
1117
|
});
|
|
1366
1118
|
}
|
|
1367
1119
|
if (value.usage != null) {
|
|
1368
|
-
usage =
|
|
1369
|
-
|
|
1370
|
-
completionTokens: value.usage.completion_tokens
|
|
1371
|
-
};
|
|
1120
|
+
usage.inputTokens = value.usage.prompt_tokens;
|
|
1121
|
+
usage.outputTokens = value.usage.completion_tokens;
|
|
1372
1122
|
}
|
|
1373
1123
|
const choice = value.choices[0];
|
|
1374
1124
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
@@ -1376,75 +1126,55 @@ var OpenAICompletionLanguageModel = class {
|
|
|
1376
1126
|
}
|
|
1377
1127
|
if ((choice == null ? void 0 : choice.text) != null) {
|
|
1378
1128
|
controller.enqueue({
|
|
1379
|
-
type: "text
|
|
1380
|
-
|
|
1129
|
+
type: "text",
|
|
1130
|
+
text: choice.text
|
|
1381
1131
|
});
|
|
1382
1132
|
}
|
|
1383
|
-
const mappedLogprobs = mapOpenAICompletionLogProbs(
|
|
1384
|
-
choice == null ? void 0 : choice.logprobs
|
|
1385
|
-
);
|
|
1386
|
-
if (mappedLogprobs == null ? void 0 : mappedLogprobs.length) {
|
|
1387
|
-
if (logprobs === void 0) logprobs = [];
|
|
1388
|
-
logprobs.push(...mappedLogprobs);
|
|
1389
|
-
}
|
|
1390
1133
|
},
|
|
1391
1134
|
flush(controller) {
|
|
1392
1135
|
controller.enqueue({
|
|
1393
1136
|
type: "finish",
|
|
1394
1137
|
finishReason,
|
|
1395
|
-
logprobs,
|
|
1396
1138
|
usage
|
|
1397
1139
|
});
|
|
1398
1140
|
}
|
|
1399
1141
|
})
|
|
1400
1142
|
),
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
warnings,
|
|
1404
|
-
request: { body: JSON.stringify(body) }
|
|
1143
|
+
request: { body },
|
|
1144
|
+
response: { headers: responseHeaders }
|
|
1405
1145
|
};
|
|
1406
1146
|
}
|
|
1407
1147
|
};
|
|
1408
|
-
var openaiCompletionResponseSchema =
|
|
1409
|
-
id:
|
|
1410
|
-
created:
|
|
1411
|
-
model:
|
|
1412
|
-
choices:
|
|
1413
|
-
|
|
1414
|
-
text:
|
|
1415
|
-
finish_reason:
|
|
1416
|
-
logprobs: z3.object({
|
|
1417
|
-
tokens: z3.array(z3.string()),
|
|
1418
|
-
token_logprobs: z3.array(z3.number()),
|
|
1419
|
-
top_logprobs: z3.array(z3.record(z3.string(), z3.number())).nullable()
|
|
1420
|
-
}).nullish()
|
|
1148
|
+
var openaiCompletionResponseSchema = z4.object({
|
|
1149
|
+
id: z4.string().nullish(),
|
|
1150
|
+
created: z4.number().nullish(),
|
|
1151
|
+
model: z4.string().nullish(),
|
|
1152
|
+
choices: z4.array(
|
|
1153
|
+
z4.object({
|
|
1154
|
+
text: z4.string(),
|
|
1155
|
+
finish_reason: z4.string()
|
|
1421
1156
|
})
|
|
1422
1157
|
),
|
|
1423
|
-
usage:
|
|
1424
|
-
prompt_tokens:
|
|
1425
|
-
completion_tokens:
|
|
1158
|
+
usage: z4.object({
|
|
1159
|
+
prompt_tokens: z4.number(),
|
|
1160
|
+
completion_tokens: z4.number()
|
|
1426
1161
|
})
|
|
1427
1162
|
});
|
|
1428
|
-
var openaiCompletionChunkSchema =
|
|
1429
|
-
|
|
1430
|
-
id:
|
|
1431
|
-
created:
|
|
1432
|
-
model:
|
|
1433
|
-
choices:
|
|
1434
|
-
|
|
1435
|
-
text:
|
|
1436
|
-
finish_reason:
|
|
1437
|
-
index:
|
|
1438
|
-
logprobs: z3.object({
|
|
1439
|
-
tokens: z3.array(z3.string()),
|
|
1440
|
-
token_logprobs: z3.array(z3.number()),
|
|
1441
|
-
top_logprobs: z3.array(z3.record(z3.string(), z3.number())).nullable()
|
|
1442
|
-
}).nullish()
|
|
1163
|
+
var openaiCompletionChunkSchema = z4.union([
|
|
1164
|
+
z4.object({
|
|
1165
|
+
id: z4.string().nullish(),
|
|
1166
|
+
created: z4.number().nullish(),
|
|
1167
|
+
model: z4.string().nullish(),
|
|
1168
|
+
choices: z4.array(
|
|
1169
|
+
z4.object({
|
|
1170
|
+
text: z4.string(),
|
|
1171
|
+
finish_reason: z4.string().nullish(),
|
|
1172
|
+
index: z4.number()
|
|
1443
1173
|
})
|
|
1444
1174
|
),
|
|
1445
|
-
usage:
|
|
1446
|
-
prompt_tokens:
|
|
1447
|
-
completion_tokens:
|
|
1175
|
+
usage: z4.object({
|
|
1176
|
+
prompt_tokens: z4.number(),
|
|
1177
|
+
completion_tokens: z4.number()
|
|
1448
1178
|
}).nullish()
|
|
1449
1179
|
}),
|
|
1450
1180
|
openaiErrorDataSchema
|
|
@@ -1457,12 +1187,30 @@ import {
|
|
|
1457
1187
|
import {
|
|
1458
1188
|
combineHeaders as combineHeaders3,
|
|
1459
1189
|
createJsonResponseHandler as createJsonResponseHandler3,
|
|
1190
|
+
parseProviderOptions as parseProviderOptions2,
|
|
1460
1191
|
postJsonToApi as postJsonToApi3
|
|
1461
1192
|
} from "@ai-sdk/provider-utils";
|
|
1462
|
-
import { z as
|
|
1193
|
+
import { z as z6 } from "zod";
|
|
1194
|
+
|
|
1195
|
+
// src/openai-embedding-options.ts
|
|
1196
|
+
import { z as z5 } from "zod";
|
|
1197
|
+
var openaiEmbeddingProviderOptions = z5.object({
|
|
1198
|
+
/**
|
|
1199
|
+
The number of dimensions the resulting output embeddings should have.
|
|
1200
|
+
Only supported in text-embedding-3 and later models.
|
|
1201
|
+
*/
|
|
1202
|
+
dimensions: z5.number().optional(),
|
|
1203
|
+
/**
|
|
1204
|
+
A unique identifier representing your end-user, which can help OpenAI to
|
|
1205
|
+
monitor and detect abuse. Learn more.
|
|
1206
|
+
*/
|
|
1207
|
+
user: z5.string().optional()
|
|
1208
|
+
});
|
|
1209
|
+
|
|
1210
|
+
// src/openai-embedding-model.ts
|
|
1463
1211
|
var OpenAIEmbeddingModel = class {
|
|
1464
1212
|
constructor(modelId, settings, config) {
|
|
1465
|
-
this.specificationVersion = "
|
|
1213
|
+
this.specificationVersion = "v2";
|
|
1466
1214
|
this.modelId = modelId;
|
|
1467
1215
|
this.settings = settings;
|
|
1468
1216
|
this.config = config;
|
|
@@ -1481,8 +1229,10 @@ var OpenAIEmbeddingModel = class {
|
|
|
1481
1229
|
async doEmbed({
|
|
1482
1230
|
values,
|
|
1483
1231
|
headers,
|
|
1484
|
-
abortSignal
|
|
1232
|
+
abortSignal,
|
|
1233
|
+
providerOptions
|
|
1485
1234
|
}) {
|
|
1235
|
+
var _a;
|
|
1486
1236
|
if (values.length > this.maxEmbeddingsPerCall) {
|
|
1487
1237
|
throw new TooManyEmbeddingValuesForCallError({
|
|
1488
1238
|
provider: this.provider,
|
|
@@ -1491,7 +1241,16 @@ var OpenAIEmbeddingModel = class {
|
|
|
1491
1241
|
values
|
|
1492
1242
|
});
|
|
1493
1243
|
}
|
|
1494
|
-
const
|
|
1244
|
+
const openaiOptions = (_a = parseProviderOptions2({
|
|
1245
|
+
provider: "openai",
|
|
1246
|
+
providerOptions,
|
|
1247
|
+
schema: openaiEmbeddingProviderOptions
|
|
1248
|
+
})) != null ? _a : {};
|
|
1249
|
+
const {
|
|
1250
|
+
responseHeaders,
|
|
1251
|
+
value: response,
|
|
1252
|
+
rawValue
|
|
1253
|
+
} = await postJsonToApi3({
|
|
1495
1254
|
url: this.config.url({
|
|
1496
1255
|
path: "/embeddings",
|
|
1497
1256
|
modelId: this.modelId
|
|
@@ -1501,8 +1260,8 @@ var OpenAIEmbeddingModel = class {
|
|
|
1501
1260
|
model: this.modelId,
|
|
1502
1261
|
input: values,
|
|
1503
1262
|
encoding_format: "float",
|
|
1504
|
-
dimensions:
|
|
1505
|
-
user:
|
|
1263
|
+
dimensions: openaiOptions.dimensions,
|
|
1264
|
+
user: openaiOptions.user
|
|
1506
1265
|
},
|
|
1507
1266
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
1508
1267
|
successfulResponseHandler: createJsonResponseHandler3(
|
|
@@ -1514,13 +1273,13 @@ var OpenAIEmbeddingModel = class {
|
|
|
1514
1273
|
return {
|
|
1515
1274
|
embeddings: response.data.map((item) => item.embedding),
|
|
1516
1275
|
usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
|
|
1517
|
-
|
|
1276
|
+
response: { headers: responseHeaders, body: rawValue }
|
|
1518
1277
|
};
|
|
1519
1278
|
}
|
|
1520
1279
|
};
|
|
1521
|
-
var openaiTextEmbeddingResponseSchema =
|
|
1522
|
-
data:
|
|
1523
|
-
usage:
|
|
1280
|
+
var openaiTextEmbeddingResponseSchema = z6.object({
|
|
1281
|
+
data: z6.array(z6.object({ embedding: z6.array(z6.number()) })),
|
|
1282
|
+
usage: z6.object({ prompt_tokens: z6.number() }).nullish()
|
|
1524
1283
|
});
|
|
1525
1284
|
|
|
1526
1285
|
// src/openai-image-model.ts
|
|
@@ -1529,7 +1288,7 @@ import {
|
|
|
1529
1288
|
createJsonResponseHandler as createJsonResponseHandler4,
|
|
1530
1289
|
postJsonToApi as postJsonToApi4
|
|
1531
1290
|
} from "@ai-sdk/provider-utils";
|
|
1532
|
-
import { z as
|
|
1291
|
+
import { z as z7 } from "zod";
|
|
1533
1292
|
|
|
1534
1293
|
// src/openai-image-settings.ts
|
|
1535
1294
|
var modelMaxImagesPerCall = {
|
|
@@ -1607,13 +1366,13 @@ var OpenAIImageModel = class {
|
|
|
1607
1366
|
};
|
|
1608
1367
|
}
|
|
1609
1368
|
};
|
|
1610
|
-
var openaiImageResponseSchema =
|
|
1611
|
-
data:
|
|
1369
|
+
var openaiImageResponseSchema = z7.object({
|
|
1370
|
+
data: z7.array(z7.object({ b64_json: z7.string() }))
|
|
1612
1371
|
});
|
|
1613
1372
|
|
|
1614
1373
|
// src/openai-tools.ts
|
|
1615
|
-
import { z as
|
|
1616
|
-
var WebSearchPreviewParameters =
|
|
1374
|
+
import { z as z8 } from "zod";
|
|
1375
|
+
var WebSearchPreviewParameters = z8.object({});
|
|
1617
1376
|
function webSearchPreviewTool({
|
|
1618
1377
|
searchContextSize,
|
|
1619
1378
|
userLocation
|
|
@@ -1632,22 +1391,196 @@ var openaiTools = {
|
|
|
1632
1391
|
webSearchPreview: webSearchPreviewTool
|
|
1633
1392
|
};
|
|
1634
1393
|
|
|
1635
|
-
// src/
|
|
1394
|
+
// src/openai-transcription-model.ts
|
|
1636
1395
|
import {
|
|
1637
1396
|
combineHeaders as combineHeaders5,
|
|
1638
|
-
|
|
1397
|
+
convertBase64ToUint8Array,
|
|
1639
1398
|
createJsonResponseHandler as createJsonResponseHandler5,
|
|
1399
|
+
parseProviderOptions as parseProviderOptions3,
|
|
1400
|
+
postFormDataToApi
|
|
1401
|
+
} from "@ai-sdk/provider-utils";
|
|
1402
|
+
import { z as z9 } from "zod";
|
|
1403
|
+
var openAIProviderOptionsSchema = z9.object({
|
|
1404
|
+
include: z9.array(z9.string()).nullish(),
|
|
1405
|
+
language: z9.string().nullish(),
|
|
1406
|
+
prompt: z9.string().nullish(),
|
|
1407
|
+
temperature: z9.number().min(0).max(1).nullish().default(0),
|
|
1408
|
+
timestampGranularities: z9.array(z9.enum(["word", "segment"])).nullish().default(["segment"])
|
|
1409
|
+
});
|
|
1410
|
+
var languageMap = {
|
|
1411
|
+
afrikaans: "af",
|
|
1412
|
+
arabic: "ar",
|
|
1413
|
+
armenian: "hy",
|
|
1414
|
+
azerbaijani: "az",
|
|
1415
|
+
belarusian: "be",
|
|
1416
|
+
bosnian: "bs",
|
|
1417
|
+
bulgarian: "bg",
|
|
1418
|
+
catalan: "ca",
|
|
1419
|
+
chinese: "zh",
|
|
1420
|
+
croatian: "hr",
|
|
1421
|
+
czech: "cs",
|
|
1422
|
+
danish: "da",
|
|
1423
|
+
dutch: "nl",
|
|
1424
|
+
english: "en",
|
|
1425
|
+
estonian: "et",
|
|
1426
|
+
finnish: "fi",
|
|
1427
|
+
french: "fr",
|
|
1428
|
+
galician: "gl",
|
|
1429
|
+
german: "de",
|
|
1430
|
+
greek: "el",
|
|
1431
|
+
hebrew: "he",
|
|
1432
|
+
hindi: "hi",
|
|
1433
|
+
hungarian: "hu",
|
|
1434
|
+
icelandic: "is",
|
|
1435
|
+
indonesian: "id",
|
|
1436
|
+
italian: "it",
|
|
1437
|
+
japanese: "ja",
|
|
1438
|
+
kannada: "kn",
|
|
1439
|
+
kazakh: "kk",
|
|
1440
|
+
korean: "ko",
|
|
1441
|
+
latvian: "lv",
|
|
1442
|
+
lithuanian: "lt",
|
|
1443
|
+
macedonian: "mk",
|
|
1444
|
+
malay: "ms",
|
|
1445
|
+
marathi: "mr",
|
|
1446
|
+
maori: "mi",
|
|
1447
|
+
nepali: "ne",
|
|
1448
|
+
norwegian: "no",
|
|
1449
|
+
persian: "fa",
|
|
1450
|
+
polish: "pl",
|
|
1451
|
+
portuguese: "pt",
|
|
1452
|
+
romanian: "ro",
|
|
1453
|
+
russian: "ru",
|
|
1454
|
+
serbian: "sr",
|
|
1455
|
+
slovak: "sk",
|
|
1456
|
+
slovenian: "sl",
|
|
1457
|
+
spanish: "es",
|
|
1458
|
+
swahili: "sw",
|
|
1459
|
+
swedish: "sv",
|
|
1460
|
+
tagalog: "tl",
|
|
1461
|
+
tamil: "ta",
|
|
1462
|
+
thai: "th",
|
|
1463
|
+
turkish: "tr",
|
|
1464
|
+
ukrainian: "uk",
|
|
1465
|
+
urdu: "ur",
|
|
1466
|
+
vietnamese: "vi",
|
|
1467
|
+
welsh: "cy"
|
|
1468
|
+
};
|
|
1469
|
+
var OpenAITranscriptionModel = class {
|
|
1470
|
+
constructor(modelId, config) {
|
|
1471
|
+
this.modelId = modelId;
|
|
1472
|
+
this.config = config;
|
|
1473
|
+
this.specificationVersion = "v1";
|
|
1474
|
+
}
|
|
1475
|
+
get provider() {
|
|
1476
|
+
return this.config.provider;
|
|
1477
|
+
}
|
|
1478
|
+
getArgs({
|
|
1479
|
+
audio,
|
|
1480
|
+
mediaType,
|
|
1481
|
+
providerOptions
|
|
1482
|
+
}) {
|
|
1483
|
+
var _a, _b, _c, _d, _e;
|
|
1484
|
+
const warnings = [];
|
|
1485
|
+
const openAIOptions = parseProviderOptions3({
|
|
1486
|
+
provider: "openai",
|
|
1487
|
+
providerOptions,
|
|
1488
|
+
schema: openAIProviderOptionsSchema
|
|
1489
|
+
});
|
|
1490
|
+
const formData = new FormData();
|
|
1491
|
+
const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([convertBase64ToUint8Array(audio)]);
|
|
1492
|
+
formData.append("model", this.modelId);
|
|
1493
|
+
formData.append("file", new File([blob], "audio", { type: mediaType }));
|
|
1494
|
+
if (openAIOptions) {
|
|
1495
|
+
const transcriptionModelOptions = {
|
|
1496
|
+
include: (_a = openAIOptions.include) != null ? _a : void 0,
|
|
1497
|
+
language: (_b = openAIOptions.language) != null ? _b : void 0,
|
|
1498
|
+
prompt: (_c = openAIOptions.prompt) != null ? _c : void 0,
|
|
1499
|
+
temperature: (_d = openAIOptions.temperature) != null ? _d : void 0,
|
|
1500
|
+
timestamp_granularities: (_e = openAIOptions.timestampGranularities) != null ? _e : void 0
|
|
1501
|
+
};
|
|
1502
|
+
for (const key in transcriptionModelOptions) {
|
|
1503
|
+
const value = transcriptionModelOptions[key];
|
|
1504
|
+
if (value !== void 0) {
|
|
1505
|
+
formData.append(key, String(value));
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
return {
|
|
1510
|
+
formData,
|
|
1511
|
+
warnings
|
|
1512
|
+
};
|
|
1513
|
+
}
|
|
1514
|
+
async doGenerate(options) {
|
|
1515
|
+
var _a, _b, _c, _d, _e, _f;
|
|
1516
|
+
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
1517
|
+
const { formData, warnings } = this.getArgs(options);
|
|
1518
|
+
const {
|
|
1519
|
+
value: response,
|
|
1520
|
+
responseHeaders,
|
|
1521
|
+
rawValue: rawResponse
|
|
1522
|
+
} = await postFormDataToApi({
|
|
1523
|
+
url: this.config.url({
|
|
1524
|
+
path: "/audio/transcriptions",
|
|
1525
|
+
modelId: this.modelId
|
|
1526
|
+
}),
|
|
1527
|
+
headers: combineHeaders5(this.config.headers(), options.headers),
|
|
1528
|
+
formData,
|
|
1529
|
+
failedResponseHandler: openaiFailedResponseHandler,
|
|
1530
|
+
successfulResponseHandler: createJsonResponseHandler5(
|
|
1531
|
+
openaiTranscriptionResponseSchema
|
|
1532
|
+
),
|
|
1533
|
+
abortSignal: options.abortSignal,
|
|
1534
|
+
fetch: this.config.fetch
|
|
1535
|
+
});
|
|
1536
|
+
const language = response.language != null && response.language in languageMap ? languageMap[response.language] : void 0;
|
|
1537
|
+
return {
|
|
1538
|
+
text: response.text,
|
|
1539
|
+
segments: (_e = (_d = response.words) == null ? void 0 : _d.map((word) => ({
|
|
1540
|
+
text: word.word,
|
|
1541
|
+
startSecond: word.start,
|
|
1542
|
+
endSecond: word.end
|
|
1543
|
+
}))) != null ? _e : [],
|
|
1544
|
+
language,
|
|
1545
|
+
durationInSeconds: (_f = response.duration) != null ? _f : void 0,
|
|
1546
|
+
warnings,
|
|
1547
|
+
response: {
|
|
1548
|
+
timestamp: currentDate,
|
|
1549
|
+
modelId: this.modelId,
|
|
1550
|
+
headers: responseHeaders,
|
|
1551
|
+
body: rawResponse
|
|
1552
|
+
}
|
|
1553
|
+
};
|
|
1554
|
+
}
|
|
1555
|
+
};
|
|
1556
|
+
var openaiTranscriptionResponseSchema = z9.object({
|
|
1557
|
+
text: z9.string(),
|
|
1558
|
+
language: z9.string().nullish(),
|
|
1559
|
+
duration: z9.number().nullish(),
|
|
1560
|
+
words: z9.array(
|
|
1561
|
+
z9.object({
|
|
1562
|
+
word: z9.string(),
|
|
1563
|
+
start: z9.number(),
|
|
1564
|
+
end: z9.number()
|
|
1565
|
+
})
|
|
1566
|
+
).nullish()
|
|
1567
|
+
});
|
|
1568
|
+
|
|
1569
|
+
// src/responses/openai-responses-language-model.ts
|
|
1570
|
+
import {
|
|
1571
|
+
combineHeaders as combineHeaders6,
|
|
1572
|
+
createEventSourceResponseHandler as createEventSourceResponseHandler3,
|
|
1573
|
+
createJsonResponseHandler as createJsonResponseHandler6,
|
|
1640
1574
|
generateId as generateId2,
|
|
1641
|
-
parseProviderOptions,
|
|
1575
|
+
parseProviderOptions as parseProviderOptions4,
|
|
1642
1576
|
postJsonToApi as postJsonToApi5
|
|
1643
1577
|
} from "@ai-sdk/provider-utils";
|
|
1644
|
-
import { z as
|
|
1578
|
+
import { z as z10 } from "zod";
|
|
1645
1579
|
|
|
1646
1580
|
// src/responses/convert-to-openai-responses-messages.ts
|
|
1647
1581
|
import {
|
|
1648
|
-
UnsupportedFunctionalityError as
|
|
1582
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError4
|
|
1649
1583
|
} from "@ai-sdk/provider";
|
|
1650
|
-
import { convertUint8ArrayToBase64 as convertUint8ArrayToBase642 } from "@ai-sdk/provider-utils";
|
|
1651
1584
|
function convertToOpenAIResponsesMessages({
|
|
1652
1585
|
prompt,
|
|
1653
1586
|
systemMessageMode
|
|
@@ -1686,38 +1619,35 @@ function convertToOpenAIResponsesMessages({
|
|
|
1686
1619
|
messages.push({
|
|
1687
1620
|
role: "user",
|
|
1688
1621
|
content: content.map((part, index) => {
|
|
1689
|
-
var _a, _b, _c
|
|
1622
|
+
var _a, _b, _c;
|
|
1690
1623
|
switch (part.type) {
|
|
1691
1624
|
case "text": {
|
|
1692
1625
|
return { type: "input_text", text: part.text };
|
|
1693
1626
|
}
|
|
1694
|
-
case "image": {
|
|
1695
|
-
return {
|
|
1696
|
-
type: "input_image",
|
|
1697
|
-
image_url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase642(part.image)}`,
|
|
1698
|
-
// OpenAI specific extension: image detail
|
|
1699
|
-
detail: (_c = (_b = part.providerMetadata) == null ? void 0 : _b.openai) == null ? void 0 : _c.imageDetail
|
|
1700
|
-
};
|
|
1701
|
-
}
|
|
1702
1627
|
case "file": {
|
|
1703
|
-
if (part.
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
}
|
|
1716
|
-
default: {
|
|
1717
|
-
throw new UnsupportedFunctionalityError6({
|
|
1718
|
-
functionality: "Only PDF files are supported in user messages"
|
|
1628
|
+
if (part.mediaType.startsWith("image/")) {
|
|
1629
|
+
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
|
1630
|
+
return {
|
|
1631
|
+
type: "input_image",
|
|
1632
|
+
image_url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${part.data}`,
|
|
1633
|
+
// OpenAI specific extension: image detail
|
|
1634
|
+
detail: (_b = (_a = part.providerOptions) == null ? void 0 : _a.openai) == null ? void 0 : _b.imageDetail
|
|
1635
|
+
};
|
|
1636
|
+
} else if (part.mediaType === "application/pdf") {
|
|
1637
|
+
if (part.data instanceof URL) {
|
|
1638
|
+
throw new UnsupportedFunctionalityError4({
|
|
1639
|
+
functionality: "PDF file parts with URLs"
|
|
1719
1640
|
});
|
|
1720
1641
|
}
|
|
1642
|
+
return {
|
|
1643
|
+
type: "input_file",
|
|
1644
|
+
filename: (_c = part.filename) != null ? _c : `part-${index}.pdf`,
|
|
1645
|
+
file_data: `data:application/pdf;base64,${part.data}`
|
|
1646
|
+
};
|
|
1647
|
+
} else {
|
|
1648
|
+
throw new UnsupportedFunctionalityError4({
|
|
1649
|
+
functionality: `file part media type ${part.mediaType}`
|
|
1650
|
+
});
|
|
1721
1651
|
}
|
|
1722
1652
|
}
|
|
1723
1653
|
}
|
|
@@ -1787,19 +1717,18 @@ function mapOpenAIResponseFinishReason({
|
|
|
1787
1717
|
|
|
1788
1718
|
// src/responses/openai-responses-prepare-tools.ts
|
|
1789
1719
|
import {
|
|
1790
|
-
UnsupportedFunctionalityError as
|
|
1720
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError5
|
|
1791
1721
|
} from "@ai-sdk/provider";
|
|
1792
1722
|
function prepareResponsesTools({
|
|
1793
|
-
|
|
1723
|
+
tools,
|
|
1724
|
+
toolChoice,
|
|
1794
1725
|
strict
|
|
1795
1726
|
}) {
|
|
1796
|
-
|
|
1797
|
-
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
|
1727
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
1798
1728
|
const toolWarnings = [];
|
|
1799
1729
|
if (tools == null) {
|
|
1800
|
-
return { tools: void 0,
|
|
1730
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
1801
1731
|
}
|
|
1802
|
-
const toolChoice = mode.toolChoice;
|
|
1803
1732
|
const openaiTools2 = [];
|
|
1804
1733
|
for (const tool of tools) {
|
|
1805
1734
|
switch (tool.type) {
|
|
@@ -1832,37 +1761,24 @@ function prepareResponsesTools({
|
|
|
1832
1761
|
}
|
|
1833
1762
|
}
|
|
1834
1763
|
if (toolChoice == null) {
|
|
1835
|
-
return { tools: openaiTools2,
|
|
1764
|
+
return { tools: openaiTools2, toolChoice: void 0, toolWarnings };
|
|
1836
1765
|
}
|
|
1837
1766
|
const type = toolChoice.type;
|
|
1838
1767
|
switch (type) {
|
|
1839
1768
|
case "auto":
|
|
1840
1769
|
case "none":
|
|
1841
1770
|
case "required":
|
|
1842
|
-
return { tools: openaiTools2,
|
|
1843
|
-
case "tool":
|
|
1844
|
-
if (toolChoice.toolName === "web_search_preview") {
|
|
1845
|
-
return {
|
|
1846
|
-
tools: openaiTools2,
|
|
1847
|
-
tool_choice: {
|
|
1848
|
-
type: "web_search_preview"
|
|
1849
|
-
},
|
|
1850
|
-
toolWarnings
|
|
1851
|
-
};
|
|
1852
|
-
}
|
|
1771
|
+
return { tools: openaiTools2, toolChoice: type, toolWarnings };
|
|
1772
|
+
case "tool":
|
|
1853
1773
|
return {
|
|
1854
1774
|
tools: openaiTools2,
|
|
1855
|
-
|
|
1856
|
-
type: "function",
|
|
1857
|
-
name: toolChoice.toolName
|
|
1858
|
-
},
|
|
1775
|
+
toolChoice: toolChoice.toolName === "web_search_preview" ? { type: "web_search_preview" } : { type: "function", name: toolChoice.toolName },
|
|
1859
1776
|
toolWarnings
|
|
1860
1777
|
};
|
|
1861
|
-
}
|
|
1862
1778
|
default: {
|
|
1863
1779
|
const _exhaustiveCheck = type;
|
|
1864
|
-
throw new
|
|
1865
|
-
functionality: `
|
|
1780
|
+
throw new UnsupportedFunctionalityError5({
|
|
1781
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
1866
1782
|
});
|
|
1867
1783
|
}
|
|
1868
1784
|
}
|
|
@@ -1872,16 +1788,19 @@ function prepareResponsesTools({
|
|
|
1872
1788
|
var OpenAIResponsesLanguageModel = class {
|
|
1873
1789
|
constructor(modelId, config) {
|
|
1874
1790
|
this.specificationVersion = "v2";
|
|
1875
|
-
this.defaultObjectGenerationMode = "json";
|
|
1876
1791
|
this.modelId = modelId;
|
|
1877
1792
|
this.config = config;
|
|
1878
1793
|
}
|
|
1794
|
+
async getSupportedUrls() {
|
|
1795
|
+
return {
|
|
1796
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
1797
|
+
};
|
|
1798
|
+
}
|
|
1879
1799
|
get provider() {
|
|
1880
1800
|
return this.config.provider;
|
|
1881
1801
|
}
|
|
1882
1802
|
getArgs({
|
|
1883
|
-
|
|
1884
|
-
maxTokens,
|
|
1803
|
+
maxOutputTokens,
|
|
1885
1804
|
temperature,
|
|
1886
1805
|
stopSequences,
|
|
1887
1806
|
topP,
|
|
@@ -1890,24 +1809,19 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1890
1809
|
frequencyPenalty,
|
|
1891
1810
|
seed,
|
|
1892
1811
|
prompt,
|
|
1893
|
-
|
|
1812
|
+
providerOptions,
|
|
1813
|
+
tools,
|
|
1814
|
+
toolChoice,
|
|
1894
1815
|
responseFormat
|
|
1895
1816
|
}) {
|
|
1896
|
-
var _a, _b
|
|
1817
|
+
var _a, _b;
|
|
1897
1818
|
const warnings = [];
|
|
1898
1819
|
const modelConfig = getResponsesModelConfig(this.modelId);
|
|
1899
|
-
const type = mode.type;
|
|
1900
1820
|
if (topK != null) {
|
|
1901
|
-
warnings.push({
|
|
1902
|
-
type: "unsupported-setting",
|
|
1903
|
-
setting: "topK"
|
|
1904
|
-
});
|
|
1821
|
+
warnings.push({ type: "unsupported-setting", setting: "topK" });
|
|
1905
1822
|
}
|
|
1906
1823
|
if (seed != null) {
|
|
1907
|
-
warnings.push({
|
|
1908
|
-
type: "unsupported-setting",
|
|
1909
|
-
setting: "seed"
|
|
1910
|
-
});
|
|
1824
|
+
warnings.push({ type: "unsupported-setting", setting: "seed" });
|
|
1911
1825
|
}
|
|
1912
1826
|
if (presencePenalty != null) {
|
|
1913
1827
|
warnings.push({
|
|
@@ -1922,19 +1836,16 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1922
1836
|
});
|
|
1923
1837
|
}
|
|
1924
1838
|
if (stopSequences != null) {
|
|
1925
|
-
warnings.push({
|
|
1926
|
-
type: "unsupported-setting",
|
|
1927
|
-
setting: "stopSequences"
|
|
1928
|
-
});
|
|
1839
|
+
warnings.push({ type: "unsupported-setting", setting: "stopSequences" });
|
|
1929
1840
|
}
|
|
1930
1841
|
const { messages, warnings: messageWarnings } = convertToOpenAIResponsesMessages({
|
|
1931
1842
|
prompt,
|
|
1932
1843
|
systemMessageMode: modelConfig.systemMessageMode
|
|
1933
1844
|
});
|
|
1934
1845
|
warnings.push(...messageWarnings);
|
|
1935
|
-
const openaiOptions =
|
|
1846
|
+
const openaiOptions = parseProviderOptions4({
|
|
1936
1847
|
provider: "openai",
|
|
1937
|
-
providerOptions
|
|
1848
|
+
providerOptions,
|
|
1938
1849
|
schema: openaiResponsesProviderOptionsSchema
|
|
1939
1850
|
});
|
|
1940
1851
|
const isStrict = (_a = openaiOptions == null ? void 0 : openaiOptions.strictSchemas) != null ? _a : true;
|
|
@@ -1943,7 +1854,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1943
1854
|
input: messages,
|
|
1944
1855
|
temperature,
|
|
1945
1856
|
top_p: topP,
|
|
1946
|
-
max_output_tokens:
|
|
1857
|
+
max_output_tokens: maxOutputTokens,
|
|
1947
1858
|
...(responseFormat == null ? void 0 : responseFormat.type) === "json" && {
|
|
1948
1859
|
text: {
|
|
1949
1860
|
format: responseFormat.schema != null ? {
|
|
@@ -1963,8 +1874,15 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1963
1874
|
user: openaiOptions == null ? void 0 : openaiOptions.user,
|
|
1964
1875
|
instructions: openaiOptions == null ? void 0 : openaiOptions.instructions,
|
|
1965
1876
|
// model-specific settings:
|
|
1966
|
-
...modelConfig.isReasoningModel && (openaiOptions == null ? void 0 : openaiOptions.reasoningEffort) != null && {
|
|
1967
|
-
reasoning: {
|
|
1877
|
+
...modelConfig.isReasoningModel && ((openaiOptions == null ? void 0 : openaiOptions.reasoningEffort) != null || (openaiOptions == null ? void 0 : openaiOptions.reasoningSummary) != null) && {
|
|
1878
|
+
reasoning: {
|
|
1879
|
+
...(openaiOptions == null ? void 0 : openaiOptions.reasoningEffort) != null && {
|
|
1880
|
+
effort: openaiOptions.reasoningEffort
|
|
1881
|
+
},
|
|
1882
|
+
...(openaiOptions == null ? void 0 : openaiOptions.reasoningSummary) != null && {
|
|
1883
|
+
summary: openaiOptions.reasoningSummary
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1968
1886
|
},
|
|
1969
1887
|
...modelConfig.requiredAutoTruncation && {
|
|
1970
1888
|
truncation: "auto"
|
|
@@ -1988,65 +1906,26 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
1988
1906
|
});
|
|
1989
1907
|
}
|
|
1990
1908
|
}
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
return {
|
|
2009
|
-
args: {
|
|
2010
|
-
...baseArgs,
|
|
2011
|
-
text: {
|
|
2012
|
-
format: mode.schema != null ? {
|
|
2013
|
-
type: "json_schema",
|
|
2014
|
-
strict: isStrict,
|
|
2015
|
-
name: (_c = mode.name) != null ? _c : "response",
|
|
2016
|
-
description: mode.description,
|
|
2017
|
-
schema: mode.schema
|
|
2018
|
-
} : { type: "json_object" }
|
|
2019
|
-
}
|
|
2020
|
-
},
|
|
2021
|
-
warnings
|
|
2022
|
-
};
|
|
2023
|
-
}
|
|
2024
|
-
case "object-tool": {
|
|
2025
|
-
return {
|
|
2026
|
-
args: {
|
|
2027
|
-
...baseArgs,
|
|
2028
|
-
tool_choice: { type: "function", name: mode.tool.name },
|
|
2029
|
-
tools: [
|
|
2030
|
-
{
|
|
2031
|
-
type: "function",
|
|
2032
|
-
name: mode.tool.name,
|
|
2033
|
-
description: mode.tool.description,
|
|
2034
|
-
parameters: mode.tool.parameters,
|
|
2035
|
-
strict: isStrict
|
|
2036
|
-
}
|
|
2037
|
-
]
|
|
2038
|
-
},
|
|
2039
|
-
warnings
|
|
2040
|
-
};
|
|
2041
|
-
}
|
|
2042
|
-
default: {
|
|
2043
|
-
const _exhaustiveCheck = type;
|
|
2044
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
2045
|
-
}
|
|
2046
|
-
}
|
|
1909
|
+
const {
|
|
1910
|
+
tools: openaiTools2,
|
|
1911
|
+
toolChoice: openaiToolChoice,
|
|
1912
|
+
toolWarnings
|
|
1913
|
+
} = prepareResponsesTools({
|
|
1914
|
+
tools,
|
|
1915
|
+
toolChoice,
|
|
1916
|
+
strict: isStrict
|
|
1917
|
+
});
|
|
1918
|
+
return {
|
|
1919
|
+
args: {
|
|
1920
|
+
...baseArgs,
|
|
1921
|
+
tools: openaiTools2,
|
|
1922
|
+
tool_choice: openaiToolChoice
|
|
1923
|
+
},
|
|
1924
|
+
warnings: [...warnings, ...toolWarnings]
|
|
1925
|
+
};
|
|
2047
1926
|
}
|
|
2048
1927
|
async doGenerate(options) {
|
|
2049
|
-
var _a, _b, _c, _d, _e;
|
|
1928
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
2050
1929
|
const { args: body, warnings } = this.getArgs(options);
|
|
2051
1930
|
const {
|
|
2052
1931
|
responseHeaders,
|
|
@@ -2057,109 +1936,129 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2057
1936
|
path: "/responses",
|
|
2058
1937
|
modelId: this.modelId
|
|
2059
1938
|
}),
|
|
2060
|
-
headers:
|
|
1939
|
+
headers: combineHeaders6(this.config.headers(), options.headers),
|
|
2061
1940
|
body,
|
|
2062
1941
|
failedResponseHandler: openaiFailedResponseHandler,
|
|
2063
|
-
successfulResponseHandler:
|
|
2064
|
-
|
|
2065
|
-
id:
|
|
2066
|
-
created_at:
|
|
2067
|
-
model:
|
|
2068
|
-
output:
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
type:
|
|
2072
|
-
role:
|
|
2073
|
-
content:
|
|
2074
|
-
|
|
2075
|
-
type:
|
|
2076
|
-
text:
|
|
2077
|
-
annotations:
|
|
2078
|
-
|
|
2079
|
-
type:
|
|
2080
|
-
start_index:
|
|
2081
|
-
end_index:
|
|
2082
|
-
url:
|
|
2083
|
-
title:
|
|
1942
|
+
successfulResponseHandler: createJsonResponseHandler6(
|
|
1943
|
+
z10.object({
|
|
1944
|
+
id: z10.string(),
|
|
1945
|
+
created_at: z10.number(),
|
|
1946
|
+
model: z10.string(),
|
|
1947
|
+
output: z10.array(
|
|
1948
|
+
z10.discriminatedUnion("type", [
|
|
1949
|
+
z10.object({
|
|
1950
|
+
type: z10.literal("message"),
|
|
1951
|
+
role: z10.literal("assistant"),
|
|
1952
|
+
content: z10.array(
|
|
1953
|
+
z10.object({
|
|
1954
|
+
type: z10.literal("output_text"),
|
|
1955
|
+
text: z10.string(),
|
|
1956
|
+
annotations: z10.array(
|
|
1957
|
+
z10.object({
|
|
1958
|
+
type: z10.literal("url_citation"),
|
|
1959
|
+
start_index: z10.number(),
|
|
1960
|
+
end_index: z10.number(),
|
|
1961
|
+
url: z10.string(),
|
|
1962
|
+
title: z10.string()
|
|
2084
1963
|
})
|
|
2085
1964
|
)
|
|
2086
1965
|
})
|
|
2087
1966
|
)
|
|
2088
1967
|
}),
|
|
2089
|
-
|
|
2090
|
-
type:
|
|
2091
|
-
call_id:
|
|
2092
|
-
name:
|
|
2093
|
-
arguments:
|
|
1968
|
+
z10.object({
|
|
1969
|
+
type: z10.literal("function_call"),
|
|
1970
|
+
call_id: z10.string(),
|
|
1971
|
+
name: z10.string(),
|
|
1972
|
+
arguments: z10.string()
|
|
2094
1973
|
}),
|
|
2095
|
-
|
|
2096
|
-
type:
|
|
1974
|
+
z10.object({
|
|
1975
|
+
type: z10.literal("web_search_call")
|
|
2097
1976
|
}),
|
|
2098
|
-
|
|
2099
|
-
type:
|
|
1977
|
+
z10.object({
|
|
1978
|
+
type: z10.literal("computer_call")
|
|
2100
1979
|
}),
|
|
2101
|
-
|
|
2102
|
-
type:
|
|
1980
|
+
z10.object({
|
|
1981
|
+
type: z10.literal("reasoning"),
|
|
1982
|
+
summary: z10.array(
|
|
1983
|
+
z10.object({
|
|
1984
|
+
type: z10.literal("summary_text"),
|
|
1985
|
+
text: z10.string()
|
|
1986
|
+
})
|
|
1987
|
+
)
|
|
2103
1988
|
})
|
|
2104
1989
|
])
|
|
2105
1990
|
),
|
|
2106
|
-
incomplete_details:
|
|
1991
|
+
incomplete_details: z10.object({ reason: z10.string() }).nullable(),
|
|
2107
1992
|
usage: usageSchema
|
|
2108
1993
|
})
|
|
2109
1994
|
),
|
|
2110
1995
|
abortSignal: options.abortSignal,
|
|
2111
1996
|
fetch: this.config.fetch
|
|
2112
1997
|
});
|
|
2113
|
-
const
|
|
2114
|
-
const
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
1998
|
+
const content = [];
|
|
1999
|
+
for (const part of response.output) {
|
|
2000
|
+
switch (part.type) {
|
|
2001
|
+
case "reasoning": {
|
|
2002
|
+
content.push({
|
|
2003
|
+
type: "reasoning",
|
|
2004
|
+
reasoningType: "text",
|
|
2005
|
+
text: part.summary.map((summary) => summary.text).join()
|
|
2006
|
+
});
|
|
2007
|
+
break;
|
|
2008
|
+
}
|
|
2009
|
+
case "message": {
|
|
2010
|
+
for (const contentPart of part.content) {
|
|
2011
|
+
content.push({
|
|
2012
|
+
type: "text",
|
|
2013
|
+
text: contentPart.text
|
|
2014
|
+
});
|
|
2015
|
+
for (const annotation of contentPart.annotations) {
|
|
2016
|
+
content.push({
|
|
2017
|
+
type: "source",
|
|
2018
|
+
sourceType: "url",
|
|
2019
|
+
id: (_c = (_b = (_a = this.config).generateId) == null ? void 0 : _b.call(_a)) != null ? _c : generateId2(),
|
|
2020
|
+
url: annotation.url,
|
|
2021
|
+
title: annotation.title
|
|
2022
|
+
});
|
|
2023
|
+
}
|
|
2024
|
+
}
|
|
2025
|
+
break;
|
|
2026
|
+
}
|
|
2027
|
+
case "function_call": {
|
|
2028
|
+
content.push({
|
|
2029
|
+
type: "tool-call",
|
|
2030
|
+
toolCallType: "function",
|
|
2031
|
+
toolCallId: part.call_id,
|
|
2032
|
+
toolName: part.name,
|
|
2033
|
+
args: part.arguments
|
|
2034
|
+
});
|
|
2035
|
+
break;
|
|
2036
|
+
}
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2120
2039
|
return {
|
|
2121
|
-
|
|
2122
|
-
sources: outputTextElements.flatMap(
|
|
2123
|
-
(content) => content.annotations.map((annotation) => {
|
|
2124
|
-
var _a2, _b2, _c2;
|
|
2125
|
-
return {
|
|
2126
|
-
sourceType: "url",
|
|
2127
|
-
id: (_c2 = (_b2 = (_a2 = this.config).generateId) == null ? void 0 : _b2.call(_a2)) != null ? _c2 : generateId2(),
|
|
2128
|
-
url: annotation.url,
|
|
2129
|
-
title: annotation.title
|
|
2130
|
-
};
|
|
2131
|
-
})
|
|
2132
|
-
),
|
|
2040
|
+
content,
|
|
2133
2041
|
finishReason: mapOpenAIResponseFinishReason({
|
|
2134
|
-
finishReason: (
|
|
2135
|
-
hasToolCalls:
|
|
2042
|
+
finishReason: (_d = response.incomplete_details) == null ? void 0 : _d.reason,
|
|
2043
|
+
hasToolCalls: content.some((part) => part.type === "tool-call")
|
|
2136
2044
|
}),
|
|
2137
|
-
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
2138
2045
|
usage: {
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
},
|
|
2142
|
-
rawCall: {
|
|
2143
|
-
rawPrompt: void 0,
|
|
2144
|
-
rawSettings: {}
|
|
2145
|
-
},
|
|
2146
|
-
rawResponse: {
|
|
2147
|
-
headers: responseHeaders,
|
|
2148
|
-
body: rawResponse
|
|
2149
|
-
},
|
|
2150
|
-
request: {
|
|
2151
|
-
body: JSON.stringify(body)
|
|
2046
|
+
inputTokens: response.usage.input_tokens,
|
|
2047
|
+
outputTokens: response.usage.output_tokens
|
|
2152
2048
|
},
|
|
2049
|
+
request: { body },
|
|
2153
2050
|
response: {
|
|
2154
2051
|
id: response.id,
|
|
2155
2052
|
timestamp: new Date(response.created_at * 1e3),
|
|
2156
|
-
modelId: response.model
|
|
2053
|
+
modelId: response.model,
|
|
2054
|
+
headers: responseHeaders,
|
|
2055
|
+
body: rawResponse
|
|
2157
2056
|
},
|
|
2158
2057
|
providerMetadata: {
|
|
2159
2058
|
openai: {
|
|
2160
2059
|
responseId: response.id,
|
|
2161
|
-
cachedPromptTokens: (
|
|
2162
|
-
reasoningTokens: (
|
|
2060
|
+
cachedPromptTokens: (_f = (_e = response.usage.input_tokens_details) == null ? void 0 : _e.cached_tokens) != null ? _f : null,
|
|
2061
|
+
reasoningTokens: (_h = (_g = response.usage.output_tokens_details) == null ? void 0 : _g.reasoning_tokens) != null ? _h : null
|
|
2163
2062
|
}
|
|
2164
2063
|
},
|
|
2165
2064
|
warnings
|
|
@@ -2172,7 +2071,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2172
2071
|
path: "/responses",
|
|
2173
2072
|
modelId: this.modelId
|
|
2174
2073
|
}),
|
|
2175
|
-
headers:
|
|
2074
|
+
headers: combineHeaders6(this.config.headers(), options.headers),
|
|
2176
2075
|
body: {
|
|
2177
2076
|
...body,
|
|
2178
2077
|
stream: true
|
|
@@ -2186,8 +2085,10 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2186
2085
|
});
|
|
2187
2086
|
const self = this;
|
|
2188
2087
|
let finishReason = "unknown";
|
|
2189
|
-
|
|
2190
|
-
|
|
2088
|
+
const usage = {
|
|
2089
|
+
inputTokens: void 0,
|
|
2090
|
+
outputTokens: void 0
|
|
2091
|
+
};
|
|
2191
2092
|
let cachedPromptTokens = null;
|
|
2192
2093
|
let reasoningTokens = null;
|
|
2193
2094
|
let responseId = null;
|
|
@@ -2196,6 +2097,9 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2196
2097
|
return {
|
|
2197
2098
|
stream: response.pipeThrough(
|
|
2198
2099
|
new TransformStream({
|
|
2100
|
+
start(controller) {
|
|
2101
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
2102
|
+
},
|
|
2199
2103
|
transform(chunk, controller) {
|
|
2200
2104
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
2201
2105
|
if (!chunk.success) {
|
|
@@ -2239,8 +2143,14 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2239
2143
|
});
|
|
2240
2144
|
} else if (isTextDeltaChunk(value)) {
|
|
2241
2145
|
controller.enqueue({
|
|
2242
|
-
type: "text
|
|
2243
|
-
|
|
2146
|
+
type: "text",
|
|
2147
|
+
text: value.delta
|
|
2148
|
+
});
|
|
2149
|
+
} else if (isResponseReasoningSummaryTextDeltaChunk(value)) {
|
|
2150
|
+
controller.enqueue({
|
|
2151
|
+
type: "reasoning",
|
|
2152
|
+
reasoningType: "text",
|
|
2153
|
+
text: value.delta
|
|
2244
2154
|
});
|
|
2245
2155
|
} else if (isResponseOutputItemDoneChunk(value) && value.item.type === "function_call") {
|
|
2246
2156
|
ongoingToolCalls[value.output_index] = void 0;
|
|
@@ -2257,19 +2167,17 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2257
2167
|
finishReason: (_a = value.response.incomplete_details) == null ? void 0 : _a.reason,
|
|
2258
2168
|
hasToolCalls
|
|
2259
2169
|
});
|
|
2260
|
-
|
|
2261
|
-
|
|
2170
|
+
usage.inputTokens = value.response.usage.input_tokens;
|
|
2171
|
+
usage.outputTokens = value.response.usage.output_tokens;
|
|
2262
2172
|
cachedPromptTokens = (_c = (_b = value.response.usage.input_tokens_details) == null ? void 0 : _b.cached_tokens) != null ? _c : cachedPromptTokens;
|
|
2263
2173
|
reasoningTokens = (_e = (_d = value.response.usage.output_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : reasoningTokens;
|
|
2264
2174
|
} else if (isResponseAnnotationAddedChunk(value)) {
|
|
2265
2175
|
controller.enqueue({
|
|
2266
2176
|
type: "source",
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
title: value.annotation.title
|
|
2272
|
-
}
|
|
2177
|
+
sourceType: "url",
|
|
2178
|
+
id: (_h = (_g = (_f = self.config).generateId) == null ? void 0 : _g.call(_f)) != null ? _h : generateId2(),
|
|
2179
|
+
url: value.annotation.url,
|
|
2180
|
+
title: value.annotation.title
|
|
2273
2181
|
});
|
|
2274
2182
|
}
|
|
2275
2183
|
},
|
|
@@ -2277,7 +2185,7 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2277
2185
|
controller.enqueue({
|
|
2278
2186
|
type: "finish",
|
|
2279
2187
|
finishReason,
|
|
2280
|
-
usage
|
|
2188
|
+
usage,
|
|
2281
2189
|
...(cachedPromptTokens != null || reasoningTokens != null) && {
|
|
2282
2190
|
providerMetadata: {
|
|
2283
2191
|
openai: {
|
|
@@ -2291,89 +2199,91 @@ var OpenAIResponsesLanguageModel = class {
|
|
|
2291
2199
|
}
|
|
2292
2200
|
})
|
|
2293
2201
|
),
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
rawSettings: {}
|
|
2297
|
-
},
|
|
2298
|
-
rawResponse: { headers: responseHeaders },
|
|
2299
|
-
request: { body: JSON.stringify(body) },
|
|
2300
|
-
warnings
|
|
2202
|
+
request: { body },
|
|
2203
|
+
response: { headers: responseHeaders }
|
|
2301
2204
|
};
|
|
2302
2205
|
}
|
|
2303
2206
|
};
|
|
2304
|
-
var usageSchema =
|
|
2305
|
-
input_tokens:
|
|
2306
|
-
input_tokens_details:
|
|
2307
|
-
output_tokens:
|
|
2308
|
-
output_tokens_details:
|
|
2207
|
+
var usageSchema = z10.object({
|
|
2208
|
+
input_tokens: z10.number(),
|
|
2209
|
+
input_tokens_details: z10.object({ cached_tokens: z10.number().nullish() }).nullish(),
|
|
2210
|
+
output_tokens: z10.number(),
|
|
2211
|
+
output_tokens_details: z10.object({ reasoning_tokens: z10.number().nullish() }).nullish()
|
|
2309
2212
|
});
|
|
2310
|
-
var textDeltaChunkSchema =
|
|
2311
|
-
type:
|
|
2312
|
-
delta:
|
|
2213
|
+
var textDeltaChunkSchema = z10.object({
|
|
2214
|
+
type: z10.literal("response.output_text.delta"),
|
|
2215
|
+
delta: z10.string()
|
|
2313
2216
|
});
|
|
2314
|
-
var responseFinishedChunkSchema =
|
|
2315
|
-
type:
|
|
2316
|
-
response:
|
|
2317
|
-
incomplete_details:
|
|
2217
|
+
var responseFinishedChunkSchema = z10.object({
|
|
2218
|
+
type: z10.enum(["response.completed", "response.incomplete"]),
|
|
2219
|
+
response: z10.object({
|
|
2220
|
+
incomplete_details: z10.object({ reason: z10.string() }).nullish(),
|
|
2318
2221
|
usage: usageSchema
|
|
2319
2222
|
})
|
|
2320
2223
|
});
|
|
2321
|
-
var responseCreatedChunkSchema =
|
|
2322
|
-
type:
|
|
2323
|
-
response:
|
|
2324
|
-
id:
|
|
2325
|
-
created_at:
|
|
2326
|
-
model:
|
|
2224
|
+
var responseCreatedChunkSchema = z10.object({
|
|
2225
|
+
type: z10.literal("response.created"),
|
|
2226
|
+
response: z10.object({
|
|
2227
|
+
id: z10.string(),
|
|
2228
|
+
created_at: z10.number(),
|
|
2229
|
+
model: z10.string()
|
|
2327
2230
|
})
|
|
2328
2231
|
});
|
|
2329
|
-
var responseOutputItemDoneSchema =
|
|
2330
|
-
type:
|
|
2331
|
-
output_index:
|
|
2332
|
-
item:
|
|
2333
|
-
|
|
2334
|
-
type:
|
|
2232
|
+
var responseOutputItemDoneSchema = z10.object({
|
|
2233
|
+
type: z10.literal("response.output_item.done"),
|
|
2234
|
+
output_index: z10.number(),
|
|
2235
|
+
item: z10.discriminatedUnion("type", [
|
|
2236
|
+
z10.object({
|
|
2237
|
+
type: z10.literal("message")
|
|
2335
2238
|
}),
|
|
2336
|
-
|
|
2337
|
-
type:
|
|
2338
|
-
id:
|
|
2339
|
-
call_id:
|
|
2340
|
-
name:
|
|
2341
|
-
arguments:
|
|
2342
|
-
status:
|
|
2239
|
+
z10.object({
|
|
2240
|
+
type: z10.literal("function_call"),
|
|
2241
|
+
id: z10.string(),
|
|
2242
|
+
call_id: z10.string(),
|
|
2243
|
+
name: z10.string(),
|
|
2244
|
+
arguments: z10.string(),
|
|
2245
|
+
status: z10.literal("completed")
|
|
2343
2246
|
})
|
|
2344
2247
|
])
|
|
2345
2248
|
});
|
|
2346
|
-
var responseFunctionCallArgumentsDeltaSchema =
|
|
2347
|
-
type:
|
|
2348
|
-
item_id:
|
|
2349
|
-
output_index:
|
|
2350
|
-
delta:
|
|
2249
|
+
var responseFunctionCallArgumentsDeltaSchema = z10.object({
|
|
2250
|
+
type: z10.literal("response.function_call_arguments.delta"),
|
|
2251
|
+
item_id: z10.string(),
|
|
2252
|
+
output_index: z10.number(),
|
|
2253
|
+
delta: z10.string()
|
|
2351
2254
|
});
|
|
2352
|
-
var responseOutputItemAddedSchema =
|
|
2353
|
-
type:
|
|
2354
|
-
output_index:
|
|
2355
|
-
item:
|
|
2356
|
-
|
|
2357
|
-
type:
|
|
2255
|
+
var responseOutputItemAddedSchema = z10.object({
|
|
2256
|
+
type: z10.literal("response.output_item.added"),
|
|
2257
|
+
output_index: z10.number(),
|
|
2258
|
+
item: z10.discriminatedUnion("type", [
|
|
2259
|
+
z10.object({
|
|
2260
|
+
type: z10.literal("message")
|
|
2358
2261
|
}),
|
|
2359
|
-
|
|
2360
|
-
type:
|
|
2361
|
-
id:
|
|
2362
|
-
call_id:
|
|
2363
|
-
name:
|
|
2364
|
-
arguments:
|
|
2262
|
+
z10.object({
|
|
2263
|
+
type: z10.literal("function_call"),
|
|
2264
|
+
id: z10.string(),
|
|
2265
|
+
call_id: z10.string(),
|
|
2266
|
+
name: z10.string(),
|
|
2267
|
+
arguments: z10.string()
|
|
2365
2268
|
})
|
|
2366
2269
|
])
|
|
2367
2270
|
});
|
|
2368
|
-
var responseAnnotationAddedSchema =
|
|
2369
|
-
type:
|
|
2370
|
-
annotation:
|
|
2371
|
-
type:
|
|
2372
|
-
url:
|
|
2373
|
-
title:
|
|
2271
|
+
var responseAnnotationAddedSchema = z10.object({
|
|
2272
|
+
type: z10.literal("response.output_text.annotation.added"),
|
|
2273
|
+
annotation: z10.object({
|
|
2274
|
+
type: z10.literal("url_citation"),
|
|
2275
|
+
url: z10.string(),
|
|
2276
|
+
title: z10.string()
|
|
2374
2277
|
})
|
|
2375
2278
|
});
|
|
2376
|
-
var
|
|
2279
|
+
var responseReasoningSummaryTextDeltaSchema = z10.object({
|
|
2280
|
+
type: z10.literal("response.reasoning_summary_text.delta"),
|
|
2281
|
+
item_id: z10.string(),
|
|
2282
|
+
output_index: z10.number(),
|
|
2283
|
+
summary_index: z10.number(),
|
|
2284
|
+
delta: z10.string()
|
|
2285
|
+
});
|
|
2286
|
+
var openaiResponsesChunkSchema = z10.union([
|
|
2377
2287
|
textDeltaChunkSchema,
|
|
2378
2288
|
responseFinishedChunkSchema,
|
|
2379
2289
|
responseCreatedChunkSchema,
|
|
@@ -2381,7 +2291,8 @@ var openaiResponsesChunkSchema = z7.union([
|
|
|
2381
2291
|
responseFunctionCallArgumentsDeltaSchema,
|
|
2382
2292
|
responseOutputItemAddedSchema,
|
|
2383
2293
|
responseAnnotationAddedSchema,
|
|
2384
|
-
|
|
2294
|
+
responseReasoningSummaryTextDeltaSchema,
|
|
2295
|
+
z10.object({ type: z10.string() }).passthrough()
|
|
2385
2296
|
// fallback for unknown chunks
|
|
2386
2297
|
]);
|
|
2387
2298
|
function isTextDeltaChunk(chunk) {
|
|
@@ -2405,6 +2316,9 @@ function isResponseOutputItemAddedChunk(chunk) {
|
|
|
2405
2316
|
function isResponseAnnotationAddedChunk(chunk) {
|
|
2406
2317
|
return chunk.type === "response.output_text.annotation.added";
|
|
2407
2318
|
}
|
|
2319
|
+
function isResponseReasoningSummaryTextDeltaChunk(chunk) {
|
|
2320
|
+
return chunk.type === "response.reasoning_summary_text.delta";
|
|
2321
|
+
}
|
|
2408
2322
|
function getResponsesModelConfig(modelId) {
|
|
2409
2323
|
if (modelId.startsWith("o")) {
|
|
2410
2324
|
if (modelId.startsWith("o1-mini") || modelId.startsWith("o1-preview")) {
|
|
@@ -2426,16 +2340,121 @@ function getResponsesModelConfig(modelId) {
|
|
|
2426
2340
|
requiredAutoTruncation: false
|
|
2427
2341
|
};
|
|
2428
2342
|
}
|
|
2429
|
-
var openaiResponsesProviderOptionsSchema =
|
|
2430
|
-
metadata:
|
|
2431
|
-
parallelToolCalls:
|
|
2432
|
-
previousResponseId:
|
|
2433
|
-
store:
|
|
2434
|
-
user:
|
|
2435
|
-
reasoningEffort:
|
|
2436
|
-
strictSchemas:
|
|
2437
|
-
instructions:
|
|
2343
|
+
var openaiResponsesProviderOptionsSchema = z10.object({
|
|
2344
|
+
metadata: z10.any().nullish(),
|
|
2345
|
+
parallelToolCalls: z10.boolean().nullish(),
|
|
2346
|
+
previousResponseId: z10.string().nullish(),
|
|
2347
|
+
store: z10.boolean().nullish(),
|
|
2348
|
+
user: z10.string().nullish(),
|
|
2349
|
+
reasoningEffort: z10.string().nullish(),
|
|
2350
|
+
strictSchemas: z10.boolean().nullish(),
|
|
2351
|
+
instructions: z10.string().nullish(),
|
|
2352
|
+
reasoningSummary: z10.string().nullish()
|
|
2353
|
+
});
|
|
2354
|
+
|
|
2355
|
+
// src/openai-speech-model.ts
|
|
2356
|
+
import {
|
|
2357
|
+
combineHeaders as combineHeaders7,
|
|
2358
|
+
createBinaryResponseHandler,
|
|
2359
|
+
parseProviderOptions as parseProviderOptions5,
|
|
2360
|
+
postJsonToApi as postJsonToApi6
|
|
2361
|
+
} from "@ai-sdk/provider-utils";
|
|
2362
|
+
import { z as z11 } from "zod";
|
|
2363
|
+
var OpenAIProviderOptionsSchema = z11.object({
|
|
2364
|
+
instructions: z11.string().nullish(),
|
|
2365
|
+
speed: z11.number().min(0.25).max(4).default(1).nullish()
|
|
2438
2366
|
});
|
|
2367
|
+
var OpenAISpeechModel = class {
|
|
2368
|
+
constructor(modelId, config) {
|
|
2369
|
+
this.modelId = modelId;
|
|
2370
|
+
this.config = config;
|
|
2371
|
+
this.specificationVersion = "v1";
|
|
2372
|
+
}
|
|
2373
|
+
get provider() {
|
|
2374
|
+
return this.config.provider;
|
|
2375
|
+
}
|
|
2376
|
+
getArgs({
|
|
2377
|
+
text,
|
|
2378
|
+
voice = "alloy",
|
|
2379
|
+
outputFormat = "mp3",
|
|
2380
|
+
speed,
|
|
2381
|
+
instructions,
|
|
2382
|
+
providerOptions
|
|
2383
|
+
}) {
|
|
2384
|
+
const warnings = [];
|
|
2385
|
+
const openAIOptions = parseProviderOptions5({
|
|
2386
|
+
provider: "openai",
|
|
2387
|
+
providerOptions,
|
|
2388
|
+
schema: OpenAIProviderOptionsSchema
|
|
2389
|
+
});
|
|
2390
|
+
const requestBody = {
|
|
2391
|
+
model: this.modelId,
|
|
2392
|
+
input: text,
|
|
2393
|
+
voice,
|
|
2394
|
+
response_format: "mp3",
|
|
2395
|
+
speed,
|
|
2396
|
+
instructions
|
|
2397
|
+
};
|
|
2398
|
+
if (outputFormat) {
|
|
2399
|
+
if (["mp3", "opus", "aac", "flac", "wav", "pcm"].includes(outputFormat)) {
|
|
2400
|
+
requestBody.response_format = outputFormat;
|
|
2401
|
+
} else {
|
|
2402
|
+
warnings.push({
|
|
2403
|
+
type: "unsupported-setting",
|
|
2404
|
+
setting: "outputFormat",
|
|
2405
|
+
details: `Unsupported output format: ${outputFormat}. Using mp3 instead.`
|
|
2406
|
+
});
|
|
2407
|
+
}
|
|
2408
|
+
}
|
|
2409
|
+
if (openAIOptions) {
|
|
2410
|
+
const speechModelOptions = {};
|
|
2411
|
+
for (const key in speechModelOptions) {
|
|
2412
|
+
const value = speechModelOptions[key];
|
|
2413
|
+
if (value !== void 0) {
|
|
2414
|
+
requestBody[key] = value;
|
|
2415
|
+
}
|
|
2416
|
+
}
|
|
2417
|
+
}
|
|
2418
|
+
return {
|
|
2419
|
+
requestBody,
|
|
2420
|
+
warnings
|
|
2421
|
+
};
|
|
2422
|
+
}
|
|
2423
|
+
async doGenerate(options) {
|
|
2424
|
+
var _a, _b, _c;
|
|
2425
|
+
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
2426
|
+
const { requestBody, warnings } = this.getArgs(options);
|
|
2427
|
+
const {
|
|
2428
|
+
value: audio,
|
|
2429
|
+
responseHeaders,
|
|
2430
|
+
rawValue: rawResponse
|
|
2431
|
+
} = await postJsonToApi6({
|
|
2432
|
+
url: this.config.url({
|
|
2433
|
+
path: "/audio/speech",
|
|
2434
|
+
modelId: this.modelId
|
|
2435
|
+
}),
|
|
2436
|
+
headers: combineHeaders7(this.config.headers(), options.headers),
|
|
2437
|
+
body: requestBody,
|
|
2438
|
+
failedResponseHandler: openaiFailedResponseHandler,
|
|
2439
|
+
successfulResponseHandler: createBinaryResponseHandler(),
|
|
2440
|
+
abortSignal: options.abortSignal,
|
|
2441
|
+
fetch: this.config.fetch
|
|
2442
|
+
});
|
|
2443
|
+
return {
|
|
2444
|
+
audio,
|
|
2445
|
+
warnings,
|
|
2446
|
+
request: {
|
|
2447
|
+
body: JSON.stringify(requestBody)
|
|
2448
|
+
},
|
|
2449
|
+
response: {
|
|
2450
|
+
timestamp: currentDate,
|
|
2451
|
+
modelId: this.modelId,
|
|
2452
|
+
headers: responseHeaders,
|
|
2453
|
+
body: rawResponse
|
|
2454
|
+
}
|
|
2455
|
+
};
|
|
2456
|
+
}
|
|
2457
|
+
};
|
|
2439
2458
|
|
|
2440
2459
|
// src/openai-provider.ts
|
|
2441
2460
|
function createOpenAI(options = {}) {
|
|
@@ -2479,6 +2498,18 @@ function createOpenAI(options = {}) {
|
|
|
2479
2498
|
headers: getHeaders,
|
|
2480
2499
|
fetch: options.fetch
|
|
2481
2500
|
});
|
|
2501
|
+
const createTranscriptionModel = (modelId) => new OpenAITranscriptionModel(modelId, {
|
|
2502
|
+
provider: `${providerName}.transcription`,
|
|
2503
|
+
url: ({ path }) => `${baseURL}${path}`,
|
|
2504
|
+
headers: getHeaders,
|
|
2505
|
+
fetch: options.fetch
|
|
2506
|
+
});
|
|
2507
|
+
const createSpeechModel = (modelId) => new OpenAISpeechModel(modelId, {
|
|
2508
|
+
provider: `${providerName}.speech`,
|
|
2509
|
+
url: ({ path }) => `${baseURL}${path}`,
|
|
2510
|
+
headers: getHeaders,
|
|
2511
|
+
fetch: options.fetch
|
|
2512
|
+
});
|
|
2482
2513
|
const createLanguageModel = (modelId, settings) => {
|
|
2483
2514
|
if (new.target) {
|
|
2484
2515
|
throw new Error(
|
|
@@ -2513,6 +2544,10 @@ function createOpenAI(options = {}) {
|
|
|
2513
2544
|
provider.textEmbeddingModel = createEmbeddingModel;
|
|
2514
2545
|
provider.image = createImageModel;
|
|
2515
2546
|
provider.imageModel = createImageModel;
|
|
2547
|
+
provider.transcription = createTranscriptionModel;
|
|
2548
|
+
provider.transcriptionModel = createTranscriptionModel;
|
|
2549
|
+
provider.speech = createSpeechModel;
|
|
2550
|
+
provider.speechModel = createSpeechModel;
|
|
2516
2551
|
provider.tools = openaiTools;
|
|
2517
2552
|
return provider;
|
|
2518
2553
|
}
|