plugin-document-understanding 1.1.14 → 1.1.15
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/README.md +29 -29
- package/client-v2.d.ts +2 -2
- package/client-v2.js +1 -1
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +8 -7
- package/dist/locale/en-US.json +3 -1
- package/dist/locale/zh-CN.json +3 -1
- package/dist/node_modules/form-data/lib/form_data.js +2 -2
- package/dist/node_modules/form-data/node_modules/es-define-property/.eslintrc +13 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/.nycrc +9 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/index.js +14 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/package.json +81 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/test/index.js +56 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/.eslintrc +16 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/RequireObjectCoercible.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/RequireObjectCoercible.js +11 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/ToObject.d.ts +7 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/ToObject.js +10 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/index.js +4 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/isObject.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/isObject.js +6 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/package.json +80 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/test/index.js +38 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/.eslintrc +13 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/.nycrc +9 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/index.d.ts +10 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/index.js +35 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/.eslintrc +5 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/.nycrc +13 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/index.js +8 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/package.json +92 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/package.json +78 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/test/index.js +85 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/.eslintrc +42 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/.nycrc +9 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/index.js +378 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/.eslintrc +11 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/.nycrc +9 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/index.js +14 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/package.json +111 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/shams.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/shams.js +45 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/test/index.js +22 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/test/shams/core-js.js +29 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/test/shams/get-own-property-symbols.js +29 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/test/tests.js +58 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/.eslintrc +5 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/.nycrc +13 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/index.js +8 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/package.json +92 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/package.json +97 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/test/GetIntrinsic.js +274 -0
- package/dist/node_modules/form-data/node_modules/gopd/.eslintrc +16 -0
- package/dist/node_modules/form-data/node_modules/gopd/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/gopd/gOPD.d.ts +1 -0
- package/dist/node_modules/form-data/node_modules/gopd/gOPD.js +4 -0
- package/dist/node_modules/form-data/node_modules/gopd/index.d.ts +5 -0
- package/dist/node_modules/form-data/node_modules/gopd/index.js +15 -0
- package/dist/node_modules/form-data/node_modules/gopd/package.json +77 -0
- package/dist/node_modules/form-data/node_modules/gopd/test/index.js +36 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/.eslintrc +5 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/.nycrc +13 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/index.js +8 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/package.json +108 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/shams.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/shams.js +8 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/test/index.js +21 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/test/shams/core-js.js +31 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/test/shams/get-own-property-symbols.js +30 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/test/tests.js +15 -0
- package/dist/node_modules/form-data/node_modules/hasown/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/hasown/.nycrc +13 -0
- package/dist/node_modules/form-data/node_modules/hasown/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/hasown/index.js +8 -0
- package/dist/node_modules/form-data/node_modules/hasown/package.json +91 -0
- package/dist/node_modules/form-data/package.json +1 -1
- package/dist/node_modules/mime-types/index.js +3 -3
- package/dist/node_modules/mime-types/package.json +1 -1
- package/dist/server/actions.js +45 -35
- package/package.json +57 -46
- package/src/client/index.tsx +15 -15
- package/src/client-v2/components/EndpointsTab.tsx +578 -578
- package/src/client-v2/components/JobsTab.tsx +294 -294
- package/src/client-v2/components/PipelinesTab.tsx +962 -962
- package/src/client-v2/components/PluginSettings.tsx +50 -50
- package/src/client-v2/components/ServiceConfigTab.tsx +139 -139
- package/src/client-v2/index.tsx +1 -1
- package/src/client-v2/locale.ts +21 -21
- package/src/client-v2/models/index.ts +12 -12
- package/src/client-v2/plugin.tsx +31 -21
- package/src/client-v2/types.ts +32 -32
- package/src/index.ts +2 -2
- package/src/locale/en-US.json +3 -1
- package/src/locale/zh-CN.json +3 -1
- package/src/server/__tests__/actions.test.ts +121 -98
- package/src/server/__tests__/cluster-safety.test.ts +259 -259
- package/src/server/__tests__/document-understanding-service.test.ts +82 -82
- package/src/server/__tests__/dugate-integration.test.ts +221 -221
- package/src/server/__tests__/smoke.test.ts +78 -55
- package/src/server/__tests__/upload-security.test.ts +128 -0
- package/src/server/actions.ts +53 -51
- package/src/server/collections/config.ts +18 -18
- package/src/server/collections/endpoints.ts +33 -33
- package/src/server/collections/jobs.ts +42 -42
- package/src/server/collections/pipeline-steps.ts +27 -27
- package/src/server/collections/pipelines.ts +19 -19
- package/src/server/dugate-seed.ts +491 -491
- package/src/server/index.ts +1 -1
- package/src/server/plugin.ts +143 -143
- package/src/server/services/AsyncJobManager.ts +238 -238
- package/src/server/services/DocumentUnderstandingService.ts +332 -332
- package/src/server/services/ExternalApiClient.ts +178 -178
- package/src/server/services/PipelineExecutor.ts +593 -593
- package/src/server/tools/document-understanding-tool.ts +181 -181
- package/src/server/types.ts +90 -90
|
@@ -1,55 +1,78 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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, type MockServer } from '@nocobase/test';
|
|
11
|
+
import PluginDocumentUnderstandingServer from '../plugin';
|
|
12
|
+
|
|
13
|
+
describe('Document Understanding plugin smoke', () => {
|
|
14
|
+
let app: MockServer;
|
|
15
|
+
let agent: ReturnType<MockServer['agent']>;
|
|
16
|
+
let plugin: PluginDocumentUnderstandingServer;
|
|
17
|
+
|
|
18
|
+
beforeEach(async () => {
|
|
19
|
+
app = await createMockServer({
|
|
20
|
+
plugins: [
|
|
21
|
+
'field-sort',
|
|
22
|
+
'system-settings',
|
|
23
|
+
'users',
|
|
24
|
+
'auth',
|
|
25
|
+
'acl',
|
|
26
|
+
'data-source-manager',
|
|
27
|
+
'data-source-main',
|
|
28
|
+
'error-handler',
|
|
29
|
+
],
|
|
30
|
+
acl: false,
|
|
31
|
+
});
|
|
32
|
+
plugin = new PluginDocumentUnderstandingServer(app, { name: 'plugin-document-understanding' });
|
|
33
|
+
await plugin.beforeLoad();
|
|
34
|
+
await plugin.load();
|
|
35
|
+
await app.db.sync();
|
|
36
|
+
|
|
37
|
+
const adminUser = await app.db.getRepository('users').findOne();
|
|
38
|
+
agent = app.agent();
|
|
39
|
+
if (adminUser) {
|
|
40
|
+
await agent.login(adminUser);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
afterEach(async () => {
|
|
45
|
+
await plugin?.beforeStop();
|
|
46
|
+
await app?.destroy();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('registers collections', async () => {
|
|
50
|
+
for (const name of [
|
|
51
|
+
'doc_understanding_config',
|
|
52
|
+
'doc_understanding_endpoints',
|
|
53
|
+
'doc_understanding_pipelines',
|
|
54
|
+
'doc_understanding_pipeline_steps',
|
|
55
|
+
'doc_understanding_jobs',
|
|
56
|
+
]) {
|
|
57
|
+
expect(app.db.getCollection(name)).toBeTruthy();
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('returns and updates default config through actions', async () => {
|
|
62
|
+
const getRes = await agent.resource('docUnderstanding').getConfig();
|
|
63
|
+
expect(getRes.status).toBe(200);
|
|
64
|
+
expect(getRes.body.data.baseUrl).toBe('http://localhost:8000');
|
|
65
|
+
expect(getRes.body.data.authType).toBe('none');
|
|
66
|
+
|
|
67
|
+
const updateRes = await agent.resource('docUnderstanding').updateConfig({
|
|
68
|
+
values: {
|
|
69
|
+
baseUrl: 'https://processor.example.com',
|
|
70
|
+
authType: 'bearer',
|
|
71
|
+
authKey: 'test-token',
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
expect(updateRes.status).toBe(200);
|
|
75
|
+
expect(updateRes.body.data.baseUrl).toBe('https://processor.example.com');
|
|
76
|
+
expect(updateRes.body.data.authType).toBe('bearer');
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -0,0 +1,128 @@
|
|
|
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, type MockServer } from '@nocobase/test';
|
|
11
|
+
import { afterAll, beforeAll, beforeEach, expect, it, vi } from 'vitest';
|
|
12
|
+
import Plugin from '../plugin';
|
|
13
|
+
|
|
14
|
+
let app: MockServer;
|
|
15
|
+
let reader: ReturnType<MockServer['agent']>;
|
|
16
|
+
let plugin: Plugin;
|
|
17
|
+
const execute = vi.fn().mockResolvedValue({ jobId: 1 });
|
|
18
|
+
|
|
19
|
+
beforeAll(async () => {
|
|
20
|
+
app = await createMockServer({
|
|
21
|
+
acl: true,
|
|
22
|
+
plugins: [
|
|
23
|
+
'field-sort',
|
|
24
|
+
'system-settings',
|
|
25
|
+
'users',
|
|
26
|
+
'auth',
|
|
27
|
+
'acl',
|
|
28
|
+
'data-source-manager',
|
|
29
|
+
'data-source-main',
|
|
30
|
+
'error-handler',
|
|
31
|
+
],
|
|
32
|
+
});
|
|
33
|
+
plugin = new Plugin(app, { name: 'plugin-document-understanding' });
|
|
34
|
+
await plugin.beforeLoad();
|
|
35
|
+
await plugin.load();
|
|
36
|
+
plugin.service.executePipeline = execute;
|
|
37
|
+
await app.db.getRepository('roles').create({ values: { name: 'pipeline-runner', title: 'Runner' } });
|
|
38
|
+
const user = await app.db
|
|
39
|
+
.getRepository('users')
|
|
40
|
+
.create({ values: { username: 'pipeline-runner', roles: ['pipeline-runner'] } });
|
|
41
|
+
reader = await app.agent().login(user, 'pipeline-runner');
|
|
42
|
+
}, 120000);
|
|
43
|
+
beforeEach(() => {
|
|
44
|
+
execute.mockClear();
|
|
45
|
+
app.acl.define({ role: 'pipeline-runner', actions: { 'docUnderstanding:executePipeline': {} } });
|
|
46
|
+
});
|
|
47
|
+
afterAll(async () => {
|
|
48
|
+
await app?.destroy();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it.each(['path', 'filepath'])('rejects JSON that supplies a local %s instead of an upload', async (key) => {
|
|
52
|
+
const response = await reader
|
|
53
|
+
.post('/docUnderstanding:executePipeline')
|
|
54
|
+
.send({ pipelineId: 1, files: { document: { [key]: __filename } } });
|
|
55
|
+
expect(response.status).toBe(400);
|
|
56
|
+
expect(execute).not.toHaveBeenCalled();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('preserves JSON pipeline execution without files', async () => {
|
|
60
|
+
const response = await reader
|
|
61
|
+
.post('/docUnderstanding:executePipeline')
|
|
62
|
+
.send({ pipelineId: 1, input: { text: 'Document' } });
|
|
63
|
+
expect(response.status).toBe(200);
|
|
64
|
+
expect(execute).toHaveBeenCalledWith(1, { text: 'Document' }, [], expect.any(Number));
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('accepts bytes from the actual multipart parser and parses input JSON', async () => {
|
|
68
|
+
const response = await reader
|
|
69
|
+
.post('/docUnderstanding:executePipeline')
|
|
70
|
+
.field('pipelineId', '2')
|
|
71
|
+
.field('input', '{"language":"en"}')
|
|
72
|
+
.attach('document', Buffer.from('UPLOADED_DOCUMENT_BYTES'), {
|
|
73
|
+
filename: 'document.txt',
|
|
74
|
+
contentType: 'text/plain',
|
|
75
|
+
});
|
|
76
|
+
expect(response.status).toBe(200);
|
|
77
|
+
expect(execute).toHaveBeenCalledWith(
|
|
78
|
+
2,
|
|
79
|
+
{ language: 'en' },
|
|
80
|
+
[
|
|
81
|
+
{
|
|
82
|
+
fieldName: 'document',
|
|
83
|
+
filename: 'document.txt',
|
|
84
|
+
mimeType: 'text/plain',
|
|
85
|
+
buffer: Buffer.from('UPLOADED_DOCUMENT_BYTES'),
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
expect.any(Number),
|
|
89
|
+
);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('rejects malformed multipart input without starting a pipeline', async () => {
|
|
93
|
+
const response = await reader
|
|
94
|
+
.post('/docUnderstanding:executePipeline')
|
|
95
|
+
.field('pipelineId', '1')
|
|
96
|
+
.field('input', '{invalid');
|
|
97
|
+
expect(response.status).toBe(400);
|
|
98
|
+
expect(execute).not.toHaveBeenCalled();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('enforces upload count and size limits', async () => {
|
|
102
|
+
const request = reader.post('/docUnderstanding:executePipeline').field('pipelineId', '1');
|
|
103
|
+
for (let i = 0; i < 6; i++) request.attach('document', Buffer.from('x'), `document-${i}.txt`);
|
|
104
|
+
expect((await request).status).toBe(413);
|
|
105
|
+
const oversized = await reader
|
|
106
|
+
.post('/docUnderstanding:executePipeline')
|
|
107
|
+
.field('pipelineId', '1')
|
|
108
|
+
.attach('document', Buffer.alloc(20 * 1024 * 1024 + 1), 'large.txt');
|
|
109
|
+
expect(oversized.status).toBe(413);
|
|
110
|
+
expect(execute).not.toHaveBeenCalled();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('accepts exactly five files together with both supported text fields', async () => {
|
|
114
|
+
const request = reader.post('/docUnderstanding:executePipeline').field('pipelineId', '1').field('input', '{}');
|
|
115
|
+
for (let i = 0; i < 5; i++) request.attach('document', Buffer.from('x'), `document-${i}.txt`);
|
|
116
|
+
expect((await request).status).toBe(200);
|
|
117
|
+
expect(execute.mock.calls[0][2]).toHaveLength(5);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('requires the resource grant before processing uploads', async () => {
|
|
121
|
+
app.acl.define({ role: 'pipeline-runner', actions: {} });
|
|
122
|
+
const response = await reader
|
|
123
|
+
.post('/docUnderstanding:executePipeline')
|
|
124
|
+
.field('pipelineId', '1')
|
|
125
|
+
.attach('document', Buffer.from('PRIVATE'), 'private.txt');
|
|
126
|
+
expect(response.status).toBe(403);
|
|
127
|
+
expect(execute).not.toHaveBeenCalled();
|
|
128
|
+
});
|
package/src/server/actions.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
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
|
+
|
|
1
10
|
import type { Context } from '@nocobase/actions';
|
|
2
|
-
import {
|
|
11
|
+
import { koaMulter } from '@nocobase/utils';
|
|
3
12
|
import { DocumentUnderstandingService } from './services/DocumentUnderstandingService';
|
|
4
13
|
import { FileInput } from './services/ExternalApiClient';
|
|
5
14
|
|
|
@@ -14,21 +23,14 @@ interface DocumentUnderstandingPluginLike {
|
|
|
14
23
|
};
|
|
15
24
|
}
|
|
16
25
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
type?: string;
|
|
23
|
-
mimetype?: string;
|
|
24
|
-
}
|
|
26
|
+
// Parse uploads after resource ACL. Memory storage never accepts or opens a client-supplied local path.
|
|
27
|
+
const uploadDocuments = koaMulter({
|
|
28
|
+
storage: koaMulter.memoryStorage(),
|
|
29
|
+
limits: { files: 5, fileSize: 20 * 1024 * 1024, fields: 2, fieldSize: 1024 * 1024, parts: 8 },
|
|
30
|
+
}).any();
|
|
25
31
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
body?: {
|
|
29
|
-
files?: Record<string, UploadedFileLike | UploadedFileLike[]>;
|
|
30
|
-
};
|
|
31
|
-
headers: Context['request']['headers'];
|
|
32
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
33
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
export const defineActions = (plugin: DocumentUnderstandingPluginLike) => {
|
|
@@ -98,47 +100,47 @@ export const defineActions = (plugin: DocumentUnderstandingPluginLike) => {
|
|
|
98
100
|
},
|
|
99
101
|
|
|
100
102
|
async executePipeline(ctx: Context, next: () => Promise<any>) {
|
|
101
|
-
|
|
102
|
-
const pipelineId = values?.pipelineId;
|
|
103
|
-
const input = values?.input || {};
|
|
104
|
-
const userId = ctx.state?.currentUser?.id;
|
|
105
|
-
|
|
103
|
+
let values: unknown = ctx.action.params.values;
|
|
106
104
|
const multipartFiles: FileInput[] = [];
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
mimeType,
|
|
121
|
-
});
|
|
122
|
-
} catch (err) {
|
|
123
|
-
plugin.app.logger.warn('Failed to read uploaded multipart file', err);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
if (typeof filesObj === 'object') {
|
|
129
|
-
for (const [key, value] of Object.entries(filesObj)) {
|
|
130
|
-
if (Array.isArray(value)) {
|
|
131
|
-
for (const f of value) {
|
|
132
|
-
addFile(key, f);
|
|
133
|
-
}
|
|
134
|
-
} else {
|
|
135
|
-
addFile(key, value);
|
|
105
|
+
if (ctx.request.is('multipart/form-data')) {
|
|
106
|
+
try {
|
|
107
|
+
await uploadDocuments(ctx, async () => undefined);
|
|
108
|
+
} catch (error: unknown) {
|
|
109
|
+
const limited = isRecord(error) && typeof error.code === 'string' && error.code.startsWith('LIMIT_');
|
|
110
|
+
ctx.throw(limited ? 413 : 400, ctx.t('Invalid document upload.', { ns: 'plugin-document-understanding' }));
|
|
111
|
+
}
|
|
112
|
+
values = ctx.request.body;
|
|
113
|
+
const files: unknown = (ctx.request as typeof ctx.request & { files?: unknown }).files;
|
|
114
|
+
if (Array.isArray(files)) {
|
|
115
|
+
for (const file of files) {
|
|
116
|
+
if (!isRecord(file) || !Buffer.isBuffer(file.buffer)) {
|
|
117
|
+
ctx.throw(400, ctx.t('Invalid document upload.', { ns: 'plugin-document-understanding' }));
|
|
136
118
|
}
|
|
119
|
+
multipartFiles.push({
|
|
120
|
+
fieldName: String(file.fieldname),
|
|
121
|
+
buffer: file.buffer,
|
|
122
|
+
filename: String(file.originalname),
|
|
123
|
+
mimeType: String(file.mimetype),
|
|
124
|
+
});
|
|
137
125
|
}
|
|
138
126
|
}
|
|
139
127
|
}
|
|
140
|
-
|
|
141
|
-
|
|
128
|
+
if (!isRecord(values) || Object.prototype.hasOwnProperty.call(values, 'files')) {
|
|
129
|
+
ctx.throw(400, ctx.t('Invalid pipeline input.', { ns: 'plugin-document-understanding' }));
|
|
130
|
+
}
|
|
131
|
+
const pipelineId = Number(values.pipelineId);
|
|
132
|
+
let input: unknown = values.input ?? {};
|
|
133
|
+
if (ctx.request.is('multipart/form-data') && typeof input === 'string') {
|
|
134
|
+
try {
|
|
135
|
+
input = JSON.parse(input);
|
|
136
|
+
} catch {
|
|
137
|
+
ctx.throw(400, ctx.t('Invalid pipeline input.', { ns: 'plugin-document-understanding' }));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (!Number.isSafeInteger(pipelineId) || pipelineId <= 0 || !isRecord(input)) {
|
|
141
|
+
ctx.throw(400, ctx.t('Invalid pipeline input.', { ns: 'plugin-document-understanding' }));
|
|
142
|
+
}
|
|
143
|
+
ctx.body = await service.executePipeline(pipelineId, input, multipartFiles, ctx.state?.currentUser?.id);
|
|
142
144
|
await next();
|
|
143
145
|
},
|
|
144
146
|
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { CollectionOptions } from '@nocobase/database';
|
|
2
|
-
|
|
3
|
-
export default {
|
|
4
|
-
name: 'doc_understanding_config',
|
|
5
|
-
title: 'Document Understanding Config',
|
|
6
|
-
// Model name can be explicitly generated by NocoBase, but we define collection fields.
|
|
7
|
-
fields: [
|
|
8
|
-
{ type: 'string', name: 'baseUrl', length: 500 },
|
|
9
|
-
{ type: 'string', name: 'authType', length: 20 }, // 'api_key' | 'bearer' | 'custom_header' | 'none'
|
|
10
|
-
{ type: 'string', name: 'authKey', length: 500 },
|
|
11
|
-
{ type: 'string', name: 'authHeaderName', length: 100 },
|
|
12
|
-
{ type: 'integer', name: 'defaultTimeout', defaultValue: 30000 },
|
|
13
|
-
{ type: 'integer', name: 'defaultRetries', defaultValue: 2 },
|
|
14
|
-
{ type: 'integer', name: 'pollInterval', defaultValue: 5000 },
|
|
15
|
-
{ type: 'integer', name: 'pollTimeout', defaultValue: 300000 },
|
|
16
|
-
{ type: 'string', name: 'webhookSecret', length: 500 },
|
|
17
|
-
],
|
|
18
|
-
} as CollectionOptions;
|
|
1
|
+
import { CollectionOptions } from '@nocobase/database';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
name: 'doc_understanding_config',
|
|
5
|
+
title: 'Document Understanding Config',
|
|
6
|
+
// Model name can be explicitly generated by NocoBase, but we define collection fields.
|
|
7
|
+
fields: [
|
|
8
|
+
{ type: 'string', name: 'baseUrl', length: 500 },
|
|
9
|
+
{ type: 'string', name: 'authType', length: 20 }, // 'api_key' | 'bearer' | 'custom_header' | 'none'
|
|
10
|
+
{ type: 'string', name: 'authKey', length: 500 },
|
|
11
|
+
{ type: 'string', name: 'authHeaderName', length: 100 },
|
|
12
|
+
{ type: 'integer', name: 'defaultTimeout', defaultValue: 30000 },
|
|
13
|
+
{ type: 'integer', name: 'defaultRetries', defaultValue: 2 },
|
|
14
|
+
{ type: 'integer', name: 'pollInterval', defaultValue: 5000 },
|
|
15
|
+
{ type: 'integer', name: 'pollTimeout', defaultValue: 300000 },
|
|
16
|
+
{ type: 'string', name: 'webhookSecret', length: 500 },
|
|
17
|
+
],
|
|
18
|
+
} as CollectionOptions;
|
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import { CollectionOptions } from '@nocobase/database';
|
|
2
|
-
|
|
3
|
-
export default {
|
|
4
|
-
name: 'doc_understanding_endpoints',
|
|
5
|
-
title: 'Document Endpoints',
|
|
6
|
-
fields: [
|
|
7
|
-
{ type: 'string', name: 'name', unique: true, length: 100 },
|
|
8
|
-
{ type: 'string', name: 'subpath', length: 500 },
|
|
9
|
-
{ type: 'string', name: 'method', length: 10 },
|
|
10
|
-
{ type: 'text', name: 'description' },
|
|
11
|
-
{ type: 'json', name: 'requestBodySchema' },
|
|
12
|
-
{ type: 'json', name: 'responseSchema' },
|
|
13
|
-
{ type: 'string', name: 'fileInputMode', length: 20 },
|
|
14
|
-
{ type: 'string', name: 'fileFieldName', length: 100, defaultValue: 'file' },
|
|
15
|
-
{ type: 'integer', name: 'maxFiles', defaultValue: 1 },
|
|
16
|
-
{ type: 'string', name: 'executionMode', length: 20 },
|
|
17
|
-
{ type: 'string', name: 'discriminatorField', length: 50 },
|
|
18
|
-
{ type: 'string', name: 'discriminatorValue', length: 100 },
|
|
19
|
-
{ type: 'string', name: 'syncQueryParam', length: 50 },
|
|
20
|
-
{ type: 'string', name: 'taskIdExtractPath', length: 200 },
|
|
21
|
-
{ type: 'string', name: 'taskIdExtractRegex', length: 200 },
|
|
22
|
-
{ type: 'string', name: 'pollResultSubpath', length: 500 },
|
|
23
|
-
{ type: 'string', name: 'pollTaskIdField', length: 100, defaultValue: 'task_id' },
|
|
24
|
-
{ type: 'string', name: 'pollResultField', length: 100 },
|
|
25
|
-
{ type: 'integer', name: 'pollInterval' },
|
|
26
|
-
{ type: 'integer', name: 'pollTimeout' },
|
|
27
|
-
{ type: 'string', name: 'pollStatusField', length: 100 },
|
|
28
|
-
{ type: 'string', name: 'pollCompletedValue', length: 100, defaultValue: 'completed' },
|
|
29
|
-
{ type: 'json', name: 'customHeaders' },
|
|
30
|
-
{ type: 'boolean', name: 'enabled', defaultValue: true },
|
|
31
|
-
{ type: 'integer', name: 'sortOrder', defaultValue: 0 },
|
|
32
|
-
],
|
|
33
|
-
} as CollectionOptions;
|
|
1
|
+
import { CollectionOptions } from '@nocobase/database';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
name: 'doc_understanding_endpoints',
|
|
5
|
+
title: 'Document Endpoints',
|
|
6
|
+
fields: [
|
|
7
|
+
{ type: 'string', name: 'name', unique: true, length: 100 },
|
|
8
|
+
{ type: 'string', name: 'subpath', length: 500 },
|
|
9
|
+
{ type: 'string', name: 'method', length: 10 },
|
|
10
|
+
{ type: 'text', name: 'description' },
|
|
11
|
+
{ type: 'json', name: 'requestBodySchema' },
|
|
12
|
+
{ type: 'json', name: 'responseSchema' },
|
|
13
|
+
{ type: 'string', name: 'fileInputMode', length: 20 },
|
|
14
|
+
{ type: 'string', name: 'fileFieldName', length: 100, defaultValue: 'file' },
|
|
15
|
+
{ type: 'integer', name: 'maxFiles', defaultValue: 1 },
|
|
16
|
+
{ type: 'string', name: 'executionMode', length: 20 },
|
|
17
|
+
{ type: 'string', name: 'discriminatorField', length: 50 },
|
|
18
|
+
{ type: 'string', name: 'discriminatorValue', length: 100 },
|
|
19
|
+
{ type: 'string', name: 'syncQueryParam', length: 50 },
|
|
20
|
+
{ type: 'string', name: 'taskIdExtractPath', length: 200 },
|
|
21
|
+
{ type: 'string', name: 'taskIdExtractRegex', length: 200 },
|
|
22
|
+
{ type: 'string', name: 'pollResultSubpath', length: 500 },
|
|
23
|
+
{ type: 'string', name: 'pollTaskIdField', length: 100, defaultValue: 'task_id' },
|
|
24
|
+
{ type: 'string', name: 'pollResultField', length: 100 },
|
|
25
|
+
{ type: 'integer', name: 'pollInterval' },
|
|
26
|
+
{ type: 'integer', name: 'pollTimeout' },
|
|
27
|
+
{ type: 'string', name: 'pollStatusField', length: 100 },
|
|
28
|
+
{ type: 'string', name: 'pollCompletedValue', length: 100, defaultValue: 'completed' },
|
|
29
|
+
{ type: 'json', name: 'customHeaders' },
|
|
30
|
+
{ type: 'boolean', name: 'enabled', defaultValue: true },
|
|
31
|
+
{ type: 'integer', name: 'sortOrder', defaultValue: 0 },
|
|
32
|
+
],
|
|
33
|
+
} as CollectionOptions;
|
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
import { CollectionOptions } from '@nocobase/database';
|
|
2
|
-
|
|
3
|
-
export default {
|
|
4
|
-
name: 'doc_understanding_jobs',
|
|
5
|
-
title: 'Document Pipeline Jobs',
|
|
6
|
-
fields: [
|
|
7
|
-
{
|
|
8
|
-
type: 'belongsTo',
|
|
9
|
-
name: 'pipeline',
|
|
10
|
-
target: 'doc_understanding_pipelines',
|
|
11
|
-
foreignKey: 'pipelineId',
|
|
12
|
-
},
|
|
13
|
-
{ type: 'string', name: 'status', length: 20, defaultValue: 'pending' },
|
|
14
|
-
{ type: 'json', name: 'input' },
|
|
15
|
-
{ type: 'integer', name: 'currentStep' },
|
|
16
|
-
{ type: 'json', name: 'stepResults' },
|
|
17
|
-
{ type: 'json', name: 'finalResult' },
|
|
18
|
-
{ type: 'text', name: 'error' },
|
|
19
|
-
{ type: 'json', name: 'externalTaskIds' },
|
|
20
|
-
{ type: 'date', name: 'startedAt' },
|
|
21
|
-
{ type: 'date', name: 'completedAt' },
|
|
22
|
-
// Cluster ownership: which node is executing/polling this job, and until when
|
|
23
|
-
// its lease is valid. Orphaned jobs (owner restarted or lease expired) are
|
|
24
|
-
// failed or re-adopted by the maintenance sweep.
|
|
25
|
-
{ type: 'string', name: 'ownedBy', length: 255 },
|
|
26
|
-
{ type: 'date', name: 'leaseExpiresAt' },
|
|
27
|
-
{
|
|
28
|
-
type: 'belongsTo',
|
|
29
|
-
name: 'createdBy',
|
|
30
|
-
target: 'users',
|
|
31
|
-
foreignKey: 'createdById',
|
|
32
|
-
},
|
|
33
|
-
],
|
|
34
|
-
indexes: [
|
|
35
|
-
{
|
|
36
|
-
fields: ['status'],
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
fields: ['leaseExpiresAt'],
|
|
40
|
-
},
|
|
41
|
-
],
|
|
42
|
-
} as CollectionOptions;
|
|
1
|
+
import { CollectionOptions } from '@nocobase/database';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
name: 'doc_understanding_jobs',
|
|
5
|
+
title: 'Document Pipeline Jobs',
|
|
6
|
+
fields: [
|
|
7
|
+
{
|
|
8
|
+
type: 'belongsTo',
|
|
9
|
+
name: 'pipeline',
|
|
10
|
+
target: 'doc_understanding_pipelines',
|
|
11
|
+
foreignKey: 'pipelineId',
|
|
12
|
+
},
|
|
13
|
+
{ type: 'string', name: 'status', length: 20, defaultValue: 'pending' },
|
|
14
|
+
{ type: 'json', name: 'input' },
|
|
15
|
+
{ type: 'integer', name: 'currentStep' },
|
|
16
|
+
{ type: 'json', name: 'stepResults' },
|
|
17
|
+
{ type: 'json', name: 'finalResult' },
|
|
18
|
+
{ type: 'text', name: 'error' },
|
|
19
|
+
{ type: 'json', name: 'externalTaskIds' },
|
|
20
|
+
{ type: 'date', name: 'startedAt' },
|
|
21
|
+
{ type: 'date', name: 'completedAt' },
|
|
22
|
+
// Cluster ownership: which node is executing/polling this job, and until when
|
|
23
|
+
// its lease is valid. Orphaned jobs (owner restarted or lease expired) are
|
|
24
|
+
// failed or re-adopted by the maintenance sweep.
|
|
25
|
+
{ type: 'string', name: 'ownedBy', length: 255 },
|
|
26
|
+
{ type: 'date', name: 'leaseExpiresAt' },
|
|
27
|
+
{
|
|
28
|
+
type: 'belongsTo',
|
|
29
|
+
name: 'createdBy',
|
|
30
|
+
target: 'users',
|
|
31
|
+
foreignKey: 'createdById',
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
indexes: [
|
|
35
|
+
{
|
|
36
|
+
fields: ['status'],
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
fields: ['leaseExpiresAt'],
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
} as CollectionOptions;
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { CollectionOptions } from '@nocobase/database';
|
|
2
|
-
|
|
3
|
-
export default {
|
|
4
|
-
name: 'doc_understanding_pipeline_steps',
|
|
5
|
-
title: 'Pipeline Steps',
|
|
6
|
-
fields: [
|
|
7
|
-
{
|
|
8
|
-
type: 'belongsTo',
|
|
9
|
-
name: 'pipeline',
|
|
10
|
-
target: 'doc_understanding_pipelines',
|
|
11
|
-
foreignKey: 'pipelineId',
|
|
12
|
-
},
|
|
13
|
-
{
|
|
14
|
-
type: 'belongsTo',
|
|
15
|
-
name: 'endpoint',
|
|
16
|
-
target: 'doc_understanding_endpoints',
|
|
17
|
-
foreignKey: 'endpointId',
|
|
18
|
-
},
|
|
19
|
-
{ type: 'integer', name: 'stepOrder' },
|
|
20
|
-
{ type: 'string', name: 'name', length: 100 },
|
|
21
|
-
{ type: 'json', name: 'inputMapping' },
|
|
22
|
-
{ type: 'string', name: 'outputAlias', length: 100 },
|
|
23
|
-
{ type: 'json', name: 'condition' },
|
|
24
|
-
{ type: 'string', name: 'onError', length: 20, defaultValue: 'fail' },
|
|
25
|
-
{ type: 'integer', name: 'retryCount', defaultValue: 0 },
|
|
26
|
-
],
|
|
27
|
-
} as CollectionOptions;
|
|
1
|
+
import { CollectionOptions } from '@nocobase/database';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
name: 'doc_understanding_pipeline_steps',
|
|
5
|
+
title: 'Pipeline Steps',
|
|
6
|
+
fields: [
|
|
7
|
+
{
|
|
8
|
+
type: 'belongsTo',
|
|
9
|
+
name: 'pipeline',
|
|
10
|
+
target: 'doc_understanding_pipelines',
|
|
11
|
+
foreignKey: 'pipelineId',
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
type: 'belongsTo',
|
|
15
|
+
name: 'endpoint',
|
|
16
|
+
target: 'doc_understanding_endpoints',
|
|
17
|
+
foreignKey: 'endpointId',
|
|
18
|
+
},
|
|
19
|
+
{ type: 'integer', name: 'stepOrder' },
|
|
20
|
+
{ type: 'string', name: 'name', length: 100 },
|
|
21
|
+
{ type: 'json', name: 'inputMapping' },
|
|
22
|
+
{ type: 'string', name: 'outputAlias', length: 100 },
|
|
23
|
+
{ type: 'json', name: 'condition' },
|
|
24
|
+
{ type: 'string', name: 'onError', length: 20, defaultValue: 'fail' },
|
|
25
|
+
{ type: 'integer', name: 'retryCount', defaultValue: 0 },
|
|
26
|
+
],
|
|
27
|
+
} as CollectionOptions;
|