@matthewdunbar/amazon-bedrock-mantle 2.0.31 → 2.0.32
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.
Potentially problematic release.
This version of @matthewdunbar/amazon-bedrock-mantle might be problematic. Click here for more details.
- package/dist/index.js +134 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +134 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/chat/openai-compatible-chat-language-model.ts +51 -3
package/dist/index.js
CHANGED
|
@@ -617,7 +617,97 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
617
617
|
};
|
|
618
618
|
}
|
|
619
619
|
async doStream(options) {
|
|
620
|
-
var _a;
|
|
620
|
+
var _a, _b, _c;
|
|
621
|
+
const shouldUseNonStreamingUsageFallback = this.providerOptionsName.includes("bedrock") && ((_a = this.config.includeUsage) != null ? _a : false);
|
|
622
|
+
if (shouldUseNonStreamingUsageFallback) {
|
|
623
|
+
const generateResult = await this.doGenerate(options);
|
|
624
|
+
const finishReason2 = this.config.legacyStringFinishReason ? generateResult.finishReason.unified : generateResult.finishReason;
|
|
625
|
+
return {
|
|
626
|
+
stream: new ReadableStream({
|
|
627
|
+
start(controller) {
|
|
628
|
+
controller.enqueue({
|
|
629
|
+
type: "stream-start",
|
|
630
|
+
warnings: generateResult.warnings
|
|
631
|
+
});
|
|
632
|
+
if (generateResult.response != null) {
|
|
633
|
+
controller.enqueue({
|
|
634
|
+
type: "response-metadata",
|
|
635
|
+
id: generateResult.response.id,
|
|
636
|
+
timestamp: generateResult.response.timestamp,
|
|
637
|
+
modelId: generateResult.response.modelId
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
let textIndex = 0;
|
|
641
|
+
let reasoningIndex = 0;
|
|
642
|
+
for (const contentPart of generateResult.content) {
|
|
643
|
+
switch (contentPart.type) {
|
|
644
|
+
case "text": {
|
|
645
|
+
const id = `txt-${textIndex++}`;
|
|
646
|
+
controller.enqueue({ type: "text-start", id });
|
|
647
|
+
controller.enqueue({
|
|
648
|
+
type: "text-delta",
|
|
649
|
+
id,
|
|
650
|
+
delta: contentPart.text
|
|
651
|
+
});
|
|
652
|
+
controller.enqueue({ type: "text-end", id });
|
|
653
|
+
break;
|
|
654
|
+
}
|
|
655
|
+
case "reasoning": {
|
|
656
|
+
const id = `reasoning-${reasoningIndex++}`;
|
|
657
|
+
controller.enqueue({ type: "reasoning-start", id });
|
|
658
|
+
controller.enqueue({
|
|
659
|
+
type: "reasoning-delta",
|
|
660
|
+
id,
|
|
661
|
+
delta: contentPart.text,
|
|
662
|
+
...contentPart.providerMetadata != null ? { providerMetadata: contentPart.providerMetadata } : {}
|
|
663
|
+
});
|
|
664
|
+
controller.enqueue({ type: "reasoning-end", id });
|
|
665
|
+
break;
|
|
666
|
+
}
|
|
667
|
+
case "tool-call": {
|
|
668
|
+
controller.enqueue({
|
|
669
|
+
type: "tool-input-start",
|
|
670
|
+
id: contentPart.toolCallId,
|
|
671
|
+
toolName: contentPart.toolName,
|
|
672
|
+
...contentPart.providerMetadata != null ? { providerMetadata: contentPart.providerMetadata } : {}
|
|
673
|
+
});
|
|
674
|
+
if (contentPart.input.length > 0) {
|
|
675
|
+
controller.enqueue({
|
|
676
|
+
type: "tool-input-delta",
|
|
677
|
+
id: contentPart.toolCallId,
|
|
678
|
+
delta: contentPart.input,
|
|
679
|
+
...contentPart.providerMetadata != null ? { providerMetadata: contentPart.providerMetadata } : {}
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
controller.enqueue({
|
|
683
|
+
type: "tool-input-end",
|
|
684
|
+
id: contentPart.toolCallId,
|
|
685
|
+
...contentPart.providerMetadata != null ? { providerMetadata: contentPart.providerMetadata } : {}
|
|
686
|
+
});
|
|
687
|
+
controller.enqueue(contentPart);
|
|
688
|
+
break;
|
|
689
|
+
}
|
|
690
|
+
case "tool-result":
|
|
691
|
+
case "tool-approval-request":
|
|
692
|
+
case "source":
|
|
693
|
+
case "file":
|
|
694
|
+
controller.enqueue(contentPart);
|
|
695
|
+
break;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
controller.enqueue({
|
|
699
|
+
type: "finish",
|
|
700
|
+
finishReason: finishReason2,
|
|
701
|
+
usage: generateResult.usage,
|
|
702
|
+
...generateResult.providerMetadata != null ? { providerMetadata: generateResult.providerMetadata } : {}
|
|
703
|
+
});
|
|
704
|
+
controller.close();
|
|
705
|
+
}
|
|
706
|
+
}),
|
|
707
|
+
request: generateResult.request,
|
|
708
|
+
response: generateResult.response == null ? void 0 : { headers: generateResult.response.headers }
|
|
709
|
+
};
|
|
710
|
+
}
|
|
621
711
|
const { args, warnings } = await this.getArgs({ ...options });
|
|
622
712
|
const body = this.transformRequestBody({
|
|
623
713
|
...args,
|
|
@@ -625,7 +715,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
625
715
|
// only include stream_options when in strict compatibility mode:
|
|
626
716
|
stream_options: this.config.includeUsage ? { include_usage: true } : void 0
|
|
627
717
|
});
|
|
628
|
-
const metadataExtractor = (
|
|
718
|
+
const metadataExtractor = (_b = this.config.metadataExtractor) == null ? void 0 : _b.createStreamExtractor();
|
|
629
719
|
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
|
|
630
720
|
url: this.config.url({
|
|
631
721
|
path: "/chat/completions",
|
|
@@ -649,11 +739,31 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
649
739
|
let isFirstChunk = true;
|
|
650
740
|
const providerOptionsName = this.providerOptionsName;
|
|
651
741
|
const shouldTerminateOnFinishReason = providerOptionsName.includes("bedrock");
|
|
742
|
+
const includeUsage = (_c = this.config.includeUsage) != null ? _c : false;
|
|
652
743
|
let isActiveReasoning = false;
|
|
653
744
|
let isActiveText = false;
|
|
654
745
|
let hasFinished = false;
|
|
746
|
+
let hasSeenFinishReason = false;
|
|
747
|
+
let forcedFinishTimer;
|
|
748
|
+
const clearForcedFinishTimer = () => {
|
|
749
|
+
if (forcedFinishTimer == null) {
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
clearTimeout(forcedFinishTimer);
|
|
753
|
+
forcedFinishTimer = void 0;
|
|
754
|
+
};
|
|
755
|
+
const scheduleForcedFinish = (controller) => {
|
|
756
|
+
clearForcedFinishTimer();
|
|
757
|
+
forcedFinishTimer = setTimeout(() => {
|
|
758
|
+
if (hasFinished) {
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
emitFinish(controller);
|
|
762
|
+
controller.terminate();
|
|
763
|
+
}, 750);
|
|
764
|
+
};
|
|
655
765
|
const emitFinish = (controller) => {
|
|
656
|
-
var _a2,
|
|
766
|
+
var _a2, _b2, _c2, _d, _e;
|
|
657
767
|
if (hasFinished) {
|
|
658
768
|
return;
|
|
659
769
|
}
|
|
@@ -690,8 +800,8 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
690
800
|
[providerOptionsName]: {},
|
|
691
801
|
...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
|
|
692
802
|
};
|
|
693
|
-
if (((
|
|
694
|
-
providerMetadata[providerOptionsName].acceptedPredictionTokens = (
|
|
803
|
+
if (((_b2 = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _b2.accepted_prediction_tokens) != null) {
|
|
804
|
+
providerMetadata[providerOptionsName].acceptedPredictionTokens = (_c2 = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _c2.accepted_prediction_tokens;
|
|
695
805
|
}
|
|
696
806
|
if (((_d = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _d.rejected_prediction_tokens) != null) {
|
|
697
807
|
providerMetadata[providerOptionsName].rejectedPredictionTokens = (_e = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _e.rejected_prediction_tokens;
|
|
@@ -712,7 +822,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
712
822
|
controller.enqueue({ type: "stream-start", warnings });
|
|
713
823
|
},
|
|
714
824
|
transform(chunk, controller) {
|
|
715
|
-
var _a2,
|
|
825
|
+
var _a2, _b2, _c2, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
|
|
716
826
|
if (options.includeRawChunks) {
|
|
717
827
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
718
828
|
}
|
|
@@ -740,17 +850,28 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
740
850
|
}
|
|
741
851
|
if (value.usage != null) {
|
|
742
852
|
usage = value.usage;
|
|
853
|
+
if (shouldTerminateOnFinishReason && hasSeenFinishReason && !hasFinished) {
|
|
854
|
+
clearForcedFinishTimer();
|
|
855
|
+
emitFinish(controller);
|
|
856
|
+
controller.terminate();
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
743
859
|
}
|
|
744
860
|
const choice = value.choices[0];
|
|
745
861
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
862
|
+
hasSeenFinishReason = true;
|
|
746
863
|
finishReason = {
|
|
747
864
|
unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
|
748
865
|
raw: (_a2 = choice.finish_reason) != null ? _a2 : void 0
|
|
749
866
|
};
|
|
750
867
|
if (shouldTerminateOnFinishReason) {
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
868
|
+
if (!includeUsage || usage != null) {
|
|
869
|
+
clearForcedFinishTimer();
|
|
870
|
+
emitFinish(controller);
|
|
871
|
+
controller.terminate();
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
scheduleForcedFinish(controller);
|
|
754
875
|
}
|
|
755
876
|
}
|
|
756
877
|
if ((choice == null ? void 0 : choice.delta) == null) {
|
|
@@ -799,7 +920,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
799
920
|
isActiveReasoning = false;
|
|
800
921
|
}
|
|
801
922
|
for (const toolCallDelta of delta.tool_calls) {
|
|
802
|
-
const index = (
|
|
923
|
+
const index = (_b2 = toolCallDelta.index) != null ? _b2 : toolCalls.length;
|
|
803
924
|
if (toolCalls[index] == null) {
|
|
804
925
|
if (toolCallDelta.id == null) {
|
|
805
926
|
throw new import_provider3.InvalidResponseDataError({
|
|
@@ -807,7 +928,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
807
928
|
message: `Expected 'id' to be a string.`
|
|
808
929
|
});
|
|
809
930
|
}
|
|
810
|
-
if (((
|
|
931
|
+
if (((_c2 = toolCallDelta.function) == null ? void 0 : _c2.name) == null) {
|
|
811
932
|
throw new import_provider3.InvalidResponseDataError({
|
|
812
933
|
data: toolCallDelta,
|
|
813
934
|
message: `Expected 'function.name' to be a string.`
|
|
@@ -896,6 +1017,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
896
1017
|
}
|
|
897
1018
|
},
|
|
898
1019
|
flush(controller) {
|
|
1020
|
+
clearForcedFinishTimer();
|
|
899
1021
|
emitFinish(controller);
|
|
900
1022
|
}
|
|
901
1023
|
})
|
|
@@ -1705,7 +1827,7 @@ var import_provider_utils6 = require("@ai-sdk/provider-utils");
|
|
|
1705
1827
|
var import_aws4fetch = require("aws4fetch");
|
|
1706
1828
|
|
|
1707
1829
|
// src/version.ts
|
|
1708
|
-
var VERSION = true ? "2.0.
|
|
1830
|
+
var VERSION = true ? "2.0.32" : "0.0.0-test";
|
|
1709
1831
|
|
|
1710
1832
|
// src/bedrock-mantle-sigv4-fetch.ts
|
|
1711
1833
|
function createMantleSigV4FetchFunction(getCredentials, fetch = globalThis.fetch) {
|