export const GITHUB_MODELS_BASE_URL = "https://models.github.ai/inference";
export const GITHUB_MODELS_API_VERSION = "2026-03-10";
const GITHUB_COPILOT_PROVIDER_PATTERNS = {
    openai: [/^gpt-/i, /^chatgpt-/i, /^o\d(?=$|[-.])/i],
    anthropic: [/^claude-/i, /^(opus|sonnet|haiku)-/i],
    google: [/^gemini-/i],
    xai: [/^grok-/i],
};
export function resolveGitHubModelsApiKey(env) {
    const githubToken = env.GITHUB_TOKEN?.trim();
    if (githubToken)
        return githubToken;
    const ghToken = env.GH_TOKEN?.trim();
    return ghToken || null;
}
function inferGitHubCopilotProvider(model) {
    for (const [provider, patterns] of Object.entries(GITHUB_COPILOT_PROVIDER_PATTERNS)) {
        if (patterns.some((pattern) => pattern.test(model))) {
            return provider;
        }
    }
    return null;
}
export function resolveGitHubCopilotBackendModelId(model) {
    const trimmed = model.trim();
    if (!trimmed)
        return trimmed;
    if (trimmed.includes("/"))
        return trimmed;
    const provider = inferGitHubCopilotProvider(trimmed);
    if (provider === "anthropic" && /^(opus|sonnet|haiku)-/i.test(trimmed)) {
        return `anthropic/claude-${trimmed}`;
    }
    if (provider)
        return `${provider}/${trimmed}`;
    return trimmed;
}
export function buildGitHubModelsHeaders(existing) {
    return {
        ...(existing ?? {}),
        Accept: "application/vnd.github+json",
        "X-GitHub-Api-Version": GITHUB_MODELS_API_VERSION,
    };
}
export function resolveGitHubModelsCompatFallbackModelId(modelId) {
    const normalized = modelId.trim().toLowerCase();
    if (!normalized.startsWith("openai/gpt-5") || normalized === "openai/gpt-5-chat") {
        return null;
    }
    return "openai/gpt-5-chat";
}
export function shouldRetryGitHubModelsCompat(error) {
    const statusCode = typeof error?.statusCode === "number"
        ? Number(error.statusCode)
        : null;
    if (statusCode === 400 || statusCode === 404 || statusCode === 500 || statusCode === 502) {
        return true;
    }
    const message = error instanceof Error
        ? error.message
        : typeof error?.errorMessage === "string"
            ? String(error.errorMessage)
            : typeof error === "string"
                ? error
                : "";
    return /\b(?:400|404|500|502)\b/u.test(message);
}
//# sourceMappingURL=github-models.js.map