@babylonjs-toolkit/agent 1.1.38 → 1.1.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +73 -4
- package/bin/bt-agent.js +23 -1
- package/lib/bridge/api.js +74 -0
- package/lib/bridge/args.js +172 -0
- package/lib/bridge/blender/discover.js +98 -0
- package/lib/bridge/blender/run.js +141 -0
- package/lib/bridge/cli.js +677 -0
- package/lib/bridge/config.js +193 -0
- package/lib/bridge/log.js +103 -0
- package/lib/bridge/loop.js +239 -0
- package/lib/bridge/pairing.js +101 -0
- package/lib/bridge/policy.js +396 -0
- package/lib/bridge/protocol.js +91 -0
- package/lib/bridge/service.js +395 -0
- package/lib/bridge/unity/automation.js +135 -0
- package/lib/bridge/unity/discover.js +606 -0
- package/lib/bridge/unity/guard.js +208 -0
- package/lib/bridge/unity/ops.js +1294 -0
- package/lib/bridge/unity/run.js +169 -0
- package/lib/bridge/unity/scaffold/bt-bootstrap.cs +39 -0
- package/lib/bridge/unity/scaffold/bt-newscene.cs +27 -0
- package/lib/doctor.js +33 -0
- package/package.json +1 -1
- package/skills/bt-execute/SKILL.md +4 -2
- package/skills/bt-plan/SKILL.md +4 -2
- package/skills/bt-spec/SKILL.md +5 -2
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Port of the App Builder's tiers.ts + validate.ts. THIS copy is the wall on the user's machine
|
|
5
|
+
* (D3): it is enforced whatever the server sent. Change both copies and both test tables together.
|
|
6
|
+
*
|
|
7
|
+
* App sources: `app/lib/bridge/tiers.ts` and `app/lib/bridge/validate.ts`. Test tables:
|
|
8
|
+
* `tests/bridge-policy.test.js` (copied from `tiers.spec.ts` / `validate.spec.ts`).
|
|
9
|
+
*
|
|
10
|
+
* Anything unknown is `consent` — the default-deny direction (D14).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const { BRIDGE_MAX_CAPTURE_PX, capText } = require('./protocol');
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {'allowed'|'scripts'|'consent'|'refused'} BridgeTier
|
|
17
|
+
* @typedef {{ tier: BridgeTier, reason?: string }} TierDecision
|
|
18
|
+
* @typedef {import('./protocol').BridgeOperation} BridgeOperation
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
// ── tiers.ts ────────────────────────────────────────────────────────────────────────────────────
|
|
22
|
+
|
|
23
|
+
const SCRIPT_COMMANDS = new Set(['run_script', 'eval', 'eval_file']);
|
|
24
|
+
const CONSENT_COMMANDS = new Set([
|
|
25
|
+
'set_import_settings',
|
|
26
|
+
'delete_gameobject',
|
|
27
|
+
'delete_asset',
|
|
28
|
+
'move_asset',
|
|
29
|
+
'rename_asset',
|
|
30
|
+
'package_remove',
|
|
31
|
+
'package_resolve',
|
|
32
|
+
'set_player_settings',
|
|
33
|
+
'set_quality_settings',
|
|
34
|
+
'set_physics_settings',
|
|
35
|
+
'set_tags_layers',
|
|
36
|
+
'set_lighting_settings',
|
|
37
|
+
'set_navmesh_settings',
|
|
38
|
+
'set_build_settings',
|
|
39
|
+
'build_player',
|
|
40
|
+
]);
|
|
41
|
+
const CONSENT_COMMAND_PATTERN = /^(delete|remove|move|rename|clear|reset|uninstall)_/;
|
|
42
|
+
const ALLOWED_COMMAND_PATTERN =
|
|
43
|
+
/^(get|find|list|search|create|add|set|instantiate|open|save|import|bake|capture|screenshot|console|editor|package_add|package_status|wait_for|recompile|run_tests|bt_|select|apply|assign|attach|duplicate|load|play|stop|pause|refresh|describe|inspect)/;
|
|
44
|
+
|
|
45
|
+
/** @type {Record<string, string>} */
|
|
46
|
+
const REFUSED_CLI = {
|
|
47
|
+
command: 'use the unity_command tool',
|
|
48
|
+
cmd: 'use the unity_command tool',
|
|
49
|
+
open: 'use the unity_editor tool',
|
|
50
|
+
close: 'use the unity_editor tool',
|
|
51
|
+
run: 'not available through the Unity Bridge',
|
|
52
|
+
shell: 'not available through the Unity Bridge',
|
|
53
|
+
license: 'Unity licences are never touched by the Unity Bridge',
|
|
54
|
+
auth: 'Unity sign-in is never touched by the Unity Bridge',
|
|
55
|
+
mcp: 'not available through the Unity Bridge',
|
|
56
|
+
skill: 'not available through the Unity Bridge',
|
|
57
|
+
job: 'use the bridge_job tool',
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/** first arg → allowed second args ('*' = any). @type {Record<string, string[] | '*'>} */
|
|
61
|
+
const ALLOWED_CLI = {
|
|
62
|
+
status: '*',
|
|
63
|
+
logs: '*',
|
|
64
|
+
recompile: '*',
|
|
65
|
+
test: '*',
|
|
66
|
+
templates: '*',
|
|
67
|
+
releases: '*',
|
|
68
|
+
doctor: '*',
|
|
69
|
+
docs: '*',
|
|
70
|
+
list: '*',
|
|
71
|
+
editors: ['list', 'path', 'running'],
|
|
72
|
+
projects: ['info', 'verify'],
|
|
73
|
+
vcs: ['diff', 'blame', 'status'],
|
|
74
|
+
assets: ['export'],
|
|
75
|
+
pipeline: ['list', 'list-versions', 'status'],
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const CONSENT_CLI = new Set([
|
|
79
|
+
'install',
|
|
80
|
+
'uninstall',
|
|
81
|
+
'install-modules',
|
|
82
|
+
'self-update',
|
|
83
|
+
'build',
|
|
84
|
+
'projects',
|
|
85
|
+
'vcs',
|
|
86
|
+
'assets',
|
|
87
|
+
'pipeline',
|
|
88
|
+
]); // reached only when not ALLOWED above
|
|
89
|
+
|
|
90
|
+
/** @type {Record<'allowed'|'scripts'|'consent', number>} */
|
|
91
|
+
const RANK = { allowed: 0, scripts: 1, consent: 2 };
|
|
92
|
+
|
|
93
|
+
/** @param {object} record @param {string} key */
|
|
94
|
+
function hasOwn(record, key) {
|
|
95
|
+
return Object.prototype.hasOwnProperty.call(record, key);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** @param {string} name @returns {TierDecision} */
|
|
99
|
+
function classifyCommandName(name) {
|
|
100
|
+
if (SCRIPT_COMMANDS.has(name)) return { tier: 'scripts' };
|
|
101
|
+
if (CONSENT_COMMANDS.has(name) || CONSENT_COMMAND_PATTERN.test(name)) return { tier: 'consent' };
|
|
102
|
+
if (ALLOWED_COMMAND_PATTERN.test(name)) return { tier: 'allowed' };
|
|
103
|
+
return { tier: 'consent', reason: 'unrecognised command — asking first' };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** @param {string} name @param {Record<string, unknown>|undefined} params @returns {TierDecision} */
|
|
107
|
+
function classifyCommand(name, params) {
|
|
108
|
+
if (name === 'batch') {
|
|
109
|
+
const commands = params && typeof params === 'object' ? params.commands : undefined;
|
|
110
|
+
if (!Array.isArray(commands)) {
|
|
111
|
+
return { tier: 'consent', reason: 'a batch without a commands list — asking first' };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** @type {TierDecision} */
|
|
115
|
+
let highest = { tier: 'allowed' };
|
|
116
|
+
for (const entry of commands) {
|
|
117
|
+
const innerName = entry && typeof entry === 'object' ? entry.name : undefined;
|
|
118
|
+
if (typeof innerName !== 'string') {
|
|
119
|
+
return { tier: 'consent', reason: 'a batch entry without a command name — asking first' };
|
|
120
|
+
}
|
|
121
|
+
// A nested batch is never unwrapped: it is classified by its name alone (unrecognised → consent).
|
|
122
|
+
/** @type {TierDecision} */
|
|
123
|
+
const inner = innerName === 'batch' ? { tier: 'consent' } : classifyCommandName(innerName);
|
|
124
|
+
if (RANK[/** @type {'allowed'} */ (inner.tier)] > RANK[/** @type {'allowed'} */ (highest.tier)]) {
|
|
125
|
+
highest = inner;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return highest;
|
|
129
|
+
}
|
|
130
|
+
return classifyCommandName(name);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** @param {unknown} args @returns {TierDecision} */
|
|
134
|
+
function classifyCli(args) {
|
|
135
|
+
if (!Array.isArray(args) || args.length === 0) {
|
|
136
|
+
return { tier: 'refused', reason: 'an empty Unity CLI command' };
|
|
137
|
+
}
|
|
138
|
+
const [first, second] = args;
|
|
139
|
+
if (hasOwn(REFUSED_CLI, first)) return { tier: 'refused', reason: REFUSED_CLI[first] };
|
|
140
|
+
// Printing help changes nothing — but only after the refusal check, so ['shell','--help'] stays refused.
|
|
141
|
+
if (args.some((arg) => arg === '--help' || arg === '-h')) return { tier: 'allowed' };
|
|
142
|
+
if (hasOwn(ALLOWED_CLI, first)) {
|
|
143
|
+
const allowed = ALLOWED_CLI[first];
|
|
144
|
+
// A bare command group (['editors']) prints its help/list, so it is allowed like its read-only verbs.
|
|
145
|
+
if (allowed === '*' || second === undefined || (typeof second === 'string' && allowed.includes(second))) {
|
|
146
|
+
return { tier: 'allowed' };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (CONSENT_CLI.has(first)) return { tier: 'consent' };
|
|
150
|
+
return { tier: 'consent' };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @param {BridgeOperation} op
|
|
155
|
+
* @returns {TierDecision}
|
|
156
|
+
*/
|
|
157
|
+
function classifyOperation(op) {
|
|
158
|
+
switch (op && op.kind) {
|
|
159
|
+
case 'unity.command':
|
|
160
|
+
return classifyCommand(/** @type {string} */ (op.name), /** @type {any} */ (op.params));
|
|
161
|
+
case 'unity.cli':
|
|
162
|
+
return classifyCli(op.args);
|
|
163
|
+
case 'unity.script':
|
|
164
|
+
case 'blender.script':
|
|
165
|
+
return { tier: 'scripts' };
|
|
166
|
+
case 'unity.project':
|
|
167
|
+
case 'unity.list':
|
|
168
|
+
case 'unity.capture':
|
|
169
|
+
case 'unity.editor':
|
|
170
|
+
case 'devserver.start':
|
|
171
|
+
case 'devserver.status':
|
|
172
|
+
return { tier: 'allowed' };
|
|
173
|
+
default:
|
|
174
|
+
return { tier: 'consent', reason: 'unrecognised operation — asking first' };
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// ── validate.ts ─────────────────────────────────────────────────────────────────────────────────
|
|
179
|
+
|
|
180
|
+
const PATH_PARAMS = new Set(['output', 'folder', 'save_path', 'file', 'outputPath']);
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Param keys a `unity.command` may never carry (D41): they name the Unity CLI's own options, so a model
|
|
184
|
+
* could otherwise turn a parameter into `--yes` or re-point `--project-path`. The helper also passes
|
|
185
|
+
* every param after `--`, where the CLI stops reading options. `timeout` and `format` are NOT here: real
|
|
186
|
+
* commands declare them (`eval`, `run_tests`, `get_serialized_fields`), and after `--` they reach the
|
|
187
|
+
* command, never the CLI.
|
|
188
|
+
*/
|
|
189
|
+
const RESERVED_PARAM_KEYS = new Set(['yes', 'project-path', 'non-interactive', 'result-only', 'detach', 'project']);
|
|
190
|
+
|
|
191
|
+
const MAX_SOURCE_CHARS = 200_000;
|
|
192
|
+
const MAX_CLI_ARG_CHARS = 512;
|
|
193
|
+
const MAX_BLENDER_PATHS = 32;
|
|
194
|
+
const COMMAND_NAME = /^[a-z][a-z0-9_]{1,63}$/;
|
|
195
|
+
const PARAM_KEY = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
|
|
196
|
+
const SCRIPT_ENTRY = /^[A-Za-z_][\w.]*\.[A-Za-z_]\w*$/;
|
|
197
|
+
/** A Unity project folder name inside the projects folder (D54) — never a path; no trailing dot/space (Windows rewrites them). */
|
|
198
|
+
const PROJECT_NAME = /^[A-Za-z0-9](?:[A-Za-z0-9 _.-]{0,62}[A-Za-z0-9_-])?$/;
|
|
199
|
+
const PROJECT_ACTIONS = new Set(['list', 'open', 'create']);
|
|
200
|
+
|
|
201
|
+
/** @param {unknown} name @returns {boolean} */
|
|
202
|
+
function isValidProjectName(name) {
|
|
203
|
+
return typeof name === 'string' && PROJECT_NAME.test(name) && !name.includes('..');
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* true for '/x', '\\x', '~/x', 'C:\\x', 'C:/x', or any value with a '..' path segment.
|
|
208
|
+
* @param {string} value
|
|
209
|
+
* @returns {boolean}
|
|
210
|
+
*/
|
|
211
|
+
function isUnsafePath(value) {
|
|
212
|
+
if (value.startsWith('/') || value.startsWith('\\') || value.startsWith('~')) return true;
|
|
213
|
+
// A drive root or a drive path ('C:', 'C:/x', 'C:\\x') — never any 'x:' prefix ('t:Texture' is a search).
|
|
214
|
+
if (/^[A-Za-z]:([\\/]|$)/.test(value)) return true;
|
|
215
|
+
return value.split(/[\\/]/).some((segment) => segment === '..');
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** @param {unknown} value @returns {value is Record<string, unknown>} */
|
|
219
|
+
function isPlainObject(value) {
|
|
220
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
221
|
+
const proto = Object.getPrototypeOf(value);
|
|
222
|
+
return proto === Object.prototype || proto === null;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** @param {unknown} value @param {number} min @param {number} max */
|
|
226
|
+
function isIntegerIn(value, min, max) {
|
|
227
|
+
return typeof value === 'number' && Number.isInteger(value) && value >= min && value <= max;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** @param {unknown} source @returns {string|null} */
|
|
231
|
+
function checkSource(source) {
|
|
232
|
+
if (typeof source !== 'string' || source.length === 0) {
|
|
233
|
+
return 'The script source is empty — pass the full script text.';
|
|
234
|
+
}
|
|
235
|
+
if (source.length > MAX_SOURCE_CHARS) {
|
|
236
|
+
return `The script source is ${source.length} characters; the limit is ${MAX_SOURCE_CHARS}.`;
|
|
237
|
+
}
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** @param {string} label @param {unknown} list @returns {string|null} */
|
|
242
|
+
function checkPathList(label, list) {
|
|
243
|
+
if (!Array.isArray(list) || list.length > MAX_BLENDER_PATHS || list.some((item) => typeof item !== 'string')) {
|
|
244
|
+
return `${label} must be a list of at most ${MAX_BLENDER_PATHS} Unity-project-relative paths.`;
|
|
245
|
+
}
|
|
246
|
+
const unsafe = list.find(isUnsafePath);
|
|
247
|
+
if (unsafe !== undefined) {
|
|
248
|
+
return `${label} contains "${unsafe}" — use a Unity-project-relative path (no leading /, \\, ~, drive letter or "..").`;
|
|
249
|
+
}
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* null when valid, else a sentence for the model.
|
|
255
|
+
* @param {BridgeOperation} op
|
|
256
|
+
* @returns {string|null}
|
|
257
|
+
*/
|
|
258
|
+
function validateOperation(op) {
|
|
259
|
+
switch (op && op.kind) {
|
|
260
|
+
case 'unity.command': {
|
|
261
|
+
if (typeof op.name !== 'string' || !COMMAND_NAME.test(op.name)) {
|
|
262
|
+
return 'The command name must be a lower-case Unity command name such as "set_transform" — call unity_list_commands to see them.';
|
|
263
|
+
}
|
|
264
|
+
if (!isPlainObject(op.params)) return 'The command params must be a JSON object.';
|
|
265
|
+
for (const [key, value] of Object.entries(op.params)) {
|
|
266
|
+
if (!PARAM_KEY.test(key)) {
|
|
267
|
+
return `The command parameter name "${key}" is not valid — use the parameter names unity_list_commands shows.`;
|
|
268
|
+
}
|
|
269
|
+
if (RESERVED_PARAM_KEYS.has(key)) {
|
|
270
|
+
return `The command parameter "${key}" is not allowed — it is reserved for the Unity Bridge itself.`;
|
|
271
|
+
}
|
|
272
|
+
if (PATH_PARAMS.has(key) && typeof value === 'string' && isUnsafePath(value)) {
|
|
273
|
+
return `The "${key}" path "${value}" is not allowed — use a Unity-project-relative path (for scratch output, .bridge/out/…).`;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
case 'unity.cli': {
|
|
280
|
+
if (!Array.isArray(op.args)) return 'The Unity CLI arguments must be a list of strings.';
|
|
281
|
+
for (const arg of op.args) {
|
|
282
|
+
if (typeof arg !== 'string' || arg.length > MAX_CLI_ARG_CHARS) {
|
|
283
|
+
return `Every Unity CLI argument must be a string of at most ${MAX_CLI_ARG_CHARS} characters.`;
|
|
284
|
+
}
|
|
285
|
+
if (arg.includes('\0')) return 'A Unity CLI argument may not contain a NUL character.';
|
|
286
|
+
if (isUnsafePath(arg)) {
|
|
287
|
+
return `The Unity CLI argument "${arg}" is not allowed — use a Unity-project-relative path.`;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
case 'unity.script': {
|
|
294
|
+
const sourceError = checkSource(op.source);
|
|
295
|
+
if (sourceError) return sourceError;
|
|
296
|
+
if (typeof op.entry !== 'string' || !SCRIPT_ENTRY.test(op.entry)) {
|
|
297
|
+
return 'The script entry must be a static method written as Class.Method (for example "BridgeScript.Run").';
|
|
298
|
+
}
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
case 'unity.capture': {
|
|
303
|
+
if (!isIntegerIn(op.width, 64, BRIDGE_MAX_CAPTURE_PX) || !isIntegerIn(op.height, 64, BRIDGE_MAX_CAPTURE_PX)) {
|
|
304
|
+
return `The capture width and height must be whole numbers between 64 and ${BRIDGE_MAX_CAPTURE_PX}.`;
|
|
305
|
+
}
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
case 'blender.script': {
|
|
310
|
+
const sourceError = checkSource(op.source);
|
|
311
|
+
if (sourceError) return sourceError;
|
|
312
|
+
const inputsError = checkPathList('inputs', op.inputs);
|
|
313
|
+
if (inputsError) return inputsError;
|
|
314
|
+
const outputsError = checkPathList('outputs', op.outputs);
|
|
315
|
+
if (outputsError) return outputsError;
|
|
316
|
+
if (!isIntegerIn(op.timeoutSeconds, 10, 3600)) {
|
|
317
|
+
return 'The Blender timeoutSeconds must be a whole number between 10 and 3600.';
|
|
318
|
+
}
|
|
319
|
+
return null;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
case 'unity.project': {
|
|
323
|
+
if (typeof op.action !== 'string' || !PROJECT_ACTIONS.has(op.action)) return 'unity_project action must be list, open or create.';
|
|
324
|
+
if (op.action === 'list') return null;
|
|
325
|
+
if (op.name === undefined || op.name === null || op.name === '') return `unity_project needs a project name for ${op.action}.`;
|
|
326
|
+
// `open` may name the projects folder too — `<folder>/<name>` — when a name exists in more than one (D55).
|
|
327
|
+
const parts = op.action === 'open' && typeof op.name === 'string' && op.name.split('/').length === 2 ? op.name.split('/') : [op.name];
|
|
328
|
+
if (!parts.every(isValidProjectName)) {
|
|
329
|
+
return `The Unity project name "${op.name}" is not valid — use letters, numbers, spaces, dots, dashes or underscores.`;
|
|
330
|
+
}
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
case 'devserver.start': {
|
|
335
|
+
if (op.port !== undefined && !isIntegerIn(op.port, 1025, 65535)) {
|
|
336
|
+
return 'The dev server port must be a whole number between 1025 and 65535.';
|
|
337
|
+
}
|
|
338
|
+
return null;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
default:
|
|
342
|
+
return null;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// ── pricing.ts (the long-operation half only: it sets the helper's timeouts, D41) ────────────────
|
|
347
|
+
|
|
348
|
+
const LONG_COMMANDS = new Set([
|
|
349
|
+
'bt_export_level',
|
|
350
|
+
'bt_export_prefab',
|
|
351
|
+
'bt_export_animation',
|
|
352
|
+
'bt_build_project',
|
|
353
|
+
'bake_lighting',
|
|
354
|
+
'bake_navmesh',
|
|
355
|
+
'bake_navmesh_surfaces',
|
|
356
|
+
'bake_occlusion_culling',
|
|
357
|
+
'run_tests',
|
|
358
|
+
]);
|
|
359
|
+
const LONG_CLI = new Set(['test', 'build', 'recompile']);
|
|
360
|
+
|
|
361
|
+
/** @param {BridgeOperation} op @returns {boolean} */
|
|
362
|
+
function isLongOperation(op) {
|
|
363
|
+
if (op.kind === 'unity.command') {
|
|
364
|
+
if (LONG_COMMANDS.has(/** @type {string} */ (op.name))) return true;
|
|
365
|
+
const params = /** @type {any} */ (op.params);
|
|
366
|
+
if (op.name === 'batch' && params && Array.isArray(params.commands)) {
|
|
367
|
+
return params.commands.some((/** @type {any} */ c) => c && LONG_COMMANDS.has(c.name));
|
|
368
|
+
}
|
|
369
|
+
return false;
|
|
370
|
+
}
|
|
371
|
+
if (op.kind === 'unity.cli') return Array.isArray(op.args) && LONG_CLI.has(op.args[0]);
|
|
372
|
+
if (op.kind === 'unity.project') return op.action === 'create' || op.action === 'open';
|
|
373
|
+
if (op.kind === 'blender.script') return typeof op.timeoutSeconds === 'number' && op.timeoutSeconds > 120;
|
|
374
|
+
return false;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
module.exports = {
|
|
378
|
+
SCRIPT_COMMANDS,
|
|
379
|
+
CONSENT_COMMANDS,
|
|
380
|
+
CONSENT_COMMAND_PATTERN,
|
|
381
|
+
ALLOWED_COMMAND_PATTERN,
|
|
382
|
+
REFUSED_CLI,
|
|
383
|
+
ALLOWED_CLI,
|
|
384
|
+
CONSENT_CLI,
|
|
385
|
+
PATH_PARAMS,
|
|
386
|
+
RESERVED_PARAM_KEYS,
|
|
387
|
+
LONG_COMMANDS,
|
|
388
|
+
LONG_CLI,
|
|
389
|
+
PROJECT_NAME,
|
|
390
|
+
isValidProjectName,
|
|
391
|
+
classifyOperation,
|
|
392
|
+
isUnsafePath,
|
|
393
|
+
validateOperation,
|
|
394
|
+
isLongOperation,
|
|
395
|
+
capText,
|
|
396
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Unity Bridge wire protocol.
|
|
5
|
+
*
|
|
6
|
+
* Port of the App Builder's `app/lib/bridge/protocol.ts` — keep the constants identical (D3).
|
|
7
|
+
* The types live there as TypeScript; here they are described in JSDoc only.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const BRIDGE_POLL_HOLD_MS = 25_000;
|
|
11
|
+
const BRIDGE_PRESENCE_MS = 45_000;
|
|
12
|
+
const BRIDGE_PICKUP_TIMEOUT_MS = 30_000;
|
|
13
|
+
const BRIDGE_SYNC_WAIT_MS = 60_000;
|
|
14
|
+
const BRIDGE_JOB_WAIT_MAX_S = 90;
|
|
15
|
+
const BRIDGE_CONSENT_TIMEOUT_MS = 120_000;
|
|
16
|
+
const BRIDGE_JOB_RETENTION_MS = 600_000;
|
|
17
|
+
const BRIDGE_LAST_SEEN_WRITE_MS = 60_000;
|
|
18
|
+
const BRIDGE_MAX_RESULT_CHARS = 20_000;
|
|
19
|
+
const BRIDGE_MAX_IMAGE_BASE64 = 400_000;
|
|
20
|
+
const BRIDGE_MAX_CAPTURE_PX = 1024;
|
|
21
|
+
const BRIDGE_TOOLKIT_MIN_VERSION = '9.25.1';
|
|
22
|
+
const BRIDGE_PROTOCOL_VERSION = 2;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @typedef {{ key: string, name: string, productGuid?: string, unityVersion?: string, toolkitVersion?: string, pipelineVersion?: string }} BridgeUnityProject
|
|
26
|
+
* @typedef {{ running: boolean, origin?: string, root?: string, project?: string, listen?: 'loopback'|'all', scenes?: string[] }} BridgeDevServerInfo
|
|
27
|
+
* @typedef {{
|
|
28
|
+
* protocol: number,
|
|
29
|
+
* helperVersion: string,
|
|
30
|
+
* os: 'darwin'|'win32'|'linux',
|
|
31
|
+
* unityCli?: { path: string, version: string },
|
|
32
|
+
* blender?: { path: string, version: string },
|
|
33
|
+
* projectsDir: string,
|
|
34
|
+
* unityProjects: BridgeUnityProject[],
|
|
35
|
+
* currentProject?: string,
|
|
36
|
+
* devServer?: BridgeDevServerInfo,
|
|
37
|
+
* scriptsDisabledLocally: boolean,
|
|
38
|
+
* }} BridgeHello
|
|
39
|
+
* (`projectsDir` is the BASENAME of the projects folder — a full local path never leaves this computer;
|
|
40
|
+
* `currentProject` names the project every Unity/Blender job runs against, D54.)
|
|
41
|
+
* @typedef {{ kind: string, [key: string]: unknown }} BridgeOperation
|
|
42
|
+
* (includes `{ kind: 'unity.project', action: 'list'|'open'|'create', name?: string }`, D54)
|
|
43
|
+
* @typedef {{ jobId: string, op: BridgeOperation, allowScripts: boolean, consentGranted: boolean }} BridgeDispatch
|
|
44
|
+
* @typedef {{ jobId: string, cancel: true }} BridgeCancel
|
|
45
|
+
* @typedef {{ jobs: BridgeDispatch[], cancels: BridgeCancel[] }} BridgePollResponse
|
|
46
|
+
* @typedef {{ ok: boolean, text: string, image?: { base64: string, mimeType: 'image/png' }, exitCode?: number }} BridgeResultPayload
|
|
47
|
+
* @typedef {{ jobId: string, type: 'started' }
|
|
48
|
+
* | { jobId: string, type: 'progress', line: string }
|
|
49
|
+
* | { jobId: string, type: 'final', result: BridgeResultPayload }
|
|
50
|
+
* | { jobId: string, type: 'refused', reason: string }} BridgeJobEvent
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A job id is used in file names on this computer (`.bridge/…/<jobId>`), so the helper accepts only
|
|
55
|
+
* this shape whatever the server sent (D3). The App Builder mints `brg_<time>_<hex>`.
|
|
56
|
+
*/
|
|
57
|
+
const SAFE_JOB_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
58
|
+
|
|
59
|
+
/** @param {unknown} jobId @returns {jobId is string} */
|
|
60
|
+
function isSafeJobId(jobId) {
|
|
61
|
+
return typeof jobId === 'string' && SAFE_JOB_ID.test(jobId);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Keep the TAIL; announce truncation.
|
|
66
|
+
* @param {string} text
|
|
67
|
+
* @param {number} [max]
|
|
68
|
+
* @returns {string}
|
|
69
|
+
*/
|
|
70
|
+
function capText(text, max = BRIDGE_MAX_RESULT_CHARS) {
|
|
71
|
+
return text.length <= max ? text : `…(earlier output truncated)\n${text.slice(text.length - max)}`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
module.exports = {
|
|
75
|
+
BRIDGE_POLL_HOLD_MS,
|
|
76
|
+
BRIDGE_PRESENCE_MS,
|
|
77
|
+
BRIDGE_PICKUP_TIMEOUT_MS,
|
|
78
|
+
BRIDGE_SYNC_WAIT_MS,
|
|
79
|
+
BRIDGE_JOB_WAIT_MAX_S,
|
|
80
|
+
BRIDGE_CONSENT_TIMEOUT_MS,
|
|
81
|
+
BRIDGE_JOB_RETENTION_MS,
|
|
82
|
+
BRIDGE_LAST_SEEN_WRITE_MS,
|
|
83
|
+
BRIDGE_MAX_RESULT_CHARS,
|
|
84
|
+
BRIDGE_MAX_IMAGE_BASE64,
|
|
85
|
+
BRIDGE_MAX_CAPTURE_PX,
|
|
86
|
+
BRIDGE_TOOLKIT_MIN_VERSION,
|
|
87
|
+
BRIDGE_PROTOCOL_VERSION,
|
|
88
|
+
capText,
|
|
89
|
+
SAFE_JOB_ID,
|
|
90
|
+
isSafeJobId,
|
|
91
|
+
};
|