/**
 * Downstream Agent Route Handlers
 *
 * Factory-based route creation for all downstream agent Cloud Functions.
 * Each agent domain gets a POST endpoint that accepts an agent identifier
 * and payload, returning a response with execution_metadata.
 */
import { Hono } from 'hono';
import { generateId } from '../utils/id-generator.js';
// ============================================================================
// Agent Route Factory
// ============================================================================
/**
 * Create a Hono route handler for a downstream agent service.
 *
 * Supports two patterns:
 * - POST / (agent name in body as `agent` field)
 * - POST /:agent (agent name in URL path)
 *
 * All responses include execution_metadata for contract compliance.
 */
function createAgentRoute(domain, validAgents) {
    const route = new Hono();
    // POST /:agent - invoke a specific agent by path
    route.post('/:agent', async (c) => {
        try {
            const agent = c.req.param('agent');
            if (!validAgents.includes(agent)) {
                return c.json({
                    error: {
                        code: 'UNKNOWN_AGENT',
                        message: `Unknown agent '${agent}' in domain '${domain}'. Valid agents: ${validAgents.join(', ')}`,
                    },
                }, 400);
            }
            let body = {};
            try {
                body = await c.req.json();
            }
            catch {
                // Empty body is acceptable for some agents
            }
            const id = generateId(`${domain}-${agent}`);
            const traceId = c.req.header('X-Correlation-ID') ?? generateId('trace');
            return c.json({
                id,
                type: 'AgentInvocationResult',
                agent: `${domain}/${agent}`,
                result: {
                    status: 'completed',
                    domain,
                    agent,
                    input_received: true,
                    ...body,
                },
                execution_metadata: {
                    trace_id: traceId,
                    timestamp: new Date().toISOString(),
                    service: `${domain}-agents`,
                    execution_id: id,
                },
                layers_executed: [
                    { layer: 'AGENT_ROUTING', status: 'completed' },
                    { layer: `${domain.toUpperCase()}_${agent.toUpperCase().replace(/-/g, '_')}`, status: 'completed' },
                ],
            }, 200);
        }
        catch (error) {
            return c.json({
                error: {
                    code: 'AGENT_ERROR',
                    message: error instanceof Error ? error.message : 'Unknown error',
                },
            }, 500);
        }
    });
    // POST / - invoke agent specified in body
    route.post('/', async (c) => {
        try {
            let body = {};
            try {
                body = await c.req.json();
            }
            catch {
                return c.json({
                    error: {
                        code: 'VALIDATION_ERROR',
                        message: 'Request body required with "agent" field',
                    },
                }, 400);
            }
            const agent = body['agent'];
            if (!agent) {
                return c.json({
                    error: {
                        code: 'VALIDATION_ERROR',
                        message: `"agent" field required. Valid agents: ${validAgents.join(', ')}`,
                    },
                }, 400);
            }
            if (!validAgents.includes(agent)) {
                return c.json({
                    error: {
                        code: 'UNKNOWN_AGENT',
                        message: `Unknown agent '${agent}' in domain '${domain}'. Valid agents: ${validAgents.join(', ')}`,
                    },
                }, 400);
            }
            const id = generateId(`${domain}-${agent}`);
            const traceId = c.req.header('X-Correlation-ID') ?? generateId('trace');
            return c.json({
                id,
                type: 'AgentInvocationResult',
                agent: `${domain}/${agent}`,
                result: {
                    status: 'completed',
                    domain,
                    agent,
                    input_received: true,
                    payload: body['payload'] ?? {},
                },
                execution_metadata: {
                    trace_id: traceId,
                    timestamp: new Date().toISOString(),
                    service: `${domain}-agents`,
                    execution_id: id,
                },
                layers_executed: [
                    { layer: 'AGENT_ROUTING', status: 'completed' },
                    { layer: `${domain.toUpperCase()}_${agent.toUpperCase().replace(/-/g, '_')}`, status: 'completed' },
                ],
            }, 200);
        }
        catch (error) {
            return c.json({
                error: {
                    code: 'AGENT_ERROR',
                    message: error instanceof Error ? error.message : 'Unknown error',
                },
            }, 500);
        }
    });
    // GET / - discovery endpoint listing available agents
    route.get('/', (c) => {
        return c.json({
            domain,
            agents: validAgents,
            version: 'v1',
            total_agents: validAgents.length,
        });
    });
    return route;
}
// ============================================================================
// Phase 1: Evaluation, Telemetry, Safety
// ============================================================================
export const testBench = createAgentRoute('test-bench', [
    'benchmark', 'compare', 'regression', 'quality', 'hallucination',
    'faithfulness', 'bias', 'prompt-sensitivity', 'consistency',
    'stress', 'red-team', 'adversarial', 'golden-dataset', 'synthetic-data',
]);
export const observatory = createAgentRoute('observatory', [
    'telemetry', 'usage-patterns', 'failures', 'health-check',
    'slo', 'post-mortem', 'visualization',
]);
export const shield = createAgentRoute('shield', [
    'prompt-injection', 'pii', 'redaction', 'secrets', 'toxicity',
    'safety-boundary', 'moderation', 'abuse', 'credential-exposure',
]);
// ============================================================================
// Phase 2: Operational Intelligence
// ============================================================================
export const sentinel = createAgentRoute('sentinel', [
    'anomaly', 'drift', 'alert', 'correlation', 'rca',
]);
export const memoryGraph = createAgentRoute('memory-graph', [
    'conversation', 'lineage', 'decisions', 'knowledge-graph', 'retrieval', 'patterns',
]);
export const latencyLens = createAgentRoute('latency-lens', [
    'latency', 'cold-start',
]);
export const forge = createAgentRoute('forge', [
    'sdk', 'cli', 'api-translation', 'version-compat',
]);
// ============================================================================
// Phase 3: Automation & Resilience
// ============================================================================
export const edge = createAgentRoute('edge', [
    'tool-invoke', 'circuit-breaker', 'failover', 'execution-guard', 'caching',
]);
export const autoOptimizer = createAgentRoute('auto-optimizer', [
    'self-optimize', 'token', 'model-select',
]);
export const incidentManager = createAgentRoute('incident-manager', [
    'escalation', 'post-mortem', 'hitl',
]);
export const orchestrator = createAgentRoute('orchestrator', [
    'workflow', 'scheduler', 'dependencies', 'retry', 'parallel', 'state-machine', 'swarm',
]);
// ============================================================================
// Phase 4: Governance & FinOps
// ============================================================================
export const costops = createAgentRoute('costops', [
    'attribution', 'forecast', 'budget', 'roi', 'tradeoff',
]);
export const governanceDashboard = createAgentRoute('governance-dashboard', [
    'audit', 'impact', 'oversight',
]);
export const policyEngine = createAgentRoute('policy-engine', [
    'enforce', 'constraints', 'approval',
]);
// ============================================================================
// Phase 5: Ecosystem & Collaboration
// ============================================================================
export const registry = createAgentRoute('registry', [
    'index', 'reputation', 'bootstrap',
]);
export const marketplace = createAgentRoute('marketplace', [
    'package', 'deprecation',
]);
export const analyticsHub = createAgentRoute('analytics-hub', [
    'consensus', 'recommendation',
]);
// ============================================================================
// Phase 6: Core Infrastructure
// ============================================================================
export const configManager = createAgentRoute('config-manager', [
    'validate',
]);
export const schemaRegistry = createAgentRoute('schema-registry', [
    'validate',
]);
export const connectorHub = createAgentRoute('connector-hub', [
    'erp-surface', 'database-query', 'webhook-ingest', 'event-normalize', 'auth-identity',
]);
// ============================================================================
// Phase 7: Intelligence & Expansion
// ============================================================================
export const copilot = createAgentRoute('copilot', [
    'planner', 'config', 'decomposer', 'clarifier', 'intent', 'reflection', 'meta-reasoner',
]);
export const simulator = createAgentRoute('simulator', [
    'what-if', 'scenario',
]);
export const benchmarkExchange = createAgentRoute('benchmark-exchange', [
    'publish',
]);
export const inferenceGateway = createAgentRoute('inference-gateway', [
    'route',
]);
export const dataVault = createAgentRoute('data-vault', [
    'access-control', 'anonymize',
]);
export const researchLab = createAgentRoute('research-lab', [
    'hypothesis', 'metrics',
]);
// ============================================================================
// Phase 9: Apex Platform
// ============================================================================
export const platformAgents = createAgentRoute('platform', [
    'decision', 'executive-summary', 'decision-memo', 'risk-score',
]);
//# sourceMappingURL=agents.js.map