/**
 * 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 __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var aiSettings_exports = {};
__export(aiSettings_exports, {
  aiSettings: () => aiSettings,
  default: () => aiSettings_default
});
module.exports = __toCommonJS(aiSettings_exports);
var import_ai_feature_manager = require("../manager/ai-feature-manager");
const aiSettings = {
  name: "aiSettings",
  actions: {
    get: async (ctx, next) => {
      const settings = await ctx.db.getRepository("aiSettings").findOne();
      ctx.body = {
        ...settings == null ? void 0 : settings.options,
        defaultLLMService: settings == null ? void 0 : settings.defaultLLMService,
        defaultModel: settings == null ? void 0 : settings.defaultModel
      };
      await next();
    },
    update: async (ctx, next) => {
      const settings = await ctx.db.getRepository("aiSettings").findOne();
      const { defaultLLMService, defaultModel, ...restValues } = ctx.action.params.values || {};
      const options = settings.get("options");
      const newOptions = {
        ...options,
        ...restValues
      };
      const updateData = { options: newOptions };
      if (defaultLLMService !== void 0) {
        updateData.defaultLLMService = defaultLLMService;
      }
      if (defaultModel !== void 0) {
        updateData.defaultModel = defaultModel;
      }
      await settings.update(updateData);
      await next();
    },
    publicGet: async (ctx, next) => {
      const settings = await ctx.db.getRepository("aiSettings").findOne();
      const options = settings.options;
      ctx.body = {
        storage: options.storage
      };
      await next();
    },
    listStorages: async (ctx, next) => {
      const plugin = ctx.app.pm.get("file-manager");
      const storages = plugin.storagesCache.values();
      ctx.body = Array.from(storages).map((storage) => ({
        label: storage.title,
        value: storage.name
      }));
      await next();
    },
    isKnowledgeBaseEnabled: async (ctx, next) => {
      const plugin = ctx.app.pm.get("ai");
      const enabled = plugin.features.isFeaturesEnabled(Object.values(import_ai_feature_manager.EEFeatures));
      ctx.body = {
        enabled
      };
      await next();
    }
  }
};
var aiSettings_default = aiSettings;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  aiSettings
});
