@ai-sdk/openai-compatible 3.0.54 → 3.0.57

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
@@ -24,7 +24,7 @@ function toCamelCase(str) {
24
24
  }
25
25
  function resolveProviderOptionsKey(rawName, providerOptions) {
26
26
  const camelName = toCamelCase(rawName);
27
- if (camelName !== rawName && (providerOptions == null ? void 0 : providerOptions[camelName]) != null) {
27
+ if (camelName !== rawName && providerOptions?.[camelName] != null) {
28
28
  return camelName;
29
29
  }
30
30
  return rawName;
@@ -35,7 +35,7 @@ function warnIfDeprecatedProviderOptionsKey({
35
35
  warnings
36
36
  }) {
37
37
  const camelName = toCamelCase(rawName);
38
- if (camelName !== rawName && (providerOptions == null ? void 0 : providerOptions[rawName]) != null) {
38
+ if (camelName !== rawName && providerOptions?.[rawName] != null) {
39
39
  warnings.push({
40
40
  type: "deprecated",
41
41
  setting: `providerOptions key '${rawName}'`,
@@ -65,14 +65,13 @@ var defaultOpenAICompatibleErrorStructure = {
65
65
  // src/chat/convert-openai-compatible-chat-usage.ts
66
66
  import { createNullLanguageModelUsage } from "@ai-sdk/provider-utils";
67
67
  function convertOpenAICompatibleChatUsage(usage) {
68
- var _a, _b, _c, _d, _e, _f;
69
68
  if (usage == null) {
70
69
  return createNullLanguageModelUsage();
71
70
  }
72
- const promptTokens = (_a = usage.prompt_tokens) != null ? _a : 0;
73
- const completionTokens = (_b = usage.completion_tokens) != null ? _b : 0;
74
- const cacheReadTokens = (_d = (_c = usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : 0;
75
- const reasoningTokens = (_f = (_e = usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : 0;
71
+ const promptTokens = usage.prompt_tokens ?? 0;
72
+ const completionTokens = usage.completion_tokens ?? 0;
73
+ const cacheReadTokens = usage.prompt_tokens_details?.cached_tokens ?? 0;
74
+ const reasoningTokens = usage.completion_tokens_details?.reasoning_tokens ?? 0;
76
75
  return {
77
76
  inputTokens: {
78
77
  total: promptTokens,
@@ -100,8 +99,7 @@ import {
100
99
  resolveFullMediaType
101
100
  } from "@ai-sdk/provider-utils";
102
101
  function getOpenAIMetadata(message) {
103
- var _a, _b;
104
- return (_b = (_a = message == null ? void 0 : message.providerOptions) == null ? void 0 : _a.openaiCompatible) != null ? _b : {};
102
+ return message?.providerOptions?.openaiCompatible ?? {};
105
103
  }
106
104
  function getAudioFormat(mediaType) {
107
105
  switch (mediaType) {
@@ -115,7 +113,6 @@ function getAudioFormat(mediaType) {
115
113
  }
116
114
  }
117
115
  function convertToOpenAICompatibleChatMessages(prompt, { providerOptionsKey = "google" } = {}) {
118
- var _a, _b, _c, _d, _e, _f;
119
116
  const messages = [];
120
117
  for (const { role, content, ...message } of prompt) {
121
118
  const metadata = getOpenAIMetadata({ ...message });
@@ -136,7 +133,6 @@ function convertToOpenAICompatibleChatMessages(prompt, { providerOptionsKey = "g
136
133
  messages.push({
137
134
  role: "user",
138
135
  content: content.map((part) => {
139
- var _a2;
140
136
  const partMetadata = getOpenAIMetadata(part);
141
137
  switch (part.type) {
142
138
  case "text": {
@@ -212,7 +208,7 @@ function convertToOpenAICompatibleChatMessages(prompt, { providerOptionsKey = "g
212
208
  return {
213
209
  type: "file",
214
210
  file: {
215
- filename: (_a2 = part.filename) != null ? _a2 : "document.pdf",
211
+ filename: part.filename ?? "document.pdf",
216
212
  file_data: `data:application/pdf;base64,${convertToBase64(part.data.data)}`
217
213
  },
218
214
  ...partMetadata
@@ -256,7 +252,7 @@ function convertToOpenAICompatibleChatMessages(prompt, { providerOptionsKey = "g
256
252
  break;
257
253
  }
258
254
  case "tool-call": {
259
- const thoughtSignature = (_e = (_b = (_a = part.providerOptions) == null ? void 0 : _a[providerOptionsKey]) == null ? void 0 : _b.thoughtSignature) != null ? _e : (_d = (_c = part.providerOptions) == null ? void 0 : _c.google) == null ? void 0 : _d.thoughtSignature;
255
+ const thoughtSignature = part.providerOptions?.[providerOptionsKey]?.thoughtSignature ?? part.providerOptions?.google?.thoughtSignature;
260
256
  toolCalls.push({
261
257
  id: part.toolCallId,
262
258
  type: "function",
@@ -300,7 +296,7 @@ function convertToOpenAICompatibleChatMessages(prompt, { providerOptionsKey = "g
300
296
  contentValue = output.value;
301
297
  break;
302
298
  case "execution-denied":
303
- contentValue = (_f = output.reason) != null ? _f : "Tool call execution denied.";
299
+ contentValue = output.reason ?? "Tool call execution denied.";
304
300
  break;
305
301
  case "content":
306
302
  case "json":
@@ -393,7 +389,7 @@ function prepareTools({
393
389
  tools,
394
390
  toolChoice
395
391
  }) {
396
- tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
392
+ tools = tools?.length ? tools : void 0;
397
393
  const toolWarnings = [];
398
394
  if (tools == null) {
399
395
  return { tools: void 0, toolChoice: void 0, toolWarnings };
@@ -448,15 +444,14 @@ function prepareTools({
448
444
  var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel {
449
445
  constructor(modelId, config) {
450
446
  this.specificationVersion = "v4";
451
- var _a, _b;
452
447
  this.modelId = modelId;
453
448
  this.config = config;
454
- const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
449
+ const errorStructure = config.errorStructure ?? defaultOpenAICompatibleErrorStructure;
455
450
  this.chunkSchema = createOpenAICompatibleChatChunkSchema(
456
451
  errorStructure.errorSchema
457
452
  );
458
453
  this.failedResponseHandler = createJsonErrorResponseHandler(errorStructure);
459
- this.supportsStructuredOutputs = (_b = config.supportsStructuredOutputs) != null ? _b : false;
454
+ this.supportsStructuredOutputs = config.supportsStructuredOutputs ?? false;
460
455
  }
461
456
  // type inferred via constructor
462
457
  static [WORKFLOW_SERIALIZE](model) {
@@ -478,16 +473,13 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
478
473
  return this.config.provider.split(".")[0].trim();
479
474
  }
480
475
  get supportedUrls() {
481
- var _a, _b, _c;
482
- return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
476
+ return this.config.supportedUrls?.() ?? {};
483
477
  }
484
478
  transformRequestBody(args) {
485
- var _a, _b, _c;
486
- return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
479
+ return this.config.transformRequestBody?.(args) ?? args;
487
480
  }
488
481
  convertUsage(usage) {
489
- var _a, _b, _c;
490
- return (_c = (_b = (_a = this.config).convertUsage) == null ? void 0 : _b.call(_a, usage)) != null ? _c : convertOpenAICompatibleChatUsage(usage);
482
+ return this.config.convertUsage?.(usage) ?? convertOpenAICompatibleChatUsage(usage);
491
483
  }
492
484
  async getArgs({
493
485
  prompt,
@@ -505,7 +497,6 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
505
497
  toolChoice,
506
498
  tools
507
499
  }) {
508
- var _a, _b, _c, _d, _e, _f;
509
500
  const warnings = [];
510
501
  const deprecatedOptions = await parseProviderOptions({
511
502
  provider: "openai-compatible",
@@ -525,28 +516,28 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
525
516
  warnings
526
517
  });
527
518
  const compatibleOptions = Object.assign(
528
- deprecatedOptions != null ? deprecatedOptions : {},
529
- (_a = await parseProviderOptions({
519
+ deprecatedOptions ?? {},
520
+ await parseProviderOptions({
530
521
  provider: "openaiCompatible",
531
522
  providerOptions,
532
523
  schema: openaiCompatibleLanguageModelChatOptions
533
- })) != null ? _a : {},
534
- (_b = await parseProviderOptions({
524
+ }) ?? {},
525
+ await parseProviderOptions({
535
526
  provider: this.providerOptionsName,
536
527
  providerOptions,
537
528
  schema: openaiCompatibleLanguageModelChatOptions
538
- })) != null ? _b : {},
539
- (_c = await parseProviderOptions({
529
+ }) ?? {},
530
+ await parseProviderOptions({
540
531
  provider: toCamelCase(this.providerOptionsName),
541
532
  providerOptions,
542
533
  schema: openaiCompatibleLanguageModelChatOptions
543
- })) != null ? _c : {}
534
+ }) ?? {}
544
535
  );
545
- const strictJsonSchema = (_d = compatibleOptions == null ? void 0 : compatibleOptions.strictJsonSchema) != null ? _d : true;
536
+ const strictJsonSchema = compatibleOptions?.strictJsonSchema ?? true;
546
537
  if (topK != null) {
547
538
  warnings.push({ type: "unsupported", feature: "topK" });
548
539
  }
549
- if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !this.supportsStructuredOutputs) {
540
+ if (responseFormat?.type === "json" && responseFormat.schema != null && !this.supportsStructuredOutputs) {
550
541
  warnings.push({
551
542
  type: "unsupported",
552
543
  feature: "responseFormat",
@@ -578,12 +569,12 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
578
569
  top_p: topP,
579
570
  frequency_penalty: frequencyPenalty,
580
571
  presence_penalty: presencePenalty,
581
- response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? this.supportsStructuredOutputs === true && responseFormat.schema != null ? {
572
+ response_format: responseFormat?.type === "json" ? this.supportsStructuredOutputs === true && responseFormat.schema != null ? {
582
573
  type: "json_schema",
583
574
  json_schema: {
584
575
  schema: responseFormat.schema,
585
576
  strict: strictJsonSchema,
586
- name: (_e = responseFormat.name) != null ? _e : "response",
577
+ name: responseFormat.name ?? "response",
587
578
  description: responseFormat.description
588
579
  }
589
580
  } : { type: "json_object" } : void 0,
@@ -591,15 +582,15 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
591
582
  seed,
592
583
  ...Object.fromEntries(
593
584
  Object.entries({
594
- ...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
595
- ...providerOptions == null ? void 0 : providerOptions[toCamelCase(this.providerOptionsName)]
585
+ ...providerOptions?.[this.providerOptionsName],
586
+ ...providerOptions?.[toCamelCase(this.providerOptionsName)]
596
587
  }).filter(
597
588
  ([key]) => !Object.keys(
598
589
  openaiCompatibleLanguageModelChatOptions.shape
599
590
  ).includes(key)
600
591
  )
601
592
  ),
602
- reasoning_effort: (_f = compatibleOptions.reasoningEffort) != null ? _f : isCustomReasoning(reasoning) ? reasoning : void 0,
593
+ reasoning_effort: compatibleOptions.reasoningEffort ?? (isCustomReasoning(reasoning) ? reasoning : void 0),
603
594
  verbosity: compatibleOptions.textVerbosity,
604
595
  // messages:
605
596
  messages: convertToOpenAICompatibleChatMessages(prompt, {
@@ -613,7 +604,6 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
613
604
  };
614
605
  }
615
606
  async doGenerate(options) {
616
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
617
607
  const { args, warnings, metadataKey } = await this.getArgs({ ...options });
618
608
  const transformedBody = this.transformRequestBody(args);
619
609
  const body = JSON.stringify(transformedBody);
@@ -626,7 +616,7 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
626
616
  path: "/chat/completions",
627
617
  modelId: this.modelId
628
618
  }),
629
- headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
619
+ headers: combineHeaders(this.config.headers?.(), options.headers),
630
620
  body: transformedBody,
631
621
  failedResponseHandler: this.failedResponseHandler,
632
622
  successfulResponseHandler: createJsonResponseHandler(
@@ -644,7 +634,7 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
644
634
  }
645
635
  const content = [];
646
636
  content.push(...convertOpenAICompatibleContent(choice.message.content));
647
- const reasoning = (_c = choice.message.reasoning_content) != null ? _c : choice.message.reasoning;
637
+ const reasoning = choice.message.reasoning_content ?? choice.message.reasoning;
648
638
  if (reasoning != null && reasoning.length > 0) {
649
639
  content.push({
650
640
  type: "reasoning",
@@ -653,7 +643,7 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
653
643
  }
654
644
  if (choice.message.tool_calls != null) {
655
645
  for (const toolCall of choice.message.tool_calls) {
656
- const thoughtSignature = (_e = (_d = toolCall.extra_content) == null ? void 0 : _d.google) == null ? void 0 : _e.thought_signature;
646
+ const thoughtSignature = toolCall.extra_content?.google?.thought_signature;
657
647
  content.push({
658
648
  type: "tool-call",
659
649
  toolCallId: toolCall.id || generateId(),
@@ -669,22 +659,22 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
669
659
  }
670
660
  const providerMetadata = {
671
661
  [metadataKey]: {},
672
- ...await ((_g = (_f = this.config.metadataExtractor) == null ? void 0 : _f.extractMetadata) == null ? void 0 : _g.call(_f, {
662
+ ...await this.config.metadataExtractor?.extractMetadata?.({
673
663
  parsedBody: rawResponse
674
- }))
664
+ })
675
665
  };
676
- const completionTokenDetails = (_h = responseBody.usage) == null ? void 0 : _h.completion_tokens_details;
677
- if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
678
- providerMetadata[metadataKey].acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
666
+ const completionTokenDetails = responseBody.usage?.completion_tokens_details;
667
+ if (completionTokenDetails?.accepted_prediction_tokens != null) {
668
+ providerMetadata[metadataKey].acceptedPredictionTokens = completionTokenDetails?.accepted_prediction_tokens;
679
669
  }
680
- if ((completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens) != null) {
681
- providerMetadata[metadataKey].rejectedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens;
670
+ if (completionTokenDetails?.rejected_prediction_tokens != null) {
671
+ providerMetadata[metadataKey].rejectedPredictionTokens = completionTokenDetails?.rejected_prediction_tokens;
682
672
  }
683
673
  return {
684
674
  content,
685
675
  finishReason: {
686
676
  unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
687
- raw: (_i = choice.finish_reason) != null ? _i : void 0
677
+ raw: choice.finish_reason ?? void 0
688
678
  },
689
679
  usage: this.convertUsage(responseBody.usage),
690
680
  providerMetadata,
@@ -698,7 +688,6 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
698
688
  };
699
689
  }
700
690
  async doStream(options) {
701
- var _a, _b, _c;
702
691
  const { args, warnings, metadataKey } = await this.getArgs({
703
692
  ...options
704
693
  });
@@ -708,13 +697,13 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
708
697
  // only include stream_options when in strict compatibility mode:
709
698
  stream_options: this.config.includeUsage ? { include_usage: true } : void 0
710
699
  });
711
- const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
700
+ const metadataExtractor = this.config.metadataExtractor?.createStreamExtractor();
712
701
  const { responseHeaders, value: response } = await postJsonToApi({
713
702
  url: this.config.url({
714
703
  path: "/chat/completions",
715
704
  modelId: this.modelId
716
705
  }),
717
- headers: combineHeaders((_c = (_b = this.config).headers) == null ? void 0 : _c.call(_b), options.headers),
706
+ headers: combineHeaders(this.config.headers?.(), options.headers),
718
707
  body,
719
708
  failedResponseHandler: this.failedResponseHandler,
720
709
  successfulResponseHandler: createEventSourceResponseHandler(
@@ -728,7 +717,6 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
728
717
  const pendingToolCalls = /* @__PURE__ */ new Map();
729
718
  const forwardedToolCallIndices = /* @__PURE__ */ new Set();
730
719
  const processToolCallDelta = (toolCallDelta) => {
731
- var _a2, _b2, _c2, _d, _e;
732
720
  const index = toolCallDelta.index;
733
721
  if (index == null || forwardedToolCallIndices.has(index)) {
734
722
  toolCallTracker.processDelta(toolCallDelta);
@@ -737,9 +725,9 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
737
725
  let pending = pendingToolCalls.get(index);
738
726
  if (pending == null) {
739
727
  pending = {
740
- id: (_a2 = toolCallDelta.id) != null ? _a2 : null,
728
+ id: toolCallDelta.id ?? null,
741
729
  bufferedArguments: "",
742
- extraContent: (_b2 = toolCallDelta.extra_content) != null ? _b2 : null
730
+ extraContent: toolCallDelta.extra_content ?? null
743
731
  };
744
732
  pendingToolCalls.set(index, pending);
745
733
  } else {
@@ -750,11 +738,11 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
750
738
  pending.extraContent = toolCallDelta.extra_content;
751
739
  }
752
740
  }
753
- const argumentsDelta = (_c2 = toolCallDelta.function) == null ? void 0 : _c2.arguments;
741
+ const argumentsDelta = toolCallDelta.function?.arguments;
754
742
  if (argumentsDelta != null) {
755
743
  pending.bufferedArguments += argumentsDelta;
756
744
  }
757
- const name = (_d = toolCallDelta.function) == null ? void 0 : _d.name;
745
+ const name = toolCallDelta.function?.name;
758
746
  if (name != null) {
759
747
  const forwardDelta = {
760
748
  index,
@@ -763,7 +751,7 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
763
751
  name,
764
752
  arguments: pending.bufferedArguments
765
753
  },
766
- extra_content: (_e = pending.extraContent) != null ? _e : void 0
754
+ extra_content: pending.extraContent ?? void 0
767
755
  };
768
756
  toolCallTracker.processDelta(forwardDelta);
769
757
  pendingToolCalls.delete(index);
@@ -785,8 +773,7 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
785
773
  {
786
774
  generateId,
787
775
  extractMetadata: (delta) => {
788
- var _a2, _b2;
789
- const thoughtSignature = (_b2 = (_a2 = delta.extra_content) == null ? void 0 : _a2.google) == null ? void 0 : _b2.thought_signature;
776
+ const thoughtSignature = delta.extra_content?.google?.thought_signature;
790
777
  return thoughtSignature ? { [providerOptionsName]: { thoughtSignature } } : void 0;
791
778
  },
792
779
  buildToolCallProviderMetadata: (metadata) => metadata
@@ -795,7 +782,6 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
795
782
  controller.enqueue({ type: "stream-start", warnings });
796
783
  },
797
784
  transform(chunk, controller) {
798
- var _a2, _b2;
799
785
  if (options.includeRawChunks) {
800
786
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
801
787
  }
@@ -804,7 +790,7 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
804
790
  controller.enqueue({ type: "error", error: chunk.error });
805
791
  return;
806
792
  }
807
- metadataExtractor == null ? void 0 : metadataExtractor.processChunk(chunk.rawValue);
793
+ metadataExtractor?.processChunk(chunk.rawValue);
808
794
  if ("error" in chunk.value) {
809
795
  finishReason = { unified: "error", raw: void 0 };
810
796
  controller.enqueue({
@@ -828,13 +814,13 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
828
814
  usage = value.usage;
829
815
  }
830
816
  const choice = value.choices[0];
831
- if ((choice == null ? void 0 : choice.finish_reason) != null) {
817
+ if (choice?.finish_reason != null) {
832
818
  finishReason = {
833
819
  unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
834
- raw: (_a2 = choice.finish_reason) != null ? _a2 : void 0
820
+ raw: choice.finish_reason ?? void 0
835
821
  };
836
822
  }
837
- if ((choice == null ? void 0 : choice.delta) == null) {
823
+ if (choice?.delta == null) {
838
824
  return;
839
825
  }
840
826
  const delta = choice.delta;
@@ -874,7 +860,7 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
874
860
  delta: textDelta
875
861
  });
876
862
  };
877
- const reasoningContent = (_b2 = delta.reasoning_content) != null ? _b2 : delta.reasoning;
863
+ const reasoningContent = delta.reasoning_content ?? delta.reasoning;
878
864
  if (reasoningContent) {
879
865
  enqueueReasoningDelta(reasoningContent);
880
866
  }
@@ -901,7 +887,6 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
901
887
  }
902
888
  },
903
889
  flush(controller) {
904
- var _a2, _b2, _c2, _d;
905
890
  if (isActiveReasoning) {
906
891
  controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
907
892
  }
@@ -929,13 +914,13 @@ var OpenAICompatibleChatLanguageModel = class _OpenAICompatibleChatLanguageModel
929
914
  }
930
915
  const providerMetadata = {
931
916
  [providerOptionsName]: {},
932
- ...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
917
+ ...metadataExtractor?.buildMetadata()
933
918
  };
934
- if (((_a2 = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _a2.accepted_prediction_tokens) != null) {
935
- providerMetadata[providerOptionsName].acceptedPredictionTokens = (_b2 = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _b2.accepted_prediction_tokens;
919
+ if (usage?.completion_tokens_details?.accepted_prediction_tokens != null) {
920
+ providerMetadata[providerOptionsName].acceptedPredictionTokens = usage?.completion_tokens_details?.accepted_prediction_tokens;
936
921
  }
937
- if (((_c2 = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _c2.rejected_prediction_tokens) != null) {
938
- providerMetadata[providerOptionsName].rejectedPredictionTokens = (_d = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _d.rejected_prediction_tokens;
922
+ if (usage?.completion_tokens_details?.rejected_prediction_tokens != null) {
923
+ providerMetadata[providerOptionsName].rejectedPredictionTokens = usage?.completion_tokens_details?.rejected_prediction_tokens;
939
924
  }
940
925
  controller.enqueue({
941
926
  type: "finish",
@@ -1083,12 +1068,11 @@ import { z as z5 } from "zod/v4";
1083
1068
  // src/completion/convert-openai-compatible-completion-usage.ts
1084
1069
  import { createNullLanguageModelUsage as createNullLanguageModelUsage2 } from "@ai-sdk/provider-utils";
1085
1070
  function convertOpenAICompatibleCompletionUsage(usage) {
1086
- var _a, _b;
1087
1071
  if (usage == null) {
1088
1072
  return createNullLanguageModelUsage2();
1089
1073
  }
1090
- const promptTokens = (_a = usage.prompt_tokens) != null ? _a : 0;
1091
- const completionTokens = (_b = usage.completion_tokens) != null ? _b : 0;
1074
+ const promptTokens = usage.prompt_tokens ?? 0;
1075
+ const completionTokens = usage.completion_tokens ?? 0;
1092
1076
  return {
1093
1077
  inputTokens: {
1094
1078
  total: promptTokens,
@@ -1232,10 +1216,9 @@ var openaiCompatibleLanguageModelCompletionOptions = z4.object({
1232
1216
  var OpenAICompatibleCompletionLanguageModel = class _OpenAICompatibleCompletionLanguageModel {
1233
1217
  constructor(modelId, config) {
1234
1218
  this.specificationVersion = "v4";
1235
- var _a;
1236
1219
  this.modelId = modelId;
1237
1220
  this.config = config;
1238
- const errorStructure = (_a = config.errorStructure) != null ? _a : defaultOpenAICompatibleErrorStructure;
1221
+ const errorStructure = config.errorStructure ?? defaultOpenAICompatibleErrorStructure;
1239
1222
  this.chunkSchema = createOpenAICompatibleCompletionChunkSchema(
1240
1223
  errorStructure.errorSchema
1241
1224
  );
@@ -1261,8 +1244,7 @@ var OpenAICompatibleCompletionLanguageModel = class _OpenAICompatibleCompletionL
1261
1244
  return this.config.provider.split(".")[0].trim();
1262
1245
  }
1263
1246
  get supportedUrls() {
1264
- var _a, _b, _c;
1265
- return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
1247
+ return this.config.supportedUrls?.() ?? {};
1266
1248
  }
1267
1249
  async getArgs({
1268
1250
  prompt,
@@ -1279,7 +1261,6 @@ var OpenAICompatibleCompletionLanguageModel = class _OpenAICompatibleCompletionL
1279
1261
  tools,
1280
1262
  toolChoice
1281
1263
  }) {
1282
- var _a, _b;
1283
1264
  const warnings = [];
1284
1265
  warnIfDeprecatedProviderOptionsKey({
1285
1266
  rawName: this.providerOptionsName,
@@ -1287,21 +1268,21 @@ var OpenAICompatibleCompletionLanguageModel = class _OpenAICompatibleCompletionL
1287
1268
  warnings
1288
1269
  });
1289
1270
  const completionOptions = Object.assign(
1290
- (_a = await parseProviderOptions2({
1271
+ await parseProviderOptions2({
1291
1272
  provider: this.providerOptionsName,
1292
1273
  providerOptions,
1293
1274
  schema: openaiCompatibleLanguageModelCompletionOptions
1294
- })) != null ? _a : {},
1295
- (_b = await parseProviderOptions2({
1275
+ }) ?? {},
1276
+ await parseProviderOptions2({
1296
1277
  provider: toCamelCase(this.providerOptionsName),
1297
1278
  providerOptions,
1298
1279
  schema: openaiCompatibleLanguageModelCompletionOptions
1299
- })) != null ? _b : {}
1280
+ }) ?? {}
1300
1281
  );
1301
1282
  if (topK != null) {
1302
1283
  warnings.push({ type: "unsupported", feature: "topK" });
1303
1284
  }
1304
- if (tools == null ? void 0 : tools.length) {
1285
+ if (tools?.length) {
1305
1286
  warnings.push({ type: "unsupported", feature: "tools" });
1306
1287
  }
1307
1288
  if (toolChoice != null) {
@@ -1315,7 +1296,7 @@ var OpenAICompatibleCompletionLanguageModel = class _OpenAICompatibleCompletionL
1315
1296
  });
1316
1297
  }
1317
1298
  const { prompt: completionPrompt, stopSequences } = convertToOpenAICompatibleCompletionPrompt({ prompt });
1318
- const stop = [...stopSequences != null ? stopSequences : [], ...userStopSequences != null ? userStopSequences : []];
1299
+ const stop = [...stopSequences ?? [], ...userStopSequences ?? []];
1319
1300
  return {
1320
1301
  args: {
1321
1302
  // model id:
@@ -1332,8 +1313,8 @@ var OpenAICompatibleCompletionLanguageModel = class _OpenAICompatibleCompletionL
1332
1313
  frequency_penalty: frequencyPenalty,
1333
1314
  presence_penalty: presencePenalty,
1334
1315
  seed,
1335
- ...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
1336
- ...providerOptions == null ? void 0 : providerOptions[toCamelCase(this.providerOptionsName)],
1316
+ ...providerOptions?.[this.providerOptionsName],
1317
+ ...providerOptions?.[toCamelCase(this.providerOptionsName)],
1337
1318
  // prompt:
1338
1319
  prompt: completionPrompt,
1339
1320
  // stop sequences:
@@ -1343,7 +1324,6 @@ var OpenAICompatibleCompletionLanguageModel = class _OpenAICompatibleCompletionL
1343
1324
  };
1344
1325
  }
1345
1326
  async doGenerate(options) {
1346
- var _a, _b;
1347
1327
  const { args, warnings } = await this.getArgs(options);
1348
1328
  const {
1349
1329
  responseHeaders,
@@ -1354,7 +1334,7 @@ var OpenAICompatibleCompletionLanguageModel = class _OpenAICompatibleCompletionL
1354
1334
  path: "/completions",
1355
1335
  modelId: this.modelId
1356
1336
  }),
1357
- headers: combineHeaders2((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
1337
+ headers: combineHeaders2(this.config.headers?.(), options.headers),
1358
1338
  body: args,
1359
1339
  failedResponseHandler: this.failedResponseHandler,
1360
1340
  successfulResponseHandler: createJsonResponseHandler2(
@@ -1385,7 +1365,6 @@ var OpenAICompatibleCompletionLanguageModel = class _OpenAICompatibleCompletionL
1385
1365
  };
1386
1366
  }
1387
1367
  async doStream(options) {
1388
- var _a, _b;
1389
1368
  const { args, warnings } = await this.getArgs(options);
1390
1369
  const body = {
1391
1370
  ...args,
@@ -1398,7 +1377,7 @@ var OpenAICompatibleCompletionLanguageModel = class _OpenAICompatibleCompletionL
1398
1377
  path: "/completions",
1399
1378
  modelId: this.modelId
1400
1379
  }),
1401
- headers: combineHeaders2((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
1380
+ headers: combineHeaders2(this.config.headers?.(), options.headers),
1402
1381
  body,
1403
1382
  failedResponseHandler: this.failedResponseHandler,
1404
1383
  successfulResponseHandler: createEventSourceResponseHandler2(
@@ -1420,7 +1399,6 @@ var OpenAICompatibleCompletionLanguageModel = class _OpenAICompatibleCompletionL
1420
1399
  controller.enqueue({ type: "stream-start", warnings });
1421
1400
  },
1422
1401
  transform(chunk, controller) {
1423
- var _a2;
1424
1402
  if (options.includeRawChunks) {
1425
1403
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1426
1404
  }
@@ -1450,13 +1428,13 @@ var OpenAICompatibleCompletionLanguageModel = class _OpenAICompatibleCompletionL
1450
1428
  usage = value.usage;
1451
1429
  }
1452
1430
  const choice = value.choices[0];
1453
- if ((choice == null ? void 0 : choice.finish_reason) != null) {
1431
+ if (choice?.finish_reason != null) {
1454
1432
  finishReason = {
1455
1433
  unified: mapOpenAICompatibleFinishReason2(choice.finish_reason),
1456
- raw: (_a2 = choice.finish_reason) != null ? _a2 : void 0
1434
+ raw: choice.finish_reason ?? void 0
1457
1435
  };
1458
1436
  }
1459
- if ((choice == null ? void 0 : choice.text) != null) {
1437
+ if (choice?.text != null) {
1460
1438
  controller.enqueue({
1461
1439
  type: "text-delta",
1462
1440
  id: "0",
@@ -1557,12 +1535,10 @@ var OpenAICompatibleEmbeddingModel = class _OpenAICompatibleEmbeddingModel {
1557
1535
  return this.config.provider;
1558
1536
  }
1559
1537
  get maxEmbeddingsPerCall() {
1560
- var _a;
1561
- return (_a = this.config.maxEmbeddingsPerCall) != null ? _a : 2048;
1538
+ return this.config.maxEmbeddingsPerCall ?? 2048;
1562
1539
  }
1563
1540
  get supportsParallelCalls() {
1564
- var _a;
1565
- return (_a = this.config.supportsParallelCalls) != null ? _a : true;
1541
+ return this.config.supportsParallelCalls ?? true;
1566
1542
  }
1567
1543
  static [WORKFLOW_SERIALIZE3](model) {
1568
1544
  return serializeModelOptions3({
@@ -1582,7 +1558,6 @@ var OpenAICompatibleEmbeddingModel = class _OpenAICompatibleEmbeddingModel {
1582
1558
  abortSignal,
1583
1559
  providerOptions
1584
1560
  }) {
1585
- var _a, _b, _c, _d, _e;
1586
1561
  const warnings = [];
1587
1562
  const deprecatedOptions = await parseProviderOptions3({
1588
1563
  provider: "openai-compatible",
@@ -1602,17 +1577,17 @@ var OpenAICompatibleEmbeddingModel = class _OpenAICompatibleEmbeddingModel {
1602
1577
  warnings
1603
1578
  });
1604
1579
  const compatibleOptions = Object.assign(
1605
- deprecatedOptions != null ? deprecatedOptions : {},
1606
- (_a = await parseProviderOptions3({
1580
+ deprecatedOptions ?? {},
1581
+ await parseProviderOptions3({
1607
1582
  provider: "openaiCompatible",
1608
1583
  providerOptions,
1609
1584
  schema: openaiCompatibleEmbeddingModelOptions
1610
- })) != null ? _a : {},
1611
- (_b = await parseProviderOptions3({
1585
+ }) ?? {},
1586
+ await parseProviderOptions3({
1612
1587
  provider: this.providerOptionsName,
1613
1588
  providerOptions,
1614
1589
  schema: openaiCompatibleEmbeddingModelOptions
1615
- })) != null ? _b : {}
1590
+ }) ?? {}
1616
1591
  );
1617
1592
  if (values.length > this.maxEmbeddingsPerCall) {
1618
1593
  throw new TooManyEmbeddingValuesForCallError({
@@ -1631,7 +1606,7 @@ var OpenAICompatibleEmbeddingModel = class _OpenAICompatibleEmbeddingModel {
1631
1606
  path: "/embeddings",
1632
1607
  modelId: this.modelId
1633
1608
  }),
1634
- headers: combineHeaders3((_d = (_c = this.config).headers) == null ? void 0 : _d.call(_c), headers),
1609
+ headers: combineHeaders3(this.config.headers?.(), headers),
1635
1610
  body: {
1636
1611
  model: this.modelId,
1637
1612
  input: values,
@@ -1640,7 +1615,7 @@ var OpenAICompatibleEmbeddingModel = class _OpenAICompatibleEmbeddingModel {
1640
1615
  user: compatibleOptions.user
1641
1616
  },
1642
1617
  failedResponseHandler: createJsonErrorResponseHandler3(
1643
- (_e = this.config.errorStructure) != null ? _e : defaultOpenAICompatibleErrorStructure
1618
+ this.config.errorStructure ?? defaultOpenAICompatibleErrorStructure
1644
1619
  ),
1645
1620
  successfulResponseHandler: createJsonResponseHandler3(
1646
1621
  openaiTextEmbeddingResponseSchema
@@ -1726,7 +1701,6 @@ var OpenAICompatibleImageModel = class _OpenAICompatibleImageModel {
1726
1701
  files,
1727
1702
  mask
1728
1703
  }) {
1729
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1730
1704
  const warnings = [];
1731
1705
  if (aspectRatio != null) {
1732
1706
  warnings.push({
@@ -1738,7 +1712,7 @@ var OpenAICompatibleImageModel = class _OpenAICompatibleImageModel {
1738
1712
  if (seed != null) {
1739
1713
  warnings.push({ type: "unsupported", feature: "seed" });
1740
1714
  }
1741
- const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
1715
+ const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
1742
1716
  const args = this.getArgs(providerOptions, warnings);
1743
1717
  if (files != null && files.length > 0) {
1744
1718
  const { value: response2, responseHeaders: responseHeaders2 } = await postFormDataToApi({
@@ -1746,7 +1720,7 @@ var OpenAICompatibleImageModel = class _OpenAICompatibleImageModel {
1746
1720
  path: "/images/edits",
1747
1721
  modelId: this.modelId
1748
1722
  }),
1749
- headers: combineHeaders4((_e = (_d = this.config).headers) == null ? void 0 : _e.call(_d), headers),
1723
+ headers: combineHeaders4(this.config.headers?.(), headers),
1750
1724
  formData: convertToFormData({
1751
1725
  model: this.modelId,
1752
1726
  prompt,
@@ -1757,7 +1731,7 @@ var OpenAICompatibleImageModel = class _OpenAICompatibleImageModel {
1757
1731
  ...args
1758
1732
  }),
1759
1733
  failedResponseHandler: createJsonErrorResponseHandler4(
1760
- (_f = this.config.errorStructure) != null ? _f : defaultOpenAICompatibleErrorStructure
1734
+ this.config.errorStructure ?? defaultOpenAICompatibleErrorStructure
1761
1735
  ),
1762
1736
  successfulResponseHandler: createJsonResponseHandler4(
1763
1737
  openaiCompatibleImageResponseSchema
@@ -1781,7 +1755,7 @@ var OpenAICompatibleImageModel = class _OpenAICompatibleImageModel {
1781
1755
  path: "/images/generations",
1782
1756
  modelId: this.modelId
1783
1757
  }),
1784
- headers: combineHeaders4((_h = (_g = this.config).headers) == null ? void 0 : _h.call(_g), headers),
1758
+ headers: combineHeaders4(this.config.headers?.(), headers),
1785
1759
  body: {
1786
1760
  model: this.modelId,
1787
1761
  prompt,
@@ -1790,7 +1764,7 @@ var OpenAICompatibleImageModel = class _OpenAICompatibleImageModel {
1790
1764
  ...args
1791
1765
  },
1792
1766
  failedResponseHandler: createJsonErrorResponseHandler4(
1793
- (_i = this.config.errorStructure) != null ? _i : defaultOpenAICompatibleErrorStructure
1767
+ this.config.errorStructure ?? defaultOpenAICompatibleErrorStructure
1794
1768
  ),
1795
1769
  successfulResponseHandler: createJsonResponseHandler4(
1796
1770
  openaiCompatibleImageResponseSchema
@@ -1811,11 +1785,10 @@ var OpenAICompatibleImageModel = class _OpenAICompatibleImageModel {
1811
1785
  }
1812
1786
  };
1813
1787
  function mapImageUsage(usage) {
1814
- var _a, _b, _c;
1815
1788
  return usage == null ? void 0 : {
1816
- inputTokens: (_a = usage.input_tokens) != null ? _a : void 0,
1817
- outputTokens: (_b = usage.output_tokens) != null ? _b : void 0,
1818
- totalTokens: (_c = usage.total_tokens) != null ? _c : void 0
1789
+ inputTokens: usage.input_tokens ?? void 0,
1790
+ outputTokens: usage.output_tokens ?? void 0,
1791
+ totalTokens: usage.total_tokens ?? void 0
1819
1792
  };
1820
1793
  }
1821
1794
  var openaiCompatibleImageResponseSchema = z8.object({
@@ -1841,7 +1814,7 @@ import {
1841
1814
  } from "@ai-sdk/provider-utils";
1842
1815
 
1843
1816
  // src/version.ts
1844
- var VERSION = true ? "3.0.54" : "0.0.0-test";
1817
+ var VERSION = true ? "3.0.57" : "0.0.0-test";
1845
1818
 
1846
1819
  // src/openai-compatible-provider.ts
1847
1820
  function createOpenAICompatible(options) {