plugin-document-understanding 1.1.2 → 1.1.4
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 +21 -8
- package/client-v2.d.ts +2 -0
- package/client-v2.js +1 -0
- package/dist/client/index.js +1 -1
- package/dist/client-v2/794.9b984a25b2fc7c14.js +10 -0
- package/dist/client-v2/index.js +10 -0
- package/dist/externalVersion.js +7 -6
- package/dist/node_modules/form-data/License +19 -0
- package/dist/node_modules/form-data/index.d.ts +62 -0
- package/dist/node_modules/form-data/lib/browser.js +4 -0
- package/dist/node_modules/form-data/lib/form_data.js +14 -0
- package/dist/node_modules/form-data/lib/populate.js +10 -0
- package/dist/node_modules/form-data/package.json +1 -0
- package/dist/node_modules/mime-types/LICENSE +23 -0
- package/dist/node_modules/mime-types/index.js +14 -0
- package/dist/node_modules/mime-types/package.json +1 -0
- package/dist/server/actions.js +7 -6
- package/dist/server/collections/endpoints.js +1 -1
- package/dist/server/plugin.js +7 -3
- package/dist/server/services/AsyncJobManager.js +14 -5
- package/dist/server/services/DocumentUnderstandingService.js +53 -23
- package/dist/server/services/PipelineExecutor.js +81 -94
- package/package.json +49 -16
- package/src/client/index.tsx +9 -0
- package/src/client-v2/client.d.ts +249 -0
- package/src/client-v2/components/EndpointsTab.tsx +458 -0
- package/src/client-v2/components/JobsTab.tsx +259 -0
- package/src/client-v2/components/PipelinesTab.tsx +872 -0
- package/src/client-v2/components/PluginSettings.tsx +48 -0
- package/src/client-v2/components/ServiceConfigTab.tsx +122 -0
- package/src/client-v2/locale.ts +21 -0
- package/{dist/client/models/index.d.ts → src/client-v2/models/index.ts} +3 -2
- package/src/client-v2/plugin.tsx +21 -0
- package/src/locale/en-US.json +121 -0
- package/src/locale/vi-VN.json +121 -0
- package/src/locale/zh-CN.json +1 -0
- package/src/server/__tests__/actions.test.ts +98 -0
- package/src/server/__tests__/pipeline-executor.test.ts +33 -0
- package/src/server/__tests__/smoke.test.ts +55 -0
- package/src/server/actions.ts +176 -0
- package/src/server/collections/.gitkeep +0 -0
- package/src/server/collections/config.ts +18 -0
- package/src/server/collections/endpoints.ts +28 -0
- package/src/server/collections/jobs.ts +29 -0
- package/src/server/collections/pipeline-steps.ts +27 -0
- package/src/server/collections/pipelines.ts +19 -0
- package/src/server/plugin.ts +108 -0
- package/src/server/services/AsyncJobManager.ts +172 -0
- package/src/server/services/DocumentUnderstandingService.ts +264 -0
- package/src/server/services/ExternalApiClient.ts +143 -0
- package/src/server/services/PipelineExecutor.ts +502 -0
- package/src/server/tools/document-understanding-tool.ts +181 -0
- package/src/server/types.ts +73 -0
- package/dist/client/components/EndpointsTab.d.ts +0 -2
- package/dist/client/components/JobsTab.d.ts +0 -2
- package/dist/client/components/PipelinesTab.d.ts +0 -2
- package/dist/client/components/PluginSettings.d.ts +0 -2
- package/dist/client/components/ServiceConfigTab.d.ts +0 -2
- package/dist/client/locale.d.ts +0 -10
- package/dist/client/plugin.d.ts +0 -6
- package/dist/server/actions.d.ts +0 -17
- package/dist/server/collections/config.d.ts +0 -3
- package/dist/server/collections/endpoints.d.ts +0 -3
- package/dist/server/collections/jobs.d.ts +0 -3
- package/dist/server/collections/pipeline-steps.d.ts +0 -3
- package/dist/server/collections/pipelines.d.ts +0 -3
- package/dist/server/plugin.d.ts +0 -15
- package/dist/server/services/AsyncJobManager.d.ts +0 -27
- package/dist/server/services/DocumentUnderstandingService.d.ts +0 -35
- package/dist/server/services/ExternalApiClient.d.ts +0 -28
- package/dist/server/services/PipelineExecutor.d.ts +0 -24
- package/dist/server/tools/document-understanding-tool.d.ts +0 -8
- package/dist/server/types.d.ts +0 -69
- /package/{dist/client/index.d.ts → src/client-v2/index.tsx} +0 -0
- /package/{dist/index.d.ts → src/index.ts} +0 -0
- /package/{dist/server/index.d.ts → src/server/index.ts} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name":"mime-types","description":"The ultimate javascript content-type utility.","version":"2.1.35","contributors":["Douglas Christopher Wilson <doug@somethingdoug.com>","Jeremiah Senkpiel <fishrock123@rocketmail.com> (https://searchbeam.jit.su)","Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)"],"license":"MIT","keywords":["mime","types"],"repository":"jshttp/mime-types","dependencies":{"mime-db":"1.52.0"},"devDependencies":{"eslint":"7.32.0","eslint-config-standard":"14.1.1","eslint-plugin-import":"2.25.4","eslint-plugin-markdown":"2.2.1","eslint-plugin-node":"11.1.0","eslint-plugin-promise":"5.2.0","eslint-plugin-standard":"4.1.0","mocha":"9.2.2","nyc":"15.1.0"},"files":["HISTORY.md","LICENSE","index.js"],"engines":{"node":">= 0.6"},"scripts":{"lint":"eslint .","test":"mocha --reporter spec test/test.js","test-ci":"nyc --reporter=lcov --reporter=text npm test","test-cov":"nyc --reporter=html --reporter=text npm test"},"_lastModified":"2026-05-21T19:00:25.493Z"}
|
package/dist/server/actions.js
CHANGED
|
@@ -29,6 +29,7 @@ __export(actions_exports, {
|
|
|
29
29
|
defineActions: () => defineActions
|
|
30
30
|
});
|
|
31
31
|
module.exports = __toCommonJS(actions_exports);
|
|
32
|
+
var import_fs = require("fs");
|
|
32
33
|
const defineActions = (plugin) => {
|
|
33
34
|
const service = plugin.service;
|
|
34
35
|
return {
|
|
@@ -91,23 +92,23 @@ const defineActions = (plugin) => {
|
|
|
91
92
|
const input = (values == null ? void 0 : values.input) || {};
|
|
92
93
|
const userId = (_b = (_a = ctx.state) == null ? void 0 : _a.currentUser) == null ? void 0 : _b.id;
|
|
93
94
|
const multipartFiles = [];
|
|
94
|
-
const
|
|
95
|
+
const request = ctx.request;
|
|
96
|
+
const filesObj = request.files || ((_c = request.body) == null ? void 0 : _c.files);
|
|
95
97
|
if (filesObj) {
|
|
96
98
|
const addFile = (key, file) => {
|
|
97
99
|
const filePath = file.path || file.filepath;
|
|
98
100
|
const fileName = file.name || file.originalFilename || "file";
|
|
99
101
|
const mimeType = file.type || file.mimetype || "application/octet-stream";
|
|
100
|
-
if (filePath) {
|
|
102
|
+
if (filePath && (0, import_fs.existsSync)(filePath)) {
|
|
101
103
|
try {
|
|
102
|
-
const buffer = require("fs").readFileSync(filePath);
|
|
103
104
|
multipartFiles.push({
|
|
104
105
|
fieldName: key,
|
|
105
|
-
buffer,
|
|
106
|
+
buffer: (0, import_fs.readFileSync)(filePath),
|
|
106
107
|
filename: fileName,
|
|
107
108
|
mimeType
|
|
108
109
|
});
|
|
109
110
|
} catch (err) {
|
|
110
|
-
|
|
111
|
+
plugin.app.logger.warn("Failed to read uploaded multipart file", err);
|
|
111
112
|
}
|
|
112
113
|
}
|
|
113
114
|
};
|
|
@@ -143,7 +144,7 @@ const defineActions = (plugin) => {
|
|
|
143
144
|
await service.handleWebhook(values || ctx.request.body, signature);
|
|
144
145
|
ctx.body = { received: true };
|
|
145
146
|
} catch (err) {
|
|
146
|
-
|
|
147
|
+
plugin.app.logger.warn(`Webhook callback error: ${err.message}`);
|
|
147
148
|
if (err.message.includes("signature")) {
|
|
148
149
|
ctx.status = 401;
|
|
149
150
|
ctx.body = { error: err.message };
|
|
@@ -52,6 +52,6 @@ var endpoints_default = {
|
|
|
52
52
|
{ type: "string", name: "pollCompletedValue", length: 100, defaultValue: "completed" },
|
|
53
53
|
{ type: "json", name: "customHeaders" },
|
|
54
54
|
{ type: "boolean", name: "enabled", defaultValue: true },
|
|
55
|
-
{ type: "
|
|
55
|
+
{ type: "integer", name: "sortOrder", defaultValue: 0 }
|
|
56
56
|
]
|
|
57
57
|
};
|
package/dist/server/plugin.js
CHANGED
|
@@ -35,6 +35,11 @@ var import_path = require("path");
|
|
|
35
35
|
var import_DocumentUnderstandingService = require("./services/DocumentUnderstandingService");
|
|
36
36
|
var import_actions = require("./actions");
|
|
37
37
|
var import_document_understanding_tool = require("./tools/document-understanding-tool");
|
|
38
|
+
const isAiPluginLike = (plugin) => {
|
|
39
|
+
var _a, _b;
|
|
40
|
+
const candidate = plugin;
|
|
41
|
+
return typeof ((_b = (_a = candidate == null ? void 0 : candidate.ai) == null ? void 0 : _a.toolsManager) == null ? void 0 : _b.registerDynamicTools) === "function";
|
|
42
|
+
};
|
|
38
43
|
class PluginDocumentUnderstandingServer extends import_server.Plugin {
|
|
39
44
|
service;
|
|
40
45
|
async afterAdd() {
|
|
@@ -48,7 +53,7 @@ class PluginDocumentUnderstandingServer extends import_server.Plugin {
|
|
|
48
53
|
this.service = new import_DocumentUnderstandingService.DocumentUnderstandingService(this.app, this.db);
|
|
49
54
|
this.app.on("afterStart", async () => {
|
|
50
55
|
try {
|
|
51
|
-
await this.service.initialize();
|
|
56
|
+
await this.service.initialize({ recoverJobs: true });
|
|
52
57
|
} catch (err) {
|
|
53
58
|
this.app.logger.warn("Document Understanding plugin not configured yet, skip init.", err);
|
|
54
59
|
}
|
|
@@ -84,10 +89,9 @@ class PluginDocumentUnderstandingServer extends import_server.Plugin {
|
|
|
84
89
|
});
|
|
85
90
|
}
|
|
86
91
|
registerAITools() {
|
|
87
|
-
var _a;
|
|
88
92
|
try {
|
|
89
93
|
const aiPlugin = this.app.pm.get("@nocobase/plugin-ai");
|
|
90
|
-
if (!(
|
|
94
|
+
if (!isAiPluginLike(aiPlugin)) {
|
|
91
95
|
this.app.logger.warn("Document Understanding: plugin-ai not available, skip AI tool registration.");
|
|
92
96
|
return;
|
|
93
97
|
}
|
|
@@ -35,11 +35,13 @@ class AsyncJobManager {
|
|
|
35
35
|
intervals = /* @__PURE__ */ new Map();
|
|
36
36
|
onJobComplete;
|
|
37
37
|
onJobError;
|
|
38
|
-
|
|
38
|
+
logger;
|
|
39
|
+
constructor(db, apiClient, callbacks, logger) {
|
|
39
40
|
this.db = db;
|
|
40
41
|
this.apiClient = apiClient;
|
|
41
42
|
this.onJobComplete = callbacks.onJobComplete;
|
|
42
43
|
this.onJobError = callbacks.onJobError;
|
|
44
|
+
this.logger = logger;
|
|
43
45
|
}
|
|
44
46
|
async startPolling(jobId, endpoint, taskId, customInterval, customTimeout) {
|
|
45
47
|
if (this.intervals.has(jobId)) return;
|
|
@@ -53,7 +55,12 @@ class AsyncJobManager {
|
|
|
53
55
|
await this.onJobError(jobId, "Polling timeout exceeded");
|
|
54
56
|
return;
|
|
55
57
|
}
|
|
56
|
-
const
|
|
58
|
+
const pollResultSubpath = endpoint.pollResultSubpath;
|
|
59
|
+
if (!pollResultSubpath) {
|
|
60
|
+
await this.onJobError(jobId, "Polling result subpath is not configured");
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const url = pollResultSubpath.replace("{taskId}", taskId);
|
|
57
64
|
const response = await this.apiClient.get(url);
|
|
58
65
|
if (endpoint.pollStatusField) {
|
|
59
66
|
const statusValue = this.getNestedValue(response, endpoint.pollStatusField);
|
|
@@ -74,7 +81,8 @@ class AsyncJobManager {
|
|
|
74
81
|
}
|
|
75
82
|
}
|
|
76
83
|
} catch (err) {
|
|
77
|
-
|
|
84
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
85
|
+
this.logger.warn(`Polling error for job ${jobId}: ${message}`);
|
|
78
86
|
}
|
|
79
87
|
}, interval);
|
|
80
88
|
this.intervals.set(jobId, timer);
|
|
@@ -125,8 +133,9 @@ class AsyncJobManager {
|
|
|
125
133
|
}
|
|
126
134
|
}
|
|
127
135
|
stopPolling(jobId) {
|
|
128
|
-
|
|
129
|
-
|
|
136
|
+
const timer = this.intervals.get(jobId);
|
|
137
|
+
if (timer) {
|
|
138
|
+
clearInterval(timer);
|
|
130
139
|
this.intervals.delete(jobId);
|
|
131
140
|
}
|
|
132
141
|
}
|
|
@@ -50,12 +50,58 @@ class DocumentUnderstandingService {
|
|
|
50
50
|
pipelineExecutor;
|
|
51
51
|
jobManager;
|
|
52
52
|
initialized = false;
|
|
53
|
+
startupRecoveryCompleted = false;
|
|
54
|
+
initializationPromise;
|
|
53
55
|
static HEADER_NAME_RE = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
|
|
54
56
|
constructor(app, db) {
|
|
55
57
|
this.app = app;
|
|
56
58
|
this.db = db;
|
|
57
59
|
}
|
|
58
|
-
async initialize() {
|
|
60
|
+
async initialize(options = {}) {
|
|
61
|
+
if (this.initializationPromise) {
|
|
62
|
+
await this.initializationPromise;
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
this.initializationPromise = this.doInitialize(options.recoverJobs ?? false).finally(() => {
|
|
66
|
+
this.initializationPromise = void 0;
|
|
67
|
+
});
|
|
68
|
+
await this.initializationPromise;
|
|
69
|
+
}
|
|
70
|
+
async ensureInitialized() {
|
|
71
|
+
if (this.initialized) return;
|
|
72
|
+
await this.initialize();
|
|
73
|
+
}
|
|
74
|
+
async doInitialize(recoverJobs) {
|
|
75
|
+
var _a;
|
|
76
|
+
const config = await this.ensureConfig();
|
|
77
|
+
(_a = this.jobManager) == null ? void 0 : _a.destroy();
|
|
78
|
+
this.apiClient = new import_ExternalApiClient.ExternalApiClient(config);
|
|
79
|
+
this.jobManager = new import_AsyncJobManager.AsyncJobManager(
|
|
80
|
+
this.db,
|
|
81
|
+
this.apiClient,
|
|
82
|
+
{
|
|
83
|
+
onJobComplete: async (jobId, result) => {
|
|
84
|
+
await this.pipelineExecutor.resumeFromStep(jobId, result);
|
|
85
|
+
},
|
|
86
|
+
onJobError: async (jobId, error) => {
|
|
87
|
+
const jobsRepo = this.db.getRepository("doc_understanding_jobs");
|
|
88
|
+
await jobsRepo.update({
|
|
89
|
+
filterByTk: jobId,
|
|
90
|
+
values: { status: "failed", error, completedAt: /* @__PURE__ */ new Date() }
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
this.app.logger
|
|
95
|
+
);
|
|
96
|
+
this.pipelineExecutor = new import_PipelineExecutor.PipelineExecutor(this.db, this.apiClient, this.jobManager, this.app.logger);
|
|
97
|
+
if (recoverJobs && !this.startupRecoveryCompleted) {
|
|
98
|
+
await this.jobManager.cleanupStuckJobs();
|
|
99
|
+
await this.jobManager.recoverPollingJobs();
|
|
100
|
+
this.startupRecoveryCompleted = true;
|
|
101
|
+
}
|
|
102
|
+
this.initialized = true;
|
|
103
|
+
}
|
|
104
|
+
async ensureConfig() {
|
|
59
105
|
const configRepo = this.db.getRepository("doc_understanding_config");
|
|
60
106
|
let config = await configRepo.findOne();
|
|
61
107
|
if (!config) {
|
|
@@ -70,32 +116,13 @@ class DocumentUnderstandingService {
|
|
|
70
116
|
}
|
|
71
117
|
});
|
|
72
118
|
}
|
|
73
|
-
|
|
74
|
-
this.jobManager = new import_AsyncJobManager.AsyncJobManager(this.db, this.apiClient, {
|
|
75
|
-
onJobComplete: async (jobId, result) => {
|
|
76
|
-
await this.pipelineExecutor.resumeFromStep(jobId, result);
|
|
77
|
-
},
|
|
78
|
-
onJobError: async (jobId, error) => {
|
|
79
|
-
const jobsRepo = this.db.getRepository("doc_understanding_jobs");
|
|
80
|
-
await jobsRepo.update({
|
|
81
|
-
filterByTk: jobId,
|
|
82
|
-
values: { status: "failed", error, completedAt: /* @__PURE__ */ new Date() }
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
this.pipelineExecutor = new import_PipelineExecutor.PipelineExecutor(this.db, this.apiClient, this.jobManager);
|
|
87
|
-
await this.jobManager.cleanupStuckJobs();
|
|
88
|
-
await this.jobManager.recoverPollingJobs();
|
|
89
|
-
this.initialized = true;
|
|
119
|
+
return config;
|
|
90
120
|
}
|
|
91
121
|
isReady() {
|
|
92
122
|
return this.initialized;
|
|
93
123
|
}
|
|
94
124
|
async getConfig() {
|
|
95
|
-
|
|
96
|
-
const conf = await configRepo.findOne();
|
|
97
|
-
if (!conf) throw new Error("Config not found");
|
|
98
|
-
return conf;
|
|
125
|
+
return this.ensureConfig();
|
|
99
126
|
}
|
|
100
127
|
async updateConfig(data) {
|
|
101
128
|
const configRepo = this.db.getRepository("doc_understanding_config");
|
|
@@ -106,6 +133,7 @@ class DocumentUnderstandingService {
|
|
|
106
133
|
await this.initialize();
|
|
107
134
|
}
|
|
108
135
|
async listEndpoints() {
|
|
136
|
+
await this.ensureInitialized();
|
|
109
137
|
return this.db.getRepository("doc_understanding_endpoints").find({ sort: "sortOrder" });
|
|
110
138
|
}
|
|
111
139
|
async createEndpoint(data) {
|
|
@@ -146,6 +174,7 @@ class DocumentUnderstandingService {
|
|
|
146
174
|
await this.db.getRepository("doc_understanding_endpoints").destroy({ filterByTk: id });
|
|
147
175
|
}
|
|
148
176
|
async listPipelines() {
|
|
177
|
+
await this.ensureInitialized();
|
|
149
178
|
return this.db.getRepository("doc_understanding_pipelines").find({
|
|
150
179
|
appends: ["steps", "steps.endpoint"]
|
|
151
180
|
});
|
|
@@ -170,7 +199,7 @@ class DocumentUnderstandingService {
|
|
|
170
199
|
await this.db.getRepository("doc_understanding_pipelines").destroy({ filterByTk: id });
|
|
171
200
|
}
|
|
172
201
|
async executePipeline(pipelineId, input, files = [], userId) {
|
|
173
|
-
|
|
202
|
+
await this.ensureInitialized();
|
|
174
203
|
const job = await this.pipelineExecutor.execute(pipelineId, input, files, userId);
|
|
175
204
|
return { jobId: job.id };
|
|
176
205
|
}
|
|
@@ -186,6 +215,7 @@ class DocumentUnderstandingService {
|
|
|
186
215
|
});
|
|
187
216
|
}
|
|
188
217
|
async handleWebhook(payload, signature) {
|
|
218
|
+
await this.ensureInitialized();
|
|
189
219
|
const config = await this.getConfig();
|
|
190
220
|
if (config.webhookSecret) {
|
|
191
221
|
if (!signature) {
|
|
@@ -7,9 +7,11 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
var __create = Object.create;
|
|
10
11
|
var __defProp = Object.defineProperty;
|
|
11
12
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
13
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
13
15
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
16
|
var __export = (target, all) => {
|
|
15
17
|
for (var name in all)
|
|
@@ -23,127 +25,109 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
23
25
|
}
|
|
24
26
|
return to;
|
|
25
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
|
+
));
|
|
26
36
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
37
|
var PipelineExecutor_exports = {};
|
|
28
38
|
__export(PipelineExecutor_exports, {
|
|
29
|
-
PipelineExecutor: () => PipelineExecutor
|
|
39
|
+
PipelineExecutor: () => PipelineExecutor,
|
|
40
|
+
resolveUploadFilePath: () => resolveUploadFilePath
|
|
30
41
|
});
|
|
31
42
|
module.exports = __toCommonJS(PipelineExecutor_exports);
|
|
43
|
+
var import_fs = require("fs");
|
|
44
|
+
var import_path = require("path");
|
|
45
|
+
var import_axios = __toESM(require("axios"));
|
|
46
|
+
var import_mime_types = __toESM(require("mime-types"));
|
|
47
|
+
const UPLOAD_ROOTS = [(0, import_path.resolve)(process.cwd(), "storage", "uploads")];
|
|
48
|
+
const isInsidePath = (basePath, targetPath) => targetPath === basePath || targetPath.startsWith(`${basePath}${import_path.sep}`);
|
|
49
|
+
function resolveUploadFilePath(urlOrPath) {
|
|
50
|
+
if (!urlOrPath || urlOrPath.includes("\0")) return null;
|
|
51
|
+
let pathname = urlOrPath.replace(/\\/g, "/").split(/[?#]/)[0];
|
|
52
|
+
try {
|
|
53
|
+
if (pathname.startsWith("http://") || pathname.startsWith("https://")) {
|
|
54
|
+
pathname = new URL(pathname).pathname;
|
|
55
|
+
}
|
|
56
|
+
} catch {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
pathname = decodeURIComponent(pathname).replace(/^\/+/, "");
|
|
60
|
+
const uploadsIndex = pathname.indexOf("uploads/");
|
|
61
|
+
if (pathname.startsWith("storage/uploads/")) {
|
|
62
|
+
pathname = pathname.slice("storage/uploads/".length);
|
|
63
|
+
} else if (pathname.startsWith("uploads/")) {
|
|
64
|
+
pathname = pathname.slice("uploads/".length);
|
|
65
|
+
} else if (uploadsIndex >= 0) {
|
|
66
|
+
pathname = pathname.slice(uploadsIndex + "uploads/".length);
|
|
67
|
+
} else {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
const parts = pathname.split("/").filter(Boolean);
|
|
71
|
+
if (parts.some((part) => part === ".." || part.includes("\0"))) return null;
|
|
72
|
+
for (const root of UPLOAD_ROOTS) {
|
|
73
|
+
const candidate = (0, import_path.resolve)(root, ...parts);
|
|
74
|
+
const realRoot = (0, import_fs.existsSync)(root) ? (0, import_fs.realpathSync)(root) : root;
|
|
75
|
+
if (!(0, import_fs.existsSync)(candidate)) continue;
|
|
76
|
+
const realCandidate = (0, import_fs.realpathSync)(candidate);
|
|
77
|
+
if (isInsidePath(realRoot, realCandidate) && (0, import_fs.statSync)(realCandidate).isFile()) {
|
|
78
|
+
return realCandidate;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
32
83
|
class PipelineExecutor {
|
|
33
84
|
db;
|
|
34
85
|
apiClient;
|
|
35
86
|
jobManager;
|
|
36
|
-
|
|
87
|
+
logger;
|
|
88
|
+
constructor(db, apiClient, jobManager, logger) {
|
|
37
89
|
this.db = db;
|
|
38
90
|
this.apiClient = apiClient;
|
|
39
91
|
this.jobManager = jobManager;
|
|
92
|
+
this.logger = logger;
|
|
40
93
|
}
|
|
41
94
|
async resolveFile(urlOrPath, fieldName = "file") {
|
|
95
|
+
var _a, _b;
|
|
42
96
|
if (!urlOrPath || typeof urlOrPath !== "string") return null;
|
|
43
97
|
try {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (urlOrPath.startsWith("/")) {
|
|
49
|
-
cleanedPath = urlOrPath.slice(1);
|
|
50
|
-
}
|
|
51
|
-
const pathOptions = [
|
|
52
|
-
require("path").resolve(process.cwd(), cleanedPath),
|
|
53
|
-
require("path").resolve(process.cwd(), "storage", cleanedPath),
|
|
54
|
-
require("path").resolve(process.cwd(), "storage/uploads", cleanedPath.replace(/^uploads\//, ""))
|
|
55
|
-
];
|
|
56
|
-
for (const p of pathOptions) {
|
|
57
|
-
if (require("fs").existsSync(p) && require("fs").statSync(p).isFile()) {
|
|
58
|
-
isLocal = true;
|
|
59
|
-
absolutePath = p;
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
if (isLocal) {
|
|
65
|
-
const fs = require("fs");
|
|
66
|
-
const path = require("path");
|
|
67
|
-
const buffer = fs.readFileSync(absolutePath);
|
|
68
|
-
const filename = path.basename(absolutePath);
|
|
69
|
-
let mimeLookup = (filePath) => "application/octet-stream";
|
|
70
|
-
try {
|
|
71
|
-
const mime = require("mime-types");
|
|
72
|
-
mimeLookup = (filePath) => mime.lookup(filePath) || "application/octet-stream";
|
|
73
|
-
} catch (e) {
|
|
74
|
-
mimeLookup = (filePath) => {
|
|
75
|
-
const ext = path.extname(filePath).toLowerCase();
|
|
76
|
-
const map = {
|
|
77
|
-
".pdf": "application/pdf",
|
|
78
|
-
".doc": "application/msword",
|
|
79
|
-
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
80
|
-
".xls": "application/vnd.ms-excel",
|
|
81
|
-
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
82
|
-
".png": "image/png",
|
|
83
|
-
".jpg": "image/jpeg",
|
|
84
|
-
".jpeg": "image/jpeg",
|
|
85
|
-
".txt": "text/plain",
|
|
86
|
-
".json": "application/json"
|
|
87
|
-
};
|
|
88
|
-
return map[ext] || "application/octet-stream";
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
const mimeType = mimeLookup(absolutePath);
|
|
98
|
+
const localPath = resolveUploadFilePath(urlOrPath);
|
|
99
|
+
if (localPath) {
|
|
100
|
+
const buffer = (0, import_fs.readFileSync)(localPath);
|
|
101
|
+
const mimeType = import_mime_types.default.lookup(localPath) || "application/octet-stream";
|
|
92
102
|
return {
|
|
93
103
|
fieldName,
|
|
94
104
|
buffer,
|
|
95
|
-
filename,
|
|
105
|
+
filename: (0, import_path.basename)(localPath),
|
|
96
106
|
mimeType
|
|
97
107
|
};
|
|
98
108
|
}
|
|
99
109
|
if (urlOrPath.startsWith("http://") || urlOrPath.startsWith("https://")) {
|
|
100
|
-
const
|
|
101
|
-
const path = require("path");
|
|
102
|
-
const response = await axios.get(urlOrPath, { responseType: "arraybuffer" });
|
|
110
|
+
const response = await import_axios.default.get(urlOrPath, { responseType: "arraybuffer" });
|
|
103
111
|
const buffer = Buffer.from(response.data);
|
|
104
112
|
let filename = "file";
|
|
105
113
|
const disposition = response.headers["content-disposition"];
|
|
106
114
|
if (disposition && disposition.includes("filename=")) {
|
|
107
115
|
const match = disposition.match(/filename="?([^";]+)"?/);
|
|
108
|
-
if (match
|
|
116
|
+
if (match == null ? void 0 : match[1]) {
|
|
109
117
|
filename = match[1];
|
|
110
118
|
}
|
|
111
119
|
} else {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if (base && base.includes(".")) {
|
|
116
|
-
filename = base;
|
|
117
|
-
}
|
|
118
|
-
} catch (e) {
|
|
120
|
+
const base = (0, import_path.basename)(new URL(urlOrPath).pathname);
|
|
121
|
+
if (base && base.includes(".")) {
|
|
122
|
+
filename = base;
|
|
119
123
|
}
|
|
120
124
|
}
|
|
121
125
|
const contentType = response.headers["content-type"] || "application/octet-stream";
|
|
122
126
|
const mimeType = contentType.split(";")[0].trim();
|
|
123
127
|
if (!filename.includes(".")) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
mimeExtension = (mime) => mimeLib.extension(mime) || "";
|
|
128
|
-
} catch (e) {
|
|
129
|
-
mimeExtension = (mime) => {
|
|
130
|
-
const map = {
|
|
131
|
-
"application/pdf": "pdf",
|
|
132
|
-
"application/msword": "doc",
|
|
133
|
-
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
|
|
134
|
-
"application/vnd.ms-excel": "xls",
|
|
135
|
-
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
|
|
136
|
-
"image/png": "png",
|
|
137
|
-
"image/jpeg": "jpg",
|
|
138
|
-
"text/plain": "txt",
|
|
139
|
-
"application/json": "json"
|
|
140
|
-
};
|
|
141
|
-
return map[mime] || "";
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
const ext = mimeExtension(mimeType);
|
|
145
|
-
if (ext) {
|
|
146
|
-
filename = `${filename}.${ext}`;
|
|
128
|
+
const extension = import_mime_types.default.extension(mimeType);
|
|
129
|
+
if (extension) {
|
|
130
|
+
filename = `${filename}.${extension}`;
|
|
147
131
|
}
|
|
148
132
|
}
|
|
149
133
|
return {
|
|
@@ -154,7 +138,8 @@ class PipelineExecutor {
|
|
|
154
138
|
};
|
|
155
139
|
}
|
|
156
140
|
} catch (err) {
|
|
157
|
-
|
|
141
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
142
|
+
(_b = (_a = this.logger).warn) == null ? void 0 : _b.call(_a, `Failed to resolve file input: ${message}`);
|
|
158
143
|
}
|
|
159
144
|
return null;
|
|
160
145
|
}
|
|
@@ -189,10 +174,11 @@ class PipelineExecutor {
|
|
|
189
174
|
if (Array.isArray(files)) {
|
|
190
175
|
for (const f of files) {
|
|
191
176
|
if (f && typeof f === "object") {
|
|
192
|
-
|
|
177
|
+
const fileRef = f;
|
|
178
|
+
if (fileRef.buffer) {
|
|
193
179
|
resolvedFiles.push(f);
|
|
194
|
-
} else if (
|
|
195
|
-
await tryAddFile(
|
|
180
|
+
} else if (fileRef.url) {
|
|
181
|
+
await tryAddFile(fileRef.url, fileRef.fieldName || "file");
|
|
196
182
|
}
|
|
197
183
|
}
|
|
198
184
|
}
|
|
@@ -241,7 +227,7 @@ class PipelineExecutor {
|
|
|
241
227
|
};
|
|
242
228
|
const steps = [...pipeline.steps || []].sort((a, b) => a.stepOrder - b.stepOrder);
|
|
243
229
|
this.runSteps(job, steps, context).catch((err) => {
|
|
244
|
-
|
|
230
|
+
this.logger.error(`Background pipeline error for job ${job.id}`, err);
|
|
245
231
|
});
|
|
246
232
|
return job;
|
|
247
233
|
}
|
|
@@ -281,7 +267,7 @@ class PipelineExecutor {
|
|
|
281
267
|
const taskIdField = step.endpoint.pollTaskIdField || "task_id";
|
|
282
268
|
const taskId = this.getNestedValue(result.data, taskIdField);
|
|
283
269
|
if (!taskId) throw new Error(`Polling taskId not found in field ${taskIdField}`);
|
|
284
|
-
|
|
270
|
+
const externalTaskIds = job.externalTaskIds || {};
|
|
285
271
|
externalTaskIds[step.stepOrder.toString()] = taskId;
|
|
286
272
|
await jobsRepo.update({
|
|
287
273
|
filterByTk: job.id,
|
|
@@ -292,7 +278,7 @@ class PipelineExecutor {
|
|
|
292
278
|
} else if (step.endpoint.executionMode === "webhook") {
|
|
293
279
|
const taskIdField = step.endpoint.pollTaskIdField || "task_id";
|
|
294
280
|
const taskId = this.getNestedValue(result.data, taskIdField);
|
|
295
|
-
|
|
281
|
+
const externalTaskIds = job.externalTaskIds || {};
|
|
296
282
|
externalTaskIds[step.stepOrder.toString()] = taskId;
|
|
297
283
|
await jobsRepo.update({
|
|
298
284
|
filterByTk: job.id,
|
|
@@ -348,7 +334,7 @@ class PipelineExecutor {
|
|
|
348
334
|
jobId: job.id
|
|
349
335
|
};
|
|
350
336
|
this.runSteps(job, steps, context, job.currentStep + 1).catch((err) => {
|
|
351
|
-
|
|
337
|
+
this.logger.error(`Failed executing step ${job.currentStep + 1} for job ${job.id}`, err);
|
|
352
338
|
});
|
|
353
339
|
}
|
|
354
340
|
async finishJob(jobId, context) {
|
|
@@ -436,5 +422,6 @@ class PipelineExecutor {
|
|
|
436
422
|
}
|
|
437
423
|
// Annotate the CommonJS export names for ESM import in node:
|
|
438
424
|
0 && (module.exports = {
|
|
439
|
-
PipelineExecutor
|
|
425
|
+
PipelineExecutor,
|
|
426
|
+
resolveUploadFilePath
|
|
440
427
|
});
|
package/package.json
CHANGED
|
@@ -1,16 +1,49 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "plugin-document-understanding",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "plugin-document-understanding",
|
|
3
|
+
"displayName": "Document Understanding",
|
|
4
|
+
"displayName.zh-CN": "文档理解",
|
|
5
|
+
"description": "Configure external document processing endpoints, chain them into pipelines, and expose enabled pipelines as AI tools.",
|
|
6
|
+
"version": "1.1.4",
|
|
7
|
+
"license": "Apache-2.0",
|
|
8
|
+
"main": "dist/server/index.js",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"client.js",
|
|
12
|
+
"client.d.ts",
|
|
13
|
+
"client-v2.js",
|
|
14
|
+
"client-v2.d.ts",
|
|
15
|
+
"server.js",
|
|
16
|
+
"server.d.ts",
|
|
17
|
+
"README.md",
|
|
18
|
+
"src"
|
|
19
|
+
],
|
|
20
|
+
"nocobase": {
|
|
21
|
+
"supportedVersions": [
|
|
22
|
+
"2.x"
|
|
23
|
+
],
|
|
24
|
+
"editionLevel": 0
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"axios": "^1.6.0"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@langchain/core": "^0.3.0",
|
|
31
|
+
"@nocobase/actions": "2.x",
|
|
32
|
+
"@nocobase/client": "2.x",
|
|
33
|
+
"@nocobase/client-v2": "2.x",
|
|
34
|
+
"@nocobase/database": "2.x",
|
|
35
|
+
"@nocobase/flow-engine": "2.x",
|
|
36
|
+
"@nocobase/plugin-ai": "2.x",
|
|
37
|
+
"@nocobase/server": "2.x"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@nocobase/test": "2.x"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [
|
|
43
|
+
"AI",
|
|
44
|
+
"document",
|
|
45
|
+
"OCR",
|
|
46
|
+
"pipeline",
|
|
47
|
+
"orchestrator"
|
|
48
|
+
]
|
|
49
|
+
}
|