export function currentModelFromRun(run) {
    if (!run.model_provider || !run.model_id)
        return undefined;
    return { providerID: run.model_provider, modelID: run.model_id, variant: run.model_variant };
}
export function resolveModelRoute(config, targetAgent, currentModel) {
    const override = config.agents[targetAgent];
    const profileName = override?.model_profile;
    const profile = profileName ? config.model_profiles[profileName] : undefined;
    const requested = override?.model
        ? modelAttempt(1, "agent_override", override.model, override.variant, profileName)
        : profile?.model
            ? modelAttempt(1, "model_profile", profile.model, profile.variant, profileName)
            : currentModelAttempt(1, currentModel, profile?.variant);
    const fallbacks = [...(override?.fallbacks ?? []), ...(profile?.fallbacks ?? [])];
    const attempts = [
        ...(requested ? [requested] : []),
        ...fallbacks.map((fallback, index) => fallbackAttempt(config, index + (requested ? 2 : 1), fallback, profileName)),
    ].filter((attempt) => Boolean(attempt));
    const deduped = dedupeAttempts(attempts).map((attempt, index) => ({ ...attempt, attempt: index + 1 }));
    const warnings = deduped
        .filter((attempt) => attempt.model && (!attempt.providerID || !attempt.modelID))
        .map((attempt) => `Model route ${attempt.model} is not provider/model; OpenCode prompt fallback cannot target it explicitly.`);
    return {
        target_agent: targetAgent,
        model_profile: profileName,
        requested_model: requested?.model,
        attempts: deduped,
        warnings,
    };
}
export function modelRefForPrompt(attempt) {
    if (!attempt.providerID || !attempt.modelID)
        return undefined;
    return { providerID: attempt.providerID, modelID: attempt.modelID };
}
export function compactModelRoute(route) {
    return {
        target_agent: route.target_agent,
        model_profile: route.model_profile,
        requested_model: route.requested_model,
        fallback_count: Math.max(0, route.attempts.length - 1),
        warnings: route.warnings,
    };
}
function modelAttempt(attempt, source, model, variant, modelProfile) {
    const parsed = parseModelString(model);
    return {
        attempt,
        source,
        model,
        providerID: parsed?.providerID,
        modelID: parsed?.modelID,
        variant,
        model_profile: modelProfile,
    };
}
function currentModelAttempt(attempt, currentModel, variant) {
    if (!currentModel?.providerID || !currentModel.modelID)
        return undefined;
    return {
        attempt,
        source: "opencode_current",
        model: `${currentModel.providerID}/${currentModel.modelID}`,
        providerID: currentModel.providerID,
        modelID: currentModel.modelID,
        variant: currentModel.variant ?? variant,
    };
}
function fallbackAttempt(config, attempt, fallback, modelProfile) {
    if (typeof fallback === "string")
        return modelAttempt(attempt, "fallback", fallback, undefined, modelProfile);
    if (fallback.model_profile) {
        const profile = config.model_profiles[fallback.model_profile];
        if (profile?.model)
            return modelAttempt(attempt, "fallback", profile.model, profile.variant, fallback.model_profile);
        return { attempt, source: "fallback", model_profile: fallback.model_profile };
    }
    if (!fallback.model)
        return undefined;
    return modelAttempt(attempt, "fallback", fallback.model, fallback.variant, modelProfile);
}
function parseModelString(model) {
    const trimmed = model.trim();
    const slash = trimmed.indexOf("/");
    if (slash <= 0 || slash === trimmed.length - 1)
        return undefined;
    return {
        providerID: trimmed.slice(0, slash),
        modelID: trimmed.slice(slash + 1),
    };
}
function dedupeAttempts(attempts) {
    const seen = new Set();
    const result = [];
    for (const attempt of attempts) {
        const key = `${attempt.model ?? ""}|${attempt.variant ?? ""}|${attempt.model_profile ?? ""}`;
        if (seen.has(key))
            continue;
        seen.add(key);
        result.push(attempt);
    }
    return result;
}
//# sourceMappingURL=index.js.map