@auto-mas/auto-mas-plugin-blockly 0.0.0-stage → 0.1.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/dist/LICENSE +661 -0
- package/dist/client/THIRD-PARTY-LICENSES.md +644 -0
- package/dist/client/index.js +65184 -0
- package/dist/client/media/1x1.gif +0 -0
- package/dist/client/media/delete-icon.svg +1 -0
- package/dist/client/media/dropdown-arrow.svg +1 -0
- package/dist/client/media/foldout-icon.svg +1 -0
- package/dist/client/media/handclosed.cur +0 -0
- package/dist/client/media/handdelete.cur +0 -0
- package/dist/client/media/handopen.cur +0 -0
- package/dist/client/media/pilcrow.png +0 -0
- package/dist/client/media/quote0.png +0 -0
- package/dist/client/media/quote1.png +0 -0
- package/dist/client/media/resize-handle.svg +3 -0
- package/dist/client/media/sprites.svg +74 -0
- package/dist/client/style.css +2 -0
- package/dist/index.js +1 -0
- package/dist/server/blockly-compiler.js +277 -0
- package/dist/server/blockly.js +157 -0
- package/dist/server/builtin-plugins/blockly.js +20 -0
- package/dist/server/features.js +31 -0
- package/package.json +33 -6
- package/README.md +0 -3
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { link, lstat, mkdir, open, readFile, realpath, unlink } from 'node:fs/promises';
|
|
3
|
+
import { join, relative } from 'node:path';
|
|
4
|
+
import { blocklyCompilerRevision, compileBlocklyWorkspace, copyBlocklyWorkspace, emitBlocklyModule, validateBlocklyProgram } from './blockly-compiler.js';
|
|
5
|
+
export const blocklyDraftCollection = 'blockly-drafts';
|
|
6
|
+
export const publishedNodeCollection = 'published-nodes';
|
|
7
|
+
export const blocklyCollections = [blocklyDraftCollection, publishedNodeCollection];
|
|
8
|
+
function isNodeId(value) {
|
|
9
|
+
return typeof value === 'string' && /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/u.test(value);
|
|
10
|
+
}
|
|
11
|
+
function isVersion(value) {
|
|
12
|
+
return typeof value === 'number' && Number.isSafeInteger(value) && value > 0;
|
|
13
|
+
}
|
|
14
|
+
function isName(value) {
|
|
15
|
+
return typeof value === 'string' && !!value.trim() && value.length <= 100 && !/[\x00-\x1f\x7f]/u.test(value);
|
|
16
|
+
}
|
|
17
|
+
const recordId = (id, version) => `${id}@${version}`;
|
|
18
|
+
function isPublished(row) {
|
|
19
|
+
return !!row && isNodeId(row.nodeId) && isVersion(row.version) && row.id === recordId(row.nodeId, row.version)
|
|
20
|
+
&& isName(row.name) && isVersion(row.draftRevision) && row.compilerRevision === blocklyCompilerRevision
|
|
21
|
+
&& typeof row.digest === 'string' && /^[a-f0-9]{64}$/u.test(row.digest);
|
|
22
|
+
}
|
|
23
|
+
function versions(store, id) {
|
|
24
|
+
return store.list(publishedNodeCollection).filter(row => isPublished(row) && (id === undefined || row.nodeId === id))
|
|
25
|
+
.sort((left, right) => left.version - right.version);
|
|
26
|
+
}
|
|
27
|
+
function render(program) {
|
|
28
|
+
const validated = validateBlocklyProgram(program);
|
|
29
|
+
const source = emitBlocklyModule(validated);
|
|
30
|
+
return { program: validated, source, digest: createHash('sha256').update(source).digest('hex') };
|
|
31
|
+
}
|
|
32
|
+
function isMissing(error) { return error.code === 'ENOENT'; }
|
|
33
|
+
async function resolveModule(dataDir, digest, source) {
|
|
34
|
+
const dataRoot = await realpath(dataDir);
|
|
35
|
+
const expected = join(dataRoot, 'blockly-artifacts');
|
|
36
|
+
await mkdir(expected, { recursive: true });
|
|
37
|
+
const root = await realpath(expected);
|
|
38
|
+
if (relative(expected, root))
|
|
39
|
+
throw new Error('发布节点目录必须位于应用数据目录内');
|
|
40
|
+
const path = join(root, `${digest}.mjs`);
|
|
41
|
+
const size = Buffer.byteLength(source);
|
|
42
|
+
async function verify() {
|
|
43
|
+
const info = await lstat(path).catch(error => {
|
|
44
|
+
if (!isMissing(error))
|
|
45
|
+
throw error;
|
|
46
|
+
});
|
|
47
|
+
if (!info)
|
|
48
|
+
return false;
|
|
49
|
+
if (!info.isFile() || info.size !== size || await readFile(path, 'utf8') !== source) {
|
|
50
|
+
throw new Error('发布节点的模块文件与保存的版本不匹配');
|
|
51
|
+
}
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
if (await verify())
|
|
55
|
+
return path;
|
|
56
|
+
const temporary = join(root, `${randomUUID()}.tmp`);
|
|
57
|
+
const file = await open(temporary, 'wx', 0o600);
|
|
58
|
+
try {
|
|
59
|
+
try {
|
|
60
|
+
await file.writeFile(source);
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
await file.close();
|
|
64
|
+
}
|
|
65
|
+
await link(temporary, path).catch(error => {
|
|
66
|
+
if (error.code !== 'EEXIST')
|
|
67
|
+
throw error;
|
|
68
|
+
});
|
|
69
|
+
if (!await verify())
|
|
70
|
+
throw new Error('未能生成发布节点的模块文件');
|
|
71
|
+
return path;
|
|
72
|
+
}
|
|
73
|
+
finally {
|
|
74
|
+
await unlink(temporary);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
export function createPublishedNodes(store, dataDir) {
|
|
78
|
+
return {
|
|
79
|
+
list() {
|
|
80
|
+
const groups = new Map();
|
|
81
|
+
for (const row of versions(store)) {
|
|
82
|
+
const group = groups.get(row.nodeId) ?? { id: row.nodeId, name: row.name, versions: [] };
|
|
83
|
+
group.name = row.name;
|
|
84
|
+
group.versions.push(row.version);
|
|
85
|
+
groups.set(row.nodeId, group);
|
|
86
|
+
}
|
|
87
|
+
return [...groups.values()];
|
|
88
|
+
},
|
|
89
|
+
async resolve(id, version) {
|
|
90
|
+
if (!isNodeId(id) || !isVersion(version))
|
|
91
|
+
throw new Error('发布节点或版本无效');
|
|
92
|
+
const row = store.get(publishedNodeCollection, recordId(id, version));
|
|
93
|
+
if (!isPublished(row))
|
|
94
|
+
throw new Error('发布节点不存在或编译版本不受支持');
|
|
95
|
+
const { source, digest } = render(row.program);
|
|
96
|
+
if (digest !== row.digest)
|
|
97
|
+
throw new Error('发布节点的内容校验失败');
|
|
98
|
+
const modulePath = await resolveModule(dataDir, digest, source);
|
|
99
|
+
return { modulePath, name: row.name };
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
export function createBlocklyProjects(store) {
|
|
104
|
+
function getDraft(id) {
|
|
105
|
+
if (!isNodeId(id))
|
|
106
|
+
throw new Error('Blockly 草稿 ID 无效');
|
|
107
|
+
const draft = store.get(blocklyDraftCollection, id);
|
|
108
|
+
if (!draft || !isName(draft.name) || !isVersion(draft.revision))
|
|
109
|
+
throw new Error('Blockly 草稿不存在或内容无效');
|
|
110
|
+
return draft;
|
|
111
|
+
}
|
|
112
|
+
function summary(draft) {
|
|
113
|
+
return { id: draft.id, name: draft.name, revision: draft.revision, versions: versions(store, draft.id).map(row => row.version) };
|
|
114
|
+
}
|
|
115
|
+
function describe(draft) { return { ...summary(draft), workspace: copyBlocklyWorkspace(draft.workspace) }; }
|
|
116
|
+
return {
|
|
117
|
+
list() {
|
|
118
|
+
return store.list(blocklyDraftCollection)
|
|
119
|
+
.filter(draft => isNodeId(draft.id) && isName(draft.name) && isVersion(draft.revision))
|
|
120
|
+
.map(summary);
|
|
121
|
+
},
|
|
122
|
+
get(id) { return describe(getDraft(id)); },
|
|
123
|
+
preview(workspace) { return { source: render(compileBlocklyWorkspace(workspace)).source }; },
|
|
124
|
+
save(input) {
|
|
125
|
+
const name = typeof input.name === 'string' ? input.name.trim() : '';
|
|
126
|
+
if (!isName(name))
|
|
127
|
+
throw new Error('请输入有效的 Blockly 草稿名称');
|
|
128
|
+
const workspace = copyBlocklyWorkspace(input.workspace);
|
|
129
|
+
return store.transaction(() => {
|
|
130
|
+
const previous = input.id === undefined ? undefined : getDraft(input.id);
|
|
131
|
+
if (previous && input.revision !== previous.revision)
|
|
132
|
+
throw new Error('草稿已修改,请重新加载当前版本');
|
|
133
|
+
const row = store.put(blocklyDraftCollection, { id: previous?.id ?? randomUUID(), name, workspace, revision: (previous?.revision ?? 0) + 1 });
|
|
134
|
+
return describe(row);
|
|
135
|
+
});
|
|
136
|
+
},
|
|
137
|
+
publish(input) {
|
|
138
|
+
return store.transaction(() => {
|
|
139
|
+
const draft = getDraft(input.id);
|
|
140
|
+
if (input.revision !== draft.revision)
|
|
141
|
+
throw new Error('草稿已修改,请先保存并确认当前版本');
|
|
142
|
+
const { program, digest } = render(compileBlocklyWorkspace(draft.workspace));
|
|
143
|
+
const previous = versions(store, draft.id);
|
|
144
|
+
const existing = previous.find(row => row.draftRevision === draft.revision && row.digest === digest);
|
|
145
|
+
if (existing)
|
|
146
|
+
return { id: draft.id, name: existing.name, version: existing.version };
|
|
147
|
+
const version = (previous.at(-1)?.version ?? 0) + 1;
|
|
148
|
+
const id = recordId(draft.id, version);
|
|
149
|
+
if (store.get(publishedNodeCollection, id))
|
|
150
|
+
throw new Error('发布节点的版本记录冲突');
|
|
151
|
+
store.put(publishedNodeCollection, { id, nodeId: draft.id, name: draft.name, version,
|
|
152
|
+
draftRevision: draft.revision, compilerRevision: blocklyCompilerRevision, digest, program });
|
|
153
|
+
return { id: draft.id, name: draft.name, version };
|
|
154
|
+
});
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createBlocklyProjects, createPublishedNodes } from '../blockly.js';
|
|
2
|
+
import { store, ui } from '../features.js';
|
|
3
|
+
export default function createFeature({ dataDir }) {
|
|
4
|
+
return { id: 'blockly', name: 'Blockly 节点编辑器', description: '编排可复用节点,发布后可加入工作流。',
|
|
5
|
+
component: { inject: ['mas.store', 'mas.ui'], apply(ctx) {
|
|
6
|
+
const database = store(ctx);
|
|
7
|
+
const editor = createBlocklyProjects(database);
|
|
8
|
+
const published = createPublishedNodes(database, dataDir);
|
|
9
|
+
ui(ctx).scope(ctx, { id: 'blockly', path: '/blockly', view: 'builtin:blockly', title: 'Blockly 节点编辑器', icon: 'blockly', components: [] }, {
|
|
10
|
+
'blockly.projects': () => editor.list(),
|
|
11
|
+
'blockly.project': input => editor.get(input.id),
|
|
12
|
+
'blockly.preview': input => editor.preview(input.workspace),
|
|
13
|
+
'blockly.artifacts': () => published.list(),
|
|
14
|
+
}, {
|
|
15
|
+
'blockly.save': input => editor.save(input),
|
|
16
|
+
'blockly.publish': input => editor.publish(input),
|
|
17
|
+
});
|
|
18
|
+
} },
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const ui = (ctx) => ctx.get('mas.ui');
|
|
2
|
+
export const store = (ctx) => ctx.get('mas.store');
|
|
3
|
+
export const runner = (ctx) => ctx.get('mas.runner');
|
|
4
|
+
export function text(input, key, label, required = true) {
|
|
5
|
+
const value = input[key];
|
|
6
|
+
if (typeof value === 'string' && value.trim())
|
|
7
|
+
return value.trim();
|
|
8
|
+
if (required)
|
|
9
|
+
throw new Error(`请输入${label}`);
|
|
10
|
+
return '';
|
|
11
|
+
}
|
|
12
|
+
export function strings(value) {
|
|
13
|
+
if (!value)
|
|
14
|
+
return [];
|
|
15
|
+
if (Array.isArray(value) && value.every(item => typeof item === 'string'))
|
|
16
|
+
return value;
|
|
17
|
+
if (typeof value === 'string') {
|
|
18
|
+
if (value.trim().startsWith('[')) {
|
|
19
|
+
const parsed = JSON.parse(value);
|
|
20
|
+
if (Array.isArray(parsed) && parsed.every(item => typeof item === 'string'))
|
|
21
|
+
return parsed;
|
|
22
|
+
throw new Error('JSON 必须是字符串数组');
|
|
23
|
+
}
|
|
24
|
+
return value.split(/[\n,]+/).map(item => item.trim()).filter(Boolean);
|
|
25
|
+
}
|
|
26
|
+
throw new Error('请输入字符串列表');
|
|
27
|
+
}
|
|
28
|
+
export const field = (key, label, extra = {}) => ({ key, label, ...extra });
|
|
29
|
+
export const columns = (...pairs) => pairs.map(([key, label]) => ({ key, label }));
|
|
30
|
+
export const action = (id, label) => ({ id, label });
|
|
31
|
+
export const runColumns = columns(['name', '托管'], ['status', '状态'], ['started', '开始时间'], ['finished', '结束时间'], ['error', '结果详情']);
|
package/package.json
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@auto-mas/auto-mas-plugin-blockly",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@auto-mas/auto-mas-plugin-blockly",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "AGPL-3.0-only",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"exports": "./dist/index.js",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=24"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"auto-mas-plugin"
|
|
16
|
+
],
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"description": "AUTO-MAS blockly Cordis component",
|
|
21
|
+
"autoMas": {
|
|
22
|
+
"id": "blockly",
|
|
23
|
+
"abi": 1,
|
|
24
|
+
"views": [
|
|
25
|
+
"blockly"
|
|
26
|
+
],
|
|
27
|
+
"hostDependencies": {
|
|
28
|
+
"cordis": "4.0.0-rc.13",
|
|
29
|
+
"jsonc-parser": "3.3.1",
|
|
30
|
+
"schemastery": "3.18.0"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
package/README.md
DELETED