@mmlotfy/intellicodemcp
Version:
IntelliCodeMCP - Advanced AI Model Context Protocol System for intelligent code management and orchestration
137 lines • 5.98 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeMemoryFile = executeMemoryFile;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const fuse_js_1 = __importDefault(require("fuse.js"));
async function executeMemoryFile(args) {
// Use absolute path to ensure consistent behavior regardless of working directory
const basePath = path_1.default.resolve(process.cwd(), 'intelliMemoryHub');
console.error(`Memory Bank using base path: ${basePath}`);
let filePath;
switch (args.category) {
case 'docs':
filePath = path_1.default.join(basePath, 'docs', args.file_name);
break;
case 'threads':
filePath = path_1.default.join(basePath, 'threads', args.file_name);
break;
case 'knots':
filePath = path_1.default.join(basePath, 'knots', args.file_name);
break;
case 'technical':
filePath = path_1.default.join(basePath, 'technical', args.file_name);
break;
case 'profiles':
filePath = path_1.default.join(basePath, 'model_profiles', args.file_name);
break;
case 'search':
filePath = path_1.default.join(basePath, 'docs/search_results', args.file_name);
break;
default:
throw new Error(`Invalid category: ${args.category}`);
}
try {
switch (args.action) {
case 'read':
const content = await fs_1.promises.readFile(filePath, 'utf8');
return { status: 'success', content };
case 'write':
if (!args.content) {
throw new Error('Content is required for write action');
}
await fs_1.promises.mkdir(path_1.default.dirname(filePath), { recursive: true });
await fs_1.promises.writeFile(filePath, args.content);
await updateSearchIndex(filePath);
return { status: 'success', message: `File ${args.file_name} written` };
case 'append':
if (!args.content) {
throw new Error('Content is required for append action');
}
await fs_1.promises.mkdir(path_1.default.dirname(filePath), { recursive: true });
await fs_1.promises.appendFile(filePath, args.content);
await updateSearchIndex(filePath);
return { status: 'success', message: `File ${args.file_name} appended` };
case 'search':
if (!args.query) {
throw new Error('Query is required for search action');
}
const results = await searchFiles(args.query);
const timestamp = new Date().toISOString().replace(/[-:.]/g, '');
const searchFile = path_1.default.join(basePath, 'docs/search_results', `search_result_${timestamp}.json`);
const searchEntry = {
query: args.query,
timestamp: new Date().toISOString(),
results,
};
await fs_1.promises.mkdir(path_1.default.dirname(searchFile), { recursive: true });
await fs_1.promises.writeFile(searchFile, JSON.stringify(searchEntry, null, 2));
return { status: 'success', results };
default:
throw new Error(`Invalid action: ${args.action}`);
}
}
catch (err) {
return { status: 'error', message: `Operation failed: ${err.message}` };
}
}
async function updateSearchIndex(filePath) {
const indexPath = path_1.default.resolve(process.cwd(), 'intelliMemoryHub', 'docs', 'search_index', 'search_index.json');
let index;
try {
const indexContent = await fs_1.promises.readFile(indexPath, 'utf8');
index = JSON.parse(indexContent);
}
catch {
index = { files: [], last_updated: new Date().toISOString() };
}
const relativePath = path_1.default.relative(process.cwd(), filePath);
const fileEntry = index.files.find(entry => entry.path === relativePath);
const lastUpdated = new Date().toISOString();
if (fileEntry) {
fileEntry.last_updated = lastUpdated;
}
else {
index.files.push({ path: relativePath, last_updated: lastUpdated });
}
index.last_updated = lastUpdated;
await fs_1.promises.mkdir(path_1.default.dirname(indexPath), { recursive: true });
await fs_1.promises.writeFile(indexPath, JSON.stringify(index, null, 2));
}
async function searchFiles(query) {
const basePath = path_1.default.resolve(process.cwd(), 'intelliMemoryHub');
const files = await getFiles(basePath);
const documents = await Promise.all(files.map(async (file) => ({
file: path_1.default.relative(process.cwd(), file),
content: await fs_1.promises.readFile(file, 'utf8'),
})));
const fuse = new fuse_js_1.default(documents, {
keys: ['content'],
includeScore: true,
threshold: 0.3,
});
const results = fuse.search(query);
return results.map((result) => ({
file: result.item.file,
content: result.item.content.slice(0, 200) + '...',
score: result.score || 0,
}));
}
async function getFiles(dir) {
const entries = await fs_1.promises.readdir(dir, { withFileTypes: true });
const files = [];
for (const entry of entries) {
const fullPath = path_1.default.join(dir, entry.name);
if (entry.isDirectory()) {
files.push(...await getFiles(fullPath));
}
else if (fullPath.match(/\.(ts|tsx|js|jsx|txt|md|json|yaml)$/)) {
files.push(fullPath);
}
}
return files;
}
//# sourceMappingURL=memory-bank.js.map