"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createConfiguredWorkerRuntime = createConfiguredWorkerRuntime;
const store_1 = require("../auth-profiles/store");
const resolve_1 = require("../system-prompts/resolve");
const cli_worker_launcher_1 = require("./cli-worker-launcher");
const runtime_failover_1 = require("./runtime-failover");
const system_prompt_names_1 = require("./system-prompt-names");
async function createConfiguredWorkerRuntime(config, options = {}) {
    const { profiles } = await (0, store_1.loadAuthProfiles)(options.homeDirectory);
    return new ConfiguredWorkerRuntime(config, profiles, options.homeDirectory, options.createLauncher, options.now);
}
class ConfiguredWorkerRuntime {
    config;
    profiles;
    homeDirectory;
    #createLauncher;
    #now;
    constructor(config, profiles, homeDirectory, createLauncher, now) {
        this.config = config;
        this.profiles = profiles;
        this.homeDirectory = homeDirectory;
        this.#createLauncher =
            createLauncher ??
                ((input) => new cli_worker_launcher_1.CliWorkerLauncher({
                    name: input.workerName,
                    authProfile: input.authProfile,
                    customWorker: input.customWorker,
                    ...(this.homeDirectory === undefined
                        ? {}
                        : { homeDirectory: this.homeDirectory }),
                    locale: this.config.i18n.defaultLanguage,
                    timeoutMs: this.config.workerTimeoutSec * 1_000,
                    noOutputTimeoutMs: this.config.workerNoOutputTimeoutSec === null
                        ? null
                        : this.config.workerNoOutputTimeoutSec * 1_000,
                }));
        this.#now = now ?? (() => new Date());
    }
    async run(input) {
        return (0, runtime_failover_1.runConfiguredWorkerRuntimeCycle)({
            config: this.config,
            workerPurpose: (0, runtime_failover_1.resolveWorkerRuntimePurposeForWorkflowMode)(input.workflow?.mode),
            profiles: this.profiles,
            homeDirectory: this.homeDirectory,
            createLauncher: this.#createLauncher,
            now: this.#now,
            input: await this.#withSystemPrompts(input, (0, system_prompt_names_1.resolveWorkerRunSystemPromptNames)(input.workflow)),
        });
    }
    async runContinuationIssueSelection(input) {
        return (0, runtime_failover_1.runConfiguredWorkerContinuationIssueSelectionCycle)({
            config: this.config,
            workerPurpose: "thinking",
            profiles: this.profiles,
            homeDirectory: this.homeDirectory,
            createLauncher: this.#createLauncher,
            now: this.#now,
            input: await this.#withSystemPrompts(input, system_prompt_names_1.CONTINUATION_ISSUE_SYSTEM_PROMPT_NAMES),
        });
    }
    async runIssueDraft(input) {
        return (0, runtime_failover_1.runConfiguredWorkerIssueDraftCycle)({
            config: this.config,
            workerPurpose: "thinking",
            profiles: this.profiles,
            homeDirectory: this.homeDirectory,
            createLauncher: this.#createLauncher,
            now: this.#now,
            input: await this.#withSystemPrompts(input, system_prompt_names_1.ISSUE_DRAFT_SYSTEM_PROMPT_NAMES),
        });
    }
    async runTemplateDraft(input) {
        return (0, runtime_failover_1.runConfiguredWorkerTemplateDraftCycle)({
            config: this.config,
            workerPurpose: "thinking",
            profiles: this.profiles,
            homeDirectory: this.homeDirectory,
            createLauncher: this.#createLauncher,
            now: this.#now,
            input: await this.#withSystemPrompts(input, system_prompt_names_1.WORKFLOW_TEMPLATE_AUTHORING_SYSTEM_PROMPT_NAMES),
        });
    }
    async runTemplateUpdate(input) {
        return (0, runtime_failover_1.runConfiguredWorkerTemplateUpdateCycle)({
            config: this.config,
            workerPurpose: "thinking",
            profiles: this.profiles,
            homeDirectory: this.homeDirectory,
            createLauncher: this.#createLauncher,
            now: this.#now,
            input: await this.#withSystemPrompts(input, system_prompt_names_1.WORKFLOW_TEMPLATE_AUTHORING_SYSTEM_PROMPT_NAMES),
        });
    }
    async runAiWorkflowOptionAutofill(input) {
        return (0, runtime_failover_1.runConfiguredWorkerAiWorkflowOptionAutofillCycle)({
            config: this.config,
            workerPurpose: "thinking",
            profiles: this.profiles,
            homeDirectory: this.homeDirectory,
            createLauncher: this.#createLauncher,
            now: this.#now,
            input: await this.#withSystemPrompts(input, system_prompt_names_1.WORKFLOW_OPTIONS_SYSTEM_PROMPT_NAMES),
        });
    }
    async runFollowupProposalReview(input) {
        return (0, runtime_failover_1.runConfiguredWorkerFollowupProposalReviewCycle)({
            config: this.config,
            workerPurpose: "thinking",
            profiles: this.profiles,
            homeDirectory: this.homeDirectory,
            createLauncher: this.#createLauncher,
            now: this.#now,
            input: await this.#withSystemPrompts(input, system_prompt_names_1.FOLLOWUP_PROPOSAL_REVIEW_SYSTEM_PROMPT_NAMES),
        });
    }
    async runWorkflowTemplateFitReview(input) {
        return (0, runtime_failover_1.runConfiguredWorkerWorkflowTemplateFitReviewCycle)({
            config: this.config,
            workerPurpose: "thinking",
            profiles: this.profiles,
            homeDirectory: this.homeDirectory,
            createLauncher: this.#createLauncher,
            now: this.#now,
            input: await this.#withSystemPrompts(input, system_prompt_names_1.WORKFLOW_OPTIONS_SYSTEM_PROMPT_NAMES),
        });
    }
    async runWorkflowPlanReview(input) {
        return (0, runtime_failover_1.runConfiguredWorkerWorkflowPlanReviewCycle)({
            config: this.config,
            workerPurpose: "thinking",
            profiles: this.profiles,
            homeDirectory: this.homeDirectory,
            createLauncher: this.#createLauncher,
            now: this.#now,
            input: await this.#withSystemPrompts(input, system_prompt_names_1.WORKFLOW_PLAN_REVIEW_SYSTEM_PROMPT_NAMES),
        });
    }
    async runPullRequestReviewNecessity(input) {
        return (0, runtime_failover_1.runConfiguredWorkerPullRequestReviewNecessityCycle)({
            config: this.config,
            workerPurpose: "thinking",
            profiles: this.profiles,
            homeDirectory: this.homeDirectory,
            createLauncher: this.#createLauncher,
            now: this.#now,
            input: await this.#withSystemPrompts(input, system_prompt_names_1.PULL_REQUEST_REVIEW_NECESSITY_SYSTEM_PROMPT_NAMES),
        });
    }
    async runCatalogSearchSelection(input) {
        return (0, runtime_failover_1.runConfiguredWorkerCatalogSearchSelectionCycle)({
            config: this.config,
            workerPurpose: "thinking",
            profiles: this.profiles,
            homeDirectory: this.homeDirectory,
            createLauncher: this.#createLauncher,
            now: this.#now,
            input,
        });
    }
    async runEventHook(input) {
        return (0, runtime_failover_1.runConfiguredWorkerEventHookCycle)({
            config: this.config,
            workerPurpose: "default",
            profiles: this.profiles,
            homeDirectory: this.homeDirectory,
            createLauncher: this.#createLauncher,
            now: this.#now,
            input: await this.#withSystemPrompts(input, system_prompt_names_1.WORKER_COMMON_SYSTEM_PROMPT_NAMES),
        });
    }
    async runEventHookDraft(input) {
        return (0, runtime_failover_1.runConfiguredWorkerEventHookDraftCycle)({
            config: this.config,
            workerPurpose: "thinking",
            profiles: this.profiles,
            homeDirectory: this.homeDirectory,
            createLauncher: this.#createLauncher,
            now: this.#now,
            input: await this.#withSystemPrompts(input, system_prompt_names_1.EVENT_HOOK_CREATE_SYSTEM_PROMPT_NAMES),
        });
    }
    async #withSystemPrompts(input, names) {
        if (names.length === 0) {
            return input;
        }
        return {
            ...input,
            systemPrompts: await (0, resolve_1.resolveSystemPrompts)({
                repoRoot: input.repositoryRoot,
                config: this.config,
                names,
            }),
        };
    }
}
