Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce replace placeholders for prompts #551

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.');
});
});
Loading