/**
 * 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 built_in_manager_exports = {};
__export(built_in_manager_exports, {
  BuiltInManager: () => BuiltInManager
});
module.exports = __toCommonJS(built_in_manager_exports);
var import_data_modeling = __toESM(require("../ai-employees/built-in/data-modeling"));
var import_ai_coding = __toESM(require("../ai-employees/built-in/ai-coding"));
var import_data_organizer = __toESM(require("../ai-employees/built-in/data-organizer"));
var import_insights_analyst = __toESM(require("../ai-employees/built-in/insights-analyst"));
var import_research_analyst = __toESM(require("../ai-employees/built-in/research-analyst"));
var import_translator = __toESM(require("../ai-employees/built-in/translator"));
var import_email_assistant = __toESM(require("../ai-employees/built-in/email-assistant"));
var import_data_visualization = __toESM(require("../ai-employees/built-in/data-visualization"));
var import_lodash = __toESM(require("lodash"));
const DEFAULT_LANGUAGE = "en-US";
const DEFAULT_KNOWLEDGE_BASE = {
  topK: 3,
  score: "0.6",
  knowledgeBaseKeys: []
};
const DEFAULT_KNOWLEDGE_BASE_PROMPT = "From knowledge base:\n{knowledgeBaseData}\nanswer user's question using this information.";
class BuiltInManager {
  constructor(plugin) {
    this.plugin = plugin;
  }
  builtInEmployees = [
    import_data_modeling.default,
    import_ai_coding.default,
    import_data_organizer.default,
    import_insights_analyst.default,
    import_research_analyst.default,
    import_translator.default,
    // nocobaseAssistant,
    import_email_assistant.default,
    import_data_visualization.default
  ];
  builtInEmployeeMap = Object.fromEntries(this.builtInEmployees.map((x) => [x.username, x]));
  setupBuiltInInfo(locale, aiEmployee) {
    var _a;
    if (!aiEmployee) {
      return;
    }
    const builtInEmployeeInfo = this.builtInEmployeeMap[aiEmployee.username];
    if (!builtInEmployeeInfo) {
      return;
    }
    const { profile } = builtInEmployeeInfo;
    const { avatar, nickname, position, bio, greeting, about } = profile[locale] ?? profile[DEFAULT_LANGUAGE];
    aiEmployee.avatar = avatar;
    aiEmployee.nickname = nickname;
    aiEmployee.position = position;
    aiEmployee.bio = bio;
    aiEmployee.greeting = greeting;
    aiEmployee.defaultPrompt = about;
    const builtInSkills = ((_a = builtInEmployeeInfo.skillSettings) == null ? void 0 : _a.skills) ?? [];
    const skillSettings = aiEmployee.skillSettings ?? {};
    const skills = skillSettings.skills ?? [];
    aiEmployee.skillSettings = {
      ...builtInEmployeeInfo.skillSettings ?? {},
      skills: import_lodash.default.uniqBy([...skills, ...builtInSkills], "name")
    };
  }
  async createOrUpdateAIEmployee(language = DEFAULT_LANGUAGE) {
    var _a;
    const aiEmployeesRepo = this.plugin.db.getRepository("aiEmployees");
    const employeeUsernames = this.builtInEmployees.map((employee) => employee.username);
    const existed = await aiEmployeesRepo.find({
      filter: {
        username: {
          $in: employeeUsernames
        }
      }
    });
    const existedUsername = existed.map((x) => x.username);
    const setups = this.builtInEmployees.filter((x) => !existedUsername.includes(x.username));
    if (setups.length) {
      this.plugin.log.info("setup built-in employees");
      for (const employee of setups) {
        const { username, description, profile, skillSettings } = employee;
        let p = profile[language];
        if (!p) {
          p = profile[DEFAULT_LANGUAGE];
        }
        if (!p) {
          continue;
        }
        const { nickname, avatar, position, bio, greeting } = p;
        await aiEmployeesRepo.create({
          values: {
            username,
            nickname,
            position,
            avatar,
            bio,
            greeting,
            about: null,
            skillSettings,
            enableKnowledgeBase: false,
            knowledgeBase: DEFAULT_KNOWLEDGE_BASE,
            knowledgeBasePrompt: DEFAULT_KNOWLEDGE_BASE_PROMPT,
            enabled: true,
            builtIn: true,
            category: (employee == null ? void 0 : employee.category) ?? "business"
          }
        });
        this.plugin.log.info(`setup [${username}] ${description}`);
      }
    }
    const updates = this.builtInEmployees.filter((x) => existedUsername.includes(x.username));
    if (updates.length) {
      this.plugin.log.info("update built-in employees");
      const existedMap = new Map(existed.map((it) => [it.username, it.toJSON()]));
      for (const { username, description, skillSettings } of updates) {
        let { skills } = ((_a = existedMap.get(username)) == null ? void 0 : _a.skillSettings) ?? { skills: [] };
        skills = (skills ?? []).filter((s) => {
          var _a2;
          return (_a2 = s.name) == null ? void 0 : _a2.startsWith("workflowCaller-");
        });
        const mergedSkills = /* @__PURE__ */ new Set([...skills, ...skillSettings.skills]);
        await aiEmployeesRepo.update({
          values: {
            skillSettings: {
              skills: [...mergedSkills]
            },
            builtIn: true
          },
          filter: {
            username
          }
        });
        this.plugin.log.info(`update [${username}] ${description}`);
      }
    }
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BuiltInManager
});
