Skip to content

Commit

Permalink
feat: introduce replace placeholders for prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
dzehnder committed Jan 21, 2025
1 parent 0117dd1 commit b6df1a1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
18 changes: 18 additions & 0 deletions packages/spacecat-shared-utils/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
}
9 changes: 9 additions & 0 deletions packages/spacecat-shared-utils/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ declare function getRUMDomainKey(baseURL: string, ctx: object): Promise<string>;
*/
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
Expand Down
41 changes: 40 additions & 1 deletion packages/spacecat-shared-utils/test/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import nock from 'nock';
import {
generateCSVFile,
resolveSecretsName,
resolveCustomerSecretsName, getRUMDomainKey,
resolveCustomerSecretsName,
getRUMDomainKey,
replacePlaceholders,
} from '../src/helpers.js';

describe('resolveSecretsName', () => {
Expand Down Expand Up @@ -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.');
});
});

0 comments on commit b6df1a1

Please sign in to comment.