UNPKG

@memory-bank/mcp

Version:

Memory-enabled Co-Pilot (MCP) server for managing project documentation and context

86 lines 3.87 kB
import { BranchInfo } from "../../../domain/entities/BranchInfo.js"; import { DocumentPath } from "../../../domain/entities/DocumentPath.js"; import { Tag } from "../../../domain/entities/Tag.js"; import { ApplicationError, ApplicationErrorCodes } from "../../../shared/errors/ApplicationError.js"; import { DomainError, DomainErrorCodes } from "../../../shared/errors/DomainError.js"; /** * Use case for searching JSON documents */ export class SearchJsonDocumentsUseCase { jsonRepository; globalRepository; /** * Constructor * @param jsonRepository JSON document repository * @param globalRepository Global JSON document repository (optional) */ constructor(jsonRepository, globalRepository) { this.jsonRepository = jsonRepository; this.globalRepository = globalRepository; } /** * Execute the use case * @param input Input data * @returns Promise resolving to output data */ async execute(input) { try { if (!input.tags && !input.documentType) { throw new ApplicationError(ApplicationErrorCodes.INVALID_INPUT, 'At least one search criteria (tags or documentType) must be provided'); } const isGlobal = !input.branchName; const repository = isGlobal ? this.globalRepository || this.jsonRepository : this.jsonRepository; const location = isGlobal ? 'global' : input.branchName; const branchInfo = isGlobal ? BranchInfo.create('feature/global') // Changed: 'global' -> 'feature/global' : BranchInfo.create(input.branchName); if (!isGlobal) { // Changed: use dummy path for existence check const dummyPath = DocumentPath.create('index.json'); const branchExists = await this.jsonRepository.exists(branchInfo, dummyPath); if (!branchExists) { throw new DomainError(DomainErrorCodes.BRANCH_NOT_FOUND, `Branch "${input.branchName}" not found`); } } const matchAllTags = input.matchAllTags ?? false; let documents = []; if (input.tags && input.tags.length > 0) { const tags = input.tags.map((tag) => Tag.create(tag)); documents = await repository.findByTags(branchInfo, tags, matchAllTags); } else if (input.documentType) { documents = await repository.findByType(branchInfo, input.documentType); } const documentDTOs = documents.map((doc) => ({ id: doc.id.value, path: doc.path.value, title: doc.title, documentType: doc.documentType, tags: doc.tags.map((tag) => tag.value), content: doc.content, lastModified: doc.lastModified.toISOString(), createdAt: doc.createdAt.toISOString(), version: doc.version, })); return { documents: documentDTOs, searchInfo: { count: documentDTOs.length, searchLocation: location, searchedTags: input.tags, searchedDocumentType: input.documentType, matchedAllTags: input.tags ? matchAllTags : undefined, }, }; } catch (error) { if (error instanceof DomainError || error instanceof ApplicationError) { throw error; } throw new ApplicationError(ApplicationErrorCodes.USE_CASE_EXECUTION_FAILED, `Failed to search JSON documents: ${error.message}`, { originalError: error }); } } } //# sourceMappingURL=SearchJsonDocumentsUseCase.js.map