@ai-sdk/openai-compatible 2.0.89 → 2.0.91

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
@@ -90,6 +90,8 @@ import {
90
90
  UnsupportedFunctionalityError
91
91
  } from "@ai-sdk/provider";
92
92
  import {
93
+ secureJsonParse,
94
+ stringifyWithBigIntAndCycles,
93
95
  convertBase64ToUint8Array,
94
96
  convertToBase64
95
97
  } from "@ai-sdk/provider-utils";
@@ -108,6 +110,17 @@ function getAudioFormat(mediaType) {
108
110
  return null;
109
111
  }
110
112
  }
113
+ function serializeToolCallArguments(input) {
114
+ if (typeof input === "string") {
115
+ try {
116
+ secureJsonParse(input);
117
+ return input;
118
+ } catch (e) {
119
+ return JSON.stringify(input);
120
+ }
121
+ }
122
+ return stringifyWithBigIntAndCycles(input);
123
+ }
111
124
  function convertToOpenAICompatibleChatMessages(prompt) {
112
125
  var _a, _b, _c;
113
126
  const messages = [];
@@ -225,7 +238,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
225
238
  type: "function",
226
239
  function: {
227
240
  name: part.toolName,
228
- arguments: JSON.stringify(part.input)
241
+ arguments: serializeToolCallArguments(part.input)
229
242
  },
230
243
  ...partMetadata,
231
244
  // Include extra_content for Google Gemini thought signatures
@@ -268,7 +281,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
268
281
  case "content":
269
282
  case "json":
270
283
  case "error-json":
271
- contentValue = JSON.stringify(output.value);
284
+ contentValue = stringifyWithBigIntAndCycles(output.value);
272
285
  break;
273
286
  }
274
287
  const toolResponseMetadata = getOpenAIMetadata(toolResponse);
@@ -304,10 +317,10 @@ function getResponseMetadata({
304
317
  }
305
318
 
306
319
  // src/chat/map-openai-compatible-finish-reason.ts
307
- function mapOpenAICompatibleFinishReason(finishReason) {
320
+ function mapOpenAICompatibleFinishReason(finishReason, { hasRefusal = false } = {}) {
308
321
  switch (finishReason) {
309
322
  case "stop":
310
- return "stop";
323
+ return hasRefusal ? "content-filter" : "stop";
311
324
  case "length":
312
325
  return "length";
313
326
  case "content_filter":
@@ -433,9 +446,9 @@ var OpenAICompatibleChatLanguageModel = class {
433
446
  var _a, _b, _c;
434
447
  return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
435
448
  }
436
- transformRequestBody(args) {
449
+ transformRequestBody(args, warnings) {
437
450
  var _a, _b, _c;
438
- return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
451
+ return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args, warnings)) != null ? _c : args;
439
452
  }
440
453
  convertUsage(usage) {
441
454
  var _a, _b, _c;
@@ -560,7 +573,7 @@ var OpenAICompatibleChatLanguageModel = class {
560
573
  async doGenerate(options) {
561
574
  var _a, _b, _c, _d, _e, _f, _g, _h;
562
575
  const { args, warnings, metadataKey } = await this.getArgs({ ...options });
563
- const transformedBody = this.transformRequestBody(args);
576
+ const transformedBody = this.transformRequestBody(args, warnings);
564
577
  const body = JSON.stringify(transformedBody);
565
578
  const {
566
579
  responseHeaders,
@@ -596,6 +609,11 @@ var OpenAICompatibleChatLanguageModel = class {
596
609
  });
597
610
  }
598
611
  content.push(...convertOpenAICompatibleContent(choice.message.content));
612
+ const refusal = choice.message.refusal;
613
+ const hasRefusal = refusal != null && refusal.length > 0;
614
+ if (hasRefusal) {
615
+ content.push({ type: "text", text: refusal });
616
+ }
599
617
  if (choice.message.tool_calls != null) {
600
618
  for (const toolCall of choice.message.tool_calls) {
601
619
  const thoughtSignature = (_c = (_b = toolCall.extra_content) == null ? void 0 : _b.google) == null ? void 0 : _c.thought_signature;
@@ -628,7 +646,9 @@ var OpenAICompatibleChatLanguageModel = class {
628
646
  return {
629
647
  content,
630
648
  finishReason: {
631
- unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
649
+ unified: mapOpenAICompatibleFinishReason(choice.finish_reason, {
650
+ hasRefusal
651
+ }),
632
652
  raw: (_h = choice.finish_reason) != null ? _h : void 0
633
653
  },
634
654
  usage: this.convertUsage(responseBody.usage),
@@ -645,12 +665,15 @@ var OpenAICompatibleChatLanguageModel = class {
645
665
  async doStream(options) {
646
666
  var _a;
647
667
  const { args, warnings, metadataKey } = await this.getArgs({ ...options });
648
- const body = this.transformRequestBody({
649
- ...args,
650
- stream: true,
651
- // only include stream_options when in strict compatibility mode:
652
- stream_options: this.config.includeUsage ? { include_usage: true } : void 0
653
- });
668
+ const body = this.transformRequestBody(
669
+ {
670
+ ...args,
671
+ stream: true,
672
+ // only include stream_options when in strict compatibility mode:
673
+ stream_options: this.config.includeUsage ? { include_usage: true } : void 0
674
+ },
675
+ warnings
676
+ );
654
677
  const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
655
678
  const { responseHeaders, value: response } = await postJsonToApi({
656
679
  url: this.config.url({
@@ -713,10 +736,12 @@ var OpenAICompatibleChatLanguageModel = class {
713
736
  }
714
737
  };
715
738
  let finishReason;
739
+ let rawFinishReason = void 0;
716
740
  let usage = void 0;
717
741
  let isFirstChunk = true;
718
742
  let isActiveReasoning = false;
719
743
  let isActiveText = false;
744
+ let hasRefusal = false;
720
745
  const convertUsage = (usage2) => this.convertUsage(usage2);
721
746
  return {
722
747
  stream: response.pipeThrough(
@@ -737,7 +762,7 @@ var OpenAICompatibleChatLanguageModel = class {
737
762
  controller.enqueue({ type: "stream-start", warnings });
738
763
  },
739
764
  transform(chunk, controller) {
740
- var _a2, _b;
765
+ var _a2;
741
766
  if (options.includeRawChunks) {
742
767
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
743
768
  }
@@ -768,10 +793,7 @@ var OpenAICompatibleChatLanguageModel = class {
768
793
  }
769
794
  const choice = value.choices[0];
770
795
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
771
- finishReason = {
772
- unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
773
- raw: (_a2 = choice.finish_reason) != null ? _a2 : void 0
774
- };
796
+ rawFinishReason = choice.finish_reason;
775
797
  }
776
798
  if ((choice == null ? void 0 : choice.delta) == null) {
777
799
  return;
@@ -813,7 +835,7 @@ var OpenAICompatibleChatLanguageModel = class {
813
835
  delta: textDelta
814
836
  });
815
837
  };
816
- const reasoningContent = (_b = delta.reasoning_content) != null ? _b : delta.reasoning;
838
+ const reasoningContent = (_a2 = delta.reasoning_content) != null ? _a2 : delta.reasoning;
817
839
  if (reasoningContent) {
818
840
  enqueueReasoningDelta(reasoningContent);
819
841
  }
@@ -826,6 +848,11 @@ var OpenAICompatibleChatLanguageModel = class {
826
848
  enqueueTextDelta(contentPart.text);
827
849
  }
828
850
  }
851
+ const refusal = delta.refusal;
852
+ if (refusal != null && refusal.length > 0) {
853
+ hasRefusal = true;
854
+ enqueueTextDelta(refusal);
855
+ }
829
856
  if (delta.tool_calls != null && delta.tool_calls.length > 0) {
830
857
  if (isActiveReasoning) {
831
858
  controller.enqueue({
@@ -856,6 +883,14 @@ var OpenAICompatibleChatLanguageModel = class {
856
883
  }
857
884
  pendingToolCalls.clear();
858
885
  toolCallTracker.flush();
886
+ if (rawFinishReason != null) {
887
+ finishReason = {
888
+ unified: mapOpenAICompatibleFinishReason(rawFinishReason, {
889
+ hasRefusal
890
+ }),
891
+ raw: rawFinishReason
892
+ };
893
+ }
859
894
  if (finishReason == null) {
860
895
  finishReason = { unified: "error", raw: void 0 };
861
896
  controller.enqueue({
@@ -946,6 +981,12 @@ var OpenAICompatibleChatResponseSchema = lazySchema(
946
981
  message: z3.object({
947
982
  role: z3.literal("assistant").nullish(),
948
983
  content: openAICompatibleContentSchema(),
984
+ // set instead of `content` when the model refuses to answer. Some
985
+ // OpenAI-compatible servers send non-string values (e.g. `false`),
986
+ // which are ignored instead of failing the whole response:
987
+ refusal: z3.unknown().nullish().transform(
988
+ (value) => typeof value === "string" ? value : void 0
989
+ ),
949
990
  reasoning_content: z3.string().nullish(),
950
991
  reasoning: z3.string().nullish(),
951
992
  tool_calls: z3.array(
@@ -980,6 +1021,12 @@ var chunkBaseSchema = () => z3.looseObject({
980
1021
  delta: z3.object({
981
1022
  role: z3.enum(["assistant", ""]).nullish(),
982
1023
  content: openAICompatibleContentSchema(),
1024
+ // set instead of `content` when the model refuses to answer. Some
1025
+ // OpenAI-compatible servers send non-string values (e.g. `false`),
1026
+ // which are ignored instead of failing the whole response:
1027
+ refusal: z3.unknown().nullish().transform(
1028
+ (value) => typeof value === "string" ? value : void 0
1029
+ ),
983
1030
  // Most openai-compatible models set `reasoning_content`, but some
984
1031
  // providers serving `gpt-oss` set `reasoning`. See #7866
985
1032
  reasoning_content: z3.string().nullish(),
@@ -1821,7 +1868,7 @@ import {
1821
1868
  } from "@ai-sdk/provider-utils";
1822
1869
 
1823
1870
  // src/version.ts
1824
- var VERSION = true ? "2.0.89" : "0.0.0-test";
1871
+ var VERSION = true ? "2.0.91" : "0.0.0-test";
1825
1872
 
1826
1873
  // src/openai-compatible-provider.ts
1827
1874
  function createOpenAICompatible(options) {