perf-lens
Version:
AI-powered frontend performance optimizer
80 lines (79 loc) • 2.09 kB
JavaScript
import { DEFAULT_PROMPTS } from './promptConfig.js';
/**
* Manages system prompts for AI interactions
*/
export class PromptManager {
constructor() {
this.prompts = new Map();
this.initialized = false;
}
/**
* Gets the singleton instance of PromptManager
*/
static getInstance() {
if (!PromptManager.instance) {
PromptManager.instance = new PromptManager();
}
return PromptManager.instance;
}
/**
* Loads prompts from the default configuration
*/
loadPrompts() {
if (this.initialized)
return;
// Load default prompts
Object.entries(DEFAULT_PROMPTS).forEach(([key, content]) => {
this.prompts.set(key, content);
});
this.initialized = true;
}
/**
* Gets a prompt by its key
* @param key - The key of the prompt to retrieve
* @returns The prompt content
* @throws Error if the prompt is not found
*/
getPrompt(key) {
if (!this.initialized) {
this.loadPrompts();
}
const prompt = this.prompts.get(key);
if (!prompt) {
throw new Error(`Prompt not found for key: ${key}`);
}
return prompt;
}
/**
* Updates a prompt's content
* @param key - The key of the prompt to update
* @param content - The new content for the prompt
*/
updatePrompt(key, content) {
if (!this.initialized) {
this.loadPrompts();
}
this.prompts.set(key, content);
}
/**
* Checks if a prompt exists
* @param key - The key to check
* @returns True if the prompt exists
*/
hasPrompt(key) {
if (!this.initialized) {
this.loadPrompts();
}
return this.prompts.has(key);
}
/**
* Gets all available prompt keys
* @returns Array of prompt keys
*/
getPromptKeys() {
if (!this.initialized) {
this.loadPrompts();
}
return Array.from(this.prompts.keys());
}
}