Skip to content

Commit

Permalink
Remove deprecated function
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Helming <[email protected]>
  • Loading branch information
JonasHelming committed Nov 22, 2024
1 parent 31db839 commit de67625
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 47 deletions.
74 changes: 36 additions & 38 deletions packages/ai-core/src/common/prompt-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ describe('PromptService', () => {
container.bind<AIVariableService>(AIVariableService).toConstantValue(variableService);

promptService = container.get<PromptService>(PromptService);
promptService.storePrompt('1', 'Hello, {{name}}!');
promptService.storePrompt('2', 'Goodbye, {{name}}!');
promptService.storePrompt('3', 'Ciao, {{invalid}}!');
promptService.storePrompt('8', 'Hello, {{{name}}}');
promptService.storePromptTemplate({ id: '1', template: 'Hello, {{name}}!' });
promptService.storePromptTemplate({ id: '2', template: 'Goodbye, {{name}}!' });
promptService.storePromptTemplate({ id: '3', template: 'Ciao, {{invalid}}!' });
promptService.storePromptTemplate({ id: '8', template: 'Hello, {{{name}}}' });
});

it('should initialize prompts from PromptCollectionService', () => {
Expand All @@ -62,7 +62,7 @@ describe('PromptService', () => {
});

it('should store a new prompt', () => {
promptService.storePrompt('3', 'Welcome, {{name}}!');
promptService.storePromptTemplate({ id: '3', template: 'Welcome, {{name}}!' });
const newPrompt = promptService.getRawPrompt('3');
expect(newPrompt?.template).to.equal('Welcome, {{name}}!');
});
Expand All @@ -88,10 +88,10 @@ describe('PromptService', () => {
});

it('should ignore whitespace in variables', async () => {
promptService.storePrompt('4', 'Hello, {{name }}!');
promptService.storePrompt('5', 'Hello, {{ name}}!');
promptService.storePrompt('6', 'Hello, {{ name }}!');
promptService.storePrompt('7', 'Hello, {{ name }}!');
promptService.storePromptTemplate({ id: '4', template: 'Hello, {{name }}!' });
promptService.storePromptTemplate({ id: '5', template: 'Hello, {{ name}}!' });
promptService.storePromptTemplate({ id: '6', template: 'Hello, {{ name }}!' });
promptService.storePromptTemplate({ id: '7', template: 'Hello, {{ name }}!' });
for (let i = 4; i <= 7; i++) {
const prompt = await promptService.getPrompt(`${i}`, { name: 'John' });
expect(prompt?.text).to.equal('Hello, John!');
Expand All @@ -109,37 +109,35 @@ describe('PromptService', () => {
});

it('should ignore whitespace in variables (three bracket)', async () => {
promptService.storePrompt('9', 'Hello, {{{name }}}');
promptService.storePrompt('10', 'Hello, {{{ name}}}');
promptService.storePrompt('11', 'Hello, {{{ name }}}');
promptService.storePrompt('12', 'Hello, {{{ name }}}');
promptService.storePromptTemplate({ id: '9', template: 'Hello, {{{name }}}' });
promptService.storePromptTemplate({ id: '10', template: 'Hello, {{{ name}}}' });
promptService.storePromptTemplate({ id: '11', template: 'Hello, {{{ name }}}' });
promptService.storePromptTemplate({ id: '12', template: 'Hello, {{{ name }}}' });
for (let i = 9; i <= 12; i++) {
const prompt = await promptService.getPrompt(`${i}`, { name: 'John' });
expect(prompt?.text).to.equal('Hello, John');
}
});

it('should ignore invalid prompts with unmatched brackets', async () => {
promptService.storePrompt('9', 'Hello, {{name');
promptService.storePrompt('10', 'Hello, {{{name');
promptService.storePrompt('11', 'Hello, name}}}}');
promptService.storePromptTemplate({ id: '9', template: 'Hello, {{name' });
promptService.storePromptTemplate({ id: '10', template: 'Hello, {{{name' });
promptService.storePromptTemplate({ id: '11', template: 'Hello, name}}}}' });
const prompt1 = await promptService.getPrompt('9', { name: 'John' });
expect(prompt1?.text).to.equal('Hello, {{name'); // Not matching due to missing closing brackets

const prompt2 = await promptService.getPrompt('10', { name: 'John' });
expect(prompt2?.text).to.equal('Hello, {{{name'); // Matches pattern due to valid three-start-two-end brackets

const prompt3 = await promptService.getPrompt('11', { name: 'John' });
expect(prompt3?.text).to.equal('Hello, name}}}}'); // Extra closing bracket, does not match cleanly
});

it('should handle a mixture of two and three brackets correctly', async () => {
promptService.storePrompt('12', 'Hi, {{name}}}'); // (invalid)
promptService.storePrompt('13', 'Hello, {{{name}}'); // (invalid)
promptService.storePrompt('14', 'Greetings, {{{name}}}}'); // (invalid)
promptService.storePrompt('15', 'Bye, {{{{name}}}'); // (invalid)
promptService.storePrompt('16', 'Ciao, {{{{name}}}}'); // (invalid)
promptService.storePrompt('17', 'Hi, {{name}}! {{{name}}}'); // Mixed valid patterns
promptService.storePromptTemplate({ id: '12', template: 'Hi, {{name}}}' }); // (invalid)
promptService.storePromptTemplate({ id: '13', template: 'Hello, {{{name}}' }); // (invalid)
promptService.storePromptTemplate({ id: '14', template: 'Greetings, {{{name}}}}' }); // (invalid)
promptService.storePromptTemplate({ id: '15', template: 'Bye, {{{{name}}}' }); // (invalid)
promptService.storePromptTemplate({ id: '16', template: 'Ciao, {{{{name}}}}' }); // (invalid)
promptService.storePromptTemplate({ id: '17', template: 'Hi, {{name}}! {{{name}}}' }); // Mixed valid patterns

const prompt12 = await promptService.getPrompt('12', { name: 'John' });
expect(prompt12?.text).to.equal('Hi, {{name}}}');
Expand All @@ -161,85 +159,85 @@ describe('PromptService', () => {
});

it('should strip single-line comments at the start of the template', () => {
promptService.storePrompt('comment-basic', '{{!-- Comment --}}Hello, {{name}}!');
promptService.storePromptTemplate({ id: 'comment-basic', template: '{{!-- Comment --}}Hello, {{name}}!' });
const prompt = promptService.getUnresolvedPrompt('comment-basic');
expect(prompt?.template).to.equal('Hello, {{name}}!');
});

it('should remove line break after first-line comment', () => {
promptService.storePrompt('comment-line-break', '{{!-- Comment --}}\nHello, {{name}}!');
promptService.storePromptTemplate({ id: 'comment-line-break', template: '{{!-- Comment --}}\nHello, {{name}}!' });
const prompt = promptService.getUnresolvedPrompt('comment-line-break');
expect(prompt?.template).to.equal('Hello, {{name}}!');
});

it('should strip multiline comments at the start of the template', () => {
promptService.storePrompt('comment-multiline', '{{!--\nMultiline comment\n--}}\nGoodbye, {{name}}!');
promptService.storePromptTemplate({ id: 'comment-multiline', template: '{{!--\nMultiline comment\n--}}\nGoodbye, {{name}}!' });
const prompt = promptService.getUnresolvedPrompt('comment-multiline');
expect(prompt?.template).to.equal('Goodbye, {{name}}!');
});

it('should not strip comments not in the first line', () => {
promptService.storePrompt('comment-second-line', 'Hello, {{name}}!\n{{!-- Comment --}}');
promptService.storePromptTemplate({ id: 'comment-second-line', template: 'Hello, {{name}}!\n{{!-- Comment --}}' });
const prompt = promptService.getUnresolvedPrompt('comment-second-line');
expect(prompt?.template).to.equal('Hello, {{name}}!\n{{!-- Comment --}}');
});

it('should treat unclosed comments as regular text', () => {
promptService.storePrompt('comment-unclosed', '{{!-- Unclosed comment');
promptService.storePromptTemplate({ id: 'comment-unclosed', template: '{{!-- Unclosed comment' });
const prompt = promptService.getUnresolvedPrompt('comment-unclosed');
expect(prompt?.template).to.equal('{{!-- Unclosed comment');
});

it('should treat standalone closing delimiters as regular text', () => {
promptService.storePrompt('comment-standalone', '--}} Hello, {{name}}!');
promptService.storePromptTemplate({ id: 'comment-standalone', template: '--}} Hello, {{name}}!' });
const prompt = promptService.getUnresolvedPrompt('comment-standalone');
expect(prompt?.template).to.equal('--}} Hello, {{name}}!');
});

it('should handle nested comments and stop at the first closing tag', () => {
promptService.storePrompt('nested-comment', '{{!-- {{!-- Nested comment --}} --}}text');
promptService.storePromptTemplate({ id: 'nested-comment', template: '{{!-- {{!-- Nested comment --}} --}}text' });
const prompt = promptService.getUnresolvedPrompt('nested-comment');
expect(prompt?.template).to.equal('--}}text');
});

it('should handle templates with only comments', () => {
promptService.storePrompt('comment-only', '{{!-- Only comments --}}');
promptService.storePromptTemplate({ id: 'comment-only', template: '{{!-- Only comments --}}' });
const prompt = promptService.getUnresolvedPrompt('comment-only');
expect(prompt?.template).to.equal('');
});

it('should handle mixed delimiters on the same line', () => {
promptService.storePrompt('comment-mixed', '{{!-- Unclosed comment --}}');
promptService.storePromptTemplate({ id: 'comment-mixed', template: '{{!-- Unclosed comment --}}' });
const prompt = promptService.getUnresolvedPrompt('comment-mixed');
expect(prompt?.template).to.equal('');
});

it('should resolve variables after stripping single-line comments', async () => {
promptService.storePrompt('comment-resolve', '{{!-- Comment --}}Hello, {{name}}!');
promptService.storePromptTemplate({ id: 'comment-resolve', template: '{{!-- Comment --}}Hello, {{name}}!' });
const prompt = await promptService.getPrompt('comment-resolve', { name: 'John' });
expect(prompt?.text).to.equal('Hello, John!');
});

it('should resolve variables in multiline templates with comments', async () => {
promptService.storePrompt('comment-multiline-vars', '{{!--\nMultiline comment\n--}}\nHello, {{name}}!');
promptService.storePromptTemplate({ id: 'comment-multiline-vars', template: '{{!--\nMultiline comment\n--}}\nHello, {{name}}!' });
const prompt = await promptService.getPrompt('comment-multiline-vars', { name: 'John' });
expect(prompt?.text).to.equal('Hello, John!');
});

it('should resolve variables with standalone closing delimiters', async () => {
promptService.storePrompt('comment-standalone-vars', '--}} Hello, {{name}}!');
promptService.storePromptTemplate({ id: 'comment-standalone-vars', template: '--}} Hello, {{name}}!' });
const prompt = await promptService.getPrompt('comment-standalone-vars', { name: 'John' });
expect(prompt?.text).to.equal('--}} Hello, John!');
});

it('should treat unclosed comments as text and resolve variables', async () => {
promptService.storePrompt('comment-unclosed-vars', '{{!-- Unclosed comment\nHello, {{name}}!');
promptService.storePromptTemplate({ id: 'comment-unclosed-vars', template: '{{!-- Unclosed comment\nHello, {{name}}!' });
const prompt = await promptService.getPrompt('comment-unclosed-vars', { name: 'John' });
expect(prompt?.text).to.equal('{{!-- Unclosed comment\nHello, John!');
});

it('should handle templates with mixed comments and variables', async () => {
promptService.storePrompt('comment-mixed-vars', '{{!-- Comment --}}Hi, {{name}}! {{!-- Another comment --}}');
promptService.storePromptTemplate({ id: 'comment-mixed-vars', template: '{{!-- Comment --}}Hi, {{name}}! {{!-- Another comment --}}' });
const prompt = await promptService.getPrompt('comment-mixed-vars', { name: 'John' });
expect(prompt?.text).to.equal('Hi, John! {{!-- Another comment --}}');
});
Expand Down
9 changes: 0 additions & 9 deletions packages/ai-core/src/common/prompt-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ export interface PromptService {
* @param args the object with placeholders, mapping the placeholder key to the value
*/
getPrompt(id: string, args?: { [key: string]: unknown }): Promise<ResolvedPromptTemplate | undefined>;
/**
* Adds a prompt to the list of prompts.
* @param id the id of the prompt
* @param prompt the prompt template to store
*/
storePrompt(id: string, prompt: string): void;
/**
* Adds a {@link PromptTemplate} to the list of prompts.
* @param promptTemplate the prompt template to store
Expand Down Expand Up @@ -316,9 +310,6 @@ export class PromptServiceImpl implements PromptService {
return { ...this._prompts };
}
}
storePrompt(id: string, prompt: string): void {
this._prompts[id] = { id, template: prompt };
}
removePrompt(id: string): void {
delete this._prompts[id];
}
Expand Down

0 comments on commit de67625

Please sign in to comment.