/**
 * The skill-aware `SubscriptionDispatcher`. Sits between the event router
 * and the existing registries: receives a `(subscription, event)` pair,
 * resolves the target, and invokes it in the right way.
 *
 * Step 14 wires the subagent target end-to-end:
 *   - synthesize a `SubagentTask` with `task.context.trigger` (shape
 *     matches issue #515) and a canned `task.prompt` that tells the model
 *     a trigger fired and what its payload looks like;
 *   - hand the task to a `SubagentExecutorLike` to run.
 *
 * Command and tool targets are stubbed - plan open question 4 still owes
 * a concrete re-injection design for command targets, and tool targets
 * are explicitly out of scope per plan step 8.
 *
 * See `agents/2026-05-20-skills-unification-plan.md` step 14.
 */
export class SkillDispatcher {
    options;
    constructor(options) {
        this.options = options;
    }
    async dispatch(subscription, event) {
        const target = subscription.target;
        if (target.kind === 'agent') {
            await this.dispatchAgent(subscription, event);
            return;
        }
        if (target.kind === 'command') {
            this.options.onUnsupportedTarget?.(subscription, 'command targets are not yet supported - see open question 4');
            return;
        }
        if (target.kind === 'tool') {
            this.options.onUnsupportedTarget?.(subscription, 'tool targets are deferred until a real use case lands');
            return;
        }
    }
    async dispatchAgent(subscription, event) {
        const task = buildTriggeredTask(subscription, event);
        const mode = modeForSubscription(subscription);
        let checkpointId;
        if (mode !== 'plan' && this.options.checkpointer) {
            try {
                checkpointId = await this.options.checkpointer.create(checkpointReason(subscription, event));
            }
            catch {
                // Checkpoint failure is non-fatal: the triggered run still
                // proceeds, but the activity report omits the checkpoint id.
            }
        }
        const executor = this.options.buildExecutor(mode);
        const start = Date.now();
        const result = await executor.execute(task);
        const durationMs = Date.now() - start;
        this.options.onActivity?.({
            subscription,
            event,
            mode,
            result,
            checkpointId,
            durationMs,
        });
    }
}
function checkpointReason(subscription, event) {
    const target = `${subscription.target.kind}:${subscription.target.name}`;
    return `trigger:${event.kind}:${target}`;
}
/**
 * Pick the development mode the triggered run should execute in. Default
 * is `headless` (autonomous, no foreground prompts). `confirm: true` opts
 * the subscription into `plan` mode, which surfaces what the subagent
 * would have done without applying any mutations.
 */
export function modeForSubscription(subscription) {
    return subscription.confirm ? 'plan' : 'headless';
}
/**
 * Build the `SubagentTask` for a triggered subagent run. Exported so the
 * registrar's spec - and later the daemon - can inspect the exact shape
 * without standing up a full router.
 */
export function buildTriggeredTask(subscription, event) {
    const trigger = event.kind === 'file.changed'
        ? { type: 'event', kind: 'file.changed', payload: event.payload }
        : { type: 'event', kind: 'schedule.cron', payload: event.payload };
    const payloadJson = JSON.stringify(event.payload);
    const prompt = `An event of kind \`${event.kind}\` fired. Payload: \`${payloadJson}\`. Proceed according to your instructions.`;
    return {
        subagent_type: subscription.target.name,
        description: `Triggered by ${event.kind} (subscription ${subscription.id})`,
        prompt,
        context: { trigger },
    };
}
//# sourceMappingURL=dispatcher.js.map