/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { Hono } from 'hono';
import { PHASE_TO_PROFILE } from '../../services/workflow-instructions.js';
import { buildDelegationPacket, renderPacketAsPrompt, parseSubAgentResponse, } from '../../services/workflow-delegation.js';
export function delegationRoute(cwd) {
    const app = new Hono();
    app.post('/packet', async (c) => {
        const body = await c.req.json();
        if (!body.taskId || !body.runId || !body.phase || !body.goal) {
            return c.json({ error: 'Missing required fields: taskId, runId, phase, goal' }, 400);
        }
        const phase = body.phase;
        if (!PHASE_TO_PROFILE[phase]) {
            return c.json({ error: `Invalid phase: ${body.phase}` }, 400);
        }
        const packet = await buildDelegationPacket({
            cwd,
            taskId: body.taskId,
            runId: body.runId,
            phase,
            goal: body.goal,
            priorPhases: body.priorPhases,
            runtimeTarget: body.runtimeTarget ?? 'generic',
        });
        const prompt = renderPacketAsPrompt(packet);
        return c.json({ packet, prompt });
    });
    app.post('/parse-response', async (c) => {
        const body = await c.req.json();
        if (!body.stdout) {
            return c.json({ error: 'Missing required field: stdout' }, 400);
        }
        const response = parseSubAgentResponse(body.stdout);
        if (!response) {
            return c.json({ parsed: false, response: null, hint: 'No structured response found in output' });
        }
        return c.json({ parsed: true, response });
    });
    return app;
}
//# sourceMappingURL=delegation.js.map