ai-expert
Version:
AI Expert CLI - Advanced management system for specialized Claude assistants
192 lines • 6.34 kB
JavaScript
import { promises as fs } from 'fs';
import { join } from 'path';
import { homedir } from 'os';
export class StorageService {
baseDir;
expertsDir;
systemsDir;
contextsDir;
rulesDir;
configPath;
constructor(baseDir) {
this.baseDir = baseDir || join(homedir(), '.ai-experts');
this.expertsDir = join(this.baseDir, 'experts');
this.systemsDir = join(this.baseDir, 'systems');
this.contextsDir = join(this.baseDir, 'contexts');
this.rulesDir = join(this.baseDir, 'rules');
this.configPath = join(this.baseDir, 'config.json');
}
async init() {
const dirs = [this.baseDir, this.expertsDir, this.systemsDir, this.contextsDir, this.rulesDir];
for (const dir of dirs) {
await fs.mkdir(dir, { recursive: true });
}
// Initialize config if not exists
try {
await fs.access(this.configPath);
}
catch {
const defaultConfig = {
version: '1.0.0',
expertsDir: this.expertsDir,
templatesDir: join(this.baseDir, 'templates'),
categories: {
experts: ['development', 'design', 'data', 'infrastructure', 'custom'],
systems: ['general', 'technical', 'creative', 'analytical', 'custom'],
contexts: ['web', 'mobile', 'backend', 'data-science', 'devops', 'custom']
}
};
await this.saveConfig(defaultConfig);
}
}
getBaseDir() {
return this.baseDir;
}
getExpertsDir() {
return this.expertsDir;
}
getSystemsDir() {
return this.systemsDir;
}
getContextsDir() {
return this.contextsDir;
}
getRulesDir() {
return this.rulesDir;
}
// Config methods
async getConfig() {
const content = await fs.readFile(this.configPath, 'utf-8');
return JSON.parse(content);
}
async saveConfig(config) {
await fs.writeFile(this.configPath, JSON.stringify(config, null, 2));
}
// Expert methods
async saveExpert(expert) {
const path = join(this.expertsDir, `${expert.slug}.json`);
await fs.writeFile(path, JSON.stringify(expert, null, 2));
}
async getExpert(slug) {
try {
const path = join(this.expertsDir, `${slug}.json`);
const content = await fs.readFile(path, 'utf-8');
return JSON.parse(content);
}
catch {
return null;
}
}
async listExperts(category) {
const files = await fs.readdir(this.expertsDir);
const experts = [];
for (const file of files) {
if (file.endsWith('.json')) {
const content = await fs.readFile(join(this.expertsDir, file), 'utf-8');
const expert = JSON.parse(content);
if (!category || expert.category === category) {
experts.push(expert);
}
}
}
return experts.sort((a, b) => (b.usageCount || 0) - (a.usageCount || 0));
}
async deleteExpert(slug) {
try {
const path = join(this.expertsDir, `${slug}.json`);
await fs.unlink(path);
return true;
}
catch {
return false;
}
}
// System methods
async saveSystem(system) {
const path = join(this.systemsDir, `${system.id}.json`);
await fs.writeFile(path, JSON.stringify(system, null, 2));
}
async getSystem(id) {
try {
const path = join(this.systemsDir, `${id}.json`);
const content = await fs.readFile(path, 'utf-8');
return JSON.parse(content);
}
catch {
return null;
}
}
async listSystems(category) {
const files = await fs.readdir(this.systemsDir);
const systems = [];
for (const file of files) {
if (file.endsWith('.json')) {
const content = await fs.readFile(join(this.systemsDir, file), 'utf-8');
const system = JSON.parse(content);
if (!category || system.category === category) {
systems.push(system);
}
}
}
return systems;
}
// Context methods
async saveContext(context) {
const path = join(this.contextsDir, `${context.id}.json`);
await fs.writeFile(path, JSON.stringify(context, null, 2));
}
async getContext(id) {
try {
const path = join(this.contextsDir, `${id}.json`);
const content = await fs.readFile(path, 'utf-8');
return JSON.parse(content);
}
catch {
return null;
}
}
async listContexts(category) {
const files = await fs.readdir(this.contextsDir);
const contexts = [];
for (const file of files) {
if (file.endsWith('.json')) {
const content = await fs.readFile(join(this.contextsDir, file), 'utf-8');
const context = JSON.parse(content);
if (!category || context.category === category) {
contexts.push(context);
}
}
}
return contexts;
}
// Rule methods
async saveRule(rule) {
const path = join(this.rulesDir, `${rule.id}.json`);
await fs.writeFile(path, JSON.stringify(rule, null, 2));
}
async getRule(id) {
try {
const path = join(this.rulesDir, `${id}.json`);
const content = await fs.readFile(path, 'utf-8');
return JSON.parse(content);
}
catch {
return null;
}
}
async listRules(category) {
const files = await fs.readdir(this.rulesDir);
const rules = [];
for (const file of files) {
if (file.endsWith('.json')) {
const content = await fs.readFile(join(this.rulesDir, file), 'utf-8');
const rule = JSON.parse(content);
if (!category || rule.category === category) {
rules.push(rule);
}
}
}
return rules;
}
}
//# sourceMappingURL=storage.js.map