plugin-document-understanding 1.1.9 → 1.1.12
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/dist/client/345.17f88ffb846ec69b.js +10 -0
- package/dist/client/index.js +1 -1
- package/dist/client-v2/345.1e0c321d30702aa1.js +10 -0
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +7 -7
- package/dist/locale/en-US.json +171 -121
- package/dist/locale/vi-VN.json +171 -121
- package/dist/locale/zh-CN.json +171 -1
- package/dist/node_modules/form-data/lib/form_data.js +3 -3
- package/dist/node_modules/form-data/package.json +1 -1
- package/dist/server/collections/jobs.js +13 -0
- package/dist/server/services/AsyncJobManager.js +87 -31
- package/dist/server/services/DocumentUnderstandingService.js +48 -5
- package/dist/server/services/PipelineExecutor.js +66 -29
- package/package.json +46 -49
- package/src/client/index.tsx +7 -1
- package/src/client-v2/components/EndpointsTab.tsx +177 -108
- package/src/client-v2/components/JobsTab.tsx +94 -59
- package/src/client-v2/components/PipelinesTab.tsx +244 -154
- package/src/client-v2/components/PluginSettings.tsx +2 -0
- package/src/client-v2/components/ServiceConfigTab.tsx +56 -39
- package/src/client-v2/types.ts +32 -0
- package/src/locale/en-US.json +171 -121
- package/src/locale/vi-VN.json +171 -121
- package/src/locale/zh-CN.json +171 -1
- package/src/server/__tests__/cluster-safety.test.ts +259 -0
- package/src/server/__tests__/document-understanding-service.test.ts +13 -2
- package/src/server/__tests__/pipeline-executor.test.ts +55 -55
- package/src/server/actions.ts +176 -176
- package/src/server/collections/jobs.ts +13 -0
- package/src/server/plugin.ts +134 -134
- package/src/server/services/AsyncJobManager.ts +98 -32
- package/src/server/services/DocumentUnderstandingService.ts +52 -4
- package/src/server/services/PipelineExecutor.ts +79 -32
- package/src/server/types.ts +2 -0
- package/dist/client-v2/794.9b984a25b2fc7c14.js +0 -10
package/src/server/actions.ts
CHANGED
|
@@ -1,176 +1,176 @@
|
|
|
1
|
-
import type { Context } from '@nocobase/actions';
|
|
2
|
-
import { existsSync, readFileSync } from 'fs';
|
|
3
|
-
import { DocumentUnderstandingService } from './services/DocumentUnderstandingService';
|
|
4
|
-
import { FileInput } from './services/ExternalApiClient';
|
|
5
|
-
|
|
6
|
-
interface LoggerLike {
|
|
7
|
-
warn: (...args: unknown[]) => void;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
interface DocumentUnderstandingPluginLike {
|
|
11
|
-
service: DocumentUnderstandingService;
|
|
12
|
-
app: {
|
|
13
|
-
logger: LoggerLike;
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
interface UploadedFileLike {
|
|
18
|
-
path?: string;
|
|
19
|
-
filepath?: string;
|
|
20
|
-
name?: string;
|
|
21
|
-
originalFilename?: string;
|
|
22
|
-
type?: string;
|
|
23
|
-
mimetype?: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
interface RequestWithFiles {
|
|
27
|
-
files?: Record<string, UploadedFileLike | UploadedFileLike[]>;
|
|
28
|
-
body?: {
|
|
29
|
-
files?: Record<string, UploadedFileLike | UploadedFileLike[]>;
|
|
30
|
-
};
|
|
31
|
-
headers: Context['request']['headers'];
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export const defineActions = (plugin: DocumentUnderstandingPluginLike) => {
|
|
35
|
-
const service = plugin.service;
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
async getConfig(ctx: Context, next: () => Promise<any>) {
|
|
39
|
-
ctx.body = await service.getConfigForClient();
|
|
40
|
-
await next();
|
|
41
|
-
},
|
|
42
|
-
|
|
43
|
-
async updateConfig(ctx: Context, next: () => Promise<any>) {
|
|
44
|
-
const { values } = ctx.action.params;
|
|
45
|
-
await service.updateConfig(values);
|
|
46
|
-
ctx.body = await service.getConfig();
|
|
47
|
-
await next();
|
|
48
|
-
},
|
|
49
|
-
|
|
50
|
-
async listEndpoints(ctx: Context, next: () => Promise<any>) {
|
|
51
|
-
ctx.body = await service.listEndpoints();
|
|
52
|
-
await next();
|
|
53
|
-
},
|
|
54
|
-
|
|
55
|
-
async createEndpoint(ctx: Context, next: () => Promise<any>) {
|
|
56
|
-
const { values } = ctx.action.params;
|
|
57
|
-
ctx.body = await service.createEndpoint(values);
|
|
58
|
-
await next();
|
|
59
|
-
},
|
|
60
|
-
|
|
61
|
-
async updateEndpoint(ctx: Context, next: () => Promise<any>) {
|
|
62
|
-
const { filterByTk, values } = ctx.action.params;
|
|
63
|
-
await service.updateEndpoint(Number(filterByTk), values);
|
|
64
|
-
ctx.body = { message: 'ok' };
|
|
65
|
-
await next();
|
|
66
|
-
},
|
|
67
|
-
|
|
68
|
-
async deleteEndpoint(ctx: Context, next: () => Promise<any>) {
|
|
69
|
-
const { filterByTk } = ctx.action.params;
|
|
70
|
-
await service.deleteEndpoint(Number(filterByTk));
|
|
71
|
-
ctx.body = { message: 'ok' };
|
|
72
|
-
await next();
|
|
73
|
-
},
|
|
74
|
-
|
|
75
|
-
async listPipelines(ctx: Context, next: () => Promise<any>) {
|
|
76
|
-
ctx.body = await service.listPipelines();
|
|
77
|
-
await next();
|
|
78
|
-
},
|
|
79
|
-
|
|
80
|
-
async createPipeline(ctx: Context, next: () => Promise<any>) {
|
|
81
|
-
const { values } = ctx.action.params;
|
|
82
|
-
ctx.body = await service.createPipeline(values);
|
|
83
|
-
await next();
|
|
84
|
-
},
|
|
85
|
-
|
|
86
|
-
async updatePipeline(ctx: Context, next: () => Promise<any>) {
|
|
87
|
-
const { filterByTk, values } = ctx.action.params;
|
|
88
|
-
await service.updatePipeline(Number(filterByTk), values);
|
|
89
|
-
ctx.body = { message: 'ok' };
|
|
90
|
-
await next();
|
|
91
|
-
},
|
|
92
|
-
|
|
93
|
-
async deletePipeline(ctx: Context, next: () => Promise<any>) {
|
|
94
|
-
const { filterByTk } = ctx.action.params;
|
|
95
|
-
await service.deletePipeline(Number(filterByTk));
|
|
96
|
-
ctx.body = { message: 'ok' };
|
|
97
|
-
await next();
|
|
98
|
-
},
|
|
99
|
-
|
|
100
|
-
async executePipeline(ctx: Context, next: () => Promise<any>) {
|
|
101
|
-
const { values } = ctx.action.params;
|
|
102
|
-
const pipelineId = values?.pipelineId;
|
|
103
|
-
const input = values?.input || {};
|
|
104
|
-
const userId = ctx.state?.currentUser?.id;
|
|
105
|
-
|
|
106
|
-
const multipartFiles: FileInput[] = [];
|
|
107
|
-
const request = ctx.request as RequestWithFiles;
|
|
108
|
-
const filesObj = request.files || request.body?.files;
|
|
109
|
-
if (filesObj) {
|
|
110
|
-
const addFile = (key: string, file: UploadedFileLike) => {
|
|
111
|
-
const filePath = file.path || file.filepath;
|
|
112
|
-
const fileName = file.name || file.originalFilename || 'file';
|
|
113
|
-
const mimeType = file.type || file.mimetype || 'application/octet-stream';
|
|
114
|
-
if (filePath && existsSync(filePath)) {
|
|
115
|
-
try {
|
|
116
|
-
multipartFiles.push({
|
|
117
|
-
fieldName: key,
|
|
118
|
-
buffer: readFileSync(filePath),
|
|
119
|
-
filename: fileName,
|
|
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);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
ctx.body = await service.executePipeline(pipelineId, input, multipartFiles, userId);
|
|
142
|
-
await next();
|
|
143
|
-
},
|
|
144
|
-
|
|
145
|
-
async getJobStatus(ctx: Context, next: () => Promise<any>) {
|
|
146
|
-
const { filterByTk } = ctx.action.params;
|
|
147
|
-
ctx.body = await service.getJobStatus(Number(filterByTk));
|
|
148
|
-
await next();
|
|
149
|
-
},
|
|
150
|
-
|
|
151
|
-
async listJobs(ctx: Context, next: () => Promise<any>) {
|
|
152
|
-
const { filter } = ctx.action.params;
|
|
153
|
-
ctx.body = await service.listJobs(filter);
|
|
154
|
-
await next();
|
|
155
|
-
},
|
|
156
|
-
|
|
157
|
-
async webhookCallback(ctx: Context, next: () => Promise<any>) {
|
|
158
|
-
const signature = ctx.request.headers['x-webhook-signature'] as string;
|
|
159
|
-
const { values } = ctx.action.params;
|
|
160
|
-
try {
|
|
161
|
-
await service.handleWebhook(values || ctx.request.body, signature);
|
|
162
|
-
ctx.body = { received: true };
|
|
163
|
-
} catch (err: any) {
|
|
164
|
-
plugin.app.logger.warn(`Webhook callback error: ${err.message}`);
|
|
165
|
-
if (err.message.includes('signature')) {
|
|
166
|
-
ctx.status = 401;
|
|
167
|
-
ctx.body = { error: err.message };
|
|
168
|
-
} else {
|
|
169
|
-
ctx.status = 400;
|
|
170
|
-
ctx.body = { error: err.message };
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
await next();
|
|
174
|
-
},
|
|
175
|
-
};
|
|
176
|
-
};
|
|
1
|
+
import type { Context } from '@nocobase/actions';
|
|
2
|
+
import { existsSync, readFileSync } from 'fs';
|
|
3
|
+
import { DocumentUnderstandingService } from './services/DocumentUnderstandingService';
|
|
4
|
+
import { FileInput } from './services/ExternalApiClient';
|
|
5
|
+
|
|
6
|
+
interface LoggerLike {
|
|
7
|
+
warn: (...args: unknown[]) => void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface DocumentUnderstandingPluginLike {
|
|
11
|
+
service: DocumentUnderstandingService;
|
|
12
|
+
app: {
|
|
13
|
+
logger: LoggerLike;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface UploadedFileLike {
|
|
18
|
+
path?: string;
|
|
19
|
+
filepath?: string;
|
|
20
|
+
name?: string;
|
|
21
|
+
originalFilename?: string;
|
|
22
|
+
type?: string;
|
|
23
|
+
mimetype?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface RequestWithFiles {
|
|
27
|
+
files?: Record<string, UploadedFileLike | UploadedFileLike[]>;
|
|
28
|
+
body?: {
|
|
29
|
+
files?: Record<string, UploadedFileLike | UploadedFileLike[]>;
|
|
30
|
+
};
|
|
31
|
+
headers: Context['request']['headers'];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const defineActions = (plugin: DocumentUnderstandingPluginLike) => {
|
|
35
|
+
const service = plugin.service;
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
async getConfig(ctx: Context, next: () => Promise<any>) {
|
|
39
|
+
ctx.body = await service.getConfigForClient();
|
|
40
|
+
await next();
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
async updateConfig(ctx: Context, next: () => Promise<any>) {
|
|
44
|
+
const { values } = ctx.action.params;
|
|
45
|
+
await service.updateConfig(values);
|
|
46
|
+
ctx.body = await service.getConfig();
|
|
47
|
+
await next();
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
async listEndpoints(ctx: Context, next: () => Promise<any>) {
|
|
51
|
+
ctx.body = await service.listEndpoints();
|
|
52
|
+
await next();
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
async createEndpoint(ctx: Context, next: () => Promise<any>) {
|
|
56
|
+
const { values } = ctx.action.params;
|
|
57
|
+
ctx.body = await service.createEndpoint(values);
|
|
58
|
+
await next();
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
async updateEndpoint(ctx: Context, next: () => Promise<any>) {
|
|
62
|
+
const { filterByTk, values } = ctx.action.params;
|
|
63
|
+
await service.updateEndpoint(Number(filterByTk), values);
|
|
64
|
+
ctx.body = { message: 'ok' };
|
|
65
|
+
await next();
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
async deleteEndpoint(ctx: Context, next: () => Promise<any>) {
|
|
69
|
+
const { filterByTk } = ctx.action.params;
|
|
70
|
+
await service.deleteEndpoint(Number(filterByTk));
|
|
71
|
+
ctx.body = { message: 'ok' };
|
|
72
|
+
await next();
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
async listPipelines(ctx: Context, next: () => Promise<any>) {
|
|
76
|
+
ctx.body = await service.listPipelines();
|
|
77
|
+
await next();
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
async createPipeline(ctx: Context, next: () => Promise<any>) {
|
|
81
|
+
const { values } = ctx.action.params;
|
|
82
|
+
ctx.body = await service.createPipeline(values);
|
|
83
|
+
await next();
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
async updatePipeline(ctx: Context, next: () => Promise<any>) {
|
|
87
|
+
const { filterByTk, values } = ctx.action.params;
|
|
88
|
+
await service.updatePipeline(Number(filterByTk), values);
|
|
89
|
+
ctx.body = { message: 'ok' };
|
|
90
|
+
await next();
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
async deletePipeline(ctx: Context, next: () => Promise<any>) {
|
|
94
|
+
const { filterByTk } = ctx.action.params;
|
|
95
|
+
await service.deletePipeline(Number(filterByTk));
|
|
96
|
+
ctx.body = { message: 'ok' };
|
|
97
|
+
await next();
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
async executePipeline(ctx: Context, next: () => Promise<any>) {
|
|
101
|
+
const { values } = ctx.action.params;
|
|
102
|
+
const pipelineId = values?.pipelineId;
|
|
103
|
+
const input = values?.input || {};
|
|
104
|
+
const userId = ctx.state?.currentUser?.id;
|
|
105
|
+
|
|
106
|
+
const multipartFiles: FileInput[] = [];
|
|
107
|
+
const request = ctx.request as RequestWithFiles;
|
|
108
|
+
const filesObj = request.files || request.body?.files;
|
|
109
|
+
if (filesObj) {
|
|
110
|
+
const addFile = (key: string, file: UploadedFileLike) => {
|
|
111
|
+
const filePath = file.path || file.filepath;
|
|
112
|
+
const fileName = file.name || file.originalFilename || 'file';
|
|
113
|
+
const mimeType = file.type || file.mimetype || 'application/octet-stream';
|
|
114
|
+
if (filePath && existsSync(filePath)) {
|
|
115
|
+
try {
|
|
116
|
+
multipartFiles.push({
|
|
117
|
+
fieldName: key,
|
|
118
|
+
buffer: readFileSync(filePath),
|
|
119
|
+
filename: fileName,
|
|
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);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
ctx.body = await service.executePipeline(pipelineId, input, multipartFiles, userId);
|
|
142
|
+
await next();
|
|
143
|
+
},
|
|
144
|
+
|
|
145
|
+
async getJobStatus(ctx: Context, next: () => Promise<any>) {
|
|
146
|
+
const { filterByTk } = ctx.action.params;
|
|
147
|
+
ctx.body = await service.getJobStatus(Number(filterByTk));
|
|
148
|
+
await next();
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
async listJobs(ctx: Context, next: () => Promise<any>) {
|
|
152
|
+
const { filter } = ctx.action.params;
|
|
153
|
+
ctx.body = await service.listJobs(filter);
|
|
154
|
+
await next();
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
async webhookCallback(ctx: Context, next: () => Promise<any>) {
|
|
158
|
+
const signature = ctx.request.headers['x-webhook-signature'] as string;
|
|
159
|
+
const { values } = ctx.action.params;
|
|
160
|
+
try {
|
|
161
|
+
await service.handleWebhook(values || ctx.request.body, signature);
|
|
162
|
+
ctx.body = { received: true };
|
|
163
|
+
} catch (err: any) {
|
|
164
|
+
plugin.app.logger.warn(`Webhook callback error: ${err.message}`);
|
|
165
|
+
if (err.message.includes('signature')) {
|
|
166
|
+
ctx.status = 401;
|
|
167
|
+
ctx.body = { error: err.message };
|
|
168
|
+
} else {
|
|
169
|
+
ctx.status = 400;
|
|
170
|
+
ctx.body = { error: err.message };
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
await next();
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
};
|
|
@@ -19,6 +19,11 @@ export default {
|
|
|
19
19
|
{ type: 'json', name: 'externalTaskIds' },
|
|
20
20
|
{ type: 'date', name: 'startedAt' },
|
|
21
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' },
|
|
22
27
|
{
|
|
23
28
|
type: 'belongsTo',
|
|
24
29
|
name: 'createdBy',
|
|
@@ -26,4 +31,12 @@ export default {
|
|
|
26
31
|
foreignKey: 'createdById',
|
|
27
32
|
},
|
|
28
33
|
],
|
|
34
|
+
indexes: [
|
|
35
|
+
{
|
|
36
|
+
fields: ['status'],
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
fields: ['leaseExpiresAt'],
|
|
40
|
+
},
|
|
41
|
+
],
|
|
29
42
|
} as CollectionOptions;
|
package/src/server/plugin.ts
CHANGED
|
@@ -1,134 +1,134 @@
|
|
|
1
|
-
import { Plugin } from '@nocobase/server';
|
|
2
|
-
import { resolve } from 'path';
|
|
3
|
-
import { DocumentUnderstandingService } from './services/DocumentUnderstandingService';
|
|
4
|
-
import { defineActions } from './actions';
|
|
5
|
-
import { createDynamicPipelineToolsProvider } from './tools/document-understanding-tool';
|
|
6
|
-
|
|
7
|
-
type DynamicToolsProvider = ReturnType<typeof createDynamicPipelineToolsProvider>;
|
|
8
|
-
|
|
9
|
-
interface AiManagerLike {
|
|
10
|
-
toolsManager?: {
|
|
11
|
-
registerDynamicTools: (provider: DynamicToolsProvider) => void;
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface DocumentToolRuntimeState {
|
|
16
|
-
documentUnderstandingToolProviderRegistered?: boolean;
|
|
17
|
-
documentUnderstandingService?: DocumentUnderstandingService;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const isAiManagerLike = (manager: unknown): manager is AiManagerLike => {
|
|
21
|
-
const candidate = manager as AiManagerLike;
|
|
22
|
-
return typeof candidate?.toolsManager?.registerDynamicTools === 'function';
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export class PluginDocumentUnderstandingServer extends Plugin {
|
|
26
|
-
public service!: DocumentUnderstandingService;
|
|
27
|
-
private aiToolsRegistered = false;
|
|
28
|
-
private readonly registerAIToolsAfterStart = () => this.registerAITools();
|
|
29
|
-
|
|
30
|
-
async afterAdd() {}
|
|
31
|
-
|
|
32
|
-
async beforeLoad() {
|
|
33
|
-
await this.db.import({
|
|
34
|
-
directory: resolve(__dirname, 'collections'),
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async load() {
|
|
39
|
-
// Collections are imported in beforeLoad so database metadata exists before
|
|
40
|
-
// resources and runtime services are registered.
|
|
41
|
-
this.service = new DocumentUnderstandingService(this.app, this.db);
|
|
42
|
-
|
|
43
|
-
// Register REST actions
|
|
44
|
-
this.registerActions();
|
|
45
|
-
|
|
46
|
-
// 4. Register AI tool (graceful)
|
|
47
|
-
this.registerAITools();
|
|
48
|
-
this.app.on('afterStart', this.registerAIToolsAfterStart);
|
|
49
|
-
|
|
50
|
-
// 5. ACL
|
|
51
|
-
this.app.acl.registerSnippet({
|
|
52
|
-
name: `pm.${this.name}`,
|
|
53
|
-
actions: ['docUnderstanding:*'],
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
private registerActions() {
|
|
58
|
-
const actions = defineActions(this);
|
|
59
|
-
|
|
60
|
-
const resourceName = 'docUnderstanding';
|
|
61
|
-
this.app.resource({
|
|
62
|
-
name: resourceName,
|
|
63
|
-
actions: {
|
|
64
|
-
getConfig: actions.getConfig,
|
|
65
|
-
updateConfig: actions.updateConfig,
|
|
66
|
-
listEndpoints: actions.listEndpoints,
|
|
67
|
-
createEndpoint: actions.createEndpoint,
|
|
68
|
-
updateEndpoint: actions.updateEndpoint,
|
|
69
|
-
deleteEndpoint: actions.deleteEndpoint,
|
|
70
|
-
listPipelines: actions.listPipelines,
|
|
71
|
-
createPipeline: actions.createPipeline,
|
|
72
|
-
updatePipeline: actions.updatePipeline,
|
|
73
|
-
deletePipeline: actions.deletePipeline,
|
|
74
|
-
executePipeline: actions.executePipeline,
|
|
75
|
-
getJobStatus: actions.getJobStatus,
|
|
76
|
-
listJobs: actions.listJobs,
|
|
77
|
-
webhookCallback: actions.webhookCallback,
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
private registerAITools() {
|
|
83
|
-
if (this.aiToolsRegistered) return;
|
|
84
|
-
try {
|
|
85
|
-
const aiManager = (this.app as typeof this.app & { aiManager?: unknown }).aiManager;
|
|
86
|
-
if (!isAiManagerLike(aiManager)) {
|
|
87
|
-
this.app.logger.warn('Document Understanding: plugin-ai not available, skip AI tool registration.');
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
const runtimeState = this.app as typeof this.app & DocumentToolRuntimeState;
|
|
91
|
-
runtimeState.documentUnderstandingService = this.service;
|
|
92
|
-
if (!runtimeState.documentUnderstandingToolProviderRegistered) {
|
|
93
|
-
aiManager.toolsManager.registerDynamicTools(async (register) => {
|
|
94
|
-
const currentService = runtimeState.documentUnderstandingService;
|
|
95
|
-
if (!currentService) return;
|
|
96
|
-
await createDynamicPipelineToolsProvider(currentService)(register);
|
|
97
|
-
});
|
|
98
|
-
runtimeState.documentUnderstandingToolProviderRegistered = true;
|
|
99
|
-
}
|
|
100
|
-
this.aiToolsRegistered = true;
|
|
101
|
-
this.app.logger.info('Document Understanding: Dynamic pipeline tools provider registered.');
|
|
102
|
-
} catch (err) {
|
|
103
|
-
this.app.logger.warn('Document Understanding: Failed to register AI tools, continue without it.', err);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
async afterStart() {
|
|
108
|
-
try {
|
|
109
|
-
await this.service.initialize({ recoverJobs: true });
|
|
110
|
-
} catch (err) {
|
|
111
|
-
this.app.logger.warn('Document Understanding plugin not configured yet, skip init.', err);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
async install() {}
|
|
116
|
-
|
|
117
|
-
async afterEnable() {}
|
|
118
|
-
|
|
119
|
-
async afterDisable() {}
|
|
120
|
-
|
|
121
|
-
async remove() {}
|
|
122
|
-
|
|
123
|
-
async beforeStop() {
|
|
124
|
-
this.app.off?.('afterStart', this.registerAIToolsAfterStart);
|
|
125
|
-
this.app.removeListener?.('afterStart', this.registerAIToolsAfterStart);
|
|
126
|
-
this.service?.destroy();
|
|
127
|
-
const runtimeState = this.app as typeof this.app & DocumentToolRuntimeState;
|
|
128
|
-
if (runtimeState.documentUnderstandingService === this.service) {
|
|
129
|
-
runtimeState.documentUnderstandingService = undefined;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export default PluginDocumentUnderstandingServer;
|
|
1
|
+
import { Plugin } from '@nocobase/server';
|
|
2
|
+
import { resolve } from 'path';
|
|
3
|
+
import { DocumentUnderstandingService } from './services/DocumentUnderstandingService';
|
|
4
|
+
import { defineActions } from './actions';
|
|
5
|
+
import { createDynamicPipelineToolsProvider } from './tools/document-understanding-tool';
|
|
6
|
+
|
|
7
|
+
type DynamicToolsProvider = ReturnType<typeof createDynamicPipelineToolsProvider>;
|
|
8
|
+
|
|
9
|
+
interface AiManagerLike {
|
|
10
|
+
toolsManager?: {
|
|
11
|
+
registerDynamicTools: (provider: DynamicToolsProvider) => void;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface DocumentToolRuntimeState {
|
|
16
|
+
documentUnderstandingToolProviderRegistered?: boolean;
|
|
17
|
+
documentUnderstandingService?: DocumentUnderstandingService;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const isAiManagerLike = (manager: unknown): manager is AiManagerLike => {
|
|
21
|
+
const candidate = manager as AiManagerLike;
|
|
22
|
+
return typeof candidate?.toolsManager?.registerDynamicTools === 'function';
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export class PluginDocumentUnderstandingServer extends Plugin {
|
|
26
|
+
public service!: DocumentUnderstandingService;
|
|
27
|
+
private aiToolsRegistered = false;
|
|
28
|
+
private readonly registerAIToolsAfterStart = () => this.registerAITools();
|
|
29
|
+
|
|
30
|
+
async afterAdd() {}
|
|
31
|
+
|
|
32
|
+
async beforeLoad() {
|
|
33
|
+
await this.db.import({
|
|
34
|
+
directory: resolve(__dirname, 'collections'),
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async load() {
|
|
39
|
+
// Collections are imported in beforeLoad so database metadata exists before
|
|
40
|
+
// resources and runtime services are registered.
|
|
41
|
+
this.service = new DocumentUnderstandingService(this.app, this.db);
|
|
42
|
+
|
|
43
|
+
// Register REST actions
|
|
44
|
+
this.registerActions();
|
|
45
|
+
|
|
46
|
+
// 4. Register AI tool (graceful)
|
|
47
|
+
this.registerAITools();
|
|
48
|
+
this.app.on('afterStart', this.registerAIToolsAfterStart);
|
|
49
|
+
|
|
50
|
+
// 5. ACL
|
|
51
|
+
this.app.acl.registerSnippet({
|
|
52
|
+
name: `pm.${this.name}`,
|
|
53
|
+
actions: ['docUnderstanding:*'],
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private registerActions() {
|
|
58
|
+
const actions = defineActions(this);
|
|
59
|
+
|
|
60
|
+
const resourceName = 'docUnderstanding';
|
|
61
|
+
this.app.resource({
|
|
62
|
+
name: resourceName,
|
|
63
|
+
actions: {
|
|
64
|
+
getConfig: actions.getConfig,
|
|
65
|
+
updateConfig: actions.updateConfig,
|
|
66
|
+
listEndpoints: actions.listEndpoints,
|
|
67
|
+
createEndpoint: actions.createEndpoint,
|
|
68
|
+
updateEndpoint: actions.updateEndpoint,
|
|
69
|
+
deleteEndpoint: actions.deleteEndpoint,
|
|
70
|
+
listPipelines: actions.listPipelines,
|
|
71
|
+
createPipeline: actions.createPipeline,
|
|
72
|
+
updatePipeline: actions.updatePipeline,
|
|
73
|
+
deletePipeline: actions.deletePipeline,
|
|
74
|
+
executePipeline: actions.executePipeline,
|
|
75
|
+
getJobStatus: actions.getJobStatus,
|
|
76
|
+
listJobs: actions.listJobs,
|
|
77
|
+
webhookCallback: actions.webhookCallback,
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private registerAITools() {
|
|
83
|
+
if (this.aiToolsRegistered) return;
|
|
84
|
+
try {
|
|
85
|
+
const aiManager = (this.app as typeof this.app & { aiManager?: unknown }).aiManager;
|
|
86
|
+
if (!isAiManagerLike(aiManager)) {
|
|
87
|
+
this.app.logger.warn('Document Understanding: plugin-ai not available, skip AI tool registration.');
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const runtimeState = this.app as typeof this.app & DocumentToolRuntimeState;
|
|
91
|
+
runtimeState.documentUnderstandingService = this.service;
|
|
92
|
+
if (!runtimeState.documentUnderstandingToolProviderRegistered) {
|
|
93
|
+
aiManager.toolsManager.registerDynamicTools(async (register) => {
|
|
94
|
+
const currentService = runtimeState.documentUnderstandingService;
|
|
95
|
+
if (!currentService) return;
|
|
96
|
+
await createDynamicPipelineToolsProvider(currentService)(register);
|
|
97
|
+
});
|
|
98
|
+
runtimeState.documentUnderstandingToolProviderRegistered = true;
|
|
99
|
+
}
|
|
100
|
+
this.aiToolsRegistered = true;
|
|
101
|
+
this.app.logger.info('Document Understanding: Dynamic pipeline tools provider registered.');
|
|
102
|
+
} catch (err) {
|
|
103
|
+
this.app.logger.warn('Document Understanding: Failed to register AI tools, continue without it.', err);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async afterStart() {
|
|
108
|
+
try {
|
|
109
|
+
await this.service.initialize({ recoverJobs: true });
|
|
110
|
+
} catch (err) {
|
|
111
|
+
this.app.logger.warn('Document Understanding plugin not configured yet, skip init.', err);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async install() {}
|
|
116
|
+
|
|
117
|
+
async afterEnable() {}
|
|
118
|
+
|
|
119
|
+
async afterDisable() {}
|
|
120
|
+
|
|
121
|
+
async remove() {}
|
|
122
|
+
|
|
123
|
+
async beforeStop() {
|
|
124
|
+
this.app.off?.('afterStart', this.registerAIToolsAfterStart);
|
|
125
|
+
this.app.removeListener?.('afterStart', this.registerAIToolsAfterStart);
|
|
126
|
+
this.service?.destroy();
|
|
127
|
+
const runtimeState = this.app as typeof this.app & DocumentToolRuntimeState;
|
|
128
|
+
if (runtimeState.documentUnderstandingService === this.service) {
|
|
129
|
+
runtimeState.documentUnderstandingService = undefined;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export default PluginDocumentUnderstandingServer;
|