UNPKG

@sofianedjerbi/knowledge-tree-mcp

Version:

MCP server for hierarchical project knowledge management

118 lines 3.72 kB
/** * JSON export utilities for Knowledge Tree MCP * Handles conversion of knowledge entries to JSON format */ /** * Exports knowledge entries to JSON format * @param entries - Array of entries to export * @param includeLinks - Whether to include relationship links * @returns JSON formatted string */ export function exportToJSON(entries, includeLinks) { const exportData = { metadata: { exported_at: new Date().toISOString(), total_entries: entries.length, include_links: includeLinks, version: '1.0.0' }, entries: entries.map(({ path, entry }) => ({ path, priority: entry.priority, problem: entry.problem, solution: entry.solution, code: entry.code, examples: entry.examples, related_to: includeLinks ? entry.related_to : undefined })) }; return JSON.stringify(exportData, null, 2); } /** * Exports entries grouped by category * @param entries - Array of entries to export * @param includeLinks - Whether to include links * @returns JSON string with hierarchical structure */ export function exportToJSONByCategory(entries, includeLinks) { const byCategory = {}; for (const { path, entry } of entries) { const parts = path.split('/'); let current = byCategory; // Build nested structure for (let i = 0; i < parts.length - 1; i++) { const part = parts[i]; if (!current[part]) { current[part] = {}; } current = current[part]; } // Add the entry const filename = parts[parts.length - 1]; current[filename] = { priority: entry.priority, problem: entry.problem, solution: entry.solution, code: entry.code, examples: entry.examples, related_to: includeLinks ? entry.related_to : undefined }; } return JSON.stringify({ metadata: { exported_at: new Date().toISOString(), total_entries: entries.length, structure: 'hierarchical' }, knowledge_tree: byCategory }, null, 2); } /** * Creates a minimal JSON export with just essential fields * @param entries - Array of entries * @returns Minimal JSON string */ export function exportToMinimalJSON(entries) { const minimal = entries.map(({ path, entry }) => ({ path, priority: entry.priority, problem: entry.problem.substring(0, 100) + (entry.problem.length > 100 ? '...' : ''), has_code: !!entry.code, relationships: entry.related_to?.length || 0 })); return JSON.stringify({ summary: { total: entries.length, exported_at: new Date().toISOString() }, entries: minimal }, null, 2); } /** * Creates a JSON export optimized for searching * @param entries - Array of entries * @returns Search-optimized JSON string */ export function exportToSearchJSON(entries) { const searchData = entries.map(({ path, entry }) => ({ path, priority: entry.priority, content: [ entry.problem, entry.solution, entry.code || '' ].join(' '), tags: [ entry.priority, ...path.split('/').slice(0, -1), ...(entry.related_to?.map(r => r.relationship) || []) ] })); return JSON.stringify({ version: '1.0.0', index_created: new Date().toISOString(), total_documents: entries.length, documents: searchData }, null, 2); } //# sourceMappingURL=json.js.map