plugin-build-visualization-block 0.1.5 → 0.2.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 +21 -0
- package/client.d.ts +2 -2
- package/client.js +1 -1
- package/dist/client/59.6bac6ada1dacea82.js +10 -0
- package/dist/client/index.js +1 -1
- package/dist/client-v2/385.1e235c5081822f07.js +10 -0
- package/dist/client-v2/59.cdb723e410dac32b.js +10 -0
- package/dist/client-v2/index.js +37 -1
- package/dist/externalVersion.js +12 -7
- package/dist/locale/en-US.json +77 -37
- package/dist/locale/vi-VN.json +58 -40
- package/dist/locale/zh-CN.json +79 -40
- package/dist/node_modules/zod/index.cjs +1 -1
- package/dist/node_modules/zod/package.json +1 -1
- package/dist/server/actions/build.js +7 -704
- package/dist/server/collections/workflow-runs.js +51 -0
- package/dist/server/pipeline/analyzer.js +18 -4
- package/dist/server/plugin.js +17 -20
- package/dist/server/tools/build-visualization-block.js +9 -26
- package/dist/server/workflow-domain.js +209 -0
- package/dist/server/workflow.js +466 -0
- package/dist/shared/blockSpec.js +1 -1
- package/dist/shared/workflow.js +58 -0
- package/package.json +14 -3
- package/server.d.ts +2 -2
- package/server.js +1 -1
- package/src/client/locale.ts +18 -18
- package/src/client/plugin.tsx +11 -0
- package/src/client/workflow.ts +28 -0
- package/src/client-v2/__tests__/buildApi.test.ts +68 -0
- package/src/client-v2/__tests__/modelOptions.test.ts +190 -0
- package/src/client-v2/__tests__/nativeModels.test.ts +77 -0
- package/src/client-v2/buildApi.ts +93 -0
- package/src/client-v2/components/AISelectors.tsx +85 -0
- package/src/client-v2/components/BuildForm.tsx +186 -0
- package/src/client-v2/components/CollectionMultiSelect.tsx +63 -0
- package/src/client-v2/components/NativePreview.tsx +53 -0
- package/src/client-v2/locale.ts +5 -0
- package/src/client-v2/modelOptions.ts +201 -0
- package/src/client-v2/models/BuildVisualizationBlockModel.tsx +95 -0
- package/src/client-v2/nativeBlock.ts +28 -0
- package/src/client-v2/pages/SettingsPage.tsx +77 -0
- package/src/client-v2/plugin.tsx +17 -4
- package/src/client-v2/workflow-fields.tsx +23 -0
- package/src/client-v2/workflow.ts +28 -0
- package/src/locale/en-US.json +77 -37
- package/src/locale/namespace.ts +6 -6
- package/src/locale/vi-VN.json +58 -40
- package/src/locale/zh-CN.json +79 -40
- package/src/server/__tests__/build-action.test.ts +121 -117
- package/src/server/__tests__/plugin.test.ts +19 -34
- package/src/server/actions/build.ts +15 -1031
- package/src/server/collections/workflow-runs.ts +30 -0
- package/src/server/pipeline/analyzer.ts +395 -368
- package/src/server/plugin.ts +26 -35
- package/src/server/tools/build-visualization-block.ts +18 -28
- package/src/server/workflow-domain.ts +235 -0
- package/src/server/workflow.ts +519 -0
- package/src/shared/blockSpec.ts +10 -1
- package/src/shared/workflow.ts +26 -0
- package/dist/client-v2/812.6b4f5e757b898c82.js +0 -10
|
@@ -0,0 +1,466 @@
|
|
|
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 __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var workflow_exports = {};
|
|
28
|
+
__export(workflow_exports, {
|
|
29
|
+
ManagedInstruction: () => ManagedInstruction,
|
|
30
|
+
WorkflowService: () => WorkflowService
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(workflow_exports);
|
|
33
|
+
var import_crypto = require("crypto");
|
|
34
|
+
var import_database = require("@nocobase/database");
|
|
35
|
+
var import_plugin_workflow = require("@nocobase/plugin-workflow");
|
|
36
|
+
var import_workflow = require("../shared/workflow");
|
|
37
|
+
var import_workflow_domain = require("./workflow-domain");
|
|
38
|
+
const LEASE_MS = 9e4;
|
|
39
|
+
const TIMEOUT_MS = 30 * 6e4;
|
|
40
|
+
class ManagedInstruction extends import_plugin_workflow.Instruction {
|
|
41
|
+
constructor(workflow, service) {
|
|
42
|
+
super(workflow);
|
|
43
|
+
this.service = service;
|
|
44
|
+
}
|
|
45
|
+
async run(node, prevJob, processor) {
|
|
46
|
+
var _a;
|
|
47
|
+
if (processor.isInstructionSync(node)) {
|
|
48
|
+
return {
|
|
49
|
+
status: import_plugin_workflow.JOB_STATUS.ERROR,
|
|
50
|
+
result: { error: this.service.t("This node requires an asynchronous workflow") }
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
const data = (_a = processor.execution.context) == null ? void 0 : _a.data;
|
|
54
|
+
const runId = typeof data === "object" && data ? data.id : data;
|
|
55
|
+
if (typeof runId !== "string" && typeof runId !== "number") {
|
|
56
|
+
return { status: import_plugin_workflow.JOB_STATUS.ERROR, result: { error: this.service.t("Workflow request not found") } };
|
|
57
|
+
}
|
|
58
|
+
const run = await this.service.app.db.getRepository(import_workflow.RUNS_COLLECTION).findOne({ filterByTk: runId });
|
|
59
|
+
if (!run || String(run.get("workflowId")) !== String(processor.execution.workflowId) || run.get("executionId") && String(run.get("executionId")) !== String(processor.execution.id)) {
|
|
60
|
+
return {
|
|
61
|
+
status: import_plugin_workflow.JOB_STATUS.ERROR,
|
|
62
|
+
result: { error: this.service.t("Workflow request does not belong to this execution") }
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const [bound] = await this.service.app.db.getCollection(import_workflow.RUNS_COLLECTION).model.update(
|
|
66
|
+
{ executionId: processor.execution.id },
|
|
67
|
+
{
|
|
68
|
+
where: {
|
|
69
|
+
id: runId,
|
|
70
|
+
state: { [import_database.Op.in]: ["queued", "running"] },
|
|
71
|
+
[import_database.Op.or]: [{ executionId: null }, { executionId: processor.execution.id }]
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
if (!bound) return { status: import_plugin_workflow.JOB_STATUS.CANCELED };
|
|
76
|
+
const job = processor.saveJob({
|
|
77
|
+
status: import_plugin_workflow.JOB_STATUS.PENDING,
|
|
78
|
+
nodeId: node.id,
|
|
79
|
+
nodeKey: node.key,
|
|
80
|
+
upstreamId: (prevJob == null ? void 0 : prevJob.id) ?? null,
|
|
81
|
+
result: { runId: String(runId) }
|
|
82
|
+
});
|
|
83
|
+
await processor.exit();
|
|
84
|
+
this.service.launch(job.id);
|
|
85
|
+
}
|
|
86
|
+
async resume(_node, job) {
|
|
87
|
+
return job;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
class WorkflowService {
|
|
91
|
+
constructor(app) {
|
|
92
|
+
this.app = app;
|
|
93
|
+
}
|
|
94
|
+
workflow;
|
|
95
|
+
timer;
|
|
96
|
+
tick;
|
|
97
|
+
stopping = false;
|
|
98
|
+
cursor;
|
|
99
|
+
active = /* @__PURE__ */ new Map();
|
|
100
|
+
t(key) {
|
|
101
|
+
return String(this.app.i18n.t(key, { ns: import_workflow.NAMESPACE }));
|
|
102
|
+
}
|
|
103
|
+
load() {
|
|
104
|
+
var _a;
|
|
105
|
+
this.workflow = (_a = this.app.pm) == null ? void 0 : _a.get("workflow");
|
|
106
|
+
if (!this.workflow) return;
|
|
107
|
+
for (const stage of import_workflow.STAGES)
|
|
108
|
+
this.workflow.registerInstruction(stage.type, new ManagedInstruction(this.workflow, this));
|
|
109
|
+
}
|
|
110
|
+
async provision() {
|
|
111
|
+
if (!this.workflow) return;
|
|
112
|
+
const lock = await this.app.lockManager.tryAcquire(import_workflow.WORKFLOW_KEY + ":provision", 3e4);
|
|
113
|
+
return lock.runExclusive(async () => {
|
|
114
|
+
const repo = this.app.db.getRepository("workflows");
|
|
115
|
+
const existing = await repo.findOne({ filter: { key: import_workflow.WORKFLOW_KEY }, sort: ["-current", "-id"] });
|
|
116
|
+
if (existing) return existing;
|
|
117
|
+
return this.app.db.sequelize.transaction(
|
|
118
|
+
{ type: this.app.db.sequelize.getDialect() === "sqlite" ? import_database.Transaction.TYPES.IMMEDIATE : void 0 },
|
|
119
|
+
async (transaction) => {
|
|
120
|
+
const flow = await repo.create({
|
|
121
|
+
values: {
|
|
122
|
+
key: import_workflow.WORKFLOW_KEY,
|
|
123
|
+
title: this.t("Build Visualization Block"),
|
|
124
|
+
type: "collection",
|
|
125
|
+
enabled: false,
|
|
126
|
+
current: true,
|
|
127
|
+
sync: false,
|
|
128
|
+
config: { collection: import_workflow.RUNS_COLLECTION, mode: 1, condition: { workflowKey: { $eq: import_workflow.WORKFLOW_KEY } } },
|
|
129
|
+
options: { timeout: TIMEOUT_MS }
|
|
130
|
+
},
|
|
131
|
+
transaction
|
|
132
|
+
});
|
|
133
|
+
let previous;
|
|
134
|
+
for (const stage of import_workflow.STAGES) {
|
|
135
|
+
const node = await flow.createNode(
|
|
136
|
+
{
|
|
137
|
+
type: stage.type,
|
|
138
|
+
key: stage.type,
|
|
139
|
+
title: this.t(stage.title),
|
|
140
|
+
config: {},
|
|
141
|
+
upstreamId: (previous == null ? void 0 : previous.id) ?? null
|
|
142
|
+
},
|
|
143
|
+
{ transaction }
|
|
144
|
+
);
|
|
145
|
+
if (previous) await previous.update({ downstreamId: node.id }, { transaction });
|
|
146
|
+
previous = node;
|
|
147
|
+
}
|
|
148
|
+
await flow.update({ enabled: true }, { transaction });
|
|
149
|
+
return flow;
|
|
150
|
+
}
|
|
151
|
+
);
|
|
152
|
+
}, 6e4);
|
|
153
|
+
}
|
|
154
|
+
async requireFlow() {
|
|
155
|
+
if (this.stopping || !this.workflow)
|
|
156
|
+
throw Object.assign(new Error(this.t("Managed workflow is unavailable or disabled")), { status: 503 });
|
|
157
|
+
await this.provision();
|
|
158
|
+
const flow = await this.app.db.getRepository("workflows").findOne({
|
|
159
|
+
filter: { key: import_workflow.WORKFLOW_KEY, current: true, enabled: true }
|
|
160
|
+
});
|
|
161
|
+
if (!flow || flow.sync)
|
|
162
|
+
throw Object.assign(new Error(this.t("Managed workflow is unavailable or disabled")), { status: 503 });
|
|
163
|
+
return flow;
|
|
164
|
+
}
|
|
165
|
+
async enqueue(targetId, input, runId, transaction, flow) {
|
|
166
|
+
return this.app.db.getRepository(import_workflow.RUNS_COLLECTION).create({
|
|
167
|
+
values: {
|
|
168
|
+
id: runId,
|
|
169
|
+
targetId: String(targetId),
|
|
170
|
+
input,
|
|
171
|
+
workflowKey: import_workflow.WORKFLOW_KEY,
|
|
172
|
+
workflowId: flow.id,
|
|
173
|
+
state: "queued"
|
|
174
|
+
},
|
|
175
|
+
transaction
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
async start() {
|
|
179
|
+
var _a, _b;
|
|
180
|
+
this.stopping = false;
|
|
181
|
+
if (!this.workflow) return;
|
|
182
|
+
const flow = await this.provision();
|
|
183
|
+
if (flow == null ? void 0 : flow.enabled) await (0, import_workflow_domain.recoverLegacy)(this);
|
|
184
|
+
this.timer ??= setInterval(() => {
|
|
185
|
+
var _a2;
|
|
186
|
+
if (this.tick || this.stopping || !((_a2 = this.workflow) == null ? void 0 : _a2.serving())) return;
|
|
187
|
+
this.tick = this.reconcile().catch((error) => {
|
|
188
|
+
this.app.logger.error(import_workflow.NAMESPACE + ": recovery failed", { error });
|
|
189
|
+
}).finally(() => {
|
|
190
|
+
this.tick = void 0;
|
|
191
|
+
});
|
|
192
|
+
}, 3e4);
|
|
193
|
+
(_b = (_a = this.timer).unref) == null ? void 0 : _b.call(_a);
|
|
194
|
+
}
|
|
195
|
+
async stop() {
|
|
196
|
+
this.stopping = true;
|
|
197
|
+
if (this.timer) clearInterval(this.timer);
|
|
198
|
+
this.timer = void 0;
|
|
199
|
+
for (const entry of this.active.values()) entry.controller.abort();
|
|
200
|
+
await this.tick;
|
|
201
|
+
await Promise.allSettled([...this.active.values()].map((entry) => entry.work));
|
|
202
|
+
}
|
|
203
|
+
status() {
|
|
204
|
+
return {
|
|
205
|
+
running: Boolean(this.workflow) && !this.stopping,
|
|
206
|
+
processing: this.active.size > 0,
|
|
207
|
+
engine: "workflow",
|
|
208
|
+
workerId: this.app.name,
|
|
209
|
+
intervalMs: 3e4,
|
|
210
|
+
workerMode: process.env.WORKER_MODE || null
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
launch(jobId) {
|
|
214
|
+
var _a;
|
|
215
|
+
const key = String(jobId);
|
|
216
|
+
if (this.stopping || this.active.has(key) || this.active.size >= 2 || !((_a = this.workflow) == null ? void 0 : _a.serving())) return;
|
|
217
|
+
const controller = new AbortController();
|
|
218
|
+
const work = this.process(jobId, controller).catch((error) => {
|
|
219
|
+
this.app.logger.error(import_workflow.NAMESPACE + ": workflow job failed", { jobId, error });
|
|
220
|
+
}).finally(() => {
|
|
221
|
+
this.active.delete(key);
|
|
222
|
+
});
|
|
223
|
+
this.active.set(key, { controller, work });
|
|
224
|
+
}
|
|
225
|
+
async process(jobId, controller = new AbortController()) {
|
|
226
|
+
var _a, _b, _c;
|
|
227
|
+
const workflow = this.workflow;
|
|
228
|
+
if (!workflow || this.stopping) return;
|
|
229
|
+
const jobs = this.app.db.getRepository("jobs");
|
|
230
|
+
const job = await jobs.findOne({ filterByTk: jobId });
|
|
231
|
+
if (!job || job.status !== import_plugin_workflow.JOB_STATUS.PENDING) return;
|
|
232
|
+
const execution = await job.getExecution();
|
|
233
|
+
if (!execution || await workflow.abortExecutionIfExpired(execution) || execution.status !== import_plugin_workflow.EXECUTION_STATUS.STARTED)
|
|
234
|
+
return;
|
|
235
|
+
const node = await this.app.db.getRepository("flow_nodes").findOne({ filterByTk: job.nodeId });
|
|
236
|
+
if (!node || !import_workflow.STAGES.some((stage) => stage.type === node.type)) return;
|
|
237
|
+
const runId = String(((_a = job.result) == null ? void 0 : _a.runId) ?? "");
|
|
238
|
+
const runs = this.app.db.getRepository(import_workflow.RUNS_COLLECTION);
|
|
239
|
+
const token = (0, import_crypto.randomUUID)();
|
|
240
|
+
const [claimed] = await this.app.db.getCollection(import_workflow.RUNS_COLLECTION).model.update(
|
|
241
|
+
{
|
|
242
|
+
state: "running",
|
|
243
|
+
executionId: execution.id,
|
|
244
|
+
jobId,
|
|
245
|
+
leaseToken: token,
|
|
246
|
+
leaseUntil: new Date(Date.now() + LEASE_MS)
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
where: {
|
|
250
|
+
id: runId,
|
|
251
|
+
workflowId: execution.workflowId,
|
|
252
|
+
state: { [import_database.Op.in]: ["queued", "running"] },
|
|
253
|
+
[import_database.Op.and]: [
|
|
254
|
+
{ [import_database.Op.or]: [{ executionId: null }, { executionId: execution.id }] },
|
|
255
|
+
{ [import_database.Op.or]: [{ leaseUntil: null }, { leaseUntil: { [import_database.Op.lt]: /* @__PURE__ */ new Date() } }] }
|
|
256
|
+
]
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
);
|
|
260
|
+
if (!claimed) return;
|
|
261
|
+
const run = await runs.findOne({ filterByTk: runId });
|
|
262
|
+
const heartbeat = setInterval(() => {
|
|
263
|
+
this.renew(runId, token, execution.id).then((owned) => {
|
|
264
|
+
if (!owned) controller.abort();
|
|
265
|
+
}).catch(() => controller.abort());
|
|
266
|
+
}, 2e4);
|
|
267
|
+
(_b = heartbeat.unref) == null ? void 0 : _b.call(heartbeat);
|
|
268
|
+
const timeout = setTimeout(() => controller.abort(), TIMEOUT_MS);
|
|
269
|
+
let result;
|
|
270
|
+
let status = import_plugin_workflow.JOB_STATUS.RESOLVED;
|
|
271
|
+
try {
|
|
272
|
+
let upstream = job.upstreamId ? await jobs.findOne({ filterByTk: job.upstreamId }) : null;
|
|
273
|
+
const stageIndex = import_workflow.STAGES.findIndex((stage) => stage.type === node.type);
|
|
274
|
+
const expectedType = (_c = import_workflow.STAGES[stageIndex - 1]) == null ? void 0 : _c.type;
|
|
275
|
+
if (expectedType) {
|
|
276
|
+
let upstreamNodeId = node.upstreamId;
|
|
277
|
+
const visited = /* @__PURE__ */ new Set();
|
|
278
|
+
upstream = null;
|
|
279
|
+
while (upstreamNodeId) {
|
|
280
|
+
if (visited.has(String(upstreamNodeId))) throw new Error(this.t("Workflow stage input is invalid"));
|
|
281
|
+
visited.add(String(upstreamNodeId));
|
|
282
|
+
const upstreamNode = await this.app.db.getRepository("flow_nodes").findOne({ filterByTk: upstreamNodeId });
|
|
283
|
+
if (!upstreamNode) break;
|
|
284
|
+
if (upstreamNode.get("type") === expectedType) {
|
|
285
|
+
upstream = await jobs.findOne({
|
|
286
|
+
filter: { executionId: execution.id, nodeId: upstreamNode.id, status: import_plugin_workflow.JOB_STATUS.RESOLVED },
|
|
287
|
+
sort: ["-id"]
|
|
288
|
+
});
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
upstreamNodeId = upstreamNode.get("upstreamId");
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
result = await (0, import_workflow_domain.executeStage)(this, node.type, run, upstream == null ? void 0 : upstream.get("result"), controller.signal);
|
|
295
|
+
controller.signal.throwIfAborted();
|
|
296
|
+
} catch (error) {
|
|
297
|
+
if (this.stopping) return;
|
|
298
|
+
status = import_plugin_workflow.JOB_STATUS.ERROR;
|
|
299
|
+
result = { error: error instanceof Error ? error.message : String(error) };
|
|
300
|
+
} finally {
|
|
301
|
+
clearInterval(heartbeat);
|
|
302
|
+
clearTimeout(timeout);
|
|
303
|
+
if (this.stopping)
|
|
304
|
+
await this.app.db.getCollection(import_workflow.RUNS_COLLECTION).model.update({ leaseToken: null, leaseUntil: null }, { where: { id: runId, leaseToken: token } });
|
|
305
|
+
}
|
|
306
|
+
try {
|
|
307
|
+
const lock = await this.app.lockManager.tryAcquire((0, import_plugin_workflow.getExecutionLockKey)(execution.id), 1e4);
|
|
308
|
+
await lock.runExclusive(async () => {
|
|
309
|
+
await execution.reload();
|
|
310
|
+
if (await workflow.abortExecutionIfExpired(execution) || execution.status !== import_plugin_workflow.EXECUTION_STATUS.STARTED)
|
|
311
|
+
return;
|
|
312
|
+
await this.app.db.sequelize.transaction(
|
|
313
|
+
{ type: this.app.db.sequelize.getDialect() === "sqlite" ? import_database.Transaction.TYPES.IMMEDIATE : void 0 },
|
|
314
|
+
async (transaction) => {
|
|
315
|
+
const owned = await runs.findOne({
|
|
316
|
+
filter: { id: runId, leaseToken: token, state: "running" },
|
|
317
|
+
transaction,
|
|
318
|
+
lock: true
|
|
319
|
+
});
|
|
320
|
+
const freshJob = await jobs.findOne({ filterByTk: jobId, transaction, lock: true });
|
|
321
|
+
if (!owned || freshJob.status !== import_plugin_workflow.JOB_STATUS.PENDING) return;
|
|
322
|
+
if (status === import_plugin_workflow.JOB_STATUS.RESOLVED) {
|
|
323
|
+
if (!await (0, import_workflow_domain.commitStage)(this, node.type, owned, result, transaction)) status = import_plugin_workflow.JOB_STATUS.CANCELED;
|
|
324
|
+
} else await (0, import_workflow_domain.syncTerminal)(this, owned, import_plugin_workflow.EXECUTION_STATUS.ERROR, result, transaction);
|
|
325
|
+
await freshJob.update({ status, result }, { transaction });
|
|
326
|
+
await owned.update({ leaseToken: null, leaseUntil: null }, { transaction });
|
|
327
|
+
}
|
|
328
|
+
);
|
|
329
|
+
}, 6e4);
|
|
330
|
+
await job.reload();
|
|
331
|
+
if (job.status !== import_plugin_workflow.JOB_STATUS.PENDING) await workflow.resume(job);
|
|
332
|
+
} finally {
|
|
333
|
+
await this.app.db.getCollection(import_workflow.RUNS_COLLECTION).model.update({ leaseToken: null, leaseUntil: null }, { where: { id: runId, leaseToken: token } });
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
async renew(runId, token, executionId) {
|
|
337
|
+
const execution = await this.app.db.getRepository("executions").findOne({ filterByTk: executionId });
|
|
338
|
+
if (!execution || await this.workflow.abortExecutionIfExpired(execution) || execution.status !== import_plugin_workflow.EXECUTION_STATUS.STARTED)
|
|
339
|
+
return false;
|
|
340
|
+
const [count] = await this.app.db.getCollection(import_workflow.RUNS_COLLECTION).model.update(
|
|
341
|
+
{ leaseUntil: new Date(Date.now() + LEASE_MS) },
|
|
342
|
+
{ where: { id: runId, leaseToken: token, state: "running" } }
|
|
343
|
+
);
|
|
344
|
+
return count > 0;
|
|
345
|
+
}
|
|
346
|
+
async cancel(runId) {
|
|
347
|
+
var _a;
|
|
348
|
+
const runs = this.app.db.getRepository(import_workflow.RUNS_COLLECTION);
|
|
349
|
+
const run = await this.app.db.sequelize.transaction(
|
|
350
|
+
{ type: this.app.db.sequelize.getDialect() === "sqlite" ? import_database.Transaction.TYPES.IMMEDIATE : void 0 },
|
|
351
|
+
async (transaction) => {
|
|
352
|
+
const current = await runs.findOne({ filterByTk: runId, transaction, lock: true });
|
|
353
|
+
if (!current || !["queued", "running"].includes(current.get("state"))) return null;
|
|
354
|
+
await current.update({ state: "canceled", leaseToken: null, leaseUntil: null }, { transaction });
|
|
355
|
+
await (0, import_workflow_domain.syncTerminal)(this, current, import_plugin_workflow.EXECUTION_STATUS.CANCELED, null, transaction);
|
|
356
|
+
return current;
|
|
357
|
+
}
|
|
358
|
+
);
|
|
359
|
+
if (!run) return;
|
|
360
|
+
(_a = this.active.get(String(run.get("jobId")))) == null ? void 0 : _a.controller.abort();
|
|
361
|
+
const executionId = run.get("executionId");
|
|
362
|
+
const execution = executionId ? await this.app.db.getRepository("executions").findOne({ filterByTk: executionId }) : await this.app.db.getCollection("executions").model.findOne({
|
|
363
|
+
where: { workflowId: run.get("workflowId"), "context.data.id": String(run.id) },
|
|
364
|
+
order: [["id", "ASC"]]
|
|
365
|
+
});
|
|
366
|
+
if (execution) {
|
|
367
|
+
const lock = await this.app.lockManager.tryAcquire((0, import_plugin_workflow.getExecutionLockKey)(execution.id), 1e4);
|
|
368
|
+
await lock.runExclusive(async () => {
|
|
369
|
+
await execution.reload();
|
|
370
|
+
if (execution.status === import_plugin_workflow.EXECUTION_STATUS.QUEUEING || execution.status === import_plugin_workflow.EXECUTION_STATUS.STARTED)
|
|
371
|
+
await (0, import_plugin_workflow.abortExecution)(this.workflow, execution, { reason: import_plugin_workflow.EXECUTION_REASON.MANUAL_CANCEL });
|
|
372
|
+
}, 6e4);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
async reconcile() {
|
|
376
|
+
var _a;
|
|
377
|
+
if (this.stopping || !((_a = this.workflow) == null ? void 0 : _a.serving())) return;
|
|
378
|
+
const pending = [];
|
|
379
|
+
const dispatches = [];
|
|
380
|
+
let lock;
|
|
381
|
+
try {
|
|
382
|
+
lock = await this.app.lockManager.tryAcquire(import_workflow.WORKFLOW_KEY + ":reconcile");
|
|
383
|
+
} catch (error) {
|
|
384
|
+
if ((0, import_plugin_workflow.isLockAcquireError)(error)) return;
|
|
385
|
+
throw error;
|
|
386
|
+
}
|
|
387
|
+
await lock.runExclusive(async () => {
|
|
388
|
+
var _a2;
|
|
389
|
+
const runs = await this.app.db.getRepository(import_workflow.RUNS_COLLECTION).find({
|
|
390
|
+
filter: { state: { $in: ["queued", "running"] }, ...this.cursor ? { id: { $gt: this.cursor } } : {} },
|
|
391
|
+
sort: ["id"],
|
|
392
|
+
limit: 100
|
|
393
|
+
});
|
|
394
|
+
this.cursor = runs.length === 100 ? String(runs[runs.length - 1].id) : void 0;
|
|
395
|
+
for (const run of runs) {
|
|
396
|
+
if (this.stopping) break;
|
|
397
|
+
const executionId = run.get("executionId");
|
|
398
|
+
let execution = executionId ? await this.app.db.getRepository("executions").findOne({ filterByTk: executionId }) : null;
|
|
399
|
+
if (!execution) {
|
|
400
|
+
execution = await this.app.db.getCollection("executions").model.findOne({
|
|
401
|
+
where: { workflowId: run.get("workflowId"), "context.data.id": String(run.id) },
|
|
402
|
+
order: [["id", "ASC"]]
|
|
403
|
+
});
|
|
404
|
+
if (!execution) {
|
|
405
|
+
const age = Date.now() - new Date(run.get("createdAt")).getTime();
|
|
406
|
+
if (age < 6e4) continue;
|
|
407
|
+
const flow = await this.app.db.getRepository("workflows").findOne({ filterByTk: run.get("workflowId") });
|
|
408
|
+
if (flow == null ? void 0 : flow.enabled)
|
|
409
|
+
dispatches.push(
|
|
410
|
+
() => this.workflow.execute(flow, { data: run.id }, { force: true, eventKey: import_workflow.WORKFLOW_KEY + ":" + run.id })
|
|
411
|
+
);
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
await this.app.db.getCollection(import_workflow.RUNS_COLLECTION).model.update(
|
|
415
|
+
{ executionId: execution.id },
|
|
416
|
+
{ where: { id: run.id, executionId: null, state: { [import_database.Op.in]: ["queued", "running"] } } }
|
|
417
|
+
);
|
|
418
|
+
await run.reload();
|
|
419
|
+
if (!["queued", "running"].includes(run.get("state"))) continue;
|
|
420
|
+
if (String(run.get("executionId")) !== String(execution.id)) continue;
|
|
421
|
+
}
|
|
422
|
+
await this.workflow.abortExecutionIfExpired(execution);
|
|
423
|
+
await execution.reload();
|
|
424
|
+
if (execution.status === import_plugin_workflow.EXECUTION_STATUS.QUEUEING) {
|
|
425
|
+
dispatches.push(() => this.workflow.start(execution));
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
428
|
+
if (execution.status !== import_plugin_workflow.EXECUTION_STATUS.STARTED) {
|
|
429
|
+
await this.app.db.sequelize.transaction(
|
|
430
|
+
{ type: this.app.db.sequelize.getDialect() === "sqlite" ? import_database.Transaction.TYPES.IMMEDIATE : void 0 },
|
|
431
|
+
async (transaction) => {
|
|
432
|
+
const current = await this.app.db.getRepository(import_workflow.RUNS_COLLECTION).findOne({ filterByTk: run.id, transaction, lock: true });
|
|
433
|
+
if (!current || !["queued", "running"].includes(current.get("state"))) return;
|
|
434
|
+
await (0, import_workflow_domain.syncTerminal)(this, current, execution.status, execution.output, transaction);
|
|
435
|
+
await current.update(
|
|
436
|
+
{
|
|
437
|
+
state: execution.status === import_plugin_workflow.EXECUTION_STATUS.RESOLVED ? "completed" : execution.status === import_plugin_workflow.EXECUTION_STATUS.CANCELED || execution.status === import_plugin_workflow.EXECUTION_STATUS.ABORTED ? "canceled" : "failed",
|
|
438
|
+
leaseToken: null,
|
|
439
|
+
leaseUntil: null
|
|
440
|
+
},
|
|
441
|
+
{ transaction }
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
);
|
|
445
|
+
(_a2 = this.active.get(String(run.get("jobId")))) == null ? void 0 : _a2.controller.abort();
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
const jobs = await this.app.db.getRepository("jobs").find({ filter: { executionId: execution.id }, sort: ["id"] });
|
|
449
|
+
for (const job of jobs) {
|
|
450
|
+
const node = await this.app.db.getRepository("flow_nodes").findOne({ filterByTk: job.nodeId });
|
|
451
|
+
if (!import_workflow.STAGES.some((stage) => stage.type === (node == null ? void 0 : node.get("type")))) continue;
|
|
452
|
+
if (job.status === import_plugin_workflow.JOB_STATUS.PENDING) pending.push(job.id);
|
|
453
|
+
else if (!jobs.some((downstream) => String(downstream.upstreamId) === String(job.id)))
|
|
454
|
+
dispatches.push(() => this.workflow.resume(job));
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}, 6e4);
|
|
458
|
+
for (const dispatch of dispatches) await dispatch();
|
|
459
|
+
for (const jobId of pending) this.launch(jobId);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
463
|
+
0 && (module.exports = {
|
|
464
|
+
ManagedInstruction,
|
|
465
|
+
WorkflowService
|
|
466
|
+
});
|
package/dist/shared/blockSpec.js
CHANGED
|
@@ -48,7 +48,7 @@ const chartSpecItemSchema = import_zod.z.object({
|
|
|
48
48
|
chartType: import_zod.z.string(),
|
|
49
49
|
measures: import_zod.z.array(chartMeasureSchema),
|
|
50
50
|
dimensions: import_zod.z.array(chartDimensionSchema).optional(),
|
|
51
|
-
config: import_zod.z.record(import_zod.z.unknown()).optional()
|
|
51
|
+
config: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional()
|
|
52
52
|
});
|
|
53
53
|
const tableSpecSchema = import_zod.z.object({
|
|
54
54
|
fields: import_zod.z.array(import_zod.z.string()),
|
|
@@ -0,0 +1,58 @@
|
|
|
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 __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var workflow_exports = {};
|
|
28
|
+
__export(workflow_exports, {
|
|
29
|
+
NAMESPACE: () => NAMESPACE,
|
|
30
|
+
RUNS_COLLECTION: () => RUNS_COLLECTION,
|
|
31
|
+
STAGES: () => STAGES,
|
|
32
|
+
WORKFLOW_KEY: () => WORKFLOW_KEY
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(workflow_exports);
|
|
35
|
+
const NAMESPACE = "plugin-build-visualization-block";
|
|
36
|
+
const WORKFLOW_KEY = "build-visualization";
|
|
37
|
+
const RUNS_COLLECTION = "aiVisualizationBuildRuns";
|
|
38
|
+
const STAGES = [
|
|
39
|
+
{
|
|
40
|
+
type: "visualization-analyze",
|
|
41
|
+
title: "Analyze visualization"
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
type: "visualization-validate",
|
|
45
|
+
title: "Validate visualization"
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
type: "visualization-generate",
|
|
49
|
+
title: "Generate visualization"
|
|
50
|
+
}
|
|
51
|
+
];
|
|
52
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
53
|
+
0 && (module.exports = {
|
|
54
|
+
NAMESPACE,
|
|
55
|
+
RUNS_COLLECTION,
|
|
56
|
+
STAGES,
|
|
57
|
+
WORKFLOW_KEY
|
|
58
|
+
});
|
package/package.json
CHANGED
|
@@ -6,13 +6,13 @@
|
|
|
6
6
|
"description": "Generate a NocoBase chart, table or form block from a natural-language requirement and selected collections using AI.",
|
|
7
7
|
"description.zh-CN": "使用 AI 根据自然语言需求和所选数据表生成 NocoBase 图表、表格或表单区块。",
|
|
8
8
|
"description.vi-VN": "Sử dụng AI để tạo khối biểu đồ, bảng hoặc biểu mẫu NocoBase từ yêu cầu bằng ngôn ngữ tự nhiên và các bộ sưu tập đã chọn.",
|
|
9
|
-
"version": "0.
|
|
9
|
+
"version": "0.2.0",
|
|
10
10
|
"license": "Apache-2.0",
|
|
11
11
|
"main": "dist/server/index.js",
|
|
12
12
|
"keywords": [
|
|
13
13
|
"AI",
|
|
14
14
|
"Visualization",
|
|
15
|
-
"
|
|
15
|
+
"Blocks",
|
|
16
16
|
"NocoBase"
|
|
17
17
|
],
|
|
18
18
|
"files": [
|
|
@@ -39,6 +39,17 @@
|
|
|
39
39
|
"@langchain/core": "*",
|
|
40
40
|
"zod": "^3.x",
|
|
41
41
|
"@nocobase/client-v2": "2.x",
|
|
42
|
-
"@nocobase/flow-engine": "2.x"
|
|
42
|
+
"@nocobase/flow-engine": "2.x",
|
|
43
|
+
"@nocobase/plugin-workflow": "^2.2.52"
|
|
44
|
+
},
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "git+https://github.com/OrgGem/nocobase-beta.git",
|
|
48
|
+
"directory": "packages/plugins/plugin-build-visualization-block"
|
|
49
|
+
},
|
|
50
|
+
"gitHead": "0e8271caaa490bd6b03a4d474920d74267c80482",
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public",
|
|
53
|
+
"registry": "https://registry.npmjs.org/"
|
|
43
54
|
}
|
|
44
55
|
}
|
package/server.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './dist/server';
|
|
2
|
-
export { default } from './dist/server';
|
|
1
|
+
export * from './dist/server';
|
|
2
|
+
export { default } from './dist/server';
|
package/server.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
module.exports = require('./dist/server/index.js');
|
|
1
|
+
module.exports = require('./dist/server/index.js');
|
package/src/client/locale.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { useApp } from '@nocobase/client-v2';
|
|
2
|
-
import { useCallback } from 'react';
|
|
3
|
-
import { name } from '../locale/namespace';
|
|
4
|
-
|
|
5
|
-
export const namespace = name;
|
|
6
|
-
|
|
7
|
-
export function useT() {
|
|
8
|
-
const app = useApp();
|
|
9
|
-
return useCallback(
|
|
10
|
-
(str: string, options?: Record<string, unknown>): string =>
|
|
11
|
-
app.i18n.t(str, { ns: [namespace, 'client'], ...options }) as unknown as string,
|
|
12
|
-
[app.i18n],
|
|
13
|
-
);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function tStr(key: string) {
|
|
17
|
-
return `{{t(${JSON.stringify(key)}, { ns: ['${namespace}', 'client'], nsMode: 'fallback' })}}`;
|
|
18
|
-
}
|
|
1
|
+
import { useApp } from '@nocobase/client-v2';
|
|
2
|
+
import { useCallback } from 'react';
|
|
3
|
+
import { name } from '../locale/namespace';
|
|
4
|
+
|
|
5
|
+
export const namespace = name;
|
|
6
|
+
|
|
7
|
+
export function useT() {
|
|
8
|
+
const app = useApp();
|
|
9
|
+
return useCallback(
|
|
10
|
+
(str: string, options?: Record<string, unknown>): string =>
|
|
11
|
+
app.i18n.t(str, { ns: [namespace, 'client'], ...options }) as unknown as string,
|
|
12
|
+
[app.i18n],
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function tStr(key: string) {
|
|
17
|
+
return `{{t(${JSON.stringify(key)}, { ns: ['${namespace}', 'client'], nsMode: 'fallback' })}}`;
|
|
18
|
+
}
|
package/src/client/plugin.tsx
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
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
|
+
import { registerWorkflow } from './workflow';
|
|
1
11
|
import { Plugin } from '@nocobase/client';
|
|
2
12
|
|
|
3
13
|
import { BuildVisualizationBlockInitializer } from './BuildVisualizationBlockInitializer';
|
|
@@ -57,6 +67,7 @@ export class PluginBuildVisualizationBlockClient extends Plugin {
|
|
|
57
67
|
}
|
|
58
68
|
|
|
59
69
|
async load() {
|
|
70
|
+
registerWorkflow(this.app);
|
|
60
71
|
this.app.addComponents({ BuildVisualizationBlockInitializer });
|
|
61
72
|
|
|
62
73
|
this.app.pluginSettingsManager.add('plugin-build-visualization-block', {
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
import { Instruction } from '@nocobase/plugin-workflow/client';
|
|
11
|
+
import type WorkflowPlugin from '@nocobase/plugin-workflow/client';
|
|
12
|
+
import type { Application } from '@nocobase/client';
|
|
13
|
+
import { NAMESPACE, STAGES } from '../shared/workflow';
|
|
14
|
+
|
|
15
|
+
export function registerWorkflow(app: Pick<Application, 'pm'>) {
|
|
16
|
+
const workflow = app.pm?.get<WorkflowPlugin>('workflow');
|
|
17
|
+
if (!workflow) return;
|
|
18
|
+
for (const stage of STAGES) {
|
|
19
|
+
class ManagedNode extends Instruction {
|
|
20
|
+
type = stage.type;
|
|
21
|
+
title = '{{t(' + JSON.stringify(stage.title) + ', { ns: ' + JSON.stringify(NAMESPACE) + ' })}}';
|
|
22
|
+
group = 'extended';
|
|
23
|
+
async = true;
|
|
24
|
+
FieldsetLoader = () => import('../client-v2/workflow-fields');
|
|
25
|
+
}
|
|
26
|
+
workflow.registerInstruction(stage.type, ManagedNode);
|
|
27
|
+
}
|
|
28
|
+
}
|