// Force strict mode and setup for ESM
"use strict";
import {
  resolveModelConfig,
  resolveProviderProtocol,
  stripRuntimeSnapshotPrefix
} from "./chunk-KN5EBZLT.js";
import {
  external_exports
} from "./chunk-H6XPXXMH.js";
import {
  AuthType,
  MODEL_GENERATION_CONFIG_FIELDS
} from "./chunk-D5K5YBSN.js";
import {
  REASONING_EFFORT_TIERS,
  normalizeReasoningEffort
} from "./chunk-XVNQMZ2I.js";
import {
  init_esbuild_shims
} from "./chunk-5O2XNYP6.js";
import {
  __name
} from "./chunk-J2S4EL5Y.js";

// packages/cli/src/utils/acpModelUtils.ts
init_esbuild_shims();
function formatAcpModelId(modelId, authType) {
  return `${modelId}(${authType})`;
}
__name(formatAcpModelId, "formatAcpModelId");
function sanitizeProviderBaseUrl(baseUrl) {
  const scheme = baseUrl.match(/^[A-Za-z][A-Za-z\d+.-]*:\/\//);
  if (!scheme) {
    return baseUrl;
  }
  const authorityStart = scheme[0].length;
  const stripAt = /* @__PURE__ */ __name((at) => `${baseUrl.slice(0, authorityStart)}${baseUrl.slice(at + 1)}`, "stripAt");
  const authorityEnd = findAuthorityEnd(baseUrl, authorityStart);
  const authorityAt = baseUrl.slice(authorityStart, authorityEnd).lastIndexOf("@");
  const authorityAtIndex = authorityAt === -1 ? -1 : authorityStart + authorityAt;
  try {
    const parsed = new URL(baseUrl);
    if (parsed.username || parsed.password) {
      return authorityAtIndex >= authorityStart ? stripAt(authorityAtIndex) : baseUrl;
    }
    return baseUrl;
  } catch {
    if (authorityAtIndex >= authorityStart) {
      return stripAt(authorityAtIndex);
    }
    const fallbackAt = findUnescapedUserInfoFallbackAt(
      baseUrl,
      authorityStart,
      authorityEnd
    );
    return fallbackAt === -1 ? baseUrl : stripAt(fallbackAt);
  }
}
__name(sanitizeProviderBaseUrl, "sanitizeProviderBaseUrl");
function findUnescapedUserInfoFallbackAt(baseUrl, authorityStart, authorityEnd) {
  const at = baseUrl.lastIndexOf("@");
  if (at < authorityStart || authorityEnd >= at) {
    return -1;
  }
  const colon = baseUrl.indexOf(":", authorityStart);
  if (colon === -1 || colon > authorityEnd) {
    return -1;
  }
  const portCandidate = baseUrl.slice(colon + 1, authorityEnd);
  return /^\d+$/.test(portCandidate) ? -1 : at;
}
__name(findUnescapedUserInfoFallbackAt, "findUnescapedUserInfoFallbackAt");
function findAuthorityEnd(baseUrl, authorityStart) {
  const slash = baseUrl.indexOf("/", authorityStart);
  const query = baseUrl.indexOf("?", authorityStart);
  const hash = baseUrl.indexOf("#", authorityStart);
  let end = baseUrl.length;
  if (slash !== -1) end = Math.min(end, slash);
  if (query !== -1) end = Math.min(end, query);
  if (hash !== -1) end = Math.min(end, hash);
  return end;
}
__name(findAuthorityEnd, "findAuthorityEnd");
function parseAcpBaseModelId(value) {
  const trimmed = value.trim();
  const closeIdx = trimmed.lastIndexOf(")");
  const openIdx = trimmed.lastIndexOf("(");
  if (openIdx >= 0 && closeIdx === trimmed.length - 1 && openIdx < closeIdx) {
    return trimmed.slice(0, openIdx);
  }
  return trimmed;
}
__name(parseAcpBaseModelId, "parseAcpBaseModelId");
function parseAcpModelOption(input) {
  const trimmed = input.trim();
  const closeIdx = trimmed.lastIndexOf(")");
  const openIdx = trimmed.lastIndexOf("(");
  if (openIdx >= 0 && closeIdx === trimmed.length - 1 && openIdx < closeIdx) {
    const maybeModelId = trimmed.slice(0, openIdx);
    const maybeAuthType = trimmed.slice(openIdx + 1, closeIdx);
    const parsedAuthType = external_exports.nativeEnum(AuthType).safeParse(maybeAuthType);
    if (parsedAuthType.success) {
      return { modelId: maybeModelId, authType: parsedAuthType.data };
    }
  }
  return { modelId: trimmed };
}
__name(parseAcpModelOption, "parseAcpModelOption");
function isInlineModelOverrideAllowed(config, modelId) {
  const contentGeneratorConfig = config.getContentGeneratorConfig();
  const authType = contentGeneratorConfig?.authType;
  if (!authType) {
    return false;
  }
  const activeBaseUrl = contentGeneratorConfig.baseUrl;
  const activeEnvKey = contentGeneratorConfig.apiKeyEnvKey;
  return config.getAvailableModelsForAuthType(authType).filter((m) => !m.fastOnly && !m.voiceOnly).some(
    (m) => m.id === modelId && (m.baseUrl ?? void 0) === (activeBaseUrl ?? void 0) && (m.envKey ?? void 0) === (activeEnvKey ?? void 0)
  );
}
__name(isInlineModelOverrideAllowed, "isInlineModelOverrideAllowed");

// packages/cli/src/utils/modelConfigUtils.ts
init_esbuild_shims();
var AUTH_ENV_MODEL_VARS = {
  ["openai" /* USE_OPENAI */]: ["OPENAI_MODEL", "QWEN_MODEL"],
  ["gemini" /* USE_GEMINI */]: ["GEMINI_MODEL"],
  ["vertex-ai" /* USE_VERTEX_AI */]: ["GOOGLE_MODEL"],
  ["anthropic" /* USE_ANTHROPIC */]: ["ANTHROPIC_MODEL"],
  ["qwen-oauth" /* QWEN_OAUTH */]: []
};
function collectProviderModelsForProtocol(modelProviders, providerProtocol, protocol) {
  if (!modelProviders) {
    return [];
  }
  const out = [];
  for (const [providerId, models] of Object.entries(modelProviders)) {
    if (!Array.isArray(models)) {
      continue;
    }
    if (resolveProviderProtocol(providerId, providerProtocol) === protocol) {
      out.push(...models);
    }
  }
  return out;
}
__name(collectProviderModelsForProtocol, "collectProviderModelsForProtocol");
function findProviderIdForModel(modelProviders, providerProtocol, protocol, modelProvider) {
  if (!modelProviders || !modelProvider) {
    return void 0;
  }
  for (const [providerId, models] of Object.entries(modelProviders)) {
    if (!Array.isArray(models)) {
      continue;
    }
    if (resolveProviderProtocol(providerId, providerProtocol) !== protocol) {
      continue;
    }
    if (models.includes(modelProvider)) {
      return providerId;
    }
  }
  return void 0;
}
__name(findProviderIdForModel, "findProviderIdForModel");
function buildSkippedProviderWarnings(modelProviders, providerProtocol) {
  if (!modelProviders) {
    return [];
  }
  const warnings = [];
  const known = Object.values(AuthType).join(", ");
  for (const [providerId, models] of Object.entries(modelProviders)) {
    if (!Array.isArray(models) || models.length === 0) {
      continue;
    }
    const protocol = resolveProviderProtocol(providerId, providerProtocol);
    if (protocol === "qwen-oauth" /* QWEN_OAUTH */ && providerId !== "qwen-oauth" /* QWEN_OAUTH */) {
      warnings.push(
        `Warning: modelProviders provider "${providerId}" maps to "qwen-oauth" via providerProtocol, but qwen-oauth uses hard-coded models only; its ${models.length} model(s) are ignored.`
      );
      continue;
    }
    if (protocol !== void 0) {
      continue;
    }
    const mapped = providerProtocol && Object.hasOwn(providerProtocol, providerId) ? providerProtocol[providerId] : void 0;
    warnings.push(
      mapped !== void 0 ? `Warning: providerProtocol["${providerId}"] = "${mapped}" is not a known protocol (expected one of: ${known}); its ${models.length} model(s) are ignored.` : `Warning: modelProviders provider "${providerId}" is not a built-in protocol (${known}) and has no providerProtocol mapping; its ${models.length} model(s) are ignored. Add providerProtocol["${providerId}"] (e.g. "openai") to enable them.`
    );
  }
  return warnings;
}
__name(buildSkippedProviderWarnings, "buildSkippedProviderWarnings");
function getIgnoredTopLevelGenerationConfigFields(settingsGenerationConfig, modelProvider) {
  if (!settingsGenerationConfig || !modelProvider) {
    return [];
  }
  const providerGenerationConfig = modelProvider.generationConfig ?? {};
  return MODEL_GENERATION_CONFIG_FIELDS.filter(
    (field) => Object.hasOwn(settingsGenerationConfig, field) && !Object.hasOwn(providerGenerationConfig, field)
  );
}
__name(getIgnoredTopLevelGenerationConfigFields, "getIgnoredTopLevelGenerationConfigFields");
function buildIgnoredTopLevelGenerationConfigWarning(providerId, modelProvider, ignoredFields) {
  if (ignoredFields.length === 0) {
    return void 0;
  }
  const fieldList = ignoredFields.map((field) => `model.generationConfig.${field}`).join(", ");
  const isSingular = ignoredFields.length === 1;
  const verb = isSingular ? "is" : "are";
  const fieldReference = isSingular ? "this field" : "these fields";
  const pronoun = isSingular ? "it" : "them";
  return `Warning: ${fieldList} ${verb} ignored for provider model "${modelProvider.id}" from modelProviders.${providerId}. Move ${fieldReference} to modelProviders.${providerId}[].generationConfig for that model if you want ${pronoun} to apply.`;
}
__name(buildIgnoredTopLevelGenerationConfigWarning, "buildIgnoredTopLevelGenerationConfigWarning");
function getAuthTypeFromEnv(env = process.env) {
  if (env["QWEN_OAUTH"]) {
    return "qwen-oauth" /* QWEN_OAUTH */;
  }
  if (env["OPENAI_API_KEY"] && (env["OPENAI_MODEL"] || env["QWEN_MODEL"]) && env["OPENAI_BASE_URL"]) {
    return "openai" /* USE_OPENAI */;
  }
  if (env["GEMINI_API_KEY"] && env["GEMINI_MODEL"]) {
    return "gemini" /* USE_GEMINI */;
  }
  if (env["GOOGLE_API_KEY"] && env["GOOGLE_MODEL"]) {
    return "vertex-ai" /* USE_VERTEX_AI */;
  }
  if (env["ANTHROPIC_API_KEY"] && env["ANTHROPIC_MODEL"] && env["ANTHROPIC_BASE_URL"]) {
    return "anthropic" /* USE_ANTHROPIC */;
  }
  return void 0;
}
__name(getAuthTypeFromEnv, "getAuthTypeFromEnv");
function resolveCliGenerationConfig(inputs) {
  const { argv, settings, selectedAuthType } = inputs;
  const env = inputs.env ?? process.env;
  const authType = selectedAuthType;
  let resolvedModel;
  let sourceEnvVar;
  let resolvedFromSettings = false;
  if (argv.model) {
    resolvedModel = argv.model;
  } else if (settings.model?.name) {
    resolvedModel = stripRuntimeSnapshotPrefix(settings.model.name);
    resolvedFromSettings = true;
  } else if (authType && AUTH_ENV_MODEL_VARS[authType]) {
    for (const envVar of AUTH_ENV_MODEL_VARS[authType]) {
      if (env[envVar]) {
        resolvedModel = env[envVar];
        sourceEnvVar = envVar;
        break;
      }
    }
  }
  let modelProvider;
  let disambiguationWarning;
  if (resolvedModel && authType && settings.modelProviders) {
    const providers = collectProviderModelsForProtocol(
      settings.modelProviders,
      settings.providerProtocol,
      authType
    );
    if (providers.length > 0) {
      const persistedBaseUrl = settings.model?.baseUrl;
      if (resolvedFromSettings && persistedBaseUrl) {
        const exactMatch = providers.find(
          (p) => p.id === resolvedModel && p.baseUrl === persistedBaseUrl
        );
        modelProvider = exactMatch ?? providers.find((p) => p.id === resolvedModel);
        if (!exactMatch && modelProvider) {
          const fallbackBaseUrl = modelProvider.baseUrl === void 0 ? "(default baseUrl)" : sanitizeProviderBaseUrl(modelProvider.baseUrl);
          disambiguationWarning = `Persisted model.baseUrl '${sanitizeProviderBaseUrl(persistedBaseUrl)}' no longer matches any provider for model '${resolvedModel}' (authType '${authType}'); using the first id match ('${fallbackBaseUrl}'). Re-select the model to update it.`;
        }
      } else {
        modelProvider = providers.find((p) => p.id === resolvedModel);
      }
    }
  }
  const allModelEnvVars = Object.values(AUTH_ENV_MODEL_VARS).flat();
  const filteredEnv = { ...env };
  if (sourceEnvVar) {
    for (const envVar of allModelEnvVars) {
      if (envVar !== sourceEnvVar) {
        delete filteredEnv[envVar];
      }
    }
  } else {
    for (const envVar of allModelEnvVars) {
      delete filteredEnv[envVar];
    }
  }
  const configSources = {
    authType,
    cli: {
      model: argv.model,
      apiKey: argv.openaiApiKey,
      baseUrl: argv.openaiBaseUrl
    },
    settings: {
      model: settings.model?.name ? stripRuntimeSnapshotPrefix(settings.model.name) : void 0,
      apiKey: settings.security?.auth?.apiKey,
      baseUrl: settings.security?.auth?.baseUrl,
      generationConfig: settings.model?.generationConfig
    },
    modelProvider,
    env: filteredEnv
  };
  const resolved = resolveModelConfig(configSources);
  const ignoredGenerationConfigWarning = authType && modelProvider ? buildIgnoredTopLevelGenerationConfigWarning(
    findProviderIdForModel(
      settings.modelProviders,
      settings.providerProtocol,
      authType,
      modelProvider
    ) ?? authType,
    modelProvider,
    getIgnoredTopLevelGenerationConfigFields(
      settings.model?.generationConfig,
      modelProvider
    )
  ) : void 0;
  const enableOpenAILogging = (typeof argv.openaiLogging === "undefined" ? settings.model?.enableOpenAILogging : argv.openaiLogging) ?? false;
  const openAILoggingDir = argv.openaiLoggingDir || settings.model?.openAILoggingDir;
  const generationConfig = {
    ...resolved.config,
    enableOpenAILogging,
    openAILoggingDir
  };
  const rawReasoningEffort = settings.model?.reasoningEffort;
  const reasoningEffort = normalizeReasoningEffort(rawReasoningEffort);
  const invalidReasoningEffortWarning = rawReasoningEffort && !reasoningEffort ? `Ignoring invalid model.reasoningEffort "${rawReasoningEffort}"; expected one of: ${REASONING_EFFORT_TIERS.join(", ")}.` : void 0;
  if (reasoningEffort && generationConfig.reasoning !== false) {
    generationConfig.reasoning = {
      ...generationConfig.reasoning ?? {},
      effort: reasoningEffort
    };
  }
  return {
    model: resolved.config.model || "",
    apiKey: resolved.config.apiKey || "",
    baseUrl: resolved.config.baseUrl || "",
    generationConfig,
    sources: resolved.sources,
    warnings: [
      ...resolved.warnings,
      ...invalidReasoningEffortWarning ? [invalidReasoningEffortWarning] : [],
      ...disambiguationWarning ? [disambiguationWarning] : [],
      ...ignoredGenerationConfigWarning ? [ignoredGenerationConfigWarning] : [],
      ...buildSkippedProviderWarnings(
        settings.modelProviders,
        settings.providerProtocol
      )
    ]
  };
}
__name(resolveCliGenerationConfig, "resolveCliGenerationConfig");

export {
  formatAcpModelId,
  sanitizeProviderBaseUrl,
  parseAcpBaseModelId,
  parseAcpModelOption,
  isInlineModelOverrideAllowed,
  collectProviderModelsForProtocol,
  getAuthTypeFromEnv,
  resolveCliGenerationConfig
};
/**
 * @license
 * Copyright 2025 Qwen Team
 * SPDX-License-Identifier: Apache-2.0
 */
