Skip to content

Commit

Permalink
feat: introduce get prompt from static files
Browse files Browse the repository at this point in the history
  • Loading branch information
dzehnder committed Jan 21, 2025
1 parent bc12266 commit 2e0fbfe
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/spacecat-shared-utils/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import { Parser } from '@json2csv/plainjs';
import { GetSecretValueCommand, SecretsManagerClient } from '@aws-sdk/client-secrets-manager';
import { promises as fs } from 'fs';
import { isString } from './functions.js';

/**
Expand Down Expand Up @@ -110,3 +111,23 @@ export function replacePlaceholders(content, placeholders) {
}
});
}

/**
* Reads the content of a prompt file asynchronously and replaces any placeholders
* with the corresponding values. Logs the error and returns null in case of an error.
*
* @param {Object} placeholders - A JSON object containing values to replace in the prompt content.
* @param {String} filename - The filename of the prompt file.
* @param {Object} log - The logger
* @returns {Promise<string|null>} - A promise that resolves to a string with the prompt content,
* or null if an error occurs.
*/
export async function getPrompt(placeholders, filename, log = console) {
try {
const promptContent = await fs.readFile(`./static/prompts/${filename}.prompt`, { encoding: 'utf8' });
return replacePlaceholders(promptContent, placeholders);
} catch (error) {
log.error('Error reading prompt file:', error.message);
return null;
}
}
13 changes: 13 additions & 0 deletions packages/spacecat-shared-utils/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,19 @@ declare function generateCSVFile(data: object[]): Buffer;
*/
declare function replacePlaceholders(content: string, placeholders: object): string;

/**
* Reads the content of a prompt file asynchronously and replaces any placeholders
* with the corresponding values. Logs the error and returns null in case of an error.
*
* @param {Object} placeholders - A JSON object containing values to replace in the prompt content.
* @param {String} filename - The filename of the prompt file.
* @param {Object} log - The logger
* @returns {Promise<string|null>} - A promise that resolves to a string with the prompt content,
* or null if an error occurs.
*/
declare function getPrompt(placeholders: object, filename: string, log: object):
Promise<string|null>;

/**
* Retrieves stored metrics from S3.
* @param config - Configuration object
Expand Down
53 changes: 52 additions & 1 deletion packages/spacecat-shared-utils/test/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
import { expect } from 'chai';

import nock from 'nock';
import { promises as fs } from 'fs';
import sinon from 'sinon';
import {
generateCSVFile,
resolveSecretsName,
resolveCustomerSecretsName,
getRUMDomainKey,
replacePlaceholders,
replacePlaceholders, getPrompt,
} from '../src/helpers.js';

describe('resolveSecretsName', () => {
Expand Down Expand Up @@ -227,3 +229,52 @@ describe('replacePlaceholders', () => {
expect(result).to.equal('Hello, John! You are {{age}} years old.');
});
});

describe('getPrompt', () => {
let readFileStub;
let logStub;

beforeEach(() => {
readFileStub = sinon.stub(fs, 'readFile');
logStub = { error: sinon.stub() };
});

afterEach(() => {
sinon.restore();
});

it('reads the prompt file and replace placeholders', async () => {
const placeholders = { name: 'John' };
const filename = 'test';
const fileContent = 'Hello, {{name}}!';
readFileStub.resolves(fileContent);

const result = await getPrompt(placeholders, filename, logStub);

expect(result).to.equal('Hello, John!');
expect(readFileStub.calledOnceWith(`./static/prompts/${filename}.prompt`, { encoding: 'utf8' })).to.be.true;
});

it('returns null and log an error if reading the file fails', async () => {
const placeholders = { name: 'John' };
const filename = 'test';
const errorMessage = 'File not found';
readFileStub.rejects(new Error(errorMessage));

const result = await getPrompt(placeholders, filename, logStub);

expect(result).to.be.null;
expect(logStub.error.calledOnceWith('Error reading prompt file:', errorMessage)).to.be.true;
});

it('handles empty placeholder object and return content as is', async () => {
const placeholders = {};
const filename = 'test';
const fileContent = 'Hello, {{name}}!';
readFileStub.resolves(fileContent);

const result = await getPrompt(placeholders, filename, logStub);

expect(result).to.equal('Hello, {{name}}!');
});
});

0 comments on commit 2e0fbfe

Please sign in to comment.