@nocobase/ai 2.1.24 → 2.1.25
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 +5 -1
- package/lib/document-loader/index.js +3 -4
- package/lib/document-loader/loader.worker.js +17 -18
- package/lib/document-loader/xlsx.d.ts +1 -1
- package/lib/document-loader/xlsx.js +6 -5
- package/package.json +6 -6
- package/src/__tests__/document-loader.test.ts +63 -0
- package/src/document-loader/index.ts +8 -4
- package/src/document-loader/loader.worker.ts +18 -21
- package/src/document-loader/xlsx.ts +6 -5
|
@@ -7,4 +7,8 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
import { Document } from '@langchain/core/documents';
|
|
10
|
-
export
|
|
10
|
+
export type DocumentLoaderWorkerOptions = {
|
|
11
|
+
filePath: string;
|
|
12
|
+
mimeType?: string;
|
|
13
|
+
};
|
|
14
|
+
export declare const loadByWorker: (extname: string, options: DocumentLoaderWorkerOptions) => Promise<Document[]>;
|
|
@@ -42,8 +42,7 @@ __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 loadByWorker = /* @__PURE__ */ __name(async (extname,
|
|
46
|
-
const buffer = Buffer.from(await blob.arrayBuffer());
|
|
45
|
+
const loadByWorker = /* @__PURE__ */ __name(async (extname, options) => {
|
|
47
46
|
const isTsRuntime = __filename.endsWith(".ts");
|
|
48
47
|
const workerPath = import_node_path.default.join(__dirname, `loader.worker.${isTsRuntime ? "ts" : "js"}`);
|
|
49
48
|
const worker = new import_node_worker_threads.Worker(workerPath, {
|
|
@@ -77,8 +76,8 @@ const loadByWorker = /* @__PURE__ */ __name(async (extname, blob) => {
|
|
|
77
76
|
});
|
|
78
77
|
worker.postMessage({
|
|
79
78
|
extname,
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
filePath: options.filePath,
|
|
80
|
+
mimeType: options.mimeType
|
|
82
81
|
});
|
|
83
82
|
}).finally(() => {
|
|
84
83
|
worker.terminate().catch(() => void 0);
|
|
@@ -17,47 +17,46 @@ var import_text = require("./vendor/langchain/document_loaders/fs/text");
|
|
|
17
17
|
var import_csv = require("@langchain/community/document_loaders/fs/csv");
|
|
18
18
|
var import_xlsx = require("./xlsx");
|
|
19
19
|
var _a;
|
|
20
|
-
const loadPdf = /* @__PURE__ */ __name(async (
|
|
21
|
-
const loader = new import_pdf.PDFLoader(
|
|
20
|
+
const loadPdf = /* @__PURE__ */ __name(async (filePath) => {
|
|
21
|
+
const loader = new import_pdf.PDFLoader(filePath);
|
|
22
22
|
return loader.load();
|
|
23
23
|
}, "loadPdf");
|
|
24
|
-
const loadDoc = /* @__PURE__ */ __name(async (
|
|
25
|
-
const loader = new import_docx.DocxLoader(
|
|
24
|
+
const loadDoc = /* @__PURE__ */ __name(async (filePath, type) => {
|
|
25
|
+
const loader = new import_docx.DocxLoader(filePath, { type });
|
|
26
26
|
return loader.load();
|
|
27
27
|
}, "loadDoc");
|
|
28
|
-
const loadPpt = /* @__PURE__ */ __name(async (
|
|
29
|
-
const loader = new import_pptx.PPTXLoader(
|
|
28
|
+
const loadPpt = /* @__PURE__ */ __name(async (filePath) => {
|
|
29
|
+
const loader = new import_pptx.PPTXLoader(filePath);
|
|
30
30
|
return loader.load();
|
|
31
31
|
}, "loadPpt");
|
|
32
|
-
const loadTxt = /* @__PURE__ */ __name(async (
|
|
33
|
-
const loader = new import_text.TextLoader(
|
|
32
|
+
const loadTxt = /* @__PURE__ */ __name(async (filePath) => {
|
|
33
|
+
const loader = new import_text.TextLoader(filePath);
|
|
34
34
|
return loader.load();
|
|
35
35
|
}, "loadTxt");
|
|
36
|
-
const loadCsv = /* @__PURE__ */ __name(async (
|
|
37
|
-
const loader = new import_csv.CSVLoader(
|
|
36
|
+
const loadCsv = /* @__PURE__ */ __name(async (filePath) => {
|
|
37
|
+
const loader = new import_csv.CSVLoader(filePath);
|
|
38
38
|
return loader.load();
|
|
39
39
|
}, "loadCsv");
|
|
40
40
|
const loadByExtname = /* @__PURE__ */ __name(async (payload) => {
|
|
41
|
-
const blob = new Blob([Buffer.from(payload.buffer)], { type: payload.mimeType ?? "application/octet-stream" });
|
|
42
41
|
switch (payload.extname) {
|
|
43
42
|
case ".pdf":
|
|
44
|
-
return loadPdf(
|
|
43
|
+
return loadPdf(payload.filePath);
|
|
45
44
|
case ".ppt":
|
|
46
45
|
case ".pptx":
|
|
47
|
-
return loadPpt(
|
|
46
|
+
return loadPpt(payload.filePath);
|
|
48
47
|
case ".doc":
|
|
49
|
-
return loadDoc(
|
|
48
|
+
return loadDoc(payload.filePath, "doc");
|
|
50
49
|
case ".docx":
|
|
51
|
-
return loadDoc(
|
|
50
|
+
return loadDoc(payload.filePath, "docx");
|
|
52
51
|
case ".csv":
|
|
53
|
-
return loadCsv(
|
|
52
|
+
return loadCsv(payload.filePath);
|
|
54
53
|
case ".xls":
|
|
55
54
|
case ".xlsx":
|
|
56
|
-
return (0, import_xlsx.loadXlsx)(
|
|
55
|
+
return (0, import_xlsx.loadXlsx)(payload.filePath, payload.mimeType);
|
|
57
56
|
case ".json":
|
|
58
57
|
case ".md":
|
|
59
58
|
case ".txt":
|
|
60
|
-
return loadTxt(
|
|
59
|
+
return loadTxt(payload.filePath);
|
|
61
60
|
default:
|
|
62
61
|
return [];
|
|
63
62
|
}
|
|
@@ -7,4 +7,4 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
import { Document } from '@langchain/core/documents';
|
|
10
|
-
export declare const loadXlsx: (
|
|
10
|
+
export declare const loadXlsx: (filePath: string, mimeType?: string) => Promise<Document[]>;
|
|
@@ -41,6 +41,7 @@ __export(xlsx_exports, {
|
|
|
41
41
|
});
|
|
42
42
|
module.exports = __toCommonJS(xlsx_exports);
|
|
43
43
|
var import_documents = require("@langchain/core/documents");
|
|
44
|
+
var import_promises = require("node:fs/promises");
|
|
44
45
|
var XLSX = __toESM(require("xlsx"));
|
|
45
46
|
const normalizeCellValue = /* @__PURE__ */ __name((value) => {
|
|
46
47
|
if (value === void 0 || value === null) {
|
|
@@ -64,10 +65,10 @@ const sheetToLines = /* @__PURE__ */ __name((sheet) => {
|
|
|
64
65
|
});
|
|
65
66
|
return rows.map((row) => trimTrailingEmptyCells(Array.isArray(row) ? row : [])).filter((row) => row.length > 0).map((row) => row.map((cell) => normalizeCellValue(cell)).join(" ")).filter((line) => line.trim().length > 0);
|
|
66
67
|
}, "sheetToLines");
|
|
67
|
-
const loadXlsx = /* @__PURE__ */ __name(async (
|
|
68
|
-
const buffer = await
|
|
68
|
+
const loadXlsx = /* @__PURE__ */ __name(async (filePath, mimeType) => {
|
|
69
|
+
const buffer = await (0, import_promises.readFile)(filePath);
|
|
69
70
|
const workbook = XLSX.read(buffer, {
|
|
70
|
-
type: "
|
|
71
|
+
type: "buffer",
|
|
71
72
|
cellText: true
|
|
72
73
|
});
|
|
73
74
|
const documents = [];
|
|
@@ -84,8 +85,8 @@ const loadXlsx = /* @__PURE__ */ __name(async (blob) => {
|
|
|
84
85
|
new import_documents.Document({
|
|
85
86
|
pageContent: [`Sheet: ${sheetName}`, ...lines].join("\n"),
|
|
86
87
|
metadata: {
|
|
87
|
-
source:
|
|
88
|
-
blobType:
|
|
88
|
+
source: filePath,
|
|
89
|
+
blobType: mimeType,
|
|
89
90
|
sheetName,
|
|
90
91
|
sheetIndex: index
|
|
91
92
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/ai",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.25",
|
|
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.25",
|
|
21
|
+
"@nocobase/logger": "2.1.25",
|
|
22
|
+
"@nocobase/resourcer": "2.1.25",
|
|
23
|
+
"@nocobase/utils": "2.1.25",
|
|
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": "f3326a2681004deee97b563fb6ab04744e26ac71"
|
|
41
41
|
}
|
|
@@ -0,0 +1,63 @@
|
|
|
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 { loadByWorker } from '../document-loader';
|
|
11
|
+
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
12
|
+
import os from 'node:os';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import * as XLSX from 'xlsx';
|
|
15
|
+
|
|
16
|
+
describe('Document loader worker', () => {
|
|
17
|
+
const tempDirs: string[] = [];
|
|
18
|
+
|
|
19
|
+
const createTempFile = async (filename: string, content: string | Buffer) => {
|
|
20
|
+
const tempDir = await mkdtemp(path.join(os.tmpdir(), 'nocobase-document-loader-test-'));
|
|
21
|
+
tempDirs.push(tempDir);
|
|
22
|
+
const filePath = path.join(tempDir, filename);
|
|
23
|
+
await writeFile(filePath, content);
|
|
24
|
+
return filePath;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
afterEach(async () => {
|
|
28
|
+
await Promise.all(tempDirs.splice(0).map((tempDir) => rm(tempDir, { recursive: true, force: true })));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('loads text files by path', async () => {
|
|
32
|
+
const filePath = await createTempFile('source.txt', 'hello knowledge base\nsecond line\n');
|
|
33
|
+
|
|
34
|
+
const documents = await loadByWorker('.txt', {
|
|
35
|
+
filePath,
|
|
36
|
+
mimeType: 'text/plain',
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
expect(documents).toHaveLength(1);
|
|
40
|
+
expect(documents[0].pageContent).toBe('hello knowledge base\nsecond line\n');
|
|
41
|
+
expect(documents[0].metadata.source).toBe(filePath);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('loads xlsx files by path', async () => {
|
|
45
|
+
const worksheet = XLSX.utils.aoa_to_sheet([
|
|
46
|
+
['name', 'value'],
|
|
47
|
+
['alpha', 1],
|
|
48
|
+
]);
|
|
49
|
+
const workbook = XLSX.utils.book_new();
|
|
50
|
+
XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
|
|
51
|
+
const buffer = XLSX.write(workbook, { type: 'buffer', bookType: 'xlsx' });
|
|
52
|
+
const filePath = await createTempFile('source.xlsx', buffer);
|
|
53
|
+
|
|
54
|
+
const documents = await loadByWorker('.xlsx', {
|
|
55
|
+
filePath,
|
|
56
|
+
mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
expect(documents).toHaveLength(1);
|
|
60
|
+
expect(documents[0].pageContent).toBe('Sheet: Sheet1\nname\tvalue\nalpha\t1');
|
|
61
|
+
expect(documents[0].metadata.source).toBe(filePath);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -11,8 +11,12 @@ import { Document } from '@langchain/core/documents';
|
|
|
11
11
|
import { Worker } from 'node:worker_threads';
|
|
12
12
|
import path from 'node:path';
|
|
13
13
|
|
|
14
|
-
export
|
|
15
|
-
|
|
14
|
+
export type DocumentLoaderWorkerOptions = {
|
|
15
|
+
filePath: string;
|
|
16
|
+
mimeType?: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const loadByWorker = async (extname: string, options: DocumentLoaderWorkerOptions): Promise<Document[]> => {
|
|
16
20
|
const isTsRuntime = __filename.endsWith('.ts');
|
|
17
21
|
const workerPath = path.join(__dirname, `loader.worker.${isTsRuntime ? 'ts' : 'js'}`);
|
|
18
22
|
const worker = new Worker(workerPath, {
|
|
@@ -48,8 +52,8 @@ export const loadByWorker = async (extname: string, blob: Blob): Promise<Documen
|
|
|
48
52
|
|
|
49
53
|
worker.postMessage({
|
|
50
54
|
extname,
|
|
51
|
-
|
|
52
|
-
|
|
55
|
+
filePath: options.filePath,
|
|
56
|
+
mimeType: options.mimeType,
|
|
53
57
|
});
|
|
54
58
|
}).finally(() => {
|
|
55
59
|
worker.terminate().catch(() => undefined);
|
|
@@ -19,7 +19,7 @@ import { loadXlsx } from './xlsx';
|
|
|
19
19
|
type ParsePayload = {
|
|
20
20
|
extname: string;
|
|
21
21
|
mimeType?: string;
|
|
22
|
-
|
|
22
|
+
filePath: string;
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
type WorkerResponse = {
|
|
@@ -27,54 +27,51 @@ type WorkerResponse = {
|
|
|
27
27
|
error?: string;
|
|
28
28
|
};
|
|
29
29
|
|
|
30
|
-
const loadPdf = async (
|
|
31
|
-
const loader = new PDFLoader(
|
|
30
|
+
const loadPdf = async (filePath: string): Promise<Document[]> => {
|
|
31
|
+
const loader = new PDFLoader(filePath);
|
|
32
32
|
return loader.load();
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
-
const loadDoc = async (
|
|
36
|
-
const loader = new DocxLoader(
|
|
35
|
+
const loadDoc = async (filePath: string, type: 'docx' | 'doc'): Promise<Document[]> => {
|
|
36
|
+
const loader = new DocxLoader(filePath, { type });
|
|
37
37
|
return loader.load();
|
|
38
38
|
};
|
|
39
39
|
|
|
40
|
-
const loadPpt = async (
|
|
41
|
-
const loader = new PPTXLoader(
|
|
40
|
+
const loadPpt = async (filePath: string): Promise<Document[]> => {
|
|
41
|
+
const loader = new PPTXLoader(filePath);
|
|
42
42
|
return loader.load();
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
-
const loadTxt = async (
|
|
46
|
-
const loader = new TextLoader(
|
|
45
|
+
const loadTxt = async (filePath: string): Promise<Document[]> => {
|
|
46
|
+
const loader = new TextLoader(filePath);
|
|
47
47
|
return loader.load();
|
|
48
48
|
};
|
|
49
49
|
|
|
50
|
-
const loadCsv = async (
|
|
51
|
-
const loader = new CSVLoader(
|
|
50
|
+
const loadCsv = async (filePath: string): Promise<Document[]> => {
|
|
51
|
+
const loader = new CSVLoader(filePath);
|
|
52
52
|
return loader.load();
|
|
53
53
|
};
|
|
54
54
|
|
|
55
55
|
const loadByExtname = async (payload: ParsePayload): Promise<Document[]> => {
|
|
56
|
-
// @ts-ignore
|
|
57
|
-
const blob = new Blob([Buffer.from(payload.buffer)], { type: payload.mimeType ?? 'application/octet-stream' });
|
|
58
|
-
|
|
59
56
|
switch (payload.extname) {
|
|
60
57
|
case '.pdf':
|
|
61
|
-
return loadPdf(
|
|
58
|
+
return loadPdf(payload.filePath);
|
|
62
59
|
case '.ppt':
|
|
63
60
|
case '.pptx':
|
|
64
|
-
return loadPpt(
|
|
61
|
+
return loadPpt(payload.filePath);
|
|
65
62
|
case '.doc':
|
|
66
|
-
return loadDoc(
|
|
63
|
+
return loadDoc(payload.filePath, 'doc');
|
|
67
64
|
case '.docx':
|
|
68
|
-
return loadDoc(
|
|
65
|
+
return loadDoc(payload.filePath, 'docx');
|
|
69
66
|
case '.csv':
|
|
70
|
-
return loadCsv(
|
|
67
|
+
return loadCsv(payload.filePath);
|
|
71
68
|
case '.xls':
|
|
72
69
|
case '.xlsx':
|
|
73
|
-
return loadXlsx(
|
|
70
|
+
return loadXlsx(payload.filePath, payload.mimeType);
|
|
74
71
|
case '.json':
|
|
75
72
|
case '.md':
|
|
76
73
|
case '.txt':
|
|
77
|
-
return loadTxt(
|
|
74
|
+
return loadTxt(payload.filePath);
|
|
78
75
|
default:
|
|
79
76
|
return [];
|
|
80
77
|
}
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { Document } from '@langchain/core/documents';
|
|
11
|
+
import { readFile } from 'node:fs/promises';
|
|
11
12
|
import * as XLSX from 'xlsx';
|
|
12
13
|
|
|
13
14
|
const normalizeCellValue = (value: unknown): string => {
|
|
@@ -43,10 +44,10 @@ const sheetToLines = (sheet: XLSX.WorkSheet): string[] => {
|
|
|
43
44
|
.filter((line) => line.trim().length > 0);
|
|
44
45
|
};
|
|
45
46
|
|
|
46
|
-
export const loadXlsx = async (
|
|
47
|
-
const buffer = await
|
|
47
|
+
export const loadXlsx = async (filePath: string, mimeType?: string): Promise<Document[]> => {
|
|
48
|
+
const buffer = await readFile(filePath);
|
|
48
49
|
const workbook = XLSX.read(buffer, {
|
|
49
|
-
type: '
|
|
50
|
+
type: 'buffer',
|
|
50
51
|
cellText: true,
|
|
51
52
|
});
|
|
52
53
|
|
|
@@ -69,8 +70,8 @@ export const loadXlsx = async (blob: Blob): Promise<Document[]> => {
|
|
|
69
70
|
new Document({
|
|
70
71
|
pageContent: [`Sheet: ${sheetName}`, ...lines].join('\n'),
|
|
71
72
|
metadata: {
|
|
72
|
-
source:
|
|
73
|
-
blobType:
|
|
73
|
+
source: filePath,
|
|
74
|
+
blobType: mimeType,
|
|
74
75
|
sheetName,
|
|
75
76
|
sheetIndex: index,
|
|
76
77
|
},
|