@memory-bank/mcp
Version:
Memory-enabled Co-Pilot (MCP) server for managing project documentation and context
155 lines • 5.47 kB
JavaScript
import { Section } from '../../domain/templates/Section.js';
import { Template } from '../../domain/templates/Template.js';
/**
* Service for managing templates
*/
export class TemplateService {
repository;
/**
* Constructor
*
* @param repository Repository for accessing templates
*/
constructor(repository) {
this.repository = repository;
}
/**
* Gets a template by ID
*
* @param id Template ID
* @returns Promise resolving to Template or null if not found
*/
async getTemplate(id) {
return this.repository.getTemplate(id);
}
/**
* Gets a template as a JSON object for a specific language
*
* @param id Template ID
* @param language Language to get template for
* @param variables Optional variables for template substitution
* @returns Promise resolving to the template content as a JSON object
* @throws Error if template not found
*/
async getTemplateAsJsonObject(id, language, variables) {
// Ensure Language is imported if not already
// import { Language } from '../../domain/i18n/Language.js';
return this.repository.getTemplateAsJsonObject(id, language, variables);
}
/**
* Creates a new template
*
* @param id Template ID
* @param type Template type
* @param namesMap Map of language codes to template names
* @returns Promise resolving to the created template
* @throws Error if template ID already exists
*/
async createTemplate(id, type, namesMap) {
// Check if template already exists
const exists = await this.repository.templateExists(id);
if (exists) {
throw new Error(`Template with ID ${id} already exists`);
}
// Create and save the template
const template = Template.create(id, type, namesMap);
await this.repository.saveTemplate(template);
return template;
}
/**
* Updates a template's basic properties
*
* @param id Template ID
* @param type New template type
* @param namesMap New map of language codes to template names
* @returns Promise resolving to the updated template
* @throws Error if template not found
*/
async updateTemplate(id, type, namesMap) {
// Get existing template
const template = await this.repository.getTemplate(id);
if (!template) {
throw new Error(`Template with ID ${id} not found`);
}
// Create updated template
const updatedTemplate = Template.create(id, type, namesMap, template.sections);
// Save and return
await this.repository.saveTemplate(updatedTemplate);
return updatedTemplate;
}
/**
* Adds or updates a section in a template
*
* @param templateId Template ID
* @param sectionId Section ID
* @param titlesMap Map of language codes to section titles
* @param contentsMap Map of language codes to section contents
* @param isOptional Whether the section is optional
* @returns Promise resolving to the updated template
* @throws Error if template not found
*/
async addOrUpdateSection(templateId, sectionId, titlesMap, contentsMap = {}, isOptional = false) {
// Get existing template
const template = await this.repository.getTemplate(templateId);
if (!template) {
throw new Error(`Template with ID ${templateId} not found`);
}
// Create section
const section = Section.create(sectionId, titlesMap, contentsMap, isOptional);
// Add section to template
const updatedTemplate = template.withSection(section);
// Save and return
await this.repository.saveTemplate(updatedTemplate);
return updatedTemplate;
}
/**
* Removes a section from a template
*
* @param templateId Template ID
* @param sectionId Section ID
* @returns Promise resolving to the updated template
* @throws Error if template not found
*/
async removeSection(templateId, sectionId) {
// Get existing template
const template = await this.repository.getTemplate(templateId);
if (!template) {
throw new Error(`Template with ID ${templateId} not found`);
}
// Remove section
const updatedTemplate = template.withoutSection(sectionId);
// If no changes (section didn't exist), return existing template
if (updatedTemplate === template) {
return template;
}
// Save and return
await this.repository.saveTemplate(updatedTemplate);
return updatedTemplate;
}
/**
* Gets all templates of a specific type
*
* @param type Template type
* @returns Promise resolving to array of Templates
*/
async getTemplatesByType(type) {
return this.repository.getTemplatesByType(type);
}
/**
* Gets IDs of all available templates
*
* @returns Promise resolving to array of template IDs
*/
async getAllTemplateIds() {
return this.repository.getAllTemplateIds();
}
/**
* Gets all available template types
*
* @returns Promise resolving to array of template types
*/
async getAllTemplateTypes() {
return this.repository.getAllTemplateTypes();
}
}
//# sourceMappingURL=TemplateService.js.map