@ai-sdk/openai-compatible 1.0.0-canary.8 → 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.mjs CHANGED
@@ -12,7 +12,7 @@ import {
12
12
  parseProviderOptions,
13
13
  postJsonToApi
14
14
  } from "@ai-sdk/provider-utils";
15
- import { z as z3 } from "zod";
15
+ import { z as z3 } from "zod/v4";
16
16
 
17
17
  // src/convert-to-openai-compatible-chat-messages.ts
18
18
  import {
@@ -86,7 +86,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
86
86
  type: "function",
87
87
  function: {
88
88
  name: part.toolName,
89
- arguments: JSON.stringify(part.args)
89
+ arguments: JSON.stringify(part.input)
90
90
  },
91
91
  ...partMetadata
92
92
  });
@@ -104,11 +104,24 @@ function convertToOpenAICompatibleChatMessages(prompt) {
104
104
  }
105
105
  case "tool": {
106
106
  for (const toolResponse of content) {
107
+ const output = toolResponse.output;
108
+ let contentValue;
109
+ switch (output.type) {
110
+ case "text":
111
+ case "error-text":
112
+ contentValue = output.value;
113
+ break;
114
+ case "content":
115
+ case "json":
116
+ case "error-json":
117
+ contentValue = JSON.stringify(output.value);
118
+ break;
119
+ }
107
120
  const toolResponseMetadata = getOpenAIMetadata(toolResponse);
108
121
  messages.push({
109
122
  role: "tool",
110
123
  tool_call_id: toolResponse.toolCallId,
111
- content: JSON.stringify(toolResponse.result),
124
+ content: contentValue,
112
125
  ...toolResponseMetadata
113
126
  });
114
127
  }
@@ -154,17 +167,21 @@ function mapOpenAICompatibleFinishReason(finishReason) {
154
167
  }
155
168
 
156
169
  // src/openai-compatible-chat-options.ts
157
- import { z } from "zod";
170
+ import { z } from "zod/v4";
158
171
  var openaiCompatibleProviderOptions = z.object({
159
172
  /**
160
173
  * A unique identifier representing your end-user, which can help the provider to
161
174
  * monitor and detect abuse.
162
175
  */
163
- user: z.string().optional()
176
+ user: z.string().optional(),
177
+ /**
178
+ * Reasoning effort for reasoning models. Defaults to `medium`.
179
+ */
180
+ reasoningEffort: z.string().optional()
164
181
  });
165
182
 
166
183
  // src/openai-compatible-error.ts
167
- import { z as z2 } from "zod";
184
+ import { z as z2 } from "zod/v4";
168
185
  var openaiCompatibleErrorDataSchema = z2.object({
169
186
  error: z2.object({
170
187
  message: z2.string(),
@@ -204,7 +221,7 @@ function prepareTools({
204
221
  function: {
205
222
  name: tool.name,
206
223
  description: tool.description,
207
- parameters: tool.parameters
224
+ parameters: tool.inputSchema
208
225
  }
209
226
  });
210
227
  }
@@ -251,16 +268,17 @@ var OpenAICompatibleChatLanguageModel = class {
251
268
  this.failedResponseHandler = createJsonErrorResponseHandler(errorStructure);
252
269
  this.supportsStructuredOutputs = (_b = config.supportsStructuredOutputs) != null ? _b : false;
253
270
  }
254
- get defaultObjectGenerationMode() {
255
- return this.config.defaultObjectGenerationMode;
256
- }
257
271
  get provider() {
258
272
  return this.config.provider;
259
273
  }
260
274
  get providerOptionsName() {
261
275
  return this.config.provider.split(".")[0].trim();
262
276
  }
263
- getArgs({
277
+ get supportedUrls() {
278
+ var _a, _b, _c;
279
+ return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
280
+ }
281
+ async getArgs({
264
282
  prompt,
265
283
  maxOutputTokens,
266
284
  temperature,
@@ -278,12 +296,12 @@ var OpenAICompatibleChatLanguageModel = class {
278
296
  var _a, _b, _c;
279
297
  const warnings = [];
280
298
  const compatibleOptions = Object.assign(
281
- (_a = parseProviderOptions({
299
+ (_a = await parseProviderOptions({
282
300
  provider: "openai-compatible",
283
301
  providerOptions,
284
302
  schema: openaiCompatibleProviderOptions
285
303
  })) != null ? _a : {},
286
- (_b = parseProviderOptions({
304
+ (_b = await parseProviderOptions({
287
305
  provider: this.providerOptionsName,
288
306
  providerOptions,
289
307
  schema: openaiCompatibleProviderOptions
@@ -330,6 +348,7 @@ var OpenAICompatibleChatLanguageModel = class {
330
348
  stop: stopSequences,
331
349
  seed,
332
350
  ...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
351
+ reasoning_effort: compatibleOptions.reasoningEffort,
333
352
  // messages:
334
353
  messages: convertToOpenAICompatibleChatMessages(prompt),
335
354
  // tools:
@@ -340,8 +359,8 @@ var OpenAICompatibleChatLanguageModel = class {
340
359
  };
341
360
  }
342
361
  async doGenerate(options) {
343
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
344
- const { args, warnings } = this.getArgs({ ...options });
362
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
363
+ const { args, warnings } = await this.getArgs({ ...options });
345
364
  const body = JSON.stringify(args);
346
365
  const {
347
366
  responseHeaders,
@@ -371,7 +390,6 @@ var OpenAICompatibleChatLanguageModel = class {
371
390
  if (reasoning != null && reasoning.length > 0) {
372
391
  content.push({
373
392
  type: "reasoning",
374
- reasoningType: "text",
375
393
  text: reasoning
376
394
  });
377
395
  }
@@ -379,39 +397,34 @@ var OpenAICompatibleChatLanguageModel = class {
379
397
  for (const toolCall of choice.message.tool_calls) {
380
398
  content.push({
381
399
  type: "tool-call",
382
- toolCallType: "function",
383
400
  toolCallId: (_a = toolCall.id) != null ? _a : generateId(),
384
401
  toolName: toolCall.function.name,
385
- args: toolCall.function.arguments
402
+ input: toolCall.function.arguments
386
403
  });
387
404
  }
388
405
  }
389
406
  const providerMetadata = {
390
407
  [this.providerOptionsName]: {},
391
- ...(_c = (_b = this.config.metadataExtractor) == null ? void 0 : _b.extractMetadata) == null ? void 0 : _c.call(_b, {
408
+ ...await ((_c = (_b = this.config.metadataExtractor) == null ? void 0 : _b.extractMetadata) == null ? void 0 : _c.call(_b, {
392
409
  parsedBody: rawResponse
393
- })
410
+ }))
394
411
  };
395
412
  const completionTokenDetails = (_d = responseBody.usage) == null ? void 0 : _d.completion_tokens_details;
396
- const promptTokenDetails = (_e = responseBody.usage) == null ? void 0 : _e.prompt_tokens_details;
397
- if ((completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens) != null) {
398
- providerMetadata[this.providerOptionsName].reasoningTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens;
399
- }
400
413
  if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
401
414
  providerMetadata[this.providerOptionsName].acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
402
415
  }
403
416
  if ((completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens) != null) {
404
417
  providerMetadata[this.providerOptionsName].rejectedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens;
405
418
  }
406
- if ((promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens) != null) {
407
- providerMetadata[this.providerOptionsName].cachedPromptTokens = promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens;
408
- }
409
419
  return {
410
420
  content,
411
421
  finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
412
422
  usage: {
413
- inputTokens: (_g = (_f = responseBody.usage) == null ? void 0 : _f.prompt_tokens) != null ? _g : void 0,
414
- outputTokens: (_i = (_h = responseBody.usage) == null ? void 0 : _h.completion_tokens) != null ? _i : void 0
423
+ inputTokens: (_f = (_e = responseBody.usage) == null ? void 0 : _e.prompt_tokens) != null ? _f : void 0,
424
+ outputTokens: (_h = (_g = responseBody.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : void 0,
425
+ totalTokens: (_j = (_i = responseBody.usage) == null ? void 0 : _i.total_tokens) != null ? _j : void 0,
426
+ reasoningTokens: (_m = (_l = (_k = responseBody.usage) == null ? void 0 : _k.completion_tokens_details) == null ? void 0 : _l.reasoning_tokens) != null ? _m : void 0,
427
+ cachedInputTokens: (_p = (_o = (_n = responseBody.usage) == null ? void 0 : _n.prompt_tokens_details) == null ? void 0 : _o.cached_tokens) != null ? _p : void 0
415
428
  },
416
429
  providerMetadata,
417
430
  request: { body },
@@ -425,8 +438,13 @@ var OpenAICompatibleChatLanguageModel = class {
425
438
  }
426
439
  async doStream(options) {
427
440
  var _a;
428
- const { args, warnings } = this.getArgs({ ...options });
429
- const body = JSON.stringify({ ...args, stream: true });
441
+ const { args, warnings } = await this.getArgs({ ...options });
442
+ const body = {
443
+ ...args,
444
+ stream: true,
445
+ // only include stream_options when in strict compatibility mode:
446
+ stream_options: this.config.includeUsage ? { include_usage: true } : void 0
447
+ };
430
448
  const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
431
449
  const { responseHeaders, value: response } = await postJsonToApi({
432
450
  url: this.config.url({
@@ -434,10 +452,7 @@ var OpenAICompatibleChatLanguageModel = class {
434
452
  modelId: this.modelId
435
453
  }),
436
454
  headers: combineHeaders(this.config.headers(), options.headers),
437
- body: {
438
- ...args,
439
- stream: true
440
- },
455
+ body,
441
456
  failedResponseHandler: this.failedResponseHandler,
442
457
  successfulResponseHandler: createEventSourceResponseHandler(
443
458
  this.chunkSchema
@@ -447,7 +462,7 @@ var OpenAICompatibleChatLanguageModel = class {
447
462
  });
448
463
  const toolCalls = [];
449
464
  let finishReason = "unknown";
450
- let usage = {
465
+ const usage = {
451
466
  completionTokens: void 0,
452
467
  completionTokensDetails: {
453
468
  reasoningTokens: void 0,
@@ -457,10 +472,13 @@ var OpenAICompatibleChatLanguageModel = class {
457
472
  promptTokens: void 0,
458
473
  promptTokensDetails: {
459
474
  cachedTokens: void 0
460
- }
475
+ },
476
+ totalTokens: void 0
461
477
  };
462
478
  let isFirstChunk = true;
463
- let providerOptionsName = this.providerOptionsName;
479
+ const providerOptionsName = this.providerOptionsName;
480
+ let isActiveReasoning = false;
481
+ let isActiveText = false;
464
482
  return {
465
483
  stream: response.pipeThrough(
466
484
  new TransformStream({
@@ -470,6 +488,9 @@ var OpenAICompatibleChatLanguageModel = class {
470
488
  // TODO we lost type safety on Chunk, most likely due to the error schema. MUST FIX
471
489
  transform(chunk, controller) {
472
490
  var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
491
+ if (options.includeRawChunks) {
492
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
493
+ }
473
494
  if (!chunk.success) {
474
495
  finishReason = "error";
475
496
  controller.enqueue({ type: "error", error: chunk.error });
@@ -493,11 +514,13 @@ var OpenAICompatibleChatLanguageModel = class {
493
514
  const {
494
515
  prompt_tokens,
495
516
  completion_tokens,
517
+ total_tokens,
496
518
  prompt_tokens_details,
497
519
  completion_tokens_details
498
520
  } = value.usage;
499
521
  usage.promptTokens = prompt_tokens != null ? prompt_tokens : void 0;
500
522
  usage.completionTokens = completion_tokens != null ? completion_tokens : void 0;
523
+ usage.totalTokens = total_tokens != null ? total_tokens : void 0;
501
524
  if ((completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens) != null) {
502
525
  usage.completionTokensDetails.reasoningTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens;
503
526
  }
@@ -522,28 +545,34 @@ var OpenAICompatibleChatLanguageModel = class {
522
545
  }
523
546
  const delta = choice.delta;
524
547
  if (delta.reasoning_content != null) {
548
+ if (!isActiveReasoning) {
549
+ controller.enqueue({
550
+ type: "reasoning-start",
551
+ id: "reasoning-0"
552
+ });
553
+ isActiveReasoning = true;
554
+ }
525
555
  controller.enqueue({
526
- type: "reasoning",
527
- reasoningType: "text",
528
- text: delta.reasoning_content
556
+ type: "reasoning-delta",
557
+ id: "reasoning-0",
558
+ delta: delta.reasoning_content
529
559
  });
530
560
  }
531
561
  if (delta.content != null) {
562
+ if (!isActiveText) {
563
+ controller.enqueue({ type: "text-start", id: "txt-0" });
564
+ isActiveText = true;
565
+ }
532
566
  controller.enqueue({
533
- type: "text",
534
- text: delta.content
567
+ type: "text-delta",
568
+ id: "txt-0",
569
+ delta: delta.content
535
570
  });
536
571
  }
537
572
  if (delta.tool_calls != null) {
538
573
  for (const toolCallDelta of delta.tool_calls) {
539
574
  const index = toolCallDelta.index;
540
575
  if (toolCalls[index] == null) {
541
- if (toolCallDelta.type !== "function") {
542
- throw new InvalidResponseDataError({
543
- data: toolCallDelta,
544
- message: `Expected 'function' type.`
545
- });
546
- }
547
576
  if (toolCallDelta.id == null) {
548
577
  throw new InvalidResponseDataError({
549
578
  data: toolCallDelta,
@@ -556,6 +585,11 @@ var OpenAICompatibleChatLanguageModel = class {
556
585
  message: `Expected 'function.name' to be a string.`
557
586
  });
558
587
  }
588
+ controller.enqueue({
589
+ type: "tool-input-start",
590
+ id: toolCallDelta.id,
591
+ toolName: toolCallDelta.function.name
592
+ });
559
593
  toolCalls[index] = {
560
594
  id: toolCallDelta.id,
561
595
  type: "function",
@@ -569,20 +603,21 @@ var OpenAICompatibleChatLanguageModel = class {
569
603
  if (((_c = toolCall2.function) == null ? void 0 : _c.name) != null && ((_d = toolCall2.function) == null ? void 0 : _d.arguments) != null) {
570
604
  if (toolCall2.function.arguments.length > 0) {
571
605
  controller.enqueue({
572
- type: "tool-call-delta",
573
- toolCallType: "function",
574
- toolCallId: toolCall2.id,
575
- toolName: toolCall2.function.name,
576
- argsTextDelta: toolCall2.function.arguments
606
+ type: "tool-input-start",
607
+ id: toolCall2.id,
608
+ toolName: toolCall2.function.name
577
609
  });
578
610
  }
579
611
  if (isParsableJson(toolCall2.function.arguments)) {
612
+ controller.enqueue({
613
+ type: "tool-input-end",
614
+ id: toolCall2.id
615
+ });
580
616
  controller.enqueue({
581
617
  type: "tool-call",
582
- toolCallType: "function",
583
618
  toolCallId: (_e = toolCall2.id) != null ? _e : generateId(),
584
619
  toolName: toolCall2.function.name,
585
- args: toolCall2.function.arguments
620
+ input: toolCall2.function.arguments
586
621
  });
587
622
  toolCall2.hasFinished = true;
588
623
  }
@@ -597,19 +632,20 @@ var OpenAICompatibleChatLanguageModel = class {
597
632
  toolCall.function.arguments += (_h = (_g = toolCallDelta.function) == null ? void 0 : _g.arguments) != null ? _h : "";
598
633
  }
599
634
  controller.enqueue({
600
- type: "tool-call-delta",
601
- toolCallType: "function",
602
- toolCallId: toolCall.id,
603
- toolName: toolCall.function.name,
604
- argsTextDelta: (_i = toolCallDelta.function.arguments) != null ? _i : ""
635
+ type: "tool-input-delta",
636
+ id: toolCall.id,
637
+ delta: (_i = toolCallDelta.function.arguments) != null ? _i : ""
605
638
  });
606
639
  if (((_j = toolCall.function) == null ? void 0 : _j.name) != null && ((_k = toolCall.function) == null ? void 0 : _k.arguments) != null && isParsableJson(toolCall.function.arguments)) {
640
+ controller.enqueue({
641
+ type: "tool-input-end",
642
+ id: toolCall.id
643
+ });
607
644
  controller.enqueue({
608
645
  type: "tool-call",
609
- toolCallType: "function",
610
646
  toolCallId: (_l = toolCall.id) != null ? _l : generateId(),
611
647
  toolName: toolCall.function.name,
612
- args: toolCall.function.arguments
648
+ input: toolCall.function.arguments
613
649
  });
614
650
  toolCall.hasFinished = true;
615
651
  }
@@ -617,29 +653,46 @@ var OpenAICompatibleChatLanguageModel = class {
617
653
  }
618
654
  },
619
655
  flush(controller) {
620
- var _a2, _b;
656
+ var _a2, _b, _c, _d, _e, _f;
657
+ if (isActiveReasoning) {
658
+ controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
659
+ }
660
+ if (isActiveText) {
661
+ controller.enqueue({ type: "text-end", id: "txt-0" });
662
+ }
663
+ for (const toolCall of toolCalls.filter(
664
+ (toolCall2) => !toolCall2.hasFinished
665
+ )) {
666
+ controller.enqueue({
667
+ type: "tool-input-end",
668
+ id: toolCall.id
669
+ });
670
+ controller.enqueue({
671
+ type: "tool-call",
672
+ toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
673
+ toolName: toolCall.function.name,
674
+ input: toolCall.function.arguments
675
+ });
676
+ }
621
677
  const providerMetadata = {
622
678
  [providerOptionsName]: {},
623
679
  ...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
624
680
  };
625
- if (usage.completionTokensDetails.reasoningTokens != null) {
626
- providerMetadata[providerOptionsName].reasoningTokens = usage.completionTokensDetails.reasoningTokens;
627
- }
628
681
  if (usage.completionTokensDetails.acceptedPredictionTokens != null) {
629
682
  providerMetadata[providerOptionsName].acceptedPredictionTokens = usage.completionTokensDetails.acceptedPredictionTokens;
630
683
  }
631
684
  if (usage.completionTokensDetails.rejectedPredictionTokens != null) {
632
685
  providerMetadata[providerOptionsName].rejectedPredictionTokens = usage.completionTokensDetails.rejectedPredictionTokens;
633
686
  }
634
- if (usage.promptTokensDetails.cachedTokens != null) {
635
- providerMetadata[providerOptionsName].cachedPromptTokens = usage.promptTokensDetails.cachedTokens;
636
- }
637
687
  controller.enqueue({
638
688
  type: "finish",
639
689
  finishReason,
640
690
  usage: {
641
- inputTokens: (_a2 = usage.promptTokens) != null ? _a2 : void 0,
642
- outputTokens: (_b = usage.completionTokens) != null ? _b : void 0
691
+ inputTokens: (_b = usage.promptTokens) != null ? _b : void 0,
692
+ outputTokens: (_c = usage.completionTokens) != null ? _c : void 0,
693
+ totalTokens: (_d = usage.totalTokens) != null ? _d : void 0,
694
+ reasoningTokens: (_e = usage.completionTokensDetails.reasoningTokens) != null ? _e : void 0,
695
+ cachedInputTokens: (_f = usage.promptTokensDetails.cachedTokens) != null ? _f : void 0
643
696
  },
644
697
  providerMetadata
645
698
  });
@@ -654,6 +707,7 @@ var OpenAICompatibleChatLanguageModel = class {
654
707
  var openaiCompatibleTokenUsageSchema = z3.object({
655
708
  prompt_tokens: z3.number().nullish(),
656
709
  completion_tokens: z3.number().nullish(),
710
+ total_tokens: z3.number().nullish(),
657
711
  prompt_tokens_details: z3.object({
658
712
  cached_tokens: z3.number().nullish()
659
713
  }).nullish(),
@@ -676,7 +730,6 @@ var OpenAICompatibleChatResponseSchema = z3.object({
676
730
  tool_calls: z3.array(
677
731
  z3.object({
678
732
  id: z3.string().nullish(),
679
- type: z3.literal("function"),
680
733
  function: z3.object({
681
734
  name: z3.string(),
682
735
  arguments: z3.string()
@@ -704,7 +757,6 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => z3.union([
704
757
  z3.object({
705
758
  index: z3.number(),
706
759
  id: z3.string().nullish(),
707
- type: z3.literal("function").nullish(),
708
760
  function: z3.object({
709
761
  name: z3.string().nullish(),
710
762
  arguments: z3.string().nullish()
@@ -729,7 +781,7 @@ import {
729
781
  parseProviderOptions as parseProviderOptions2,
730
782
  postJsonToApi as postJsonToApi2
731
783
  } from "@ai-sdk/provider-utils";
732
- import { z as z5 } from "zod";
784
+ import { z as z5 } from "zod/v4";
733
785
 
734
786
  // src/convert-to-openai-compatible-completion-prompt.ts
735
787
  import {
@@ -738,13 +790,9 @@ import {
738
790
  } from "@ai-sdk/provider";
739
791
  function convertToOpenAICompatibleCompletionPrompt({
740
792
  prompt,
741
- inputFormat,
742
793
  user = "user",
743
794
  assistant = "assistant"
744
795
  }) {
745
- if (inputFormat === "prompt" && prompt.length === 1 && prompt[0].role === "user" && prompt[0].content.length === 1 && prompt[0].content[0].type === "text") {
746
- return { prompt: prompt[0].content[0].text };
747
- }
748
796
  let text = "";
749
797
  if (prompt[0].role === "system") {
750
798
  text += `${prompt[0].content}
@@ -814,7 +862,7 @@ ${user}:`]
814
862
  }
815
863
 
816
864
  // src/openai-compatible-completion-options.ts
817
- import { z as z4 } from "zod";
865
+ import { z as z4 } from "zod/v4";
818
866
  var openaiCompatibleCompletionProviderOptions = z4.object({
819
867
  /**
820
868
  * Echo back the prompt in addition to the completion.
@@ -826,7 +874,7 @@ var openaiCompatibleCompletionProviderOptions = z4.object({
826
874
  * Accepts a JSON object that maps tokens (specified by their token ID in
827
875
  * the GPT tokenizer) to an associated bias value from -100 to 100.
828
876
  */
829
- logitBias: z4.record(z4.number(), z4.number()).optional(),
877
+ logitBias: z4.record(z4.string(), z4.number()).optional(),
830
878
  /**
831
879
  * The suffix that comes after a completion of inserted text.
832
880
  */
@@ -843,7 +891,6 @@ var OpenAICompatibleCompletionLanguageModel = class {
843
891
  // type inferred via constructor
844
892
  constructor(modelId, config) {
845
893
  this.specificationVersion = "v2";
846
- this.defaultObjectGenerationMode = void 0;
847
894
  var _a;
848
895
  this.modelId = modelId;
849
896
  this.config = config;
@@ -859,8 +906,11 @@ var OpenAICompatibleCompletionLanguageModel = class {
859
906
  get providerOptionsName() {
860
907
  return this.config.provider.split(".")[0].trim();
861
908
  }
862
- getArgs({
863
- inputFormat,
909
+ get supportedUrls() {
910
+ var _a, _b, _c;
911
+ return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
912
+ }
913
+ async getArgs({
864
914
  prompt,
865
915
  maxOutputTokens,
866
916
  temperature,
@@ -877,7 +927,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
877
927
  }) {
878
928
  var _a;
879
929
  const warnings = [];
880
- const completionOptions = (_a = parseProviderOptions2({
930
+ const completionOptions = (_a = await parseProviderOptions2({
881
931
  provider: this.providerOptionsName,
882
932
  providerOptions,
883
933
  schema: openaiCompatibleCompletionProviderOptions
@@ -898,7 +948,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
898
948
  details: "JSON response format is not supported."
899
949
  });
900
950
  }
901
- const { prompt: completionPrompt, stopSequences } = convertToOpenAICompatibleCompletionPrompt({ prompt, inputFormat });
951
+ const { prompt: completionPrompt, stopSequences } = convertToOpenAICompatibleCompletionPrompt({ prompt });
902
952
  const stop = [...stopSequences != null ? stopSequences : [], ...userStopSequences != null ? userStopSequences : []];
903
953
  return {
904
954
  args: {
@@ -926,8 +976,8 @@ var OpenAICompatibleCompletionLanguageModel = class {
926
976
  };
927
977
  }
928
978
  async doGenerate(options) {
929
- var _a, _b, _c, _d;
930
- const { args, warnings } = this.getArgs(options);
979
+ var _a, _b, _c, _d, _e, _f;
980
+ const { args, warnings } = await this.getArgs(options);
931
981
  const {
932
982
  responseHeaders,
933
983
  value: response,
@@ -955,7 +1005,8 @@ var OpenAICompatibleCompletionLanguageModel = class {
955
1005
  content,
956
1006
  usage: {
957
1007
  inputTokens: (_b = (_a = response.usage) == null ? void 0 : _a.prompt_tokens) != null ? _b : void 0,
958
- outputTokens: (_d = (_c = response.usage) == null ? void 0 : _c.completion_tokens) != null ? _d : void 0
1008
+ outputTokens: (_d = (_c = response.usage) == null ? void 0 : _c.completion_tokens) != null ? _d : void 0,
1009
+ totalTokens: (_f = (_e = response.usage) == null ? void 0 : _e.total_tokens) != null ? _f : void 0
959
1010
  },
960
1011
  finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
961
1012
  request: { body: args },
@@ -968,10 +1019,12 @@ var OpenAICompatibleCompletionLanguageModel = class {
968
1019
  };
969
1020
  }
970
1021
  async doStream(options) {
971
- const { args, warnings } = this.getArgs(options);
1022
+ const { args, warnings } = await this.getArgs(options);
972
1023
  const body = {
973
1024
  ...args,
974
- stream: true
1025
+ stream: true,
1026
+ // only include stream_options when in strict compatibility mode:
1027
+ stream_options: this.config.includeUsage ? { include_usage: true } : void 0
975
1028
  };
976
1029
  const { responseHeaders, value: response } = await postJsonToApi2({
977
1030
  url: this.config.url({
@@ -990,7 +1043,8 @@ var OpenAICompatibleCompletionLanguageModel = class {
990
1043
  let finishReason = "unknown";
991
1044
  const usage = {
992
1045
  inputTokens: void 0,
993
- outputTokens: void 0
1046
+ outputTokens: void 0,
1047
+ totalTokens: void 0
994
1048
  };
995
1049
  let isFirstChunk = true;
996
1050
  return {
@@ -1000,7 +1054,10 @@ var OpenAICompatibleCompletionLanguageModel = class {
1000
1054
  controller.enqueue({ type: "stream-start", warnings });
1001
1055
  },
1002
1056
  transform(chunk, controller) {
1003
- var _a, _b;
1057
+ var _a, _b, _c;
1058
+ if (options.includeRawChunks) {
1059
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1060
+ }
1004
1061
  if (!chunk.success) {
1005
1062
  finishReason = "error";
1006
1063
  controller.enqueue({ type: "error", error: chunk.error });
@@ -1018,10 +1075,15 @@ var OpenAICompatibleCompletionLanguageModel = class {
1018
1075
  type: "response-metadata",
1019
1076
  ...getResponseMetadata(value)
1020
1077
  });
1078
+ controller.enqueue({
1079
+ type: "text-start",
1080
+ id: "0"
1081
+ });
1021
1082
  }
1022
1083
  if (value.usage != null) {
1023
1084
  usage.inputTokens = (_a = value.usage.prompt_tokens) != null ? _a : void 0;
1024
1085
  usage.outputTokens = (_b = value.usage.completion_tokens) != null ? _b : void 0;
1086
+ usage.totalTokens = (_c = value.usage.total_tokens) != null ? _c : void 0;
1025
1087
  }
1026
1088
  const choice = value.choices[0];
1027
1089
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
@@ -1031,12 +1093,16 @@ var OpenAICompatibleCompletionLanguageModel = class {
1031
1093
  }
1032
1094
  if ((choice == null ? void 0 : choice.text) != null) {
1033
1095
  controller.enqueue({
1034
- type: "text",
1035
- text: choice.text
1096
+ type: "text-delta",
1097
+ id: "0",
1098
+ delta: choice.text
1036
1099
  });
1037
1100
  }
1038
1101
  },
1039
1102
  flush(controller) {
1103
+ if (!isFirstChunk) {
1104
+ controller.enqueue({ type: "text-end", id: "0" });
1105
+ }
1040
1106
  controller.enqueue({
1041
1107
  type: "finish",
1042
1108
  finishReason,
@@ -1050,6 +1116,11 @@ var OpenAICompatibleCompletionLanguageModel = class {
1050
1116
  };
1051
1117
  }
1052
1118
  };
1119
+ var usageSchema = z5.object({
1120
+ prompt_tokens: z5.number(),
1121
+ completion_tokens: z5.number(),
1122
+ total_tokens: z5.number()
1123
+ });
1053
1124
  var openaiCompatibleCompletionResponseSchema = z5.object({
1054
1125
  id: z5.string().nullish(),
1055
1126
  created: z5.number().nullish(),
@@ -1060,10 +1131,7 @@ var openaiCompatibleCompletionResponseSchema = z5.object({
1060
1131
  finish_reason: z5.string()
1061
1132
  })
1062
1133
  ),
1063
- usage: z5.object({
1064
- prompt_tokens: z5.number(),
1065
- completion_tokens: z5.number()
1066
- }).nullish()
1134
+ usage: usageSchema.nullish()
1067
1135
  });
1068
1136
  var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => z5.union([
1069
1137
  z5.object({
@@ -1077,10 +1145,7 @@ var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => z5.union([
1077
1145
  index: z5.number()
1078
1146
  })
1079
1147
  ),
1080
- usage: z5.object({
1081
- prompt_tokens: z5.number(),
1082
- completion_tokens: z5.number()
1083
- }).nullish()
1148
+ usage: usageSchema.nullish()
1084
1149
  }),
1085
1150
  errorSchema
1086
1151
  ]);
@@ -1096,10 +1161,10 @@ import {
1096
1161
  parseProviderOptions as parseProviderOptions3,
1097
1162
  postJsonToApi as postJsonToApi3
1098
1163
  } from "@ai-sdk/provider-utils";
1099
- import { z as z7 } from "zod";
1164
+ import { z as z7 } from "zod/v4";
1100
1165
 
1101
1166
  // src/openai-compatible-embedding-options.ts
1102
- import { z as z6 } from "zod";
1167
+ import { z as z6 } from "zod/v4";
1103
1168
  var openaiCompatibleEmbeddingProviderOptions = z6.object({
1104
1169
  /**
1105
1170
  * The number of dimensions the resulting output embeddings should have.
@@ -1142,12 +1207,12 @@ var OpenAICompatibleEmbeddingModel = class {
1142
1207
  }) {
1143
1208
  var _a, _b, _c;
1144
1209
  const compatibleOptions = Object.assign(
1145
- (_a = parseProviderOptions3({
1210
+ (_a = await parseProviderOptions3({
1146
1211
  provider: "openai-compatible",
1147
1212
  providerOptions,
1148
1213
  schema: openaiCompatibleEmbeddingProviderOptions
1149
1214
  })) != null ? _a : {},
1150
- (_b = parseProviderOptions3({
1215
+ (_b = await parseProviderOptions3({
1151
1216
  provider: this.providerOptionsName,
1152
1217
  providerOptions,
1153
1218
  schema: openaiCompatibleEmbeddingProviderOptions
@@ -1206,17 +1271,13 @@ import {
1206
1271
  createJsonResponseHandler as createJsonResponseHandler4,
1207
1272
  postJsonToApi as postJsonToApi4
1208
1273
  } from "@ai-sdk/provider-utils";
1209
- import { z as z8 } from "zod";
1274
+ import { z as z8 } from "zod/v4";
1210
1275
  var OpenAICompatibleImageModel = class {
1211
- constructor(modelId, settings, config) {
1276
+ constructor(modelId, config) {
1212
1277
  this.modelId = modelId;
1213
- this.settings = settings;
1214
1278
  this.config = config;
1215
- this.specificationVersion = "v1";
1216
- }
1217
- get maxImagesPerCall() {
1218
- var _a;
1219
- return (_a = this.settings.maxImagesPerCall) != null ? _a : 10;
1279
+ this.specificationVersion = "v2";
1280
+ this.maxImagesPerCall = 10;
1220
1281
  }
1221
1282
  get provider() {
1222
1283
  return this.config.provider;
@@ -1256,8 +1317,7 @@ var OpenAICompatibleImageModel = class {
1256
1317
  n,
1257
1318
  size,
1258
1319
  ...(_d = providerOptions.openai) != null ? _d : {},
1259
- response_format: "b64_json",
1260
- ...this.settings.user ? { user: this.settings.user } : {}
1320
+ response_format: "b64_json"
1261
1321
  },
1262
1322
  failedResponseHandler: createJsonErrorResponseHandler4(
1263
1323
  (_e = this.config.errorStructure) != null ? _e : defaultOpenAICompatibleErrorStructure
@@ -1307,20 +1367,16 @@ function createOpenAICompatible(options) {
1307
1367
  const createLanguageModel = (modelId) => createChatModel(modelId);
1308
1368
  const createChatModel = (modelId) => new OpenAICompatibleChatLanguageModel(modelId, {
1309
1369
  ...getCommonModelConfig("chat"),
1310
- defaultObjectGenerationMode: "tool"
1370
+ includeUsage: options.includeUsage
1371
+ });
1372
+ const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(modelId, {
1373
+ ...getCommonModelConfig("completion"),
1374
+ includeUsage: options.includeUsage
1311
1375
  });
1312
- const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(
1313
- modelId,
1314
- getCommonModelConfig("completion")
1315
- );
1316
1376
  const createEmbeddingModel = (modelId) => new OpenAICompatibleEmbeddingModel(modelId, {
1317
1377
  ...getCommonModelConfig("embedding")
1318
1378
  });
1319
- const createImageModel = (modelId, settings = {}) => new OpenAICompatibleImageModel(
1320
- modelId,
1321
- settings,
1322
- getCommonModelConfig("image")
1323
- );
1379
+ const createImageModel = (modelId) => new OpenAICompatibleImageModel(modelId, getCommonModelConfig("image"));
1324
1380
  const provider = (modelId) => createLanguageModel(modelId);
1325
1381
  provider.languageModel = createLanguageModel;
1326
1382
  provider.chatModel = createChatModel;