import { WorkerBootstrapResponseSchema, } from "dp-shared";
import { DipclawConfig } from "./types.js";
/**
 * Validate a raw bootstrap response payload (anything that survives JSON
 * deserialization), then transform it into a runtime-ready DipclawConfig
 * plus the center-only extras. Throws on schema failure with a Zod-formatted
 * issue list.
 */
export function parseCenterBootstrap(raw, opts) {
    const response = WorkerBootstrapResponseSchema.parse(raw);
    return bootstrapResponseToConfig(response, opts);
}
/**
 * Lower-level variant for callers that already validated the response via
 * dp-shared's schema (e.g. the center-client's fetch wrapper).
 */
export function bootstrapResponseToConfig(response, opts) {
    const c = response.config;
    const llm = {
        protocol: c.llm.protocol,
        baseUrl: c.llm.base_url,
        apiKey: c.llm.api_key,
        model: c.llm.model,
        ...(c.llm.image_generation
            ? {
                imageGeneration: {
                    apiKey: c.llm.image_generation.api_key,
                    model: c.llm.image_generation.model,
                    size: c.llm.image_generation.size,
                    timeoutMs: c.llm.image_generation.timeout_ms,
                },
            }
            : {}),
    };
    const telegram = c.telegram
        ? {
            botToken: c.telegram.bot_token,
            allowedUsers: c.telegram.allowed_users,
        }
        : undefined;
    const browser = {
        connectionMode: "extension-relay",
        headless: c.headless ?? opts.headless ?? false,
        allowEvaluateFallbacks: false,
        backend: c.browser_automation_mode ?? "chrome",
        // Immutable per-worker anchor for the cloakbrowser fingerprint seed.
        fingerprintKey: c.worker_id,
        ...(c.proxy
            ? {
                proxy: {
                    protocol: c.proxy.protocol,
                    host: c.proxy.host,
                    port: c.proxy.port,
                    username: c.proxy.username,
                    password: c.proxy.password,
                },
            }
            : {}),
        ...(c.timezone ? { timezone: c.timezone } : {}),
    };
    // scheduler.* limits travel even though Scheduler.start() will be skipped
    // in center mode — the watchdog reads chromeMaxRuntimeMs and task_timeout_ms
    // is consumed by the center dispatch path.
    const scheduler = {
        taskTimeoutMs: c.limits.task_timeout_ms,
        chromeMaxRuntimeMs: c.limits.chrome_max_runtime_ms,
        // Defaults from DipclawConfig — bootstrap doesn't transmit these tuning
        // knobs; the host can override via env vars later if needed.
        chromeRestartCheckIntervalMs: 10 * 60 * 1000,
        chromeRestartMinLeadMs: 10 * 60 * 1000,
        chromeIdleCloseMs: 5 * 60 * 1000,
        chromeIdleCloseMinLeadMs: 2 * 60 * 1000,
        chromeIdleCloseCheckIntervalMs: 60 * 1000,
        ...(c.timezone ? { timezone: c.timezone } : {}),
    };
    const config = DipclawConfig.parse({
        name: c.name,
        workspace: opts.workspace,
        llm,
        telegram,
        maxIterations: c.limits.max_iterations,
        debug: false,
        tui: false,
        scheduler,
        browser,
        // org: intentionally OMITTED — §worker-D forbids propagating any org field
        // from center bootstrap, even if a future server release ships one.
    });
    const extras = {
        workerId: c.worker_id,
        configEtag: c.config_etag,
        socialAccounts: c.social_accounts,
        ...(response.session_blob ? { sessionBlob: response.session_blob } : {}),
    };
    return { config, extras };
}
//# sourceMappingURL=center-config.js.map