@ai-sdk/openai-compatible 1.0.60 → 1.0.62

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
@@ -20,7 +20,11 @@ import { z as z3 } from "zod/v4";
20
20
  import {
21
21
  UnsupportedFunctionalityError
22
22
  } from "@ai-sdk/provider";
23
- import { convertToBase64 } from "@ai-sdk/provider-utils";
23
+ import {
24
+ stringifyWithBigIntAndCycles,
25
+ secureJsonParse,
26
+ convertToBase64
27
+ } from "@ai-sdk/provider-utils";
24
28
  function getOpenAIMetadata(message) {
25
29
  var _a, _b;
26
30
  return (_b = (_a = message == null ? void 0 : message.providerOptions) == null ? void 0 : _a.openaiCompatible) != null ? _b : {};
@@ -94,7 +98,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
94
98
  type: "function",
95
99
  function: {
96
100
  name: part.toolName,
97
- arguments: JSON.stringify(part.input)
101
+ arguments: serializeToolCallArguments(part.input)
98
102
  },
99
103
  ...partMetadata
100
104
  });
@@ -123,7 +127,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
123
127
  case "content":
124
128
  case "json":
125
129
  case "error-json":
126
- contentValue = JSON.stringify(output.value);
130
+ contentValue = stringifyWithBigIntAndCycles(output.value);
127
131
  break;
128
132
  }
129
133
  const toolResponseMetadata = getOpenAIMetadata(toolResponse);
@@ -144,6 +148,17 @@ function convertToOpenAICompatibleChatMessages(prompt) {
144
148
  }
145
149
  return messages;
146
150
  }
151
+ function serializeToolCallArguments(input) {
152
+ if (typeof input === "string") {
153
+ try {
154
+ secureJsonParse(input);
155
+ return input;
156
+ } catch (e) {
157
+ return JSON.stringify(input);
158
+ }
159
+ }
160
+ return stringifyWithBigIntAndCycles(input);
161
+ }
147
162
 
148
163
  // src/chat/get-response-metadata.ts
149
164
  function getResponseMetadata({
@@ -159,10 +174,10 @@ function getResponseMetadata({
159
174
  }
160
175
 
161
176
  // src/chat/map-openai-compatible-finish-reason.ts
162
- function mapOpenAICompatibleFinishReason(finishReason) {
177
+ function mapOpenAICompatibleFinishReason(finishReason, { hasRefusal = false } = {}) {
163
178
  switch (finishReason) {
164
179
  case "stop":
165
- return "stop";
180
+ return hasRefusal ? "content-filter" : "stop";
166
181
  case "length":
167
182
  return "length";
168
183
  case "content_filter":
@@ -291,9 +306,9 @@ var OpenAICompatibleChatLanguageModel = class {
291
306
  var _a, _b, _c;
292
307
  return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
293
308
  }
294
- transformRequestBody(args) {
309
+ transformRequestBody(args, warnings) {
295
310
  var _a, _b, _c;
296
- return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
311
+ return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args, warnings)) != null ? _c : args;
297
312
  }
298
313
  convertUsage(usage) {
299
314
  var _a, _b, _c;
@@ -389,7 +404,7 @@ var OpenAICompatibleChatLanguageModel = class {
389
404
  async doGenerate(options) {
390
405
  var _a, _b, _c, _d, _e;
391
406
  const { args, warnings } = await this.getArgs({ ...options });
392
- const transformedArgs = this.transformRequestBody(args);
407
+ const transformedArgs = this.transformRequestBody(args, warnings);
393
408
  const body = JSON.stringify(transformedArgs);
394
409
  const {
395
410
  responseHeaders,
@@ -421,6 +436,11 @@ var OpenAICompatibleChatLanguageModel = class {
421
436
  if (text != null && text.length > 0) {
422
437
  content.push({ type: "text", text });
423
438
  }
439
+ const refusal = choice.message.refusal;
440
+ const hasRefusal = refusal != null && refusal.length > 0;
441
+ if (hasRefusal) {
442
+ content.push({ type: "text", text: refusal });
443
+ }
424
444
  const reasoning = (_a = choice.message.reasoning_content) != null ? _a : choice.message.reasoning;
425
445
  if (reasoning != null && reasoning.length > 0) {
426
446
  content.push({
@@ -453,7 +473,9 @@ var OpenAICompatibleChatLanguageModel = class {
453
473
  }
454
474
  return {
455
475
  content,
456
- finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
476
+ finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason, {
477
+ hasRefusal
478
+ }),
457
479
  usage: this.convertUsage(responseBody.usage),
458
480
  providerMetadata,
459
481
  request: { body },
@@ -468,12 +490,15 @@ var OpenAICompatibleChatLanguageModel = class {
468
490
  async doStream(options) {
469
491
  var _a;
470
492
  const { args, warnings } = await this.getArgs({ ...options });
471
- const body = this.transformRequestBody({
472
- ...args,
473
- stream: true,
474
- // only include stream_options when in strict compatibility mode:
475
- stream_options: this.config.includeUsage ? { include_usage: true } : void 0
476
- });
493
+ const body = this.transformRequestBody(
494
+ {
495
+ ...args,
496
+ stream: true,
497
+ // only include stream_options when in strict compatibility mode:
498
+ stream_options: this.config.includeUsage ? { include_usage: true } : void 0
499
+ },
500
+ warnings
501
+ );
477
502
  const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
478
503
  const { responseHeaders, value: response } = await postJsonToApi({
479
504
  url: this.config.url({
@@ -491,6 +516,8 @@ var OpenAICompatibleChatLanguageModel = class {
491
516
  });
492
517
  let toolCallTracker;
493
518
  let finishReason = "unknown";
519
+ let rawFinishReason = void 0;
520
+ let hasRefusal = false;
494
521
  const usage = {
495
522
  completionTokens: void 0,
496
523
  completionTokensDetails: {
@@ -567,9 +594,7 @@ var OpenAICompatibleChatLanguageModel = class {
567
594
  }
568
595
  }
569
596
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
570
- finishReason = mapOpenAICompatibleFinishReason(
571
- choice.finish_reason
572
- );
597
+ rawFinishReason = choice.finish_reason;
573
598
  }
574
599
  if ((choice == null ? void 0 : choice.delta) == null) {
575
600
  return;
@@ -601,6 +626,19 @@ var OpenAICompatibleChatLanguageModel = class {
601
626
  delta: delta.content
602
627
  });
603
628
  }
629
+ const refusal = delta.refusal;
630
+ if (refusal != null && refusal.length > 0) {
631
+ hasRefusal = true;
632
+ if (!isActiveText) {
633
+ controller.enqueue({ type: "text-start", id: "txt-0" });
634
+ isActiveText = true;
635
+ }
636
+ controller.enqueue({
637
+ type: "text-delta",
638
+ id: "txt-0",
639
+ delta: refusal
640
+ });
641
+ }
604
642
  if (delta.tool_calls != null) {
605
643
  for (const [
606
644
  fallbackIndex,
@@ -621,6 +659,11 @@ var OpenAICompatibleChatLanguageModel = class {
621
659
  controller.enqueue({ type: "text-end", id: "txt-0" });
622
660
  }
623
661
  toolCallTracker.flush();
662
+ if (rawFinishReason != null) {
663
+ finishReason = mapOpenAICompatibleFinishReason(rawFinishReason, {
664
+ hasRefusal
665
+ });
666
+ }
624
667
  const providerMetadata = {
625
668
  [providerOptionsName]: {},
626
669
  ...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
@@ -679,6 +722,12 @@ var OpenAICompatibleChatResponseSchema = lazySchema(
679
722
  message: z3.object({
680
723
  role: z3.literal("assistant").nullish(),
681
724
  content: z3.string().nullish(),
725
+ // set instead of `content` when the model refuses to answer. Some
726
+ // OpenAI-compatible servers send non-string values (e.g. `false`),
727
+ // which are ignored instead of failing the whole response:
728
+ refusal: z3.unknown().nullish().transform(
729
+ (value) => typeof value === "string" ? value : void 0
730
+ ),
682
731
  reasoning_content: z3.string().nullish(),
683
732
  reasoning: z3.string().nullish(),
684
733
  tool_calls: z3.array(
@@ -708,6 +757,12 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => z3.union([
708
757
  delta: z3.object({
709
758
  role: z3.enum(["assistant"]).nullish(),
710
759
  content: z3.string().nullish(),
760
+ // set instead of `content` when the model refuses to answer. Some
761
+ // OpenAI-compatible servers send non-string values (e.g. `false`),
762
+ // which are ignored instead of failing the whole response:
763
+ refusal: z3.unknown().nullish().transform(
764
+ (value) => typeof value === "string" ? value : void 0
765
+ ),
711
766
  // Most openai-compatible models set `reasoning_content`, but some
712
767
  // providers serving `gpt-oss` set `reasoning`. See #7866
713
768
  reasoning_content: z3.string().nullish(),
@@ -1382,7 +1437,7 @@ import {
1382
1437
  } from "@ai-sdk/provider-utils";
1383
1438
 
1384
1439
  // src/version.ts
1385
- var VERSION = true ? "1.0.60" : "0.0.0-test";
1440
+ var VERSION = true ? "1.0.62" : "0.0.0-test";
1386
1441
 
1387
1442
  // src/openai-compatible-provider.ts
1388
1443
  function createOpenAICompatible(options) {