/**
 * Specialist persistence utilities.
 *
 * Specialists are stored as JSON files in ~/.clawx/specialists/.
 * They survive cleanup and are auto-spawned on agent_serve.
 */
import { existsSync, mkdirSync, readFileSync, writeFileSync, readdirSync, unlinkSync } from 'fs';
import { join } from 'path';
import { homedir } from 'os';
import { log } from './logger.js';
const SPECIALISTS_DIR = join(homedir(), '.clawx', 'specialists');
function ensureDir() {
    if (!existsSync(SPECIALISTS_DIR)) {
        mkdirSync(SPECIALISTS_DIR, { recursive: true });
    }
}
export function getSpecialistsDir() {
    return SPECIALISTS_DIR;
}
export function listSpecialists() {
    ensureDir();
    const results = [];
    for (const file of readdirSync(SPECIALISTS_DIR)) {
        if (!file.endsWith('.json'))
            continue;
        try {
            const content = readFileSync(join(SPECIALISTS_DIR, file), 'utf8');
            const def = JSON.parse(content);
            if (def.name && def.persona) {
                results.push(def);
            }
        }
        catch {
            log.debug(`Skipping invalid specialist file: ${file}`);
        }
    }
    return results;
}
export function getSpecialist(name) {
    const filePath = join(SPECIALISTS_DIR, `${name}.json`);
    if (!existsSync(filePath))
        return undefined;
    try {
        return JSON.parse(readFileSync(filePath, 'utf8'));
    }
    catch {
        return undefined;
    }
}
export function saveSpecialist(def) {
    ensureDir();
    const filePath = join(SPECIALISTS_DIR, `${def.name}.json`);
    writeFileSync(filePath, JSON.stringify(def, null, 2), 'utf8');
    log.info(`Specialist saved: ${def.name} → ${filePath}`);
}
export function removeSpecialist(name) {
    const filePath = join(SPECIALISTS_DIR, `${name}.json`);
    if (!existsSync(filePath))
        return false;
    unlinkSync(filePath);
    log.info(`Specialist removed: ${name}`);
    return true;
}
//# sourceMappingURL=specialist-utils.js.map