import { createModelExecutor } from "../engine/model-executor.js";
import { resolveRunApiStatus } from "./api-status.js";
import { createRunMetrics } from "./metrics.js";
import { resolveModelSelection } from "./model-selection.js";
import { resolveDesiredOutputTokens } from "./output-policy.js";
import { resolveProviderRuntimeBindings } from "./provider-runtime.js";
export function resolveRunModelSpec({ context, envForRun, explicitModelArg, configForSelection, lengthArg, maxOutputTokensArg, }) {
    const selection = resolveModelSelection({
        config: context.config,
        configForCli: configForSelection,
        configPath: context.configPath,
        envForRun,
        explicitModelArg,
    });
    return {
        ...selection,
        fixedModelSpec: selection.requestedModel.kind === "fixed" ? selection.requestedModel : null,
        desiredOutputTokens: resolveDesiredOutputTokens({
            lengthArg,
            maxOutputTokensArg,
        }),
    };
}
export function createRunModelRuntime({ context, env, envForRun, metricsEnv, fetchImpl, execFileImpl, maxOutputTokensArg, timeoutMs, retries, streamingEnabled, requestOptions = {}, log, trace, }) {
    const metrics = createRunMetrics({
        env: metricsEnv,
        fetchImpl,
        maxOutputTokensArg,
    });
    const apiStatus = resolveRunApiStatus(context);
    const providerRuntime = resolveProviderRuntimeBindings({
        env: envForRun,
        envState: context,
        configForCli: context.configForCli,
    });
    const summaryEngine = createModelExecutor({
        env,
        envForRun,
        execFileImpl,
        timeoutMs,
        retries,
        streamingEnabled,
        ...requestOptions,
        cliConfigForRun: context.cliConfigForRun ?? null,
        cliAvailability: context.cliAvailability,
        trackedFetch: metrics.trackedFetch,
        resolveMaxOutputTokensForCall: metrics.resolveMaxOutputTokensForCall,
        resolveMaxInputTokensForCall: metrics.resolveMaxInputTokensForCall,
        llmCalls: metrics.llmCalls,
        log,
        trace,
        providerRuntime,
        openrouterApiKey: apiStatus.openrouterApiKey,
    });
    return {
        metrics,
        apiStatus,
        summaryEngine,
    };
}
export function createExecutableRunModel({ spec, runtime, context, allowAutoCliFallback, summaryStream, requestOptions = {}, }) {
    return {
        ...spec,
        allowAutoCliFallback,
        envForAuto: context.envForAuto,
        cliAvailability: context.cliAvailability,
        openaiUseChatCompletions: context.openaiUseChatCompletions,
        openaiWhisperUsdPerMinute: context.openaiWhisperUsdPerMinute,
        ...requestOptions,
        apiStatus: runtime.apiStatus,
        summaryEngine: runtime.summaryEngine,
        summaryStream,
        getLiteLlmCatalog: runtime.metrics.getLiteLlmCatalog,
        llmCalls: runtime.metrics.llmCalls,
    };
}
//# sourceMappingURL=model-runtime.js.map