import { createFixedModelAttempt } from "../engine/fixed-model-attempt.js";
import { applyProviderRuntimeToModelAttempt } from "../engine/provider-attempt.js";
import { cliProviderForRequiredEnv, envHasRequiredKey } from "../llm/provider-capabilities.js";
import { buildAutoModelAttempts } from "../model-auto.js";
import { parseCliUserModelId } from "../run/env.js";
function bindAttempt(attempt, providerRuntime) {
    if (attempt.transport === "cli") {
        const parsed = attempt.cliProvider != null
            ? { provider: attempt.cliProvider, model: attempt.cliModel ?? null }
            : parseCliUserModelId(attempt.userModelId);
        return {
            ...attempt,
            cliProvider: parsed.provider,
            cliModel: parsed.model,
        };
    }
    return applyProviderRuntimeToModelAttempt(attempt, providerRuntime);
}
export function resolveFixedModelAttempt({ requestedModel, providerRuntime, }) {
    return bindAttempt(createFixedModelAttempt(requestedModel), providerRuntime);
}
export function resolveModelAttempts({ requestedModel, kind, promptTokens, desiredOutputTokens, requiresVideoUnderstanding, envForAuto, configForModelSelection, catalog, openrouterProvidersFromEnv, cliAvailability, isImplicitAutoSelection = false, allowAutoCliFallback = false, lastSuccessfulCliProvider = null, providerRuntime, }) {
    if (requestedModel.kind === "fixed") {
        return [resolveFixedModelAttempt({ requestedModel, providerRuntime })];
    }
    return buildAutoModelAttempts({
        kind,
        promptTokens,
        desiredOutputTokens,
        requiresVideoUnderstanding,
        env: envForAuto,
        config: configForModelSelection,
        catalog,
        openrouterProvidersFromEnv,
        cliAvailability,
        isImplicitAutoSelection,
        allowAutoCliFallback,
        lastSuccessfulCliProvider,
    }).map((attempt) => bindAttempt(attempt, providerRuntime));
}
export function selectPreferredInteractiveModelAttempt({ attempts, envForAuto, cliAvailability, requireCliAvailability = true, }) {
    const apiAttempt = attempts.find((attempt) => attempt.transport !== "cli" && envHasRequiredKey(envForAuto, attempt.requiredEnv)) ?? null;
    if (apiAttempt)
        return apiAttempt;
    return (attempts.find((attempt) => {
        if (attempt.transport !== "cli")
            return false;
        const provider = attempt.cliProvider ?? cliProviderForRequiredEnv(attempt.requiredEnv) ?? null;
        return provider !== null && (!requireCliAvailability || Boolean(cliAvailability[provider]));
    }) ?? null);
}
//# sourceMappingURL=model-attempts.js.map