bambu-printer-mcp 1.1.67 → 1.1.69
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 +12 -0
- package/dist/bambu-network-bridge.d.ts +51 -1
- package/dist/bambu-network-bridge.js +230 -20
- package/dist/counterpart/model-plates.d.ts +20 -0
- package/dist/counterpart/model-plates.js +106 -0
- package/dist/counterpart/remote.js +1 -0
- package/dist/counterpart/setup.d.ts +2 -2
- package/dist/counterpart/sliced-file.d.ts +7 -2
- package/dist/counterpart/sliced-file.js +8 -2
- package/dist/design-guidance.d.ts +14 -0
- package/dist/design-guidance.js +27 -1
- package/dist/index.js +45 -9
- package/dist/safety/artifact.d.ts +5 -2
- package/dist/safety/artifact.js +6 -3
- package/dist/safety/print-file.js +6 -1
- package/dist/slicer/slicer-messages.d.ts +4 -0
- package/dist/slicer/slicer-messages.js +62 -0
- package/dist/stl/stl-manipulator.d.ts +4 -0
- package/dist/stl/stl-manipulator.js +8 -1
- package/dist/tool-annotations.js +1 -0
- package/package.json +1 -1
- package/src/bambu-network-bridge.ts +227 -24
- package/src/counterpart/model-plates.ts +111 -0
- package/src/counterpart/remote.ts +1 -0
- package/src/counterpart/sliced-file.ts +14 -5
- package/src/design-guidance.ts +30 -1
- package/src/index.ts +43 -9
- package/src/safety/artifact.ts +6 -3
- package/src/safety/print-file.ts +6 -1
- package/src/slicer/slicer-messages.ts +64 -0
- package/src/stl/stl-manipulator.ts +12 -1
- package/src/tool-annotations.ts +1 -0
package/README.md
CHANGED
|
@@ -1022,6 +1022,17 @@ Dry-run the AMS match without uploading or starting a print. The tool reads `Met
|
|
|
1022
1022
|
}
|
|
1023
1023
|
```
|
|
1024
1024
|
|
|
1025
|
+
#### list_3mf_plates
|
|
1026
|
+
|
|
1027
|
+
List a designer's 3MF plates as Bambu Studio shows them when the file is opened, before slicing. A downloaded kit's six plates are six plates here too. For each plate it gives `index` (from 0, so `slice_stl`'s `slice_plate` is `index + 1`), `name`, the `objects` on it, and `sliced` (whether the file already holds its G-code). It also gives `plate_count` and the file's `title`, `designer` and `licence` when they're set. A plain 3MF with no plate metadata is one plate. `include_thumbnails` adds each plate's own preview from the file. Read-only.
|
|
1028
|
+
|
|
1029
|
+
```json
|
|
1030
|
+
{
|
|
1031
|
+
"three_mf_path": "/Users/yourname/Downloads/kit.3mf",
|
|
1032
|
+
"include_thumbnails": true
|
|
1033
|
+
}
|
|
1034
|
+
```
|
|
1035
|
+
|
|
1025
1036
|
#### list_3mf_plate_objects
|
|
1026
1037
|
|
|
1027
1038
|
List object IDs from a sliced 3MF plate. Use this before `skip_objects` so you pass real Bambu object IDs instead of display-order guesses.
|
|
@@ -1457,6 +1468,7 @@ The server carries manufacturing judgment for agents that design or edit a model
|
|
|
1457
1468
|
- `fdm_multicolor` -- when several colours or support materials share a print: filament transitions across layers, colour placement, plate scheduling and flushing.
|
|
1458
1469
|
- `fdm_assembly` -- when a split, accent insert or joint could materially improve the result.
|
|
1459
1470
|
- `fdm_after_slice` -- a separate review turn after `slice_stl`, before presenting the manufacturing recommendation: keep, revise, or validate one specific uncertainty, using the returned `slicer_result`.
|
|
1471
|
+
- `fdm_slicing` -- for every design or model-editing task, next to `fdm_design`: how this server's configured slicer (`SLICER_TYPE`) orients a model and generates supports, and how to check a choice with a checkpoint `slice_stl`. Its text follows the slicer: Bambu Studio's auto-orient and `enable_support`, or plainly that a slicer here doesn't orient or take support settings.
|
|
1460
1472
|
- `guidance://fdm/capability-guidance` (resource) -- planning bands and sources for bridges and overhangs, for when one is consequential and there is no sufficiently specific measured capability for the printer, material and profile.
|
|
1461
1473
|
|
|
1462
1474
|
mcp-3D-printer-server serves the same guidance.
|
|
@@ -106,6 +106,50 @@ export type BambuNetworkBridgeStatus = {
|
|
|
106
106
|
export declare function bambuNetworkCertDir(configDir: string, env?: NodeJS.ProcessEnv): string;
|
|
107
107
|
export declare function prepareBambuNetworkCertDir(configDir: string, env?: NodeJS.ProcessEnv): string;
|
|
108
108
|
export declare function defaultBambuNetworkConfigDir(): string;
|
|
109
|
+
/**
|
|
110
|
+
* FULU's Linux host run in a Docker image (BAMBU_NETWORK_BRIDGE_IMAGE), for a Mac or any machine without the
|
|
111
|
+
* x86-64 Linux runtime installed natively. The image holds FULU's host runtime in /fulu (pjarczak_bambu_linux_host
|
|
112
|
+
* and ca-certificates.crt) and the networking plug-in in /plugins (libbambu_networking.so, libBambuSource.so).
|
|
113
|
+
*/
|
|
114
|
+
export type BridgeContainer = {
|
|
115
|
+
image: string;
|
|
116
|
+
docker: string;
|
|
117
|
+
dockerHost?: string;
|
|
118
|
+
/** A Colima profile started (never stopped or restarted) when Docker doesn't answer. */
|
|
119
|
+
colima?: {
|
|
120
|
+
executable: string;
|
|
121
|
+
profile: string;
|
|
122
|
+
home?: string;
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
export declare function bridgeContainer(env?: NodeJS.ProcessEnv): BridgeContainer | undefined;
|
|
126
|
+
/**
|
|
127
|
+
* Where a print's checked snapshot is written when the host runs in the Docker image: inside the agent's config
|
|
128
|
+
* folder, which the container mounts. The system temporary folder is no good there: Colima shares the home
|
|
129
|
+
* folder but not /tmp or /var/folders, so the host would see an empty folder. Undefined without the image.
|
|
130
|
+
*/
|
|
131
|
+
export declare function bridgeSnapshotRoot(env?: NodeJS.ProcessEnv): string | undefined;
|
|
132
|
+
/**
|
|
133
|
+
* The folders the host must see at the same absolute paths: the agent's config folder (the plug-in writes its
|
|
134
|
+
* logs and session there; a host given a log_dir it can't open aborts, FULU #54), the folder print snapshots are
|
|
135
|
+
* written to, and the certificate folder.
|
|
136
|
+
*/
|
|
137
|
+
export declare function bridgeContainerMounts(configDir: string, env?: NodeJS.ProcessEnv): string[];
|
|
138
|
+
/**
|
|
139
|
+
* docker run for the host: stdin/stdout are the bridge's framed protocol; the host is piped through cat (FULU #50).
|
|
140
|
+
* `--init` puts an init process first, so the SIGTERM docker forwards on stop reaches the host: a shell as the
|
|
141
|
+
* container's first process ignores it, and the container kept running after the server stopped. `name` lets stop
|
|
142
|
+
* remove the container even when the docker client was killed.
|
|
143
|
+
*/
|
|
144
|
+
export declare function bridgeContainerArgs(container: BridgeContainer, mounts: string[], name?: string): string[];
|
|
145
|
+
/**
|
|
146
|
+
* Docker must answer before the host starts. When it doesn't and a Colima profile is configured, that profile is
|
|
147
|
+
* started without changing Docker's global context; a running profile is never stopped or restarted.
|
|
148
|
+
*/
|
|
149
|
+
export declare function ensureBridgeContainerRuntime(container: BridgeContainer, timeouts?: {
|
|
150
|
+
infoMs: number;
|
|
151
|
+
startMs: number;
|
|
152
|
+
}): Promise<void>;
|
|
109
153
|
/**
|
|
110
154
|
* The client identity FULU's GUI gives the plug-in before agent start
|
|
111
155
|
* (GUI_App::init_http_extra_header). Through the Linux bridge FULU presents
|
|
@@ -127,6 +171,9 @@ export declare function linuxPluginLibraryEnv(env: NodeJS.ProcessEnv, platform:
|
|
|
127
171
|
export declare class BambuNetworkBridge {
|
|
128
172
|
private child?;
|
|
129
173
|
private commandLine?;
|
|
174
|
+
/** This bridge's own container, when the host runs in the Docker image: unique, so two servers never remove each other's. */
|
|
175
|
+
private readonly containerName;
|
|
176
|
+
private runningContainer?;
|
|
130
177
|
private stdoutBuffer;
|
|
131
178
|
private pending;
|
|
132
179
|
private nextRequestId;
|
|
@@ -143,7 +190,7 @@ export declare class BambuNetworkBridge {
|
|
|
143
190
|
* so LAN sessions run one at a time, as Bambu Studio connects one printer.
|
|
144
191
|
*/
|
|
145
192
|
private localSessionTail;
|
|
146
|
-
resolveBridgeCommand(override?: string): string;
|
|
193
|
+
resolveBridgeCommand(override?: string, configDir?: string): string;
|
|
147
194
|
resolveConfigDir(override?: string): string;
|
|
148
195
|
resolveCountryCode(override?: string): string;
|
|
149
196
|
getStatus(options?: BambuNetworkBridgeOptions): BambuNetworkBridgeStatus;
|
|
@@ -214,7 +261,10 @@ export declare class BambuNetworkBridge {
|
|
|
214
261
|
private controlTrace;
|
|
215
262
|
callWithAgent(method: string, payload?: JsonObject, options?: BambuNetworkBridgeOptions): Promise<unknown>;
|
|
216
263
|
stop(): Promise<void>;
|
|
264
|
+
/** A start in progress: concurrent callers wait for it, so a cold Docker host is started (and named) once. */
|
|
265
|
+
private starting?;
|
|
217
266
|
private ensureStarted;
|
|
267
|
+
private startBridge;
|
|
218
268
|
private handleStdout;
|
|
219
269
|
private resolvePendingJson;
|
|
220
270
|
private rememberLog;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
1
|
+
import { execFile, spawn } from "node:child_process";
|
|
2
2
|
import { createHash, randomUUID } from "node:crypto";
|
|
3
3
|
import fs from "node:fs";
|
|
4
4
|
import os from "node:os";
|
|
@@ -65,7 +65,8 @@ const nextControlSequence = () => String(controlSequence = controlSequence >= 29
|
|
|
65
65
|
* which the host must already be able to read.
|
|
66
66
|
*/
|
|
67
67
|
export function bambuNetworkCertDir(configDir, env = process.env) {
|
|
68
|
-
|
|
68
|
+
// Absolute, as every folder given to the host is: a containerized host runs from /plugins, not this server's folder.
|
|
69
|
+
return path.resolve(trimEnv(env.BAMBU_NETWORK_CERT_DIR) || path.join(configDir, "cert"));
|
|
69
70
|
}
|
|
70
71
|
export function prepareBambuNetworkCertDir(configDir, env = process.env) {
|
|
71
72
|
const certDir = bambuNetworkCertDir(configDir, env);
|
|
@@ -88,6 +89,176 @@ export function defaultBambuNetworkConfigDir() {
|
|
|
88
89
|
function shellQuote(value) {
|
|
89
90
|
return `'${value.replace(/'/g, "'\\''")}'`;
|
|
90
91
|
}
|
|
92
|
+
export function bridgeContainer(env = process.env) {
|
|
93
|
+
const image = trimEnv(env.BAMBU_NETWORK_BRIDGE_IMAGE);
|
|
94
|
+
if (!image)
|
|
95
|
+
return undefined;
|
|
96
|
+
const dockerHost = trimEnv(env.BAMBU_NETWORK_BRIDGE_DOCKER_HOST);
|
|
97
|
+
const profile = trimEnv(env.BAMBU_NETWORK_BRIDGE_COLIMA_PROFILE);
|
|
98
|
+
const home = trimEnv(env.BAMBU_NETWORK_BRIDGE_COLIMA_HOME);
|
|
99
|
+
// A Colima profile started with --activate=false leaves Docker's default context alone, so without an explicit
|
|
100
|
+
// host the server talks to that profile's own socket.
|
|
101
|
+
const colimaHome = home || path.join(os.homedir(), ".colima");
|
|
102
|
+
const host = dockerHost || (profile ? `unix://${path.join(colimaHome, profile, "docker.sock")}` : undefined);
|
|
103
|
+
return {
|
|
104
|
+
image,
|
|
105
|
+
docker: trimEnv(env.BAMBU_NETWORK_BRIDGE_DOCKER) || "docker",
|
|
106
|
+
...(host ? { dockerHost: host } : {}),
|
|
107
|
+
...(profile ? { colima: { executable: trimEnv(env.BAMBU_NETWORK_BRIDGE_COLIMA) || "colima", profile, ...(home ? { home } : {}) } } : {}),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/** `--host <socket>` for every docker call, when a host is set. */
|
|
111
|
+
function dockerHostArgs(container) {
|
|
112
|
+
return container.dockerHost ? ["--host", container.dockerHost] : [];
|
|
113
|
+
}
|
|
114
|
+
/** The image's fixed layout: FULU's runtime and the networking plug-in, with the CA bundle cloud calls need. */
|
|
115
|
+
const CONTAINER_ENV = [
|
|
116
|
+
"LD_LIBRARY_PATH=/plugins:/fulu",
|
|
117
|
+
"SSL_CERT_FILE=/fulu/ca-certificates.crt",
|
|
118
|
+
"CURL_CA_BUNDLE=/fulu/ca-certificates.crt",
|
|
119
|
+
"SSL_CERT_DIR=/etc/ssl/certs",
|
|
120
|
+
"PJARCZAK_BAMBU_PLUGIN_DIR=/plugins",
|
|
121
|
+
"PJARCZAK_BAMBU_NETWORK_SO=/plugins/libbambu_networking.so",
|
|
122
|
+
"PJARCZAK_BAMBU_SOURCE_SO=/plugins/libBambuSource.so",
|
|
123
|
+
];
|
|
124
|
+
/** Set by the server for the host (country, expected network version): passed into the container from its env. */
|
|
125
|
+
const CONTAINER_PASSED_ENV = ["PJARCZAK_BAMBU_COUNTRY_CODE", "PJARCZAK_EXPECTED_BAMBU_NETWORK_VERSION"];
|
|
126
|
+
/** The explicit host command, when one is set: it wins over the Docker image. */
|
|
127
|
+
function explicitBridgeCommand(env = process.env) {
|
|
128
|
+
return trimEnv(env.BAMBU_NETWORK_BRIDGE_COMMAND) || trimEnv(env.FULU_BAMBU_NETWORK_BRIDGE_COMMAND) || trimEnv(env.PJARCZAK_BAMBU_BRIDGE_COMMAND);
|
|
129
|
+
}
|
|
130
|
+
function configuredBridgeConfigDir(env = process.env) {
|
|
131
|
+
return path.resolve(trimEnv(env.BAMBU_NETWORK_CONFIG_DIR) || trimEnv(env.PJARCZAK_BAMBU_PROBE_LOG_DIR) || defaultBambuNetworkConfigDir());
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Where a print's checked snapshot is written when the host runs in the Docker image: inside the agent's config
|
|
135
|
+
* folder, which the container mounts. The system temporary folder is no good there: Colima shares the home
|
|
136
|
+
* folder but not /tmp or /var/folders, so the host would see an empty folder. Undefined without the image.
|
|
137
|
+
*/
|
|
138
|
+
export function bridgeSnapshotRoot(env = process.env) {
|
|
139
|
+
if (explicitBridgeCommand(env) || !bridgeContainer(env))
|
|
140
|
+
return undefined;
|
|
141
|
+
const root = path.join(configuredBridgeConfigDir(env), "print-snapshots");
|
|
142
|
+
fs.mkdirSync(root, { recursive: true, mode: 0o700 });
|
|
143
|
+
clearStaleSnapshots(root);
|
|
144
|
+
return root;
|
|
145
|
+
}
|
|
146
|
+
const STALE_SNAPSHOT_MS = 24 * 60 * 60 * 1000;
|
|
147
|
+
const clearedSnapshotRoots = new Set();
|
|
148
|
+
/**
|
|
149
|
+
* A snapshot kept for an upload still running is removed when the server exits; one a killed server left stays
|
|
150
|
+
* here, where no system cleanup reaches it. Once per server, snapshots older than a day are removed.
|
|
151
|
+
*/
|
|
152
|
+
function clearStaleSnapshots(root, now = Date.now()) {
|
|
153
|
+
if (clearedSnapshotRoots.has(root))
|
|
154
|
+
return;
|
|
155
|
+
clearedSnapshotRoots.add(root);
|
|
156
|
+
for (const name of fs.readdirSync(root)) {
|
|
157
|
+
if (!name.startsWith("bambu-print-"))
|
|
158
|
+
continue;
|
|
159
|
+
const dir = path.join(root, name);
|
|
160
|
+
try {
|
|
161
|
+
if (now - fs.statSync(dir).mtimeMs > STALE_SNAPSHOT_MS)
|
|
162
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
163
|
+
}
|
|
164
|
+
catch { /* gone already */ }
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* The folders the host must see at the same absolute paths: the agent's config folder (the plug-in writes its
|
|
169
|
+
* logs and session there; a host given a log_dir it can't open aborts, FULU #54), the folder print snapshots are
|
|
170
|
+
* written to, and the certificate folder.
|
|
171
|
+
*/
|
|
172
|
+
export function bridgeContainerMounts(configDir, env = process.env) {
|
|
173
|
+
fs.mkdirSync(configDir, { recursive: true });
|
|
174
|
+
const folders = [path.resolve(configDir)];
|
|
175
|
+
const snapshots = bridgeSnapshotRoot(env);
|
|
176
|
+
if (snapshots)
|
|
177
|
+
folders.push(path.resolve(snapshots));
|
|
178
|
+
const certDir = trimEnv(env.BAMBU_NETWORK_CERT_DIR);
|
|
179
|
+
if (certDir) {
|
|
180
|
+
// Made here, so Docker doesn't create it (as root, inside the VM) for the mount.
|
|
181
|
+
fs.mkdirSync(certDir, { recursive: true });
|
|
182
|
+
folders.push(path.resolve(certDir));
|
|
183
|
+
}
|
|
184
|
+
const unique = [...new Set(folders)].filter((dir, _i, all) => !all.some(other => other !== dir && dir.startsWith(other + path.sep)));
|
|
185
|
+
// The host is given these paths as written, so each is mounted there; the VM reads the folder it really is
|
|
186
|
+
// (a symlinked folder's link target need not be shared with the VM).
|
|
187
|
+
return unique.map(dir => {
|
|
188
|
+
let real = dir;
|
|
189
|
+
try {
|
|
190
|
+
real = fs.realpathSync(dir);
|
|
191
|
+
}
|
|
192
|
+
catch { /* created later by the host */ }
|
|
193
|
+
if (dir.includes(":") || real.includes(":"))
|
|
194
|
+
throw new Error(`The FULU container can't mount ${dir}: Docker volume paths can't contain ':'. Move that folder.`);
|
|
195
|
+
return `${real}:${dir}`;
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* docker run for the host: stdin/stdout are the bridge's framed protocol; the host is piped through cat (FULU #50).
|
|
200
|
+
* `--init` puts an init process first, so the SIGTERM docker forwards on stop reaches the host: a shell as the
|
|
201
|
+
* container's first process ignores it, and the container kept running after the server stopped. `name` lets stop
|
|
202
|
+
* remove the container even when the docker client was killed.
|
|
203
|
+
*/
|
|
204
|
+
export function bridgeContainerArgs(container, mounts, name) {
|
|
205
|
+
return [
|
|
206
|
+
...dockerHostArgs(container),
|
|
207
|
+
"run", "--rm", "-i", "--init", ...(name ? ["--name", name] : []),
|
|
208
|
+
"--platform", "linux/amd64", "--network", "host", "--log-driver=none", "--workdir", "/plugins",
|
|
209
|
+
...mounts.flatMap(mount => ["-v", `${mount}:rw`]),
|
|
210
|
+
...CONTAINER_ENV.flatMap(value => ["-e", value]),
|
|
211
|
+
...CONTAINER_PASSED_ENV.flatMap(name => ["-e", name]),
|
|
212
|
+
container.image,
|
|
213
|
+
"/bin/sh", "-c", "/fulu/pjarczak_bambu_linux_host | cat",
|
|
214
|
+
];
|
|
215
|
+
}
|
|
216
|
+
const runQuietly = (file, args, options) => new Promise((resolve, reject) => {
|
|
217
|
+
execFile(file, args, { env: options.env ?? process.env, timeout: options.timeout, maxBuffer: 1024 * 1024 }, (error, _stdout, stderr) => {
|
|
218
|
+
if (error)
|
|
219
|
+
reject(Object.assign(error, { stderr: String(stderr ?? "") }));
|
|
220
|
+
else
|
|
221
|
+
resolve();
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
/**
|
|
225
|
+
* Docker must answer before the host starts. When it doesn't and a Colima profile is configured, that profile is
|
|
226
|
+
* started without changing Docker's global context; a running profile is never stopped or restarted.
|
|
227
|
+
*/
|
|
228
|
+
export async function ensureBridgeContainerRuntime(container, timeouts = { infoMs: 20000, startMs: 600000 }) {
|
|
229
|
+
const info = () => runQuietly(container.docker, [...dockerHostArgs(container), "info"], { timeout: timeouts.infoMs });
|
|
230
|
+
try {
|
|
231
|
+
await info();
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
catch (error) {
|
|
235
|
+
if (!container.colima) {
|
|
236
|
+
throw new Error(`Docker didn't answer (${container.docker}${container.dockerHost ? ` --host ${container.dockerHost}` : ""} info), so the FULU container can't start. Start Docker, or set BAMBU_NETWORK_BRIDGE_COLIMA_PROFILE.`);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
const { executable, profile, home } = container.colima;
|
|
240
|
+
try {
|
|
241
|
+
await runQuietly(executable, ["start", "--profile", profile, "--activate=false"], { env: { ...process.env, ...(home ? { COLIMA_HOME: home } : {}) }, timeout: timeouts.startMs });
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
const detail = String(error.stderr ?? "").trim().split("\n").slice(-3).join(" ");
|
|
245
|
+
throw new Error(`Colima profile ${profile} didn't start for the FULU container${detail ? `: ${detail}` : "."}`);
|
|
246
|
+
}
|
|
247
|
+
try {
|
|
248
|
+
await info();
|
|
249
|
+
}
|
|
250
|
+
catch {
|
|
251
|
+
throw new Error(`Colima profile ${profile} started, but Docker still doesn't answer at ${container.dockerHost ?? "its default host"}.`);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Stops the bridge's own named container (SIGTERM to the host through --init, two seconds to exit), then removes
|
|
256
|
+
* it. A container that is already gone is fine.
|
|
257
|
+
*/
|
|
258
|
+
async function removeBridgeContainer(container, name) {
|
|
259
|
+
await runQuietly(container.docker, [...dockerHostArgs(container), "stop", "--time", "2", name], { timeout: 20000 }).catch(() => undefined);
|
|
260
|
+
await runQuietly(container.docker, [...dockerHostArgs(container), "rm", "-f", name], { timeout: 20000 }).catch(() => undefined);
|
|
261
|
+
}
|
|
91
262
|
function trimEnv(value) {
|
|
92
263
|
const trimmed = value?.trim();
|
|
93
264
|
return trimmed ? trimmed : undefined;
|
|
@@ -246,6 +417,8 @@ function assertBridgeOk(value, method) {
|
|
|
246
417
|
}
|
|
247
418
|
export class BambuNetworkBridge {
|
|
248
419
|
constructor() {
|
|
420
|
+
/** This bridge's own container, when the host runs in the Docker image: unique, so two servers never remove each other's. */
|
|
421
|
+
this.containerName = `bambu-fulu-host-${process.pid}-${randomUUID().slice(0, 8)}`;
|
|
249
422
|
this.stdoutBuffer = Buffer.alloc(0);
|
|
250
423
|
this.pending = new Map();
|
|
251
424
|
this.nextRequestId = 1;
|
|
@@ -259,18 +432,19 @@ export class BambuNetworkBridge {
|
|
|
259
432
|
*/
|
|
260
433
|
this.localSessionTail = Promise.resolve();
|
|
261
434
|
}
|
|
262
|
-
resolveBridgeCommand(override) {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
435
|
+
resolveBridgeCommand(override, configDir) {
|
|
436
|
+
const explicit = trimEnv(override) || explicitBridgeCommand();
|
|
437
|
+
if (explicit)
|
|
438
|
+
return explicit;
|
|
439
|
+
const container = bridgeContainer();
|
|
440
|
+
if (!container)
|
|
441
|
+
return "";
|
|
442
|
+
const mounts = bridgeContainerMounts(this.resolveConfigDir(configDir));
|
|
443
|
+
return [container.docker, ...bridgeContainerArgs(container, mounts, this.containerName)].map(shellQuote).join(" ");
|
|
268
444
|
}
|
|
269
445
|
resolveConfigDir(override) {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
trimEnv(process.env.PJARCZAK_BAMBU_PROBE_LOG_DIR) ||
|
|
273
|
-
defaultBambuNetworkConfigDir());
|
|
446
|
+
const dir = trimEnv(override);
|
|
447
|
+
return dir ? path.resolve(dir) : configuredBridgeConfigDir();
|
|
274
448
|
}
|
|
275
449
|
resolveCountryCode(override) {
|
|
276
450
|
return (trimEnv(override) ||
|
|
@@ -279,7 +453,7 @@ export class BambuNetworkBridge {
|
|
|
279
453
|
"US");
|
|
280
454
|
}
|
|
281
455
|
getStatus(options = {}) {
|
|
282
|
-
const command = this.resolveBridgeCommand(options.bridgeCommand);
|
|
456
|
+
const command = this.resolveBridgeCommand(options.bridgeCommand, options.configDir);
|
|
283
457
|
const configDir = this.resolveConfigDir(options.configDir);
|
|
284
458
|
const countryCode = this.resolveCountryCode(options.countryCode);
|
|
285
459
|
const runtime = { certDir: bambuNetworkCertDir(configDir) };
|
|
@@ -310,14 +484,14 @@ export class BambuNetworkBridge {
|
|
|
310
484
|
runtime,
|
|
311
485
|
note: command
|
|
312
486
|
? undefined
|
|
313
|
-
: "Set BAMBU_NETWORK_BRIDGE_COMMAND to the FULU OrcaSlicer-bambulab Linux host or macOS/WSL wrapper command.",
|
|
487
|
+
: "Set BAMBU_NETWORK_BRIDGE_COMMAND to the FULU OrcaSlicer-bambulab Linux host or macOS/WSL wrapper command, or BAMBU_NETWORK_BRIDGE_IMAGE to a Docker image holding FULU's Linux host.",
|
|
314
488
|
};
|
|
315
489
|
}
|
|
316
490
|
async request(method, payload = {}, options = {}) {
|
|
317
491
|
if (!method.trim()) {
|
|
318
492
|
throw new Error("BambuNetwork bridge method is required.");
|
|
319
493
|
}
|
|
320
|
-
await this.ensureStarted(options.bridgeCommand);
|
|
494
|
+
await this.ensureStarted(options.bridgeCommand, options.configDir);
|
|
321
495
|
await options.beforeDispatch?.(method);
|
|
322
496
|
const id = this.nextRequestId++;
|
|
323
497
|
const requestPayload = Buffer.from(JSON.stringify({ method, payload }), "utf8");
|
|
@@ -355,7 +529,7 @@ export class BambuNetworkBridge {
|
|
|
355
529
|
});
|
|
356
530
|
}
|
|
357
531
|
async ensureAgent(options = {}) {
|
|
358
|
-
const command = this.resolveBridgeCommand(options.bridgeCommand);
|
|
532
|
+
const command = this.resolveBridgeCommand(options.bridgeCommand, options.configDir);
|
|
359
533
|
const configDir = this.resolveConfigDir(options.configDir);
|
|
360
534
|
const countryCode = this.resolveCountryCode(options.countryCode);
|
|
361
535
|
const userInfo = trimEnv(options.userInfo) || trimEnv(process.env.BAMBU_NETWORK_USER_INFO) || "";
|
|
@@ -787,6 +961,8 @@ export class BambuNetworkBridge {
|
|
|
787
961
|
}
|
|
788
962
|
async stop() {
|
|
789
963
|
const child = this.child;
|
|
964
|
+
const running = this.runningContainer;
|
|
965
|
+
this.runningContainer = undefined;
|
|
790
966
|
this.child = undefined;
|
|
791
967
|
this.commandLine = undefined;
|
|
792
968
|
this.agentId = undefined;
|
|
@@ -798,26 +974,53 @@ export class BambuNetworkBridge {
|
|
|
798
974
|
pending.reject(new Error(`FULU BambuNetwork bridge stopped before ${pending.method} completed.`));
|
|
799
975
|
this.pending.delete(id);
|
|
800
976
|
}
|
|
977
|
+
// The container goes first: a shell that keeps the docker client as its own child holds the pipes open until
|
|
978
|
+
// the client exits, so waiting for the shell before removing the container could wait for ever.
|
|
979
|
+
if (running)
|
|
980
|
+
await removeBridgeContainer(running, this.containerName);
|
|
801
981
|
if (child && child.exitCode === null) {
|
|
802
982
|
await new Promise((resolve) => {
|
|
983
|
+
let settleTimer;
|
|
803
984
|
const forceTimer = setTimeout(() => {
|
|
804
985
|
if (child.exitCode === null) {
|
|
805
986
|
child.kill("SIGKILL");
|
|
806
987
|
}
|
|
988
|
+
// A descendant can still hold the pipes after the shell is killed: stop waiting for it, and let go of
|
|
989
|
+
// those pipes so nothing it writes reaches a bridge started after this one.
|
|
990
|
+
settleTimer = setTimeout(() => {
|
|
991
|
+
for (const stream of [child.stdin, child.stdout, child.stderr])
|
|
992
|
+
stream.destroy();
|
|
993
|
+
resolve();
|
|
994
|
+
}, 2000);
|
|
807
995
|
}, 2000);
|
|
808
996
|
child.once("close", () => {
|
|
809
997
|
clearTimeout(forceTimer);
|
|
998
|
+
if (settleTimer)
|
|
999
|
+
clearTimeout(settleTimer);
|
|
810
1000
|
resolve();
|
|
811
1001
|
});
|
|
812
1002
|
child.kill("SIGTERM");
|
|
813
1003
|
});
|
|
814
1004
|
}
|
|
815
1005
|
}
|
|
816
|
-
async ensureStarted(commandOverride) {
|
|
817
|
-
|
|
1006
|
+
async ensureStarted(commandOverride, configDir) {
|
|
1007
|
+
while (this.starting)
|
|
1008
|
+
await this.starting.catch(() => undefined);
|
|
1009
|
+
const start = this.startBridge(commandOverride, configDir);
|
|
1010
|
+
this.starting = start;
|
|
1011
|
+
try {
|
|
1012
|
+
await start;
|
|
1013
|
+
}
|
|
1014
|
+
finally {
|
|
1015
|
+
if (this.starting === start)
|
|
1016
|
+
this.starting = undefined;
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
async startBridge(commandOverride, configDir) {
|
|
1020
|
+
const command = this.resolveBridgeCommand(commandOverride, configDir);
|
|
818
1021
|
if (!command) {
|
|
819
|
-
throw new Error("BAMBU_NETWORK_BRIDGE_COMMAND is required for FULU BambuNetwork support. " +
|
|
820
|
-
"Point
|
|
1022
|
+
throw new Error("BAMBU_NETWORK_BRIDGE_COMMAND or BAMBU_NETWORK_BRIDGE_IMAGE is required for FULU BambuNetwork support. " +
|
|
1023
|
+
"Point the command at pjarczak_bambu_linux_host or the OrcaSlicer-bambulab macOS/WSL wrapper, or name a Docker image holding FULU's Linux host.");
|
|
821
1024
|
}
|
|
822
1025
|
if (this.child && this.child.exitCode === null && this.commandLine === command) {
|
|
823
1026
|
return;
|
|
@@ -825,6 +1028,13 @@ export class BambuNetworkBridge {
|
|
|
825
1028
|
if (this.child && this.commandLine !== command) {
|
|
826
1029
|
await this.stop();
|
|
827
1030
|
}
|
|
1031
|
+
const container = trimEnv(commandOverride) || explicitBridgeCommand() ? undefined : bridgeContainer();
|
|
1032
|
+
if (container) {
|
|
1033
|
+
await ensureBridgeContainerRuntime(container);
|
|
1034
|
+
// The docker client can exit (a dropped socket) while its container keeps running under this bridge's name.
|
|
1035
|
+
await removeBridgeContainer(container, this.containerName);
|
|
1036
|
+
this.runningContainer = container;
|
|
1037
|
+
}
|
|
828
1038
|
const shell = process.platform === "win32" ? process.env.ComSpec || "cmd.exe" : "/bin/sh";
|
|
829
1039
|
const shellArgs = process.platform === "win32"
|
|
830
1040
|
? ["/d", "/s", "/c", command]
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface ModelPlate {
|
|
2
|
+
/** Zero-based, as inspect_print_file's plate_index; slice_stl's slice_plate is index + 1. */
|
|
3
|
+
index: number;
|
|
4
|
+
name: string;
|
|
5
|
+
objects: string[];
|
|
6
|
+
/** The file already holds this plate's G-code. */
|
|
7
|
+
sliced: boolean;
|
|
8
|
+
/** The plate's own preview from the file (Metadata/plate_N.png), when asked for and present. */
|
|
9
|
+
thumbnail_png_base64?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface ModelPlates {
|
|
12
|
+
plate_count: number;
|
|
13
|
+
plates: ModelPlate[];
|
|
14
|
+
title?: string;
|
|
15
|
+
designer?: string;
|
|
16
|
+
licence?: string;
|
|
17
|
+
}
|
|
18
|
+
export declare function listModelPlates(filePath: string, options?: {
|
|
19
|
+
thumbnails?: boolean;
|
|
20
|
+
}): Promise<ModelPlates>;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* list_3mf_plates: a Bambu Studio / OrcaSlicer project's plates as its designer laid them out, before slicing
|
|
3
|
+
* (Counterpart PRD §5.5 "Plates of an imported 3MF", §8.0 item 9). Bambu Studio shows a downloaded kit as its
|
|
4
|
+
* plates straight away; clients show the same from here instead of parsing the project files themselves.
|
|
5
|
+
*
|
|
6
|
+
* Plates come from Metadata/model_settings.config (`<plate>` with `plater_id`, `plater_name` and the objects it
|
|
7
|
+
* holds) and, for a sliced file, from Metadata/plate_N.json and plate_N.gcode. A plain 3MF with no plate metadata
|
|
8
|
+
* is one plate. Only bounded metadata is read; nothing is extracted or run.
|
|
9
|
+
*/
|
|
10
|
+
import { readSafe3mfArchive } from "../safety/archive.js";
|
|
11
|
+
/** For the settings and plate files; the model file can be large and is only scanned. */
|
|
12
|
+
const MAX_METADATA_BYTES = 8 * 1024 * 1024;
|
|
13
|
+
const MAX_THUMBNAIL_BYTES = 2 * 1024 * 1024;
|
|
14
|
+
const MAX_PLATES = 1000;
|
|
15
|
+
const words = (value) => value
|
|
16
|
+
.replace(/&(?:amp|lt|gt|quot|apos);/g, entity => ({ "&": "&", "<": "<", ">": ">", """: "\"", "'": "'" }[entity]))
|
|
17
|
+
.replace(/[\u0000-\u001f\u007f]/g, " ").trim().slice(0, 256);
|
|
18
|
+
export async function listModelPlates(filePath, options = {}) {
|
|
19
|
+
const { zip } = await readSafe3mfArchive(filePath);
|
|
20
|
+
const text = async (name) => {
|
|
21
|
+
const file = zip.file(name);
|
|
22
|
+
if (!file)
|
|
23
|
+
return "";
|
|
24
|
+
const bytes = await file.async("nodebuffer");
|
|
25
|
+
if (bytes.length > MAX_METADATA_BYTES)
|
|
26
|
+
throw new Error(`${name} is too large to read.`);
|
|
27
|
+
return bytes.toString("utf8");
|
|
28
|
+
};
|
|
29
|
+
// The model file can hold the whole mesh (a plain 3MF): scanned as bytes, never made one string.
|
|
30
|
+
const modelBytes = await zip.file("3D/3dmodel.model")?.async("nodebuffer");
|
|
31
|
+
const head = modelBytes?.subarray(0, 1024 * 1024).toString("utf8") ?? "";
|
|
32
|
+
if (!/<(?:\w+:)?model\b/.test(head))
|
|
33
|
+
throw new Error("That file is not a 3MF model.");
|
|
34
|
+
// An element named `tag`, plain or with a namespace prefix (<item …>, <p:item …>).
|
|
35
|
+
const has = (tag) => !!modelBytes && [" ", ">", "/", "\n", "\r", "\t"].some(end => modelBytes.includes(`<${tag}${end}`) || modelBytes.includes(`:${tag}${end}`));
|
|
36
|
+
const result = { plate_count: 0, plates: [] };
|
|
37
|
+
const metadata = new Map([...head.matchAll(/<metadata\s+name="([^"]+)"[^>]*>([^<]*)<\/metadata>/g)].map(match => [match[1], words(match[2])]));
|
|
38
|
+
if (metadata.get("Title"))
|
|
39
|
+
result.title = metadata.get("Title");
|
|
40
|
+
if (metadata.get("Designer"))
|
|
41
|
+
result.designer = metadata.get("Designer");
|
|
42
|
+
if (metadata.get("License"))
|
|
43
|
+
result.licence = metadata.get("License");
|
|
44
|
+
const settings = await text("Metadata/model_settings.config");
|
|
45
|
+
const objects = new Map([...settings.matchAll(/<object\b[^>]*\bid="(\d+)"[^>]*>([\s\S]*?)<\/object>/g)].map(match => {
|
|
46
|
+
const name = /<metadata\s+key="name"\s+value="([^"]*)"/.exec(match[2]);
|
|
47
|
+
return [match[1], words(name?.[1] ?? "")];
|
|
48
|
+
}));
|
|
49
|
+
const plates = new Map();
|
|
50
|
+
// A plate number out of range is skipped, as a slicer would not show it.
|
|
51
|
+
const valid = (number) => Number.isSafeInteger(number) && number >= 1 && number <= MAX_PLATES;
|
|
52
|
+
for (const match of settings.matchAll(/<plate\b[^>]*>([\s\S]*?)<\/plate>/g)) {
|
|
53
|
+
const id = /<metadata\s+key="plater_id"\s+value="(\d+)"/.exec(match[1]);
|
|
54
|
+
if (!id)
|
|
55
|
+
continue;
|
|
56
|
+
const number = Number(id[1]);
|
|
57
|
+
if (!valid(number))
|
|
58
|
+
continue;
|
|
59
|
+
const name = /<metadata\s+key="plater_name"\s+value="([^"]*)"/.exec(match[1]);
|
|
60
|
+
plates.set(number, { index: number - 1, name: words(name?.[1] ?? "") || `Plate ${number}`, sliced: false,
|
|
61
|
+
objects: [...match[1].matchAll(/<metadata\s+key="object_id"\s+value="(\d+)"/g)].flatMap(item => objects.get(item[1]) ? [objects.get(item[1])] : []) });
|
|
62
|
+
}
|
|
63
|
+
for (const name of Object.keys(zip.files)) {
|
|
64
|
+
const match = /^Metadata\/plate_([1-9]\d*)\.(json|gcode)$/.exec(name);
|
|
65
|
+
if (!match)
|
|
66
|
+
continue;
|
|
67
|
+
const number = Number(match[1]);
|
|
68
|
+
if (!valid(number))
|
|
69
|
+
continue;
|
|
70
|
+
const plate = plates.get(number) ?? { index: number - 1, name: `Plate ${number}`, objects: [], sliced: false };
|
|
71
|
+
if (match[2] === "gcode")
|
|
72
|
+
plate.sliced = true;
|
|
73
|
+
else {
|
|
74
|
+
try {
|
|
75
|
+
const value = JSON.parse(await text(name));
|
|
76
|
+
if (Array.isArray(value.bbox_objects))
|
|
77
|
+
plate.objects = value.bbox_objects
|
|
78
|
+
.flatMap(object => typeof object.name === "string" && object.name !== "wipe_tower" ? [words(object.name)] : []).slice(0, 200);
|
|
79
|
+
}
|
|
80
|
+
catch { /* the plate keeps the objects model_settings lists */ }
|
|
81
|
+
}
|
|
82
|
+
plates.set(number, plate);
|
|
83
|
+
}
|
|
84
|
+
result.plates = [...plates.values()].sort((a, b) => a.index - b.index);
|
|
85
|
+
// A standard single-build 3MF has no Bambu plate metadata: it is one plate.
|
|
86
|
+
if (!result.plates.length)
|
|
87
|
+
result.plates.push({ index: 0, name: "Plate 1", objects: [], sliced: false });
|
|
88
|
+
const geometry = has("item") && (has("mesh") || has("component"));
|
|
89
|
+
if (!geometry) {
|
|
90
|
+
// A sliced-only file (previews and G-code, no model): only its sliced plates can print.
|
|
91
|
+
result.plates = result.plates.filter(plate => plate.sliced);
|
|
92
|
+
if (!result.plates.length)
|
|
93
|
+
throw new Error("This 3MF has previews and settings but no model or printable plate.");
|
|
94
|
+
}
|
|
95
|
+
if (options.thumbnails)
|
|
96
|
+
for (const plate of result.plates) {
|
|
97
|
+
const file = zip.file(`Metadata/plate_${plate.index + 1}.png`);
|
|
98
|
+
if (!file)
|
|
99
|
+
continue;
|
|
100
|
+
const bytes = await file.async("nodebuffer");
|
|
101
|
+
if (bytes.length <= MAX_THUMBNAIL_BYTES && bytes.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])))
|
|
102
|
+
plate.thumbnail_png_base64 = bytes.toString("base64");
|
|
103
|
+
}
|
|
104
|
+
result.plate_count = result.plates.length;
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
@@ -126,7 +126,7 @@ export declare const SETUP_FIELDS_OUTPUT_SCHEMA: {
|
|
|
126
126
|
properties: {
|
|
127
127
|
name: {
|
|
128
128
|
type: string;
|
|
129
|
-
enum: ("model" | "
|
|
129
|
+
enum: ("model" | "host" | "slicer" | "port" | "profiles" | "serial" | "secret" | "slicer-path")[];
|
|
130
130
|
};
|
|
131
131
|
env: {
|
|
132
132
|
type: string;
|
|
@@ -221,7 +221,7 @@ export declare const SETUP_FIELDS_OUTPUT_SCHEMA: {
|
|
|
221
221
|
properties: {
|
|
222
222
|
name: {
|
|
223
223
|
type: string;
|
|
224
|
-
enum: ("model" | "
|
|
224
|
+
enum: ("model" | "host" | "slicer" | "port" | "profiles" | "serial" | "secret" | "slicer-path")[];
|
|
225
225
|
};
|
|
226
226
|
env: {
|
|
227
227
|
type: string;
|
|
@@ -20,12 +20,17 @@ export interface SlicedFileFacts {
|
|
|
20
20
|
/** The facts of a sliced 3MF or G-code file for one zero-based plate; what can't be read is left out, never an error. */
|
|
21
21
|
export declare function slicedFileFacts(filePath: string, plateIndex?: number): Promise<SlicedFileFacts>;
|
|
22
22
|
/**
|
|
23
|
-
* Bambu Studio's (or OrcaSlicer's) verdict on a slice, from its result.json: whether it failed,
|
|
24
|
-
* (
|
|
23
|
+
* Bambu Studio's (or OrcaSlicer's) verdict on a slice, from its result.json: whether it failed, what a person is
|
|
24
|
+
* told (`message`: a plain sentence for a refusal, src/slicer/slicer-messages.ts; the plates' warnings for a slice
|
|
25
|
+
* that worked), the slicer's own words for the log (`slicer_message`: the error and every plate's warning,
|
|
26
|
+
* verbatim), and how many plates it sliced.
|
|
25
27
|
*/
|
|
26
28
|
export interface SlicerReport {
|
|
27
29
|
failed: boolean;
|
|
28
30
|
message?: string;
|
|
31
|
+
slicer_message?: string;
|
|
29
32
|
plate_count?: number;
|
|
30
33
|
}
|
|
34
|
+
/** A refusal's report: the plain sentence, and the slicer's words kept beside it. */
|
|
35
|
+
export declare function refusalReport(words: string): SlicerReport;
|
|
31
36
|
export declare function slicerReport(result: unknown): SlicerReport | undefined;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import { loadSafe3mfArchive, readBoundedPrintFile } from "../safety/archive.js";
|
|
3
|
+
import { plainSlicerError, UNKNOWN_SLICER_ERROR } from "../slicer/slicer-messages.js";
|
|
3
4
|
const number = (value) => {
|
|
4
5
|
const parsed = typeof value === "number" ? value : typeof value === "string" ? Number.parseFloat(value) : NaN;
|
|
5
6
|
return Number.isFinite(parsed) && parsed >= 0 ? parsed : undefined;
|
|
@@ -92,6 +93,11 @@ async function readFacts(filePath, plateIndex) {
|
|
|
92
93
|
return { ...(plates.size ? { plate_count: plates.size } : {}), ...(estimate ? { estimate } : {}), settings };
|
|
93
94
|
}
|
|
94
95
|
const clean = (text) => text.replace(/\r\n?/g, "\n").replace(/[\x00-\x09\x0b-\x1f\x7f]/g, " ").trim();
|
|
96
|
+
/** A refusal's report: the plain sentence, and the slicer's words kept beside it. */
|
|
97
|
+
export function refusalReport(words) {
|
|
98
|
+
const raw = clean(words).slice(0, 2000);
|
|
99
|
+
return { failed: true, message: plainSlicerError(raw) ?? UNKNOWN_SLICER_ERROR, ...(raw ? { slicer_message: raw } : {}) };
|
|
100
|
+
}
|
|
95
101
|
export function slicerReport(result) {
|
|
96
102
|
if (!result || typeof result !== "object" || Array.isArray(result))
|
|
97
103
|
return undefined;
|
|
@@ -105,6 +111,6 @@ export function slicerReport(result) {
|
|
|
105
111
|
const error = typeof value.error_string === "string" ? clean(value.error_string) : "";
|
|
106
112
|
const words = [...new Set([...(failed && error ? [error] : []), ...warnings])].join("\n").slice(0, 2000);
|
|
107
113
|
if (failed)
|
|
108
|
-
return
|
|
109
|
-
return { failed, ...(words ? { message: words } : {}), ...(plateCount ? { plate_count: plateCount } : {}) };
|
|
114
|
+
return refusalReport(words || `return code ${String(value.return_code)}`);
|
|
115
|
+
return { failed, ...(words ? { message: words, slicer_message: words } : {}), ...(plateCount ? { plate_count: plateCount } : {}) };
|
|
110
116
|
}
|