bambu-printer-mcp 1.1.49 → 1.1.51

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.
@@ -0,0 +1,201 @@
1
+ /**
2
+ * The print options Bambu Studio's Send dialog sends, resolved the way it
3
+ * resolves them, so every print route sends what Bambu's own Print button
4
+ * would. Source: Bambu Studio v02.08.02.61 (commit 926a7192574b), the version
5
+ * the hidden-HMS and stage tables are pinned to:
6
+ *
7
+ * - SelectMachine.cpp:3344-3356 `set_print_config(bed_type,
8
+ * bed_leveling == "on", flow_cali == "on", false, timelapse, true,
9
+ * ext_change_assist, bed_leveling.getValueInt(), flow_cali.getValueInt(),
10
+ * nozzle_offset_cali.getValueInt(), pa_value_switch ? 0 : 1)`. Vibration
11
+ * calibration is always false and layer inspection always true; the
12
+ * parameter order is PrintJob.hpp:98-116.
13
+ * - SelectMachine.cpp:7318-7329 `getValueInt`: off 0, on 1, auto 2.
14
+ * - SelectMachine.cpp:1117-1123 `update_select_layout`: update_option_opts
15
+ * (which choices each option offers on this printer) runs before
16
+ * load_option_vals.
17
+ * - SelectMachine.cpp:2993-3027 `load_option_vals`: the value saved in
18
+ * BambuStudio.conf under the printer type, if the option offers it; else
19
+ * "auto" if offered; else "on". Dual-nozzle printers then set nozzle offset
20
+ * calibration to "auto" for a two-nozzle job and, without a filament
21
+ * switcher, "off" for a one-nozzle job.
22
+ * - SelectMachine.cpp:2586-2615 `update_option_opts`: bed leveling offers
23
+ * auto/on/off when the printer's support_bed_leveling is 2 and on/off when it
24
+ * is 1; with 0 it is hidden and keeps the auto/on/off it was built with
25
+ * (SelectMachine.cpp:676-691), as does flow calibration when the printer has
26
+ * no PA calibration. DeviceManager.cpp:1100/4441 give P-series printers
27
+ * (series_p1p: P1P, P1S) no PA calibration, so their flow calibration is
28
+ * hidden and resolves to "auto". Nozzle offset calibration always offers auto.
29
+ * - SelectMachine.cpp:705-707: the PA value switch starts on and is not saved,
30
+ * so extruder_cali_manual_mode is 0.
31
+ *
32
+ * Approximations, outside the P1P/P1S: flow calibration on other printers is
33
+ * shown when the printer's push status reports PA calibration (assumed here),
34
+ * and offers auto when support_auto_flow_calibration is true and the mapped
35
+ * filaments allow it (can_support_pa_auto_cali, assumed true). A filament
36
+ * switcher on a dual-nozzle printer is assumed absent.
37
+ */
38
+ import fs from "node:fs";
39
+ import os from "node:os";
40
+ import path from "node:path";
41
+
42
+ export type StudioOptionValue = "off" | "on" | "auto";
43
+ type Offers = readonly StudioOptionValue[];
44
+ const AUTO_ON_OFF: Offers = ["auto", "on", "off"];
45
+ const ON_OFF: Offers = ["on", "off"];
46
+
47
+ /**
48
+ * Studio's printer types (resources/printers/<type>.json at the pinned commit:
49
+ * model_id/printer_type, printer_series, support_bed_leveling,
50
+ * support_auto_flow_calibration), keyed by this server's model names.
51
+ */
52
+ const STUDIO_PRINTERS: Record<string, { types: string[]; pSeries: boolean; bedLeveling: 0 | 1 | 2; autoFlow: boolean; dualNozzle: boolean }> = {
53
+ p1p: { types: ["C11"], pSeries: true, bedLeveling: 1, autoFlow: false, dualNozzle: false },
54
+ p1s: { types: ["C12"], pSeries: true, bedLeveling: 1, autoFlow: false, dualNozzle: false },
55
+ x1c: { types: ["3DPrinter-X1-Carbon"], pSeries: false, bedLeveling: 1, autoFlow: false, dualNozzle: false },
56
+ x1: { types: ["3DPrinter-X1"], pSeries: false, bedLeveling: 1, autoFlow: false, dualNozzle: false },
57
+ x1e: { types: ["C13"], pSeries: false, bedLeveling: 1, autoFlow: false, dualNozzle: false },
58
+ a1mini: { types: ["N1"], pSeries: false, bedLeveling: 1, autoFlow: false, dualNozzle: false },
59
+ a1: { types: ["N2S"], pSeries: false, bedLeveling: 1, autoFlow: false, dualNozzle: false },
60
+ p2s: { types: ["N7"], pSeries: false, bedLeveling: 2, autoFlow: true, dualNozzle: false },
61
+ x2d: { types: ["N6"], pSeries: false, bedLeveling: 2, autoFlow: true, dualNozzle: true },
62
+ h2s: { types: ["O1S"], pSeries: false, bedLeveling: 2, autoFlow: true, dualNozzle: false },
63
+ h2d: { types: ["O1D"], pSeries: false, bedLeveling: 2, autoFlow: true, dualNozzle: true },
64
+ h2dpro: { types: ["O1E"], pSeries: false, bedLeveling: 2, autoFlow: true, dualNozzle: true },
65
+ h2c: { types: ["O1C", "O1C2"], pSeries: false, bedLeveling: 2, autoFlow: true, dualNozzle: true },
66
+ };
67
+
68
+ export type StudioOptionSource = "caller" | "studio-config" | "studio-default";
69
+
70
+ /** Explicit caller choices; each one wins over Studio's resolution. */
71
+ export type CallerPrintOptions = {
72
+ bedLeveling?: boolean;
73
+ flowCalibration?: boolean;
74
+ vibrationCalibration?: boolean;
75
+ layerInspect?: boolean;
76
+ /** 0 off, 1 on, 2 auto, as Studio's getValueInt. */
77
+ autoBedLeveling?: number;
78
+ autoFlowCali?: number;
79
+ autoOffsetCali?: number;
80
+ extruderCaliManualMode?: number;
81
+ };
82
+
83
+ export type StudioPrintOptions = {
84
+ printerType?: string;
85
+ values: { bed_leveling: StudioOptionValue; flow_cali: StudioOptionValue; nozzle_offset_cali: StudioOptionValue };
86
+ sources: { bed_leveling: StudioOptionSource; flow_cali: StudioOptionSource; nozzle_offset_cali: StudioOptionSource };
87
+ task_bed_leveling: boolean;
88
+ task_flow_cali: boolean;
89
+ task_vibration_cali: boolean;
90
+ task_layer_inspect: boolean;
91
+ auto_bed_leveling: number;
92
+ auto_flow_cali: number;
93
+ auto_offset_cali: number;
94
+ extruder_cali_manual_mode: number;
95
+ };
96
+
97
+ /** Where Studio keeps BambuStudio.conf; BAMBU_STUDIO_CONFIG overrides it (empty: none). */
98
+ export function studioConfigPath(): string | undefined {
99
+ const override = process.env.BAMBU_STUDIO_CONFIG;
100
+ if (override !== undefined) return override.trim() || undefined;
101
+ if (process.platform === "darwin") return path.join(os.homedir(), "Library", "Application Support", "BambuStudio", "BambuStudio.conf");
102
+ if (process.platform === "win32") return process.env.APPDATA ? path.join(process.env.APPDATA, "BambuStudio", "BambuStudio.conf") : undefined;
103
+ return path.join(process.env.XDG_CONFIG_HOME || path.join(os.homedir(), ".config"), "BambuStudio", "BambuStudio.conf");
104
+ }
105
+
106
+ /**
107
+ * The Send dialog options Studio saved for a printer type, and nothing else:
108
+ * BambuStudio.conf also holds access codes, so only that one section is kept,
109
+ * and a parse failure is reported without any of the file's text.
110
+ */
111
+ export function readStudioSavedOptions(printerTypes: string[], configPath: string | undefined): Record<string, string> | undefined {
112
+ if (!configPath) return undefined;
113
+ let text: string;
114
+ try {
115
+ const stat = fs.statSync(configPath);
116
+ if (!stat.isFile() || stat.size > 16 * 1024 * 1024) return undefined;
117
+ text = fs.readFileSync(configPath, "utf8");
118
+ } catch { return undefined; }
119
+ let config: unknown;
120
+ // Studio appends "# MD5 checksum <hex>" after the JSON.
121
+ try { config = JSON.parse(text.split("\n").filter(line => !line.trimStart().startsWith("#")).join("\n")); }
122
+ catch { return undefined; }
123
+ if (!config || typeof config !== "object") return undefined;
124
+ for (const type of printerTypes) {
125
+ const section = (config as Record<string, unknown>)[type];
126
+ if (!section || typeof section !== "object" || Array.isArray(section)) continue;
127
+ const saved: Record<string, string> = {};
128
+ for (const key of ["bed_leveling", "flow_cali", "nozzle_offset_cali", "timelapse"]) {
129
+ const value = (section as Record<string, unknown>)[key];
130
+ if (typeof value === "string") saved[key] = value;
131
+ }
132
+ return saved;
133
+ }
134
+ return undefined;
135
+ }
136
+
137
+ function fromInt(value: number, label: string): StudioOptionValue {
138
+ if (value === 0) return "off";
139
+ if (value === 1) return "on";
140
+ if (value === 2) return "auto";
141
+ throw new Error(`${label} must be 0 (off), 1 (on) or 2 (auto).`);
142
+ }
143
+
144
+ const toInt = (value: StudioOptionValue) => value === "off" ? 0 : value === "on" ? 1 : 2;
145
+
146
+ /** load_option_vals for one option: saved if offered, else auto if offered, else on. */
147
+ function loaded(saved: string | undefined, offers: Offers): { value: StudioOptionValue; source: StudioOptionSource } {
148
+ if (saved !== undefined && (offers as readonly string[]).includes(saved)) return { value: saved as StudioOptionValue, source: "studio-config" };
149
+ return { value: offers.includes("auto") ? "auto" : "on", source: "studio-default" };
150
+ }
151
+
152
+ /**
153
+ * What Studio's Send dialog would send for this model, with the caller's
154
+ * explicit choices taking precedence. usedNozzles is the number of nozzles the
155
+ * job uses, for dual-nozzle printers.
156
+ */
157
+ export function resolveStudioPrintOptions(model: string, caller: CallerPrintOptions = {}, options: { usedNozzles?: number; configPath?: string } = {}): StudioPrintOptions {
158
+ const printer = STUDIO_PRINTERS[model];
159
+ const saved = printer ? readStudioSavedOptions(printer.types, "configPath" in options ? options.configPath : studioConfigPath()) ?? {} : {};
160
+ const printerType = printer?.types[0];
161
+
162
+ const bedOffers = !printer || printer.bedLeveling !== 1 ? AUTO_ON_OFF : ON_OFF;
163
+ const flowOffers = !printer || printer.pSeries || printer.autoFlow ? AUTO_ON_OFF : ON_OFF;
164
+ const bed = loaded(saved.bed_leveling, bedOffers);
165
+ const flow = loaded(saved.flow_cali, flowOffers);
166
+ let offset = loaded(saved.nozzle_offset_cali, AUTO_ON_OFF);
167
+ if (printer?.dualNozzle && options.usedNozzles !== undefined) {
168
+ if (options.usedNozzles >= 2) offset = { value: "auto", source: "studio-default" };
169
+ else if (options.usedNozzles === 1) offset = { value: "off", source: "studio-default" };
170
+ }
171
+
172
+ const choose = (current: { value: StudioOptionValue; source: StudioOptionSource }, auto: number | undefined, flag: boolean | undefined, label: string) =>
173
+ auto !== undefined ? { value: fromInt(auto, label), source: "caller" as const }
174
+ : flag !== undefined ? { value: flag ? "on" as const : "off" as const, source: "caller" as const }
175
+ : current;
176
+ const bedLeveling = choose(bed, caller.autoBedLeveling, caller.bedLeveling, "auto_bed_leveling");
177
+ const flowCali = choose(flow, caller.autoFlowCali, caller.flowCalibration, "auto_flow_cali");
178
+ const nozzleOffset = choose(offset, caller.autoOffsetCali, undefined, "auto_offset_cali");
179
+ const manualMode = caller.extruderCaliManualMode ?? 0;
180
+ if (!Number.isInteger(manualMode) || manualMode < -1 || manualMode > 1) throw new Error("extruder_cali_manual_mode must be -1, 0 or 1.");
181
+
182
+ return {
183
+ ...(printerType ? { printerType } : {}),
184
+ values: { bed_leveling: bedLeveling.value, flow_cali: flowCali.value, nozzle_offset_cali: nozzleOffset.value },
185
+ sources: { bed_leveling: bedLeveling.source, flow_cali: flowCali.source, nozzle_offset_cali: nozzleOffset.source },
186
+ task_bed_leveling: bedLeveling.value === "on",
187
+ task_flow_cali: flowCali.value === "on",
188
+ task_vibration_cali: caller.vibrationCalibration ?? false,
189
+ task_layer_inspect: caller.layerInspect ?? true,
190
+ auto_bed_leveling: toInt(bedLeveling.value),
191
+ auto_flow_cali: toInt(flowCali.value),
192
+ auto_offset_cali: toInt(nozzleOffset.value),
193
+ extruder_cali_manual_mode: manualMode,
194
+ };
195
+ }
196
+
197
+ /** The number of nozzles a job's used filaments print from, when the inspection knows. */
198
+ export function usedNozzleCount(inspection: { usedFilamentPositions: number[]; filamentNozzleIndices?: Array<number | undefined> }): number | undefined {
199
+ const nozzles = inspection.usedFilamentPositions.map(position => inspection.filamentNozzleIndices?.[position]).filter((value): value is number => Number.isInteger(value));
200
+ return nozzles.length === inspection.usedFilamentPositions.length && nozzles.length ? new Set(nozzles).size : undefined;
201
+ }