@ai-sdk/openai-compatible 1.0.61 → 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
@@ -174,10 +174,10 @@ function getResponseMetadata({
174
174
  }
175
175
 
176
176
  // src/chat/map-openai-compatible-finish-reason.ts
177
- function mapOpenAICompatibleFinishReason(finishReason) {
177
+ function mapOpenAICompatibleFinishReason(finishReason, { hasRefusal = false } = {}) {
178
178
  switch (finishReason) {
179
179
  case "stop":
180
- return "stop";
180
+ return hasRefusal ? "content-filter" : "stop";
181
181
  case "length":
182
182
  return "length";
183
183
  case "content_filter":
@@ -306,9 +306,9 @@ var OpenAICompatibleChatLanguageModel = class {
306
306
  var _a, _b, _c;
307
307
  return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
308
308
  }
309
- transformRequestBody(args) {
309
+ transformRequestBody(args, warnings) {
310
310
  var _a, _b, _c;
311
- 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;
312
312
  }
313
313
  convertUsage(usage) {
314
314
  var _a, _b, _c;
@@ -404,7 +404,7 @@ var OpenAICompatibleChatLanguageModel = class {
404
404
  async doGenerate(options) {
405
405
  var _a, _b, _c, _d, _e;
406
406
  const { args, warnings } = await this.getArgs({ ...options });
407
- const transformedArgs = this.transformRequestBody(args);
407
+ const transformedArgs = this.transformRequestBody(args, warnings);
408
408
  const body = JSON.stringify(transformedArgs);
409
409
  const {
410
410
  responseHeaders,
@@ -436,6 +436,11 @@ var OpenAICompatibleChatLanguageModel = class {
436
436
  if (text != null && text.length > 0) {
437
437
  content.push({ type: "text", text });
438
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
+ }
439
444
  const reasoning = (_a = choice.message.reasoning_content) != null ? _a : choice.message.reasoning;
440
445
  if (reasoning != null && reasoning.length > 0) {
441
446
  content.push({
@@ -468,7 +473,9 @@ var OpenAICompatibleChatLanguageModel = class {
468
473
  }
469
474
  return {
470
475
  content,
471
- finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
476
+ finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason, {
477
+ hasRefusal
478
+ }),
472
479
  usage: this.convertUsage(responseBody.usage),
473
480
  providerMetadata,
474
481
  request: { body },
@@ -483,12 +490,15 @@ var OpenAICompatibleChatLanguageModel = class {
483
490
  async doStream(options) {
484
491
  var _a;
485
492
  const { args, warnings } = await this.getArgs({ ...options });
486
- const body = this.transformRequestBody({
487
- ...args,
488
- stream: true,
489
- // only include stream_options when in strict compatibility mode:
490
- stream_options: this.config.includeUsage ? { include_usage: true } : void 0
491
- });
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
+ );
492
502
  const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
493
503
  const { responseHeaders, value: response } = await postJsonToApi({
494
504
  url: this.config.url({
@@ -506,6 +516,8 @@ var OpenAICompatibleChatLanguageModel = class {
506
516
  });
507
517
  let toolCallTracker;
508
518
  let finishReason = "unknown";
519
+ let rawFinishReason = void 0;
520
+ let hasRefusal = false;
509
521
  const usage = {
510
522
  completionTokens: void 0,
511
523
  completionTokensDetails: {
@@ -582,9 +594,7 @@ var OpenAICompatibleChatLanguageModel = class {
582
594
  }
583
595
  }
584
596
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
585
- finishReason = mapOpenAICompatibleFinishReason(
586
- choice.finish_reason
587
- );
597
+ rawFinishReason = choice.finish_reason;
588
598
  }
589
599
  if ((choice == null ? void 0 : choice.delta) == null) {
590
600
  return;
@@ -616,6 +626,19 @@ var OpenAICompatibleChatLanguageModel = class {
616
626
  delta: delta.content
617
627
  });
618
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
+ }
619
642
  if (delta.tool_calls != null) {
620
643
  for (const [
621
644
  fallbackIndex,
@@ -636,6 +659,11 @@ var OpenAICompatibleChatLanguageModel = class {
636
659
  controller.enqueue({ type: "text-end", id: "txt-0" });
637
660
  }
638
661
  toolCallTracker.flush();
662
+ if (rawFinishReason != null) {
663
+ finishReason = mapOpenAICompatibleFinishReason(rawFinishReason, {
664
+ hasRefusal
665
+ });
666
+ }
639
667
  const providerMetadata = {
640
668
  [providerOptionsName]: {},
641
669
  ...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
@@ -694,6 +722,12 @@ var OpenAICompatibleChatResponseSchema = lazySchema(
694
722
  message: z3.object({
695
723
  role: z3.literal("assistant").nullish(),
696
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
+ ),
697
731
  reasoning_content: z3.string().nullish(),
698
732
  reasoning: z3.string().nullish(),
699
733
  tool_calls: z3.array(
@@ -723,6 +757,12 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => z3.union([
723
757
  delta: z3.object({
724
758
  role: z3.enum(["assistant"]).nullish(),
725
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
+ ),
726
766
  // Most openai-compatible models set `reasoning_content`, but some
727
767
  // providers serving `gpt-oss` set `reasoning`. See #7866
728
768
  reasoning_content: z3.string().nullish(),
@@ -1397,7 +1437,7 @@ import {
1397
1437
  } from "@ai-sdk/provider-utils";
1398
1438
 
1399
1439
  // src/version.ts
1400
- var VERSION = true ? "1.0.61" : "0.0.0-test";
1440
+ var VERSION = true ? "1.0.62" : "0.0.0-test";
1401
1441
 
1402
1442
  // src/openai-compatible-provider.ts
1403
1443
  function createOpenAICompatible(options) {