@babylonjs-toolkit/agent 1.1.38 → 1.1.44
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 +80 -7
- 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/persona.md +1 -1
- package/skills/bt-execute/SKILL.md +32 -11
- package/skills/bt-plan/SKILL.md +4 -2
- package/skills/bt-spec/SKILL.md +5 -2
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The helper's own wall (D3, D14, D15, D20, D39): tiers, paths, the toolkit version floor and the
|
|
5
|
+
* unsaved-work guard. Enforced whatever the App Builder sent.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const fs = require('fs');
|
|
9
|
+
const path = require('path');
|
|
10
|
+
const { classifyOperation, validateOperation, isUnsafePath } = require('../policy');
|
|
11
|
+
const { BRIDGE_TOOLKIT_MIN_VERSION } = require('../protocol');
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* D58: a model-supplied script is refused when the computer's Allow scripts switch is off (the dispatch
|
|
15
|
+
* says `allowScripts: false`) OR the helper runs with --no-scripts — one sentence for each, because the
|
|
16
|
+
* user fixes them in different places. When both are true the --no-scripts sentence wins: turning the
|
|
17
|
+
* switch on would not help while this computer refuses scripts itself.
|
|
18
|
+
*/
|
|
19
|
+
const SCRIPTS_OFF =
|
|
20
|
+
'Scripts are off for this computer — the user can turn on Allow scripts in the Unity Bridge dialog (the cube icon in the App Builder).';
|
|
21
|
+
const SCRIPTS_DISABLED_LOCALLY =
|
|
22
|
+
'Scripts are disabled on this computer (--no-scripts) — the Allow scripts switch has no effect; the user must re-run the install command from the Unity Bridge dialog without --no-scripts.';
|
|
23
|
+
const CONSENT_MISSING = 'consent was not granted';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Numeric major.minor.patch compare; pre-release suffixes are ignored.
|
|
27
|
+
* @param {string} a @param {string} b @returns {number} <0, 0, >0
|
|
28
|
+
*/
|
|
29
|
+
function compareVersions(a, b) {
|
|
30
|
+
const parse = (/** @type {string} */ v) =>
|
|
31
|
+
String(v)
|
|
32
|
+
.split(/[.\-+]/)
|
|
33
|
+
.slice(0, 3)
|
|
34
|
+
.map((n) => parseInt(n, 10) || 0);
|
|
35
|
+
const x = parse(a);
|
|
36
|
+
const y = parse(b);
|
|
37
|
+
for (let i = 0; i < 3; i += 1) {
|
|
38
|
+
const d = (x[i] || 0) - (y[i] || 0);
|
|
39
|
+
if (d !== 0) return d;
|
|
40
|
+
}
|
|
41
|
+
return 0;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The name of the toolkit command an operation runs, if any (`bt_*`).
|
|
46
|
+
* @param {import('../protocol').BridgeOperation} op
|
|
47
|
+
* @returns {string|undefined}
|
|
48
|
+
*/
|
|
49
|
+
function toolkitCommandOf(op) {
|
|
50
|
+
if (op.kind === 'unity.command' && typeof op.name === 'string' && op.name.startsWith('bt_')) return op.name;
|
|
51
|
+
if (op.kind === 'devserver.start') return 'bt_devserver_start';
|
|
52
|
+
if (op.kind === 'devserver.status') return 'bt_devserver_status';
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @param {import('../protocol').BridgeDispatch} dispatch
|
|
58
|
+
* @param {{ toolkitVersion?: string }} project
|
|
59
|
+
* @param {{ noScripts?: boolean }} [opts]
|
|
60
|
+
* @returns {{ ok: true, tier: string } | { ok: false, reason: string }}
|
|
61
|
+
*/
|
|
62
|
+
function helperTier(dispatch, project, { noScripts = false } = {}) {
|
|
63
|
+
const op = dispatch && dispatch.op;
|
|
64
|
+
if (!op || typeof op !== 'object') return { ok: false, reason: 'The job carried no operation.' };
|
|
65
|
+
|
|
66
|
+
const invalid = validateOperation(op);
|
|
67
|
+
if (invalid) return { ok: false, reason: invalid };
|
|
68
|
+
|
|
69
|
+
const decision = classifyOperation(op);
|
|
70
|
+
if (decision.tier === 'refused') {
|
|
71
|
+
return { ok: false, reason: `Refused on this computer: ${decision.reason || 'not available through the Unity Bridge'}.` };
|
|
72
|
+
}
|
|
73
|
+
if (decision.tier === 'scripts' && noScripts) return { ok: false, reason: SCRIPTS_DISABLED_LOCALLY };
|
|
74
|
+
if (decision.tier === 'scripts' && dispatch.allowScripts !== true) return { ok: false, reason: SCRIPTS_OFF };
|
|
75
|
+
if (decision.tier === 'consent' && !dispatch.consentGranted) return { ok: false, reason: CONSENT_MISSING };
|
|
76
|
+
|
|
77
|
+
const bt = toolkitCommandOf(op);
|
|
78
|
+
const v = project && project.toolkitVersion;
|
|
79
|
+
if (bt && v && compareVersions(v, BRIDGE_TOOLKIT_MIN_VERSION) < 0) {
|
|
80
|
+
return {
|
|
81
|
+
ok: false,
|
|
82
|
+
reason: `Babylon Toolkit ${v} is older than ${BRIDGE_TOOLKIT_MIN_VERSION}, which the bt_* commands need. Upgrade the com.babylontoolkit.editor package.`,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return { ok: true, tier: decision.tier };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* A Unity-project-relative path resolved under `root`, or null when it would escape it (D20).
|
|
90
|
+
* Any `X:` prefix is refused too: a Windows drive-relative `C:x` resolves outside the project.
|
|
91
|
+
* @param {string} root
|
|
92
|
+
* @param {unknown} rel
|
|
93
|
+
* @returns {string|null}
|
|
94
|
+
*/
|
|
95
|
+
function resolveProjectPath(root, rel) {
|
|
96
|
+
if (typeof rel !== 'string' || rel.length === 0 || rel.includes('\0')) return null;
|
|
97
|
+
if (isUnsafePath(rel) || /^[A-Za-z]:/.test(rel)) return null;
|
|
98
|
+
const base = path.resolve(root);
|
|
99
|
+
const abs = path.resolve(base, rel);
|
|
100
|
+
if (abs !== base && !abs.startsWith(base.endsWith(path.sep) ? base : base + path.sep)) return null;
|
|
101
|
+
return abs;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The helper's scratch folders under `<project>/.bridge/`. */
|
|
105
|
+
const SCRATCH_DIRS = ['out', 'scripts', 'blender'];
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* After a job's clean-up: removes `.bridge/out`, `.bridge/scripts` and `.bridge/blender` when they are
|
|
109
|
+
* EMPTY, then `.bridge` itself when that leaves it empty. `rmdir` (never a recursive delete) is the
|
|
110
|
+
* guarantee: the OS refuses a folder with anything in it, so a capture the model kept, a file the user
|
|
111
|
+
* put there, or a symlink is never touched. Best effort — a failure never affects the job.
|
|
112
|
+
* @param {string} root the Unity project root
|
|
113
|
+
*/
|
|
114
|
+
function pruneEmptyScratchDirs(root) {
|
|
115
|
+
const bridge = path.join(root, '.bridge');
|
|
116
|
+
for (const dir of [...SCRATCH_DIRS.map((name) => path.join(bridge, name)), bridge]) {
|
|
117
|
+
try {
|
|
118
|
+
fs.rmdirSync(dir);
|
|
119
|
+
} catch {
|
|
120
|
+
// not empty, not there, or not a folder — leave it
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* @param {unknown} node
|
|
127
|
+
* @param {string[]} found
|
|
128
|
+
* @param {number} depth
|
|
129
|
+
*/
|
|
130
|
+
function walkDirty(node, found, depth) {
|
|
131
|
+
if (depth > 12 || node === null || typeof node !== 'object') return;
|
|
132
|
+
if (Array.isArray(node)) {
|
|
133
|
+
for (const item of node) walkDirty(item, found, depth + 1);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const o = /** @type {Record<string, unknown>} */ (node);
|
|
137
|
+
if (o.dirty === true || o.isDirty === true) {
|
|
138
|
+
const label = typeof o.path === 'string' && o.path ? o.path : typeof o.name === 'string' ? o.name : undefined;
|
|
139
|
+
if (label && !found.includes(label)) found.push(label);
|
|
140
|
+
}
|
|
141
|
+
for (const value of Object.values(o)) walkDirty(value, found, depth + 1);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* D39: which open scenes have unsaved changes. `runUnity(args)` runs the Unity CLI for THIS project
|
|
146
|
+
* (it adds nothing — the args are complete).
|
|
147
|
+
* @param {(args: string[]) => Promise<import('./run').RunResult>} runUnity
|
|
148
|
+
* @param {string} projectRoot
|
|
149
|
+
* @returns {Promise<{ names: string[] } | { error: string }>}
|
|
150
|
+
*/
|
|
151
|
+
async function dirtyScenes(runUnity, projectRoot) {
|
|
152
|
+
let res;
|
|
153
|
+
try {
|
|
154
|
+
res = await runUnity([
|
|
155
|
+
'command',
|
|
156
|
+
'list_open_scenes',
|
|
157
|
+
'--project-path',
|
|
158
|
+
projectRoot,
|
|
159
|
+
'--format',
|
|
160
|
+
'json',
|
|
161
|
+
'--non-interactive',
|
|
162
|
+
]);
|
|
163
|
+
} catch (err) {
|
|
164
|
+
return { error: err && err.message ? err.message : String(err) };
|
|
165
|
+
}
|
|
166
|
+
let parsed;
|
|
167
|
+
try {
|
|
168
|
+
parsed = JSON.parse(res.stdout);
|
|
169
|
+
} catch {
|
|
170
|
+
const detail = (res.stderr || res.stdout || '').trim().split(/\r?\n/).pop();
|
|
171
|
+
return { error: res.timedOut ? 'Unity did not answer in time' : detail || `exit code ${res.code}` };
|
|
172
|
+
}
|
|
173
|
+
if (parsed && parsed.success === false) {
|
|
174
|
+
const first = Array.isArray(parsed.errors) && parsed.errors[0];
|
|
175
|
+
return { error: (first && first.message) || 'Unity could not list the open scenes' };
|
|
176
|
+
}
|
|
177
|
+
/** @type {string[]} */
|
|
178
|
+
const names = [];
|
|
179
|
+
walkDirty(parsed, names, 0);
|
|
180
|
+
return { names };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* The D39 refusal sentence, or null when nothing is unsaved.
|
|
185
|
+
* @param {{ names: string[] } | { error: string }} state
|
|
186
|
+
* @returns {string|null}
|
|
187
|
+
*/
|
|
188
|
+
function unsavedWorkReason(state) {
|
|
189
|
+
if ('error' in state) {
|
|
190
|
+
return `Could not check Unity for unsaved changes (${state.error}). Save them with unity_command save_all if they are yours, or ask the user.`;
|
|
191
|
+
}
|
|
192
|
+
if (!state.names.length) return null;
|
|
193
|
+
return `Unity has unsaved changes in ${state.names.join(', ')}. Save them with unity_command save_all if they are yours, or ask the user.`;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
module.exports = {
|
|
197
|
+
helperTier,
|
|
198
|
+
resolveProjectPath,
|
|
199
|
+
pruneEmptyScratchDirs,
|
|
200
|
+
SCRATCH_DIRS,
|
|
201
|
+
dirtyScenes,
|
|
202
|
+
unsavedWorkReason,
|
|
203
|
+
compareVersions,
|
|
204
|
+
toolkitCommandOf,
|
|
205
|
+
SCRIPTS_OFF,
|
|
206
|
+
SCRIPTS_DISABLED_LOCALLY,
|
|
207
|
+
CONSENT_MISSING,
|
|
208
|
+
};
|