From b6df1a184b842288da4cfdf8c7960f6d2806c8d0 Mon Sep 17 00:00:00 2001 From: Damian Zehnder Date: Tue, 21 Jan 2025 08:39:25 +0100 Subject: [PATCH] feat: introduce replace placeholders for prompts --- packages/spacecat-shared-utils/src/helpers.js | 18 ++++++++ packages/spacecat-shared-utils/src/index.d.ts | 9 ++++ .../test/helpers.test.js | 41 ++++++++++++++++++- 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/packages/spacecat-shared-utils/src/helpers.js b/packages/spacecat-shared-utils/src/helpers.js index 3909fa7f..f895cbc8 100644 --- a/packages/spacecat-shared-utils/src/helpers.js +++ b/packages/spacecat-shared-utils/src/helpers.js @@ -92,3 +92,21 @@ export function generateCSVFile(data) { const json2csvParser = new Parser(); return Buffer.from(json2csvParser.parse(data), 'utf-8'); } + +/** + * Replaces placeholders in the prompt content with their corresponding values. + * + * @param {string} content - The prompt content with placeholders. + * @param {Object} placeholders - The placeholders and their values. + * @returns {string} - The content with placeholders replaced. + */ +export function replacePlaceholders(content, placeholders) { + return content.replace(/{{(.*?)}}/g, (match, key) => { + if (key in placeholders) { + const value = placeholders[key]; + return typeof value === 'object' && value !== null ? JSON.stringify(value) : value; + } else { + return match; + } + }); +} diff --git a/packages/spacecat-shared-utils/src/index.d.ts b/packages/spacecat-shared-utils/src/index.d.ts index be7368b1..04c172c1 100644 --- a/packages/spacecat-shared-utils/src/index.d.ts +++ b/packages/spacecat-shared-utils/src/index.d.ts @@ -143,6 +143,15 @@ declare function getRUMDomainKey(baseURL: string, ctx: object): Promise; */ declare function generateCSVFile(data: object[]): Buffer; +/** + * Replaces placeholders in the prompt content with their corresponding values. + * + * @param {string} content - The prompt content with placeholders. + * @param {Object} placeholders - The placeholders and their values. + * @returns {string} - The content with placeholders replaced. + */ +declare function replacePlaceholders(content: string, placeholders: object): string; + /** * Retrieves stored metrics from S3. * @param config - Configuration object diff --git a/packages/spacecat-shared-utils/test/helpers.test.js b/packages/spacecat-shared-utils/test/helpers.test.js index ee631cba..383f1b4b 100644 --- a/packages/spacecat-shared-utils/test/helpers.test.js +++ b/packages/spacecat-shared-utils/test/helpers.test.js @@ -18,7 +18,9 @@ import nock from 'nock'; import { generateCSVFile, resolveSecretsName, - resolveCustomerSecretsName, getRUMDomainKey, + resolveCustomerSecretsName, + getRUMDomainKey, + replacePlaceholders, } from '../src/helpers.js'; describe('resolveSecretsName', () => { @@ -188,3 +190,40 @@ describe('generateCSVFile', () => { expect(csvString).to.equal(expectedCsv); }); }); + +describe('replacePlaceholders', () => { + it('replaces placeholders with corresponding values', () => { + const content = 'Hello, {{name}}!'; + const placeholders = { name: 'John' }; + const result = replacePlaceholders(content, placeholders); + expect(result).to.equal('Hello, John!'); + }); + + it('does not replace placeholders if key is not found in placeholders object', () => { + const content = 'Hello, {{name}}!'; + const placeholders = { age: 30 }; + const result = replacePlaceholders(content, placeholders); + expect(result).to.equal('Hello, {{name}}!'); + }); + + it('replaces multiple placeholders with corresponding values', () => { + const content = 'Hello, {{name}}! You are {{age}} years old.'; + const placeholders = { name: 'John', age: 30 }; + const result = replacePlaceholders(content, placeholders); + expect(result).to.equal('Hello, John! You are 30 years old.'); + }); + + it('replaces placeholders with stringified objects if value is an object', () => { + const content = 'User: {{user}}'; + const placeholders = { user: { name: 'John', age: 30 } }; + const result = replacePlaceholders(content, placeholders); + expect(result).to.equal('User: {"name":"John","age":30}'); + }); + + it('leaves placeholders unchanged if they are not found in placeholders object', () => { + const content = 'Hello, {{name}}! You are {{age}} years old.'; + const placeholders = { name: 'John' }; + const result = replacePlaceholders(content, placeholders); + expect(result).to.equal('Hello, John! You are {{age}} years old.'); + }); +});