@nocobase/ai 2.3.0-alpha.1 → 3.0.0-alpha.2
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.
- package/lib/document-loader/index.d.ts +2 -0
- package/lib/document-loader/index.js +6 -0
- package/lib/document-loader/loader.worker.js +15 -6
- package/lib/tools-manager/index.d.ts +1 -0
- package/lib/tools-manager/index.js +1 -0
- package/package.json +7 -7
- package/src/__tests__/document-loader.test.ts +11 -0
- package/src/document-loader/index.ts +10 -0
- package/src/document-loader/loader.worker.ts +14 -5
- package/src/tools-manager/index.ts +1 -0
|
@@ -10,5 +10,7 @@ import { Document } from '@langchain/core/documents';
|
|
|
10
10
|
export type DocumentLoaderWorkerOptions = {
|
|
11
11
|
filePath: string;
|
|
12
12
|
mimeType?: string;
|
|
13
|
+
/** Timeout in milliseconds for the worker to complete. Defaults to 5 minutes. */
|
|
14
|
+
timeout?: number;
|
|
13
15
|
};
|
|
14
16
|
export declare const loadByWorker: (extname: string, options: DocumentLoaderWorkerOptions) => Promise<Document[]>;
|
|
@@ -42,12 +42,14 @@ __export(document_loader_exports, {
|
|
|
42
42
|
module.exports = __toCommonJS(document_loader_exports);
|
|
43
43
|
var import_node_worker_threads = require("node:worker_threads");
|
|
44
44
|
var import_node_path = __toESM(require("node:path"));
|
|
45
|
+
const DEFAULT_WORKER_TIMEOUT = 5 * 60 * 1e3;
|
|
45
46
|
const loadByWorker = /* @__PURE__ */ __name(async (extname, options) => {
|
|
46
47
|
const isTsRuntime = __filename.endsWith(".ts");
|
|
47
48
|
const workerPath = import_node_path.default.join(__dirname, `loader.worker.${isTsRuntime ? "ts" : "js"}`);
|
|
48
49
|
const worker = new import_node_worker_threads.Worker(workerPath, {
|
|
49
50
|
execArgv: isTsRuntime ? ["--require", "tsx/cjs"] : void 0
|
|
50
51
|
});
|
|
52
|
+
const timeout = options.timeout ?? DEFAULT_WORKER_TIMEOUT;
|
|
51
53
|
return new Promise((resolve, reject) => {
|
|
52
54
|
let settled = false;
|
|
53
55
|
const close = /* @__PURE__ */ __name((error, result) => {
|
|
@@ -55,12 +57,16 @@ const loadByWorker = /* @__PURE__ */ __name(async (extname, options) => {
|
|
|
55
57
|
return;
|
|
56
58
|
}
|
|
57
59
|
settled = true;
|
|
60
|
+
clearTimeout(timer);
|
|
58
61
|
if (error) {
|
|
59
62
|
reject(error);
|
|
60
63
|
return;
|
|
61
64
|
}
|
|
62
65
|
resolve(result || []);
|
|
63
66
|
}, "close");
|
|
67
|
+
const timer = setTimeout(() => {
|
|
68
|
+
close(new Error(`Document loading timed out after ${Math.round(timeout / 1e3)}s`));
|
|
69
|
+
}, timeout);
|
|
64
70
|
worker.once("message", (payload) => {
|
|
65
71
|
if (payload == null ? void 0 : payload.error) {
|
|
66
72
|
close(new Error(payload.error));
|
|
@@ -19,16 +19,24 @@ var import_xlsx = require("./xlsx");
|
|
|
19
19
|
var _a;
|
|
20
20
|
const loadPdf = /* @__PURE__ */ __name(async (filePath) => {
|
|
21
21
|
const loader = new import_pdf.PDFLoader(filePath);
|
|
22
|
-
|
|
22
|
+
try {
|
|
23
|
+
return await loader.load();
|
|
24
|
+
} catch (error) {
|
|
25
|
+
const err = error;
|
|
26
|
+
if ((err == null ? void 0 : err.name) === "PasswordException" || /password/i.test(err == null ? void 0 : err.message)) {
|
|
27
|
+
throw new Error("The PDF file is password-protected and cannot be parsed. Please upload an unlocked version.");
|
|
28
|
+
}
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
23
31
|
}, "loadPdf");
|
|
24
32
|
const loadDoc = /* @__PURE__ */ __name(async (filePath, type) => {
|
|
25
33
|
const loader = new import_docx.DocxLoader(filePath, { type });
|
|
26
34
|
return loader.load();
|
|
27
35
|
}, "loadDoc");
|
|
28
|
-
const
|
|
36
|
+
const loadPptx = /* @__PURE__ */ __name(async (filePath) => {
|
|
29
37
|
const loader = new import_pptx.PPTXLoader(filePath);
|
|
30
38
|
return loader.load();
|
|
31
|
-
}, "
|
|
39
|
+
}, "loadPptx");
|
|
32
40
|
const loadTxt = /* @__PURE__ */ __name(async (filePath) => {
|
|
33
41
|
const loader = new import_text.TextLoader(filePath);
|
|
34
42
|
return loader.load();
|
|
@@ -41,9 +49,8 @@ const loadByExtname = /* @__PURE__ */ __name(async (payload) => {
|
|
|
41
49
|
switch (payload.extname) {
|
|
42
50
|
case ".pdf":
|
|
43
51
|
return loadPdf(payload.filePath);
|
|
44
|
-
case ".ppt":
|
|
45
52
|
case ".pptx":
|
|
46
|
-
return
|
|
53
|
+
return loadPptx(payload.filePath);
|
|
47
54
|
case ".doc":
|
|
48
55
|
return loadDoc(payload.filePath, "doc");
|
|
49
56
|
case ".docx":
|
|
@@ -52,6 +59,7 @@ const loadByExtname = /* @__PURE__ */ __name(async (payload) => {
|
|
|
52
59
|
return loadCsv(payload.filePath);
|
|
53
60
|
case ".xls":
|
|
54
61
|
case ".xlsx":
|
|
62
|
+
case ".xlsm":
|
|
55
63
|
return (0, import_xlsx.loadXlsx)(payload.filePath, payload.mimeType);
|
|
56
64
|
case ".json":
|
|
57
65
|
case ".md":
|
|
@@ -74,8 +82,9 @@ const loadByExtname = /* @__PURE__ */ __name(async (payload) => {
|
|
|
74
82
|
};
|
|
75
83
|
(_a2 = import_node_worker_threads.parentPort) == null ? void 0 : _a2.postMessage(response);
|
|
76
84
|
} catch (error) {
|
|
85
|
+
const err = error;
|
|
77
86
|
const response = {
|
|
78
|
-
error:
|
|
87
|
+
error: (err == null ? void 0 : err.message) || String(error)
|
|
79
88
|
};
|
|
80
89
|
(_b = import_node_worker_threads.parentPort) == null ? void 0 : _b.postMessage(response);
|
|
81
90
|
}
|
|
@@ -22,6 +22,7 @@ export declare class DefaultToolsManager implements ToolsManager {
|
|
|
22
22
|
}
|
|
23
23
|
export declare function defineTools(options: ToolsOptions): ToolsOptions;
|
|
24
24
|
export declare const SYSTEM_TOOLS: {
|
|
25
|
+
GET_SKILL: string;
|
|
25
26
|
WEB_SEARCH: string;
|
|
26
27
|
KNOWLEDGE_BASE: string;
|
|
27
28
|
WORK_FLOW_TASK_OUTPUT: string;
|
|
@@ -132,6 +132,7 @@ function defineTools(options) {
|
|
|
132
132
|
}
|
|
133
133
|
__name(defineTools, "defineTools");
|
|
134
134
|
const SYSTEM_TOOLS = {
|
|
135
|
+
GET_SKILL: "getSkill",
|
|
135
136
|
WEB_SEARCH: "subAgentWebSearch",
|
|
136
137
|
KNOWLEDGE_BASE: "knowledge-base-retrieve",
|
|
137
138
|
WORK_FLOW_TASK_OUTPUT: "aiEmployeeWorkflowTaskOutput"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/ai",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-alpha.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -17,17 +17,17 @@
|
|
|
17
17
|
"@langchain/mcp-adapters": "1.1.3",
|
|
18
18
|
"@langchain/ollama": "1.2.7",
|
|
19
19
|
"@langchain/openai": "1.4.7",
|
|
20
|
-
"@nocobase/data-source-manager": "
|
|
21
|
-
"@nocobase/logger": "
|
|
22
|
-
"@nocobase/resourcer": "
|
|
23
|
-
"@nocobase/utils": "
|
|
20
|
+
"@nocobase/data-source-manager": "3.0.0-alpha.2",
|
|
21
|
+
"@nocobase/logger": "3.0.0-alpha.2",
|
|
22
|
+
"@nocobase/resourcer": "3.0.0-alpha.2",
|
|
23
|
+
"@nocobase/utils": "3.0.0-alpha.2",
|
|
24
24
|
"d3-dsv": "2",
|
|
25
25
|
"fast-glob": "^3.3.2",
|
|
26
26
|
"flexsearch": "^0.8.2",
|
|
27
27
|
"gray-matter": "^4.0.3",
|
|
28
28
|
"langchain": "1.2.39",
|
|
29
29
|
"mammoth": "^1.10.0",
|
|
30
|
-
"officeparser": "^
|
|
30
|
+
"officeparser": "^6.0.4",
|
|
31
31
|
"pdf-parse": "^1.1.1",
|
|
32
32
|
"word-extractor": "^1.0.4",
|
|
33
33
|
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz"
|
|
@@ -37,5 +37,5 @@
|
|
|
37
37
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
38
38
|
"directory": "packages/ai"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "a3faea21082b30989301de04591ee1b91fd4e6a5"
|
|
41
41
|
}
|
|
@@ -60,4 +60,15 @@ describe('Document loader worker', () => {
|
|
|
60
60
|
expect(documents[0].pageContent).toBe('Sheet: Sheet1\nname\tvalue\nalpha\t1');
|
|
61
61
|
expect(documents[0].metadata.source).toBe(filePath);
|
|
62
62
|
});
|
|
63
|
+
|
|
64
|
+
it('does not parse legacy ppt files', async () => {
|
|
65
|
+
const filePath = await createTempFile('source.ppt', 'legacy powerpoint content');
|
|
66
|
+
|
|
67
|
+
const documents = await loadByWorker('.ppt', {
|
|
68
|
+
filePath,
|
|
69
|
+
mimeType: 'application/vnd.ms-powerpoint',
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
expect(documents).toEqual([]);
|
|
73
|
+
});
|
|
63
74
|
});
|
|
@@ -14,14 +14,19 @@ import path from 'node:path';
|
|
|
14
14
|
export type DocumentLoaderWorkerOptions = {
|
|
15
15
|
filePath: string;
|
|
16
16
|
mimeType?: string;
|
|
17
|
+
/** Timeout in milliseconds for the worker to complete. Defaults to 5 minutes. */
|
|
18
|
+
timeout?: number;
|
|
17
19
|
};
|
|
18
20
|
|
|
21
|
+
const DEFAULT_WORKER_TIMEOUT = 5 * 60 * 1000;
|
|
22
|
+
|
|
19
23
|
export const loadByWorker = async (extname: string, options: DocumentLoaderWorkerOptions): Promise<Document[]> => {
|
|
20
24
|
const isTsRuntime = __filename.endsWith('.ts');
|
|
21
25
|
const workerPath = path.join(__dirname, `loader.worker.${isTsRuntime ? 'ts' : 'js'}`);
|
|
22
26
|
const worker = new Worker(workerPath, {
|
|
23
27
|
execArgv: isTsRuntime ? ['--require', 'tsx/cjs'] : undefined,
|
|
24
28
|
});
|
|
29
|
+
const timeout = options.timeout ?? DEFAULT_WORKER_TIMEOUT;
|
|
25
30
|
return new Promise<Document[]>((resolve, reject) => {
|
|
26
31
|
let settled = false;
|
|
27
32
|
const close = (error?: Error, result?: Document[]) => {
|
|
@@ -29,6 +34,7 @@ export const loadByWorker = async (extname: string, options: DocumentLoaderWorke
|
|
|
29
34
|
return;
|
|
30
35
|
}
|
|
31
36
|
settled = true;
|
|
37
|
+
clearTimeout(timer);
|
|
32
38
|
if (error) {
|
|
33
39
|
reject(error);
|
|
34
40
|
return;
|
|
@@ -36,6 +42,10 @@ export const loadByWorker = async (extname: string, options: DocumentLoaderWorke
|
|
|
36
42
|
resolve(result || []);
|
|
37
43
|
};
|
|
38
44
|
|
|
45
|
+
const timer = setTimeout(() => {
|
|
46
|
+
close(new Error(`Document loading timed out after ${Math.round(timeout / 1000)}s`));
|
|
47
|
+
}, timeout);
|
|
48
|
+
|
|
39
49
|
worker.once('message', (payload: { documents?: Document[]; error?: string }) => {
|
|
40
50
|
if (payload?.error) {
|
|
41
51
|
close(new Error(payload.error));
|
|
@@ -29,7 +29,15 @@ type WorkerResponse = {
|
|
|
29
29
|
|
|
30
30
|
const loadPdf = async (filePath: string): Promise<Document[]> => {
|
|
31
31
|
const loader = new PDFLoader(filePath);
|
|
32
|
-
|
|
32
|
+
try {
|
|
33
|
+
return await loader.load();
|
|
34
|
+
} catch (error) {
|
|
35
|
+
const err = error as Error;
|
|
36
|
+
if (err?.name === 'PasswordException' || /password/i.test(err?.message)) {
|
|
37
|
+
throw new Error('The PDF file is password-protected and cannot be parsed. Please upload an unlocked version.');
|
|
38
|
+
}
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
33
41
|
};
|
|
34
42
|
|
|
35
43
|
const loadDoc = async (filePath: string, type: 'docx' | 'doc'): Promise<Document[]> => {
|
|
@@ -37,7 +45,7 @@ const loadDoc = async (filePath: string, type: 'docx' | 'doc'): Promise<Document
|
|
|
37
45
|
return loader.load();
|
|
38
46
|
};
|
|
39
47
|
|
|
40
|
-
const
|
|
48
|
+
const loadPptx = async (filePath: string): Promise<Document[]> => {
|
|
41
49
|
const loader = new PPTXLoader(filePath);
|
|
42
50
|
return loader.load();
|
|
43
51
|
};
|
|
@@ -56,9 +64,8 @@ const loadByExtname = async (payload: ParsePayload): Promise<Document[]> => {
|
|
|
56
64
|
switch (payload.extname) {
|
|
57
65
|
case '.pdf':
|
|
58
66
|
return loadPdf(payload.filePath);
|
|
59
|
-
case '.ppt':
|
|
60
67
|
case '.pptx':
|
|
61
|
-
return
|
|
68
|
+
return loadPptx(payload.filePath);
|
|
62
69
|
case '.doc':
|
|
63
70
|
return loadDoc(payload.filePath, 'doc');
|
|
64
71
|
case '.docx':
|
|
@@ -67,6 +74,7 @@ const loadByExtname = async (payload: ParsePayload): Promise<Document[]> => {
|
|
|
67
74
|
return loadCsv(payload.filePath);
|
|
68
75
|
case '.xls':
|
|
69
76
|
case '.xlsx':
|
|
77
|
+
case '.xlsm':
|
|
70
78
|
return loadXlsx(payload.filePath, payload.mimeType);
|
|
71
79
|
case '.json':
|
|
72
80
|
case '.md':
|
|
@@ -89,8 +97,9 @@ parentPort?.on('message', async (payload: ParsePayload) => {
|
|
|
89
97
|
};
|
|
90
98
|
parentPort?.postMessage(response);
|
|
91
99
|
} catch (error) {
|
|
100
|
+
const err = error as Error;
|
|
92
101
|
const response: WorkerResponse = {
|
|
93
|
-
error:
|
|
102
|
+
error: err?.message || String(error),
|
|
94
103
|
};
|
|
95
104
|
parentPort?.postMessage(response);
|
|
96
105
|
}
|
|
@@ -106,6 +106,7 @@ export function defineTools(options: ToolsOptions) {
|
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
export const SYSTEM_TOOLS = {
|
|
109
|
+
GET_SKILL: 'getSkill',
|
|
109
110
|
WEB_SEARCH: 'subAgentWebSearch',
|
|
110
111
|
KNOWLEDGE_BASE: 'knowledge-base-retrieve',
|
|
111
112
|
WORK_FLOW_TASK_OUTPUT: 'aiEmployeeWorkflowTaskOutput',
|