import fs from "node:fs";
import path from "node:path";
import { parse, stringify } from "yaml";
/** Module-level cached configDir. Set once by initAssistantRegistry(). */
let _configDir;
/** Initialize the assistant registry with a config directory. Called by server startup. */
export function initAssistantRegistry(configDir) {
    _configDir = configDir;
    ASSISTANT_REGISTRY = buildRegistry();
}
function getConfigDir() {
    if (!_configDir)
        throw new Error("assistant-registry: initAssistantRegistry() not called");
    return _configDir;
}
// Hardcoded fallback defaults
import { GOAL_ASSISTANT_PROMPT } from "./goal-assistant.js";
import { ROLE_ASSISTANT_PROMPT } from "./role-assistant.js";
import { TOOL_ASSISTANT_PROMPT } from "./tool-assistant.js";
import { STAFF_ASSISTANT_PROMPT } from "./staff-assistant.js";
import { PROJECT_ASSISTANT_PROMPT, PROJECT_ASSISTANT_SCAFFOLDING_PROMPT } from "./project-assistant.js";
/** Hardcoded fallback defaults used when YAML files don't exist on disk. */
const FALLBACK_DEFAULTS = {
    goal: {
        type: "goal",
        title: "Goal Assistant",
        promptTitle: "Goal Creation Assistant",
        prompt: GOAL_ASSISTANT_PROMPT,
    },
    role: {
        type: "role",
        title: "Role Assistant",
        promptTitle: "Role Creation Assistant",
        prompt: ROLE_ASSISTANT_PROMPT,
    },
    tool: {
        type: "tool",
        title: "Tool Assistant",
        promptTitle: "Tool Management Assistant",
        prompt: TOOL_ASSISTANT_PROMPT,
    },
    staff: {
        type: "staff",
        title: "Staff Assistant",
        promptTitle: "Staff Agent Creation Assistant",
        prompt: STAFF_ASSISTANT_PROMPT,
    },
    project: {
        type: "project",
        title: "Project Assistant",
        promptTitle: "Project Registration Assistant",
        prompt: PROJECT_ASSISTANT_PROMPT,
    },
    "project-scaffolding": {
        type: "project-scaffolding",
        title: "Project Scaffolding Assistant",
        promptTitle: "New Project Setup Assistant",
        prompt: PROJECT_ASSISTANT_SCAFFOLDING_PROMPT,
    },
};
/** Returns the path to the assistant YAML config directory. */
function assistantConfigDir() {
    return path.join(getConfigDir(), "roles", "assistant");
}
/** Load a single assistant def from a YAML file, or return undefined. */
function loadYamlDef(filePath) {
    try {
        const content = fs.readFileSync(filePath, "utf-8");
        const data = parse(content);
        if (data && typeof data.type === "string" && typeof data.prompt === "string") {
            return {
                type: data.type,
                title: data.title || data.type,
                promptTitle: data.promptTitle || data.title || data.type,
                prompt: data.prompt,
            };
        }
    }
    catch {
        // File doesn't exist or parse error — fall through
    }
    return undefined;
}
/** Build the registry by reading YAML files from disk, falling back to hardcoded defaults. */
function buildRegistry() {
    const registry = {};
    const configDir = assistantConfigDir();
    // 1. Read YAML files from disk
    if (fs.existsSync(configDir)) {
        try {
            for (const entry of fs.readdirSync(configDir)) {
                if (!entry.endsWith(".yaml") && !entry.endsWith(".yml"))
                    continue;
                const filePath = path.join(configDir, entry);
                const def = loadYamlDef(filePath);
                if (def) {
                    registry[def.type] = def;
                }
            }
        }
        catch {
            // Directory read error — fall through to defaults
        }
    }
    // 2. Fill in any missing types from hardcoded fallback defaults
    for (const [type, def] of Object.entries(FALLBACK_DEFAULTS)) {
        if (!registry[type]) {
            registry[type] = def;
        }
    }
    return registry;
}
// Initialize with fallback defaults (safe at import time — no disk access needed).
// initAssistantRegistry() rebuilds from disk when the server starts.
export let ASSISTANT_REGISTRY = { ...FALLBACK_DEFAULTS };
export function getAssistantDef(type) {
    return ASSISTANT_REGISTRY[type];
}
export function isAssistantType(type) {
    return type in ASSISTANT_REGISTRY;
}
/** Re-read all assistant definitions from disk and rebuild the in-memory registry. */
export function reloadAssistantDefs() {
    ASSISTANT_REGISTRY = buildRegistry();
}
/**
 * Update an assistant definition. Writes to the YAML file on disk and updates
 * the in-memory registry. Returns the updated def, or undefined if the type
 * doesn't exist in the registry.
 */
export function updateAssistantDef(type, updates) {
    const existing = ASSISTANT_REGISTRY[type];
    if (!existing)
        return undefined;
    const updated = {
        type,
        title: updates.title ?? existing.title,
        promptTitle: updates.promptTitle ?? existing.promptTitle,
        prompt: updates.prompt ?? existing.prompt,
    };
    // Write to disk
    const configDir = assistantConfigDir();
    fs.mkdirSync(configDir, { recursive: true });
    const filePath = path.join(configDir, `${type}.yaml`);
    const yamlContent = stringify({
        type: updated.type,
        title: updated.title,
        promptTitle: updated.promptTitle,
        prompt: updated.prompt,
    });
    fs.writeFileSync(filePath, yamlContent, "utf-8");
    // Update in-memory registry
    ASSISTANT_REGISTRY[type] = updated;
    return updated;
}
//# sourceMappingURL=assistant-registry.js.map