/**
 * Capability validation — checks that referenced plugins and MCP servers exist.
 *
 * Validates at workflow creation/update time. If a step references a plugin
 * that doesn't exist or whose MCP server isn't built, the workflow is invalid.
 */
import { existsSync } from "node:fs";
import { resolve } from "node:path";
/**
 * Validate that every step's referenced plugin directory and MCP server exist.
 * Returns per-step validation results.
 */
export function validateCapabilities(steps, platformRoot) {
    const pluginsRoot = resolve(platformRoot, "plugins");
    const errors = [];
    for (const step of steps) {
        // LLM steps don't reference plugins — no capability validation needed
        if (step.type === "llm")
            continue;
        const pluginDir = resolve(pluginsRoot, step.plugin);
        // Path traversal check
        if (!pluginDir.startsWith(pluginsRoot + "/")) {
            errors.push({
                stepId: step.stepId,
                label: step.label,
                plugin: step.plugin,
                tool: step.tool,
                reason: `Invalid plugin name: "${step.plugin}" — path traversal blocked`,
            });
            continue;
        }
        // Plugin directory existence
        if (!existsSync(pluginDir)) {
            errors.push({
                stepId: step.stepId,
                label: step.label,
                plugin: step.plugin,
                tool: step.tool,
                reason: `Plugin not found: "${step.plugin}" — directory does not exist at ${pluginDir}`,
            });
            continue;
        }
        // MCP server built
        const serverPath = resolve(pluginDir, "mcp", "dist", "index.js");
        if (!existsSync(serverPath)) {
            errors.push({
                stepId: step.stepId,
                label: step.label,
                plugin: step.plugin,
                tool: step.tool,
                reason: `Plugin "${step.plugin}" exists but MCP server is not built — expected ${serverPath}`,
            });
        }
    }
    return {
        valid: errors.length === 0,
        errors,
    };
}
//# sourceMappingURL=validate-capabilities.js.map