Merge pull request #5035 from chezsmithy/feat/bedrock-secure-prompt-caching

feat/bedrock-secure-prompt-caching
This commit is contained in:
Nate Sesti 2025-04-09 10:02:46 -07:00 • committed by GitHub
commit 30b5f32c9e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 64 additions and 0 deletions

View file

@ -14,6 +14,7 @@ import {
CompletionOptions,
LLMOptions,
} from "../../index.js";
import { getSecureID } from "../utils/getSecureID.js";
import { renderChatMessage } from "../../util/messageContent.js";
import { BaseLLM } from "../index.js";
import { PROVIDER_TOOL_SUPPORT } from "../toolSupport.js";
@ -427,6 +428,9 @@ class Bedrock extends BaseLLM {
// Standard text message
if (typeof message.content === "string") {
if (addCaching) {
message.content += getSecureID();
}
const content: any[] = [{ text: message.content }];
if (addCaching) {
content.push({ cachePoint: { type: "default" } });
@ -444,6 +448,9 @@ class Bedrock extends BaseLLM {
// Process all parts first
message.content.forEach((part) => {
if (part.type === "text") {
if (addCaching) {
part.text += getSecureID();
}
content.push({ text: part.text });
} else if (part.type === "imageUrl" && part.imageUrl) {
try {

View file

@ -0,0 +1,49 @@
import { getSecureID } from '../getSecureID';
// Mock the crypto.randomUUID function
const mockRandomUUID = jest.fn();
global.crypto = {
...global.crypto,
randomUUID: mockRandomUUID
};
describe('getSecureID', () => {
beforeEach(() => {
// Reset the static property before each test
(getSecureID as any).uuid = undefined;
// Reset the mock implementation
mockRandomUUID.mockReset();
// Setup a mock implementation that returns a fixed value for testing
mockRandomUUID.mockReturnValue('test-uuid-1234');
});
test('should generate a UUID on first call', () => {
const result = getSecureID();
expect(mockRandomUUID).toHaveBeenCalledTimes(1);
expect(result).toBe('<!-- SID: test-uuid-1234 -->');
});
test('should reuse the same UUID on subsequent calls', () => {
const firstResult = getSecureID();
const secondResult = getSecureID();
// The UUID generation should only happen once
expect(mockRandomUUID).toHaveBeenCalledTimes(1);
// Both results should be identical
expect(firstResult).toBe(secondResult);
expect(firstResult).toBe('<!-- SID: test-uuid-1234 -->');
});
test('should maintain the same UUID across different test cases if not reset', () => {
// Don't reset the UUID here to test persistence
(getSecureID as any).uuid = 'persistent-uuid';
const result = getSecureID();
// No new UUID should be generated
expect(mockRandomUUID).not.toHaveBeenCalled();
expect(result).toBe('<!-- SID: persistent-uuid -->');
});
});

View file

@ -0,0 +1,8 @@
// Utility function to get or generate UUID for LLM prompts
export function getSecureID(): string {
// Adding a type declaration for the static property
if (!(getSecureID as any).uuid) {
(getSecureID as any).uuid = crypto.randomUUID();
}
return `<!-- SID: ${(getSecureID as any).uuid} -->`;
}