@nocobase/ai 2.1.0-alpha.2 → 2.1.0-alpha.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/LICENSE +201 -661
  2. package/README.md +79 -10
  3. package/lib/ai-employee-manager/index.d.ts +29 -0
  4. package/lib/ai-employee-manager/index.js +167 -0
  5. package/lib/ai-employee-manager/types.d.ts +56 -0
  6. package/lib/ai-employee-manager/types.js +24 -0
  7. package/lib/ai-manager.d.ts +8 -0
  8. package/lib/ai-manager.js +12 -0
  9. package/lib/index.d.ts +4 -0
  10. package/lib/index.js +8 -0
  11. package/lib/loader/employee.d.ts +36 -0
  12. package/lib/loader/employee.js +207 -0
  13. package/lib/loader/index.d.ts +3 -0
  14. package/lib/loader/index.js +7 -1
  15. package/lib/loader/mcp.d.ts +34 -0
  16. package/lib/loader/mcp.js +108 -0
  17. package/lib/loader/skills.d.ts +42 -0
  18. package/lib/loader/skills.js +138 -0
  19. package/lib/mcp-manager/index.d.ts +43 -0
  20. package/lib/mcp-manager/index.js +341 -0
  21. package/lib/mcp-manager/types.d.ts +61 -0
  22. package/lib/mcp-manager/types.js +24 -0
  23. package/lib/mcp-tools-manager.d.ts +43 -0
  24. package/lib/mcp-tools-manager.js +77 -0
  25. package/lib/skills-manager/index.d.ts +29 -0
  26. package/lib/skills-manager/index.js +169 -0
  27. package/lib/skills-manager/types.d.ts +33 -0
  28. package/lib/skills-manager/types.js +24 -0
  29. package/lib/tools-manager/index.js +3 -0
  30. package/lib/tools-manager/types.d.ts +7 -1
  31. package/package.json +25 -7
  32. package/src/__tests__/ai-employees.test.ts +108 -0
  33. package/src/__tests__/mcp.test.ts +105 -0
  34. package/src/__tests__/resource/ai/ai-employees/index-employee/index.ts +16 -0
  35. package/src/__tests__/resource/ai/ai-employees/index-employee/prompt.md +1 -0
  36. package/src/__tests__/resource/ai/ai-employees/named-file-employee.ts +16 -0
  37. package/src/__tests__/resource/ai/ai-employees/with-skills/index.ts +16 -0
  38. package/src/__tests__/resource/ai/ai-employees/with-skills/skills/analysis/SKILLS.md +6 -0
  39. package/src/__tests__/resource/ai/ai-employees/with-skills-merge/index.ts +17 -0
  40. package/src/__tests__/resource/ai/ai-employees/with-skills-merge/skills/discovered-skill/SKILLS.md +6 -0
  41. package/src/__tests__/resource/ai/ai-employees/with-tools/index.ts +16 -0
  42. package/src/__tests__/resource/ai/ai-employees/with-tools/tools/discoveredTool.ts +23 -0
  43. package/src/__tests__/resource/ai/ai-employees/with-tools-merge/index.ts +16 -0
  44. package/src/__tests__/resource/ai/ai-employees/with-tools-merge/tools/discoveredTool.ts +23 -0
  45. package/src/__tests__/resource/ai/mcp/weather.ts +25 -0
  46. package/src/__tests__/resource/ai/skills/data-modeling/SKILLS.md +24 -0
  47. package/src/__tests__/resource/ai/skills/data-modeling/tools/read.ts +23 -0
  48. package/src/__tests__/resource/ai/skills/data-modeling/tools/search/description.md +1 -0
  49. package/src/__tests__/resource/ai/skills/data-modeling/tools/search/index.ts +23 -0
  50. package/src/__tests__/resource/ai/skills/document/tools/read.ts +1 -1
  51. package/src/__tests__/resource/ai/skills/document/tools/search/index.ts +1 -1
  52. package/src/__tests__/resource/ai/tools/desc/index.ts +1 -1
  53. package/src/__tests__/resource/ai/tools/group/group1.ts +1 -1
  54. package/src/__tests__/resource/ai/tools/group/group2.ts +1 -1
  55. package/src/__tests__/resource/ai/tools/group/group3/index.ts +1 -1
  56. package/src/__tests__/resource/ai/tools/hallow/index.ts +1 -1
  57. package/src/__tests__/resource/ai/tools/print.ts +1 -1
  58. package/src/__tests__/skills.test.ts +55 -0
  59. package/src/__tests__/tools.test.ts +5 -3
  60. package/src/ai-employee-manager/index.ts +148 -0
  61. package/src/ai-employee-manager/types.ts +63 -0
  62. package/src/ai-manager.ts +12 -0
  63. package/src/index.ts +4 -0
  64. package/src/loader/employee.ts +194 -0
  65. package/src/loader/index.ts +3 -0
  66. package/src/loader/mcp.ts +101 -0
  67. package/src/loader/skills.ts +129 -0
  68. package/src/mcp-manager/index.ts +364 -0
  69. package/src/mcp-manager/types.ts +68 -0
  70. package/src/mcp-tools-manager.ts +90 -0
  71. package/src/skills-manager/index.ts +148 -0
  72. package/src/skills-manager/types.ts +38 -0
  73. package/src/tools-manager/index.ts +3 -0
  74. package/src/tools-manager/types.ts +8 -1
@@ -0,0 +1,207 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __create = Object.create;
11
+ var __defProp = Object.defineProperty;
12
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
+ var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
17
+ var __export = (target, all) => {
18
+ for (var name in all)
19
+ __defProp(target, name, { get: all[name], enumerable: true });
20
+ };
21
+ var __copyProps = (to, from, except, desc) => {
22
+ if (from && typeof from === "object" || typeof from === "function") {
23
+ for (let key of __getOwnPropNames(from))
24
+ if (!__hasOwnProp.call(to, key) && key !== except)
25
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
26
+ }
27
+ return to;
28
+ };
29
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
30
+ // If the importer is in node compatibility mode or this is not an ESM
31
+ // file that has been converted to a CommonJS file using a Babel-
32
+ // compatible transform (i.e. "__esModule" has not been set), then set
33
+ // "default" to the CommonJS "module.exports" for node compatibility.
34
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
35
+ mod
36
+ ));
37
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
+ var employee_exports = {};
39
+ __export(employee_exports, {
40
+ AIEmployeeLoader: () => AIEmployeeLoader,
41
+ normalizeAIEmployeeToolSetting: () => normalizeAIEmployeeToolSetting
42
+ });
43
+ module.exports = __toCommonJS(employee_exports);
44
+ var import_utils = require("@nocobase/utils");
45
+ var import_scanner = require("./scanner");
46
+ var import_fs = require("fs");
47
+ var import_promises = require("fs/promises");
48
+ var import_types = require("./types");
49
+ var import_path = __toESM(require("path"));
50
+ const _AIEmployeeLoader = class _AIEmployeeLoader extends import_types.LoadAndRegister {
51
+ constructor(ai, options) {
52
+ super(ai, options);
53
+ this.ai = ai;
54
+ this.options = options;
55
+ this.log = options.log;
56
+ this.scanner = new import_scanner.DirectoryScanner(this.options.scan);
57
+ }
58
+ scanner;
59
+ files = [];
60
+ employeeDescriptors = [];
61
+ log;
62
+ async scan() {
63
+ this.files = await this.scanner.scan();
64
+ }
65
+ async import() {
66
+ if (!this.files.length) {
67
+ return;
68
+ }
69
+ const grouped = /* @__PURE__ */ new Map();
70
+ for (const fd of this.files) {
71
+ const employeeRoot = getEmployeeRoot(fd);
72
+ const group = grouped.get(employeeRoot) ?? [];
73
+ group.push(fd);
74
+ grouped.set(employeeRoot, group);
75
+ }
76
+ const descriptors = await Promise.all(
77
+ Array.from(grouped.entries()).map(async ([employeeRoot, fds]) => {
78
+ var _a, _b, _c;
79
+ const file = fds.find((fd) => fd.extname === ".ts" || fd.extname === ".js");
80
+ if (!file || !(0, import_fs.existsSync)(file.path)) {
81
+ return null;
82
+ }
83
+ const promptFile = fds.find((fd) => fd.basename === "prompt.md");
84
+ const name = import_path.default.basename(employeeRoot);
85
+ try {
86
+ const imported = await (0, import_utils.importModule)(file.path);
87
+ const mod = (imported == null ? void 0 : imported.default) ?? imported;
88
+ const employeeOptions = typeof mod === "function" ? mod() : mod;
89
+ if (!employeeOptions || !employeeOptions.username) {
90
+ (_a = this.log) == null ? void 0 : _a.warn(`ai employee [${name}] register ignored: invalid definition at ${file.path}`);
91
+ return null;
92
+ }
93
+ const { skills = [], tools = [] } = employeeOptions;
94
+ if (promptFile && (0, import_fs.existsSync)(promptFile.path)) {
95
+ try {
96
+ employeeOptions.systemPrompt = await (0, import_promises.readFile)(promptFile.path, "utf-8");
97
+ } catch (e) {
98
+ (_b = this.log) == null ? void 0 : _b.error(
99
+ `ai employee [${name}] load fail: error occur when reading prompt.md at ${promptFile.path}`,
100
+ e
101
+ );
102
+ return null;
103
+ }
104
+ }
105
+ return {
106
+ name,
107
+ employeeRoot,
108
+ file,
109
+ options: {
110
+ ...employeeOptions,
111
+ skills: [...skills, ...await discoverSkills(employeeRoot)],
112
+ tools: [...tools, ...await discoverTools(employeeRoot)]
113
+ }
114
+ };
115
+ } catch (e) {
116
+ (_c = this.log) == null ? void 0 : _c.error(`ai employee [${name}] load fail: error occur when import ${file.path}`, e);
117
+ return null;
118
+ }
119
+ })
120
+ );
121
+ this.employeeDescriptors = descriptors.filter((item) => Boolean(item));
122
+ }
123
+ async register() {
124
+ if (!this.employeeDescriptors.length) {
125
+ return;
126
+ }
127
+ const { employeeManager } = this.ai;
128
+ for (const descriptor of this.employeeDescriptors) {
129
+ await employeeManager.registerEmployee(descriptor.options);
130
+ }
131
+ }
132
+ };
133
+ __name(_AIEmployeeLoader, "AIEmployeeLoader");
134
+ let AIEmployeeLoader = _AIEmployeeLoader;
135
+ function getEmployeeRoot(fd) {
136
+ if (fd.basename === "index.ts" || fd.basename === "index.js" || fd.basename === "prompt.md") {
137
+ return import_path.default.dirname(fd.path);
138
+ }
139
+ return fd.path;
140
+ }
141
+ __name(getEmployeeRoot, "getEmployeeRoot");
142
+ async function discoverSkills(employeeRoot) {
143
+ const skillsDir = import_path.default.join(employeeRoot, "skills");
144
+ if (!(0, import_fs.existsSync)(skillsDir)) {
145
+ return [];
146
+ }
147
+ const scanner = new import_scanner.DirectoryScanner({
148
+ basePath: employeeRoot,
149
+ pattern: ["skills/**/SKILLS.md"]
150
+ });
151
+ const skillFiles = await scanner.scan();
152
+ return uniq(skillFiles.map((it) => it.directory).filter(Boolean));
153
+ }
154
+ __name(discoverSkills, "discoverSkills");
155
+ async function discoverTools(employeeRoot) {
156
+ const toolsDir = import_path.default.join(employeeRoot, "tools");
157
+ if (!(0, import_fs.existsSync)(toolsDir)) {
158
+ return [];
159
+ }
160
+ const scanner = new import_scanner.DirectoryScanner({
161
+ basePath: employeeRoot,
162
+ pattern: ["tools/**/*.ts", "tools/**/*.js", "!tools/**/*.d.ts"]
163
+ });
164
+ const toolFiles = await scanner.scan();
165
+ return uniqBy(
166
+ toolFiles.map(
167
+ (it) => normalizeAIEmployeeToolSetting(
168
+ it.basename === "index.ts" || it.basename === "index.js" ? it.directory : it.name
169
+ )
170
+ ).filter((tool) => Boolean(tool == null ? void 0 : tool.name)),
171
+ "name"
172
+ );
173
+ }
174
+ __name(discoverTools, "discoverTools");
175
+ function uniq(values) {
176
+ return [...new Set(values)];
177
+ }
178
+ __name(uniq, "uniq");
179
+ function uniqBy(values, key) {
180
+ const valueMap = /* @__PURE__ */ new Map();
181
+ for (const value of values) {
182
+ valueMap.set(value[key], value);
183
+ }
184
+ return [...valueMap.values()];
185
+ }
186
+ __name(uniqBy, "uniqBy");
187
+ function normalizeAIEmployeeToolSetting(tool) {
188
+ if (!tool) {
189
+ return;
190
+ }
191
+ if (typeof tool === "string") {
192
+ return { name: tool };
193
+ }
194
+ if (!tool.name) {
195
+ return;
196
+ }
197
+ return {
198
+ name: tool.name,
199
+ ...tool.autoCall != null ? { autoCall: tool.autoCall } : {}
200
+ };
201
+ }
202
+ __name(normalizeAIEmployeeToolSetting, "normalizeAIEmployeeToolSetting");
203
+ // Annotate the CommonJS export names for ESM import in node:
204
+ 0 && (module.exports = {
205
+ AIEmployeeLoader,
206
+ normalizeAIEmployeeToolSetting
207
+ });
@@ -9,3 +9,6 @@
9
9
  export * from './types';
10
10
  export * from './scanner';
11
11
  export * from './tools';
12
+ export * from './skills';
13
+ export * from './mcp';
14
+ export * from './employee';
@@ -26,9 +26,15 @@ module.exports = __toCommonJS(loader_exports);
26
26
  __reExport(loader_exports, require("./types"), module.exports);
27
27
  __reExport(loader_exports, require("./scanner"), module.exports);
28
28
  __reExport(loader_exports, require("./tools"), module.exports);
29
+ __reExport(loader_exports, require("./skills"), module.exports);
30
+ __reExport(loader_exports, require("./mcp"), module.exports);
31
+ __reExport(loader_exports, require("./employee"), module.exports);
29
32
  // Annotate the CommonJS export names for ESM import in node:
30
33
  0 && (module.exports = {
31
34
  ...require("./types"),
32
35
  ...require("./scanner"),
33
- ...require("./tools")
36
+ ...require("./tools"),
37
+ ...require("./skills"),
38
+ ...require("./mcp"),
39
+ ...require("./employee")
34
40
  });
@@ -0,0 +1,34 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { Logger } from '@nocobase/logger';
10
+ import { AIManager } from '../ai-manager';
11
+ import { MCPOptions } from '../mcp-manager';
12
+ import { LoadAndRegister } from './types';
13
+ import { DirectoryScanner, DirectoryScannerOptions, FileDescriptor } from './scanner';
14
+ export type MCPLoaderOptions = {
15
+ scan: DirectoryScannerOptions;
16
+ log?: Logger;
17
+ };
18
+ export declare class MCPLoader extends LoadAndRegister<MCPLoaderOptions> {
19
+ protected readonly ai: AIManager;
20
+ protected readonly options: MCPLoaderOptions;
21
+ protected readonly scanner: DirectoryScanner;
22
+ protected files: FileDescriptor[];
23
+ protected mcpDescriptors: MCPDescriptor[];
24
+ protected log: Logger;
25
+ constructor(ai: AIManager, options: MCPLoaderOptions);
26
+ protected scan(): Promise<void>;
27
+ protected import(): Promise<void>;
28
+ protected register(): Promise<void>;
29
+ }
30
+ export type MCPDescriptor = {
31
+ name: string;
32
+ file: FileDescriptor;
33
+ options: MCPOptions;
34
+ };
@@ -0,0 +1,108 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
+ var __export = (target, all) => {
16
+ for (var name in all)
17
+ __defProp(target, name, { get: all[name], enumerable: true });
18
+ };
19
+ var __copyProps = (to, from, except, desc) => {
20
+ if (from && typeof from === "object" || typeof from === "function") {
21
+ for (let key of __getOwnPropNames(from))
22
+ if (!__hasOwnProp.call(to, key) && key !== except)
23
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
24
+ }
25
+ return to;
26
+ };
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var mcp_exports = {};
29
+ __export(mcp_exports, {
30
+ MCPLoader: () => MCPLoader
31
+ });
32
+ module.exports = __toCommonJS(mcp_exports);
33
+ var import_utils = require("@nocobase/utils");
34
+ var import_fs = require("fs");
35
+ var import_types = require("./types");
36
+ var import_scanner = require("./scanner");
37
+ var import_utils2 = require("./utils");
38
+ const _MCPLoader = class _MCPLoader extends import_types.LoadAndRegister {
39
+ constructor(ai, options) {
40
+ super(ai, options);
41
+ this.ai = ai;
42
+ this.options = options;
43
+ this.log = options.log;
44
+ this.scanner = new import_scanner.DirectoryScanner(this.options.scan);
45
+ }
46
+ scanner;
47
+ files = [];
48
+ mcpDescriptors = [];
49
+ log;
50
+ async scan() {
51
+ this.files = await this.scanner.scan();
52
+ }
53
+ async import() {
54
+ if (!this.files.length) {
55
+ return;
56
+ }
57
+ const descriptors = await Promise.all(
58
+ this.files.map(async (file) => {
59
+ var _a, _b, _c;
60
+ const name = file.name;
61
+ if (!(0, import_fs.existsSync)(file.path)) {
62
+ (_a = this.log) == null ? void 0 : _a.error(`mcp [${name}] ignored: can not find definition file at ${file.path}`);
63
+ return null;
64
+ }
65
+ try {
66
+ const imported = await (0, import_utils.importModule)(file.path);
67
+ const mod = (imported == null ? void 0 : imported.default) ?? imported;
68
+ const options = typeof mod === "function" ? mod() : mod;
69
+ if (!(0, import_utils2.isNonEmptyObject)(options)) {
70
+ (_b = this.log) == null ? void 0 : _b.warn(`mcp [${name}] register ignored: invalid definition at ${file.path}`);
71
+ return null;
72
+ }
73
+ return {
74
+ name,
75
+ file,
76
+ options
77
+ };
78
+ } catch (e) {
79
+ (_c = this.log) == null ? void 0 : _c.error(`mcp [${name}] load fail: error occur when import ${file.path}`, e);
80
+ return null;
81
+ }
82
+ })
83
+ );
84
+ this.mcpDescriptors = descriptors.filter((item) => Boolean(item));
85
+ }
86
+ async register() {
87
+ var _a;
88
+ if (!this.mcpDescriptors.length) {
89
+ return;
90
+ }
91
+ const { mcpManager } = this.ai;
92
+ for (const descriptor of this.mcpDescriptors) {
93
+ try {
94
+ await mcpManager.registerMCP({
95
+ [descriptor.name]: descriptor.options
96
+ });
97
+ } catch (e) {
98
+ (_a = this.log) == null ? void 0 : _a.error(`mcp [${descriptor.name}] register ignored: error occur when invoke registerMCP`, e);
99
+ }
100
+ }
101
+ }
102
+ };
103
+ __name(_MCPLoader, "MCPLoader");
104
+ let MCPLoader = _MCPLoader;
105
+ // Annotate the CommonJS export names for ESM import in node:
106
+ 0 && (module.exports = {
107
+ MCPLoader
108
+ });
@@ -0,0 +1,42 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { DirectoryScanner, DirectoryScannerOptions, FileDescriptor } from './scanner';
10
+ import { AIManager } from '../ai-manager';
11
+ import { LoadAndRegister } from './types';
12
+ import { Logger } from '@nocobase/logger';
13
+ import { SkillsScope } from '../skills-manager';
14
+ export type SkillsLoaderOptions = {
15
+ scan: DirectoryScannerOptions;
16
+ log?: Logger;
17
+ };
18
+ export declare class SkillsLoader extends LoadAndRegister<SkillsLoaderOptions> {
19
+ protected readonly ai: AIManager;
20
+ protected readonly options: SkillsLoaderOptions;
21
+ protected readonly scanner: DirectoryScanner;
22
+ protected files: FileDescriptor[];
23
+ protected skillsDescriptors: SkillsDescriptor[];
24
+ protected log: Logger;
25
+ constructor(ai: AIManager, options: SkillsLoaderOptions);
26
+ protected scan(): Promise<void>;
27
+ protected import(): Promise<void>;
28
+ protected register(): Promise<void>;
29
+ }
30
+ export type SkillsDescriptor = {
31
+ scope: SkillsScope;
32
+ name: string;
33
+ description: string;
34
+ content: string;
35
+ skillsFile: FileDescriptor;
36
+ skillsDir: FileDescriptor;
37
+ tools?: string[];
38
+ introduction?: {
39
+ title: string;
40
+ about?: string;
41
+ };
42
+ };
@@ -0,0 +1,138 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __create = Object.create;
11
+ var __defProp = Object.defineProperty;
12
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
+ var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
17
+ var __export = (target, all) => {
18
+ for (var name in all)
19
+ __defProp(target, name, { get: all[name], enumerable: true });
20
+ };
21
+ var __copyProps = (to, from, except, desc) => {
22
+ if (from && typeof from === "object" || typeof from === "function") {
23
+ for (let key of __getOwnPropNames(from))
24
+ if (!__hasOwnProp.call(to, key) && key !== except)
25
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
26
+ }
27
+ return to;
28
+ };
29
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
30
+ // If the importer is in node compatibility mode or this is not an ESM
31
+ // file that has been converted to a CommonJS file using a Babel-
32
+ // compatible transform (i.e. "__esModule" has not been set), then set
33
+ // "default" to the CommonJS "module.exports" for node compatibility.
34
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
35
+ mod
36
+ ));
37
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
+ var skills_exports = {};
39
+ __export(skills_exports, {
40
+ SkillsLoader: () => SkillsLoader
41
+ });
42
+ module.exports = __toCommonJS(skills_exports);
43
+ var import_scanner = require("./scanner");
44
+ var import_promises = require("fs/promises");
45
+ var import_fs = require("fs");
46
+ var import_types = require("./types");
47
+ var import_gray_matter = __toESM(require("gray-matter"));
48
+ var import_path = __toESM(require("path"));
49
+ const _SkillsLoader = class _SkillsLoader extends import_types.LoadAndRegister {
50
+ constructor(ai, options) {
51
+ super(ai, options);
52
+ this.ai = ai;
53
+ this.options = options;
54
+ this.log = options.log;
55
+ this.scanner = new import_scanner.DirectoryScanner(this.options.scan);
56
+ }
57
+ scanner;
58
+ files = [];
59
+ skillsDescriptors = [];
60
+ log;
61
+ async scan() {
62
+ this.files = await this.scanner.scan();
63
+ }
64
+ async import() {
65
+ if (!this.files.length) {
66
+ return;
67
+ }
68
+ this.skillsDescriptors = await Promise.all(
69
+ this.files.map(async (skillsFile) => {
70
+ var _a, _b, _c;
71
+ if (skillsFile.basename !== "SKILLS.md") {
72
+ return null;
73
+ }
74
+ if (!(0, import_fs.existsSync)(skillsFile.path)) {
75
+ (_a = this.log) == null ? void 0 : _a.error(`skills [${skillsFile.directory}] ignored: can not find SKILLS.md at ${skillsFile.path}`);
76
+ return null;
77
+ }
78
+ const skillsDir = new import_scanner.FileDescriptor(import_path.default.dirname(skillsFile.path));
79
+ const name = skillsFile.directory;
80
+ const entry = { name, skillsFile, skillsDir };
81
+ try {
82
+ const skills = await (0, import_promises.readFile)(skillsFile.path, "utf-8");
83
+ const { data, content } = (0, import_gray_matter.default)(skills);
84
+ entry.scope = data["scope"] ?? "SPECIFIED";
85
+ entry.name = data["name"];
86
+ entry.description = data["description"];
87
+ entry.content = content;
88
+ entry.introduction = data["introduction"];
89
+ entry.tools = data["tools"] ?? [];
90
+ } catch (e) {
91
+ (_b = this.log) == null ? void 0 : _b.error(`skills [${name}] load fail: error occur when reading SKILLS.md at ${skillsFile.path}`, e);
92
+ return null;
93
+ }
94
+ try {
95
+ const toolsScanner = new import_scanner.DirectoryScanner({
96
+ basePath: skillsDir.path,
97
+ pattern: ["tools/**/*.ts", "tools/**/*.js", "!tools/**/*.d.ts"]
98
+ });
99
+ const toolsFiles = await toolsScanner.scan();
100
+ entry.tools = Array.from(
101
+ /* @__PURE__ */ new Set([
102
+ ...entry.tools,
103
+ ...toolsFiles.map(
104
+ (it) => it.basename === "index.ts" || it.basename === "index.js" ? it.directory : it.name
105
+ )
106
+ ])
107
+ );
108
+ } catch (e) {
109
+ (_c = this.log) == null ? void 0 : _c.error(`skills [${name}] load fail: error occur when loading tools at ${skillsDir.path}`, e);
110
+ return null;
111
+ }
112
+ return entry;
113
+ }).filter((it) => it != null)
114
+ );
115
+ }
116
+ async register() {
117
+ if (!this.skillsDescriptors.length) {
118
+ return;
119
+ }
120
+ const { skillsManager } = this.ai;
121
+ for (const descriptor of this.skillsDescriptors) {
122
+ await skillsManager.registerSkills({
123
+ scope: descriptor.scope,
124
+ name: descriptor.name,
125
+ description: descriptor.description,
126
+ content: descriptor.content,
127
+ tools: descriptor.tools,
128
+ introduction: descriptor.introduction
129
+ });
130
+ }
131
+ }
132
+ };
133
+ __name(_SkillsLoader, "SkillsLoader");
134
+ let SkillsLoader = _SkillsLoader;
135
+ // Annotate the CommonJS export names for ESM import in node:
136
+ 0 && (module.exports = {
137
+ SkillsLoader
138
+ });
@@ -0,0 +1,43 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { MultiServerMCPClient } from '@langchain/mcp-adapters';
10
+ import { MCPEntry, MCPFilter, MCPManager, MCPOptions, MCPTestResult, MCPToolEntry } from './types';
11
+ import type { DynamicToolsProvider, Permission } from '../tools-manager/types';
12
+ export declare class DefaultMCPManager implements MCPManager {
13
+ private readonly app;
14
+ private readonly mcpRegistry;
15
+ private readonly provideCollectionManager;
16
+ private mode;
17
+ private client;
18
+ private toolsMap;
19
+ private toolsPermissionMap;
20
+ constructor(app: any);
21
+ init(): Promise<void>;
22
+ registerMCP(registration: {
23
+ [key: string | symbol]: MCPOptions;
24
+ }): Promise<void>;
25
+ getMCP(name: string): Promise<MCPEntry>;
26
+ listMCP(filter?: MCPFilter): Promise<MCPEntry[]>;
27
+ rebuildClient(): Promise<void>;
28
+ getClient(): MultiServerMCPClient | null;
29
+ getMCPToolsProvider(): DynamicToolsProvider;
30
+ listMCPTools(): Promise<Record<string, MCPToolEntry[]>>;
31
+ updateMCPToolPermission(toolName: string, permission: Permission): Promise<void>;
32
+ testConnection(options: MCPOptions): Promise<MCPTestResult>;
33
+ private buildMCPConnection;
34
+ persistence(): Promise<void>;
35
+ private persistenceEntry;
36
+ private normalizeEntry;
37
+ private get aiMcpClientsCollection();
38
+ private get aiMcpClientsModel();
39
+ private get sequelize();
40
+ private get collectionManager();
41
+ }
42
+ export declare function defineMCP(options: MCPOptions): MCPOptions;
43
+ export * from './types';