plugin-document-understanding 1.1.16 → 2.0.0
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/LICENSE +201 -0
- package/README.md +19 -25
- package/dist/client/169.6888909107c553c3.js +10 -0
- package/dist/client/379.e29f5ced467ce23d.js +10 -0
- package/dist/client/867.3ed3b814115181ca.js +10 -0
- package/dist/client/index.js +1 -1
- package/dist/client-v2/169.53bf1be2ec192f0c.js +10 -0
- package/dist/client-v2/379.e75c4829567cdb89.js +10 -0
- package/dist/client-v2/867.66d7291e95f1de78.js +10 -0
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +9 -7
- package/dist/locale/en-US.json +126 -0
- package/dist/locale/vi-VN.json +130 -2
- package/dist/locale/zh-CN.json +127 -1
- package/dist/node_modules/form-data/package.json +1 -1
- package/dist/node_modules/mime-types/package.json +1 -1
- package/dist/server/actions.js +211 -11
- package/dist/server/collections/api-keys.js +55 -0
- package/dist/server/collections/artifacts.js +59 -0
- package/dist/server/collections/config.js +9 -3
- package/dist/server/collections/endpoints.js +3 -0
- package/dist/server/collections/jobs.js +7 -4
- package/dist/server/collections/operations.js +76 -0
- package/dist/server/contracts/du-contracts.js +828 -0
- package/dist/server/dugate-seed.js +98 -340
- package/dist/server/plugin.js +49 -7
- package/dist/server/routes/du-gateway-router.js +443 -0
- package/dist/server/services/ArtifactStorageService.js +459 -0
- package/dist/server/services/AsyncJobManager.js +1 -1
- package/dist/server/services/BuiltinBusinessEngine.js +1223 -0
- package/dist/server/services/DocumentUnderstandingService.js +151 -25
- package/dist/server/services/ExternalDuReworkClient.js +210 -0
- package/dist/server/services/OperationOrchestrator.js +1087 -0
- package/dist/server/services/PipelineExecutor.js +164 -43
- package/dist/server/tools/document-understanding-tool.js +49 -24
- package/dist/server/workflow/DocumentUnderstandingInstruction.js +292 -0
- package/package.json +8 -4
- package/src/client/index.tsx +11 -0
- package/src/client-v2/components/ApiKeysTab.tsx +198 -0
- package/src/client-v2/components/EndpointsTab.tsx +39 -2
- package/src/client-v2/components/JobsTab.tsx +136 -21
- package/src/client-v2/components/OperationsTab.tsx +586 -0
- package/src/client-v2/components/PluginSettings.tsx +12 -0
- package/src/client-v2/components/ServiceConfigTab.tsx +225 -65
- package/src/client-v2/components/__tests__/PluginSettings.test.tsx +4 -0
- package/src/client-v2/locale.ts +1 -8
- package/src/client-v2/models/index.ts +1 -8
- package/src/client-v2/plugin.tsx +12 -8
- package/src/client-v2/types.ts +13 -1
- package/src/client-v2/workflow/DocumentUnderstandingFieldset.tsx +122 -0
- package/src/client-v2/workflow/DocumentUnderstandingInstruction.tsx +49 -0
- package/src/locale/en-US.json +126 -0
- package/src/locale/vi-VN.json +130 -2
- package/src/locale/zh-CN.json +127 -1
- package/src/server/__tests__/actions.test.ts +1 -8
- package/src/server/__tests__/du-rework-gateway.test.ts +462 -0
- package/src/server/__tests__/dugate-integration.test.ts +79 -42
- package/src/server/__tests__/smoke.test.ts +1 -8
- package/src/server/__tests__/upload-security.test.ts +1 -8
- package/src/server/actions.ts +252 -36
- package/src/server/collections/api-keys.ts +35 -0
- package/src/server/collections/artifacts.ts +39 -0
- package/src/server/collections/config.ts +18 -2
- package/src/server/collections/endpoints.ts +12 -0
- package/src/server/collections/jobs.ts +16 -4
- package/src/server/collections/operations.ts +56 -0
- package/src/server/contracts/du-contracts.ts +1026 -0
- package/src/server/dugate-seed.ts +126 -453
- package/src/server/plugin.ts +64 -14
- package/src/server/routes/du-gateway-router.ts +465 -0
- package/src/server/services/ArtifactStorageService.ts +596 -0
- package/src/server/services/AsyncJobManager.ts +10 -1
- package/src/server/services/BuiltinBusinessEngine.ts +1544 -0
- package/src/server/services/DocumentUnderstandingService.ts +186 -44
- package/src/server/services/ExternalDuReworkClient.ts +222 -0
- package/src/server/services/OperationOrchestrator.ts +1359 -0
- package/src/server/services/PipelineExecutor.ts +245 -108
- package/src/server/tools/document-understanding-tool.ts +63 -45
- package/src/server/types.ts +101 -16
- package/src/server/workflow/DocumentUnderstandingInstruction.ts +308 -0
- package/dist/client/345.277fbb2b97424db5.js +0 -10
- package/dist/client-v2/345.2fa9834b46dd4531.js +0 -10
|
@@ -0,0 +1,292 @@
|
|
|
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
|
+
var __create = Object.create;
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
13
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
15
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
+
var __export = (target, all) => {
|
|
17
|
+
for (var name in all)
|
|
18
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
19
|
+
};
|
|
20
|
+
var __copyProps = (to, from, except, desc) => {
|
|
21
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
22
|
+
for (let key of __getOwnPropNames(from))
|
|
23
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
24
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
25
|
+
}
|
|
26
|
+
return to;
|
|
27
|
+
};
|
|
28
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
29
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
30
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
31
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
32
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
33
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
34
|
+
mod
|
|
35
|
+
));
|
|
36
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
37
|
+
var DocumentUnderstandingInstruction_exports = {};
|
|
38
|
+
__export(DocumentUnderstandingInstruction_exports, {
|
|
39
|
+
DocumentUnderstandingWorkflowInstruction: () => DocumentUnderstandingWorkflowInstruction
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(DocumentUnderstandingInstruction_exports);
|
|
42
|
+
var import_fs = require("fs");
|
|
43
|
+
var import_path = require("path");
|
|
44
|
+
var import_mime_types = __toESM(require("mime-types"));
|
|
45
|
+
var import_plugin_workflow = require("@nocobase/plugin-workflow");
|
|
46
|
+
var import_du_contracts = require("../contracts/du-contracts");
|
|
47
|
+
var import_PipelineExecutor = require("../services/PipelineExecutor");
|
|
48
|
+
function isRecord(value) {
|
|
49
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
50
|
+
}
|
|
51
|
+
function collectFileUrlStrings(rawFiles) {
|
|
52
|
+
if (!rawFiles) return [];
|
|
53
|
+
if (typeof rawFiles === "string") {
|
|
54
|
+
return rawFiles.split(",").map((s) => s.trim()).filter(Boolean);
|
|
55
|
+
}
|
|
56
|
+
if (Array.isArray(rawFiles)) {
|
|
57
|
+
const urls = [];
|
|
58
|
+
for (const item of rawFiles) {
|
|
59
|
+
urls.push(...collectFileUrlStrings(item));
|
|
60
|
+
}
|
|
61
|
+
return urls;
|
|
62
|
+
}
|
|
63
|
+
if (isRecord(rawFiles)) {
|
|
64
|
+
if (typeof rawFiles.url === "string") return [rawFiles.url];
|
|
65
|
+
if (typeof rawFiles.filename === "string") return [`uploads/${rawFiles.filename}`];
|
|
66
|
+
}
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
function applyVariantToInput(action, variant, target) {
|
|
70
|
+
if (!variant || !variant.trim()) return;
|
|
71
|
+
const v = variant.trim();
|
|
72
|
+
if (action === "ingest" || action === "compare") {
|
|
73
|
+
if (target.mode === void 0) target.mode = v;
|
|
74
|
+
} else if (action === "extract") {
|
|
75
|
+
if (target.type === void 0) target.type = v;
|
|
76
|
+
} else if (action === "analyze" || action === "generate") {
|
|
77
|
+
if (target.task === void 0) target.task = v;
|
|
78
|
+
} else if (action === "transform") {
|
|
79
|
+
if (target.variant === void 0) target.variant = v;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
class DocumentUnderstandingWorkflowInstruction extends import_plugin_workflow.Instruction {
|
|
83
|
+
getService() {
|
|
84
|
+
var _a, _b, _c, _d;
|
|
85
|
+
const appWithService = this.workflow.app;
|
|
86
|
+
const pluginInstance = ((_b = (_a = this.workflow.app.pm) == null ? void 0 : _a.get) == null ? void 0 : _b.call(_a, "plugin-document-understanding")) || ((_d = (_c = this.workflow.app.pm) == null ? void 0 : _c.get) == null ? void 0 : _d.call(_c, "document-understanding"));
|
|
87
|
+
const service = (pluginInstance == null ? void 0 : pluginInstance.service) || appWithService.documentUnderstandingService;
|
|
88
|
+
if (!service) {
|
|
89
|
+
throw new Error("Document Understanding service is not available");
|
|
90
|
+
}
|
|
91
|
+
return service;
|
|
92
|
+
}
|
|
93
|
+
async executeConfig(config, signal) {
|
|
94
|
+
var _a, _b;
|
|
95
|
+
const service = this.getService();
|
|
96
|
+
await service.ensureInitialized();
|
|
97
|
+
let parsedInput = {};
|
|
98
|
+
if (typeof config.inputJson === "string" && config.inputJson.trim()) {
|
|
99
|
+
const parsed = JSON.parse(config.inputJson);
|
|
100
|
+
if (isRecord(parsed)) parsedInput = { ...parsed };
|
|
101
|
+
} else if (isRecord(config.inputJson)) {
|
|
102
|
+
parsedInput = { ...config.inputJson };
|
|
103
|
+
}
|
|
104
|
+
const fileUrls = collectFileUrlStrings(config.files);
|
|
105
|
+
if (config.mode === "pipeline") {
|
|
106
|
+
const pipelineId = Number(config.pipelineId);
|
|
107
|
+
if (!Number.isSafeInteger(pipelineId) || pipelineId <= 0) {
|
|
108
|
+
throw new Error("Workflow node in pipeline mode requires a valid pipelineId");
|
|
109
|
+
}
|
|
110
|
+
if (config.pages && !parsedInput.pages) {
|
|
111
|
+
parsedInput.pages = config.pages;
|
|
112
|
+
}
|
|
113
|
+
const fileRefs = fileUrls.map((url) => ({
|
|
114
|
+
url,
|
|
115
|
+
fieldName: "file",
|
|
116
|
+
buffer: void 0,
|
|
117
|
+
filename: "",
|
|
118
|
+
mimeType: ""
|
|
119
|
+
}));
|
|
120
|
+
const { jobId } = await service.executePipeline(pipelineId, parsedInput, fileRefs);
|
|
121
|
+
const serviceConf2 = await service.getConfig();
|
|
122
|
+
const deadline2 = Date.now() + (serviceConf2.pollTimeout || 3e5);
|
|
123
|
+
while (Date.now() < deadline2) {
|
|
124
|
+
if (signal == null ? void 0 : signal.aborted) {
|
|
125
|
+
await service.cancelJob(jobId).catch(() => void 0);
|
|
126
|
+
throw new Error("Workflow execution aborted");
|
|
127
|
+
}
|
|
128
|
+
const job = await service.getJobStatus(jobId);
|
|
129
|
+
if (job.status === "completed") {
|
|
130
|
+
const finalObj = isRecord(job.finalResult) ? job.finalResult : { data: job.finalResult };
|
|
131
|
+
return {
|
|
132
|
+
jobId,
|
|
133
|
+
operationId: job.operationId ?? null,
|
|
134
|
+
state: "SUCCEEDED",
|
|
135
|
+
...finalObj,
|
|
136
|
+
usage: job.usage ?? finalObj.usage ?? null
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
if (["failed", "cancelled", "timeout"].includes(job.status)) {
|
|
140
|
+
throw new Error(job.error || `Document pipeline job #${jobId} ended with status ${job.status}`);
|
|
141
|
+
}
|
|
142
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
143
|
+
}
|
|
144
|
+
throw new Error(`Document pipeline job #${jobId} timed out`);
|
|
145
|
+
}
|
|
146
|
+
const businessId = config.businessId || "document-core";
|
|
147
|
+
const action = config.action || "extract";
|
|
148
|
+
applyVariantToInput(action, config.variant, parsedInput);
|
|
149
|
+
const artifactIds = [];
|
|
150
|
+
for (const rawUrl of fileUrls) {
|
|
151
|
+
const localPath = (0, import_PipelineExecutor.resolveUploadFilePath)(rawUrl);
|
|
152
|
+
if (!localPath) continue;
|
|
153
|
+
const bytes = (0, import_fs.readFileSync)(localPath);
|
|
154
|
+
const fileName = (0, import_path.basename)(localPath);
|
|
155
|
+
const mimeType = String(import_mime_types.default.lookup(localPath) || "application/octet-stream");
|
|
156
|
+
const created = await service.orchestrator.artifactStorage.createReadyArtifactFromBuffer({
|
|
157
|
+
tenantId: "default",
|
|
158
|
+
fileName,
|
|
159
|
+
mimeType,
|
|
160
|
+
buffer: bytes
|
|
161
|
+
});
|
|
162
|
+
artifactIds.push(created.artifactId);
|
|
163
|
+
}
|
|
164
|
+
let documentSelection;
|
|
165
|
+
if (config.pages && config.pages.trim() && artifactIds.length > 0) {
|
|
166
|
+
const ranges = (0, import_du_contracts.parsePageRangeString)(config.pages, 500);
|
|
167
|
+
if (ranges.length > 0) {
|
|
168
|
+
documentSelection = {
|
|
169
|
+
kind: "requestSelection",
|
|
170
|
+
schemaVersion: 1,
|
|
171
|
+
mode: "combined",
|
|
172
|
+
artifactIds,
|
|
173
|
+
pageSelections: artifactIds.map((artifactId) => ({
|
|
174
|
+
artifactId,
|
|
175
|
+
selection: { kind: "ranges", ranges }
|
|
176
|
+
}))
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
const submitted = await service.orchestrator.submitOperation({
|
|
181
|
+
principal: {
|
|
182
|
+
kind: "nocobase_user",
|
|
183
|
+
tenantId: "default",
|
|
184
|
+
allowedBusinesses: ["*"],
|
|
185
|
+
allowedActions: ["*"]
|
|
186
|
+
},
|
|
187
|
+
businessId,
|
|
188
|
+
action,
|
|
189
|
+
submission: {
|
|
190
|
+
input: parsedInput,
|
|
191
|
+
artifacts: artifactIds.map((artifactId) => ({ artifactId, role: "input" })),
|
|
192
|
+
documentSelection
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
const serviceConf = await service.getConfig();
|
|
196
|
+
const deadline = Date.now() + (serviceConf.pollTimeout || 3e5);
|
|
197
|
+
while (Date.now() < deadline) {
|
|
198
|
+
if (signal == null ? void 0 : signal.aborted) {
|
|
199
|
+
await service.orchestrator.cancelOperation("default", submitted.operationId).catch(() => void 0);
|
|
200
|
+
throw new Error("Workflow execution aborted");
|
|
201
|
+
}
|
|
202
|
+
const view = await service.orchestrator.waitForOperation("default", submitted.operationId, 5);
|
|
203
|
+
if (view.state === "SUCCEEDED") {
|
|
204
|
+
const opRecord = await service.orchestrator.getOperationRecordOrThrow("default", submitted.operationId);
|
|
205
|
+
const payload = opRecord.outputPayload || {};
|
|
206
|
+
return {
|
|
207
|
+
operationId: submitted.operationId,
|
|
208
|
+
state: "SUCCEEDED",
|
|
209
|
+
data: payload.data ?? payload,
|
|
210
|
+
markdown: payload.markdown ?? null,
|
|
211
|
+
text: payload.text ?? null,
|
|
212
|
+
report: payload.report ?? null,
|
|
213
|
+
verdict: payload.verdict ?? null,
|
|
214
|
+
discrepancies: payload.discrepancies ?? null,
|
|
215
|
+
usage: opRecord.usage ?? null
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
if (["FAILED", "CANCELLED", "TIMED_OUT"].includes(view.state)) {
|
|
219
|
+
throw new Error(((_a = view.error) == null ? void 0 : _a.detail) || ((_b = view.error) == null ? void 0 : _b.title) || `Operation ended in state ${view.state}`);
|
|
220
|
+
}
|
|
221
|
+
if (view.state === "WAITING_INPUT") {
|
|
222
|
+
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
throw new Error(`Document Understanding operation ${submitted.operationId} timed out`);
|
|
226
|
+
}
|
|
227
|
+
async run(node, prevJob, processor, options) {
|
|
228
|
+
const config = processor.getParsedValue(node.config, node.id);
|
|
229
|
+
const sync = processor.isInstructionSync(node);
|
|
230
|
+
if (sync) {
|
|
231
|
+
try {
|
|
232
|
+
const result = await this.executeConfig(config, options == null ? void 0 : options.signal);
|
|
233
|
+
return { status: import_plugin_workflow.JOB_STATUS.RESOLVED, result };
|
|
234
|
+
} catch (err) {
|
|
235
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
236
|
+
return {
|
|
237
|
+
status: config.ignoreFail ? import_plugin_workflow.JOB_STATUS.RESOLVED : import_plugin_workflow.JOB_STATUS.ERROR,
|
|
238
|
+
result: { error: message }
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
const { id } = processor.saveJob({
|
|
243
|
+
status: import_plugin_workflow.JOB_STATUS.PENDING,
|
|
244
|
+
nodeId: node.id,
|
|
245
|
+
nodeKey: node.key,
|
|
246
|
+
upstreamId: (prevJob == null ? void 0 : prevJob.id) ?? null
|
|
247
|
+
});
|
|
248
|
+
await processor.exit();
|
|
249
|
+
const abortHandle = processor.createBackgroundAbortHandle();
|
|
250
|
+
const jobDone = { status: import_plugin_workflow.JOB_STATUS.PENDING };
|
|
251
|
+
const settle = async () => {
|
|
252
|
+
try {
|
|
253
|
+
const result = await this.executeConfig(config, abortHandle.signal);
|
|
254
|
+
jobDone.status = import_plugin_workflow.JOB_STATUS.RESOLVED;
|
|
255
|
+
jobDone.result = result;
|
|
256
|
+
} catch (err) {
|
|
257
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
258
|
+
jobDone.status = abortHandle.signal.aborted ? import_plugin_workflow.JOB_STATUS.ABORTED : config.ignoreFail ? import_plugin_workflow.JOB_STATUS.RESOLVED : import_plugin_workflow.JOB_STATUS.ERROR;
|
|
259
|
+
jobDone.result = { error: message };
|
|
260
|
+
} finally {
|
|
261
|
+
abortHandle.dispose();
|
|
262
|
+
const job = await this.workflow.app.db.getRepository("jobs").findOne({ filterByTk: id });
|
|
263
|
+
if (job) {
|
|
264
|
+
const execution = await job.getExecution();
|
|
265
|
+
const aborted = await this.workflow.abortExecutionIfExpired(execution);
|
|
266
|
+
if (!aborted) {
|
|
267
|
+
await execution.reload();
|
|
268
|
+
await job.reload();
|
|
269
|
+
}
|
|
270
|
+
if (!aborted && execution.status === import_plugin_workflow.EXECUTION_STATUS.STARTED && job.status === import_plugin_workflow.JOB_STATUS.PENDING) {
|
|
271
|
+
job.set(jobDone);
|
|
272
|
+
await this.workflow.resume(job);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
settle().catch((err) => {
|
|
278
|
+
processor.logger.error(`Document Understanding workflow node (#${node.id}) settling failed`, err);
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
async resume(node, job) {
|
|
282
|
+
const config = node.config || {};
|
|
283
|
+
if (config.ignoreFail) {
|
|
284
|
+
job.set("status", import_plugin_workflow.JOB_STATUS.RESOLVED);
|
|
285
|
+
}
|
|
286
|
+
return job;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
290
|
+
0 && (module.exports = {
|
|
291
|
+
DocumentUnderstandingWorkflowInstruction
|
|
292
|
+
});
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "plugin-document-understanding",
|
|
3
3
|
"displayName": "Document Understanding",
|
|
4
|
+
"displayName.vi-VN": "Hiểu Tài Liệu",
|
|
4
5
|
"displayName.zh-CN": "文档理解",
|
|
5
|
-
"description": "
|
|
6
|
-
"version": "
|
|
6
|
+
"description": "Asynchronous Document Understanding service & gateway (du-rework compatible) with built-in AI/parser engine, external gateway connector, pipeline orchestration, NocoBase workflow nodes, and AI tools.",
|
|
7
|
+
"version": "2.0.0",
|
|
7
8
|
"license": "Apache-2.0",
|
|
8
9
|
"main": "dist/server/index.js",
|
|
9
10
|
"files": [
|
|
@@ -29,11 +30,13 @@
|
|
|
29
30
|
"peerDependencies": {
|
|
30
31
|
"@langchain/core": "^0.3.0",
|
|
31
32
|
"@nocobase/actions": "2.x",
|
|
33
|
+
"@nocobase/ai": "2.x",
|
|
32
34
|
"@nocobase/client": "2.x",
|
|
33
35
|
"@nocobase/client-v2": "2.x",
|
|
34
36
|
"@nocobase/database": "2.x",
|
|
35
37
|
"@nocobase/flow-engine": "2.x",
|
|
36
38
|
"@nocobase/plugin-ai": "2.x",
|
|
39
|
+
"@nocobase/plugin-workflow": "2.x",
|
|
37
40
|
"@nocobase/server": "2.x",
|
|
38
41
|
"@nocobase/utils": "2.x"
|
|
39
42
|
},
|
|
@@ -42,14 +45,15 @@
|
|
|
42
45
|
"document",
|
|
43
46
|
"OCR",
|
|
44
47
|
"pipeline",
|
|
45
|
-
"orchestrator"
|
|
48
|
+
"orchestrator",
|
|
49
|
+
"gateway"
|
|
46
50
|
],
|
|
47
51
|
"repository": {
|
|
48
52
|
"type": "git",
|
|
49
53
|
"url": "git+https://github.com/OrgGem/nocobase-beta.git",
|
|
50
54
|
"directory": "packages/plugins/plugin-document-understanding"
|
|
51
55
|
},
|
|
52
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "0e69c550149db9fe19d3cd7549e6d7a6e84b9f7d",
|
|
53
57
|
"publishConfig": {
|
|
54
58
|
"access": "public",
|
|
55
59
|
"registry": "https://registry.npmjs.org/"
|
package/src/client/index.tsx
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
|
|
1
3
|
import React from 'react';
|
|
2
4
|
import { Plugin } from '@nocobase/client';
|
|
5
|
+
import type WorkflowPlugin from '@nocobase/plugin-workflow/client';
|
|
6
|
+
import V2DocumentUnderstandingInstruction from '../client-v2/workflow/DocumentUnderstandingInstruction';
|
|
7
|
+
|
|
8
|
+
class LegacyDocumentUnderstandingInstruction extends V2DocumentUnderstandingInstruction {}
|
|
3
9
|
|
|
4
10
|
export class PluginDocumentUnderstandingClient extends Plugin {
|
|
5
11
|
async load() {
|
|
@@ -9,6 +15,11 @@ export class PluginDocumentUnderstandingClient extends Plugin {
|
|
|
9
15
|
Component: React.lazy(() => import('../client-v2/components/PluginSettings')),
|
|
10
16
|
aclSnippet: 'pm.document-understanding',
|
|
11
17
|
});
|
|
18
|
+
|
|
19
|
+
const workflow = (this.app.pm.get('workflow') || this.app.pm.get('@nocobase/plugin-workflow')) as
|
|
20
|
+
| WorkflowPlugin
|
|
21
|
+
| undefined;
|
|
22
|
+
workflow?.registerInstruction?.('document-understanding', LegacyDocumentUnderstandingInstruction);
|
|
12
23
|
}
|
|
13
24
|
}
|
|
14
25
|
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
|
|
3
|
+
import React, { useCallback, useEffect, useState } from 'react';
|
|
4
|
+
import { Alert, Button, Form, Input, Modal, Popconfirm, Select, Space, Table, Tag, Typography, message } from 'antd';
|
|
5
|
+
import type { ColumnsType } from 'antd/es/table';
|
|
6
|
+
import { KeyOutlined, PlusOutlined, StopOutlined } from '@ant-design/icons';
|
|
7
|
+
import { useFlowContext } from '@nocobase/flow-engine';
|
|
8
|
+
import { useT } from '../locale';
|
|
9
|
+
import { DuApiKeySummary, IssuedDuApiKey, errorMessage, formatDateTime, unwrapData } from '../types';
|
|
10
|
+
|
|
11
|
+
export const ApiKeysTab: React.FC = () => {
|
|
12
|
+
const ctx = useFlowContext();
|
|
13
|
+
const api = ctx.api;
|
|
14
|
+
const t = useT();
|
|
15
|
+
const [keys, setKeys] = useState<DuApiKeySummary[]>([]);
|
|
16
|
+
const [loading, setLoading] = useState(false);
|
|
17
|
+
const [modalOpen, setModalOpen] = useState(false);
|
|
18
|
+
const [issuing, setIssuing] = useState(false);
|
|
19
|
+
const [newlyIssued, setNewlyIssued] = useState<IssuedDuApiKey | null>(null);
|
|
20
|
+
const [form] = Form.useForm();
|
|
21
|
+
|
|
22
|
+
const fetchKeys = useCallback(async () => {
|
|
23
|
+
setLoading(true);
|
|
24
|
+
try {
|
|
25
|
+
const res = await api.request({ url: 'docUnderstanding:listApiKeys' });
|
|
26
|
+
setKeys(unwrapData<DuApiKeySummary[]>(res, []));
|
|
27
|
+
} finally {
|
|
28
|
+
setLoading(false);
|
|
29
|
+
}
|
|
30
|
+
}, [api]);
|
|
31
|
+
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
fetchKeys();
|
|
34
|
+
}, [fetchKeys]);
|
|
35
|
+
|
|
36
|
+
const handleIssue = async () => {
|
|
37
|
+
setIssuing(true);
|
|
38
|
+
try {
|
|
39
|
+
const values = await form.validateFields();
|
|
40
|
+
const res = await api.request({
|
|
41
|
+
url: 'docUnderstanding:issueApiKey',
|
|
42
|
+
method: 'POST',
|
|
43
|
+
data: {
|
|
44
|
+
name: values.name,
|
|
45
|
+
tenantId: values.tenantId || 'default',
|
|
46
|
+
allowedBusinesses: values.allowedBusinesses?.length ? values.allowedBusinesses : ['*'],
|
|
47
|
+
allowedActions: values.allowedActions?.length ? values.allowedActions : ['*'],
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
const created = unwrapData<IssuedDuApiKey | null>(res, null);
|
|
51
|
+
setNewlyIssued(created);
|
|
52
|
+
setModalOpen(false);
|
|
53
|
+
fetchKeys();
|
|
54
|
+
message.success(t('API key issued'));
|
|
55
|
+
} catch (err) {
|
|
56
|
+
message.error(errorMessage(err));
|
|
57
|
+
} finally {
|
|
58
|
+
setIssuing(false);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const handleRevoke = async (id: number) => {
|
|
63
|
+
await api.request({
|
|
64
|
+
url: 'docUnderstanding:revokeApiKey',
|
|
65
|
+
method: 'POST',
|
|
66
|
+
params: { filterByTk: id },
|
|
67
|
+
});
|
|
68
|
+
message.success(t('API key revoked'));
|
|
69
|
+
fetchKeys();
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const columns: ColumnsType<DuApiKeySummary> = [
|
|
73
|
+
{ title: t('Name'), dataIndex: 'name', width: 180 },
|
|
74
|
+
{
|
|
75
|
+
title: t('Key Prefix'),
|
|
76
|
+
dataIndex: 'keyPrefix',
|
|
77
|
+
width: 160,
|
|
78
|
+
render: (v: string) => <code>{v}...</code>,
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
title: t('Tenant'),
|
|
82
|
+
dataIndex: 'tenantId',
|
|
83
|
+
width: 120,
|
|
84
|
+
render: (v: string) => <Tag>{v}</Tag>,
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
title: t('Allowed Businesses'),
|
|
88
|
+
dataIndex: 'allowedBusinesses',
|
|
89
|
+
width: 220,
|
|
90
|
+
render: (list: string[]) => (list || ['*']).map((b) => <Tag key={b}>{b}</Tag>),
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
title: t('Status'),
|
|
94
|
+
dataIndex: 'status',
|
|
95
|
+
width: 100,
|
|
96
|
+
render: (s: string) => <Tag color={s === 'active' ? 'success' : 'error'}>{s}</Tag>,
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
title: t('Last Used'),
|
|
100
|
+
dataIndex: 'lastUsedAt',
|
|
101
|
+
width: 165,
|
|
102
|
+
render: (v: string) => formatDateTime(v),
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
title: t('Action'),
|
|
106
|
+
width: 110,
|
|
107
|
+
render: (_: unknown, record) =>
|
|
108
|
+
record.status === 'active' ? (
|
|
109
|
+
<Popconfirm title={t('Revoke this API key?')} onConfirm={() => handleRevoke(record.id)}>
|
|
110
|
+
<Button size="small" danger icon={<StopOutlined />} aria-label={`${t('Revoke')} ${record.name}`}>
|
|
111
|
+
{t('Revoke')}
|
|
112
|
+
</Button>
|
|
113
|
+
</Popconfirm>
|
|
114
|
+
) : null,
|
|
115
|
+
},
|
|
116
|
+
];
|
|
117
|
+
|
|
118
|
+
return (
|
|
119
|
+
<div>
|
|
120
|
+
<Alert
|
|
121
|
+
type="info"
|
|
122
|
+
showIcon
|
|
123
|
+
icon={<KeyOutlined />}
|
|
124
|
+
message={t('Gateway API Keys (x-api-key)')}
|
|
125
|
+
description={t(
|
|
126
|
+
'External integrations pass x-api-key to call /api/du/v1/* or /api/v1/{uploads,businesses,operations,artifacts,usage}. Keys are stored as one-way SHA-256 hashes and isolated by Tenant ID.',
|
|
127
|
+
)}
|
|
128
|
+
style={{ marginBottom: 16 }}
|
|
129
|
+
/>
|
|
130
|
+
|
|
131
|
+
{newlyIssued && (
|
|
132
|
+
<Alert
|
|
133
|
+
type="success"
|
|
134
|
+
showIcon
|
|
135
|
+
closable
|
|
136
|
+
onClose={() => setNewlyIssued(null)}
|
|
137
|
+
message={t('Copy your new x-api-key now — it will never be shown again')}
|
|
138
|
+
description={
|
|
139
|
+
<Typography.Paragraph copyable style={{ margin: '8px 0 0', fontFamily: 'monospace' }}>
|
|
140
|
+
{newlyIssued.rawKey}
|
|
141
|
+
</Typography.Paragraph>
|
|
142
|
+
}
|
|
143
|
+
style={{ marginBottom: 16 }}
|
|
144
|
+
/>
|
|
145
|
+
)}
|
|
146
|
+
|
|
147
|
+
<Space style={{ marginBottom: 16 }}>
|
|
148
|
+
<Button
|
|
149
|
+
type="primary"
|
|
150
|
+
icon={<PlusOutlined />}
|
|
151
|
+
onClick={() => {
|
|
152
|
+
form.resetFields();
|
|
153
|
+
setModalOpen(true);
|
|
154
|
+
}}
|
|
155
|
+
>
|
|
156
|
+
{t('Issue API Key')}
|
|
157
|
+
</Button>
|
|
158
|
+
</Space>
|
|
159
|
+
|
|
160
|
+
<Table rowKey="id" columns={columns} dataSource={keys} loading={loading} size="small" />
|
|
161
|
+
|
|
162
|
+
<Modal
|
|
163
|
+
title={t('Issue Gateway API Key')}
|
|
164
|
+
open={modalOpen}
|
|
165
|
+
onOk={handleIssue}
|
|
166
|
+
onCancel={() => setModalOpen(false)}
|
|
167
|
+
confirmLoading={issuing}
|
|
168
|
+
okText={t('Issue Key')}
|
|
169
|
+
cancelText={t('Cancel')}
|
|
170
|
+
destroyOnClose
|
|
171
|
+
>
|
|
172
|
+
<Form form={form} layout="vertical" initialValues={{ tenantId: 'default', allowedBusinesses: ['*'] }}>
|
|
173
|
+
<Form.Item name="name" label={t('Key Name')} rules={[{ required: true }]}>
|
|
174
|
+
<Input placeholder="ERP Invoice Integration" />
|
|
175
|
+
</Form.Item>
|
|
176
|
+
<Form.Item
|
|
177
|
+
name="tenantId"
|
|
178
|
+
label={t('Tenant ID')}
|
|
179
|
+
help={t('Operations and artifacts are isolated per Tenant ID (cross-tenant lookups return 404)')}
|
|
180
|
+
>
|
|
181
|
+
<Input placeholder="default" />
|
|
182
|
+
</Form.Item>
|
|
183
|
+
<Form.Item name="allowedBusinesses" label={t('Allowed Businesses')}>
|
|
184
|
+
<Select
|
|
185
|
+
mode="multiple"
|
|
186
|
+
options={[
|
|
187
|
+
{ value: '*', label: t('All Businesses (*)') },
|
|
188
|
+
{ value: 'document-core', label: 'document-core' },
|
|
189
|
+
{ value: 'lc-checker', label: 'lc-checker' },
|
|
190
|
+
{ value: 'example-review', label: 'example-review' },
|
|
191
|
+
]}
|
|
192
|
+
/>
|
|
193
|
+
</Form.Item>
|
|
194
|
+
</Form>
|
|
195
|
+
</Modal>
|
|
196
|
+
</div>
|
|
197
|
+
);
|
|
198
|
+
};
|
|
@@ -1,3 +1,12 @@
|
|
|
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 React, { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
11
|
import {
|
|
3
12
|
Table,
|
|
@@ -260,8 +269,18 @@ export const EndpointsTab = () => {
|
|
|
260
269
|
};
|
|
261
270
|
|
|
262
271
|
const columns: ColumnsType<EndpointDef> = [
|
|
263
|
-
{ title: t('Name'), dataIndex: 'name', width:
|
|
264
|
-
{
|
|
272
|
+
{ title: t('Name'), dataIndex: 'name', width: 170 },
|
|
273
|
+
{
|
|
274
|
+
title: t('Protocol'),
|
|
275
|
+
dataIndex: 'protocolMode',
|
|
276
|
+
width: 130,
|
|
277
|
+
render: (v: string) => {
|
|
278
|
+
if (v === 'builtin_du') return <Tag color="geekblue">du-rework (Built-in)</Tag>;
|
|
279
|
+
if (v === 'du_rework_v1') return <Tag color="blue">du-rework (Remote)</Tag>;
|
|
280
|
+
return <Tag>{t('Custom REST')}</Tag>;
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
{ title: t('Subpath'), dataIndex: 'subpath', width: 220 },
|
|
265
284
|
{
|
|
266
285
|
title: t('Method'),
|
|
267
286
|
dataIndex: 'method',
|
|
@@ -411,6 +430,24 @@ export const EndpointsTab = () => {
|
|
|
411
430
|
label: t('Gateway / Discriminator'),
|
|
412
431
|
children: (
|
|
413
432
|
<>
|
|
433
|
+
<Form.Item name="protocolMode" label={t('Protocol Mode')} initialValue="legacy_rest">
|
|
434
|
+
<Select
|
|
435
|
+
style={{ width: 280 }}
|
|
436
|
+
options={[
|
|
437
|
+
{ value: 'builtin_du', label: t('Canonical du-rework (Built-in / Hybrid)') },
|
|
438
|
+
{ value: 'du_rework_v1', label: t('Canonical du-rework (External Gateway)') },
|
|
439
|
+
{ value: 'legacy_rest', label: t('Legacy / Generic HTTP REST') },
|
|
440
|
+
]}
|
|
441
|
+
/>
|
|
442
|
+
</Form.Item>
|
|
443
|
+
<Space size="middle">
|
|
444
|
+
<Form.Item name="businessId" label={t('Business ID')}>
|
|
445
|
+
<Input placeholder="document-core" style={{ width: 180 }} />
|
|
446
|
+
</Form.Item>
|
|
447
|
+
<Form.Item name="actionName" label={t('Action Name')}>
|
|
448
|
+
<Input placeholder="extract" style={{ width: 180 }} />
|
|
449
|
+
</Form.Item>
|
|
450
|
+
</Space>
|
|
414
451
|
<Form.Item
|
|
415
452
|
name="discriminatorField"
|
|
416
453
|
label={t('Discriminator Field')}
|