fix: 🐛 Restore files

This commit is contained in:
chezsmithy 2025-08-21 00:21:17 -07:00
parent 7fdce04449
commit e2e5730215
2 changed files with 45 additions and 304 deletions

View file

@ -7,21 +7,13 @@ import {
import Bedrock from "./Bedrock.js";
// Mock AWS SDK
const mockSend = jest.fn();
const mockMiddlewareStackAdd = jest.fn();
jest.mock("@aws-sdk/client-bedrock-runtime", () => ({
BedrockRuntimeClient: jest.fn().mockImplementation(() => ({
send: mockSend,
middlewareStack: { add: mockMiddlewareStackAdd },
send: jest.fn(),
middlewareStack: { add: jest.fn() },
})),
ConverseStreamCommand: jest.fn(),
ConverseCommand: jest.fn(),
InvokeModelCommand: jest.fn(),
ConversationRole: {
USER: "user",
ASSISTANT: "assistant",
},
}));
// Mock credential provider
@ -98,8 +90,6 @@ class TestBedrock extends Bedrock {
describe("Bedrock", () => {
beforeEach(() => {
jest.clearAllMocks();
mockSend.mockReset();
mockMiddlewareStackAdd.mockReset();
});
describe("constructor", () => {
@ -314,142 +304,6 @@ describe("Bedrock", () => {
});
});
describe("streaming vs non-streaming behavior", () => {
let bedrock: Bedrock;
const mockAbortSignal = new AbortController().signal;
beforeEach(() => {
bedrock = new Bedrock({
apiKey: "test-key",
model: "anthropic.claude-3-sonnet-20240229-v1:0",
region: "us-east-1",
});
});
it("should route to streaming path when stream is not false", async () => {
const messages: ChatMessage[] = [
{ role: "user", content: "Hello" } as UserChatMessage,
];
// Mock the private methods by spying on them
const streamingMethodSpy = jest
.spyOn(bedrock as any, "_streamChatStreaming")
.mockImplementation(async function* () {
yield { role: "assistant", content: "Streaming response" };
});
const nonStreamingMethodSpy = jest
.spyOn(bedrock as any, "_streamChatNonStreaming")
.mockImplementation(async function* () {
yield { role: "assistant", content: "Non-streaming response" };
});
const options: CompletionOptions = {
model: "anthropic.claude-3-sonnet-20240229-v1:0",
stream: true, // Explicitly set streaming
};
const results = [];
for await (const message of bedrock["_streamChat"](
messages,
mockAbortSignal,
options,
)) {
results.push(message);
}
expect(streamingMethodSpy).toHaveBeenCalled();
expect(nonStreamingMethodSpy).not.toHaveBeenCalled();
expect(results).toEqual([
{ role: "assistant", content: "Streaming response" },
]);
streamingMethodSpy.mockRestore();
nonStreamingMethodSpy.mockRestore();
});
it("should route to non-streaming path when stream is false", async () => {
const messages: ChatMessage[] = [
{ role: "user", content: "Hello" } as UserChatMessage,
];
// Mock the private methods by spying on them
const streamingMethodSpy = jest
.spyOn(bedrock as any, "_streamChatStreaming")
.mockImplementation(async function* () {
yield { role: "assistant", content: "Streaming response" };
});
const nonStreamingMethodSpy = jest
.spyOn(bedrock as any, "_streamChatNonStreaming")
.mockImplementation(async function* () {
yield { role: "assistant", content: "Non-streaming response" };
});
const options: CompletionOptions = {
model: "anthropic.claude-3-sonnet-20240229-v1:0",
stream: false, // Explicitly set non-streaming
};
const results = [];
for await (const message of bedrock["_streamChat"](
messages,
mockAbortSignal,
options,
)) {
results.push(message);
}
expect(streamingMethodSpy).not.toHaveBeenCalled();
expect(nonStreamingMethodSpy).toHaveBeenCalled();
expect(results).toEqual([
{ role: "assistant", content: "Non-streaming response" },
]);
streamingMethodSpy.mockRestore();
nonStreamingMethodSpy.mockRestore();
});
it("should default to streaming path when stream option is undefined", async () => {
const messages: ChatMessage[] = [
{ role: "user", content: "Hello" } as UserChatMessage,
];
// Mock the private methods by spying on them
const streamingMethodSpy = jest
.spyOn(bedrock as any, "_streamChatStreaming")
.mockImplementation(async function* () {
yield { role: "assistant", content: "Streaming response" };
});
const nonStreamingMethodSpy = jest
.spyOn(bedrock as any, "_streamChatNonStreaming")
.mockImplementation(async function* () {
yield { role: "assistant", content: "Non-streaming response" };
});
const options: CompletionOptions = {
model: "anthropic.claude-3-sonnet-20240229-v1:0",
// stream option is undefined, should default to streaming
};
const results = [];
for await (const message of bedrock["_streamChat"](
messages,
mockAbortSignal,
options,
)) {
results.push(message);
}
expect(streamingMethodSpy).toHaveBeenCalled();
expect(nonStreamingMethodSpy).not.toHaveBeenCalled();
streamingMethodSpy.mockRestore();
nonStreamingMethodSpy.mockRestore();
});
});
describe("message conversion", () => {
let bedrock: TestBedrock;

View file

@ -2,8 +2,8 @@ import {
BedrockRuntimeClient,
ContentBlock,
ConversationRole,
ConverseCommand,
ConverseStreamCommand,
ConverseStreamCommandOutput,
ImageFormat,
InvokeModelCommand,
Message,
@ -91,33 +91,47 @@ class Bedrock extends BaseLLM {
signal: AbortSignal,
options: CompletionOptions,
): AsyncGenerator<ChatMessage> {
if (options.stream !== false) {
yield* this._streamChatStreaming(messages, signal, options);
} else {
yield* this._streamChatNonStreaming(messages, signal, options);
}
}
const credentials = await this._getCredentials();
const client = new BedrockRuntimeClient({
region: this.region,
endpoint: this.apiBase,
credentials: {
accessKeyId: credentials.accessKeyId,
secretAccessKey: credentials.secretAccessKey,
sessionToken: credentials.sessionToken || "",
},
});
/**
* Handles streaming chat using ConverseStreamCommand
*/
private async *_streamChatStreaming(
messages: ChatMessage[],
signal: AbortSignal,
options: CompletionOptions,
): AsyncGenerator<ChatMessage> {
const client = await this._createBedrockClient();
this._addClientMiddleware(client);
let config_headers =
this.requestOptions && this.requestOptions.headers
? this.requestOptions.headers
: {};
// AWS SigV4 requires strict canonicalization of headers.
// DO NOT USE "_" in your header name. It will return an error like below.
// "The request signature we calculated does not match the signature you provided."
client.middlewareStack.add(
(next) => async (args: any) => {
args.request.headers = {
...args.request.headers,
...config_headers,
};
return next(args);
},
{
step: "build",
},
);
const input = this._generateConverseInput(messages, {
...options,
stream: true,
});
const command = new ConverseStreamCommand(input);
const response = await client.send(command, {
const response = (await client.send(command, {
abortSignal: signal,
});
})) as ConverseStreamCommandOutput;
if (!response?.stream) {
throw new Error("No stream received from Bedrock API");
@ -144,17 +158,17 @@ class Bedrock extends BaseLLM {
role: "assistant",
content: chunk.contentBlockDelta.delta.text,
};
continue; // Continue parsing the stream
continue;
}
// Handle reasoning text content
// Handle text content
if ((chunk.contentBlockDelta.delta as any).reasoningContent?.text) {
yield {
role: "thinking",
content: (chunk.contentBlockDelta.delta as any).reasoningContent
.text,
};
continue; // Continue parsing the stream
continue;
}
// Handle signature for thinking
@ -164,7 +178,7 @@ class Bedrock extends BaseLLM {
content: "",
signature: delta.reasoningContent.signature,
};
continue; // Continue parsing the stream
continue;
}
// Handle redacted thinking
@ -174,7 +188,7 @@ class Bedrock extends BaseLLM {
content: "",
redactedThinking: delta.redactedReasoning.data,
};
continue; // Continue parsing the stream
continue;
}
if (
@ -187,7 +201,7 @@ class Bedrock extends BaseLLM {
}
this._currentToolResponse.input +=
chunk.contentBlockDelta.delta.toolUse.input;
continue; // Continue parsing the stream
continue;
}
}
@ -199,7 +213,7 @@ class Bedrock extends BaseLLM {
content: "",
redactedThinking: start.redactedReasoning.data,
};
continue; // Continue parsing the stream
continue;
}
const toolUse = chunk.contentBlockStart.start.toolUse;
@ -210,7 +224,7 @@ class Bedrock extends BaseLLM {
input: "",
};
}
continue; // Continue parsing the stream
continue;
}
if (chunk.contentBlockStop) {
@ -231,7 +245,7 @@ class Bedrock extends BaseLLM {
};
this._currentToolResponse = null;
}
continue; // Continue parsing the stream
continue;
}
}
} catch (error: unknown) {
@ -241,133 +255,6 @@ class Bedrock extends BaseLLM {
}
}
/**
* Handles non-streaming chat using ConverseCommand
*/
private async *_streamChatNonStreaming(
messages: ChatMessage[],
signal: AbortSignal,
options: CompletionOptions,
): AsyncGenerator<ChatMessage> {
const client = await this._createBedrockClient();
this._addClientMiddleware(client);
const input = this._generateConverseInput(messages, {
...options,
stream: false,
});
const command = new ConverseCommand(input);
const response = await client.send(command, {
abortSignal: signal,
});
// Reset cache metrics for new request
this._promptCachingMetrics = {
cacheReadInputTokens: 0,
cacheWriteInputTokens: 0,
};
try {
if (response.output?.message?.content) {
for (const contentBlock of response.output.message.content) {
if (contentBlock.text) {
yield {
role: "assistant",
content: contentBlock.text,
};
}
if ((contentBlock as any).reasoningContent) {
const reasoningContent = (contentBlock as any).reasoningContent;
if (reasoningContent.reasoningText) {
yield {
role: "thinking",
content: reasoningContent.reasoningText.text || "",
signature: reasoningContent.reasoningText.signature,
};
}
if (reasoningContent.redactedContent) {
yield {
role: "thinking",
content: "",
redactedThinking: reasoningContent.redactedContent,
};
}
}
if (contentBlock.toolUse) {
yield {
role: "assistant",
content: "",
toolCalls: [
{
id: contentBlock.toolUse.toolUseId,
type: "function",
function: {
name: contentBlock.toolUse.name,
arguments: JSON.stringify(contentBlock.toolUse.input || {}),
},
},
],
};
}
}
}
// Handle usage metadata if available
if (response.usage) {
console.log(`${JSON.stringify(response.usage)}`);
}
} catch (error: unknown) {
// Clean up state and let the original error bubble up to the retry decorator
this._currentToolResponse = null;
throw error;
}
}
/**
* Creates and configures a Bedrock Runtime Client
*/
private async _createBedrockClient(): Promise<BedrockRuntimeClient> {
const credentials = await this._getCredentials();
return new BedrockRuntimeClient({
region: this.region,
endpoint: this.apiBase,
credentials: {
accessKeyId: credentials.accessKeyId,
secretAccessKey: credentials.secretAccessKey,
sessionToken: credentials.sessionToken || "",
},
});
}
/**
* Adds middleware to the Bedrock client for custom headers
*/
private _addClientMiddleware(client: BedrockRuntimeClient): void {
const config_headers =
this.requestOptions && this.requestOptions.headers
? this.requestOptions.headers
: {};
// AWS SigV4 requires strict canonicalization of headers.
// DO NOT USE "_" in your header name. It will return an error like below.
// "The request signature we calculated does not match the signature you provided."
client.middlewareStack.add(
(next) => async (args: any) => {
args.request.headers = {
...args.request.headers,
...config_headers,
};
return next(args);
},
{
step: "build",
},
);
}
/**
* Generates the input payload for the Bedrock Converse API
* @param messages - Array of chat messages