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
package/src/server/plugin.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
1
10
|
import { InstallOptions, Plugin } from '@nocobase/server';
|
|
2
11
|
import { resolve } from 'path';
|
|
3
12
|
import type { ZodTypeAny } from 'zod';
|
|
4
13
|
import { COLLECTION_NAME, SETTINGS_COLLECTION_NAME } from '../shared/constants';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
recoverInterruptedBuilds,
|
|
9
|
-
registerBuildQueue,
|
|
10
|
-
retry,
|
|
11
|
-
startBuildQueueProcessor,
|
|
12
|
-
unregisterBuildQueue,
|
|
13
|
-
} from './actions/build';
|
|
14
|
+
import { build, retry, getResult } from './actions/build';
|
|
15
|
+
import { WorkflowService } from './workflow';
|
|
16
|
+
import { RUNS_COLLECTION } from '../shared/workflow';
|
|
14
17
|
import aiVisualizationBuildSettings from './resource/settings';
|
|
15
18
|
import { buildVisualizationBlockTool } from './tools';
|
|
16
19
|
|
|
@@ -56,13 +59,14 @@ interface AIToolsManagerLike {
|
|
|
56
59
|
* - `beforeLoad`: import the collection definitions so the database/model layer
|
|
57
60
|
* is available before resources are registered.
|
|
58
61
|
* - `load`: register resource actions, ACL snippets, AI tools, and lifecycle
|
|
59
|
-
* handlers for
|
|
60
|
-
* - `afterStart` / `beforeStop`: start and stop
|
|
62
|
+
* handlers for native workflow instructions.
|
|
63
|
+
* - `afterStart` / `beforeStop`: start and stop workflow recovery and pending instruction work.
|
|
61
64
|
* - `install` / `upgrade`: reconcile schema idempotently for fresh installs and
|
|
62
65
|
* upgrades.
|
|
63
66
|
*/
|
|
64
67
|
export class PluginBuildVisualizationBlockServer extends Plugin {
|
|
65
|
-
|
|
68
|
+
workflowService!: WorkflowService;
|
|
69
|
+
private readonly schemaCollections = [COLLECTION_NAME, SETTINGS_COLLECTION_NAME, RUNS_COLLECTION];
|
|
66
70
|
|
|
67
71
|
afterAdd() {}
|
|
68
72
|
|
|
@@ -106,24 +110,11 @@ export class PluginBuildVisualizationBlockServer extends Plugin {
|
|
|
106
110
|
actions: [`${SETTINGS_COLLECTION_NAME}:get`, `${SETTINGS_COLLECTION_NAME}:update`],
|
|
107
111
|
});
|
|
108
112
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
this.app.on('
|
|
114
|
-
startBuildQueueProcessor(this.app);
|
|
115
|
-
try {
|
|
116
|
-
await recoverInterruptedBuilds(this.app);
|
|
117
|
-
} catch (err) {
|
|
118
|
-
this.app.logger.warn('[plugin-build-visualization-block] Failed to recover interrupted builds', err);
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
this.app.on('beforeStop', () => {
|
|
122
|
-
unregisterBuildQueue(this.app);
|
|
123
|
-
});
|
|
124
|
-
this.app.on('beforeDestroy', () => {
|
|
125
|
-
unregisterBuildQueue(this.app);
|
|
126
|
-
});
|
|
113
|
+
this.workflowService = new WorkflowService(this.app);
|
|
114
|
+
this.workflowService.load();
|
|
115
|
+
this.app.on('afterStart', () => this.workflowService.start());
|
|
116
|
+
this.app.on('beforeStop', () => this.workflowService.stop());
|
|
117
|
+
this.app.on('beforeDestroy', () => this.workflowService.stop());
|
|
127
118
|
|
|
128
119
|
this.registerAITools();
|
|
129
120
|
}
|
|
@@ -203,7 +194,7 @@ export class PluginBuildVisualizationBlockServer extends Plugin {
|
|
|
203
194
|
await queryInterface.addColumn(
|
|
204
195
|
tableName,
|
|
205
196
|
columnName,
|
|
206
|
-
columnDefinition as Parameters<typeof queryInterface.addColumn>[2],
|
|
197
|
+
columnDefinition as unknown as Parameters<typeof queryInterface.addColumn>[2],
|
|
207
198
|
);
|
|
208
199
|
columns[columnName] = columnDefinition;
|
|
209
200
|
this.app.logger.info(
|
|
@@ -236,19 +227,19 @@ export class PluginBuildVisualizationBlockServer extends Plugin {
|
|
|
236
227
|
async afterEnable() {}
|
|
237
228
|
|
|
238
229
|
async beforeDisable() {
|
|
239
|
-
|
|
230
|
+
await this.workflowService?.stop();
|
|
240
231
|
}
|
|
241
232
|
|
|
242
233
|
async afterDisable() {
|
|
243
|
-
|
|
234
|
+
await this.workflowService?.stop();
|
|
244
235
|
}
|
|
245
236
|
|
|
246
237
|
async beforeUnload() {
|
|
247
|
-
|
|
238
|
+
await this.workflowService?.stop();
|
|
248
239
|
}
|
|
249
240
|
|
|
250
241
|
async remove() {
|
|
251
|
-
|
|
242
|
+
await this.workflowService?.stop();
|
|
252
243
|
}
|
|
253
244
|
}
|
|
254
245
|
|
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
|
|
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
|
+
|
|
2
10
|
import { z } from 'zod';
|
|
3
11
|
|
|
4
12
|
import type { Application } from '@nocobase/server';
|
|
5
13
|
import { COLLECTION_NAME, MAX_COLLECTIONS, MIN_COLLECTIONS, SETTINGS_COLLECTION_NAME } from '../../shared/constants';
|
|
6
|
-
import {
|
|
14
|
+
import { requestBuild, getWorkflowService } from '../workflow-domain';
|
|
7
15
|
|
|
8
16
|
/**
|
|
9
17
|
* The zod input schema for the `build_visualization_block` AI tool. The shape
|
|
@@ -125,34 +133,16 @@ export default {
|
|
|
125
133
|
return { status: 'error', content: 'AI service and model are required.' };
|
|
126
134
|
}
|
|
127
135
|
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
dataSource,
|
|
137
|
-
primaryCollection: collections[0],
|
|
138
|
-
llmService,
|
|
139
|
-
model,
|
|
140
|
-
status: 'building',
|
|
141
|
-
buildPhase: 'queued',
|
|
142
|
-
buildRunId: runId,
|
|
143
|
-
buildQueuedAt: new Date(),
|
|
144
|
-
...(createdById !== undefined ? { createdById } : {}),
|
|
145
|
-
},
|
|
136
|
+
const record = await requestBuild(getWorkflowService(ctx.app), {
|
|
137
|
+
requirement,
|
|
138
|
+
collections,
|
|
139
|
+
dataSource,
|
|
140
|
+
primaryCollection: collections[0],
|
|
141
|
+
llmService,
|
|
142
|
+
model,
|
|
143
|
+
createdById: ctx.auth?.user?.id ?? null,
|
|
146
144
|
});
|
|
147
|
-
|
|
148
145
|
const id = record.get('id');
|
|
149
|
-
const buildId = String(id);
|
|
150
|
-
await enqueueBuild(ctx.app, {
|
|
151
|
-
buildId,
|
|
152
|
-
runId,
|
|
153
|
-
userId: createdById ?? null,
|
|
154
|
-
queuedAt: new Date().toISOString(),
|
|
155
|
-
});
|
|
156
146
|
|
|
157
147
|
return {
|
|
158
148
|
status: 'success',
|
|
@@ -0,0 +1,235 @@
|
|
|
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 { randomUUID } from 'crypto';
|
|
11
|
+
import type { Application } from '@nocobase/server';
|
|
12
|
+
import { type Model, Transaction } from '@nocobase/database';
|
|
13
|
+
import { EXECUTION_STATUS } from '@nocobase/plugin-workflow';
|
|
14
|
+
import { COLLECTION_NAME } from '../shared/constants';
|
|
15
|
+
import { RUNS_COLLECTION } from '../shared/workflow';
|
|
16
|
+
import { blockSpecSchema, type SchemaSummary, type BlockSpec } from '../shared/blockSpec';
|
|
17
|
+
import type { WorkflowService } from './workflow';
|
|
18
|
+
import type PluginBuildVisualizationBlockServer from './plugin';
|
|
19
|
+
import { introspect } from './pipeline/introspector';
|
|
20
|
+
import { analyze } from './pipeline/analyzer';
|
|
21
|
+
import { validate } from './pipeline/validator';
|
|
22
|
+
import { generate } from './pipeline/generator';
|
|
23
|
+
import { buildFallbackSpec } from './pipeline/fallback';
|
|
24
|
+
|
|
25
|
+
export function getWorkflowService(app: Application): WorkflowService {
|
|
26
|
+
const plugin = app.pm.get<PluginBuildVisualizationBlockServer>('plugin-build-visualization-block');
|
|
27
|
+
if (!plugin?.workflowService)
|
|
28
|
+
throw new Error(
|
|
29
|
+
String(app.i18n.t('Managed workflow is unavailable or disabled', { ns: 'plugin-build-visualization-block' })),
|
|
30
|
+
);
|
|
31
|
+
return plugin.workflowService;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function requestBuild(
|
|
35
|
+
service: WorkflowService,
|
|
36
|
+
values: Record<string, unknown>,
|
|
37
|
+
targetId?: string | number,
|
|
38
|
+
) {
|
|
39
|
+
const flow = await service.requireFlow();
|
|
40
|
+
const db = service.app.db;
|
|
41
|
+
const lock = await service.app.lockManager.tryAcquire(
|
|
42
|
+
'build-visualization:request:' + (targetId ?? randomUUID()),
|
|
43
|
+
10_000,
|
|
44
|
+
);
|
|
45
|
+
return lock.runExclusive(async () => {
|
|
46
|
+
const repo = db.getRepository(COLLECTION_NAME);
|
|
47
|
+
const previous = targetId ? await repo.findOne({ filterByTk: targetId }) : null;
|
|
48
|
+
if (previous?.get('buildRunId')) await service.cancel(previous.get('buildRunId'));
|
|
49
|
+
return db.sequelize.transaction(
|
|
50
|
+
{ type: db.sequelize.getDialect() === 'sqlite' ? Transaction.TYPES.IMMEDIATE : undefined },
|
|
51
|
+
async (transaction) => {
|
|
52
|
+
const runId = randomUUID();
|
|
53
|
+
const patch = {
|
|
54
|
+
...values,
|
|
55
|
+
status: 'building',
|
|
56
|
+
buildPhase: 'queued',
|
|
57
|
+
buildRunId: runId,
|
|
58
|
+
buildQueuedAt: new Date(),
|
|
59
|
+
buildStartedAt: null,
|
|
60
|
+
buildHeartbeatAt: null,
|
|
61
|
+
buildWorkerId: null,
|
|
62
|
+
blockSpec: null,
|
|
63
|
+
blockSchema: null,
|
|
64
|
+
adjustments: null,
|
|
65
|
+
usedFallback: false,
|
|
66
|
+
errorMessage: null,
|
|
67
|
+
buildLog: service.t('Build queued in workflow'),
|
|
68
|
+
};
|
|
69
|
+
let build = targetId ? await repo.findOne({ filterByTk: targetId, transaction, lock: true }) : null;
|
|
70
|
+
if (targetId && !build) throw new Error(service.t('Build record not found'));
|
|
71
|
+
if (build) await build.update(patch, { transaction });
|
|
72
|
+
else build = await repo.create({ values: patch, transaction });
|
|
73
|
+
const input = Object.fromEntries(
|
|
74
|
+
['requirement', 'dataSource', 'collections', 'primaryCollection', 'llmService', 'model'].map((key) => [
|
|
75
|
+
key,
|
|
76
|
+
build.get(key),
|
|
77
|
+
]),
|
|
78
|
+
);
|
|
79
|
+
await service.enqueue(build.id, input, runId, transaction, flow);
|
|
80
|
+
return build;
|
|
81
|
+
},
|
|
82
|
+
);
|
|
83
|
+
}, 60_000);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface StageResult {
|
|
87
|
+
kind: string;
|
|
88
|
+
summary: SchemaSummary;
|
|
89
|
+
spec: BlockSpec;
|
|
90
|
+
usedFallback: boolean;
|
|
91
|
+
error?: string;
|
|
92
|
+
adjustments?: unknown;
|
|
93
|
+
schema?: unknown;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export async function executeStage(
|
|
97
|
+
service: WorkflowService,
|
|
98
|
+
type: string,
|
|
99
|
+
run: Model,
|
|
100
|
+
previous: unknown,
|
|
101
|
+
signal: AbortSignal,
|
|
102
|
+
): Promise<StageResult> {
|
|
103
|
+
signal.throwIfAborted();
|
|
104
|
+
const target = await service.app.db.getRepository(COLLECTION_NAME).findOne({
|
|
105
|
+
filter: { id: run.get('targetId'), buildRunId: run.id, status: 'building' },
|
|
106
|
+
});
|
|
107
|
+
if (!target) throw new Error(service.t('Workflow request was canceled or superseded'));
|
|
108
|
+
if (type === 'visualization-analyze') {
|
|
109
|
+
const input = run.get('input') as {
|
|
110
|
+
requirement: string;
|
|
111
|
+
dataSource?: string;
|
|
112
|
+
collections: string[];
|
|
113
|
+
llmService: string;
|
|
114
|
+
model: string;
|
|
115
|
+
};
|
|
116
|
+
const [started] = await service.app.db
|
|
117
|
+
.getCollection(COLLECTION_NAME)
|
|
118
|
+
.model.update(
|
|
119
|
+
{ buildPhase: 'analyzing', buildStartedAt: new Date(), buildWorkerId: service.app.name },
|
|
120
|
+
{ where: { id: target.id, buildRunId: run.id, status: 'building' } },
|
|
121
|
+
);
|
|
122
|
+
if (!started) throw new Error(service.t('Workflow request was canceled or superseded'));
|
|
123
|
+
const summary = await introspect(service.app, {
|
|
124
|
+
dataSource: input.dataSource || 'main',
|
|
125
|
+
collections: input.collections,
|
|
126
|
+
});
|
|
127
|
+
signal.throwIfAborted();
|
|
128
|
+
const analysis = await analyze(service.app, { ...input, summary, signal });
|
|
129
|
+
signal.throwIfAborted();
|
|
130
|
+
return { kind: type, summary, ...analysis };
|
|
131
|
+
}
|
|
132
|
+
const prior = previous as StageResult | null;
|
|
133
|
+
const expected = type === 'visualization-validate' ? 'visualization-analyze' : 'visualization-validate';
|
|
134
|
+
if (!prior || prior.kind !== expected || !prior.summary)
|
|
135
|
+
throw new Error(service.t('Workflow stage input is invalid'));
|
|
136
|
+
const spec = blockSpecSchema.parse(prior.spec);
|
|
137
|
+
if (type === 'visualization-validate') {
|
|
138
|
+
const validation = validate(spec, prior.summary);
|
|
139
|
+
return {
|
|
140
|
+
...prior,
|
|
141
|
+
kind: type,
|
|
142
|
+
spec: validation.usedFallback ? buildFallbackSpec(prior.summary) : validation.spec,
|
|
143
|
+
usedFallback: prior.usedFallback || validation.usedFallback,
|
|
144
|
+
adjustments: validation.adjustments,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
const generated = generate(spec, prior.summary);
|
|
148
|
+
if (generated.ok === false) throw new Error(service.t('Schema generation failed') + ': ' + generated.error);
|
|
149
|
+
return { ...prior, kind: type, schema: generated.schema, usedFallback: prior.usedFallback || generated.usedFallback };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export async function commitStage(
|
|
153
|
+
service: WorkflowService,
|
|
154
|
+
type: string,
|
|
155
|
+
run: Model,
|
|
156
|
+
output: unknown,
|
|
157
|
+
transaction: Transaction,
|
|
158
|
+
) {
|
|
159
|
+
const result = output as StageResult;
|
|
160
|
+
const target = await service.app.db.getRepository(COLLECTION_NAME).findOne({
|
|
161
|
+
filter: { id: run.get('targetId'), buildRunId: run.id, status: 'building' },
|
|
162
|
+
transaction,
|
|
163
|
+
lock: true,
|
|
164
|
+
});
|
|
165
|
+
if (!target) return false;
|
|
166
|
+
const complete = type === 'visualization-generate';
|
|
167
|
+
await target.update(
|
|
168
|
+
{
|
|
169
|
+
buildPhase: complete ? 'completed' : type === 'visualization-validate' ? 'generating' : 'analyzing',
|
|
170
|
+
status: complete ? 'completed' : 'building',
|
|
171
|
+
buildHeartbeatAt: new Date(),
|
|
172
|
+
usedFallback: result.usedFallback,
|
|
173
|
+
...(complete
|
|
174
|
+
? { blockSpec: result.spec, blockSchema: result.schema, adjustments: result.adjustments, errorMessage: null }
|
|
175
|
+
: {}),
|
|
176
|
+
buildLog: service.t(
|
|
177
|
+
complete
|
|
178
|
+
? result.usedFallback
|
|
179
|
+
? 'Build completed using fallback'
|
|
180
|
+
: 'Build completed in workflow'
|
|
181
|
+
: 'Build running in workflow',
|
|
182
|
+
),
|
|
183
|
+
},
|
|
184
|
+
{ transaction },
|
|
185
|
+
);
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export async function syncTerminal(
|
|
190
|
+
service: WorkflowService,
|
|
191
|
+
run: Model,
|
|
192
|
+
status: number,
|
|
193
|
+
output: unknown,
|
|
194
|
+
transaction: Transaction,
|
|
195
|
+
) {
|
|
196
|
+
const error =
|
|
197
|
+
status === EXECUTION_STATUS.RESOLVED
|
|
198
|
+
? service.t('Workflow completed without finishing the plugin request')
|
|
199
|
+
: output && typeof output === 'object' && 'error' in output
|
|
200
|
+
? String(output.error)
|
|
201
|
+
: service.t('Workflow execution failed or was canceled');
|
|
202
|
+
await service.app.db
|
|
203
|
+
.getCollection(COLLECTION_NAME)
|
|
204
|
+
.model.update(
|
|
205
|
+
{ status: 'error', buildPhase: 'failed', errorMessage: error, buildLog: error },
|
|
206
|
+
{ where: { id: run.get('targetId'), buildRunId: run.id, status: 'building' }, transaction },
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export async function recoverLegacy(service: WorkflowService) {
|
|
211
|
+
const builds = await service.app.db.getRepository(COLLECTION_NAME).find({ filter: { status: 'building' } });
|
|
212
|
+
for (const build of builds) {
|
|
213
|
+
const lock = await service.app.lockManager.tryAcquire('build-visualization:request:' + build.id, 10_000);
|
|
214
|
+
await lock.runExclusive(async () => {
|
|
215
|
+
await build.reload();
|
|
216
|
+
if (build.get('status') !== 'building') return;
|
|
217
|
+
const runId = build.get('buildRunId') || randomUUID();
|
|
218
|
+
if (await service.app.db.getRepository(RUNS_COLLECTION).findOne({ filterByTk: runId })) return;
|
|
219
|
+
const flow = await service.requireFlow();
|
|
220
|
+
await service.app.db.sequelize.transaction(
|
|
221
|
+
{ type: service.app.db.sequelize.getDialect() === 'sqlite' ? Transaction.TYPES.IMMEDIATE : undefined },
|
|
222
|
+
async (transaction) => {
|
|
223
|
+
await build.update({ buildRunId: runId, buildPhase: 'queued' }, { transaction });
|
|
224
|
+
const input = Object.fromEntries(
|
|
225
|
+
['requirement', 'dataSource', 'collections', 'primaryCollection', 'llmService', 'model'].map((key) => [
|
|
226
|
+
key,
|
|
227
|
+
build.get(key),
|
|
228
|
+
]),
|
|
229
|
+
);
|
|
230
|
+
await service.enqueue(build.id, input, runId, transaction, flow);
|
|
231
|
+
},
|
|
232
|
+
);
|
|
233
|
+
}, 60_000);
|
|
234
|
+
}
|
|
235
|
+
}
|