/**
 * MCP handler group — kernel execution.
 *
 * Thin routing layer: parses the JSON args, hands off to kernel.run.step /
 * verify / merge, and returns the serialized result. All real work lives
 * in the kernel service; this file exists only so MCP callers get the
 * exact same path CLI callers take.
 */
import { getKernelExecutionService } from '../../../../kernel/execution/index.js';
/**
 * Dispatcher-compatible wrapper. Returns `undefined` when the tool name
 * isn't owned by this group, so the router falls through to the next one.
 */
export async function handleKernelExecutionGroup(toolName, _hctx, _request, args, _ctx) {
    if (toolName !== 'kernel_run_step' &&
        toolName !== 'kernel_run_verify' &&
        toolName !== 'kernel_run_merge') {
        return undefined;
    }
    try {
        return await handleKernelExecutionTool(toolName, args);
    }
    catch (err) {
        return errorResult(String(err?.message ?? err));
    }
}
export async function handleKernelExecutionTool(toolName, args, ctx = {}) {
    const svc = ctx.kernelExecutionService ?? getKernelExecutionService();
    switch (toolName) {
        case 'kernel_run_step': {
            const input = parseStepRequest(args);
            const result = await svc.step(input);
            return jsonResult(result);
        }
        case 'kernel_run_verify': {
            const input = parseVerifyRequest(args);
            const result = await svc.verify(input);
            return jsonResult(result);
        }
        case 'kernel_run_merge': {
            const input = parseMergeRequest(args);
            const result = await svc.merge(input);
            return jsonResult(result);
        }
        default:
            return errorResult(`unknown kernel-exec tool: ${toolName}`);
    }
}
function parseStepRequest(args) {
    const runId = requireString(args, 'runId');
    const stepId = requireString(args, 'stepId');
    const task = requireString(args, 'task');
    return {
        runId,
        stepId,
        task,
        intent: optionalString(args, 'intent'),
        baseRef: optionalString(args, 'baseRef'),
        worktreeHintRef: optionalString(args, 'worktreeHintRef'),
        mutations: Array.isArray(args.mutations) ? args.mutations : undefined,
        metadata: args.metadata ?? undefined,
    };
}
function parseVerifyRequest(args) {
    return {
        runId: requireString(args, 'runId'),
        stepId: requireString(args, 'stepId'),
        verifierIds: Array.isArray(args.verifierIds) ? args.verifierIds : undefined,
        metadata: args.metadata ?? undefined,
    };
}
function parseMergeRequest(args) {
    return {
        runId: requireString(args, 'runId'),
        stepId: requireString(args, 'stepId'),
        verifyId: optionalString(args, 'verifyId'),
        targetRef: requireString(args, 'targetRef'),
        reason: optionalString(args, 'reason'),
        metadata: args.metadata ?? undefined,
    };
}
function requireString(args, key) {
    const v = args[key];
    if (typeof v !== 'string' || v.trim().length === 0) {
        throw new Error(`"${key}" is required (string)`);
    }
    return v;
}
function optionalString(args, key) {
    const v = args[key];
    return typeof v === 'string' && v.length > 0 ? v : undefined;
}
function jsonResult(obj) {
    return { content: [{ type: 'text', text: JSON.stringify(obj, null, 2) }] };
}
function errorResult(message) {
    return { content: [{ type: 'text', text: JSON.stringify({ error: message }) }] };
}
