import { COPILOT_CATALOG_HEADERS, DEFAULT_COPILOT_API_BASE_URL } from "./copilot-model-catalog.js";
import { withContextWindowOptions } from "./context-window.js";
const GITHUB_COPILOT_PROVIDER = "github-copilot";
const ENDPOINT_API_PREFERENCE = [
    ["/v1/messages", "anthropic-messages"],
    ["/responses", "openai-responses"],
    ["/chat/completions", "openai-completions"],
];
const ZERO_COST = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
const ADAPTIVE_THINKING_LEVEL_MAP = { minimal: "low", xhigh: "max", max: "max" };
export function copilotThinkingLevelMapFor(entry, api) {
    const advertised = entry.supports?.reasoningEffortLevels;
    if (!advertised || advertised.length === 0) {
        return entry.supports?.adaptiveThinking ? ADAPTIVE_THINKING_LEVEL_MAP : undefined;
    }
    const levels = new Set(advertised);
    const map = {
        minimal: levels.has("minimal") ? "minimal" : null,
        low: levels.has("low") ? "low" : null,
        medium: levels.has("medium") ? "medium" : null,
        high: levels.has("high") ? "high" : null,
    };
    if (levels.has("none"))
        map.off = "none";
    else if (api !== "anthropic-messages" || !entry.supports?.adaptiveThinking)
        map.off = null;
    if (levels.has("xhigh"))
        map.xhigh = "xhigh";
    else if (levels.has("max") && entry.supports?.adaptiveThinking)
        map.xhigh = "max";
    if (levels.has("max"))
        map.max = "max";
    return map;
}
function mapCopilotApi(entry) {
    const endpoints = new Set(entry.supportedEndpoints ?? []);
    return ENDPOINT_API_PREFERENCE.find(([endpoint]) => endpoints.has(endpoint))?.[1];
}
function hasReasoning(entry) {
    return Boolean(entry.supports?.reasoningEffort || entry.supports?.adaptiveThinking || entry.supports?.minThinkingBudget || entry.supports?.maxThinkingBudget);
}
function canSynthesizeCopilotModel(id, entry) {
    if (id.includes("/"))
        return false;
    if (entry.modelPickerEnabled !== true)
        return false;
    if (entry.type !== "chat")
        return false;
    if (entry.policyState?.toLowerCase() === "disabled")
        return false;
    return mapCopilotApi(entry) !== undefined;
}
export function copilotTemplateFromModels(models) {
    const sibling = models.find((model) => model.provider === GITHUB_COPILOT_PROVIDER);
    return {
        baseUrl: sibling?.baseUrl ?? DEFAULT_COPILOT_API_BASE_URL,
        headers: sibling?.headers ?? COPILOT_CATALOG_HEADERS,
    };
}
export function synthesizeCopilotCatalogModels(catalog, existingIds, template) {
    const synthesized = [];
    for (const [id, entry] of catalog) {
        if (existingIds.has(id) || !canSynthesizeCopilotModel(id, entry))
            continue;
        const api = mapCopilotApi(entry);
        if (!api)
            continue;
        let model = {
            id,
            name: entry.displayName ?? id,
            api,
            provider: GITHUB_COPILOT_PROVIDER,
            baseUrl: template.baseUrl,
            headers: template.headers,
            reasoning: hasReasoning(entry),
            thinkingLevelMap: copilotThinkingLevelMapFor(entry, api),
            compat: entry.supports?.adaptiveThinking ? { forceAdaptiveThinking: true } : undefined,
            input: entry.supports?.vision ? ["text", "image"] : ["text"],
            cost: ZERO_COST,
            maxInputTokens: entry.maxInputTokens,
            contextWindow: entry.contextWindow,
            maxTokens: entry.maxTokens ?? entry.limits?.maxOutputTokens ?? entry.maxInputTokens ?? entry.contextWindow,
        };
        if (entry.contextWindowOptions && entry.contextWindowOptions.length > 1) {
            model = withContextWindowOptions(model, entry.contextWindowOptions);
        }
        synthesized.push(model);
    }
    return synthesized;
}
//# sourceMappingURL=copilot-model-synthesis.js.map