plugin-document-understanding 1.1.8 → 1.1.11
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 +6 -6
- 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/server/actions.js +1 -1
- package/dist/server/plugin.js +11 -10
- package/dist/server/services/DocumentUnderstandingService.js +25 -6
- package/dist/server/services/PipelineExecutor.js +8 -32
- 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__/document-understanding-service.test.ts +82 -0
- package/src/server/__tests__/pipeline-executor.test.ts +55 -33
- package/src/server/actions.ts +176 -176
- package/src/server/plugin.ts +134 -134
- package/src/server/services/DocumentUnderstandingService.ts +29 -7
- package/src/server/services/PipelineExecutor.ts +477 -502
- package/src/server/types.ts +78 -73
- package/dist/client-v2/794.9b984a25b2fc7c14.js +0 -10
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
//
|
|
41
|
-
this.service = new DocumentUnderstandingService(this.app, this.db);
|
|
42
|
-
|
|
43
|
-
//
|
|
44
|
-
this.
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
this.
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
this.app.
|
|
126
|
-
this.
|
|
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;
|
|
@@ -4,7 +4,7 @@ import { Database, Repository } from '@nocobase/database';
|
|
|
4
4
|
import { ExternalApiClient, FileInput } from './ExternalApiClient';
|
|
5
5
|
import { AsyncJobManager } from './AsyncJobManager';
|
|
6
6
|
import { PipelineExecutor } from './PipelineExecutor';
|
|
7
|
-
import { ServiceConfig, EndpointDef, PipelineDef, JobState } from '../types';
|
|
7
|
+
import { ClientServiceConfig, ServiceConfig, EndpointDef, PipelineDef, JobState } from '../types';
|
|
8
8
|
|
|
9
9
|
export class DocumentUnderstandingService {
|
|
10
10
|
private app: Application;
|
|
@@ -90,7 +90,9 @@ export class DocumentUnderstandingService {
|
|
|
90
90
|
});
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
|
|
93
|
+
// Attributes live on the Sequelize model prototype, so callers that spread the
|
|
94
|
+
// result would otherwise get an object with none of the config fields.
|
|
95
|
+
return config.toJSON();
|
|
94
96
|
}
|
|
95
97
|
|
|
96
98
|
isReady(): boolean {
|
|
@@ -101,16 +103,34 @@ export class DocumentUnderstandingService {
|
|
|
101
103
|
return this.ensureConfig();
|
|
102
104
|
}
|
|
103
105
|
|
|
106
|
+
async getConfigForClient(): Promise<ClientServiceConfig> {
|
|
107
|
+
const { authKey, webhookSecret, ...config } = await this.ensureConfig();
|
|
108
|
+
return {
|
|
109
|
+
...config,
|
|
110
|
+
hasAuthKey: Boolean(authKey),
|
|
111
|
+
hasWebhookSecret: Boolean(webhookSecret),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
104
115
|
async updateConfig(data: Partial<ServiceConfig>): Promise<void> {
|
|
105
116
|
const configRepo = this.db.getRepository<any>('doc_understanding_config');
|
|
106
117
|
const conf = await configRepo.findOne();
|
|
107
118
|
if (conf) {
|
|
108
|
-
|
|
119
|
+
const values = { ...data };
|
|
120
|
+
if (!values.authKey) delete values.authKey;
|
|
121
|
+
if (!values.webhookSecret) delete values.webhookSecret;
|
|
122
|
+
await configRepo.update({ filterByTk: conf.id, values });
|
|
109
123
|
}
|
|
110
124
|
// Reinitialize to pick up new config
|
|
111
125
|
await this.initialize();
|
|
112
126
|
}
|
|
113
127
|
|
|
128
|
+
destroy(): void {
|
|
129
|
+
this.jobManager?.destroy();
|
|
130
|
+
this.initialized = false;
|
|
131
|
+
this.startupRecoveryCompleted = false;
|
|
132
|
+
}
|
|
133
|
+
|
|
114
134
|
async listEndpoints(): Promise<EndpointDef[]> {
|
|
115
135
|
await this.ensureInitialized();
|
|
116
136
|
return this.db.getRepository<any>('doc_understanding_endpoints').find({ sort: 'sortOrder' });
|
|
@@ -200,15 +220,17 @@ export class DocumentUnderstandingService {
|
|
|
200
220
|
return { jobId: job.id };
|
|
201
221
|
}
|
|
202
222
|
|
|
203
|
-
async getJobStatus(jobId: number): Promise<JobState> {
|
|
204
|
-
const
|
|
223
|
+
async getJobStatus(jobId: number, userId?: number): Promise<JobState> {
|
|
224
|
+
const filter = userId ? { id: jobId, createdById: userId } : { id: jobId };
|
|
225
|
+
const job = await this.db.getRepository<any>('doc_understanding_jobs').findOne({ filter });
|
|
205
226
|
if (!job) throw new Error('Job not found');
|
|
206
227
|
return job;
|
|
207
228
|
}
|
|
208
229
|
|
|
209
|
-
async listJobs(filters?:
|
|
230
|
+
async listJobs(filters?: Record<string, unknown>, userId?: number): Promise<JobState[]> {
|
|
231
|
+
const filter = userId ? { ...(filters || {}), createdById: userId } : filters;
|
|
210
232
|
return this.db.getRepository<any>('doc_understanding_jobs').find({
|
|
211
|
-
filter
|
|
233
|
+
filter,
|
|
212
234
|
sort: ['-createdAt'],
|
|
213
235
|
});
|
|
214
236
|
}
|