@ai-sdk/openai-compatible 1.0.55 → 1.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.mjs CHANGED
@@ -3,12 +3,12 @@ import {
3
3
  InvalidResponseDataError
4
4
  } from "@ai-sdk/provider";
5
5
  import {
6
+ StreamingToolCallTracker,
6
7
  combineHeaders,
7
8
  createEventSourceResponseHandler,
8
9
  createJsonErrorResponseHandler,
9
10
  createJsonResponseHandler,
10
11
  generateId,
11
- isParsableJson,
12
12
  parseProviderOptions,
13
13
  postJsonToApi
14
14
  } from "@ai-sdk/provider-utils";
@@ -487,7 +487,7 @@ var OpenAICompatibleChatLanguageModel = class {
487
487
  abortSignal: options.abortSignal,
488
488
  fetch: this.config.fetch
489
489
  });
490
- const toolCalls = [];
490
+ let toolCallTracker;
491
491
  let finishReason = "unknown";
492
492
  const usage = {
493
493
  completionTokens: void 0,
@@ -523,11 +523,14 @@ var OpenAICompatibleChatLanguageModel = class {
523
523
  stream: response.pipeThrough(
524
524
  new TransformStream({
525
525
  start(controller) {
526
+ toolCallTracker = new StreamingToolCallTracker(controller, {
527
+ generateId
528
+ });
526
529
  controller.enqueue({ type: "stream-start", warnings });
527
530
  },
528
531
  // TODO we lost type safety on Chunk, most likely due to the error schema. MUST FIX
529
532
  transform(chunk, controller) {
530
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
533
+ var _a2, _b;
531
534
  if (options.includeRawChunks) {
532
535
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
533
536
  }
@@ -601,109 +604,21 @@ var OpenAICompatibleChatLanguageModel = class {
601
604
  fallbackIndex,
602
605
  toolCallDelta
603
606
  ] of delta.tool_calls.entries()) {
604
- const index = (_b = toolCallDelta.index) != null ? _b : fallbackIndex;
605
- if (toolCalls[index] == null) {
606
- if (toolCallDelta.id == null) {
607
- throw new InvalidResponseDataError({
608
- data: toolCallDelta,
609
- message: `Expected 'id' to be a string.`
610
- });
611
- }
612
- if (((_c = toolCallDelta.function) == null ? void 0 : _c.name) == null) {
613
- throw new InvalidResponseDataError({
614
- data: toolCallDelta,
615
- message: `Expected 'function.name' to be a string.`
616
- });
617
- }
618
- controller.enqueue({
619
- type: "tool-input-start",
620
- id: toolCallDelta.id,
621
- toolName: toolCallDelta.function.name
622
- });
623
- toolCalls[index] = {
624
- id: toolCallDelta.id,
625
- type: "function",
626
- function: {
627
- name: toolCallDelta.function.name,
628
- arguments: (_d = toolCallDelta.function.arguments) != null ? _d : ""
629
- },
630
- hasFinished: false
631
- };
632
- const toolCall2 = toolCalls[index];
633
- if (((_e = toolCall2.function) == null ? void 0 : _e.name) != null && ((_f = toolCall2.function) == null ? void 0 : _f.arguments) != null) {
634
- if (toolCall2.function.arguments.length > 0) {
635
- controller.enqueue({
636
- type: "tool-input-delta",
637
- id: toolCall2.id,
638
- delta: toolCall2.function.arguments
639
- });
640
- }
641
- if (isParsableJson(toolCall2.function.arguments)) {
642
- controller.enqueue({
643
- type: "tool-input-end",
644
- id: toolCall2.id
645
- });
646
- controller.enqueue({
647
- type: "tool-call",
648
- toolCallId: (_g = toolCall2.id) != null ? _g : generateId(),
649
- toolName: toolCall2.function.name,
650
- input: toolCall2.function.arguments
651
- });
652
- toolCall2.hasFinished = true;
653
- }
654
- }
655
- continue;
656
- }
657
- const toolCall = toolCalls[index];
658
- if (toolCall.hasFinished) {
659
- continue;
660
- }
661
- if (((_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null) {
662
- toolCall.function.arguments += (_j = (_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null ? _j : "";
663
- }
664
- controller.enqueue({
665
- type: "tool-input-delta",
666
- id: toolCall.id,
667
- delta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
607
+ toolCallTracker.processDelta({
608
+ ...toolCallDelta,
609
+ index: (_b = toolCallDelta.index) != null ? _b : fallbackIndex
668
610
  });
669
- if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && isParsableJson(toolCall.function.arguments)) {
670
- controller.enqueue({
671
- type: "tool-input-end",
672
- id: toolCall.id
673
- });
674
- controller.enqueue({
675
- type: "tool-call",
676
- toolCallId: (_n = toolCall.id) != null ? _n : generateId(),
677
- toolName: toolCall.function.name,
678
- input: toolCall.function.arguments
679
- });
680
- toolCall.hasFinished = true;
681
- }
682
611
  }
683
612
  }
684
613
  },
685
614
  flush(controller) {
686
- var _a2;
687
615
  if (isActiveReasoning) {
688
616
  controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
689
617
  }
690
618
  if (isActiveText) {
691
619
  controller.enqueue({ type: "text-end", id: "txt-0" });
692
620
  }
693
- for (const toolCall of toolCalls.filter(
694
- (toolCall2) => !toolCall2.hasFinished
695
- )) {
696
- controller.enqueue({
697
- type: "tool-input-end",
698
- id: toolCall.id
699
- });
700
- controller.enqueue({
701
- type: "tool-call",
702
- toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
703
- toolName: toolCall.function.name,
704
- input: toolCall.function.arguments
705
- });
706
- }
621
+ toolCallTracker.flush();
707
622
  const providerMetadata = {
708
623
  [providerOptionsName]: {},
709
624
  ...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
@@ -1427,7 +1342,7 @@ import {
1427
1342
  } from "@ai-sdk/provider-utils";
1428
1343
 
1429
1344
  // src/version.ts
1430
- var VERSION = true ? "1.0.55" : "0.0.0-test";
1345
+ var VERSION = true ? "1.0.57" : "0.0.0-test";
1431
1346
 
1432
1347
  // src/openai-compatible-provider.ts
1433
1348
  function createOpenAICompatible(options) {