import { createProviderBackedTaskRunner, } from "../providers/provider-runtime.js";
import { createRuntimeBackedTaskRunner, } from "../runtime/runtime-backed-task-runner.js";
export async function createHarnessTaskRunner(options) {
    const env = {
        ...(options.env ?? {}),
        OMK_RUN_ID: options.runId,
    };
    if (options.useRuntimeBacked === true || options.mode === "chat") {
        const factory = options.factories?.runtimeBacked ?? createRuntimeBackedTaskRunner;
        return factory({
            cwd: options.root,
            env,
            runId: options.runId,
            runtimePolicy: options.runtimeOptions?.runtimePolicy ?? options.providerPolicy,
            defaultRuntime: options.runtimeOptions?.defaultRuntime,
            fallbackChain: options.runtimeOptions?.fallbackChain,
            goal: options.runtimeOptions?.goal,
            onOutput: options.runtimeOptions?.onOutput,
        });
    }
    const providerOptions = options.providerOptions ?? {};
    const providerEnv = {
        ...env,
        ...(providerOptions.model ? { OMK_PROVIDER_MODEL: providerOptions.model } : {}),
    };
    const factory = options.factories?.providerBacked ?? createProviderBackedTaskRunner;
    return factory({
        ...(providerOptions.providerBackedOptions ?? {}),
        cwd: options.root,
        providerPolicy: options.providerPolicy,
        eventRunDir: providerOptions.eventRunDir ?? providerOptions.providerBackedOptions?.eventRunDir,
        deepseekPromptPrefix: providerOptions.deepseekPromptPrefix ?? providerOptions.providerBackedOptions?.deepseekPromptPrefix,
        allowDeepSeekAdvisoryFileNodes: providerOptions.allowDeepSeekAdvisoryFileNodes
            ?? providerOptions.providerBackedOptions?.allowDeepSeekAdvisoryFileNodes,
        fallbackChain: providerOptions.fallbackChain ?? providerOptions.providerBackedOptions?.fallbackChain,
        kimi: {
            ...(providerOptions.providerBackedOptions?.kimi ?? {}),
            cwd: providerOptions.providerBackedOptions?.kimi?.cwd ?? options.root,
            timeout: providerOptions.providerBackedOptions?.kimi?.timeout ?? 0,
            agentFile: providerOptions.agentFile ?? providerOptions.providerBackedOptions?.kimi?.agentFile,
            promptPrefix: providerOptions.promptPrefix ?? providerOptions.providerBackedOptions?.kimi?.promptPrefix,
            mcpScope: providerOptions.mcpScope ?? providerOptions.providerBackedOptions?.kimi?.mcpScope ?? "project",
            skillsScope: providerOptions.skillsScope ?? providerOptions.providerBackedOptions?.kimi?.skillsScope ?? "project",
            hooksScope: providerOptions.hooksScope ?? providerOptions.providerBackedOptions?.kimi?.hooksScope ?? "project",
            roleAgentFiles: providerOptions.providerBackedOptions?.kimi?.roleAgentFiles ?? true,
            mcpNames: providerOptions.mcpNames ?? providerOptions.providerBackedOptions?.kimi?.mcpNames ?? [],
            skillNames: providerOptions.skillNames ?? providerOptions.providerBackedOptions?.kimi?.skillNames ?? [],
            hookNames: providerOptions.hookNames ?? providerOptions.providerBackedOptions?.kimi?.hookNames ?? [],
            toolNames: providerOptions.toolNames ?? providerOptions.providerBackedOptions?.kimi?.toolNames ?? [],
            env: {
                ...(providerOptions.providerBackedOptions?.kimi?.env ?? {}),
                ...providerEnv,
            },
        },
    });
}
