@ai-sdk/openai-compatible 0.2.3 → 0.2.5

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
@@ -365,7 +365,7 @@ var OpenAICompatibleChatLanguageModel = class {
365
365
  }
366
366
  }
367
367
  async doGenerate(options) {
368
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
368
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
369
369
  const { args, warnings } = this.getArgs({ ...options });
370
370
  const body = JSON.stringify(args);
371
371
  const {
@@ -388,13 +388,30 @@ var OpenAICompatibleChatLanguageModel = class {
388
388
  });
389
389
  const { messages: rawPrompt, ...rawSettings } = args;
390
390
  const choice = responseBody.choices[0];
391
- const providerMetadata = (_b = (_a = this.config.metadataExtractor) == null ? void 0 : _a.extractMetadata) == null ? void 0 : _b.call(_a, {
392
- parsedBody: rawResponse
393
- });
391
+ const providerMetadata = {
392
+ [this.providerOptionsName]: {},
393
+ ...(_b = (_a = this.config.metadataExtractor) == null ? void 0 : _a.extractMetadata) == null ? void 0 : _b.call(_a, {
394
+ parsedBody: rawResponse
395
+ })
396
+ };
397
+ const completionTokenDetails = (_c = responseBody.usage) == null ? void 0 : _c.completion_tokens_details;
398
+ const promptTokenDetails = (_d = responseBody.usage) == null ? void 0 : _d.prompt_tokens_details;
399
+ if ((completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens) != null) {
400
+ providerMetadata[this.providerOptionsName].reasoningTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens;
401
+ }
402
+ if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
403
+ providerMetadata[this.providerOptionsName].acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
404
+ }
405
+ if ((completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens) != null) {
406
+ providerMetadata[this.providerOptionsName].rejectedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens;
407
+ }
408
+ if ((promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens) != null) {
409
+ providerMetadata[this.providerOptionsName].cachedPromptTokens = promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens;
410
+ }
394
411
  return {
395
- text: (_c = choice.message.content) != null ? _c : void 0,
396
- reasoning: (_d = choice.message.reasoning_content) != null ? _d : void 0,
397
- toolCalls: (_e = choice.message.tool_calls) == null ? void 0 : _e.map((toolCall) => {
412
+ text: (_e = choice.message.content) != null ? _e : void 0,
413
+ reasoning: (_f = choice.message.reasoning_content) != null ? _f : void 0,
414
+ toolCalls: (_g = choice.message.tool_calls) == null ? void 0 : _g.map((toolCall) => {
398
415
  var _a2;
399
416
  return {
400
417
  toolCallType: "function",
@@ -405,10 +422,10 @@ var OpenAICompatibleChatLanguageModel = class {
405
422
  }),
406
423
  finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
407
424
  usage: {
408
- promptTokens: (_g = (_f = responseBody.usage) == null ? void 0 : _f.prompt_tokens) != null ? _g : NaN,
409
- completionTokens: (_i = (_h = responseBody.usage) == null ? void 0 : _h.completion_tokens) != null ? _i : NaN
425
+ promptTokens: (_i = (_h = responseBody.usage) == null ? void 0 : _h.prompt_tokens) != null ? _i : NaN,
426
+ completionTokens: (_k = (_j = responseBody.usage) == null ? void 0 : _j.completion_tokens) != null ? _k : NaN
410
427
  },
411
- ...providerMetadata && { providerMetadata },
428
+ providerMetadata,
412
429
  rawCall: { rawPrompt, rawSettings },
413
430
  rawResponse: { headers: responseHeaders, body: rawResponse },
414
431
  response: getResponseMetadata(responseBody),
@@ -495,16 +512,25 @@ var OpenAICompatibleChatLanguageModel = class {
495
512
  const toolCalls = [];
496
513
  let finishReason = "unknown";
497
514
  let usage = {
515
+ completionTokens: void 0,
516
+ completionTokensDetails: {
517
+ reasoningTokens: void 0,
518
+ acceptedPredictionTokens: void 0,
519
+ rejectedPredictionTokens: void 0
520
+ },
498
521
  promptTokens: void 0,
499
- completionTokens: void 0
522
+ promptTokensDetails: {
523
+ cachedTokens: void 0
524
+ }
500
525
  };
501
526
  let isFirstChunk = true;
527
+ let providerOptionsName = this.providerOptionsName;
502
528
  return {
503
529
  stream: response.pipeThrough(
504
530
  new TransformStream({
505
531
  // TODO we lost type safety on Chunk, most likely due to the error schema. MUST FIX
506
532
  transform(chunk, controller) {
507
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
533
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
508
534
  if (!chunk.success) {
509
535
  finishReason = "error";
510
536
  controller.enqueue({ type: "error", error: chunk.error });
@@ -525,10 +551,26 @@ var OpenAICompatibleChatLanguageModel = class {
525
551
  });
526
552
  }
527
553
  if (value.usage != null) {
528
- usage = {
529
- promptTokens: (_a2 = value.usage.prompt_tokens) != null ? _a2 : void 0,
530
- completionTokens: (_b = value.usage.completion_tokens) != null ? _b : void 0
531
- };
554
+ const {
555
+ prompt_tokens,
556
+ completion_tokens,
557
+ prompt_tokens_details,
558
+ completion_tokens_details
559
+ } = value.usage;
560
+ usage.promptTokens = prompt_tokens != null ? prompt_tokens : void 0;
561
+ usage.completionTokens = completion_tokens != null ? completion_tokens : void 0;
562
+ if ((completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens) != null) {
563
+ usage.completionTokensDetails.reasoningTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens;
564
+ }
565
+ if ((completion_tokens_details == null ? void 0 : completion_tokens_details.accepted_prediction_tokens) != null) {
566
+ usage.completionTokensDetails.acceptedPredictionTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.accepted_prediction_tokens;
567
+ }
568
+ if ((completion_tokens_details == null ? void 0 : completion_tokens_details.rejected_prediction_tokens) != null) {
569
+ usage.completionTokensDetails.rejectedPredictionTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.rejected_prediction_tokens;
570
+ }
571
+ if ((prompt_tokens_details == null ? void 0 : prompt_tokens_details.cached_tokens) != null) {
572
+ usage.promptTokensDetails.cachedTokens = prompt_tokens_details == null ? void 0 : prompt_tokens_details.cached_tokens;
573
+ }
532
574
  }
533
575
  const choice = value.choices[0];
534
576
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
@@ -568,7 +610,7 @@ var OpenAICompatibleChatLanguageModel = class {
568
610
  message: `Expected 'id' to be a string.`
569
611
  });
570
612
  }
571
- if (((_c = toolCallDelta.function) == null ? void 0 : _c.name) == null) {
613
+ if (((_a2 = toolCallDelta.function) == null ? void 0 : _a2.name) == null) {
572
614
  throw new InvalidResponseDataError({
573
615
  data: toolCallDelta,
574
616
  message: `Expected 'function.name' to be a string.`
@@ -579,12 +621,12 @@ var OpenAICompatibleChatLanguageModel = class {
579
621
  type: "function",
580
622
  function: {
581
623
  name: toolCallDelta.function.name,
582
- arguments: (_d = toolCallDelta.function.arguments) != null ? _d : ""
624
+ arguments: (_b = toolCallDelta.function.arguments) != null ? _b : ""
583
625
  },
584
626
  hasFinished: false
585
627
  };
586
628
  const toolCall2 = toolCalls[index];
587
- if (((_e = toolCall2.function) == null ? void 0 : _e.name) != null && ((_f = toolCall2.function) == null ? void 0 : _f.arguments) != null) {
629
+ if (((_c = toolCall2.function) == null ? void 0 : _c.name) != null && ((_d = toolCall2.function) == null ? void 0 : _d.arguments) != null) {
588
630
  if (toolCall2.function.arguments.length > 0) {
589
631
  controller.enqueue({
590
632
  type: "tool-call-delta",
@@ -598,7 +640,7 @@ var OpenAICompatibleChatLanguageModel = class {
598
640
  controller.enqueue({
599
641
  type: "tool-call",
600
642
  toolCallType: "function",
601
- toolCallId: (_g = toolCall2.id) != null ? _g : generateId(),
643
+ toolCallId: (_e = toolCall2.id) != null ? _e : generateId(),
602
644
  toolName: toolCall2.function.name,
603
645
  args: toolCall2.function.arguments
604
646
  });
@@ -611,21 +653,21 @@ var OpenAICompatibleChatLanguageModel = class {
611
653
  if (toolCall.hasFinished) {
612
654
  continue;
613
655
  }
614
- if (((_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null) {
615
- toolCall.function.arguments += (_j = (_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null ? _j : "";
656
+ if (((_f = toolCallDelta.function) == null ? void 0 : _f.arguments) != null) {
657
+ toolCall.function.arguments += (_h = (_g = toolCallDelta.function) == null ? void 0 : _g.arguments) != null ? _h : "";
616
658
  }
617
659
  controller.enqueue({
618
660
  type: "tool-call-delta",
619
661
  toolCallType: "function",
620
662
  toolCallId: toolCall.id,
621
663
  toolName: toolCall.function.name,
622
- argsTextDelta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
664
+ argsTextDelta: (_i = toolCallDelta.function.arguments) != null ? _i : ""
623
665
  });
624
- if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && isParsableJson(toolCall.function.arguments)) {
666
+ if (((_j = toolCall.function) == null ? void 0 : _j.name) != null && ((_k = toolCall.function) == null ? void 0 : _k.arguments) != null && isParsableJson(toolCall.function.arguments)) {
625
667
  controller.enqueue({
626
668
  type: "tool-call",
627
669
  toolCallType: "function",
628
- toolCallId: (_n = toolCall.id) != null ? _n : generateId(),
670
+ toolCallId: (_l = toolCall.id) != null ? _l : generateId(),
629
671
  toolName: toolCall.function.name,
630
672
  args: toolCall.function.arguments
631
673
  });
@@ -636,7 +678,22 @@ var OpenAICompatibleChatLanguageModel = class {
636
678
  },
637
679
  flush(controller) {
638
680
  var _a2, _b;
639
- const metadata = metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata();
681
+ const providerMetadata = {
682
+ [providerOptionsName]: {},
683
+ ...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
684
+ };
685
+ if (usage.completionTokensDetails.reasoningTokens != null) {
686
+ providerMetadata[providerOptionsName].reasoningTokens = usage.completionTokensDetails.reasoningTokens;
687
+ }
688
+ if (usage.completionTokensDetails.acceptedPredictionTokens != null) {
689
+ providerMetadata[providerOptionsName].acceptedPredictionTokens = usage.completionTokensDetails.acceptedPredictionTokens;
690
+ }
691
+ if (usage.completionTokensDetails.rejectedPredictionTokens != null) {
692
+ providerMetadata[providerOptionsName].rejectedPredictionTokens = usage.completionTokensDetails.rejectedPredictionTokens;
693
+ }
694
+ if (usage.promptTokensDetails.cachedTokens != null) {
695
+ providerMetadata[providerOptionsName].cachedPromptTokens = usage.promptTokensDetails.cachedTokens;
696
+ }
640
697
  controller.enqueue({
641
698
  type: "finish",
642
699
  finishReason,
@@ -644,7 +701,7 @@ var OpenAICompatibleChatLanguageModel = class {
644
701
  promptTokens: (_a2 = usage.promptTokens) != null ? _a2 : NaN,
645
702
  completionTokens: (_b = usage.completionTokens) != null ? _b : NaN
646
703
  },
647
- ...metadata && { providerMetadata: metadata }
704
+ providerMetadata
648
705
  });
649
706
  }
650
707
  })
@@ -656,6 +713,18 @@ var OpenAICompatibleChatLanguageModel = class {
656
713
  };
657
714
  }
658
715
  };
716
+ var openaiCompatibleTokenUsageSchema = z2.object({
717
+ prompt_tokens: z2.number().nullish(),
718
+ completion_tokens: z2.number().nullish(),
719
+ prompt_tokens_details: z2.object({
720
+ cached_tokens: z2.number().nullish()
721
+ }).nullish(),
722
+ completion_tokens_details: z2.object({
723
+ reasoning_tokens: z2.number().nullish(),
724
+ accepted_prediction_tokens: z2.number().nullish(),
725
+ rejected_prediction_tokens: z2.number().nullish()
726
+ }).nullish()
727
+ }).nullish();
659
728
  var OpenAICompatibleChatResponseSchema = z2.object({
660
729
  id: z2.string().nullish(),
661
730
  created: z2.number().nullish(),
@@ -680,10 +749,7 @@ var OpenAICompatibleChatResponseSchema = z2.object({
680
749
  finish_reason: z2.string().nullish()
681
750
  })
682
751
  ),
683
- usage: z2.object({
684
- prompt_tokens: z2.number().nullish(),
685
- completion_tokens: z2.number().nullish()
686
- }).nullish()
752
+ usage: openaiCompatibleTokenUsageSchema
687
753
  });
688
754
  var createOpenAICompatibleChatChunkSchema = (errorSchema) => z2.union([
689
755
  z2.object({
@@ -711,10 +777,7 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => z2.union([
711
777
  finish_reason: z2.string().nullish()
712
778
  })
713
779
  ),
714
- usage: z2.object({
715
- prompt_tokens: z2.number().nullish(),
716
- completion_tokens: z2.number().nullish()
717
- }).nullish()
780
+ usage: openaiCompatibleTokenUsageSchema
718
781
  }),
719
782
  errorSchema
720
783
  ]);