semantic-prompt-mcp
Version:
MCP server for semantic prompt framework - NLP-inspired adaptive reasoning engine for LLM orchestration
70 lines • 2.46 kB
JavaScript
/**
* File system utilities for semantic-prompt-mcp
*/
import { readFileSync, existsSync, readdirSync } from 'fs';
import { join } from 'path';
import { homedir } from 'os';
import { VALIDATION_PATTERNS } from '../constants/index.js';
export class FileUtils {
static expandPath(path) {
if (path.startsWith('~/')) {
return join(homedir(), path.slice(2));
}
return path;
}
static readFileIfExists(filePath) {
try {
if (existsSync(filePath)) {
return readFileSync(filePath, 'utf-8');
}
return null;
}
catch (error) {
console.error(`Failed to read file ${filePath}:`, error);
return null;
}
}
static resolveAvailableAgents(agentsPath, agentsExtension, availableAgents) {
if (availableAgents && availableAgents.length > 0) {
return availableAgents
.map(a => a.toLowerCase())
.filter(a => VALIDATION_PATTERNS.SAFE_NAME.test(a));
}
try {
const base = this.expandPath(agentsPath);
const ext = agentsExtension.toLowerCase();
const entries = readdirSync(base, { withFileTypes: true });
return entries
.filter(e => e.isFile())
.map(e => e.name)
.filter(name => name.toLowerCase().endsWith(ext))
.map(name => name.slice(0, name.length - ext.length).toLowerCase())
.filter(a => VALIDATION_PATTERNS.SAFE_NAME.test(a));
}
catch {
return [];
}
}
static parseAgentsFromToml(tomlContent) {
try {
const match = tomlContent.match(/\bagents\s*=\s*\[([\s\S]*?)\]/i);
if (!match)
return [];
const inner = match[1];
const items = inner.split(',');
const agents = items
.map(s => s.replace(/#.*$/m, ''))
.map(s => s.trim())
.map(s => s.replace(/^['"]/, '').replace(/['"]$/, ''))
.map(s => s.toLowerCase())
.filter(Boolean)
.filter(s => VALIDATION_PATTERNS.SAFE_NAME.test(s));
const seen = new Set();
return agents.filter(a => (seen.has(a) ? false : (seen.add(a), true)));
}
catch {
return [];
}
}
}
//# sourceMappingURL=fileUtils.js.map