import { spawn as nodeSpawn } from "node:child_process";
function defaultContainerName(agentId, executorId) {
    return `d-pi-${agentId}-${executorId}`.replace(/[^a-zA-Z0-9_.-]+/g, "-");
}
export class NodeContainerExecutorLauncher {
    spawn;
    constructor(options = {}) {
        this.spawn = options.spawn ?? nodeSpawn;
    }
    start(input) {
        const { executor } = input;
        const args = [
            "run",
            "--rm",
            "--name",
            executor.containerName ?? defaultContainerName(input.agentId, executor.id),
            "-e",
            `D_PI_TOKEN=${executor.token}`,
            "-e",
            `D_PI_HUB_URL=${input.hubUrl}`,
            "-e",
            `D_PI_AGENT_ID=${input.agentId}`,
            "-e",
            `D_PI_PEER_ID=${executor.peerId}`,
        ];
        for (const [key, value] of Object.entries(executor.env ?? {})) {
            args.push("-e", `${key}=${value}`);
        }
        if (executor.workdir) {
            args.push("-w", executor.workdir);
        }
        args.push(executor.image, ...executor.command);
        const child = this.spawn("docker", args, { cwd: input.cwd, stdio: ["ignore", "pipe", "pipe"] });
        return {
            process: child,
            stop() {
                child.kill("SIGTERM");
            },
        };
    }
}
//# sourceMappingURL=container-executor.js.map