"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 loadModels_exports = {};
__export(loadModels_exports, {
  searchModels: () => searchModels
});
module.exports = __toCommonJS(loadModels_exports);
var import_openai = __toESM(require("openai"));
var import_modelFiltering = require("../../../vendors/OpenAi/helpers/modelFiltering");
var import_httpProxyAgent = require("../../../../utils/httpProxyAgent");
var import_di = require("@n8n/di");
var import_config = require("@n8n/config");
async function searchModels(filter) {
  const credentials = await this.getCredentials("openAiApi");
  const baseURL = this.getNodeParameter("options.baseURL", "") || credentials.url || "https://api.openai.com/v1";
  const { openAiDefaultHeaders: defaultHeaders } = import_di.Container.get(import_config.AiConfig);
  const openai = new import_openai.default({
    baseURL,
    apiKey: credentials.apiKey,
    fetchOptions: {
      dispatcher: (0, import_httpProxyAgent.getProxyAgent)(baseURL)
    },
    defaultHeaders
  });
  const { data: models = [] } = await openai.models.list();
  const url = baseURL && new URL(baseURL);
  const isCustomAPI = !!(url && !["api.openai.com", "ai-assistant.n8n.io"].includes(url.hostname));
  const filteredModels = models.filter((model) => {
    const includeModel = (0, import_modelFiltering.shouldIncludeModel)(model.id, isCustomAPI);
    if (!filter) return includeModel;
    return includeModel && model.id.toLowerCase().includes(filter.toLowerCase());
  });
  filteredModels.sort((a, b) => a.id.localeCompare(b.id));
  return {
    results: filteredModels.map((model) => ({
      name: model.id,
      value: model.id
    }))
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  searchModels
});
//# sourceMappingURL=loadModels.js.map