/**
 * This file is part of the NocoBase (R) project.
 * Copyright (c) 2020-2024 NocoBase Co., Ltd.
 * Authors: NocoBase Team.
 *
 * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
 * For more information, please refer to: https://www.nocobase.com/agreement.
 */

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 ollama_exports = {};
__export(ollama_exports, {
  OllamaEmbeddingProvider: () => OllamaEmbeddingProvider,
  OllamaProvider: () => OllamaProvider,
  ollamaProviderOptions: () => ollamaProviderOptions
});
module.exports = __toCommonJS(ollama_exports);
var import_ollama = require("@langchain/ollama");
var import_provider = require("./provider");
var import_ai_manager = require("../manager/ai-manager");
var import_axios = __toESM(require("axios"));
const OLLAMA_DEFAULT_URL = "http://localhost:11434";
class OllamaProvider extends import_provider.LLMProvider {
  get baseURL() {
    return OLLAMA_DEFAULT_URL;
  }
  createModel() {
    const { baseURL } = this.serviceOptions || {};
    const { model, temperature, topP, topK, numPredict, ...rest } = this.modelOptions || {};
    return new import_ollama.ChatOllama({
      baseUrl: baseURL || this.baseURL,
      model: model || "mistral-nemo:12b",
      temperature,
      topP,
      topK,
      numPredict,
      ...rest,
      verbose: false,
      format: void 0,
      keepAlive: "5m"
    });
  }
  async listModels() {
    var _a;
    const options = this.serviceOptions || {};
    let baseURL = options.baseURL || this.baseURL;
    if (!baseURL) {
      return { code: 400, errMsg: "baseURL is required" };
    }
    if (baseURL && baseURL.endsWith("/")) {
      baseURL = baseURL.slice(0, -1);
    }
    try {
      const res = await import_axios.default.get(`${baseURL}/api/tags`);
      const models = ((_a = res == null ? void 0 : res.data) == null ? void 0 : _a.models) || [];
      return {
        models: models.map((model) => ({
          id: model.name
        }))
      };
    } catch (e) {
      return {
        code: 500,
        errMsg: `Failed to fetch Ollama models: ${e.message}. Make sure Ollama is running at ${baseURL}`
      };
    }
  }
}
class OllamaEmbeddingProvider extends import_provider.EmbeddingProvider {
  getDefaultUrl() {
    return OLLAMA_DEFAULT_URL;
  }
  createEmbedding() {
    return new import_ollama.OllamaEmbeddings({
      baseUrl: this.baseUrl,
      model: this.model
    });
  }
}
const ollamaProviderOptions = {
  title: "Ollama",
  supportedModel: [import_ai_manager.SupportedModel.LLM, import_ai_manager.SupportedModel.EMBEDDING],
  models: {
    [import_ai_manager.SupportedModel.LLM]: [],
    [import_ai_manager.SupportedModel.EMBEDDING]: []
  },
  provider: OllamaProvider,
  embedding: OllamaEmbeddingProvider
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  OllamaEmbeddingProvider,
  OllamaProvider,
  ollamaProviderOptions
});
