bambu-printer-mcp 1.1.92 → 1.1.94

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.
Files changed (43) hide show
  1. package/README.md +6 -2
  2. package/dist/3mf_parser.js +3 -2
  3. package/dist/cad/contracts.js +9 -0
  4. package/dist/cad-engine.d.ts +5 -0
  5. package/dist/cad-engine.js +34 -0
  6. package/dist/counterpart/flight-tools.d.ts +2 -2
  7. package/dist/counterpart/info.d.ts +2 -0
  8. package/dist/counterpart/info.js +6 -1
  9. package/dist/counterpart/make-3mf.d.ts +10 -0
  10. package/dist/counterpart/make-3mf.js +472 -65
  11. package/dist/counterpart/model-plates.d.ts +8 -1
  12. package/dist/counterpart/model-plates.js +9 -4
  13. package/dist/counterpart/plate-filaments.d.ts +19 -0
  14. package/dist/counterpart/plate-filaments.js +72 -10
  15. package/dist/counterpart/remote.js +1 -0
  16. package/dist/counterpart/schemas.d.ts +7 -2
  17. package/dist/counterpart/schemas.js +1 -0
  18. package/dist/counterpart/self-check.d.ts +79 -0
  19. package/dist/counterpart/self-check.js +163 -0
  20. package/dist/index.js +12 -2
  21. package/dist/printers/bambu.js +27 -19
  22. package/dist/printers/report-merge.d.ts +12 -0
  23. package/dist/printers/report-merge.js +46 -0
  24. package/dist/stl/stl-manipulator.js +79 -20
  25. package/dist/tool-annotations.js +1 -0
  26. package/package.json +6 -1
  27. package/src/3mf_parser.ts +3 -2
  28. package/src/cad/contracts.ts +10 -0
  29. package/src/cad/project3mf.py +426 -0
  30. package/src/cad/worker.py +6 -2
  31. package/src/cad-engine.ts +31 -0
  32. package/src/counterpart/info.ts +7 -1
  33. package/src/counterpart/make-3mf.ts +446 -61
  34. package/src/counterpart/model-plates.ts +17 -5
  35. package/src/counterpart/plate-filaments.ts +75 -7
  36. package/src/counterpart/remote.ts +1 -0
  37. package/src/counterpart/schemas.ts +3 -1
  38. package/src/counterpart/self-check.ts +164 -0
  39. package/src/index.ts +13 -2
  40. package/src/printers/bambu.ts +27 -19
  41. package/src/printers/report-merge.ts +48 -0
  42. package/src/stl/stl-manipulator.ts +75 -14
  43. package/src/tool-annotations.ts +1 -0
@@ -10,7 +10,7 @@ import path from 'path';
10
10
  import { promisify } from 'util';
11
11
  import { EventEmitter } from 'events';
12
12
  import * as crypto from 'crypto';
13
- import { execFile } from 'child_process';
13
+ import { spawn, type ChildProcess } from 'child_process';
14
14
  import { keepSlice, restoreSlice, sliceCacheKey } from '../slicer/slice-cache.js';
15
15
  import { listModelPlates } from '../counterpart/model-plates.js';
16
16
  import { flattenForCli, detectProfilesRoot, resolveBblMachineProfile } from '../slicer/profile-flatten.js';
@@ -283,6 +283,33 @@ function slicerResultText(result: SlicerResult): string {
283
283
  ].filter((text): text is string => typeof text === "string" && text.trim() !== "").map(text => text.trim()).join("\n");
284
284
  }
285
285
 
286
+ /** What execFile hands its callback when the slicer fails: its message carries the command and stderr. */
287
+ type SlicerExit = Error & { code?: number | string | null; signal?: NodeJS.Signals | null; killed?: boolean; cmd?: string };
288
+ /** The output kept from each stream, as execFile's default maxBuffer; the rest is dropped, not failed. */
289
+ const SLICER_OUTPUT_LIMIT = 1024 * 1024;
290
+
291
+ /**
292
+ * execFile for the slicer, with detached passed through (execFile drops it): on POSIX the slicer then leads its own
293
+ * process group, so a cancel can stop the helpers it starts too. The callback gets what execFile's would.
294
+ */
295
+ function execSlicer(file: string, args: string[], env: NodeJS.ProcessEnv | undefined, detached: boolean,
296
+ callback: (error: SlicerExit | null, stdout: string, stderr: string) => void): ChildProcess {
297
+ // stdin is ignored, as execFile ends it: no slicer reads it.
298
+ const child = spawn(file, args, { ...(env ? { env } : {}), detached, stdio: ["ignore", "pipe", "pipe"] });
299
+ let stdout = "", stderr = "", settled = false;
300
+ const keep = (text: string, chunk: Buffer) => text.length >= SLICER_OUTPUT_LIMIT ? text : (text + chunk.toString("utf8")).slice(0, SLICER_OUTPUT_LIMIT);
301
+ child.stdout?.on("data", (chunk: Buffer) => { stdout = keep(stdout, chunk); });
302
+ child.stderr?.on("data", (chunk: Buffer) => { stderr = keep(stderr, chunk); });
303
+ const finish = (error: SlicerExit | null) => { if (!settled) { settled = true; callback(error, stdout, stderr); } };
304
+ child.on("error", error => finish(Object.assign(error, { cmd: [file, ...args].join(" ") })));
305
+ child.on("close", (code, signal) => {
306
+ if (code === 0 && !signal) return finish(null);
307
+ const cmd = [file, ...args].join(" ");
308
+ finish(Object.assign(new Error(`Command failed: ${cmd}\n${stderr}`), { code, signal, killed: child.killed, cmd }));
309
+ });
310
+ return child;
311
+ }
312
+
286
313
  function describeSlicerFailure(
287
314
  error: { message: string; code?: unknown; signal?: unknown },
288
315
  stdout: string,
@@ -2074,10 +2101,17 @@ export class STLManipulator extends EventEmitter {
2074
2101
  const launch = await prepareSlicerLaunch(slicerType, this.tempDir);
2075
2102
  const stallSeconds = launch.stallSeconds;
2076
2103
  let stalled = false;
2104
+ let cancelled = false;
2077
2105
  // Execute the slicer
2078
2106
  try { await new Promise<void>((resolve, reject) => {
2079
- const process = execFile(slicerPath, args, launch.env ? { env: launch.env } : {}, (error, stdout, stderr) => {
2080
- if (stalled && error) {
2107
+ // POSIX: the slicer leads its own process group, so stopping it also stops the helpers it starts.
2108
+ const group = globalThis.process.platform !== 'win32';
2109
+ const process = execSlicer(slicerPath, args, launch.env, group, (error, stdout, stderr) => {
2110
+ if (cancelled) {
2111
+ // Settled once the slicer and every process sharing its output have gone, so its folder is removed after
2112
+ // nothing can write to it.
2113
+ reject(new Error("Slicing operation cancelled"));
2114
+ } else if (stalled && error) {
2081
2115
  reject(stalledSlicerError(slicerType, stallSeconds));
2082
2116
  } else if (error) {
2083
2117
  console.error(`Slicer Error: ${error.message}`);
@@ -2097,26 +2131,49 @@ export class STLManipulator extends EventEmitter {
2097
2131
  // process.stdout?.on('data', (data) => { console.error(`Slicer stdout: ${data}`); });
2098
2132
  // process.stderr?.on('data', (data) => { console.error(`Slicer stderr: ${data}`); });
2099
2133
 
2100
- // Check for cancellation periodically
2134
+ // Stops the slicer and, on POSIX, its whole process group (a group already gone is not an error).
2135
+ const stop = (signal: NodeJS.Signals) => {
2136
+ try {
2137
+ if (group && process.pid !== undefined) globalThis.process.kill(-process.pid, signal);
2138
+ else process.kill(signal);
2139
+ } catch (killError) {
2140
+ if ((killError as NodeJS.ErrnoException).code !== 'ESRCH') console.error(`Could not send ${signal} to the slicer:`, killError);
2141
+ }
2142
+ };
2143
+ // Check for cancellation periodically: the slicer is stopped, and the call settles when it has gone.
2144
+ let forceKill: NodeJS.Timeout | undefined;
2145
+ let abandon: NodeJS.Timeout | undefined;
2101
2146
  const checkCancel = setInterval(() => {
2102
2147
  if (!this.activeOperations.get(operationId)) {
2103
2148
  clearInterval(checkCancel);
2104
- try {
2105
- process.kill(); // Attempt to kill the slicer process
2149
+ cancelled = true;
2150
+ stop('SIGTERM');
2151
+ // A slicer or helper that ignores SIGTERM is killed outright rather than waited on. A helper outside the
2152
+ // group (daemonized, or any helper on Windows) can still hold the output pipes, so 'close' never comes:
2153
+ // a second later the pipes are let go and the cancel settles.
2154
+ forceKill = setTimeout(() => {
2155
+ stop('SIGKILL');
2156
+ abandon = setTimeout(() => {
2157
+ process.stdout?.destroy();
2158
+ process.stderr?.destroy();
2106
2159
  reject(new Error("Slicing operation cancelled"));
2107
- } catch (killError) {
2108
- console.error("Error attempting to kill slicer process:", killError);
2109
- reject(new Error("Slicing operation cancelled, but failed to kill process."));
2110
- }
2160
+ }, 1000);
2161
+ }, 5000);
2111
2162
  }
2112
2163
  }, 500);
2113
2164
 
2114
- process.on('exit', () => clearInterval(checkCancel));
2115
- process.on('error', () => clearInterval(checkCancel)); // Ensure interval cleared on process error too
2165
+ process.on('exit', () => {
2166
+ // Exit can precede a client cancel while a helper retains output pipes; polling runs until close.
2167
+ // A helper that outlived the slicer still holds its output; finish it so the call can settle.
2168
+ if (cancelled && group) stop('SIGKILL');
2169
+ });
2170
+ // The deadline runs until every process sharing the slicer's output has let go, not only the slicer.
2171
+ process.on('close', () => { clearInterval(checkCancel); clearTimeout(forceKill); clearTimeout(abandon); });
2172
+ process.on('error', () => { clearInterval(checkCancel); clearTimeout(forceKill); clearTimeout(abandon); });
2116
2173
  watchForStall(process, stallSeconds, () => {
2117
2174
  stalled = true;
2118
2175
  console.error(`${slicerType} used no CPU and wrote nothing for ${stallSeconds} s; stopping it as waiting on a dialog.`);
2119
- try { process.kill('SIGKILL'); } catch { /* its exit is reported through the callback */ }
2176
+ stop('SIGKILL');
2120
2177
  });
2121
2178
 
2122
2179
  }); } finally { await launch.cleanup(); }
@@ -2156,7 +2213,11 @@ export class STLManipulator extends EventEmitter {
2156
2213
  } catch (error) {
2157
2214
  console.error(`Slicing failed for ${stlFilePath}:`, error);
2158
2215
  // Nothing in a failed or cancelled slice's folder is handed back (its result.json is already in the error).
2159
- if (sliceDir) await fs.promises.rm(sliceDir, { recursive: true, force: true }).catch(() => undefined);
2216
+ if (sliceDir) {
2217
+ await fs.promises.rm(sliceDir, { recursive: true, force: true }).then(
2218
+ () => console.error(`Removed the folder of the failed or cancelled slice: ${sliceDir}`),
2219
+ removeError => console.error(`Could not remove the folder of the failed or cancelled slice ${sliceDir}:`, removeError));
2220
+ }
2160
2221
  this.emit('operationError', {
2161
2222
  operationId,
2162
2223
  type: 'slice',
@@ -67,6 +67,7 @@ export const TOOL_ANNOTATIONS: Record<string, ToolAnnotations> = {
67
67
  list_templates: READ_LOCAL,
68
68
  get_slice_settings: LOCAL_OUTPUT, // a 3MF's settings are extracted to a fixed file in the temporary directory
69
69
  counterpart_printer_info: READ_LOCAL, // configuration and capabilities; sends nothing to the printer
70
+ counterpart_self_check: READ_LOCAL, // loads this install's own modules; sends nothing to the printer
70
71
  counterpart_setup_fields: READ_LOCAL, // the settings this server reads; no configured values
71
72
 
72
73
  // Local outputs in the server's temporary directory