@nocobase/ai 2.1.0-beta.14 → 2.1.0-beta.16

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 (87) hide show
  1. package/lib/ai-employee-manager/index.d.ts +29 -0
  2. package/lib/ai-employee-manager/index.js +167 -0
  3. package/lib/ai-employee-manager/types.d.ts +56 -0
  4. package/lib/ai-employee-manager/types.js +24 -0
  5. package/lib/ai-manager.d.ts +8 -0
  6. package/lib/ai-manager.js +12 -0
  7. package/lib/document-loader/index.d.ts +10 -0
  8. package/lib/document-loader/index.js +90 -0
  9. package/lib/document-loader/loader.worker.d.ts +9 -0
  10. package/lib/document-loader/loader.worker.js +83 -0
  11. package/lib/document-loader/vendor/langchain/document_loaders/fs/text.d.ts +20 -0
  12. package/lib/document-loader/vendor/langchain/document_loaders/fs/text.js +99 -0
  13. package/lib/document-loader/xlsx.d.ts +10 -0
  14. package/lib/document-loader/xlsx.js +100 -0
  15. package/lib/index.d.ts +5 -0
  16. package/lib/index.js +11 -1
  17. package/lib/loader/employee.d.ts +37 -0
  18. package/lib/loader/employee.js +207 -0
  19. package/lib/loader/index.d.ts +3 -0
  20. package/lib/loader/index.js +7 -1
  21. package/lib/loader/mcp.d.ts +35 -0
  22. package/lib/loader/mcp.js +108 -0
  23. package/lib/loader/skills.d.ts +43 -0
  24. package/lib/loader/skills.js +138 -0
  25. package/lib/loader/tools.d.ts +1 -0
  26. package/lib/loader/tools.js +4 -3
  27. package/lib/mcp-manager/index.d.ts +43 -0
  28. package/lib/mcp-manager/index.js +341 -0
  29. package/lib/mcp-manager/types.d.ts +61 -0
  30. package/lib/mcp-manager/types.js +24 -0
  31. package/lib/mcp-tools-manager.d.ts +43 -0
  32. package/lib/mcp-tools-manager.js +77 -0
  33. package/lib/skills-manager/index.d.ts +29 -0
  34. package/lib/skills-manager/index.js +169 -0
  35. package/lib/skills-manager/types.d.ts +33 -0
  36. package/lib/skills-manager/types.js +24 -0
  37. package/lib/tools-manager/index.d.ts +2 -1
  38. package/lib/tools-manager/index.js +17 -7
  39. package/lib/tools-manager/types.d.ts +12 -4
  40. package/package.json +11 -6
  41. package/src/__tests__/ai-employees.test.ts +108 -0
  42. package/src/__tests__/mcp.test.ts +105 -0
  43. package/src/__tests__/resource/ai/ai-employees/index-employee/index.ts +16 -0
  44. package/src/__tests__/resource/ai/ai-employees/index-employee/prompt.md +1 -0
  45. package/src/__tests__/resource/ai/ai-employees/named-file-employee.ts +16 -0
  46. package/src/__tests__/resource/ai/ai-employees/with-skills/index.ts +16 -0
  47. package/src/__tests__/resource/ai/ai-employees/with-skills/skills/analysis/SKILLS.md +6 -0
  48. package/src/__tests__/resource/ai/ai-employees/with-skills-merge/index.ts +17 -0
  49. package/src/__tests__/resource/ai/ai-employees/with-skills-merge/skills/discovered-skill/SKILLS.md +6 -0
  50. package/src/__tests__/resource/ai/ai-employees/with-tools/index.ts +16 -0
  51. package/src/__tests__/resource/ai/ai-employees/with-tools/tools/discoveredTool.ts +23 -0
  52. package/src/__tests__/resource/ai/ai-employees/with-tools-merge/index.ts +16 -0
  53. package/src/__tests__/resource/ai/ai-employees/with-tools-merge/tools/discoveredTool.ts +23 -0
  54. package/src/__tests__/resource/ai/mcp/weather.ts +25 -0
  55. package/src/__tests__/resource/ai/skills/data-modeling/SKILLS.md +24 -0
  56. package/src/__tests__/resource/ai/skills/data-modeling/tools/read.ts +23 -0
  57. package/src/__tests__/resource/ai/skills/data-modeling/tools/search/description.md +1 -0
  58. package/src/__tests__/resource/ai/skills/data-modeling/tools/search/index.ts +23 -0
  59. package/src/__tests__/resource/ai/skills/document/tools/read.ts +1 -1
  60. package/src/__tests__/resource/ai/skills/document/tools/search/index.ts +1 -1
  61. package/src/__tests__/resource/ai/tools/desc/index.ts +1 -1
  62. package/src/__tests__/resource/ai/tools/group/group1.ts +1 -1
  63. package/src/__tests__/resource/ai/tools/group/group2.ts +1 -1
  64. package/src/__tests__/resource/ai/tools/group/group3/index.ts +1 -1
  65. package/src/__tests__/resource/ai/tools/hallow/index.ts +1 -1
  66. package/src/__tests__/resource/ai/tools/print.ts +1 -1
  67. package/src/__tests__/skills.test.ts +55 -0
  68. package/src/ai-employee-manager/index.ts +148 -0
  69. package/src/ai-employee-manager/types.ts +63 -0
  70. package/src/ai-manager.ts +12 -0
  71. package/src/document-loader/index.ts +57 -0
  72. package/src/document-loader/loader.worker.ts +100 -0
  73. package/src/document-loader/vendor/langchain/document_loaders/fs/text.ts +72 -0
  74. package/src/document-loader/xlsx.ts +82 -0
  75. package/src/index.ts +5 -0
  76. package/src/loader/employee.ts +194 -0
  77. package/src/loader/index.ts +3 -0
  78. package/src/loader/mcp.ts +101 -0
  79. package/src/loader/skills.ts +129 -0
  80. package/src/loader/tools.ts +3 -2
  81. package/src/mcp-manager/index.ts +364 -0
  82. package/src/mcp-manager/types.ts +68 -0
  83. package/src/mcp-tools-manager.ts +90 -0
  84. package/src/skills-manager/index.ts +148 -0
  85. package/src/skills-manager/types.ts +38 -0
  86. package/src/tools-manager/index.ts +18 -7
  87. package/src/tools-manager/types.ts +13 -4
@@ -0,0 +1,77 @@
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_tools_manager_exports = {};
29
+ __export(mcp_tools_manager_exports, {
30
+ McpToolsManager: () => McpToolsManager
31
+ });
32
+ module.exports = __toCommonJS(mcp_tools_manager_exports);
33
+ var import_utils = require("@nocobase/utils");
34
+ const _McpToolsManager = class _McpToolsManager {
35
+ tools = new import_utils.Registry();
36
+ resultPostProcessors = /* @__PURE__ */ new Map();
37
+ getActionKey(resourceName, actionName) {
38
+ return `${resourceName}:${actionName}`;
39
+ }
40
+ registerTools(tools) {
41
+ for (const tool of tools) {
42
+ this.tools.register(tool.name, tool);
43
+ }
44
+ }
45
+ registerToolResultPostProcessor(resourceName, actionName, processor) {
46
+ const key = this.getActionKey(resourceName, actionName);
47
+ const processors = this.resultPostProcessors.get(key) || [];
48
+ processors.push(processor);
49
+ this.resultPostProcessors.set(key, processors);
50
+ }
51
+ async postProcessToolResult(tool, result, context) {
52
+ if (!tool.resourceName || !tool.actionName) {
53
+ return result;
54
+ }
55
+ const processors = this.resultPostProcessors.get(this.getActionKey(tool.resourceName, tool.actionName)) || [];
56
+ let current = result;
57
+ for (const processor of processors) {
58
+ current = await processor(current, {
59
+ ...context,
60
+ tool
61
+ });
62
+ }
63
+ return current;
64
+ }
65
+ listTools() {
66
+ return [...this.tools.getValues()];
67
+ }
68
+ getTool(name) {
69
+ return this.tools.get(name);
70
+ }
71
+ };
72
+ __name(_McpToolsManager, "McpToolsManager");
73
+ let McpToolsManager = _McpToolsManager;
74
+ // Annotate the CommonJS export names for ESM import in node:
75
+ 0 && (module.exports = {
76
+ McpToolsManager
77
+ });
@@ -0,0 +1,29 @@
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 { SkillsEntry, SkillsManager, SkillsOptions, SkillsFilter } from './types';
10
+ export declare class DefaultSkillsManager implements SkillsManager {
11
+ private readonly app;
12
+ private readonly skills;
13
+ private readonly provideCollectionManager;
14
+ private mode;
15
+ constructor(app: any);
16
+ init(): Promise<void>;
17
+ getSkills(name: string[]): Promise<SkillsEntry[]>;
18
+ getSkills(name: string): Promise<SkillsEntry>;
19
+ listSkills(filter: SkillsFilter): Promise<SkillsEntry[]>;
20
+ registerSkills(options: SkillsOptions): Promise<void>;
21
+ persistence(): Promise<void>;
22
+ private registerSkillsInMemory;
23
+ registerSkillsInDatabase(options: SkillsOptions): Promise<void>;
24
+ private get aiSkillsCollection();
25
+ private get aiSkillsModel();
26
+ private get sequelize();
27
+ private get collectionManager();
28
+ }
29
+ export * from './types';
@@ -0,0 +1,169 @@
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 __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
30
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
31
+ // If the importer is in node compatibility mode or this is not an ESM
32
+ // file that has been converted to a CommonJS file using a Babel-
33
+ // compatible transform (i.e. "__esModule" has not been set), then set
34
+ // "default" to the CommonJS "module.exports" for node compatibility.
35
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
36
+ mod
37
+ ));
38
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
39
+ var skills_manager_exports = {};
40
+ __export(skills_manager_exports, {
41
+ DefaultSkillsManager: () => DefaultSkillsManager
42
+ });
43
+ module.exports = __toCommonJS(skills_manager_exports);
44
+ var import_database = require("@nocobase/database");
45
+ var import_utils = require("@nocobase/utils");
46
+ var import_lodash = __toESM(require("lodash"));
47
+ __reExport(skills_manager_exports, require("./types"), module.exports);
48
+ const _DefaultSkillsManager = class _DefaultSkillsManager {
49
+ constructor(app) {
50
+ this.app = app;
51
+ this.provideCollectionManager = () => app.mainDataSource;
52
+ }
53
+ skills = new import_utils.Registry();
54
+ provideCollectionManager;
55
+ mode = "memory";
56
+ async init() {
57
+ if (this.mode === "memory") {
58
+ await this.persistence();
59
+ this.mode = "database";
60
+ }
61
+ }
62
+ async getSkills(name) {
63
+ var _a;
64
+ if (import_lodash.default.isArray(name)) {
65
+ return (await this.aiSkillsModel.findAll({ where: { name: { [import_database.Op.in]: name } } })).map((it) => it.toJSON()).map(converterSkillsEntry);
66
+ } else {
67
+ return converterSkillsEntry((_a = await this.aiSkillsModel.findOne({ where: { name } })) == null ? void 0 : _a.toJSON());
68
+ }
69
+ }
70
+ async listSkills(filter) {
71
+ const where = {};
72
+ if (filter == null ? void 0 : filter.scope) {
73
+ where["scope"] = filter.scope;
74
+ }
75
+ if (filter == null ? void 0 : filter.name) {
76
+ where["name"] = {
77
+ [import_database.Op.substring]: filter.name
78
+ };
79
+ }
80
+ return (await this.aiSkillsModel.findAll({ where })).map((it) => it.toJSON()).map(converterSkillsEntry);
81
+ }
82
+ async registerSkills(options) {
83
+ if (this.mode === "memory") {
84
+ return this.registerSkillsInMemory(options);
85
+ }
86
+ return this.registerSkillsInDatabase(options);
87
+ }
88
+ async persistence() {
89
+ const skillsList = [...this.skills.getValues()];
90
+ for (const skill of skillsList) {
91
+ await this.registerSkillsInDatabase(skill);
92
+ }
93
+ }
94
+ registerSkillsInMemory(options) {
95
+ const skillsEntry = { ...options };
96
+ this.skills.register(options.name, skillsEntry);
97
+ }
98
+ async registerSkillsInDatabase(options) {
99
+ var _a, _b;
100
+ const title = (_a = options.introduction) == null ? void 0 : _a.title;
101
+ const about = (_b = options.introduction) == null ? void 0 : _b.about;
102
+ const from = options.from || "loader";
103
+ await this.sequelize.transaction(async (transaction) => {
104
+ const existed = await this.aiSkillsModel.findOne({ where: { name: options.name }, transaction });
105
+ if (existed) {
106
+ await this.aiSkillsModel.update(
107
+ {
108
+ scope: options.scope,
109
+ description: options.description,
110
+ content: options.content,
111
+ tools: options.tools,
112
+ title,
113
+ about,
114
+ from
115
+ },
116
+ {
117
+ where: { name: options.name },
118
+ transaction
119
+ }
120
+ );
121
+ } else {
122
+ await this.aiSkillsModel.create(
123
+ {
124
+ name: options.name,
125
+ scope: options.scope,
126
+ description: options.description,
127
+ content: options.content,
128
+ tools: options.tools,
129
+ title,
130
+ about,
131
+ from
132
+ },
133
+ {
134
+ transaction
135
+ }
136
+ );
137
+ }
138
+ });
139
+ }
140
+ get aiSkillsCollection() {
141
+ return this.collectionManager.getCollection("aiSkills");
142
+ }
143
+ get aiSkillsModel() {
144
+ var _a;
145
+ return (_a = this.aiSkillsCollection) == null ? void 0 : _a.model;
146
+ }
147
+ get sequelize() {
148
+ return this.collectionManager.db.sequelize;
149
+ }
150
+ get collectionManager() {
151
+ return this.provideCollectionManager().collectionManager;
152
+ }
153
+ };
154
+ __name(_DefaultSkillsManager, "DefaultSkillsManager");
155
+ let DefaultSkillsManager = _DefaultSkillsManager;
156
+ const converterSkillsEntry = /* @__PURE__ */ __name((model) => {
157
+ return {
158
+ ...model ?? {},
159
+ introduction: (model == null ? void 0 : model.title) ? {
160
+ title: model.title,
161
+ about: model == null ? void 0 : model.about
162
+ } : void 0
163
+ };
164
+ }, "converterSkillsEntry");
165
+ // Annotate the CommonJS export names for ESM import in node:
166
+ 0 && (module.exports = {
167
+ DefaultSkillsManager,
168
+ ...require("./types")
169
+ });
@@ -0,0 +1,33 @@
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
+ export interface SkillsManager {
10
+ init(): Promise<void>;
11
+ getSkills(name: string[]): Promise<SkillsEntry[]>;
12
+ getSkills(name: string): Promise<SkillsEntry>;
13
+ listSkills(filter?: SkillsFilter): Promise<SkillsEntry[]>;
14
+ registerSkills(options: SkillsOptions): Promise<void>;
15
+ }
16
+ export type SkillsOptions = {
17
+ scope: SkillsScope;
18
+ name: string;
19
+ description: string;
20
+ content: string;
21
+ tools?: string[];
22
+ introduction?: {
23
+ title: string;
24
+ about?: string;
25
+ };
26
+ from?: string;
27
+ };
28
+ export type SkillsEntry = SkillsOptions;
29
+ export type SkillsFilter = {
30
+ scope?: SkillsScope;
31
+ name?: string;
32
+ };
33
+ export type SkillsScope = 'SPECIFIED' | 'GENERAL' | 'CUSTOM';
@@ -0,0 +1,24 @@
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 __copyProps = (to, from, except, desc) => {
15
+ if (from && typeof from === "object" || typeof from === "function") {
16
+ for (let key of __getOwnPropNames(from))
17
+ if (!__hasOwnProp.call(to, key) && key !== except)
18
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
19
+ }
20
+ return to;
21
+ };
22
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
+ var types_exports = {};
24
+ module.exports = __toCommonJS(types_exports);
@@ -12,8 +12,9 @@ export declare class DefaultToolsManager implements ToolsManager {
12
12
  private readonly tools;
13
13
  private readonly dynamicTools;
14
14
  constructor(tools?: Registry<ToolsOptions>, dynamicTools?: DynamicToolsProvider[]);
15
- getTools(toolName: string): Promise<ToolsEntry>;
15
+ getTools(toolName: string, filter?: ToolsFilter): Promise<ToolsEntry>;
16
16
  listTools(filter?: ToolsFilter): Promise<ToolsEntry[]>;
17
+ isToolsExisted(toolName: string): boolean;
17
18
  registerTools(options: ToolsOptions | ToolsOptions[]): void;
18
19
  registerDynamicTools(provider: DynamicToolsProvider): void;
19
20
  private getToolsList;
@@ -50,16 +50,16 @@ const _DefaultToolsManager = class _DefaultToolsManager {
50
50
  this.tools = tools;
51
51
  this.dynamicTools = dynamicTools;
52
52
  }
53
- async getTools(toolName) {
53
+ async getTools(toolName, filter) {
54
54
  const target = this.tools.get(toolName);
55
55
  if (target) {
56
56
  return target;
57
57
  }
58
- const dynamicTools = await this.syncDynamicTools();
58
+ const dynamicTools = await this.syncDynamicTools(filter);
59
59
  return dynamicTools.find((x) => x.definition.name === toolName);
60
60
  }
61
61
  async listTools(filter) {
62
- const toolsList = await this.getToolsList();
62
+ const toolsList = await this.getToolsList(filter);
63
63
  return toolsList.filter((x) => {
64
64
  if (!filter) {
65
65
  return true;
@@ -77,10 +77,20 @@ const _DefaultToolsManager = class _DefaultToolsManager {
77
77
  return result;
78
78
  });
79
79
  }
80
+ isToolsExisted(toolName) {
81
+ const target = this.tools.get(toolName);
82
+ if (target) {
83
+ return true;
84
+ }
85
+ return false;
86
+ }
80
87
  registerTools(options) {
81
88
  const list = import_lodash.default.isArray(options) ? options : [options];
82
89
  for (const item of list) {
83
90
  const toolsEntry = { ...item };
91
+ if (!toolsEntry.from) {
92
+ toolsEntry.from = "loader";
93
+ }
84
94
  if (!toolsEntry.execution) {
85
95
  toolsEntry.execution = "backend";
86
96
  }
@@ -99,17 +109,17 @@ const _DefaultToolsManager = class _DefaultToolsManager {
99
109
  registerDynamicTools(provider) {
100
110
  this.dynamicTools.push(provider);
101
111
  }
102
- async getToolsList() {
103
- const dynamicTools = await this.syncDynamicTools();
112
+ async getToolsList(filter) {
113
+ const dynamicTools = await this.syncDynamicTools(filter);
104
114
  return [...this.tools.getValues(), ...dynamicTools];
105
115
  }
106
- async syncDynamicTools() {
116
+ async syncDynamicTools(filter) {
107
117
  if (this.dynamicTools.length === 0) {
108
118
  return [];
109
119
  }
110
120
  const registry = new import_utils.Registry();
111
121
  const ephemeral = new _DefaultToolsManager(registry);
112
- await Promise.all(this.dynamicTools.map((register) => register(ephemeral)));
122
+ await Promise.all(this.dynamicTools.map((register) => register(ephemeral, filter)));
113
123
  return [...registry.getValues()];
114
124
  }
115
125
  };
@@ -8,15 +8,17 @@
8
8
  */
9
9
  import type { Context } from '@nocobase/actions';
10
10
  export interface ToolsManager extends ToolsRegistration {
11
- getTools(toolName: string): Promise<ToolsEntry>;
11
+ getTools(toolName: string, filter?: ToolsFilter): Promise<ToolsEntry>;
12
12
  listTools(filter?: ToolsFilter): Promise<ToolsEntry[]>;
13
+ isToolsExisted(toolName: string): boolean;
13
14
  }
14
15
  export interface ToolsRegistration {
15
16
  registerTools(options: ToolsOptions | ToolsOptions[]): void;
16
- registerDynamicTools(provider: (register: ToolsRegistration) => Promise<void>): void;
17
+ registerDynamicTools(provider: DynamicToolsProvider): void;
17
18
  }
18
19
  export type ToolsOptions = {
19
20
  scope: Scope;
21
+ from?: From;
20
22
  execution?: 'frontend' | 'backend';
21
23
  defaultPermission?: Permission;
22
24
  silence?: boolean;
@@ -29,14 +31,20 @@ export type ToolsOptions = {
29
31
  description: string;
30
32
  schema?: any;
31
33
  };
32
- invoke: (ctx: Context, args: any, id: string) => Promise<any>;
34
+ invoke: (ctx: Context, args: any, runtime: ToolsRuntime) => Promise<any>;
35
+ };
36
+ export type ToolsRuntime = {
37
+ toolCallId: string;
38
+ writer: (chunk: any) => void;
33
39
  };
34
40
  export type ToolsEntry = ToolsOptions;
35
41
  export type Scope = 'SPECIFIED' | 'GENERAL' | 'CUSTOM';
36
42
  export type Permission = 'ASK' | 'ALLOW';
37
- export type DynamicToolsProvider = (register: ToolsRegistration) => Promise<void>;
43
+ export type From = 'loader' | 'workflow' | 'mcp';
44
+ export type DynamicToolsProvider = (register: ToolsRegistration, filter?: ToolsFilter) => Promise<void>;
38
45
  export type ToolsFilter = {
39
46
  scope?: Scope;
40
47
  defaultPermission?: Permission;
41
48
  silence?: boolean;
49
+ sessionId?: string;
42
50
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/ai",
3
- "version": "2.1.0-beta.14",
3
+ "version": "2.1.0-beta.16",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./lib/index.js",
@@ -14,23 +14,28 @@
14
14
  "@langchain/google-genai": "^2.1.18",
15
15
  "@langchain/langgraph": "^1.1.4",
16
16
  "@langchain/langgraph-checkpoint": "^1.0.0",
17
+ "@langchain/mcp-adapters": "^1.1.3",
17
18
  "@langchain/ollama": "^1.2.2",
18
19
  "@langchain/openai": "^1.2.7",
19
- "@nocobase/logger": "2.1.0-beta.14",
20
- "@nocobase/resourcer": "2.1.0-beta.14",
21
- "@nocobase/utils": "2.1.0-beta.14",
20
+ "@nocobase/data-source-manager": "2.1.0-beta.16",
21
+ "@nocobase/logger": "2.1.0-beta.16",
22
+ "@nocobase/resourcer": "2.1.0-beta.16",
23
+ "@nocobase/utils": "2.1.0-beta.16",
24
+ "d3-dsv": "2",
22
25
  "fast-glob": "^3.3.2",
23
26
  "flexsearch": "^0.8.2",
27
+ "gray-matter": "^4.0.3",
24
28
  "langchain": "^1.2.24",
25
29
  "mammoth": "^1.10.0",
26
30
  "officeparser": "^5.2.0",
27
31
  "pdf-parse": "^1.1.1",
28
- "word-extractor": "^1.0.4"
32
+ "word-extractor": "^1.0.4",
33
+ "xlsx": "^0.18.5"
29
34
  },
30
35
  "repository": {
31
36
  "type": "git",
32
37
  "url": "git+https://github.com/nocobase/nocobase.git",
33
38
  "directory": "packages/ai"
34
39
  },
35
- "gitHead": "cd55c9a14596d834c30c83a82c38f1f719f7aac4"
40
+ "gitHead": "b9a191705a440a336c85d82fd877fdf152bec70f"
36
41
  }
@@ -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
+ import { createMockServer, MockServer } from '@nocobase/test';
11
+ import { AIEmployeeLoader } from '../loader';
12
+ import path from 'path';
13
+ import { AIManager } from '../ai-manager';
14
+ import { AIEmployeeManager, AIEmployeeEntry } from '../ai-employee-manager';
15
+
16
+ const normalizeEOL = (value: string) => value.replace(/\r\n?/g, '\n');
17
+ const normalizeTools = (entry: AIEmployeeEntry) =>
18
+ [...(entry.skillSettings?.tools ?? [])].sort((a, b) => a.name.localeCompare(b.name));
19
+ const normalizeSkills = (entry: AIEmployeeEntry) => [...(entry.skillSettings?.skills ?? [])].sort();
20
+
21
+ describe('AI employee loader test cases', () => {
22
+ const basePath = path.resolve(__dirname, 'resource', 'ai');
23
+ let app: MockServer;
24
+ let aiManager: AIManager;
25
+ let employeeManager: AIEmployeeManager;
26
+ let loader: AIEmployeeLoader;
27
+
28
+ beforeEach(async () => {
29
+ app = await createMockServer({
30
+ plugins: ['nocobase'],
31
+ });
32
+ await app.pm.enable('ai');
33
+ aiManager = app.aiManager;
34
+ employeeManager = aiManager.employeeManager;
35
+ loader = new AIEmployeeLoader(aiManager, {
36
+ scan: {
37
+ basePath,
38
+ pattern: [
39
+ '**/ai-employees/*.ts',
40
+ '**/ai-employees/*/index.ts',
41
+ '**/ai-employees/*.js',
42
+ '**/ai-employees/*/index.js',
43
+ '**/ai-employees/*/prompt.md',
44
+ '!**/ai-employees/**/*.d.ts',
45
+ ],
46
+ },
47
+ });
48
+ });
49
+
50
+ afterEach(async () => {
51
+ await app.destroy();
52
+ });
53
+
54
+ it('should load ai employee from named file', async () => {
55
+ await loader.load();
56
+ const employee = await employeeManager.getEmployee('named-file-employee');
57
+
58
+ expect(employee).toBeDefined();
59
+ expect(employee.username).toBe('named-file-employee');
60
+ expect(employee.nickname).toBe('Named File Employee');
61
+ expect(normalizeTools(employee)).toEqual([]);
62
+ expect(normalizeSkills(employee)).toEqual([]);
63
+ });
64
+
65
+ it('should load ai employee from index file', async () => {
66
+ await loader.load();
67
+ const employee = await employeeManager.getEmployee('index-employee');
68
+
69
+ expect(employee).toBeDefined();
70
+ expect(employee.username).toBe('index-employee');
71
+ expect(employee.nickname).toBe('Index Employee');
72
+ expect(normalizeEOL(employee.defaultPrompt)).toBe('Prompt from markdown file.\n');
73
+ expect(normalizeTools(employee)).toEqual([]);
74
+ expect(normalizeSkills(employee)).toEqual([]);
75
+ });
76
+
77
+ it('should load ai employee with tools directory', async () => {
78
+ await loader.load();
79
+ const employee = await employeeManager.getEmployee('with-tools');
80
+
81
+ expect(employee).toBeDefined();
82
+ expect(normalizeTools(employee)).toEqual([{ name: 'discoveredTool' }]);
83
+ });
84
+
85
+ it('should load ai employee with skills directory', async () => {
86
+ await loader.load();
87
+ const employee = await employeeManager.getEmployee('with-skills');
88
+
89
+ expect(employee).toBeDefined();
90
+ expect(normalizeSkills(employee)).toEqual(['analysis']);
91
+ });
92
+
93
+ it('should merge ai employee tools directory with tools config', async () => {
94
+ await loader.load();
95
+ const employee = await employeeManager.getEmployee('with-tools-merge');
96
+
97
+ expect(employee).toBeDefined();
98
+ expect(normalizeTools(employee)).toEqual([{ autoCall: true, name: 'configuredTool' }, { name: 'discoveredTool' }]);
99
+ });
100
+
101
+ it('should merge ai employee skills directory with skills config', async () => {
102
+ await loader.load();
103
+ const employee = await employeeManager.getEmployee('with-skills-merge');
104
+
105
+ expect(employee).toBeDefined();
106
+ expect(normalizeSkills(employee)).toEqual(['configured-skill', 'discovered-skill']);
107
+ });
108
+ });