@ai-sdk/openai 2.0.0-canary.1 → 2.0.0-canary.10

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/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 z2 } from "zod";
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 { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
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, _d;
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.data instanceof URL) {
87
- throw new UnsupportedFunctionalityError({
88
- functionality: "'File content parts with URL data' functionality not supported."
89
- });
90
- }
91
- switch (part.mimeType) {
92
- case "audio/wav": {
93
- return {
94
- type: "input_audio",
95
- input_audio: { data: part.data, format: "wav" }
96
- };
97
- }
98
- case "audio/mp3":
99
- case "audio/mpeg": {
100
- return {
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
- case "application/pdf": {
106
- return {
107
- type: "file",
108
- file: {
109
- filename: (_d = part.filename) != null ? _d : `part-${index}.pdf`,
110
- file_data: `data:application/pdf;base64,${part.data}`
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
- default: {
117
+ } else if (part.mediaType === "application/pdf") {
118
+ if (part.data instanceof URL) {
115
119
  throw new UnsupportedFunctionalityError({
116
- functionality: `File content part type ${part.mimeType} in user messages`
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
- if (useLegacyFunctionCalling) {
149
- if (toolCalls.length > 1) {
150
- throw new UnsupportedFunctionalityError({
151
- functionality: "useLegacyFunctionCalling with multiple tool calls in one message"
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
- if (useLegacyFunctionCalling) {
171
- messages.push({
172
- role: "function",
173
- name: toolResponse.toolName,
174
- content: JSON.stringify(toolResponse.result)
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,6 +186,19 @@ function convertToOpenAIChatMessages({
192
186
  return { messages, warnings };
193
187
  }
194
188
 
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
+ };
200
+ }
201
+
195
202
  // src/map-openai-chat-logprobs.ts
196
203
  function mapOpenAIChatLogProbsOutput(logprobs) {
197
204
  var _a, _b;
@@ -222,18 +229,69 @@ function mapOpenAIFinishReason(finishReason) {
222
229
  }
223
230
  }
224
231
 
225
- // src/openai-error.ts
232
+ // src/openai-chat-options.ts
226
233
  import { z } from "zod";
234
+ var openaiProviderOptions = z.object({
235
+ /**
236
+ * Modify the likelihood of specified tokens appearing in the completion.
237
+ *
238
+ * Accepts a JSON object that maps tokens (specified by their token ID in
239
+ * the GPT tokenizer) to an associated bias value from -100 to 100.
240
+ */
241
+ logitBias: z.record(z.coerce.number(), z.number()).optional(),
242
+ /**
243
+ * Return the log probabilities of the tokens.
244
+ *
245
+ * Setting to true will return the log probabilities of the tokens that
246
+ * were generated.
247
+ *
248
+ * Setting to a number will return the log probabilities of the top n
249
+ * tokens that were generated.
250
+ */
251
+ logprobs: z.union([z.boolean(), z.number()]).optional(),
252
+ /**
253
+ * Whether to enable parallel function calling during tool use. Default to true.
254
+ */
255
+ parallelToolCalls: z.boolean().optional(),
256
+ /**
257
+ * A unique identifier representing your end-user, which can help OpenAI to
258
+ * monitor and detect abuse.
259
+ */
260
+ user: z.string().optional(),
261
+ /**
262
+ * Reasoning effort for reasoning models. Defaults to `medium`.
263
+ */
264
+ reasoningEffort: z.enum(["low", "medium", "high"]).optional(),
265
+ /**
266
+ * Maximum number of completion tokens to generate. Useful for reasoning models.
267
+ */
268
+ maxCompletionTokens: z.number().optional(),
269
+ /**
270
+ * Whether to enable persistence in responses API.
271
+ */
272
+ store: z.boolean().optional(),
273
+ /**
274
+ * Metadata to associate with the request.
275
+ */
276
+ metadata: z.record(z.string()).optional(),
277
+ /**
278
+ * Parameters for prediction mode.
279
+ */
280
+ prediction: z.record(z.any()).optional()
281
+ });
282
+
283
+ // src/openai-error.ts
284
+ import { z as z2 } from "zod";
227
285
  import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
228
- var openaiErrorDataSchema = z.object({
229
- error: z.object({
230
- message: z.string(),
286
+ var openaiErrorDataSchema = z2.object({
287
+ error: z2.object({
288
+ message: z2.string(),
231
289
  // The additional information below is handled loosely to support
232
290
  // OpenAI-compatible providers that have slightly different error
233
291
  // responses:
234
- type: z.string().nullish(),
235
- param: z.any().nullish(),
236
- code: z.union([z.string(), z.number()]).nullish()
292
+ type: z2.string().nullish(),
293
+ param: z2.any().nullish(),
294
+ code: z2.union([z2.string(), z2.number()]).nullish()
237
295
  })
238
296
  });
239
297
  var openaiFailedResponseHandler = createJsonErrorResponseHandler({
@@ -241,76 +299,19 @@ var openaiFailedResponseHandler = createJsonErrorResponseHandler({
241
299
  errorToMessage: (data) => data.error.message
242
300
  });
243
301
 
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
302
  // src/openai-prepare-tools.ts
258
303
  import {
259
304
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
260
305
  } from "@ai-sdk/provider";
261
306
  function prepareTools({
262
- mode,
263
- useLegacyFunctionCalling = false,
307
+ tools,
308
+ toolChoice,
264
309
  structuredOutputs
265
310
  }) {
266
- var _a;
267
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
311
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
268
312
  const toolWarnings = [];
269
313
  if (tools == null) {
270
- return { tools: void 0, tool_choice: void 0, toolWarnings };
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
- }
314
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
314
315
  }
315
316
  const openaiTools2 = [];
316
317
  for (const tool of tools) {
@@ -329,18 +330,18 @@ function prepareTools({
329
330
  }
330
331
  }
331
332
  if (toolChoice == null) {
332
- return { tools: openaiTools2, tool_choice: void 0, toolWarnings };
333
+ return { tools: openaiTools2, toolChoice: void 0, toolWarnings };
333
334
  }
334
335
  const type = toolChoice.type;
335
336
  switch (type) {
336
337
  case "auto":
337
338
  case "none":
338
339
  case "required":
339
- return { tools: openaiTools2, tool_choice: type, toolWarnings };
340
+ return { tools: openaiTools2, toolChoice: type, toolWarnings };
340
341
  case "tool":
341
342
  return {
342
343
  tools: openaiTools2,
343
- tool_choice: {
344
+ toolChoice: {
344
345
  type: "function",
345
346
  function: {
346
347
  name: toolChoice.toolName
@@ -351,7 +352,7 @@ function prepareTools({
351
352
  default: {
352
353
  const _exhaustiveCheck = type;
353
354
  throw new UnsupportedFunctionalityError2({
354
- functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
355
+ functionality: `tool choice type: ${_exhaustiveCheck}`
355
356
  });
356
357
  }
357
358
  }
@@ -365,26 +366,17 @@ var OpenAIChatLanguageModel = class {
365
366
  this.settings = settings;
366
367
  this.config = config;
367
368
  }
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
369
  get provider() {
379
370
  return this.config.provider;
380
371
  }
381
- get supportsImageUrls() {
382
- return !this.settings.downloadImages;
372
+ async getSupportedUrls() {
373
+ return {
374
+ "image/*": [/^https?:\/\/.*$/]
375
+ };
383
376
  }
384
377
  getArgs({
385
- mode,
386
378
  prompt,
387
- maxTokens,
379
+ maxOutputTokens,
388
380
  temperature,
389
381
  topP,
390
382
  topK,
@@ -393,39 +385,33 @@ var OpenAIChatLanguageModel = class {
393
385
  stopSequences,
394
386
  responseFormat,
395
387
  seed,
396
- providerMetadata
388
+ tools,
389
+ toolChoice,
390
+ providerOptions
397
391
  }) {
398
- var _a, _b, _c, _d, _e, _f, _g, _h;
399
- const type = mode.type;
392
+ var _a, _b, _c;
400
393
  const warnings = [];
394
+ const openaiOptions = (_a = parseProviderOptions({
395
+ provider: "openai",
396
+ providerOptions,
397
+ schema: openaiProviderOptions
398
+ })) != null ? _a : {};
401
399
  if (topK != null) {
402
400
  warnings.push({
403
401
  type: "unsupported-setting",
404
402
  setting: "topK"
405
403
  });
406
404
  }
407
- if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !this.supportsStructuredOutputs) {
405
+ if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !this.settings.structuredOutputs) {
408
406
  warnings.push({
409
407
  type: "unsupported-setting",
410
408
  setting: "responseFormat",
411
409
  details: "JSON response format schema is only supported with structuredOutputs"
412
410
  });
413
411
  }
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
412
  const { messages, warnings: messageWarnings } = convertToOpenAIChatMessages(
426
413
  {
427
414
  prompt,
428
- useLegacyFunctionCalling,
429
415
  systemMessageMode: getSystemMessageMode(this.modelId)
430
416
  }
431
417
  );
@@ -434,35 +420,38 @@ var OpenAIChatLanguageModel = class {
434
420
  // model id:
435
421
  model: this.modelId,
436
422
  // model specific settings:
437
- logit_bias: this.settings.logitBias,
438
- logprobs: this.settings.logprobs === true || typeof this.settings.logprobs === "number" ? true : void 0,
439
- top_logprobs: typeof this.settings.logprobs === "number" ? this.settings.logprobs : typeof this.settings.logprobs === "boolean" ? this.settings.logprobs ? 0 : void 0 : void 0,
440
- user: this.settings.user,
441
- parallel_tool_calls: this.settings.parallelToolCalls,
423
+ logit_bias: openaiOptions.logitBias,
424
+ logprobs: openaiOptions.logprobs === true || typeof openaiOptions.logprobs === "number" ? true : void 0,
425
+ top_logprobs: typeof openaiOptions.logprobs === "number" ? openaiOptions.logprobs : typeof openaiOptions.logprobs === "boolean" ? openaiOptions.logprobs ? 0 : void 0 : void 0,
426
+ user: openaiOptions.user,
427
+ parallel_tool_calls: openaiOptions.parallelToolCalls,
442
428
  // standardized settings:
443
- max_tokens: maxTokens,
429
+ max_tokens: maxOutputTokens,
444
430
  temperature,
445
431
  top_p: topP,
446
432
  frequency_penalty: frequencyPenalty,
447
433
  presence_penalty: presencePenalty,
448
- response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? this.supportsStructuredOutputs && responseFormat.schema != null ? {
449
- type: "json_schema",
450
- json_schema: {
451
- schema: responseFormat.schema,
452
- strict: true,
453
- name: (_a = responseFormat.name) != null ? _a : "response",
454
- description: responseFormat.description
455
- }
456
- } : { type: "json_object" } : void 0,
434
+ response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? (
435
+ // TODO convert into provider option
436
+ this.settings.structuredOutputs && responseFormat.schema != null ? {
437
+ type: "json_schema",
438
+ json_schema: {
439
+ schema: responseFormat.schema,
440
+ strict: true,
441
+ name: (_b = responseFormat.name) != null ? _b : "response",
442
+ description: responseFormat.description
443
+ }
444
+ } : { type: "json_object" }
445
+ ) : void 0,
457
446
  stop: stopSequences,
458
447
  seed,
459
448
  // openai specific settings:
460
- // TODO remove in next major version; we auto-map maxTokens now
461
- max_completion_tokens: (_b = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _b.maxCompletionTokens,
462
- store: (_c = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _c.store,
463
- metadata: (_d = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _d.metadata,
464
- prediction: (_e = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _e.prediction,
465
- reasoning_effort: (_g = (_f = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _f.reasoningEffort) != null ? _g : this.settings.reasoningEffort,
449
+ // TODO remove in next major version; we auto-map maxOutputTokens now
450
+ max_completion_tokens: openaiOptions.maxCompletionTokens,
451
+ store: openaiOptions.store,
452
+ metadata: openaiOptions.metadata,
453
+ prediction: openaiOptions.prediction,
454
+ reasoning_effort: openaiOptions.reasoningEffort,
466
455
  // messages:
467
456
  messages
468
457
  };
@@ -526,82 +515,33 @@ var OpenAIChatLanguageModel = class {
526
515
  }
527
516
  baseArgs.max_tokens = void 0;
528
517
  }
529
- }
530
- switch (type) {
531
- case "regular": {
532
- const { tools, tool_choice, functions, function_call, toolWarnings } = prepareTools({
533
- mode,
534
- useLegacyFunctionCalling,
535
- structuredOutputs: this.supportsStructuredOutputs
518
+ } else if (this.modelId.startsWith("gpt-4o-search-preview") || this.modelId.startsWith("gpt-4o-mini-search-preview")) {
519
+ if (baseArgs.temperature != null) {
520
+ baseArgs.temperature = void 0;
521
+ warnings.push({
522
+ type: "unsupported-setting",
523
+ setting: "temperature",
524
+ details: "temperature is not supported for the search preview models and has been removed."
536
525
  });
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
526
  }
604
527
  }
528
+ const {
529
+ tools: openaiTools2,
530
+ toolChoice: openaiToolChoice,
531
+ toolWarnings
532
+ } = prepareTools({
533
+ tools,
534
+ toolChoice,
535
+ structuredOutputs: (_c = this.settings.structuredOutputs) != null ? _c : false
536
+ });
537
+ return {
538
+ args: {
539
+ ...baseArgs,
540
+ tools: openaiTools2,
541
+ tool_choice: openaiToolChoice
542
+ },
543
+ warnings: [...warnings, ...toolWarnings]
544
+ };
605
545
  }
606
546
  async doGenerate(options) {
607
547
  var _a, _b, _c, _d, _e, _f, _g, _h;
@@ -624,10 +564,23 @@ var OpenAIChatLanguageModel = class {
624
564
  abortSignal: options.abortSignal,
625
565
  fetch: this.config.fetch
626
566
  });
627
- const { messages: rawPrompt, ...rawSettings } = body;
628
567
  const choice = response.choices[0];
629
- const completionTokenDetails = (_a = response.usage) == null ? void 0 : _a.completion_tokens_details;
630
- const promptTokenDetails = (_b = response.usage) == null ? void 0 : _b.prompt_tokens_details;
568
+ const content = [];
569
+ const text = choice.message.content;
570
+ if (text != null && text.length > 0) {
571
+ content.push({ type: "text", text });
572
+ }
573
+ for (const toolCall of (_a = choice.message.tool_calls) != null ? _a : []) {
574
+ content.push({
575
+ type: "tool-call",
576
+ toolCallType: "function",
577
+ toolCallId: (_b = toolCall.id) != null ? _b : generateId(),
578
+ toolName: toolCall.function.name,
579
+ args: toolCall.function.arguments
580
+ });
581
+ }
582
+ const completionTokenDetails = (_c = response.usage) == null ? void 0 : _c.completion_tokens_details;
583
+ const promptTokenDetails = (_d = response.usage) == null ? void 0 : _d.prompt_tokens_details;
631
584
  const providerMetadata = { openai: {} };
632
585
  if ((completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens) != null) {
633
586
  providerMetadata.openai.reasoningTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens;
@@ -642,81 +595,24 @@ var OpenAIChatLanguageModel = class {
642
595
  providerMetadata.openai.cachedPromptTokens = promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens;
643
596
  }
644
597
  return {
645
- text: (_c = choice.message.content) != null ? _c : void 0,
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
- }),
598
+ content,
662
599
  finishReason: mapOpenAIFinishReason(choice.finish_reason),
663
600
  usage: {
664
- promptTokens: (_f = (_e = response.usage) == null ? void 0 : _e.prompt_tokens) != null ? _f : NaN,
665
- completionTokens: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : NaN
601
+ inputTokens: (_f = (_e = response.usage) == null ? void 0 : _e.prompt_tokens) != null ? _f : void 0,
602
+ outputTokens: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : void 0
603
+ },
604
+ request: { body },
605
+ response: {
606
+ ...getResponseMetadata(response),
607
+ headers: responseHeaders,
608
+ body: rawResponse
666
609
  },
667
- rawCall: { rawPrompt, rawSettings },
668
- rawResponse: { headers: responseHeaders, body: rawResponse },
669
- request: { body: JSON.stringify(body) },
670
- response: getResponseMetadata(response),
671
610
  warnings,
672
611
  logprobs: mapOpenAIChatLogProbsOutput(choice.logprobs),
673
612
  providerMetadata
674
613
  };
675
614
  }
676
615
  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
616
  const { args, warnings } = this.getArgs(options);
721
617
  const body = {
722
618
  ...args,
@@ -741,17 +637,19 @@ var OpenAIChatLanguageModel = class {
741
637
  const { messages: rawPrompt, ...rawSettings } = args;
742
638
  const toolCalls = [];
743
639
  let finishReason = "unknown";
744
- let usage = {
745
- promptTokens: void 0,
746
- completionTokens: void 0
640
+ const usage = {
641
+ inputTokens: void 0,
642
+ outputTokens: void 0
747
643
  };
748
644
  let logprobs;
749
645
  let isFirstChunk = true;
750
- const { useLegacyFunctionCalling } = this.settings;
751
646
  const providerMetadata = { openai: {} };
752
647
  return {
753
648
  stream: response.pipeThrough(
754
649
  new TransformStream({
650
+ start(controller) {
651
+ controller.enqueue({ type: "stream-start", warnings });
652
+ },
755
653
  transform(chunk, controller) {
756
654
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
757
655
  if (!chunk.success) {
@@ -779,10 +677,8 @@ var OpenAIChatLanguageModel = class {
779
677
  prompt_tokens_details,
780
678
  completion_tokens_details
781
679
  } = value.usage;
782
- usage = {
783
- promptTokens: prompt_tokens != null ? prompt_tokens : void 0,
784
- completionTokens: completion_tokens != null ? completion_tokens : void 0
785
- };
680
+ usage.inputTokens = prompt_tokens != null ? prompt_tokens : void 0;
681
+ usage.outputTokens = completion_tokens != null ? completion_tokens : void 0;
786
682
  if ((completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens) != null) {
787
683
  providerMetadata.openai.reasoningTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens;
788
684
  }
@@ -806,8 +702,8 @@ var OpenAIChatLanguageModel = class {
806
702
  const delta = choice.delta;
807
703
  if (delta.content != null) {
808
704
  controller.enqueue({
809
- type: "text-delta",
810
- textDelta: delta.content
705
+ type: "text",
706
+ text: delta.content
811
707
  });
812
708
  }
813
709
  const mappedLogprobs = mapOpenAIChatLogProbsOutput(
@@ -817,16 +713,8 @@ var OpenAIChatLanguageModel = class {
817
713
  if (logprobs === void 0) logprobs = [];
818
714
  logprobs.push(...mappedLogprobs);
819
715
  }
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) {
716
+ if (delta.tool_calls != null) {
717
+ for (const toolCallDelta of delta.tool_calls) {
830
718
  const index = toolCallDelta.index;
831
719
  if (toolCalls[index] == null) {
832
720
  if (toolCallDelta.type !== "function") {
@@ -908,125 +796,111 @@ var OpenAIChatLanguageModel = class {
908
796
  }
909
797
  },
910
798
  flush(controller) {
911
- var _a, _b;
912
799
  controller.enqueue({
913
800
  type: "finish",
914
801
  finishReason,
915
802
  logprobs,
916
- usage: {
917
- promptTokens: (_a = usage.promptTokens) != null ? _a : NaN,
918
- completionTokens: (_b = usage.completionTokens) != null ? _b : NaN
919
- },
803
+ usage,
920
804
  ...providerMetadata != null ? { providerMetadata } : {}
921
805
  });
922
806
  }
923
807
  })
924
808
  ),
925
- rawCall: { rawPrompt, rawSettings },
926
- rawResponse: { headers: responseHeaders },
927
- request: { body: JSON.stringify(body) },
928
- warnings
809
+ request: { body },
810
+ response: { headers: responseHeaders }
929
811
  };
930
812
  }
931
813
  };
932
- var openaiTokenUsageSchema = z2.object({
933
- prompt_tokens: z2.number().nullish(),
934
- completion_tokens: z2.number().nullish(),
935
- prompt_tokens_details: z2.object({
936
- cached_tokens: z2.number().nullish()
814
+ var openaiTokenUsageSchema = z3.object({
815
+ prompt_tokens: z3.number().nullish(),
816
+ completion_tokens: z3.number().nullish(),
817
+ prompt_tokens_details: z3.object({
818
+ cached_tokens: z3.number().nullish()
937
819
  }).nullish(),
938
- completion_tokens_details: z2.object({
939
- reasoning_tokens: z2.number().nullish(),
940
- accepted_prediction_tokens: z2.number().nullish(),
941
- rejected_prediction_tokens: z2.number().nullish()
820
+ completion_tokens_details: z3.object({
821
+ reasoning_tokens: z3.number().nullish(),
822
+ accepted_prediction_tokens: z3.number().nullish(),
823
+ rejected_prediction_tokens: z3.number().nullish()
942
824
  }).nullish()
943
825
  }).nullish();
944
- var openaiChatResponseSchema = z2.object({
945
- id: z2.string().nullish(),
946
- created: z2.number().nullish(),
947
- model: z2.string().nullish(),
948
- choices: z2.array(
949
- z2.object({
950
- message: z2.object({
951
- role: z2.literal("assistant").nullish(),
952
- content: z2.string().nullish(),
953
- function_call: z2.object({
954
- arguments: z2.string(),
955
- name: z2.string()
956
- }).nullish(),
957
- tool_calls: z2.array(
958
- z2.object({
959
- id: z2.string().nullish(),
960
- type: z2.literal("function"),
961
- function: z2.object({
962
- name: z2.string(),
963
- arguments: z2.string()
826
+ var openaiChatResponseSchema = z3.object({
827
+ id: z3.string().nullish(),
828
+ created: z3.number().nullish(),
829
+ model: z3.string().nullish(),
830
+ choices: z3.array(
831
+ z3.object({
832
+ message: z3.object({
833
+ role: z3.literal("assistant").nullish(),
834
+ content: z3.string().nullish(),
835
+ tool_calls: z3.array(
836
+ z3.object({
837
+ id: z3.string().nullish(),
838
+ type: z3.literal("function"),
839
+ function: z3.object({
840
+ name: z3.string(),
841
+ arguments: z3.string()
964
842
  })
965
843
  })
966
844
  ).nullish()
967
845
  }),
968
- index: z2.number(),
969
- logprobs: z2.object({
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()
846
+ index: z3.number(),
847
+ logprobs: z3.object({
848
+ content: z3.array(
849
+ z3.object({
850
+ token: z3.string(),
851
+ logprob: z3.number(),
852
+ top_logprobs: z3.array(
853
+ z3.object({
854
+ token: z3.string(),
855
+ logprob: z3.number()
978
856
  })
979
857
  )
980
858
  })
981
859
  ).nullable()
982
860
  }).nullish(),
983
- finish_reason: z2.string().nullish()
861
+ finish_reason: z3.string().nullish()
984
862
  })
985
863
  ),
986
864
  usage: openaiTokenUsageSchema
987
865
  });
988
- var openaiChatChunkSchema = z2.union([
989
- z2.object({
990
- id: z2.string().nullish(),
991
- created: z2.number().nullish(),
992
- model: z2.string().nullish(),
993
- choices: z2.array(
994
- z2.object({
995
- delta: z2.object({
996
- role: z2.enum(["assistant"]).nullish(),
997
- content: z2.string().nullish(),
998
- function_call: z2.object({
999
- name: z2.string().optional(),
1000
- arguments: z2.string().optional()
1001
- }).nullish(),
1002
- tool_calls: z2.array(
1003
- z2.object({
1004
- index: z2.number(),
1005
- id: z2.string().nullish(),
1006
- type: z2.literal("function").optional(),
1007
- function: z2.object({
1008
- name: z2.string().nullish(),
1009
- arguments: z2.string().nullish()
866
+ var openaiChatChunkSchema = z3.union([
867
+ z3.object({
868
+ id: z3.string().nullish(),
869
+ created: z3.number().nullish(),
870
+ model: z3.string().nullish(),
871
+ choices: z3.array(
872
+ z3.object({
873
+ delta: z3.object({
874
+ role: z3.enum(["assistant"]).nullish(),
875
+ content: z3.string().nullish(),
876
+ tool_calls: z3.array(
877
+ z3.object({
878
+ index: z3.number(),
879
+ id: z3.string().nullish(),
880
+ type: z3.literal("function").optional(),
881
+ function: z3.object({
882
+ name: z3.string().nullish(),
883
+ arguments: z3.string().nullish()
1010
884
  })
1011
885
  })
1012
886
  ).nullish()
1013
887
  }).nullish(),
1014
- logprobs: z2.object({
1015
- content: z2.array(
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()
888
+ logprobs: z3.object({
889
+ content: z3.array(
890
+ z3.object({
891
+ token: z3.string(),
892
+ logprob: z3.number(),
893
+ top_logprobs: z3.array(
894
+ z3.object({
895
+ token: z3.string(),
896
+ logprob: z3.number()
1023
897
  })
1024
898
  )
1025
899
  })
1026
900
  ).nullable()
1027
901
  }).nullish(),
1028
- finish_reason: z2.string().nullable().optional(),
1029
- index: z2.number()
902
+ finish_reason: z3.string().nullable().optional(),
903
+ index: z3.number()
1030
904
  })
1031
905
  ),
1032
906
  usage: openaiTokenUsageSchema
@@ -1034,10 +908,7 @@ var openaiChatChunkSchema = z2.union([
1034
908
  openaiErrorDataSchema
1035
909
  ]);
1036
910
  function isReasoningModel(modelId) {
1037
- return modelId === "o1" || modelId.startsWith("o1-") || modelId === "o3" || modelId.startsWith("o3-");
1038
- }
1039
- function isAudioModel(modelId) {
1040
- return modelId.startsWith("gpt-4o-audio-preview");
911
+ return modelId.startsWith("o");
1041
912
  }
1042
913
  function getSystemMessageMode(modelId) {
1043
914
  var _a, _b;
@@ -1068,21 +939,18 @@ var reasoningModels = {
1068
939
  };
1069
940
 
1070
941
  // src/openai-completion-language-model.ts
1071
- import {
1072
- UnsupportedFunctionalityError as UnsupportedFunctionalityError5
1073
- } from "@ai-sdk/provider";
1074
942
  import {
1075
943
  combineHeaders as combineHeaders2,
1076
944
  createEventSourceResponseHandler as createEventSourceResponseHandler2,
1077
945
  createJsonResponseHandler as createJsonResponseHandler2,
1078
946
  postJsonToApi as postJsonToApi2
1079
947
  } from "@ai-sdk/provider-utils";
1080
- import { z as z3 } from "zod";
948
+ import { z as z4 } from "zod";
1081
949
 
1082
950
  // src/convert-to-openai-completion-prompt.ts
1083
951
  import {
1084
952
  InvalidPromptError,
1085
- UnsupportedFunctionalityError as UnsupportedFunctionalityError4
953
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError3
1086
954
  } from "@ai-sdk/provider";
1087
955
  function convertToOpenAICompletionPrompt({
1088
956
  prompt,
@@ -1114,13 +982,8 @@ function convertToOpenAICompletionPrompt({
1114
982
  case "text": {
1115
983
  return part.text;
1116
984
  }
1117
- case "image": {
1118
- throw new UnsupportedFunctionalityError4({
1119
- functionality: "images"
1120
- });
1121
- }
1122
985
  }
1123
- }).join("");
986
+ }).filter(Boolean).join("");
1124
987
  text += `${user}:
1125
988
  ${userMessage}
1126
989
 
@@ -1134,7 +997,7 @@ ${userMessage}
1134
997
  return part.text;
1135
998
  }
1136
999
  case "tool-call": {
1137
- throw new UnsupportedFunctionalityError4({
1000
+ throw new UnsupportedFunctionalityError3({
1138
1001
  functionality: "tool-call messages"
1139
1002
  });
1140
1003
  }
@@ -1147,7 +1010,7 @@ ${assistantMessage}
1147
1010
  break;
1148
1011
  }
1149
1012
  case "tool": {
1150
- throw new UnsupportedFunctionalityError4({
1013
+ throw new UnsupportedFunctionalityError3({
1151
1014
  functionality: "tool messages"
1152
1015
  });
1153
1016
  }
@@ -1184,7 +1047,6 @@ function mapOpenAICompletionLogProbs(logprobs) {
1184
1047
  var OpenAICompletionLanguageModel = class {
1185
1048
  constructor(modelId, settings, config) {
1186
1049
  this.specificationVersion = "v2";
1187
- this.defaultObjectGenerationMode = void 0;
1188
1050
  this.modelId = modelId;
1189
1051
  this.settings = settings;
1190
1052
  this.config = config;
@@ -1192,11 +1054,15 @@ var OpenAICompletionLanguageModel = class {
1192
1054
  get provider() {
1193
1055
  return this.config.provider;
1194
1056
  }
1057
+ async getSupportedUrls() {
1058
+ return {
1059
+ // no supported urls for completion models
1060
+ };
1061
+ }
1195
1062
  getArgs({
1196
- mode,
1197
1063
  inputFormat,
1198
1064
  prompt,
1199
- maxTokens,
1065
+ maxOutputTokens,
1200
1066
  temperature,
1201
1067
  topP,
1202
1068
  topK,
@@ -1204,16 +1070,19 @@ var OpenAICompletionLanguageModel = class {
1204
1070
  presencePenalty,
1205
1071
  stopSequences: userStopSequences,
1206
1072
  responseFormat,
1073
+ tools,
1074
+ toolChoice,
1207
1075
  seed
1208
1076
  }) {
1209
- var _a;
1210
- const type = mode.type;
1211
1077
  const warnings = [];
1212
1078
  if (topK != null) {
1213
- warnings.push({
1214
- type: "unsupported-setting",
1215
- setting: "topK"
1216
- });
1079
+ warnings.push({ type: "unsupported-setting", setting: "topK" });
1080
+ }
1081
+ if (tools == null ? void 0 : tools.length) {
1082
+ warnings.push({ type: "unsupported-setting", setting: "tools" });
1083
+ }
1084
+ if (toolChoice != null) {
1085
+ warnings.push({ type: "unsupported-setting", setting: "toolChoice" });
1217
1086
  }
1218
1087
  if (responseFormat != null && responseFormat.type !== "text") {
1219
1088
  warnings.push({
@@ -1224,56 +1093,30 @@ var OpenAICompletionLanguageModel = class {
1224
1093
  }
1225
1094
  const { prompt: completionPrompt, stopSequences } = convertToOpenAICompletionPrompt({ prompt, inputFormat });
1226
1095
  const stop = [...stopSequences != null ? stopSequences : [], ...userStopSequences != null ? userStopSequences : []];
1227
- const baseArgs = {
1228
- // model id:
1229
- model: this.modelId,
1230
- // model specific settings:
1231
- echo: this.settings.echo,
1232
- logit_bias: this.settings.logitBias,
1233
- logprobs: typeof this.settings.logprobs === "number" ? this.settings.logprobs : typeof this.settings.logprobs === "boolean" ? this.settings.logprobs ? 0 : void 0 : void 0,
1234
- suffix: this.settings.suffix,
1235
- user: this.settings.user,
1236
- // standardized settings:
1237
- max_tokens: maxTokens,
1238
- temperature,
1239
- top_p: topP,
1240
- frequency_penalty: frequencyPenalty,
1241
- presence_penalty: presencePenalty,
1242
- seed,
1243
- // prompt:
1244
- prompt: completionPrompt,
1245
- // stop sequences:
1246
- stop: stop.length > 0 ? stop : void 0
1096
+ return {
1097
+ args: {
1098
+ // model id:
1099
+ model: this.modelId,
1100
+ // model specific settings:
1101
+ echo: this.settings.echo,
1102
+ logit_bias: this.settings.logitBias,
1103
+ logprobs: typeof this.settings.logprobs === "number" ? this.settings.logprobs : typeof this.settings.logprobs === "boolean" ? this.settings.logprobs ? 0 : void 0 : void 0,
1104
+ suffix: this.settings.suffix,
1105
+ user: this.settings.user,
1106
+ // standardized settings:
1107
+ max_tokens: maxOutputTokens,
1108
+ temperature,
1109
+ top_p: topP,
1110
+ frequency_penalty: frequencyPenalty,
1111
+ presence_penalty: presencePenalty,
1112
+ seed,
1113
+ // prompt:
1114
+ prompt: completionPrompt,
1115
+ // stop sequences:
1116
+ stop: stop.length > 0 ? stop : void 0
1117
+ },
1118
+ warnings
1247
1119
  };
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
1120
  }
1278
1121
  async doGenerate(options) {
1279
1122
  const { args, warnings } = this.getArgs(options);
@@ -1295,21 +1138,22 @@ var OpenAICompletionLanguageModel = class {
1295
1138
  abortSignal: options.abortSignal,
1296
1139
  fetch: this.config.fetch
1297
1140
  });
1298
- const { prompt: rawPrompt, ...rawSettings } = args;
1299
1141
  const choice = response.choices[0];
1300
1142
  return {
1301
- text: choice.text,
1143
+ content: [{ type: "text", text: choice.text }],
1302
1144
  usage: {
1303
- promptTokens: response.usage.prompt_tokens,
1304
- completionTokens: response.usage.completion_tokens
1145
+ inputTokens: response.usage.prompt_tokens,
1146
+ outputTokens: response.usage.completion_tokens
1305
1147
  },
1306
1148
  finishReason: mapOpenAIFinishReason(choice.finish_reason),
1307
1149
  logprobs: mapOpenAICompletionLogProbs(choice.logprobs),
1308
- rawCall: { rawPrompt, rawSettings },
1309
- rawResponse: { headers: responseHeaders, body: rawResponse },
1310
- response: getResponseMetadata(response),
1311
- warnings,
1312
- request: { body: JSON.stringify(args) }
1150
+ request: { body: args },
1151
+ response: {
1152
+ ...getResponseMetadata(response),
1153
+ headers: responseHeaders,
1154
+ body: rawResponse
1155
+ },
1156
+ warnings
1313
1157
  };
1314
1158
  }
1315
1159
  async doStream(options) {
@@ -1334,17 +1178,19 @@ var OpenAICompletionLanguageModel = class {
1334
1178
  abortSignal: options.abortSignal,
1335
1179
  fetch: this.config.fetch
1336
1180
  });
1337
- const { prompt: rawPrompt, ...rawSettings } = args;
1338
1181
  let finishReason = "unknown";
1339
- let usage = {
1340
- promptTokens: Number.NaN,
1341
- completionTokens: Number.NaN
1182
+ const usage = {
1183
+ inputTokens: void 0,
1184
+ outputTokens: void 0
1342
1185
  };
1343
1186
  let logprobs;
1344
1187
  let isFirstChunk = true;
1345
1188
  return {
1346
1189
  stream: response.pipeThrough(
1347
1190
  new TransformStream({
1191
+ start(controller) {
1192
+ controller.enqueue({ type: "stream-start", warnings });
1193
+ },
1348
1194
  transform(chunk, controller) {
1349
1195
  if (!chunk.success) {
1350
1196
  finishReason = "error";
@@ -1365,10 +1211,8 @@ var OpenAICompletionLanguageModel = class {
1365
1211
  });
1366
1212
  }
1367
1213
  if (value.usage != null) {
1368
- usage = {
1369
- promptTokens: value.usage.prompt_tokens,
1370
- completionTokens: value.usage.completion_tokens
1371
- };
1214
+ usage.inputTokens = value.usage.prompt_tokens;
1215
+ usage.outputTokens = value.usage.completion_tokens;
1372
1216
  }
1373
1217
  const choice = value.choices[0];
1374
1218
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
@@ -1376,8 +1220,8 @@ var OpenAICompletionLanguageModel = class {
1376
1220
  }
1377
1221
  if ((choice == null ? void 0 : choice.text) != null) {
1378
1222
  controller.enqueue({
1379
- type: "text-delta",
1380
- textDelta: choice.text
1223
+ type: "text",
1224
+ text: choice.text
1381
1225
  });
1382
1226
  }
1383
1227
  const mappedLogprobs = mapOpenAICompletionLogProbs(
@@ -1398,53 +1242,51 @@ var OpenAICompletionLanguageModel = class {
1398
1242
  }
1399
1243
  })
1400
1244
  ),
1401
- rawCall: { rawPrompt, rawSettings },
1402
- rawResponse: { headers: responseHeaders },
1403
- warnings,
1404
- request: { body: JSON.stringify(body) }
1245
+ request: { body },
1246
+ response: { headers: responseHeaders }
1405
1247
  };
1406
1248
  }
1407
1249
  };
1408
- var openaiCompletionResponseSchema = z3.object({
1409
- id: z3.string().nullish(),
1410
- created: z3.number().nullish(),
1411
- model: z3.string().nullish(),
1412
- choices: z3.array(
1413
- z3.object({
1414
- text: z3.string(),
1415
- finish_reason: z3.string(),
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()
1250
+ var openaiCompletionResponseSchema = z4.object({
1251
+ id: z4.string().nullish(),
1252
+ created: z4.number().nullish(),
1253
+ model: z4.string().nullish(),
1254
+ choices: z4.array(
1255
+ z4.object({
1256
+ text: z4.string(),
1257
+ finish_reason: z4.string(),
1258
+ logprobs: z4.object({
1259
+ tokens: z4.array(z4.string()),
1260
+ token_logprobs: z4.array(z4.number()),
1261
+ top_logprobs: z4.array(z4.record(z4.string(), z4.number())).nullable()
1420
1262
  }).nullish()
1421
1263
  })
1422
1264
  ),
1423
- usage: z3.object({
1424
- prompt_tokens: z3.number(),
1425
- completion_tokens: z3.number()
1265
+ usage: z4.object({
1266
+ prompt_tokens: z4.number(),
1267
+ completion_tokens: z4.number()
1426
1268
  })
1427
1269
  });
1428
- var openaiCompletionChunkSchema = z3.union([
1429
- z3.object({
1430
- id: z3.string().nullish(),
1431
- created: z3.number().nullish(),
1432
- model: z3.string().nullish(),
1433
- choices: z3.array(
1434
- z3.object({
1435
- text: z3.string(),
1436
- finish_reason: z3.string().nullish(),
1437
- index: z3.number(),
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()
1270
+ var openaiCompletionChunkSchema = z4.union([
1271
+ z4.object({
1272
+ id: z4.string().nullish(),
1273
+ created: z4.number().nullish(),
1274
+ model: z4.string().nullish(),
1275
+ choices: z4.array(
1276
+ z4.object({
1277
+ text: z4.string(),
1278
+ finish_reason: z4.string().nullish(),
1279
+ index: z4.number(),
1280
+ logprobs: z4.object({
1281
+ tokens: z4.array(z4.string()),
1282
+ token_logprobs: z4.array(z4.number()),
1283
+ top_logprobs: z4.array(z4.record(z4.string(), z4.number())).nullable()
1442
1284
  }).nullish()
1443
1285
  })
1444
1286
  ),
1445
- usage: z3.object({
1446
- prompt_tokens: z3.number(),
1447
- completion_tokens: z3.number()
1287
+ usage: z4.object({
1288
+ prompt_tokens: z4.number(),
1289
+ completion_tokens: z4.number()
1448
1290
  }).nullish()
1449
1291
  }),
1450
1292
  openaiErrorDataSchema
@@ -1457,12 +1299,30 @@ import {
1457
1299
  import {
1458
1300
  combineHeaders as combineHeaders3,
1459
1301
  createJsonResponseHandler as createJsonResponseHandler3,
1302
+ parseProviderOptions as parseProviderOptions2,
1460
1303
  postJsonToApi as postJsonToApi3
1461
1304
  } from "@ai-sdk/provider-utils";
1462
- import { z as z4 } from "zod";
1305
+ import { z as z6 } from "zod";
1306
+
1307
+ // src/openai-embedding-options.ts
1308
+ import { z as z5 } from "zod";
1309
+ var openaiEmbeddingProviderOptions = z5.object({
1310
+ /**
1311
+ The number of dimensions the resulting output embeddings should have.
1312
+ Only supported in text-embedding-3 and later models.
1313
+ */
1314
+ dimensions: z5.number().optional(),
1315
+ /**
1316
+ A unique identifier representing your end-user, which can help OpenAI to
1317
+ monitor and detect abuse. Learn more.
1318
+ */
1319
+ user: z5.string().optional()
1320
+ });
1321
+
1322
+ // src/openai-embedding-model.ts
1463
1323
  var OpenAIEmbeddingModel = class {
1464
1324
  constructor(modelId, settings, config) {
1465
- this.specificationVersion = "v1";
1325
+ this.specificationVersion = "v2";
1466
1326
  this.modelId = modelId;
1467
1327
  this.settings = settings;
1468
1328
  this.config = config;
@@ -1481,8 +1341,10 @@ var OpenAIEmbeddingModel = class {
1481
1341
  async doEmbed({
1482
1342
  values,
1483
1343
  headers,
1484
- abortSignal
1344
+ abortSignal,
1345
+ providerOptions
1485
1346
  }) {
1347
+ var _a;
1486
1348
  if (values.length > this.maxEmbeddingsPerCall) {
1487
1349
  throw new TooManyEmbeddingValuesForCallError({
1488
1350
  provider: this.provider,
@@ -1491,7 +1353,16 @@ var OpenAIEmbeddingModel = class {
1491
1353
  values
1492
1354
  });
1493
1355
  }
1494
- const { responseHeaders, value: response } = await postJsonToApi3({
1356
+ const openaiOptions = (_a = parseProviderOptions2({
1357
+ provider: "openai",
1358
+ providerOptions,
1359
+ schema: openaiEmbeddingProviderOptions
1360
+ })) != null ? _a : {};
1361
+ const {
1362
+ responseHeaders,
1363
+ value: response,
1364
+ rawValue
1365
+ } = await postJsonToApi3({
1495
1366
  url: this.config.url({
1496
1367
  path: "/embeddings",
1497
1368
  modelId: this.modelId
@@ -1501,8 +1372,8 @@ var OpenAIEmbeddingModel = class {
1501
1372
  model: this.modelId,
1502
1373
  input: values,
1503
1374
  encoding_format: "float",
1504
- dimensions: this.settings.dimensions,
1505
- user: this.settings.user
1375
+ dimensions: openaiOptions.dimensions,
1376
+ user: openaiOptions.user
1506
1377
  },
1507
1378
  failedResponseHandler: openaiFailedResponseHandler,
1508
1379
  successfulResponseHandler: createJsonResponseHandler3(
@@ -1514,13 +1385,13 @@ var OpenAIEmbeddingModel = class {
1514
1385
  return {
1515
1386
  embeddings: response.data.map((item) => item.embedding),
1516
1387
  usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
1517
- rawResponse: { headers: responseHeaders }
1388
+ response: { headers: responseHeaders, body: rawValue }
1518
1389
  };
1519
1390
  }
1520
1391
  };
1521
- var openaiTextEmbeddingResponseSchema = z4.object({
1522
- data: z4.array(z4.object({ embedding: z4.array(z4.number()) })),
1523
- usage: z4.object({ prompt_tokens: z4.number() }).nullish()
1392
+ var openaiTextEmbeddingResponseSchema = z6.object({
1393
+ data: z6.array(z6.object({ embedding: z6.array(z6.number()) })),
1394
+ usage: z6.object({ prompt_tokens: z6.number() }).nullish()
1524
1395
  });
1525
1396
 
1526
1397
  // src/openai-image-model.ts
@@ -1529,7 +1400,7 @@ import {
1529
1400
  createJsonResponseHandler as createJsonResponseHandler4,
1530
1401
  postJsonToApi as postJsonToApi4
1531
1402
  } from "@ai-sdk/provider-utils";
1532
- import { z as z5 } from "zod";
1403
+ import { z as z7 } from "zod";
1533
1404
 
1534
1405
  // src/openai-image-settings.ts
1535
1406
  var modelMaxImagesPerCall = {
@@ -1607,13 +1478,13 @@ var OpenAIImageModel = class {
1607
1478
  };
1608
1479
  }
1609
1480
  };
1610
- var openaiImageResponseSchema = z5.object({
1611
- data: z5.array(z5.object({ b64_json: z5.string() }))
1481
+ var openaiImageResponseSchema = z7.object({
1482
+ data: z7.array(z7.object({ b64_json: z7.string() }))
1612
1483
  });
1613
1484
 
1614
1485
  // src/openai-tools.ts
1615
- import { z as z6 } from "zod";
1616
- var WebSearchPreviewParameters = z6.object({});
1486
+ import { z as z8 } from "zod";
1487
+ var WebSearchPreviewParameters = z8.object({});
1617
1488
  function webSearchPreviewTool({
1618
1489
  searchContextSize,
1619
1490
  userLocation
@@ -1632,22 +1503,196 @@ var openaiTools = {
1632
1503
  webSearchPreview: webSearchPreviewTool
1633
1504
  };
1634
1505
 
1635
- // src/responses/openai-responses-language-model.ts
1506
+ // src/openai-transcription-model.ts
1636
1507
  import {
1637
1508
  combineHeaders as combineHeaders5,
1638
- createEventSourceResponseHandler as createEventSourceResponseHandler3,
1509
+ convertBase64ToUint8Array,
1639
1510
  createJsonResponseHandler as createJsonResponseHandler5,
1511
+ parseProviderOptions as parseProviderOptions3,
1512
+ postFormDataToApi
1513
+ } from "@ai-sdk/provider-utils";
1514
+ import { z as z9 } from "zod";
1515
+ var openAIProviderOptionsSchema = z9.object({
1516
+ include: z9.array(z9.string()).nullish(),
1517
+ language: z9.string().nullish(),
1518
+ prompt: z9.string().nullish(),
1519
+ temperature: z9.number().min(0).max(1).nullish().default(0),
1520
+ timestampGranularities: z9.array(z9.enum(["word", "segment"])).nullish().default(["segment"])
1521
+ });
1522
+ var languageMap = {
1523
+ afrikaans: "af",
1524
+ arabic: "ar",
1525
+ armenian: "hy",
1526
+ azerbaijani: "az",
1527
+ belarusian: "be",
1528
+ bosnian: "bs",
1529
+ bulgarian: "bg",
1530
+ catalan: "ca",
1531
+ chinese: "zh",
1532
+ croatian: "hr",
1533
+ czech: "cs",
1534
+ danish: "da",
1535
+ dutch: "nl",
1536
+ english: "en",
1537
+ estonian: "et",
1538
+ finnish: "fi",
1539
+ french: "fr",
1540
+ galician: "gl",
1541
+ german: "de",
1542
+ greek: "el",
1543
+ hebrew: "he",
1544
+ hindi: "hi",
1545
+ hungarian: "hu",
1546
+ icelandic: "is",
1547
+ indonesian: "id",
1548
+ italian: "it",
1549
+ japanese: "ja",
1550
+ kannada: "kn",
1551
+ kazakh: "kk",
1552
+ korean: "ko",
1553
+ latvian: "lv",
1554
+ lithuanian: "lt",
1555
+ macedonian: "mk",
1556
+ malay: "ms",
1557
+ marathi: "mr",
1558
+ maori: "mi",
1559
+ nepali: "ne",
1560
+ norwegian: "no",
1561
+ persian: "fa",
1562
+ polish: "pl",
1563
+ portuguese: "pt",
1564
+ romanian: "ro",
1565
+ russian: "ru",
1566
+ serbian: "sr",
1567
+ slovak: "sk",
1568
+ slovenian: "sl",
1569
+ spanish: "es",
1570
+ swahili: "sw",
1571
+ swedish: "sv",
1572
+ tagalog: "tl",
1573
+ tamil: "ta",
1574
+ thai: "th",
1575
+ turkish: "tr",
1576
+ ukrainian: "uk",
1577
+ urdu: "ur",
1578
+ vietnamese: "vi",
1579
+ welsh: "cy"
1580
+ };
1581
+ var OpenAITranscriptionModel = class {
1582
+ constructor(modelId, config) {
1583
+ this.modelId = modelId;
1584
+ this.config = config;
1585
+ this.specificationVersion = "v1";
1586
+ }
1587
+ get provider() {
1588
+ return this.config.provider;
1589
+ }
1590
+ getArgs({
1591
+ audio,
1592
+ mediaType,
1593
+ providerOptions
1594
+ }) {
1595
+ var _a, _b, _c, _d, _e;
1596
+ const warnings = [];
1597
+ const openAIOptions = parseProviderOptions3({
1598
+ provider: "openai",
1599
+ providerOptions,
1600
+ schema: openAIProviderOptionsSchema
1601
+ });
1602
+ const formData = new FormData();
1603
+ const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([convertBase64ToUint8Array(audio)]);
1604
+ formData.append("model", this.modelId);
1605
+ formData.append("file", new File([blob], "audio", { type: mediaType }));
1606
+ if (openAIOptions) {
1607
+ const transcriptionModelOptions = {
1608
+ include: (_a = openAIOptions.include) != null ? _a : void 0,
1609
+ language: (_b = openAIOptions.language) != null ? _b : void 0,
1610
+ prompt: (_c = openAIOptions.prompt) != null ? _c : void 0,
1611
+ temperature: (_d = openAIOptions.temperature) != null ? _d : void 0,
1612
+ timestamp_granularities: (_e = openAIOptions.timestampGranularities) != null ? _e : void 0
1613
+ };
1614
+ for (const key in transcriptionModelOptions) {
1615
+ const value = transcriptionModelOptions[key];
1616
+ if (value !== void 0) {
1617
+ formData.append(key, String(value));
1618
+ }
1619
+ }
1620
+ }
1621
+ return {
1622
+ formData,
1623
+ warnings
1624
+ };
1625
+ }
1626
+ async doGenerate(options) {
1627
+ var _a, _b, _c, _d, _e, _f;
1628
+ const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
1629
+ const { formData, warnings } = this.getArgs(options);
1630
+ const {
1631
+ value: response,
1632
+ responseHeaders,
1633
+ rawValue: rawResponse
1634
+ } = await postFormDataToApi({
1635
+ url: this.config.url({
1636
+ path: "/audio/transcriptions",
1637
+ modelId: this.modelId
1638
+ }),
1639
+ headers: combineHeaders5(this.config.headers(), options.headers),
1640
+ formData,
1641
+ failedResponseHandler: openaiFailedResponseHandler,
1642
+ successfulResponseHandler: createJsonResponseHandler5(
1643
+ openaiTranscriptionResponseSchema
1644
+ ),
1645
+ abortSignal: options.abortSignal,
1646
+ fetch: this.config.fetch
1647
+ });
1648
+ const language = response.language != null && response.language in languageMap ? languageMap[response.language] : void 0;
1649
+ return {
1650
+ text: response.text,
1651
+ segments: (_e = (_d = response.words) == null ? void 0 : _d.map((word) => ({
1652
+ text: word.word,
1653
+ startSecond: word.start,
1654
+ endSecond: word.end
1655
+ }))) != null ? _e : [],
1656
+ language,
1657
+ durationInSeconds: (_f = response.duration) != null ? _f : void 0,
1658
+ warnings,
1659
+ response: {
1660
+ timestamp: currentDate,
1661
+ modelId: this.modelId,
1662
+ headers: responseHeaders,
1663
+ body: rawResponse
1664
+ }
1665
+ };
1666
+ }
1667
+ };
1668
+ var openaiTranscriptionResponseSchema = z9.object({
1669
+ text: z9.string(),
1670
+ language: z9.string().nullish(),
1671
+ duration: z9.number().nullish(),
1672
+ words: z9.array(
1673
+ z9.object({
1674
+ word: z9.string(),
1675
+ start: z9.number(),
1676
+ end: z9.number()
1677
+ })
1678
+ ).nullish()
1679
+ });
1680
+
1681
+ // src/responses/openai-responses-language-model.ts
1682
+ import {
1683
+ combineHeaders as combineHeaders6,
1684
+ createEventSourceResponseHandler as createEventSourceResponseHandler3,
1685
+ createJsonResponseHandler as createJsonResponseHandler6,
1640
1686
  generateId as generateId2,
1641
- parseProviderOptions,
1687
+ parseProviderOptions as parseProviderOptions4,
1642
1688
  postJsonToApi as postJsonToApi5
1643
1689
  } from "@ai-sdk/provider-utils";
1644
- import { z as z7 } from "zod";
1690
+ import { z as z10 } from "zod";
1645
1691
 
1646
1692
  // src/responses/convert-to-openai-responses-messages.ts
1647
1693
  import {
1648
- UnsupportedFunctionalityError as UnsupportedFunctionalityError6
1694
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError4
1649
1695
  } from "@ai-sdk/provider";
1650
- import { convertUint8ArrayToBase64 as convertUint8ArrayToBase642 } from "@ai-sdk/provider-utils";
1651
1696
  function convertToOpenAIResponsesMessages({
1652
1697
  prompt,
1653
1698
  systemMessageMode
@@ -1686,38 +1731,35 @@ function convertToOpenAIResponsesMessages({
1686
1731
  messages.push({
1687
1732
  role: "user",
1688
1733
  content: content.map((part, index) => {
1689
- var _a, _b, _c, _d;
1734
+ var _a, _b, _c;
1690
1735
  switch (part.type) {
1691
1736
  case "text": {
1692
1737
  return { type: "input_text", text: part.text };
1693
1738
  }
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
1739
  case "file": {
1703
- if (part.data instanceof URL) {
1704
- throw new UnsupportedFunctionalityError6({
1705
- functionality: "File URLs in user messages"
1706
- });
1707
- }
1708
- switch (part.mimeType) {
1709
- case "application/pdf": {
1710
- return {
1711
- type: "input_file",
1712
- filename: (_d = part.filename) != null ? _d : `part-${index}.pdf`,
1713
- file_data: `data:application/pdf;base64,${part.data}`
1714
- };
1715
- }
1716
- default: {
1717
- throw new UnsupportedFunctionalityError6({
1718
- functionality: "Only PDF files are supported in user messages"
1740
+ if (part.mediaType.startsWith("image/")) {
1741
+ const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
1742
+ return {
1743
+ type: "input_image",
1744
+ image_url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${part.data}`,
1745
+ // OpenAI specific extension: image detail
1746
+ detail: (_b = (_a = part.providerOptions) == null ? void 0 : _a.openai) == null ? void 0 : _b.imageDetail
1747
+ };
1748
+ } else if (part.mediaType === "application/pdf") {
1749
+ if (part.data instanceof URL) {
1750
+ throw new UnsupportedFunctionalityError4({
1751
+ functionality: "PDF file parts with URLs"
1719
1752
  });
1720
1753
  }
1754
+ return {
1755
+ type: "input_file",
1756
+ filename: (_c = part.filename) != null ? _c : `part-${index}.pdf`,
1757
+ file_data: `data:application/pdf;base64,${part.data}`
1758
+ };
1759
+ } else {
1760
+ throw new UnsupportedFunctionalityError4({
1761
+ functionality: `file part media type ${part.mediaType}`
1762
+ });
1721
1763
  }
1722
1764
  }
1723
1765
  }
@@ -1787,19 +1829,18 @@ function mapOpenAIResponseFinishReason({
1787
1829
 
1788
1830
  // src/responses/openai-responses-prepare-tools.ts
1789
1831
  import {
1790
- UnsupportedFunctionalityError as UnsupportedFunctionalityError7
1832
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError5
1791
1833
  } from "@ai-sdk/provider";
1792
1834
  function prepareResponsesTools({
1793
- mode,
1835
+ tools,
1836
+ toolChoice,
1794
1837
  strict
1795
1838
  }) {
1796
- var _a;
1797
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
1839
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
1798
1840
  const toolWarnings = [];
1799
1841
  if (tools == null) {
1800
- return { tools: void 0, tool_choice: void 0, toolWarnings };
1842
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
1801
1843
  }
1802
- const toolChoice = mode.toolChoice;
1803
1844
  const openaiTools2 = [];
1804
1845
  for (const tool of tools) {
1805
1846
  switch (tool.type) {
@@ -1832,37 +1873,24 @@ function prepareResponsesTools({
1832
1873
  }
1833
1874
  }
1834
1875
  if (toolChoice == null) {
1835
- return { tools: openaiTools2, tool_choice: void 0, toolWarnings };
1876
+ return { tools: openaiTools2, toolChoice: void 0, toolWarnings };
1836
1877
  }
1837
1878
  const type = toolChoice.type;
1838
1879
  switch (type) {
1839
1880
  case "auto":
1840
1881
  case "none":
1841
1882
  case "required":
1842
- return { tools: openaiTools2, tool_choice: type, toolWarnings };
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
- }
1883
+ return { tools: openaiTools2, toolChoice: type, toolWarnings };
1884
+ case "tool":
1853
1885
  return {
1854
1886
  tools: openaiTools2,
1855
- tool_choice: {
1856
- type: "function",
1857
- name: toolChoice.toolName
1858
- },
1887
+ toolChoice: toolChoice.toolName === "web_search_preview" ? { type: "web_search_preview" } : { type: "function", name: toolChoice.toolName },
1859
1888
  toolWarnings
1860
1889
  };
1861
- }
1862
1890
  default: {
1863
1891
  const _exhaustiveCheck = type;
1864
- throw new UnsupportedFunctionalityError7({
1865
- functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
1892
+ throw new UnsupportedFunctionalityError5({
1893
+ functionality: `tool choice type: ${_exhaustiveCheck}`
1866
1894
  });
1867
1895
  }
1868
1896
  }
@@ -1872,16 +1900,19 @@ function prepareResponsesTools({
1872
1900
  var OpenAIResponsesLanguageModel = class {
1873
1901
  constructor(modelId, config) {
1874
1902
  this.specificationVersion = "v2";
1875
- this.defaultObjectGenerationMode = "json";
1876
1903
  this.modelId = modelId;
1877
1904
  this.config = config;
1878
1905
  }
1906
+ async getSupportedUrls() {
1907
+ return {
1908
+ "image/*": [/^https?:\/\/.*$/]
1909
+ };
1910
+ }
1879
1911
  get provider() {
1880
1912
  return this.config.provider;
1881
1913
  }
1882
1914
  getArgs({
1883
- mode,
1884
- maxTokens,
1915
+ maxOutputTokens,
1885
1916
  temperature,
1886
1917
  stopSequences,
1887
1918
  topP,
@@ -1890,24 +1921,19 @@ var OpenAIResponsesLanguageModel = class {
1890
1921
  frequencyPenalty,
1891
1922
  seed,
1892
1923
  prompt,
1893
- providerMetadata,
1924
+ providerOptions,
1925
+ tools,
1926
+ toolChoice,
1894
1927
  responseFormat
1895
1928
  }) {
1896
- var _a, _b, _c;
1929
+ var _a, _b;
1897
1930
  const warnings = [];
1898
1931
  const modelConfig = getResponsesModelConfig(this.modelId);
1899
- const type = mode.type;
1900
1932
  if (topK != null) {
1901
- warnings.push({
1902
- type: "unsupported-setting",
1903
- setting: "topK"
1904
- });
1933
+ warnings.push({ type: "unsupported-setting", setting: "topK" });
1905
1934
  }
1906
1935
  if (seed != null) {
1907
- warnings.push({
1908
- type: "unsupported-setting",
1909
- setting: "seed"
1910
- });
1936
+ warnings.push({ type: "unsupported-setting", setting: "seed" });
1911
1937
  }
1912
1938
  if (presencePenalty != null) {
1913
1939
  warnings.push({
@@ -1922,19 +1948,16 @@ var OpenAIResponsesLanguageModel = class {
1922
1948
  });
1923
1949
  }
1924
1950
  if (stopSequences != null) {
1925
- warnings.push({
1926
- type: "unsupported-setting",
1927
- setting: "stopSequences"
1928
- });
1951
+ warnings.push({ type: "unsupported-setting", setting: "stopSequences" });
1929
1952
  }
1930
1953
  const { messages, warnings: messageWarnings } = convertToOpenAIResponsesMessages({
1931
1954
  prompt,
1932
1955
  systemMessageMode: modelConfig.systemMessageMode
1933
1956
  });
1934
1957
  warnings.push(...messageWarnings);
1935
- const openaiOptions = parseProviderOptions({
1958
+ const openaiOptions = parseProviderOptions4({
1936
1959
  provider: "openai",
1937
- providerOptions: providerMetadata,
1960
+ providerOptions,
1938
1961
  schema: openaiResponsesProviderOptionsSchema
1939
1962
  });
1940
1963
  const isStrict = (_a = openaiOptions == null ? void 0 : openaiOptions.strictSchemas) != null ? _a : true;
@@ -1943,7 +1966,7 @@ var OpenAIResponsesLanguageModel = class {
1943
1966
  input: messages,
1944
1967
  temperature,
1945
1968
  top_p: topP,
1946
- max_output_tokens: maxTokens,
1969
+ max_output_tokens: maxOutputTokens,
1947
1970
  ...(responseFormat == null ? void 0 : responseFormat.type) === "json" && {
1948
1971
  text: {
1949
1972
  format: responseFormat.schema != null ? {
@@ -1988,65 +2011,26 @@ var OpenAIResponsesLanguageModel = class {
1988
2011
  });
1989
2012
  }
1990
2013
  }
1991
- switch (type) {
1992
- case "regular": {
1993
- const { tools, tool_choice, toolWarnings } = prepareResponsesTools({
1994
- mode,
1995
- strict: isStrict
1996
- // TODO support provider options on tools
1997
- });
1998
- return {
1999
- args: {
2000
- ...baseArgs,
2001
- tools,
2002
- tool_choice
2003
- },
2004
- warnings: [...warnings, ...toolWarnings]
2005
- };
2006
- }
2007
- case "object-json": {
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
- }
2014
+ const {
2015
+ tools: openaiTools2,
2016
+ toolChoice: openaiToolChoice,
2017
+ toolWarnings
2018
+ } = prepareResponsesTools({
2019
+ tools,
2020
+ toolChoice,
2021
+ strict: isStrict
2022
+ });
2023
+ return {
2024
+ args: {
2025
+ ...baseArgs,
2026
+ tools: openaiTools2,
2027
+ tool_choice: openaiToolChoice
2028
+ },
2029
+ warnings: [...warnings, ...toolWarnings]
2030
+ };
2047
2031
  }
2048
2032
  async doGenerate(options) {
2049
- var _a, _b, _c, _d, _e;
2033
+ var _a, _b, _c, _d, _e, _f, _g, _h;
2050
2034
  const { args: body, warnings } = this.getArgs(options);
2051
2035
  const {
2052
2036
  responseHeaders,
@@ -2057,109 +2041,115 @@ var OpenAIResponsesLanguageModel = class {
2057
2041
  path: "/responses",
2058
2042
  modelId: this.modelId
2059
2043
  }),
2060
- headers: combineHeaders5(this.config.headers(), options.headers),
2044
+ headers: combineHeaders6(this.config.headers(), options.headers),
2061
2045
  body,
2062
2046
  failedResponseHandler: openaiFailedResponseHandler,
2063
- successfulResponseHandler: createJsonResponseHandler5(
2064
- z7.object({
2065
- id: z7.string(),
2066
- created_at: z7.number(),
2067
- model: z7.string(),
2068
- output: z7.array(
2069
- z7.discriminatedUnion("type", [
2070
- z7.object({
2071
- type: z7.literal("message"),
2072
- role: z7.literal("assistant"),
2073
- content: z7.array(
2074
- z7.object({
2075
- type: z7.literal("output_text"),
2076
- text: z7.string(),
2077
- annotations: z7.array(
2078
- z7.object({
2079
- type: z7.literal("url_citation"),
2080
- start_index: z7.number(),
2081
- end_index: z7.number(),
2082
- url: z7.string(),
2083
- title: z7.string()
2047
+ successfulResponseHandler: createJsonResponseHandler6(
2048
+ z10.object({
2049
+ id: z10.string(),
2050
+ created_at: z10.number(),
2051
+ model: z10.string(),
2052
+ output: z10.array(
2053
+ z10.discriminatedUnion("type", [
2054
+ z10.object({
2055
+ type: z10.literal("message"),
2056
+ role: z10.literal("assistant"),
2057
+ content: z10.array(
2058
+ z10.object({
2059
+ type: z10.literal("output_text"),
2060
+ text: z10.string(),
2061
+ annotations: z10.array(
2062
+ z10.object({
2063
+ type: z10.literal("url_citation"),
2064
+ start_index: z10.number(),
2065
+ end_index: z10.number(),
2066
+ url: z10.string(),
2067
+ title: z10.string()
2084
2068
  })
2085
2069
  )
2086
2070
  })
2087
2071
  )
2088
2072
  }),
2089
- z7.object({
2090
- type: z7.literal("function_call"),
2091
- call_id: z7.string(),
2092
- name: z7.string(),
2093
- arguments: z7.string()
2073
+ z10.object({
2074
+ type: z10.literal("function_call"),
2075
+ call_id: z10.string(),
2076
+ name: z10.string(),
2077
+ arguments: z10.string()
2094
2078
  }),
2095
- z7.object({
2096
- type: z7.literal("web_search_call")
2079
+ z10.object({
2080
+ type: z10.literal("web_search_call")
2097
2081
  }),
2098
- z7.object({
2099
- type: z7.literal("computer_call")
2082
+ z10.object({
2083
+ type: z10.literal("computer_call")
2100
2084
  }),
2101
- z7.object({
2102
- type: z7.literal("reasoning")
2085
+ z10.object({
2086
+ type: z10.literal("reasoning")
2103
2087
  })
2104
2088
  ])
2105
2089
  ),
2106
- incomplete_details: z7.object({ reason: z7.string() }).nullable(),
2090
+ incomplete_details: z10.object({ reason: z10.string() }).nullable(),
2107
2091
  usage: usageSchema
2108
2092
  })
2109
2093
  ),
2110
2094
  abortSignal: options.abortSignal,
2111
2095
  fetch: this.config.fetch
2112
2096
  });
2113
- const outputTextElements = response.output.filter((output) => output.type === "message").flatMap((output) => output.content).filter((content) => content.type === "output_text");
2114
- const toolCalls = response.output.filter((output) => output.type === "function_call").map((output) => ({
2115
- toolCallType: "function",
2116
- toolCallId: output.call_id,
2117
- toolName: output.name,
2118
- args: output.arguments
2119
- }));
2097
+ const content = [];
2098
+ for (const part of response.output) {
2099
+ switch (part.type) {
2100
+ case "message": {
2101
+ for (const contentPart of part.content) {
2102
+ content.push({
2103
+ type: "text",
2104
+ text: contentPart.text
2105
+ });
2106
+ for (const annotation of contentPart.annotations) {
2107
+ content.push({
2108
+ type: "source",
2109
+ sourceType: "url",
2110
+ id: (_c = (_b = (_a = this.config).generateId) == null ? void 0 : _b.call(_a)) != null ? _c : generateId2(),
2111
+ url: annotation.url,
2112
+ title: annotation.title
2113
+ });
2114
+ }
2115
+ }
2116
+ break;
2117
+ }
2118
+ case "function_call": {
2119
+ content.push({
2120
+ type: "tool-call",
2121
+ toolCallType: "function",
2122
+ toolCallId: part.call_id,
2123
+ toolName: part.name,
2124
+ args: part.arguments
2125
+ });
2126
+ break;
2127
+ }
2128
+ }
2129
+ }
2120
2130
  return {
2121
- text: outputTextElements.map((content) => content.text).join("\n"),
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
- ),
2131
+ content,
2133
2132
  finishReason: mapOpenAIResponseFinishReason({
2134
- finishReason: (_a = response.incomplete_details) == null ? void 0 : _a.reason,
2135
- hasToolCalls: toolCalls.length > 0
2133
+ finishReason: (_d = response.incomplete_details) == null ? void 0 : _d.reason,
2134
+ hasToolCalls: content.some((part) => part.type === "tool-call")
2136
2135
  }),
2137
- toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
2138
2136
  usage: {
2139
- promptTokens: response.usage.input_tokens,
2140
- completionTokens: response.usage.output_tokens
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)
2137
+ inputTokens: response.usage.input_tokens,
2138
+ outputTokens: response.usage.output_tokens
2152
2139
  },
2140
+ request: { body },
2153
2141
  response: {
2154
2142
  id: response.id,
2155
2143
  timestamp: new Date(response.created_at * 1e3),
2156
- modelId: response.model
2144
+ modelId: response.model,
2145
+ headers: responseHeaders,
2146
+ body: rawResponse
2157
2147
  },
2158
2148
  providerMetadata: {
2159
2149
  openai: {
2160
2150
  responseId: response.id,
2161
- cachedPromptTokens: (_c = (_b = response.usage.input_tokens_details) == null ? void 0 : _b.cached_tokens) != null ? _c : null,
2162
- reasoningTokens: (_e = (_d = response.usage.output_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : null
2151
+ cachedPromptTokens: (_f = (_e = response.usage.input_tokens_details) == null ? void 0 : _e.cached_tokens) != null ? _f : null,
2152
+ reasoningTokens: (_h = (_g = response.usage.output_tokens_details) == null ? void 0 : _g.reasoning_tokens) != null ? _h : null
2163
2153
  }
2164
2154
  },
2165
2155
  warnings
@@ -2172,7 +2162,7 @@ var OpenAIResponsesLanguageModel = class {
2172
2162
  path: "/responses",
2173
2163
  modelId: this.modelId
2174
2164
  }),
2175
- headers: combineHeaders5(this.config.headers(), options.headers),
2165
+ headers: combineHeaders6(this.config.headers(), options.headers),
2176
2166
  body: {
2177
2167
  ...body,
2178
2168
  stream: true
@@ -2186,8 +2176,10 @@ var OpenAIResponsesLanguageModel = class {
2186
2176
  });
2187
2177
  const self = this;
2188
2178
  let finishReason = "unknown";
2189
- let promptTokens = NaN;
2190
- let completionTokens = NaN;
2179
+ const usage = {
2180
+ inputTokens: void 0,
2181
+ outputTokens: void 0
2182
+ };
2191
2183
  let cachedPromptTokens = null;
2192
2184
  let reasoningTokens = null;
2193
2185
  let responseId = null;
@@ -2196,6 +2188,9 @@ var OpenAIResponsesLanguageModel = class {
2196
2188
  return {
2197
2189
  stream: response.pipeThrough(
2198
2190
  new TransformStream({
2191
+ start(controller) {
2192
+ controller.enqueue({ type: "stream-start", warnings });
2193
+ },
2199
2194
  transform(chunk, controller) {
2200
2195
  var _a, _b, _c, _d, _e, _f, _g, _h;
2201
2196
  if (!chunk.success) {
@@ -2239,8 +2234,8 @@ var OpenAIResponsesLanguageModel = class {
2239
2234
  });
2240
2235
  } else if (isTextDeltaChunk(value)) {
2241
2236
  controller.enqueue({
2242
- type: "text-delta",
2243
- textDelta: value.delta
2237
+ type: "text",
2238
+ text: value.delta
2244
2239
  });
2245
2240
  } else if (isResponseOutputItemDoneChunk(value) && value.item.type === "function_call") {
2246
2241
  ongoingToolCalls[value.output_index] = void 0;
@@ -2257,19 +2252,17 @@ var OpenAIResponsesLanguageModel = class {
2257
2252
  finishReason: (_a = value.response.incomplete_details) == null ? void 0 : _a.reason,
2258
2253
  hasToolCalls
2259
2254
  });
2260
- promptTokens = value.response.usage.input_tokens;
2261
- completionTokens = value.response.usage.output_tokens;
2255
+ usage.inputTokens = value.response.usage.input_tokens;
2256
+ usage.outputTokens = value.response.usage.output_tokens;
2262
2257
  cachedPromptTokens = (_c = (_b = value.response.usage.input_tokens_details) == null ? void 0 : _b.cached_tokens) != null ? _c : cachedPromptTokens;
2263
2258
  reasoningTokens = (_e = (_d = value.response.usage.output_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : reasoningTokens;
2264
2259
  } else if (isResponseAnnotationAddedChunk(value)) {
2265
2260
  controller.enqueue({
2266
2261
  type: "source",
2267
- source: {
2268
- sourceType: "url",
2269
- id: (_h = (_g = (_f = self.config).generateId) == null ? void 0 : _g.call(_f)) != null ? _h : generateId2(),
2270
- url: value.annotation.url,
2271
- title: value.annotation.title
2272
- }
2262
+ sourceType: "url",
2263
+ id: (_h = (_g = (_f = self.config).generateId) == null ? void 0 : _g.call(_f)) != null ? _h : generateId2(),
2264
+ url: value.annotation.url,
2265
+ title: value.annotation.title
2273
2266
  });
2274
2267
  }
2275
2268
  },
@@ -2277,7 +2270,7 @@ var OpenAIResponsesLanguageModel = class {
2277
2270
  controller.enqueue({
2278
2271
  type: "finish",
2279
2272
  finishReason,
2280
- usage: { promptTokens, completionTokens },
2273
+ usage,
2281
2274
  ...(cachedPromptTokens != null || reasoningTokens != null) && {
2282
2275
  providerMetadata: {
2283
2276
  openai: {
@@ -2291,89 +2284,84 @@ var OpenAIResponsesLanguageModel = class {
2291
2284
  }
2292
2285
  })
2293
2286
  ),
2294
- rawCall: {
2295
- rawPrompt: void 0,
2296
- rawSettings: {}
2297
- },
2298
- rawResponse: { headers: responseHeaders },
2299
- request: { body: JSON.stringify(body) },
2300
- warnings
2287
+ request: { body },
2288
+ response: { headers: responseHeaders }
2301
2289
  };
2302
2290
  }
2303
2291
  };
2304
- var usageSchema = z7.object({
2305
- input_tokens: z7.number(),
2306
- input_tokens_details: z7.object({ cached_tokens: z7.number().nullish() }).nullish(),
2307
- output_tokens: z7.number(),
2308
- output_tokens_details: z7.object({ reasoning_tokens: z7.number().nullish() }).nullish()
2292
+ var usageSchema = z10.object({
2293
+ input_tokens: z10.number(),
2294
+ input_tokens_details: z10.object({ cached_tokens: z10.number().nullish() }).nullish(),
2295
+ output_tokens: z10.number(),
2296
+ output_tokens_details: z10.object({ reasoning_tokens: z10.number().nullish() }).nullish()
2309
2297
  });
2310
- var textDeltaChunkSchema = z7.object({
2311
- type: z7.literal("response.output_text.delta"),
2312
- delta: z7.string()
2298
+ var textDeltaChunkSchema = z10.object({
2299
+ type: z10.literal("response.output_text.delta"),
2300
+ delta: z10.string()
2313
2301
  });
2314
- var responseFinishedChunkSchema = z7.object({
2315
- type: z7.enum(["response.completed", "response.incomplete"]),
2316
- response: z7.object({
2317
- incomplete_details: z7.object({ reason: z7.string() }).nullish(),
2302
+ var responseFinishedChunkSchema = z10.object({
2303
+ type: z10.enum(["response.completed", "response.incomplete"]),
2304
+ response: z10.object({
2305
+ incomplete_details: z10.object({ reason: z10.string() }).nullish(),
2318
2306
  usage: usageSchema
2319
2307
  })
2320
2308
  });
2321
- var responseCreatedChunkSchema = z7.object({
2322
- type: z7.literal("response.created"),
2323
- response: z7.object({
2324
- id: z7.string(),
2325
- created_at: z7.number(),
2326
- model: z7.string()
2309
+ var responseCreatedChunkSchema = z10.object({
2310
+ type: z10.literal("response.created"),
2311
+ response: z10.object({
2312
+ id: z10.string(),
2313
+ created_at: z10.number(),
2314
+ model: z10.string()
2327
2315
  })
2328
2316
  });
2329
- var responseOutputItemDoneSchema = z7.object({
2330
- type: z7.literal("response.output_item.done"),
2331
- output_index: z7.number(),
2332
- item: z7.discriminatedUnion("type", [
2333
- z7.object({
2334
- type: z7.literal("message")
2317
+ var responseOutputItemDoneSchema = z10.object({
2318
+ type: z10.literal("response.output_item.done"),
2319
+ output_index: z10.number(),
2320
+ item: z10.discriminatedUnion("type", [
2321
+ z10.object({
2322
+ type: z10.literal("message")
2335
2323
  }),
2336
- z7.object({
2337
- type: z7.literal("function_call"),
2338
- id: z7.string(),
2339
- call_id: z7.string(),
2340
- name: z7.string(),
2341
- arguments: z7.string(),
2342
- status: z7.literal("completed")
2324
+ z10.object({
2325
+ type: z10.literal("function_call"),
2326
+ id: z10.string(),
2327
+ call_id: z10.string(),
2328
+ name: z10.string(),
2329
+ arguments: z10.string(),
2330
+ status: z10.literal("completed")
2343
2331
  })
2344
2332
  ])
2345
2333
  });
2346
- var responseFunctionCallArgumentsDeltaSchema = z7.object({
2347
- type: z7.literal("response.function_call_arguments.delta"),
2348
- item_id: z7.string(),
2349
- output_index: z7.number(),
2350
- delta: z7.string()
2334
+ var responseFunctionCallArgumentsDeltaSchema = z10.object({
2335
+ type: z10.literal("response.function_call_arguments.delta"),
2336
+ item_id: z10.string(),
2337
+ output_index: z10.number(),
2338
+ delta: z10.string()
2351
2339
  });
2352
- var responseOutputItemAddedSchema = z7.object({
2353
- type: z7.literal("response.output_item.added"),
2354
- output_index: z7.number(),
2355
- item: z7.discriminatedUnion("type", [
2356
- z7.object({
2357
- type: z7.literal("message")
2340
+ var responseOutputItemAddedSchema = z10.object({
2341
+ type: z10.literal("response.output_item.added"),
2342
+ output_index: z10.number(),
2343
+ item: z10.discriminatedUnion("type", [
2344
+ z10.object({
2345
+ type: z10.literal("message")
2358
2346
  }),
2359
- z7.object({
2360
- type: z7.literal("function_call"),
2361
- id: z7.string(),
2362
- call_id: z7.string(),
2363
- name: z7.string(),
2364
- arguments: z7.string()
2347
+ z10.object({
2348
+ type: z10.literal("function_call"),
2349
+ id: z10.string(),
2350
+ call_id: z10.string(),
2351
+ name: z10.string(),
2352
+ arguments: z10.string()
2365
2353
  })
2366
2354
  ])
2367
2355
  });
2368
- var responseAnnotationAddedSchema = z7.object({
2369
- type: z7.literal("response.output_text.annotation.added"),
2370
- annotation: z7.object({
2371
- type: z7.literal("url_citation"),
2372
- url: z7.string(),
2373
- title: z7.string()
2356
+ var responseAnnotationAddedSchema = z10.object({
2357
+ type: z10.literal("response.output_text.annotation.added"),
2358
+ annotation: z10.object({
2359
+ type: z10.literal("url_citation"),
2360
+ url: z10.string(),
2361
+ title: z10.string()
2374
2362
  })
2375
2363
  });
2376
- var openaiResponsesChunkSchema = z7.union([
2364
+ var openaiResponsesChunkSchema = z10.union([
2377
2365
  textDeltaChunkSchema,
2378
2366
  responseFinishedChunkSchema,
2379
2367
  responseCreatedChunkSchema,
@@ -2381,7 +2369,7 @@ var openaiResponsesChunkSchema = z7.union([
2381
2369
  responseFunctionCallArgumentsDeltaSchema,
2382
2370
  responseOutputItemAddedSchema,
2383
2371
  responseAnnotationAddedSchema,
2384
- z7.object({ type: z7.string() }).passthrough()
2372
+ z10.object({ type: z10.string() }).passthrough()
2385
2373
  // fallback for unknown chunks
2386
2374
  ]);
2387
2375
  function isTextDeltaChunk(chunk) {
@@ -2426,17 +2414,121 @@ function getResponsesModelConfig(modelId) {
2426
2414
  requiredAutoTruncation: false
2427
2415
  };
2428
2416
  }
2429
- var openaiResponsesProviderOptionsSchema = z7.object({
2430
- metadata: z7.any().nullish(),
2431
- parallelToolCalls: z7.boolean().nullish(),
2432
- previousResponseId: z7.string().nullish(),
2433
- store: z7.boolean().nullish(),
2434
- user: z7.string().nullish(),
2435
- reasoningEffort: z7.string().nullish(),
2436
- strictSchemas: z7.boolean().nullish(),
2437
- instructions: z7.string().nullish()
2417
+ var openaiResponsesProviderOptionsSchema = z10.object({
2418
+ metadata: z10.any().nullish(),
2419
+ parallelToolCalls: z10.boolean().nullish(),
2420
+ previousResponseId: z10.string().nullish(),
2421
+ store: z10.boolean().nullish(),
2422
+ user: z10.string().nullish(),
2423
+ reasoningEffort: z10.string().nullish(),
2424
+ strictSchemas: z10.boolean().nullish(),
2425
+ instructions: z10.string().nullish()
2438
2426
  });
2439
2427
 
2428
+ // src/openai-speech-model.ts
2429
+ import {
2430
+ combineHeaders as combineHeaders7,
2431
+ createBinaryResponseHandler,
2432
+ parseProviderOptions as parseProviderOptions5,
2433
+ postJsonToApi as postJsonToApi6
2434
+ } from "@ai-sdk/provider-utils";
2435
+ import { z as z11 } from "zod";
2436
+ var OpenAIProviderOptionsSchema = z11.object({
2437
+ instructions: z11.string().nullish(),
2438
+ speed: z11.number().min(0.25).max(4).default(1).nullish()
2439
+ });
2440
+ var OpenAISpeechModel = class {
2441
+ constructor(modelId, config) {
2442
+ this.modelId = modelId;
2443
+ this.config = config;
2444
+ this.specificationVersion = "v1";
2445
+ }
2446
+ get provider() {
2447
+ return this.config.provider;
2448
+ }
2449
+ getArgs({
2450
+ text,
2451
+ voice = "alloy",
2452
+ outputFormat = "mp3",
2453
+ speed,
2454
+ instructions,
2455
+ providerOptions
2456
+ }) {
2457
+ const warnings = [];
2458
+ const openAIOptions = parseProviderOptions5({
2459
+ provider: "openai",
2460
+ providerOptions,
2461
+ schema: OpenAIProviderOptionsSchema
2462
+ });
2463
+ const requestBody = {
2464
+ model: this.modelId,
2465
+ input: text,
2466
+ voice,
2467
+ response_format: "mp3",
2468
+ speed,
2469
+ instructions
2470
+ };
2471
+ if (outputFormat) {
2472
+ if (["mp3", "opus", "aac", "flac", "wav", "pcm"].includes(outputFormat)) {
2473
+ requestBody.response_format = outputFormat;
2474
+ } else {
2475
+ warnings.push({
2476
+ type: "unsupported-setting",
2477
+ setting: "outputFormat",
2478
+ details: `Unsupported output format: ${outputFormat}. Using mp3 instead.`
2479
+ });
2480
+ }
2481
+ }
2482
+ if (openAIOptions) {
2483
+ const speechModelOptions = {};
2484
+ for (const key in speechModelOptions) {
2485
+ const value = speechModelOptions[key];
2486
+ if (value !== void 0) {
2487
+ requestBody[key] = value;
2488
+ }
2489
+ }
2490
+ }
2491
+ return {
2492
+ requestBody,
2493
+ warnings
2494
+ };
2495
+ }
2496
+ async doGenerate(options) {
2497
+ var _a, _b, _c;
2498
+ const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
2499
+ const { requestBody, warnings } = this.getArgs(options);
2500
+ const {
2501
+ value: audio,
2502
+ responseHeaders,
2503
+ rawValue: rawResponse
2504
+ } = await postJsonToApi6({
2505
+ url: this.config.url({
2506
+ path: "/audio/speech",
2507
+ modelId: this.modelId
2508
+ }),
2509
+ headers: combineHeaders7(this.config.headers(), options.headers),
2510
+ body: requestBody,
2511
+ failedResponseHandler: openaiFailedResponseHandler,
2512
+ successfulResponseHandler: createBinaryResponseHandler(),
2513
+ abortSignal: options.abortSignal,
2514
+ fetch: this.config.fetch
2515
+ });
2516
+ return {
2517
+ audio,
2518
+ warnings,
2519
+ request: {
2520
+ body: JSON.stringify(requestBody)
2521
+ },
2522
+ response: {
2523
+ timestamp: currentDate,
2524
+ modelId: this.modelId,
2525
+ headers: responseHeaders,
2526
+ body: rawResponse
2527
+ }
2528
+ };
2529
+ }
2530
+ };
2531
+
2440
2532
  // src/openai-provider.ts
2441
2533
  function createOpenAI(options = {}) {
2442
2534
  var _a, _b, _c;
@@ -2479,6 +2571,18 @@ function createOpenAI(options = {}) {
2479
2571
  headers: getHeaders,
2480
2572
  fetch: options.fetch
2481
2573
  });
2574
+ const createTranscriptionModel = (modelId) => new OpenAITranscriptionModel(modelId, {
2575
+ provider: `${providerName}.transcription`,
2576
+ url: ({ path }) => `${baseURL}${path}`,
2577
+ headers: getHeaders,
2578
+ fetch: options.fetch
2579
+ });
2580
+ const createSpeechModel = (modelId) => new OpenAISpeechModel(modelId, {
2581
+ provider: `${providerName}.speech`,
2582
+ url: ({ path }) => `${baseURL}${path}`,
2583
+ headers: getHeaders,
2584
+ fetch: options.fetch
2585
+ });
2482
2586
  const createLanguageModel = (modelId, settings) => {
2483
2587
  if (new.target) {
2484
2588
  throw new Error(
@@ -2513,6 +2617,10 @@ function createOpenAI(options = {}) {
2513
2617
  provider.textEmbeddingModel = createEmbeddingModel;
2514
2618
  provider.image = createImageModel;
2515
2619
  provider.imageModel = createImageModel;
2620
+ provider.transcription = createTranscriptionModel;
2621
+ provider.transcriptionModel = createTranscriptionModel;
2622
+ provider.speech = createSpeechModel;
2623
+ provider.speechModel = createSpeechModel;
2516
2624
  provider.tools = openaiTools;
2517
2625
  return provider;
2518
2626
  }