"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var provider_config_exports = {};
__export(provider_config_exports, {
  PROVIDER_CONFIGS: () => PROVIDER_CONFIGS,
  getAvailableProviders: () => getAvailableProviders,
  getProviderByMenuIndex: () => getProviderByMenuIndex,
  getProviderConfig: () => getProviderConfig,
  getProviderLabel: () => getProviderLabel,
  hasAsyncValidation: () => hasAsyncValidation,
  runAsyncValidation: () => runAsyncValidation
});
module.exports = __toCommonJS(provider_config_exports);
var import_agent_management = require("@dexto/agent-management");
var import_llm = require("@dexto/llm");
var import_core = require("@dexto/core");
var import_types = require("./types.js");
var fs = __toESM(require("fs"), 1);
var os = __toESM(require("os"), 1);
var path = __toESM(require("path"), 1);
const API_KEY_STEP = {
  field: "apiKey",
  label: "API Key (optional)",
  placeholder: "Enter API key for authentication",
  required: false
};
const MAX_INPUT_TOKENS_STEP = {
  field: "maxInputTokens",
  label: "Max Input Tokens (optional)",
  placeholder: "e.g., 128000 (leave blank for default)",
  required: false,
  validate: import_types.validators.positiveNumber
};
const DISPLAY_NAME_STEP = {
  field: "displayName",
  label: "Display Name (optional)",
  placeholder: "e.g., My Custom Model",
  required: false
};
const REASONING_PRESET_STEP = {
  field: "reasoningPreset",
  label: "Reasoning Variant (optional)",
  placeholder: "e.g., low / medium / high / enabled / disabled (blank for provider default)",
  required: false,
  condition: (values) => {
    const modelName = values.name || "";
    return (0, import_llm.isReasoningCapableModel)(modelName);
  }
};
function parseReasoningVariant(value) {
  const normalized = value?.trim().toLowerCase();
  return normalized || void 0;
}
function resolveReasoningOverride(provider, modelName, rawVariant) {
  const variant = parseReasoningVariant(rawVariant);
  if (!variant) return void 0;
  const profile = (0, import_llm.getReasoningProfile)(provider, modelName);
  if (!profile.capable) return void 0;
  if (!(0, import_llm.supportsReasoningVariant)(profile, variant)) return void 0;
  if (variant === profile.defaultVariant) return void 0;
  return { variant };
}
const PROVIDER_CONFIGS = {
  "openai-compatible": {
    displayName: "OpenAI-Compatible",
    description: "Custom or self-hosted endpoints (vLLM, LM Studio)",
    steps: [
      {
        field: "name",
        label: "Model Name",
        placeholder: "e.g., llama-3-70b, mixtral-8x7b",
        required: true,
        validate: import_types.validators.required("Model name")
      },
      {
        field: "baseURL",
        label: "API Base URL",
        placeholder: "e.g., http://localhost:11434/v1",
        required: true,
        validate: import_types.validators.url
      },
      { ...DISPLAY_NAME_STEP, placeholder: "e.g., My Local Llama 3" },
      MAX_INPUT_TOKENS_STEP,
      REASONING_PRESET_STEP,
      API_KEY_STEP
    ],
    buildModel: (values, provider) => {
      const model = {
        name: values.name || "",
        provider
      };
      if (values.baseURL) {
        model.baseURL = values.baseURL;
      }
      if (values.displayName?.trim()) {
        model.displayName = values.displayName.trim();
      }
      if (values.maxInputTokens?.trim()) {
        model.maxInputTokens = parseInt(values.maxInputTokens, 10);
      }
      const reasoning = resolveReasoningOverride(
        provider,
        model.name,
        values.reasoningPreset
      );
      if (reasoning) model.reasoning = reasoning;
      return model;
    }
  },
  openrouter: {
    displayName: "OpenRouter",
    description: "100+ cloud models via unified API",
    steps: [
      {
        field: "name",
        label: "OpenRouter Model ID",
        placeholder: "e.g., anthropic/claude-3.5-sonnet, openai/gpt-4o",
        required: true,
        validate: import_types.validators.slashFormat
      },
      { ...DISPLAY_NAME_STEP, placeholder: "e.g., Claude 3.5 Sonnet" },
      MAX_INPUT_TOKENS_STEP,
      REASONING_PRESET_STEP,
      {
        ...API_KEY_STEP,
        placeholder: "Saved as OPENROUTER_API_KEY if not set, otherwise per-model"
      }
    ],
    buildModel: (values, provider) => {
      const model = {
        name: values.name || "",
        provider
      };
      if (values.displayName?.trim()) {
        model.displayName = values.displayName.trim();
      }
      if (values.maxInputTokens?.trim()) {
        model.maxInputTokens = parseInt(values.maxInputTokens, 10);
      }
      const reasoning = resolveReasoningOverride(
        provider,
        model.name,
        values.reasoningPreset
      );
      if (reasoning) model.reasoning = reasoning;
      return model;
    },
    asyncValidation: {
      field: "name",
      validate: async (modelId) => {
        let status = (0, import_core.lookupOpenRouterModel)(modelId);
        if (status === "unknown") {
          try {
            await (0, import_core.refreshOpenRouterModelCache)();
            status = (0, import_core.lookupOpenRouterModel)(modelId);
          } catch {
            return null;
          }
        }
        if (status === "invalid") {
          return `Model '${modelId}' not found in OpenRouter. Check the model ID at https://openrouter.ai/models`;
        }
        return null;
      }
    }
  },
  glama: {
    displayName: "Glama",
    description: "OpenAI-compatible gateway",
    steps: [
      {
        field: "name",
        label: "Glama Model ID",
        placeholder: "e.g., openai/gpt-4o, anthropic/claude-3-sonnet",
        required: true,
        validate: import_types.validators.slashFormat
      },
      { ...DISPLAY_NAME_STEP, placeholder: "e.g., GPT-4o via Glama" },
      REASONING_PRESET_STEP,
      {
        ...API_KEY_STEP,
        placeholder: "Saved as GLAMA_API_KEY if not set, otherwise per-model"
      }
    ],
    buildModel: (values, provider) => {
      const model = {
        name: values.name || "",
        provider
      };
      if (values.displayName?.trim()) {
        model.displayName = values.displayName.trim();
      }
      const reasoning = resolveReasoningOverride(
        provider,
        model.name,
        values.reasoningPreset
      );
      if (reasoning) model.reasoning = reasoning;
      return model;
    }
  },
  litellm: {
    displayName: "LiteLLM",
    description: "Unified proxy for 100+ providers",
    steps: [
      {
        field: "name",
        label: "Model Name",
        placeholder: "e.g., gpt-4, claude-3-sonnet, bedrock/anthropic.claude-v2",
        required: true,
        validate: import_types.validators.required("Model name")
      },
      {
        field: "baseURL",
        label: "LiteLLM Proxy URL",
        placeholder: "e.g., http://localhost:4000",
        required: true,
        validate: import_types.validators.url
      },
      { ...DISPLAY_NAME_STEP, placeholder: "e.g., My LiteLLM GPT-4" },
      MAX_INPUT_TOKENS_STEP,
      REASONING_PRESET_STEP,
      {
        ...API_KEY_STEP,
        placeholder: "Saved as LITELLM_API_KEY if not set, otherwise per-model"
      }
    ],
    buildModel: (values, provider) => {
      const model = {
        name: values.name || "",
        provider
      };
      if (values.baseURL) {
        model.baseURL = values.baseURL;
      }
      if (values.displayName?.trim()) {
        model.displayName = values.displayName.trim();
      }
      if (values.maxInputTokens?.trim()) {
        model.maxInputTokens = parseInt(values.maxInputTokens, 10);
      }
      const reasoning = resolveReasoningOverride(
        provider,
        model.name,
        values.reasoningPreset
      );
      if (reasoning) model.reasoning = reasoning;
      return model;
    }
  },
  bedrock: {
    displayName: "AWS Bedrock",
    description: "Custom model IDs via AWS credentials",
    steps: [
      {
        field: "name",
        label: "Bedrock Model ID",
        placeholder: "e.g., anthropic.claude-3-haiku-20240307-v1:0",
        required: true,
        validate: import_types.validators.required("Model ID")
      },
      { ...DISPLAY_NAME_STEP, placeholder: "e.g., Claude 3 Haiku" },
      {
        ...MAX_INPUT_TOKENS_STEP,
        placeholder: "e.g., 200000 (leave blank for default)"
      }
      // NO apiKey step - Bedrock uses AWS credentials from environment
    ],
    buildModel: (values, provider) => {
      const model = {
        name: values.name || "",
        provider
      };
      if (values.displayName?.trim()) {
        model.displayName = values.displayName.trim();
      }
      if (values.maxInputTokens?.trim()) {
        model.maxInputTokens = parseInt(values.maxInputTokens, 10);
      }
      return model;
    },
    setupInfo: {
      title: "AWS Bedrock Setup",
      description: "Bedrock uses AWS credentials from your environment. Ensure AWS_REGION and either AWS_BEARER_TOKEN_BEDROCK or IAM credentials are set.",
      docsUrl: "https://docs.dexto.ai/docs/guides/supported-llm-providers#amazon-bedrock"
    }
  },
  ollama: {
    displayName: "Ollama",
    description: "Local Ollama server models",
    steps: [
      {
        field: "name",
        label: "Model Name",
        placeholder: "e.g., llama3.3:70b, qwen3n:e2b",
        required: true,
        validate: import_types.validators.required("Model name")
      },
      {
        field: "baseURL",
        label: "Ollama Server URL (optional)",
        placeholder: "Default: http://localhost:11434",
        required: false,
        validate: (value) => {
          if (!value?.trim()) return null;
          return import_types.validators.url(value);
        }
      },
      { ...DISPLAY_NAME_STEP, placeholder: "e.g., Llama 3.3 70B" },
      MAX_INPUT_TOKENS_STEP
    ],
    buildModel: (values, provider) => {
      const model = {
        name: values.name || "",
        provider
      };
      if (values.baseURL?.trim()) {
        model.baseURL = values.baseURL.trim();
      }
      if (values.displayName?.trim()) {
        model.displayName = values.displayName.trim();
      }
      if (values.maxInputTokens?.trim()) {
        model.maxInputTokens = parseInt(values.maxInputTokens, 10);
      }
      return model;
    },
    setupInfo: {
      title: "Ollama Setup",
      description: "Add custom Ollama models by name. Ensure Ollama is running (default: http://localhost:11434). Pull models with: ollama pull <model>",
      docsUrl: "https://docs.dexto.ai/docs/guides/supported-llm-providers#ollama"
    }
  },
  local: {
    displayName: "Local (node-llama)",
    description: "Custom GGUF models via node-llama-cpp",
    steps: [
      {
        field: "name",
        label: "Model ID or Path",
        placeholder: "e.g., llama-3.3-8b-q4 or /path/to/model.gguf",
        required: true,
        validate: import_types.validators.required("Model ID or path")
      },
      { ...DISPLAY_NAME_STEP, placeholder: "e.g., My Custom Llama" }
      // Note: No MAX_INPUT_TOKENS_STEP - node-llama-cpp auto-detects context size
    ],
    buildModel: (values, provider) => {
      const model = {
        name: values.name || "",
        provider
      };
      if (values.displayName?.trim()) {
        model.displayName = values.displayName.trim();
      }
      return model;
    },
    asyncValidation: {
      field: "name",
      validate: async (value) => {
        const trimmed = value.trim();
        const isFilePath = trimmed.includes(path.sep) || trimmed.startsWith("/") || trimmed.startsWith("~") || trimmed.toLowerCase().endsWith(".gguf");
        if (isFilePath) {
          if (!trimmed.toLowerCase().endsWith(".gguf")) {
            return "File path must end with .gguf";
          }
          const expandedPath = trimmed.startsWith("~") ? trimmed.replace("~", os.homedir()) : trimmed;
          if (!fs.existsSync(expandedPath)) {
            return `File not found: ${trimmed}`;
          }
          return null;
        }
        const modelInfo = (0, import_core.getLocalModelById)(trimmed);
        if (!modelInfo) {
          return `Model ID '${trimmed}' not found in registry. Use a full file path to a .gguf file, or one of the registry IDs (e.g., llama-3.3-8b-q4, qwen-2.5-coder-7b-q4)`;
        }
        return null;
      }
    },
    setupInfo: {
      title: "Local Models Setup",
      description: "Add custom GGUF models by ID (from registry) or absolute file path. Ensure node-llama-cpp is installed and GPU acceleration is configured.",
      docsUrl: "https://docs.dexto.ai/docs/guides/supported-llm-providers#local-models"
    }
  },
  vertex: {
    displayName: "Google Vertex AI",
    description: "Custom Vertex model IDs",
    steps: [
      {
        field: "name",
        label: "Vertex Model ID",
        placeholder: "e.g., gemini-2.0-flash-exp, claude-4-5-sonnet@20250929",
        required: true,
        validate: import_types.validators.required("Model ID")
      },
      { ...DISPLAY_NAME_STEP, placeholder: "e.g., Gemini 2.0 Flash Exp" },
      {
        ...MAX_INPUT_TOKENS_STEP,
        placeholder: "e.g., 1048576 (leave blank for default)"
      }
    ],
    buildModel: (values, provider) => {
      const model = {
        name: values.name || "",
        provider
      };
      if (values.displayName?.trim()) {
        model.displayName = values.displayName.trim();
      }
      if (values.maxInputTokens?.trim()) {
        model.maxInputTokens = parseInt(values.maxInputTokens, 10);
      }
      return model;
    },
    setupInfo: {
      title: "Google Vertex AI Setup",
      description: "Vertex AI uses Google Cloud Application Default Credentials (ADC). Set GOOGLE_VERTEX_PROJECT and optionally GOOGLE_VERTEX_LOCATION. Run: gcloud auth application-default login",
      docsUrl: "https://docs.dexto.ai/docs/guides/supported-llm-providers#google-vertex-ai"
    }
  },
  "dexto-nova": {
    displayName: "Dexto Nova",
    description: "Access 100+ models with Nova credits",
    steps: [
      {
        field: "name",
        label: "Model ID (OpenRouter format)",
        placeholder: "e.g., anthropic/claude-sonnet-4.5, openai/gpt-5.2",
        required: true,
        validate: import_types.validators.slashFormat
      },
      { ...DISPLAY_NAME_STEP, placeholder: "e.g., Claude 4.5 Sonnet via Dexto" },
      MAX_INPUT_TOKENS_STEP,
      REASONING_PRESET_STEP
      // No API key step - Dexto uses OAuth login (DEXTO_API_KEY from auth.json)
    ],
    buildModel: (values, provider) => {
      const model = {
        name: values.name || "",
        provider
      };
      if (values.displayName?.trim()) {
        model.displayName = values.displayName.trim();
      }
      if (values.maxInputTokens?.trim()) {
        model.maxInputTokens = parseInt(values.maxInputTokens, 10);
      }
      const reasoning = resolveReasoningOverride(
        provider,
        model.name,
        values.reasoningPreset
      );
      if (reasoning) model.reasoning = reasoning;
      return model;
    },
    asyncValidation: {
      field: "name",
      validate: async (modelId) => {
        let status = (0, import_core.lookupOpenRouterModel)(modelId);
        if (status === "unknown") {
          try {
            await (0, import_core.refreshOpenRouterModelCache)();
            status = (0, import_core.lookupOpenRouterModel)(modelId);
          } catch {
            return null;
          }
        }
        if (status === "invalid") {
          return `Model '${modelId}' not found. Dexto Nova uses OpenRouter model IDs - check https://openrouter.ai/models`;
        }
        return null;
      }
    },
    setupInfo: {
      title: "Dexto Nova Setup",
      description: "Add OpenRouter-format models that use your Dexto Nova credits. Requires login: run `/login` first.",
      docsUrl: "https://openrouter.ai/models"
    }
  }
};
function getProviderConfig(provider) {
  return PROVIDER_CONFIGS[provider];
}
function getProviderLabel(provider) {
  const config = PROVIDER_CONFIGS[provider];
  return `${config.displayName} (${config.description})`;
}
function getAvailableProviders() {
  const dextoEnabled = (0, import_agent_management.isDextoAuthEnabled)();
  const providers = import_agent_management.CUSTOM_MODEL_PROVIDERS.filter(
    (provider) => provider !== "dexto-nova" || dextoEnabled
  );
  if (!dextoEnabled) {
    return providers;
  }
  const withoutDexto = providers.filter((p) => p !== "dexto-nova");
  return ["dexto-nova", ...withoutDexto];
}
function getProviderByMenuIndex(index) {
  const providers = getAvailableProviders();
  if (index < 0 || index >= providers.length) return void 0;
  return providers[index];
}
function hasAsyncValidation(provider) {
  return PROVIDER_CONFIGS[provider].asyncValidation !== void 0;
}
async function runAsyncValidation(provider, field, value) {
  const config = PROVIDER_CONFIGS[provider];
  if (config.asyncValidation && config.asyncValidation.field === field) {
    return config.asyncValidation.validate(value);
  }
  return null;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PROVIDER_CONFIGS,
  getAvailableProviders,
  getProviderByMenuIndex,
  getProviderConfig,
  getProviderLabel,
  hasAsyncValidation,
  runAsyncValidation
});
