function statusEvent(taskId, state, isFinal, contextId, message) {
    return {
        taskId,
        contextId,
        status: {
            state,
            message,
            timestamp: new Date().toISOString(),
        },
        final: isFinal,
    };
}
function artifactEvent(taskId, contextId, artifact) {
    return {
        taskId,
        contextId,
        artifact,
    };
}
const MAPPING = {
    run_started: (e, ctx) => statusEvent(e.runId, 'running', false, ctx),
    run_completed: (e, ctx) => {
        const completedEvent = statusEvent(e.runId, 'completed', true, ctx, {
            role: 'agent',
            parts: [{ kind: 'text', text: e.result }],
        });
        return completedEvent;
    },
    run_failed: (e, ctx) => statusEvent(e.runId, 'failed', true, ctx, {
        role: 'agent',
        parts: [{ kind: 'text', text: e.error }],
    }),
    iteration_started: (e, ctx) => statusEvent(e.runId, 'running', false, ctx, {
        role: 'agent',
        parts: [{ kind: 'text', text: `Iteration ${e.iteration} started` }],
    }),
    tool_completed: (e, ctx) => artifactEvent(e.runId, ctx, {
        artifactId: `tool-${e.toolName}-${Date.now()}`,
        name: `${e.toolName} result`,
        parts: [{ kind: 'text', text: e.result }],
        metadata: {
            toolName: e.toolName,
            toolUseId: e.toolUseId,
            isError: e.isError,
        },
    }),
    tool_review_requested: (e, ctx) => {
        const toolNames = e.toolCalls.map((tc) => tc.name).join(', ');
        return statusEvent(e.runId, 'input-required', false, ctx, {
            role: 'agent',
            parts: [
                { kind: 'text', text: `Review requested for tools: ${toolNames}` },
                {
                    kind: 'data',
                    data: {
                        toolCalls: e.toolCalls.map((tc) => ({
                            id: tc.id,
                            name: tc.name,
                            isDestructive: tc.isDestructive,
                        })),
                    },
                    mimeType: 'application/x-namzu-review-request',
                },
            ],
        });
    },
    plan_ready: (e, ctx) => statusEvent(e.runId, 'input-required', false, ctx, {
        role: 'agent',
        parts: [
            { kind: 'text', text: `Plan ready: ${e.title}` },
            {
                kind: 'data',
                data: {
                    planId: e.planId,
                    title: e.title,
                    summary: e.summary,
                    steps: e.steps.map((s) => ({
                        id: s.id,
                        description: s.description,
                        toolName: s.toolName,
                    })),
                },
                mimeType: 'application/x-namzu-plan',
            },
        ],
    }),
    run_paused: (e, ctx) => statusEvent(e.runId, 'input-required', false, ctx, {
        role: 'agent',
        parts: [{ kind: 'text', text: `Run paused: ${e.reason}` }],
    }),
    iteration_completed: null,
    tool_executing: null,
    tool_review_completed: null,
    checkpoint_created: null,
    run_resuming: null,
    token_usage_updated: null,
    activity_created: null,
    activity_updated: null,
    plan_approved: null,
    plan_rejected: null,
    plan_step_updated: null,
    agent_pending: null,
    agent_completed: null,
    agent_failed: null,
    agent_canceled: null,
    task_created: null,
    task_updated: null,
    plugin_hook_executing: null,
    plugin_hook_completed: null,
    sandbox_created: null,
    sandbox_exec: null,
    sandbox_destroyed: null,
    // Sub-session lifecycle events (session-hierarchy.md §10.4). These are
    // in-flight visibility signals for the kernel bus; the A2A bridge does not
    // surface them today.
    subsession_spawned: null,
    subsession_messaged: null,
    subsession_idled: null,
    // v3 message + tool-input lifecycle (ses_001-tool-stream-events). A2A's
    // status-update model is coarse-grained, so per-delta events are dropped
    // at this layer. `message_completed` carries the aggregated assistant
    // content (codex A1) — A2A surfaces a single status event with the
    // full text, replacing the per-iteration `llm_response` mapping.
    message_started: null,
    text_delta: null,
    message_completed: (e, ctx) => {
        if (!e.content)
            return null;
        return statusEvent(e.runId, 'running', false, ctx, {
            role: 'agent',
            parts: [{ kind: 'text', text: e.content }],
        });
    },
    tool_input_started: null,
    tool_input_delta: null,
    tool_input_completed: null,
};
export function mapRunToA2AEvent(event, contextId) {
    const transform = MAPPING[event.type];
    if (!transform)
        return null;
    return transform(event, contextId);
}
/** @deprecated Use mapRunToA2AEvent */
export const mapSessionToA2AEvent = mapRunToA2AEvent;
//# sourceMappingURL=mapper.js.map