@nocobase/ai 2.1.30 → 2.1.32
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.
|
@@ -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,7 +19,15 @@ 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 });
|
|
@@ -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
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/ai",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.32",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -17,10 +17,10 @@
|
|
|
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": "2.1.
|
|
21
|
-
"@nocobase/logger": "2.1.
|
|
22
|
-
"@nocobase/resourcer": "2.1.
|
|
23
|
-
"@nocobase/utils": "2.1.
|
|
20
|
+
"@nocobase/data-source-manager": "2.1.32",
|
|
21
|
+
"@nocobase/logger": "2.1.32",
|
|
22
|
+
"@nocobase/resourcer": "2.1.32",
|
|
23
|
+
"@nocobase/utils": "2.1.32",
|
|
24
24
|
"d3-dsv": "2",
|
|
25
25
|
"fast-glob": "^3.3.2",
|
|
26
26
|
"flexsearch": "^0.8.2",
|
|
@@ -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": "737a1324f650d7bc66594203b68eb0180553a551"
|
|
41
41
|
}
|
|
@@ -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[]> => {
|
|
@@ -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
|
}
|