import { createUserMessage } from '../types/message/index.js';
import { toErrorMessage } from '../utils/error.js';
import { getRootLogger } from '../utils/logger.js';
export class LocalTaskGateway {
    agentManager;
    taskContext;
    listener;
    trackedTaskIds = new Set();
    parentInput;
    completionListeners = new Set();
    constructor(agentManager, taskContext, listener, parentInput) {
        this.agentManager = agentManager;
        this.taskContext = taskContext;
        this.listener = listener;
        this.parentInput = parentInput;
    }
    async createTask(options) {
        const task = await this.agentManager.sendMessage({
            agentId: options.agentId,
            input: {
                messages: [createUserMessage(options.prompt)],
                workingDirectory: options.workingDirectory,
                taskStore: this.parentInput?.taskStore,
                runtimeToolOverrides: this.parentInput?.runtimeToolOverrides,
                runtimeContext: options.runtimeContext ?? this.parentInput?.runtimeContext,
            },
            // Phase 6: spawn scope propagates from the gateway's task context.
            // The caller built it at SupervisorAgent boundary (§12.1).
            parentSessionId: this.taskContext.sessionId,
            tenantId: this.taskContext.tenantId,
            projectId: this.taskContext.projectId,
            parentActor: this.taskContext.parentActor,
        }, { ...this.taskContext, budgetTracker: { ...this.taskContext.budgetTracker } }, this.listener);
        this.trackedTaskIds.add(task.taskId);
        this.agentManager
            .waitForCompletion(task.taskId)
            .then(() => {
            const completed = this.agentManager.getInstance(task.taskId);
            if (completed) {
                const handle = toHandle(completed);
                for (const cb of this.completionListeners) {
                    cb(handle);
                }
            }
        })
            .catch((err) => {
            getRootLogger()
                .child({ component: 'LocalTaskGateway' })
                .error('Task completion tracking failed', {
                taskId: task.taskId,
                error: toErrorMessage(err),
            });
        });
        return toHandle(task);
    }
    async waitForTask(taskId) {
        await this.agentManager.waitForCompletion(taskId);
        const task = this.agentManager.getInstance(taskId);
        if (!task) {
            throw new Error(`Task ${taskId} not found after completion`);
        }
        return toHandle(task);
    }
    async continueTask(taskId, message) {
        await this.agentManager.continueTask(taskId, message);
    }
    cancelTask(taskId) {
        this.agentManager.cancel(taskId);
    }
    getTask(taskId) {
        const task = this.agentManager.getInstance(taskId);
        return task ? toHandle(task) : undefined;
    }
    listTasks() {
        const handles = [];
        for (const taskId of this.trackedTaskIds) {
            const task = this.agentManager.getInstance(taskId);
            if (task)
                handles.push(toHandle(task));
        }
        return handles;
    }
    onTaskCompleted(callback) {
        this.completionListeners.add(callback);
        return () => {
            this.completionListeners.delete(callback);
        };
    }
}
function toHandle(task) {
    return {
        taskId: task.taskId,
        agentId: task.agentId,
        state: task.state,
        result: task.result,
        createdAt: task.createdAt,
        completedAt: task.completedAt,
    };
}
//# sourceMappingURL=local.js.map