diff --git a/core/llm/llms/Bedrock.ts b/core/llm/llms/Bedrock.ts index 7fefbd329..b9517bb59 100644 --- a/core/llm/llms/Bedrock.ts +++ b/core/llm/llms/Bedrock.ts @@ -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 { diff --git a/core/llm/utils/__tests__/getSecureID.test.ts b/core/llm/utils/__tests__/getSecureID.test.ts new file mode 100644 index 000000000..9c4ffd6e7 --- /dev/null +++ b/core/llm/utils/__tests__/getSecureID.test.ts @@ -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(''); + }); + + 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(''); + }); + + 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(''); + }); +}); \ No newline at end of file diff --git a/core/llm/utils/getSecureID.ts b/core/llm/utils/getSecureID.ts new file mode 100644 index 000000000..b05fefab6 --- /dev/null +++ b/core/llm/utils/getSecureID.ts @@ -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 ``; +}