// src/gateway-models.ts
var DEFAULT_GATEWAY_MODEL = "claude-opus-4-7";
var DEFAULT_CODEX_MODEL = "gpt-5.4";
var BLOCKED_MODELS = /* @__PURE__ */ new Set(["gpt-5-mini", "openai/gpt-5-mini"]);
var CACHE_TTL = 10 * 60 * 1e3;
var gatewayModelsCache = null;
async function fetchGatewayModels(options) {
  const gatewayUrl = options?.gatewayUrl ?? process.env.ANTHROPIC_BASE_URL;
  if (!gatewayUrl) {
    return [];
  }
  if (gatewayModelsCache && gatewayModelsCache.url === gatewayUrl && Date.now() < gatewayModelsCache.expiry) {
    return gatewayModelsCache.models;
  }
  const modelsUrl = `${gatewayUrl}/v1/models`;
  try {
    const response = await fetch(modelsUrl);
    if (!response.ok) {
      return [];
    }
    const data = await response.json();
    const models = (data.data ?? []).filter((m) => !BLOCKED_MODELS.has(m.id));
    gatewayModelsCache = {
      models,
      expiry: Date.now() + CACHE_TTL,
      url: gatewayUrl
    };
    return models;
  } catch {
    return [];
  }
}
function isAnthropicModel(model) {
  if (model.owned_by) {
    return model.owned_by === "anthropic";
  }
  return model.id.startsWith("claude-") || model.id.startsWith("anthropic/");
}
function isOpenAIModel(model) {
  if (model.owned_by) {
    return model.owned_by === "openai";
  }
  return model.id.startsWith("gpt-") || model.id.startsWith("openai/");
}
var modelsListCache = null;
async function fetchModelsList(options) {
  const gatewayUrl = options?.gatewayUrl ?? process.env.ANTHROPIC_BASE_URL;
  if (!gatewayUrl) {
    return [];
  }
  if (modelsListCache && modelsListCache.url === gatewayUrl && Date.now() < modelsListCache.expiry) {
    return modelsListCache.models;
  }
  try {
    const modelsUrl = `${gatewayUrl}/v1/models`;
    const response = await fetch(modelsUrl);
    if (!response.ok) {
      return [];
    }
    const data = await response.json();
    const models = Array.isArray(data) ? data : data.data ?? data.models ?? [];
    const results = [];
    for (const model of models) {
      const id = model?.id ? String(model.id) : "";
      if (!id) continue;
      results.push({ id, owned_by: model?.owned_by });
    }
    modelsListCache = {
      models: results,
      expiry: Date.now() + CACHE_TTL,
      url: gatewayUrl
    };
    return results;
  } catch {
    return [];
  }
}
var PROVIDER_NAMES = {
  anthropic: "Anthropic",
  openai: "OpenAI",
  "google-vertex": "Gemini"
};
function getProviderName(ownedBy) {
  return PROVIDER_NAMES[ownedBy] ?? ownedBy;
}
var PROVIDER_PREFIXES = ["anthropic/", "openai/", "google-vertex/"];
function formatGatewayModelName(model) {
  return formatModelId(model.id);
}
function formatModelId(modelId) {
  let cleanId = modelId;
  for (const prefix of PROVIDER_PREFIXES) {
    if (cleanId.startsWith(prefix)) {
      cleanId = cleanId.slice(prefix.length);
      break;
    }
  }
  cleanId = cleanId.replace(/(\d)-(\d)/g, "$1.$2");
  const words = cleanId.split(/[-_]/).map((word) => {
    if (word.match(/^[0-9.]+$/)) return word;
    return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
  });
  return words.join(" ");
}
export {
  BLOCKED_MODELS,
  DEFAULT_CODEX_MODEL,
  DEFAULT_GATEWAY_MODEL,
  fetchGatewayModels,
  fetchModelsList,
  formatGatewayModelName,
  formatModelId,
  getProviderName,
  isAnthropicModel,
  isOpenAIModel
};
//# sourceMappingURL=gateway-models.js.map