plugin-build-visualization-block 0.1.4 → 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 +8 -720
- 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 +268 -1318
- 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,51 @@
|
|
|
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_runs_exports = {};
|
|
28
|
+
__export(workflow_runs_exports, {
|
|
29
|
+
default: () => workflow_runs_default
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(workflow_runs_exports);
|
|
32
|
+
var import_database = require("@nocobase/database");
|
|
33
|
+
var import_workflow = require("../../shared/workflow");
|
|
34
|
+
var workflow_runs_default = (0, import_database.defineCollection)({
|
|
35
|
+
name: import_workflow.RUNS_COLLECTION,
|
|
36
|
+
shared: true,
|
|
37
|
+
autoGenId: false,
|
|
38
|
+
timestamps: true,
|
|
39
|
+
fields: [
|
|
40
|
+
{ name: "id", type: "uuid", primaryKey: true },
|
|
41
|
+
{ name: "targetId", type: "string", index: true },
|
|
42
|
+
{ name: "input", type: "json" },
|
|
43
|
+
{ name: "workflowKey", type: "string", index: true },
|
|
44
|
+
{ name: "workflowId", type: "bigInt" },
|
|
45
|
+
{ name: "executionId", type: "bigInt", index: true },
|
|
46
|
+
{ name: "jobId", type: "bigInt" },
|
|
47
|
+
{ name: "state", type: "string", defaultValue: "queued", index: true },
|
|
48
|
+
{ name: "leaseToken", type: "string" },
|
|
49
|
+
{ name: "leaseUntil", type: "date" }
|
|
50
|
+
]
|
|
51
|
+
});
|
|
@@ -160,15 +160,27 @@ ${schemaJson}
|
|
|
160
160
|
Return ONLY the BlockSpec JSON object.`)
|
|
161
161
|
];
|
|
162
162
|
}
|
|
163
|
-
async function invokeLLM(provider, messages) {
|
|
163
|
+
async function invokeLLM(provider, messages, signal) {
|
|
164
|
+
signal == null ? void 0 : signal.throwIfAborted();
|
|
165
|
+
let rejectAbort;
|
|
166
|
+
const canceled = new Promise((_resolve, reject) => {
|
|
167
|
+
rejectAbort = reject;
|
|
168
|
+
});
|
|
169
|
+
const onAbort = () => rejectAbort(signal.reason);
|
|
170
|
+
signal == null ? void 0 : signal.addEventListener("abort", onAbort, { once: true });
|
|
164
171
|
let timer;
|
|
165
172
|
const timeout = new Promise((_resolve, reject) => {
|
|
166
173
|
timer = setTimeout(() => reject(new Error(`LLM invocation timed out after ${import_constants.LLM_TIMEOUT_MS}ms`)), import_constants.LLM_TIMEOUT_MS);
|
|
167
174
|
});
|
|
168
175
|
try {
|
|
169
|
-
const response = await Promise.race([
|
|
176
|
+
const response = await Promise.race([
|
|
177
|
+
signal ? provider.chatModel.invoke(messages, { signal }) : provider.chatModel.invoke(messages),
|
|
178
|
+
timeout,
|
|
179
|
+
canceled
|
|
180
|
+
]);
|
|
170
181
|
return extractJsonObject(toPlainText(response.content));
|
|
171
182
|
} finally {
|
|
183
|
+
signal == null ? void 0 : signal.removeEventListener("abort", onAbort);
|
|
172
184
|
if (timer) {
|
|
173
185
|
clearTimeout(timer);
|
|
174
186
|
}
|
|
@@ -190,18 +202,20 @@ function parseBlockSpec(raw) {
|
|
|
190
202
|
return { ok: true, spec: result.data };
|
|
191
203
|
}
|
|
192
204
|
async function analyze(app, params) {
|
|
205
|
+
var _a;
|
|
193
206
|
const { requirement, summary, llmService, model } = params;
|
|
194
207
|
let raw;
|
|
195
208
|
try {
|
|
196
209
|
const provider = await getLLMProvider(app, llmService, model);
|
|
197
210
|
const messages = buildAnalyzerMessages({ requirement, summary });
|
|
198
|
-
raw = await invokeLLM(provider, messages);
|
|
211
|
+
raw = await invokeLLM(provider, messages, params.signal);
|
|
199
212
|
} catch (err) {
|
|
213
|
+
(_a = params.signal) == null ? void 0 : _a.throwIfAborted();
|
|
200
214
|
const error = err instanceof Error ? err.message : String(err);
|
|
201
215
|
return { spec: (0, import_fallback.buildFallbackSpec)(summary), usedFallback: true, error };
|
|
202
216
|
}
|
|
203
217
|
const parsed = parseBlockSpec(raw);
|
|
204
|
-
if (parsed.ok) {
|
|
218
|
+
if (parsed.ok === true) {
|
|
205
219
|
return { spec: parsed.spec, usedFallback: false };
|
|
206
220
|
}
|
|
207
221
|
return { spec: (0, import_fallback.buildFallbackSpec)(summary), usedFallback: true, error: parsed.error };
|
package/dist/server/plugin.js
CHANGED
|
@@ -44,10 +44,13 @@ var import_server = require("@nocobase/server");
|
|
|
44
44
|
var import_path = require("path");
|
|
45
45
|
var import_constants = require("../shared/constants");
|
|
46
46
|
var import_build = require("./actions/build");
|
|
47
|
+
var import_workflow = require("./workflow");
|
|
48
|
+
var import_workflow2 = require("../shared/workflow");
|
|
47
49
|
var import_settings = __toESM(require("./resource/settings"));
|
|
48
50
|
var import_tools = require("./tools");
|
|
49
51
|
class PluginBuildVisualizationBlockServer extends import_server.Plugin {
|
|
50
|
-
|
|
52
|
+
workflowService;
|
|
53
|
+
schemaCollections = [import_constants.COLLECTION_NAME, import_constants.SETTINGS_COLLECTION_NAME, import_workflow2.RUNS_COLLECTION];
|
|
51
54
|
afterAdd() {
|
|
52
55
|
}
|
|
53
56
|
async beforeLoad() {
|
|
@@ -81,21 +84,11 @@ class PluginBuildVisualizationBlockServer extends import_server.Plugin {
|
|
|
81
84
|
name: "pm.plugin-build-visualization-block.settings",
|
|
82
85
|
actions: [`${import_constants.SETTINGS_COLLECTION_NAME}:get`, `${import_constants.SETTINGS_COLLECTION_NAME}:update`]
|
|
83
86
|
});
|
|
84
|
-
|
|
85
|
-
this.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
} catch (err) {
|
|
90
|
-
this.app.logger.warn("[plugin-build-visualization-block] Failed to recover interrupted builds", err);
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
this.app.on("beforeStop", () => {
|
|
94
|
-
(0, import_build.unregisterBuildQueue)(this.app);
|
|
95
|
-
});
|
|
96
|
-
this.app.on("beforeDestroy", () => {
|
|
97
|
-
(0, import_build.unregisterBuildQueue)(this.app);
|
|
98
|
-
});
|
|
87
|
+
this.workflowService = new import_workflow.WorkflowService(this.app);
|
|
88
|
+
this.workflowService.load();
|
|
89
|
+
this.app.on("afterStart", () => this.workflowService.start());
|
|
90
|
+
this.app.on("beforeStop", () => this.workflowService.stop());
|
|
91
|
+
this.app.on("beforeDestroy", () => this.workflowService.stop());
|
|
99
92
|
this.registerAITools();
|
|
100
93
|
}
|
|
101
94
|
/**
|
|
@@ -191,16 +184,20 @@ class PluginBuildVisualizationBlockServer extends import_server.Plugin {
|
|
|
191
184
|
async afterEnable() {
|
|
192
185
|
}
|
|
193
186
|
async beforeDisable() {
|
|
194
|
-
|
|
187
|
+
var _a;
|
|
188
|
+
await ((_a = this.workflowService) == null ? void 0 : _a.stop());
|
|
195
189
|
}
|
|
196
190
|
async afterDisable() {
|
|
197
|
-
|
|
191
|
+
var _a;
|
|
192
|
+
await ((_a = this.workflowService) == null ? void 0 : _a.stop());
|
|
198
193
|
}
|
|
199
194
|
async beforeUnload() {
|
|
200
|
-
|
|
195
|
+
var _a;
|
|
196
|
+
await ((_a = this.workflowService) == null ? void 0 : _a.stop());
|
|
201
197
|
}
|
|
202
198
|
async remove() {
|
|
203
|
-
|
|
199
|
+
var _a;
|
|
200
|
+
await ((_a = this.workflowService) == null ? void 0 : _a.stop());
|
|
204
201
|
}
|
|
205
202
|
}
|
|
206
203
|
var plugin_default = PluginBuildVisualizationBlockServer;
|
|
@@ -29,10 +29,9 @@ __export(build_visualization_block_exports, {
|
|
|
29
29
|
default: () => build_visualization_block_default
|
|
30
30
|
});
|
|
31
31
|
module.exports = __toCommonJS(build_visualization_block_exports);
|
|
32
|
-
var import_crypto = require("crypto");
|
|
33
32
|
var import_zod = require("zod");
|
|
34
33
|
var import_constants = require("../../shared/constants");
|
|
35
|
-
var
|
|
34
|
+
var import_workflow_domain = require("../workflow-domain");
|
|
36
35
|
const buildVisualizationBlockSchema = import_zod.z.object({
|
|
37
36
|
requirement: import_zod.z.string().describe("Natural-language description of the visualization block to build."),
|
|
38
37
|
collections: import_zod.z.array(import_zod.z.string()).min(import_constants.MIN_COLLECTIONS).max(import_constants.MAX_COLLECTIONS).optional().describe("The selected target collection names the block should be built from."),
|
|
@@ -85,32 +84,16 @@ var build_visualization_block_default = {
|
|
|
85
84
|
if (!llmService || !model) {
|
|
86
85
|
return { status: "error", content: "AI service and model are required." };
|
|
87
86
|
}
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
primaryCollection: collections[0],
|
|
97
|
-
llmService,
|
|
98
|
-
model,
|
|
99
|
-
status: "building",
|
|
100
|
-
buildPhase: "queued",
|
|
101
|
-
buildRunId: runId,
|
|
102
|
-
buildQueuedAt: /* @__PURE__ */ new Date(),
|
|
103
|
-
...createdById !== void 0 ? { createdById } : {}
|
|
104
|
-
}
|
|
87
|
+
const record = await (0, import_workflow_domain.requestBuild)((0, import_workflow_domain.getWorkflowService)(ctx.app), {
|
|
88
|
+
requirement,
|
|
89
|
+
collections,
|
|
90
|
+
dataSource,
|
|
91
|
+
primaryCollection: collections[0],
|
|
92
|
+
llmService,
|
|
93
|
+
model,
|
|
94
|
+
createdById: ((_c = (_b = ctx.auth) == null ? void 0 : _b.user) == null ? void 0 : _c.id) ?? null
|
|
105
95
|
});
|
|
106
96
|
const id = record.get("id");
|
|
107
|
-
const buildId = String(id);
|
|
108
|
-
await (0, import_build.enqueueBuild)(ctx.app, {
|
|
109
|
-
buildId,
|
|
110
|
-
runId,
|
|
111
|
-
userId: createdById ?? null,
|
|
112
|
-
queuedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
113
|
-
});
|
|
114
97
|
return {
|
|
115
98
|
status: "success",
|
|
116
99
|
content: {
|
|
@@ -0,0 +1,209 @@
|
|
|
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_domain_exports = {};
|
|
28
|
+
__export(workflow_domain_exports, {
|
|
29
|
+
commitStage: () => commitStage,
|
|
30
|
+
executeStage: () => executeStage,
|
|
31
|
+
getWorkflowService: () => getWorkflowService,
|
|
32
|
+
recoverLegacy: () => recoverLegacy,
|
|
33
|
+
requestBuild: () => requestBuild,
|
|
34
|
+
syncTerminal: () => syncTerminal
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(workflow_domain_exports);
|
|
37
|
+
var import_crypto = require("crypto");
|
|
38
|
+
var import_database = require("@nocobase/database");
|
|
39
|
+
var import_plugin_workflow = require("@nocobase/plugin-workflow");
|
|
40
|
+
var import_constants = require("../shared/constants");
|
|
41
|
+
var import_workflow = require("../shared/workflow");
|
|
42
|
+
var import_blockSpec = require("../shared/blockSpec");
|
|
43
|
+
var import_introspector = require("./pipeline/introspector");
|
|
44
|
+
var import_analyzer = require("./pipeline/analyzer");
|
|
45
|
+
var import_validator = require("./pipeline/validator");
|
|
46
|
+
var import_generator = require("./pipeline/generator");
|
|
47
|
+
var import_fallback = require("./pipeline/fallback");
|
|
48
|
+
function getWorkflowService(app) {
|
|
49
|
+
const plugin = app.pm.get("plugin-build-visualization-block");
|
|
50
|
+
if (!(plugin == null ? void 0 : plugin.workflowService))
|
|
51
|
+
throw new Error(
|
|
52
|
+
String(app.i18n.t("Managed workflow is unavailable or disabled", { ns: "plugin-build-visualization-block" }))
|
|
53
|
+
);
|
|
54
|
+
return plugin.workflowService;
|
|
55
|
+
}
|
|
56
|
+
async function requestBuild(service, values, targetId) {
|
|
57
|
+
const flow = await service.requireFlow();
|
|
58
|
+
const db = service.app.db;
|
|
59
|
+
const lock = await service.app.lockManager.tryAcquire(
|
|
60
|
+
"build-visualization:request:" + (targetId ?? (0, import_crypto.randomUUID)()),
|
|
61
|
+
1e4
|
|
62
|
+
);
|
|
63
|
+
return lock.runExclusive(async () => {
|
|
64
|
+
const repo = db.getRepository(import_constants.COLLECTION_NAME);
|
|
65
|
+
const previous = targetId ? await repo.findOne({ filterByTk: targetId }) : null;
|
|
66
|
+
if (previous == null ? void 0 : previous.get("buildRunId")) await service.cancel(previous.get("buildRunId"));
|
|
67
|
+
return db.sequelize.transaction(
|
|
68
|
+
{ type: db.sequelize.getDialect() === "sqlite" ? import_database.Transaction.TYPES.IMMEDIATE : void 0 },
|
|
69
|
+
async (transaction) => {
|
|
70
|
+
const runId = (0, import_crypto.randomUUID)();
|
|
71
|
+
const patch = {
|
|
72
|
+
...values,
|
|
73
|
+
status: "building",
|
|
74
|
+
buildPhase: "queued",
|
|
75
|
+
buildRunId: runId,
|
|
76
|
+
buildQueuedAt: /* @__PURE__ */ new Date(),
|
|
77
|
+
buildStartedAt: null,
|
|
78
|
+
buildHeartbeatAt: null,
|
|
79
|
+
buildWorkerId: null,
|
|
80
|
+
blockSpec: null,
|
|
81
|
+
blockSchema: null,
|
|
82
|
+
adjustments: null,
|
|
83
|
+
usedFallback: false,
|
|
84
|
+
errorMessage: null,
|
|
85
|
+
buildLog: service.t("Build queued in workflow")
|
|
86
|
+
};
|
|
87
|
+
let build = targetId ? await repo.findOne({ filterByTk: targetId, transaction, lock: true }) : null;
|
|
88
|
+
if (targetId && !build) throw new Error(service.t("Build record not found"));
|
|
89
|
+
if (build) await build.update(patch, { transaction });
|
|
90
|
+
else build = await repo.create({ values: patch, transaction });
|
|
91
|
+
const input = Object.fromEntries(
|
|
92
|
+
["requirement", "dataSource", "collections", "primaryCollection", "llmService", "model"].map((key) => [
|
|
93
|
+
key,
|
|
94
|
+
build.get(key)
|
|
95
|
+
])
|
|
96
|
+
);
|
|
97
|
+
await service.enqueue(build.id, input, runId, transaction, flow);
|
|
98
|
+
return build;
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
}, 6e4);
|
|
102
|
+
}
|
|
103
|
+
async function executeStage(service, type, run, previous, signal) {
|
|
104
|
+
signal.throwIfAborted();
|
|
105
|
+
const target = await service.app.db.getRepository(import_constants.COLLECTION_NAME).findOne({
|
|
106
|
+
filter: { id: run.get("targetId"), buildRunId: run.id, status: "building" }
|
|
107
|
+
});
|
|
108
|
+
if (!target) throw new Error(service.t("Workflow request was canceled or superseded"));
|
|
109
|
+
if (type === "visualization-analyze") {
|
|
110
|
+
const input = run.get("input");
|
|
111
|
+
const [started] = await service.app.db.getCollection(import_constants.COLLECTION_NAME).model.update(
|
|
112
|
+
{ buildPhase: "analyzing", buildStartedAt: /* @__PURE__ */ new Date(), buildWorkerId: service.app.name },
|
|
113
|
+
{ where: { id: target.id, buildRunId: run.id, status: "building" } }
|
|
114
|
+
);
|
|
115
|
+
if (!started) throw new Error(service.t("Workflow request was canceled or superseded"));
|
|
116
|
+
const summary = await (0, import_introspector.introspect)(service.app, {
|
|
117
|
+
dataSource: input.dataSource || "main",
|
|
118
|
+
collections: input.collections
|
|
119
|
+
});
|
|
120
|
+
signal.throwIfAborted();
|
|
121
|
+
const analysis = await (0, import_analyzer.analyze)(service.app, { ...input, summary, signal });
|
|
122
|
+
signal.throwIfAborted();
|
|
123
|
+
return { kind: type, summary, ...analysis };
|
|
124
|
+
}
|
|
125
|
+
const prior = previous;
|
|
126
|
+
const expected = type === "visualization-validate" ? "visualization-analyze" : "visualization-validate";
|
|
127
|
+
if (!prior || prior.kind !== expected || !prior.summary)
|
|
128
|
+
throw new Error(service.t("Workflow stage input is invalid"));
|
|
129
|
+
const spec = import_blockSpec.blockSpecSchema.parse(prior.spec);
|
|
130
|
+
if (type === "visualization-validate") {
|
|
131
|
+
const validation = (0, import_validator.validate)(spec, prior.summary);
|
|
132
|
+
return {
|
|
133
|
+
...prior,
|
|
134
|
+
kind: type,
|
|
135
|
+
spec: validation.usedFallback ? (0, import_fallback.buildFallbackSpec)(prior.summary) : validation.spec,
|
|
136
|
+
usedFallback: prior.usedFallback || validation.usedFallback,
|
|
137
|
+
adjustments: validation.adjustments
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
const generated = (0, import_generator.generate)(spec, prior.summary);
|
|
141
|
+
if (generated.ok === false) throw new Error(service.t("Schema generation failed") + ": " + generated.error);
|
|
142
|
+
return { ...prior, kind: type, schema: generated.schema, usedFallback: prior.usedFallback || generated.usedFallback };
|
|
143
|
+
}
|
|
144
|
+
async function commitStage(service, type, run, output, transaction) {
|
|
145
|
+
const result = output;
|
|
146
|
+
const target = await service.app.db.getRepository(import_constants.COLLECTION_NAME).findOne({
|
|
147
|
+
filter: { id: run.get("targetId"), buildRunId: run.id, status: "building" },
|
|
148
|
+
transaction,
|
|
149
|
+
lock: true
|
|
150
|
+
});
|
|
151
|
+
if (!target) return false;
|
|
152
|
+
const complete = type === "visualization-generate";
|
|
153
|
+
await target.update(
|
|
154
|
+
{
|
|
155
|
+
buildPhase: complete ? "completed" : type === "visualization-validate" ? "generating" : "analyzing",
|
|
156
|
+
status: complete ? "completed" : "building",
|
|
157
|
+
buildHeartbeatAt: /* @__PURE__ */ new Date(),
|
|
158
|
+
usedFallback: result.usedFallback,
|
|
159
|
+
...complete ? { blockSpec: result.spec, blockSchema: result.schema, adjustments: result.adjustments, errorMessage: null } : {},
|
|
160
|
+
buildLog: service.t(
|
|
161
|
+
complete ? result.usedFallback ? "Build completed using fallback" : "Build completed in workflow" : "Build running in workflow"
|
|
162
|
+
)
|
|
163
|
+
},
|
|
164
|
+
{ transaction }
|
|
165
|
+
);
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
async function syncTerminal(service, run, status, output, transaction) {
|
|
169
|
+
const error = status === import_plugin_workflow.EXECUTION_STATUS.RESOLVED ? service.t("Workflow completed without finishing the plugin request") : output && typeof output === "object" && "error" in output ? String(output.error) : service.t("Workflow execution failed or was canceled");
|
|
170
|
+
await service.app.db.getCollection(import_constants.COLLECTION_NAME).model.update(
|
|
171
|
+
{ status: "error", buildPhase: "failed", errorMessage: error, buildLog: error },
|
|
172
|
+
{ where: { id: run.get("targetId"), buildRunId: run.id, status: "building" }, transaction }
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
async function recoverLegacy(service) {
|
|
176
|
+
const builds = await service.app.db.getRepository(import_constants.COLLECTION_NAME).find({ filter: { status: "building" } });
|
|
177
|
+
for (const build of builds) {
|
|
178
|
+
const lock = await service.app.lockManager.tryAcquire("build-visualization:request:" + build.id, 1e4);
|
|
179
|
+
await lock.runExclusive(async () => {
|
|
180
|
+
await build.reload();
|
|
181
|
+
if (build.get("status") !== "building") return;
|
|
182
|
+
const runId = build.get("buildRunId") || (0, import_crypto.randomUUID)();
|
|
183
|
+
if (await service.app.db.getRepository(import_workflow.RUNS_COLLECTION).findOne({ filterByTk: runId })) return;
|
|
184
|
+
const flow = await service.requireFlow();
|
|
185
|
+
await service.app.db.sequelize.transaction(
|
|
186
|
+
{ type: service.app.db.sequelize.getDialect() === "sqlite" ? import_database.Transaction.TYPES.IMMEDIATE : void 0 },
|
|
187
|
+
async (transaction) => {
|
|
188
|
+
await build.update({ buildRunId: runId, buildPhase: "queued" }, { transaction });
|
|
189
|
+
const input = Object.fromEntries(
|
|
190
|
+
["requirement", "dataSource", "collections", "primaryCollection", "llmService", "model"].map((key) => [
|
|
191
|
+
key,
|
|
192
|
+
build.get(key)
|
|
193
|
+
])
|
|
194
|
+
);
|
|
195
|
+
await service.enqueue(build.id, input, runId, transaction, flow);
|
|
196
|
+
}
|
|
197
|
+
);
|
|
198
|
+
}, 6e4);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
202
|
+
0 && (module.exports = {
|
|
203
|
+
commitStage,
|
|
204
|
+
executeStage,
|
|
205
|
+
getWorkflowService,
|
|
206
|
+
recoverLegacy,
|
|
207
|
+
requestBuild,
|
|
208
|
+
syncTerminal
|
|
209
|
+
});
|