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.
Files changed (31) hide show
  1. package/dist/client/345.17f88ffb846ec69b.js +10 -0
  2. package/dist/client/index.js +1 -1
  3. package/dist/client-v2/345.1e0c321d30702aa1.js +10 -0
  4. package/dist/client-v2/index.js +1 -1
  5. package/dist/externalVersion.js +6 -6
  6. package/dist/locale/en-US.json +171 -121
  7. package/dist/locale/vi-VN.json +171 -121
  8. package/dist/locale/zh-CN.json +171 -1
  9. package/dist/server/actions.js +1 -1
  10. package/dist/server/plugin.js +11 -10
  11. package/dist/server/services/DocumentUnderstandingService.js +25 -6
  12. package/dist/server/services/PipelineExecutor.js +8 -32
  13. package/package.json +46 -49
  14. package/src/client/index.tsx +7 -1
  15. package/src/client-v2/components/EndpointsTab.tsx +177 -108
  16. package/src/client-v2/components/JobsTab.tsx +94 -59
  17. package/src/client-v2/components/PipelinesTab.tsx +244 -154
  18. package/src/client-v2/components/PluginSettings.tsx +2 -0
  19. package/src/client-v2/components/ServiceConfigTab.tsx +56 -39
  20. package/src/client-v2/types.ts +32 -0
  21. package/src/locale/en-US.json +171 -121
  22. package/src/locale/vi-VN.json +171 -121
  23. package/src/locale/zh-CN.json +171 -1
  24. package/src/server/__tests__/document-understanding-service.test.ts +82 -0
  25. package/src/server/__tests__/pipeline-executor.test.ts +55 -33
  26. package/src/server/actions.ts +176 -176
  27. package/src/server/plugin.ts +134 -134
  28. package/src/server/services/DocumentUnderstandingService.ts +29 -7
  29. package/src/server/services/PipelineExecutor.ts +477 -502
  30. package/src/server/types.ts +78 -73
  31. package/dist/client-v2/794.9b984a25b2fc7c14.js +0 -10
@@ -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
- async load() {
35
- // 1. Import collections
36
- await this.db.import({
37
- directory: resolve(__dirname, 'collections'),
38
- });
39
-
40
- // 2. Create service
41
- this.service = new DocumentUnderstandingService(this.app, this.db);
42
-
43
- // Defer initialization to after app has loaded database so config exists
44
- this.app.on('afterStart', async () => {
45
- try {
46
- await this.service.initialize({ recoverJobs: true });
47
- } catch (err) {
48
- this.app.logger.warn('Document Understanding plugin not configured yet, skip init.', err);
49
- }
50
- });
51
-
52
- // 3. Register REST actions
53
- this.registerActions();
54
-
55
- // 4. Register AI tool (graceful)
56
- this.registerAITools();
57
- this.app.on('afterStart', this.registerAIToolsAfterStart);
58
-
59
- // 5. ACL
60
- this.app.acl.registerSnippet({
61
- name: `pm.${this.name}`,
62
- actions: ['docUnderstanding:*'],
63
- });
64
- }
65
-
66
- private registerActions() {
67
- const actions = defineActions(this);
68
-
69
- const resourceName = 'docUnderstanding';
70
- this.app.resource({
71
- name: resourceName,
72
- actions: {
73
- getConfig: actions.getConfig,
74
- updateConfig: actions.updateConfig,
75
- listEndpoints: actions.listEndpoints,
76
- createEndpoint: actions.createEndpoint,
77
- updateEndpoint: actions.updateEndpoint,
78
- deleteEndpoint: actions.deleteEndpoint,
79
- listPipelines: actions.listPipelines,
80
- createPipeline: actions.createPipeline,
81
- updatePipeline: actions.updatePipeline,
82
- deletePipeline: actions.deletePipeline,
83
- executePipeline: actions.executePipeline,
84
- getJobStatus: actions.getJobStatus,
85
- listJobs: actions.listJobs,
86
- webhookCallback: actions.webhookCallback,
87
- },
88
- });
89
- }
90
-
91
- private registerAITools() {
92
- if (this.aiToolsRegistered) return;
93
- try {
94
- const aiManager = (this.app as typeof this.app & { aiManager?: unknown }).aiManager;
95
- if (!isAiManagerLike(aiManager)) {
96
- this.app.logger.warn('Document Understanding: plugin-ai not available, skip AI tool registration.');
97
- return;
98
- }
99
- const runtimeState = this.app as typeof this.app & DocumentToolRuntimeState;
100
- runtimeState.documentUnderstandingService = this.service;
101
- if (!runtimeState.documentUnderstandingToolProviderRegistered) {
102
- aiManager.toolsManager.registerDynamicTools(async (register) => {
103
- const currentService = runtimeState.documentUnderstandingService;
104
- if (!currentService) return;
105
- await createDynamicPipelineToolsProvider(currentService)(register);
106
- });
107
- runtimeState.documentUnderstandingToolProviderRegistered = true;
108
- }
109
- this.aiToolsRegistered = true;
110
- this.app.logger.info('Document Understanding: Dynamic pipeline tools provider registered.');
111
- } catch (err) {
112
- this.app.logger.warn('Document Understanding: Failed to register AI tools, continue without it.', err);
113
- }
114
- }
115
-
116
- async install() {}
117
-
118
- async afterEnable() {}
119
-
120
- async afterDisable() {}
121
-
122
- async remove() {}
123
-
124
- async beforeStop() {
125
- this.app.off?.('afterStart', this.registerAIToolsAfterStart);
126
- this.app.removeListener?.('afterStart', this.registerAIToolsAfterStart);
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
- return config;
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
- await configRepo.update({ filterByTk: conf.id, values: data });
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 job = await this.db.getRepository<any>('doc_understanding_jobs').findOne({ filterByTk: jobId });
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?: any): Promise<JobState[]> {
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: filters,
233
+ filter,
212
234
  sort: ['-createdAt'],
213
235
  });
214
236
  }