@ai-sdk/openai-compatible 1.0.0-canary.9 → 1.0.0

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.js CHANGED
@@ -31,7 +31,7 @@ module.exports = __toCommonJS(src_exports);
31
31
  // src/openai-compatible-chat-language-model.ts
32
32
  var import_provider3 = require("@ai-sdk/provider");
33
33
  var import_provider_utils = require("@ai-sdk/provider-utils");
34
- var import_zod3 = require("zod");
34
+ var import_v43 = require("zod/v4");
35
35
 
36
36
  // src/convert-to-openai-compatible-chat-messages.ts
37
37
  var import_provider = require("@ai-sdk/provider");
@@ -103,7 +103,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
103
103
  type: "function",
104
104
  function: {
105
105
  name: part.toolName,
106
- arguments: JSON.stringify(part.args)
106
+ arguments: JSON.stringify(part.input)
107
107
  },
108
108
  ...partMetadata
109
109
  });
@@ -121,11 +121,24 @@ function convertToOpenAICompatibleChatMessages(prompt) {
121
121
  }
122
122
  case "tool": {
123
123
  for (const toolResponse of content) {
124
+ const output = toolResponse.output;
125
+ let contentValue;
126
+ switch (output.type) {
127
+ case "text":
128
+ case "error-text":
129
+ contentValue = output.value;
130
+ break;
131
+ case "content":
132
+ case "json":
133
+ case "error-json":
134
+ contentValue = JSON.stringify(output.value);
135
+ break;
136
+ }
124
137
  const toolResponseMetadata = getOpenAIMetadata(toolResponse);
125
138
  messages.push({
126
139
  role: "tool",
127
140
  tool_call_id: toolResponse.toolCallId,
128
- content: JSON.stringify(toolResponse.result),
141
+ content: contentValue,
129
142
  ...toolResponseMetadata
130
143
  });
131
144
  }
@@ -171,26 +184,30 @@ function mapOpenAICompatibleFinishReason(finishReason) {
171
184
  }
172
185
 
173
186
  // src/openai-compatible-chat-options.ts
174
- var import_zod = require("zod");
175
- var openaiCompatibleProviderOptions = import_zod.z.object({
187
+ var import_v4 = require("zod/v4");
188
+ var openaiCompatibleProviderOptions = import_v4.z.object({
176
189
  /**
177
190
  * A unique identifier representing your end-user, which can help the provider to
178
191
  * monitor and detect abuse.
179
192
  */
180
- user: import_zod.z.string().optional()
193
+ user: import_v4.z.string().optional(),
194
+ /**
195
+ * Reasoning effort for reasoning models. Defaults to `medium`.
196
+ */
197
+ reasoningEffort: import_v4.z.string().optional()
181
198
  });
182
199
 
183
200
  // src/openai-compatible-error.ts
184
- var import_zod2 = require("zod");
185
- var openaiCompatibleErrorDataSchema = import_zod2.z.object({
186
- error: import_zod2.z.object({
187
- message: import_zod2.z.string(),
201
+ var import_v42 = require("zod/v4");
202
+ var openaiCompatibleErrorDataSchema = import_v42.z.object({
203
+ error: import_v42.z.object({
204
+ message: import_v42.z.string(),
188
205
  // The additional information below is handled loosely to support
189
206
  // OpenAI-compatible providers that have slightly different error
190
207
  // responses:
191
- type: import_zod2.z.string().nullish(),
192
- param: import_zod2.z.any().nullish(),
193
- code: import_zod2.z.union([import_zod2.z.string(), import_zod2.z.number()]).nullish()
208
+ type: import_v42.z.string().nullish(),
209
+ param: import_v42.z.any().nullish(),
210
+ code: import_v42.z.union([import_v42.z.string(), import_v42.z.number()]).nullish()
194
211
  })
195
212
  });
196
213
  var defaultOpenAICompatibleErrorStructure = {
@@ -219,7 +236,7 @@ function prepareTools({
219
236
  function: {
220
237
  name: tool.name,
221
238
  description: tool.description,
222
- parameters: tool.parameters
239
+ parameters: tool.inputSchema
223
240
  }
224
241
  });
225
242
  }
@@ -272,11 +289,11 @@ var OpenAICompatibleChatLanguageModel = class {
272
289
  get providerOptionsName() {
273
290
  return this.config.provider.split(".")[0].trim();
274
291
  }
275
- async getSupportedUrls() {
292
+ get supportedUrls() {
276
293
  var _a, _b, _c;
277
- return (_c = (_b = (_a = this.config).getSupportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
294
+ return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
278
295
  }
279
- getArgs({
296
+ async getArgs({
280
297
  prompt,
281
298
  maxOutputTokens,
282
299
  temperature,
@@ -294,12 +311,12 @@ var OpenAICompatibleChatLanguageModel = class {
294
311
  var _a, _b, _c;
295
312
  const warnings = [];
296
313
  const compatibleOptions = Object.assign(
297
- (_a = (0, import_provider_utils.parseProviderOptions)({
314
+ (_a = await (0, import_provider_utils.parseProviderOptions)({
298
315
  provider: "openai-compatible",
299
316
  providerOptions,
300
317
  schema: openaiCompatibleProviderOptions
301
318
  })) != null ? _a : {},
302
- (_b = (0, import_provider_utils.parseProviderOptions)({
319
+ (_b = await (0, import_provider_utils.parseProviderOptions)({
303
320
  provider: this.providerOptionsName,
304
321
  providerOptions,
305
322
  schema: openaiCompatibleProviderOptions
@@ -346,6 +363,7 @@ var OpenAICompatibleChatLanguageModel = class {
346
363
  stop: stopSequences,
347
364
  seed,
348
365
  ...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
366
+ reasoning_effort: compatibleOptions.reasoningEffort,
349
367
  // messages:
350
368
  messages: convertToOpenAICompatibleChatMessages(prompt),
351
369
  // tools:
@@ -356,8 +374,8 @@ var OpenAICompatibleChatLanguageModel = class {
356
374
  };
357
375
  }
358
376
  async doGenerate(options) {
359
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
360
- const { args, warnings } = this.getArgs({ ...options });
377
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
378
+ const { args, warnings } = await this.getArgs({ ...options });
361
379
  const body = JSON.stringify(args);
362
380
  const {
363
381
  responseHeaders,
@@ -387,7 +405,6 @@ var OpenAICompatibleChatLanguageModel = class {
387
405
  if (reasoning != null && reasoning.length > 0) {
388
406
  content.push({
389
407
  type: "reasoning",
390
- reasoningType: "text",
391
408
  text: reasoning
392
409
  });
393
410
  }
@@ -395,39 +412,34 @@ var OpenAICompatibleChatLanguageModel = class {
395
412
  for (const toolCall of choice.message.tool_calls) {
396
413
  content.push({
397
414
  type: "tool-call",
398
- toolCallType: "function",
399
415
  toolCallId: (_a = toolCall.id) != null ? _a : (0, import_provider_utils.generateId)(),
400
416
  toolName: toolCall.function.name,
401
- args: toolCall.function.arguments
417
+ input: toolCall.function.arguments
402
418
  });
403
419
  }
404
420
  }
405
421
  const providerMetadata = {
406
422
  [this.providerOptionsName]: {},
407
- ...(_c = (_b = this.config.metadataExtractor) == null ? void 0 : _b.extractMetadata) == null ? void 0 : _c.call(_b, {
423
+ ...await ((_c = (_b = this.config.metadataExtractor) == null ? void 0 : _b.extractMetadata) == null ? void 0 : _c.call(_b, {
408
424
  parsedBody: rawResponse
409
- })
425
+ }))
410
426
  };
411
427
  const completionTokenDetails = (_d = responseBody.usage) == null ? void 0 : _d.completion_tokens_details;
412
- const promptTokenDetails = (_e = responseBody.usage) == null ? void 0 : _e.prompt_tokens_details;
413
- if ((completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens) != null) {
414
- providerMetadata[this.providerOptionsName].reasoningTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens;
415
- }
416
428
  if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
417
429
  providerMetadata[this.providerOptionsName].acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
418
430
  }
419
431
  if ((completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens) != null) {
420
432
  providerMetadata[this.providerOptionsName].rejectedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens;
421
433
  }
422
- if ((promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens) != null) {
423
- providerMetadata[this.providerOptionsName].cachedPromptTokens = promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens;
424
- }
425
434
  return {
426
435
  content,
427
436
  finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
428
437
  usage: {
429
- inputTokens: (_g = (_f = responseBody.usage) == null ? void 0 : _f.prompt_tokens) != null ? _g : void 0,
430
- outputTokens: (_i = (_h = responseBody.usage) == null ? void 0 : _h.completion_tokens) != null ? _i : void 0
438
+ inputTokens: (_f = (_e = responseBody.usage) == null ? void 0 : _e.prompt_tokens) != null ? _f : void 0,
439
+ outputTokens: (_h = (_g = responseBody.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : void 0,
440
+ totalTokens: (_j = (_i = responseBody.usage) == null ? void 0 : _i.total_tokens) != null ? _j : void 0,
441
+ reasoningTokens: (_m = (_l = (_k = responseBody.usage) == null ? void 0 : _k.completion_tokens_details) == null ? void 0 : _l.reasoning_tokens) != null ? _m : void 0,
442
+ cachedInputTokens: (_p = (_o = (_n = responseBody.usage) == null ? void 0 : _n.prompt_tokens_details) == null ? void 0 : _o.cached_tokens) != null ? _p : void 0
431
443
  },
432
444
  providerMetadata,
433
445
  request: { body },
@@ -441,8 +453,13 @@ var OpenAICompatibleChatLanguageModel = class {
441
453
  }
442
454
  async doStream(options) {
443
455
  var _a;
444
- const { args, warnings } = this.getArgs({ ...options });
445
- const body = JSON.stringify({ ...args, stream: true });
456
+ const { args, warnings } = await this.getArgs({ ...options });
457
+ const body = {
458
+ ...args,
459
+ stream: true,
460
+ // only include stream_options when in strict compatibility mode:
461
+ stream_options: this.config.includeUsage ? { include_usage: true } : void 0
462
+ };
446
463
  const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
447
464
  const { responseHeaders, value: response } = await (0, import_provider_utils.postJsonToApi)({
448
465
  url: this.config.url({
@@ -450,10 +467,7 @@ var OpenAICompatibleChatLanguageModel = class {
450
467
  modelId: this.modelId
451
468
  }),
452
469
  headers: (0, import_provider_utils.combineHeaders)(this.config.headers(), options.headers),
453
- body: {
454
- ...args,
455
- stream: true
456
- },
470
+ body,
457
471
  failedResponseHandler: this.failedResponseHandler,
458
472
  successfulResponseHandler: (0, import_provider_utils.createEventSourceResponseHandler)(
459
473
  this.chunkSchema
@@ -463,7 +477,7 @@ var OpenAICompatibleChatLanguageModel = class {
463
477
  });
464
478
  const toolCalls = [];
465
479
  let finishReason = "unknown";
466
- let usage = {
480
+ const usage = {
467
481
  completionTokens: void 0,
468
482
  completionTokensDetails: {
469
483
  reasoningTokens: void 0,
@@ -473,10 +487,13 @@ var OpenAICompatibleChatLanguageModel = class {
473
487
  promptTokens: void 0,
474
488
  promptTokensDetails: {
475
489
  cachedTokens: void 0
476
- }
490
+ },
491
+ totalTokens: void 0
477
492
  };
478
493
  let isFirstChunk = true;
479
- let providerOptionsName = this.providerOptionsName;
494
+ const providerOptionsName = this.providerOptionsName;
495
+ let isActiveReasoning = false;
496
+ let isActiveText = false;
480
497
  return {
481
498
  stream: response.pipeThrough(
482
499
  new TransformStream({
@@ -486,6 +503,9 @@ var OpenAICompatibleChatLanguageModel = class {
486
503
  // TODO we lost type safety on Chunk, most likely due to the error schema. MUST FIX
487
504
  transform(chunk, controller) {
488
505
  var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
506
+ if (options.includeRawChunks) {
507
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
508
+ }
489
509
  if (!chunk.success) {
490
510
  finishReason = "error";
491
511
  controller.enqueue({ type: "error", error: chunk.error });
@@ -509,11 +529,13 @@ var OpenAICompatibleChatLanguageModel = class {
509
529
  const {
510
530
  prompt_tokens,
511
531
  completion_tokens,
532
+ total_tokens,
512
533
  prompt_tokens_details,
513
534
  completion_tokens_details
514
535
  } = value.usage;
515
536
  usage.promptTokens = prompt_tokens != null ? prompt_tokens : void 0;
516
537
  usage.completionTokens = completion_tokens != null ? completion_tokens : void 0;
538
+ usage.totalTokens = total_tokens != null ? total_tokens : void 0;
517
539
  if ((completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens) != null) {
518
540
  usage.completionTokensDetails.reasoningTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens;
519
541
  }
@@ -538,28 +560,34 @@ var OpenAICompatibleChatLanguageModel = class {
538
560
  }
539
561
  const delta = choice.delta;
540
562
  if (delta.reasoning_content != null) {
563
+ if (!isActiveReasoning) {
564
+ controller.enqueue({
565
+ type: "reasoning-start",
566
+ id: "reasoning-0"
567
+ });
568
+ isActiveReasoning = true;
569
+ }
541
570
  controller.enqueue({
542
- type: "reasoning",
543
- reasoningType: "text",
544
- text: delta.reasoning_content
571
+ type: "reasoning-delta",
572
+ id: "reasoning-0",
573
+ delta: delta.reasoning_content
545
574
  });
546
575
  }
547
576
  if (delta.content != null) {
577
+ if (!isActiveText) {
578
+ controller.enqueue({ type: "text-start", id: "txt-0" });
579
+ isActiveText = true;
580
+ }
548
581
  controller.enqueue({
549
- type: "text",
550
- text: delta.content
582
+ type: "text-delta",
583
+ id: "txt-0",
584
+ delta: delta.content
551
585
  });
552
586
  }
553
587
  if (delta.tool_calls != null) {
554
588
  for (const toolCallDelta of delta.tool_calls) {
555
589
  const index = toolCallDelta.index;
556
590
  if (toolCalls[index] == null) {
557
- if (toolCallDelta.type !== "function") {
558
- throw new import_provider3.InvalidResponseDataError({
559
- data: toolCallDelta,
560
- message: `Expected 'function' type.`
561
- });
562
- }
563
591
  if (toolCallDelta.id == null) {
564
592
  throw new import_provider3.InvalidResponseDataError({
565
593
  data: toolCallDelta,
@@ -572,6 +600,11 @@ var OpenAICompatibleChatLanguageModel = class {
572
600
  message: `Expected 'function.name' to be a string.`
573
601
  });
574
602
  }
603
+ controller.enqueue({
604
+ type: "tool-input-start",
605
+ id: toolCallDelta.id,
606
+ toolName: toolCallDelta.function.name
607
+ });
575
608
  toolCalls[index] = {
576
609
  id: toolCallDelta.id,
577
610
  type: "function",
@@ -585,20 +618,21 @@ var OpenAICompatibleChatLanguageModel = class {
585
618
  if (((_c = toolCall2.function) == null ? void 0 : _c.name) != null && ((_d = toolCall2.function) == null ? void 0 : _d.arguments) != null) {
586
619
  if (toolCall2.function.arguments.length > 0) {
587
620
  controller.enqueue({
588
- type: "tool-call-delta",
589
- toolCallType: "function",
590
- toolCallId: toolCall2.id,
591
- toolName: toolCall2.function.name,
592
- argsTextDelta: toolCall2.function.arguments
621
+ type: "tool-input-start",
622
+ id: toolCall2.id,
623
+ toolName: toolCall2.function.name
593
624
  });
594
625
  }
595
626
  if ((0, import_provider_utils.isParsableJson)(toolCall2.function.arguments)) {
627
+ controller.enqueue({
628
+ type: "tool-input-end",
629
+ id: toolCall2.id
630
+ });
596
631
  controller.enqueue({
597
632
  type: "tool-call",
598
- toolCallType: "function",
599
633
  toolCallId: (_e = toolCall2.id) != null ? _e : (0, import_provider_utils.generateId)(),
600
634
  toolName: toolCall2.function.name,
601
- args: toolCall2.function.arguments
635
+ input: toolCall2.function.arguments
602
636
  });
603
637
  toolCall2.hasFinished = true;
604
638
  }
@@ -613,19 +647,20 @@ var OpenAICompatibleChatLanguageModel = class {
613
647
  toolCall.function.arguments += (_h = (_g = toolCallDelta.function) == null ? void 0 : _g.arguments) != null ? _h : "";
614
648
  }
615
649
  controller.enqueue({
616
- type: "tool-call-delta",
617
- toolCallType: "function",
618
- toolCallId: toolCall.id,
619
- toolName: toolCall.function.name,
620
- argsTextDelta: (_i = toolCallDelta.function.arguments) != null ? _i : ""
650
+ type: "tool-input-delta",
651
+ id: toolCall.id,
652
+ delta: (_i = toolCallDelta.function.arguments) != null ? _i : ""
621
653
  });
622
654
  if (((_j = toolCall.function) == null ? void 0 : _j.name) != null && ((_k = toolCall.function) == null ? void 0 : _k.arguments) != null && (0, import_provider_utils.isParsableJson)(toolCall.function.arguments)) {
655
+ controller.enqueue({
656
+ type: "tool-input-end",
657
+ id: toolCall.id
658
+ });
623
659
  controller.enqueue({
624
660
  type: "tool-call",
625
- toolCallType: "function",
626
661
  toolCallId: (_l = toolCall.id) != null ? _l : (0, import_provider_utils.generateId)(),
627
662
  toolName: toolCall.function.name,
628
- args: toolCall.function.arguments
663
+ input: toolCall.function.arguments
629
664
  });
630
665
  toolCall.hasFinished = true;
631
666
  }
@@ -633,29 +668,46 @@ var OpenAICompatibleChatLanguageModel = class {
633
668
  }
634
669
  },
635
670
  flush(controller) {
636
- var _a2, _b;
671
+ var _a2, _b, _c, _d, _e, _f;
672
+ if (isActiveReasoning) {
673
+ controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
674
+ }
675
+ if (isActiveText) {
676
+ controller.enqueue({ type: "text-end", id: "txt-0" });
677
+ }
678
+ for (const toolCall of toolCalls.filter(
679
+ (toolCall2) => !toolCall2.hasFinished
680
+ )) {
681
+ controller.enqueue({
682
+ type: "tool-input-end",
683
+ id: toolCall.id
684
+ });
685
+ controller.enqueue({
686
+ type: "tool-call",
687
+ toolCallId: (_a2 = toolCall.id) != null ? _a2 : (0, import_provider_utils.generateId)(),
688
+ toolName: toolCall.function.name,
689
+ input: toolCall.function.arguments
690
+ });
691
+ }
637
692
  const providerMetadata = {
638
693
  [providerOptionsName]: {},
639
694
  ...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
640
695
  };
641
- if (usage.completionTokensDetails.reasoningTokens != null) {
642
- providerMetadata[providerOptionsName].reasoningTokens = usage.completionTokensDetails.reasoningTokens;
643
- }
644
696
  if (usage.completionTokensDetails.acceptedPredictionTokens != null) {
645
697
  providerMetadata[providerOptionsName].acceptedPredictionTokens = usage.completionTokensDetails.acceptedPredictionTokens;
646
698
  }
647
699
  if (usage.completionTokensDetails.rejectedPredictionTokens != null) {
648
700
  providerMetadata[providerOptionsName].rejectedPredictionTokens = usage.completionTokensDetails.rejectedPredictionTokens;
649
701
  }
650
- if (usage.promptTokensDetails.cachedTokens != null) {
651
- providerMetadata[providerOptionsName].cachedPromptTokens = usage.promptTokensDetails.cachedTokens;
652
- }
653
702
  controller.enqueue({
654
703
  type: "finish",
655
704
  finishReason,
656
705
  usage: {
657
- inputTokens: (_a2 = usage.promptTokens) != null ? _a2 : void 0,
658
- outputTokens: (_b = usage.completionTokens) != null ? _b : void 0
706
+ inputTokens: (_b = usage.promptTokens) != null ? _b : void 0,
707
+ outputTokens: (_c = usage.completionTokens) != null ? _c : void 0,
708
+ totalTokens: (_d = usage.totalTokens) != null ? _d : void 0,
709
+ reasoningTokens: (_e = usage.completionTokensDetails.reasoningTokens) != null ? _e : void 0,
710
+ cachedInputTokens: (_f = usage.promptTokensDetails.cachedTokens) != null ? _f : void 0
659
711
  },
660
712
  providerMetadata
661
713
  });
@@ -667,68 +719,67 @@ var OpenAICompatibleChatLanguageModel = class {
667
719
  };
668
720
  }
669
721
  };
670
- var openaiCompatibleTokenUsageSchema = import_zod3.z.object({
671
- prompt_tokens: import_zod3.z.number().nullish(),
672
- completion_tokens: import_zod3.z.number().nullish(),
673
- prompt_tokens_details: import_zod3.z.object({
674
- cached_tokens: import_zod3.z.number().nullish()
722
+ var openaiCompatibleTokenUsageSchema = import_v43.z.object({
723
+ prompt_tokens: import_v43.z.number().nullish(),
724
+ completion_tokens: import_v43.z.number().nullish(),
725
+ total_tokens: import_v43.z.number().nullish(),
726
+ prompt_tokens_details: import_v43.z.object({
727
+ cached_tokens: import_v43.z.number().nullish()
675
728
  }).nullish(),
676
- completion_tokens_details: import_zod3.z.object({
677
- reasoning_tokens: import_zod3.z.number().nullish(),
678
- accepted_prediction_tokens: import_zod3.z.number().nullish(),
679
- rejected_prediction_tokens: import_zod3.z.number().nullish()
729
+ completion_tokens_details: import_v43.z.object({
730
+ reasoning_tokens: import_v43.z.number().nullish(),
731
+ accepted_prediction_tokens: import_v43.z.number().nullish(),
732
+ rejected_prediction_tokens: import_v43.z.number().nullish()
680
733
  }).nullish()
681
734
  }).nullish();
682
- var OpenAICompatibleChatResponseSchema = import_zod3.z.object({
683
- id: import_zod3.z.string().nullish(),
684
- created: import_zod3.z.number().nullish(),
685
- model: import_zod3.z.string().nullish(),
686
- choices: import_zod3.z.array(
687
- import_zod3.z.object({
688
- message: import_zod3.z.object({
689
- role: import_zod3.z.literal("assistant").nullish(),
690
- content: import_zod3.z.string().nullish(),
691
- reasoning_content: import_zod3.z.string().nullish(),
692
- tool_calls: import_zod3.z.array(
693
- import_zod3.z.object({
694
- id: import_zod3.z.string().nullish(),
695
- type: import_zod3.z.literal("function"),
696
- function: import_zod3.z.object({
697
- name: import_zod3.z.string(),
698
- arguments: import_zod3.z.string()
735
+ var OpenAICompatibleChatResponseSchema = import_v43.z.object({
736
+ id: import_v43.z.string().nullish(),
737
+ created: import_v43.z.number().nullish(),
738
+ model: import_v43.z.string().nullish(),
739
+ choices: import_v43.z.array(
740
+ import_v43.z.object({
741
+ message: import_v43.z.object({
742
+ role: import_v43.z.literal("assistant").nullish(),
743
+ content: import_v43.z.string().nullish(),
744
+ reasoning_content: import_v43.z.string().nullish(),
745
+ tool_calls: import_v43.z.array(
746
+ import_v43.z.object({
747
+ id: import_v43.z.string().nullish(),
748
+ function: import_v43.z.object({
749
+ name: import_v43.z.string(),
750
+ arguments: import_v43.z.string()
699
751
  })
700
752
  })
701
753
  ).nullish()
702
754
  }),
703
- finish_reason: import_zod3.z.string().nullish()
755
+ finish_reason: import_v43.z.string().nullish()
704
756
  })
705
757
  ),
706
758
  usage: openaiCompatibleTokenUsageSchema
707
759
  });
708
- var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_zod3.z.union([
709
- import_zod3.z.object({
710
- id: import_zod3.z.string().nullish(),
711
- created: import_zod3.z.number().nullish(),
712
- model: import_zod3.z.string().nullish(),
713
- choices: import_zod3.z.array(
714
- import_zod3.z.object({
715
- delta: import_zod3.z.object({
716
- role: import_zod3.z.enum(["assistant"]).nullish(),
717
- content: import_zod3.z.string().nullish(),
718
- reasoning_content: import_zod3.z.string().nullish(),
719
- tool_calls: import_zod3.z.array(
720
- import_zod3.z.object({
721
- index: import_zod3.z.number(),
722
- id: import_zod3.z.string().nullish(),
723
- type: import_zod3.z.literal("function").nullish(),
724
- function: import_zod3.z.object({
725
- name: import_zod3.z.string().nullish(),
726
- arguments: import_zod3.z.string().nullish()
760
+ var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_v43.z.union([
761
+ import_v43.z.object({
762
+ id: import_v43.z.string().nullish(),
763
+ created: import_v43.z.number().nullish(),
764
+ model: import_v43.z.string().nullish(),
765
+ choices: import_v43.z.array(
766
+ import_v43.z.object({
767
+ delta: import_v43.z.object({
768
+ role: import_v43.z.enum(["assistant"]).nullish(),
769
+ content: import_v43.z.string().nullish(),
770
+ reasoning_content: import_v43.z.string().nullish(),
771
+ tool_calls: import_v43.z.array(
772
+ import_v43.z.object({
773
+ index: import_v43.z.number(),
774
+ id: import_v43.z.string().nullish(),
775
+ function: import_v43.z.object({
776
+ name: import_v43.z.string().nullish(),
777
+ arguments: import_v43.z.string().nullish()
727
778
  })
728
779
  })
729
780
  ).nullish()
730
781
  }).nullish(),
731
- finish_reason: import_zod3.z.string().nullish()
782
+ finish_reason: import_v43.z.string().nullish()
732
783
  })
733
784
  ),
734
785
  usage: openaiCompatibleTokenUsageSchema
@@ -738,19 +789,15 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_zod3.z.union
738
789
 
739
790
  // src/openai-compatible-completion-language-model.ts
740
791
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
741
- var import_zod5 = require("zod");
792
+ var import_v45 = require("zod/v4");
742
793
 
743
794
  // src/convert-to-openai-compatible-completion-prompt.ts
744
795
  var import_provider4 = require("@ai-sdk/provider");
745
796
  function convertToOpenAICompatibleCompletionPrompt({
746
797
  prompt,
747
- inputFormat,
748
798
  user = "user",
749
799
  assistant = "assistant"
750
800
  }) {
751
- if (inputFormat === "prompt" && prompt.length === 1 && prompt[0].role === "user" && prompt[0].content.length === 1 && prompt[0].content[0].type === "text") {
752
- return { prompt: prompt[0].content[0].text };
753
- }
754
801
  let text = "";
755
802
  if (prompt[0].role === "system") {
756
803
  text += `${prompt[0].content}
@@ -820,28 +867,28 @@ ${user}:`]
820
867
  }
821
868
 
822
869
  // src/openai-compatible-completion-options.ts
823
- var import_zod4 = require("zod");
824
- var openaiCompatibleCompletionProviderOptions = import_zod4.z.object({
870
+ var import_v44 = require("zod/v4");
871
+ var openaiCompatibleCompletionProviderOptions = import_v44.z.object({
825
872
  /**
826
873
  * Echo back the prompt in addition to the completion.
827
874
  */
828
- echo: import_zod4.z.boolean().optional(),
875
+ echo: import_v44.z.boolean().optional(),
829
876
  /**
830
877
  * Modify the likelihood of specified tokens appearing in the completion.
831
878
  *
832
879
  * Accepts a JSON object that maps tokens (specified by their token ID in
833
880
  * the GPT tokenizer) to an associated bias value from -100 to 100.
834
881
  */
835
- logitBias: import_zod4.z.record(import_zod4.z.number(), import_zod4.z.number()).optional(),
882
+ logitBias: import_v44.z.record(import_v44.z.string(), import_v44.z.number()).optional(),
836
883
  /**
837
884
  * The suffix that comes after a completion of inserted text.
838
885
  */
839
- suffix: import_zod4.z.string().optional(),
886
+ suffix: import_v44.z.string().optional(),
840
887
  /**
841
888
  * A unique identifier representing your end-user, which can help providers to
842
889
  * monitor and detect abuse.
843
890
  */
844
- user: import_zod4.z.string().optional()
891
+ user: import_v44.z.string().optional()
845
892
  });
846
893
 
847
894
  // src/openai-compatible-completion-language-model.ts
@@ -864,12 +911,11 @@ var OpenAICompatibleCompletionLanguageModel = class {
864
911
  get providerOptionsName() {
865
912
  return this.config.provider.split(".")[0].trim();
866
913
  }
867
- async getSupportedUrls() {
914
+ get supportedUrls() {
868
915
  var _a, _b, _c;
869
- return (_c = (_b = (_a = this.config).getSupportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
916
+ return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
870
917
  }
871
- getArgs({
872
- inputFormat,
918
+ async getArgs({
873
919
  prompt,
874
920
  maxOutputTokens,
875
921
  temperature,
@@ -886,7 +932,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
886
932
  }) {
887
933
  var _a;
888
934
  const warnings = [];
889
- const completionOptions = (_a = (0, import_provider_utils2.parseProviderOptions)({
935
+ const completionOptions = (_a = await (0, import_provider_utils2.parseProviderOptions)({
890
936
  provider: this.providerOptionsName,
891
937
  providerOptions,
892
938
  schema: openaiCompatibleCompletionProviderOptions
@@ -907,7 +953,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
907
953
  details: "JSON response format is not supported."
908
954
  });
909
955
  }
910
- const { prompt: completionPrompt, stopSequences } = convertToOpenAICompatibleCompletionPrompt({ prompt, inputFormat });
956
+ const { prompt: completionPrompt, stopSequences } = convertToOpenAICompatibleCompletionPrompt({ prompt });
911
957
  const stop = [...stopSequences != null ? stopSequences : [], ...userStopSequences != null ? userStopSequences : []];
912
958
  return {
913
959
  args: {
@@ -935,8 +981,8 @@ var OpenAICompatibleCompletionLanguageModel = class {
935
981
  };
936
982
  }
937
983
  async doGenerate(options) {
938
- var _a, _b, _c, _d;
939
- const { args, warnings } = this.getArgs(options);
984
+ var _a, _b, _c, _d, _e, _f;
985
+ const { args, warnings } = await this.getArgs(options);
940
986
  const {
941
987
  responseHeaders,
942
988
  value: response,
@@ -964,7 +1010,8 @@ var OpenAICompatibleCompletionLanguageModel = class {
964
1010
  content,
965
1011
  usage: {
966
1012
  inputTokens: (_b = (_a = response.usage) == null ? void 0 : _a.prompt_tokens) != null ? _b : void 0,
967
- outputTokens: (_d = (_c = response.usage) == null ? void 0 : _c.completion_tokens) != null ? _d : void 0
1013
+ outputTokens: (_d = (_c = response.usage) == null ? void 0 : _c.completion_tokens) != null ? _d : void 0,
1014
+ totalTokens: (_f = (_e = response.usage) == null ? void 0 : _e.total_tokens) != null ? _f : void 0
968
1015
  },
969
1016
  finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
970
1017
  request: { body: args },
@@ -977,10 +1024,12 @@ var OpenAICompatibleCompletionLanguageModel = class {
977
1024
  };
978
1025
  }
979
1026
  async doStream(options) {
980
- const { args, warnings } = this.getArgs(options);
1027
+ const { args, warnings } = await this.getArgs(options);
981
1028
  const body = {
982
1029
  ...args,
983
- stream: true
1030
+ stream: true,
1031
+ // only include stream_options when in strict compatibility mode:
1032
+ stream_options: this.config.includeUsage ? { include_usage: true } : void 0
984
1033
  };
985
1034
  const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
986
1035
  url: this.config.url({
@@ -999,7 +1048,8 @@ var OpenAICompatibleCompletionLanguageModel = class {
999
1048
  let finishReason = "unknown";
1000
1049
  const usage = {
1001
1050
  inputTokens: void 0,
1002
- outputTokens: void 0
1051
+ outputTokens: void 0,
1052
+ totalTokens: void 0
1003
1053
  };
1004
1054
  let isFirstChunk = true;
1005
1055
  return {
@@ -1009,7 +1059,10 @@ var OpenAICompatibleCompletionLanguageModel = class {
1009
1059
  controller.enqueue({ type: "stream-start", warnings });
1010
1060
  },
1011
1061
  transform(chunk, controller) {
1012
- var _a, _b;
1062
+ var _a, _b, _c;
1063
+ if (options.includeRawChunks) {
1064
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1065
+ }
1013
1066
  if (!chunk.success) {
1014
1067
  finishReason = "error";
1015
1068
  controller.enqueue({ type: "error", error: chunk.error });
@@ -1027,10 +1080,15 @@ var OpenAICompatibleCompletionLanguageModel = class {
1027
1080
  type: "response-metadata",
1028
1081
  ...getResponseMetadata(value)
1029
1082
  });
1083
+ controller.enqueue({
1084
+ type: "text-start",
1085
+ id: "0"
1086
+ });
1030
1087
  }
1031
1088
  if (value.usage != null) {
1032
1089
  usage.inputTokens = (_a = value.usage.prompt_tokens) != null ? _a : void 0;
1033
1090
  usage.outputTokens = (_b = value.usage.completion_tokens) != null ? _b : void 0;
1091
+ usage.totalTokens = (_c = value.usage.total_tokens) != null ? _c : void 0;
1034
1092
  }
1035
1093
  const choice = value.choices[0];
1036
1094
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
@@ -1040,12 +1098,16 @@ var OpenAICompatibleCompletionLanguageModel = class {
1040
1098
  }
1041
1099
  if ((choice == null ? void 0 : choice.text) != null) {
1042
1100
  controller.enqueue({
1043
- type: "text",
1044
- text: choice.text
1101
+ type: "text-delta",
1102
+ id: "0",
1103
+ delta: choice.text
1045
1104
  });
1046
1105
  }
1047
1106
  },
1048
1107
  flush(controller) {
1108
+ if (!isFirstChunk) {
1109
+ controller.enqueue({ type: "text-end", id: "0" });
1110
+ }
1049
1111
  controller.enqueue({
1050
1112
  type: "finish",
1051
1113
  finishReason,
@@ -1059,37 +1121,36 @@ var OpenAICompatibleCompletionLanguageModel = class {
1059
1121
  };
1060
1122
  }
1061
1123
  };
1062
- var openaiCompatibleCompletionResponseSchema = import_zod5.z.object({
1063
- id: import_zod5.z.string().nullish(),
1064
- created: import_zod5.z.number().nullish(),
1065
- model: import_zod5.z.string().nullish(),
1066
- choices: import_zod5.z.array(
1067
- import_zod5.z.object({
1068
- text: import_zod5.z.string(),
1069
- finish_reason: import_zod5.z.string()
1124
+ var usageSchema = import_v45.z.object({
1125
+ prompt_tokens: import_v45.z.number(),
1126
+ completion_tokens: import_v45.z.number(),
1127
+ total_tokens: import_v45.z.number()
1128
+ });
1129
+ var openaiCompatibleCompletionResponseSchema = import_v45.z.object({
1130
+ id: import_v45.z.string().nullish(),
1131
+ created: import_v45.z.number().nullish(),
1132
+ model: import_v45.z.string().nullish(),
1133
+ choices: import_v45.z.array(
1134
+ import_v45.z.object({
1135
+ text: import_v45.z.string(),
1136
+ finish_reason: import_v45.z.string()
1070
1137
  })
1071
1138
  ),
1072
- usage: import_zod5.z.object({
1073
- prompt_tokens: import_zod5.z.number(),
1074
- completion_tokens: import_zod5.z.number()
1075
- }).nullish()
1139
+ usage: usageSchema.nullish()
1076
1140
  });
1077
- var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => import_zod5.z.union([
1078
- import_zod5.z.object({
1079
- id: import_zod5.z.string().nullish(),
1080
- created: import_zod5.z.number().nullish(),
1081
- model: import_zod5.z.string().nullish(),
1082
- choices: import_zod5.z.array(
1083
- import_zod5.z.object({
1084
- text: import_zod5.z.string(),
1085
- finish_reason: import_zod5.z.string().nullish(),
1086
- index: import_zod5.z.number()
1141
+ var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => import_v45.z.union([
1142
+ import_v45.z.object({
1143
+ id: import_v45.z.string().nullish(),
1144
+ created: import_v45.z.number().nullish(),
1145
+ model: import_v45.z.string().nullish(),
1146
+ choices: import_v45.z.array(
1147
+ import_v45.z.object({
1148
+ text: import_v45.z.string(),
1149
+ finish_reason: import_v45.z.string().nullish(),
1150
+ index: import_v45.z.number()
1087
1151
  })
1088
1152
  ),
1089
- usage: import_zod5.z.object({
1090
- prompt_tokens: import_zod5.z.number(),
1091
- completion_tokens: import_zod5.z.number()
1092
- }).nullish()
1153
+ usage: usageSchema.nullish()
1093
1154
  }),
1094
1155
  errorSchema
1095
1156
  ]);
@@ -1097,21 +1158,21 @@ var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => import_zod5.z
1097
1158
  // src/openai-compatible-embedding-model.ts
1098
1159
  var import_provider5 = require("@ai-sdk/provider");
1099
1160
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
1100
- var import_zod7 = require("zod");
1161
+ var import_v47 = require("zod/v4");
1101
1162
 
1102
1163
  // src/openai-compatible-embedding-options.ts
1103
- var import_zod6 = require("zod");
1104
- var openaiCompatibleEmbeddingProviderOptions = import_zod6.z.object({
1164
+ var import_v46 = require("zod/v4");
1165
+ var openaiCompatibleEmbeddingProviderOptions = import_v46.z.object({
1105
1166
  /**
1106
1167
  * The number of dimensions the resulting output embeddings should have.
1107
1168
  * Only supported in text-embedding-3 and later models.
1108
1169
  */
1109
- dimensions: import_zod6.z.number().optional(),
1170
+ dimensions: import_v46.z.number().optional(),
1110
1171
  /**
1111
1172
  * A unique identifier representing your end-user, which can help providers to
1112
1173
  * monitor and detect abuse.
1113
1174
  */
1114
- user: import_zod6.z.string().optional()
1175
+ user: import_v46.z.string().optional()
1115
1176
  });
1116
1177
 
1117
1178
  // src/openai-compatible-embedding-model.ts
@@ -1143,12 +1204,12 @@ var OpenAICompatibleEmbeddingModel = class {
1143
1204
  }) {
1144
1205
  var _a, _b, _c;
1145
1206
  const compatibleOptions = Object.assign(
1146
- (_a = (0, import_provider_utils3.parseProviderOptions)({
1207
+ (_a = await (0, import_provider_utils3.parseProviderOptions)({
1147
1208
  provider: "openai-compatible",
1148
1209
  providerOptions,
1149
1210
  schema: openaiCompatibleEmbeddingProviderOptions
1150
1211
  })) != null ? _a : {},
1151
- (_b = (0, import_provider_utils3.parseProviderOptions)({
1212
+ (_b = await (0, import_provider_utils3.parseProviderOptions)({
1152
1213
  provider: this.providerOptionsName,
1153
1214
  providerOptions,
1154
1215
  schema: openaiCompatibleEmbeddingProviderOptions
@@ -1195,24 +1256,20 @@ var OpenAICompatibleEmbeddingModel = class {
1195
1256
  };
1196
1257
  }
1197
1258
  };
1198
- var openaiTextEmbeddingResponseSchema = import_zod7.z.object({
1199
- data: import_zod7.z.array(import_zod7.z.object({ embedding: import_zod7.z.array(import_zod7.z.number()) })),
1200
- usage: import_zod7.z.object({ prompt_tokens: import_zod7.z.number() }).nullish()
1259
+ var openaiTextEmbeddingResponseSchema = import_v47.z.object({
1260
+ data: import_v47.z.array(import_v47.z.object({ embedding: import_v47.z.array(import_v47.z.number()) })),
1261
+ usage: import_v47.z.object({ prompt_tokens: import_v47.z.number() }).nullish()
1201
1262
  });
1202
1263
 
1203
1264
  // src/openai-compatible-image-model.ts
1204
1265
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
1205
- var import_zod8 = require("zod");
1266
+ var import_v48 = require("zod/v4");
1206
1267
  var OpenAICompatibleImageModel = class {
1207
- constructor(modelId, settings, config) {
1268
+ constructor(modelId, config) {
1208
1269
  this.modelId = modelId;
1209
- this.settings = settings;
1210
1270
  this.config = config;
1211
- this.specificationVersion = "v1";
1212
- }
1213
- get maxImagesPerCall() {
1214
- var _a;
1215
- return (_a = this.settings.maxImagesPerCall) != null ? _a : 10;
1271
+ this.specificationVersion = "v2";
1272
+ this.maxImagesPerCall = 10;
1216
1273
  }
1217
1274
  get provider() {
1218
1275
  return this.config.provider;
@@ -1252,8 +1309,7 @@ var OpenAICompatibleImageModel = class {
1252
1309
  n,
1253
1310
  size,
1254
1311
  ...(_d = providerOptions.openai) != null ? _d : {},
1255
- response_format: "b64_json",
1256
- ...this.settings.user ? { user: this.settings.user } : {}
1312
+ response_format: "b64_json"
1257
1313
  },
1258
1314
  failedResponseHandler: (0, import_provider_utils4.createJsonErrorResponseHandler)(
1259
1315
  (_e = this.config.errorStructure) != null ? _e : defaultOpenAICompatibleErrorStructure
@@ -1275,8 +1331,8 @@ var OpenAICompatibleImageModel = class {
1275
1331
  };
1276
1332
  }
1277
1333
  };
1278
- var openaiCompatibleImageResponseSchema = import_zod8.z.object({
1279
- data: import_zod8.z.array(import_zod8.z.object({ b64_json: import_zod8.z.string() }))
1334
+ var openaiCompatibleImageResponseSchema = import_v48.z.object({
1335
+ data: import_v48.z.array(import_v48.z.object({ b64_json: import_v48.z.string() }))
1280
1336
  });
1281
1337
 
1282
1338
  // src/openai-compatible-provider.ts
@@ -1301,22 +1357,18 @@ function createOpenAICompatible(options) {
1301
1357
  fetch: options.fetch
1302
1358
  });
1303
1359
  const createLanguageModel = (modelId) => createChatModel(modelId);
1304
- const createChatModel = (modelId) => new OpenAICompatibleChatLanguageModel(
1305
- modelId,
1306
- getCommonModelConfig("chat")
1307
- );
1308
- const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(
1309
- modelId,
1310
- getCommonModelConfig("completion")
1311
- );
1360
+ const createChatModel = (modelId) => new OpenAICompatibleChatLanguageModel(modelId, {
1361
+ ...getCommonModelConfig("chat"),
1362
+ includeUsage: options.includeUsage
1363
+ });
1364
+ const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(modelId, {
1365
+ ...getCommonModelConfig("completion"),
1366
+ includeUsage: options.includeUsage
1367
+ });
1312
1368
  const createEmbeddingModel = (modelId) => new OpenAICompatibleEmbeddingModel(modelId, {
1313
1369
  ...getCommonModelConfig("embedding")
1314
1370
  });
1315
- const createImageModel = (modelId, settings = {}) => new OpenAICompatibleImageModel(
1316
- modelId,
1317
- settings,
1318
- getCommonModelConfig("image")
1319
- );
1371
+ const createImageModel = (modelId) => new OpenAICompatibleImageModel(modelId, getCommonModelConfig("image"));
1320
1372
  const provider = (modelId) => createLanguageModel(modelId);
1321
1373
  provider.languageModel = createLanguageModel;
1322
1374
  provider.chatModel = createChatModel;