@rig-c/parts 2.26.1 → 2.27.0

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.
@@ -148,6 +148,69 @@ export class ComfyClient {
148
148
  return (Date.now() - t0) / 1000;
149
149
  }
150
150
 
151
+ /**
152
+ * Where a queued prompt stands: `running` when the box is running it,
153
+ * `pending` with its 1-based place among the pending jobs (and how many are
154
+ * ahead of it, running included), or `absent` when the queue no longer lists
155
+ * it (finished, or never queued).
156
+ */
157
+ async queuePlace(id: string): Promise<{ state: 'running' } | { state: 'pending'; place: number; pending: number; ahead: number } | { state: 'absent' }> {
158
+ const q = asObject(await this.json('GET', '/queue'));
159
+ if (q === null || !Array.isArray(q.queue_running) || !Array.isArray(q.queue_pending)) {
160
+ refuse('COMFY_REQUEST_OK', `GET ${this.host}/queue`, 'answered without queue_running and queue_pending arrays');
161
+ }
162
+ const idOf = (e: unknown): unknown => (Array.isArray(e) ? e[1] : undefined);
163
+ const running = q.queue_running as unknown[];
164
+ if (running.some((e) => idOf(e) === id)) return { state: 'running' };
165
+ // ComfyUI orders pending jobs by their queue number (the entry's first element), not by list position.
166
+ const pending = [...(q.queue_pending as unknown[])].sort((a, b) => Number(Array.isArray(a) ? a[0] : 0) - Number(Array.isArray(b) ? b[0] : 0));
167
+ const at = pending.findIndex((e) => idOf(e) === id);
168
+ if (at < 0) return { state: 'absent' };
169
+ return { state: 'pending', place: at + 1, pending: pending.length, ahead: running.length + at };
170
+ }
171
+
172
+ /**
173
+ * Wait for a prompt queued behind other jobs (it was appended, never put in
174
+ * front, and nothing here reorders or cancels anyone's job): at most `wait`
175
+ * seconds while it is still pending, then at most `timeout` seconds once the
176
+ * box is running it. A wait past either bound is refused naming the job's
177
+ * place in the queue; the job is left on the box.
178
+ */
179
+ async waitBehind(id: string, wait: number, timeout: number, say: (line: string) => void): Promise<{ entry: HistoryEntry; vramLow: Record<string, number>; elapsed: number }> {
180
+ const t0 = Date.now();
181
+ let started: number | null = null;
182
+ let lastPlace = -1;
183
+ for (;;) {
184
+ const place = await this.queuePlace(id);
185
+ if (place.state === 'absent') {
186
+ const done = await this.waitHistory(id, Math.max(this.timing.request, this.timing.poll * 2));
187
+ return { ...done, elapsed: (Date.now() - t0) / 1000 };
188
+ }
189
+ const now = Date.now();
190
+ if (place.state === 'running') {
191
+ if (started === null) {
192
+ started = now;
193
+ say(` prompt ${id} is running`);
194
+ }
195
+ if ((now - started) / 1000 >= timeout) {
196
+ refuse('COMFY_HISTORY_WITHIN', `prompt ${id}`, `was still running ${((now - started) / 1000).toFixed(1)} s after it started; it is required within --timeout ${timeout} s (the job was left on the box, not cancelled)`);
197
+ }
198
+ } else {
199
+ if (place.place !== lastPlace) say(` prompt ${id} queued behind ${place.ahead} job(s) (pending place ${place.place} of ${place.pending})`);
200
+ lastPlace = place.place;
201
+ const waited = (now - t0) / 1000;
202
+ if (waited >= wait) {
203
+ refuse(
204
+ 'COMFY_QUEUE_BEHIND',
205
+ `prompt ${id}`,
206
+ `was still pending after ${waited.toFixed(1)} s, at place ${place.place} of ${place.pending} pending with ${place.ahead} job(s) ahead of it; it is required to start within --wait ${wait} s — the job was left in its place on the box, not moved and not cancelled, and no one else's job was touched`,
207
+ );
208
+ }
209
+ }
210
+ await sleep(this.timing.poll);
211
+ }
212
+ }
213
+
151
214
  /** `GET /object_info`: every node class the box has, with its inputs. */
152
215
  async objectInfo(): Promise<Record<string, unknown>> {
153
216
  const o = asObject(await this.json('GET', '/object_info'));
@@ -16,4 +16,5 @@
16
16
  */
17
17
  export { ComfyClient, type ClientTiming, resolveHost } from './client.ts';
18
18
  export { runPainting, type PaintRun, type Painted } from './painting.ts';
19
+ export { runBackground, type BackgroundRun } from './plate.ts';
19
20
  export { runSeeThrough, type SeeThroughResult, type SeeThroughRun } from './seethrough.ts';
@@ -0,0 +1,135 @@
1
+ /**
2
+ * `comfy plate --mode background`: the figure-removed background plate of a
3
+ * painting, inpainted on a ComfyUI box over a source mask (issue #1183).
4
+ *
5
+ * The route, in order, each step refusing by name:
6
+ *
7
+ * 1. the source mask is read (`src/plate.ts`); with none, the run is refused
8
+ * before anything is written and before the host is reached;
9
+ * 2. the declared checkpoint sha256 and, for `core`, the painting's grid are
10
+ * checked;
11
+ * 3. the box answers `/system_stats` and names its GPU;
12
+ * 4. every node, input and model of the chosen graph is on the box's
13
+ * `/object_info` (`checkGraph`) — a box without the inpaint pack is refused
14
+ * here for `lama`, before anything is uploaded;
15
+ * 5. the painting and the grown mask are uploaded and the graph is appended
16
+ * to the queue: behind every other job, never in front, nothing reordered
17
+ * and nothing cancelled; a wait past `--wait` while pending is refused
18
+ * naming the job's place (`COMFY_QUEUE_BEHIND`);
19
+ * 6. the box's image is read back, required at the painting's size, pasted
20
+ * into the painting inside the mask only, and written with its receipt
21
+ * through `publishPlate` (staged, read back, then moved).
22
+ *
23
+ * ⚠️ This is a different queue policy from `comfy paint` and `comfy
24
+ * seethrough`, which wait for an empty queue and never queue behind anyone.
25
+ */
26
+ import { refuseIfAny } from '../errors.ts';
27
+ import { checkGraph, type Graph } from '../graphs.ts';
28
+ import {
29
+ assertPlateOutFree,
30
+ CORE_DEFAULTS,
31
+ type CoreParams,
32
+ coreGridProblem,
33
+ corePlateGraph,
34
+ declaredChecksum,
35
+ type FigureMask,
36
+ growMask,
37
+ type LamaParams,
38
+ LAMA_MODEL,
39
+ lamaPlateGraph,
40
+ maskImage,
41
+ parseMaskFrom,
42
+ pasteInside,
43
+ PLATE_GRAPH_IDS,
44
+ type PlateGraph,
45
+ type PlateReceipt,
46
+ publishPlate,
47
+ readPlatePainting,
48
+ readSourceMask,
49
+ } from '../plate.ts';
50
+ import { OPAQUE_ALPHA_ABOVE } from '../layers.ts';
51
+ import { decodePngBytes, encodePngBytes } from '../raster/png.ts';
52
+ import { ComfyClient, historyImages, refuse } from './client.ts';
53
+
54
+ export interface BackgroundRun {
55
+ painting: string;
56
+ maskFrom: string | undefined;
57
+ out: string;
58
+ graph: PlateGraph;
59
+ /** The model file the graph loads: the LaMa model for `lama`, the checkpoint for `core`. */
60
+ model: string | undefined;
61
+ checkpointSha256: string | undefined;
62
+ seed: number;
63
+ growPx: number;
64
+ steps: number;
65
+ prompt: string | undefined;
66
+ negative: string | undefined;
67
+ wait: number;
68
+ timeout: number;
69
+ }
70
+
71
+ /** The GPU the box runs on, as `/system_stats` names its torch device. */
72
+ export function gpuName(stats: Record<string, unknown>, host: string): string {
73
+ const devices = Array.isArray(stats.devices) ? stats.devices : [];
74
+ const names = devices.map((d) => (typeof d === 'object' && d !== null ? (d as Record<string, unknown>).name : undefined)).filter((n): n is string => typeof n === 'string' && n !== '');
75
+ if (names.length !== 1) {
76
+ refuse('COMFY_GPU_NAMED', `GET ${host}/system_stats`, `names ${names.length} device(s); exactly one is required, because the receipt records the GPU (the same seed gives other pixels on another GPU)`);
77
+ }
78
+ return names[0];
79
+ }
80
+
81
+ /** `connect` is called only after every local check has passed, so a refused run never resolves a host or reaches a box. */
82
+ export async function runBackground(connect: () => ComfyClient, run: BackgroundRun, say: (line: string) => void): Promise<PlateReceipt> {
83
+ // Local first: no source mask, a bad checksum or an off-grid painting is refused before the box is reached.
84
+ const from = parseMaskFrom(run.maskFrom);
85
+ const painting = readPlatePainting(run.painting);
86
+ const { width: W, height: H } = painting.raster;
87
+ const source: FigureMask = readSourceMask(from.source, from.dir, W, H);
88
+ const checkpoint = declaredChecksum(run.checkpointSha256);
89
+ if (run.graph === 'core') {
90
+ if (run.model === undefined) refuse('PLATE_MODEL_GIVEN', '--model', 'is absent; the core graph needs the checkpoint the box loads, by its name on the box — there is no default checkpoint');
91
+ const grid = coreGridProblem(W, H, run.painting);
92
+ if (grid !== null) refuseIfAny([grid]);
93
+ }
94
+ assertPlateOutFree(run.out);
95
+ const mask = growMask(source, run.growPx);
96
+ const model = run.model ?? LAMA_MODEL;
97
+ const lama: LamaParams = { seed: run.seed, grow_px: run.growPx };
98
+ const core: CoreParams = { seed: run.seed, grow_px: run.growPx, steps: run.steps, cfg: CORE_DEFAULTS.cfg, sampler: CORE_DEFAULTS.sampler, scheduler: CORE_DEFAULTS.scheduler, denoise: CORE_DEFAULTS.denoise, prompt: run.prompt ?? CORE_DEFAULTS.prompt, negative: run.negative ?? CORE_DEFAULTS.negative };
99
+ const prefix = `rig_parts_plate_${crypto.randomUUID().slice(0, 8)}`;
100
+ const build = (p: string, m: string): Graph => (run.graph === 'lama' ? lamaPlateGraph(p, m, model, lama, prefix) : corePlateGraph(p, m, model, core, prefix));
101
+
102
+ const client = connect();
103
+ const gpu = gpuName(await client.systemStats(), client.host);
104
+ const info = await client.objectInfo();
105
+ refuseIfAny(checkGraph(build(`${prefix}_painting.png`, `${prefix}_mask.png`), info, client.host, new Set(['1.image', '2.image'])));
106
+ say(` box: every node, input and model of the ${run.graph} graph is on its /object_info; GPU ${gpu}`);
107
+
108
+ const paintingRef = await client.uploadImage(painting.bytes, `${prefix}_painting.png`);
109
+ const maskRef = await client.uploadImage(encodePngBytes(maskImage(mask)), `${prefix}_mask.png`);
110
+ say(` uploaded ${run.painting} (${W}x${H}) and its mask (${mask.inside.reduce((a, b) => a + b, 0)} px, grown ${run.growPx} px)`);
111
+ const id = await client.submit(build(paintingRef, maskRef), 'rig-parts');
112
+ say(` appended prompt ${id} to the queue (behind any other job; nothing reordered or cancelled)`);
113
+ const { entry, elapsed } = await client.waitBehind(id, run.wait, run.timeout, say);
114
+ say(` GPU job finished ${elapsed.toFixed(1)} s after it was queued`);
115
+ const images = historyImages(entry);
116
+ if (images.length !== 1) refuse('COMFY_HISTORY_OUTPUTS', `prompt ${id}`, `lists ${images.length} image(s); the graph's one SaveImage is required to have saved exactly one`);
117
+ const got = decodePngBytes(await client.view(images[0].filename, images[0].subfolder, images[0].type), images[0].filename);
118
+ if (got.width !== W || got.height !== H) refuse('COMFY_PLATE_SIZE', `prompt ${id} ${images[0].filename}`, `is ${got.width}x${got.height}; the painting's ${W}x${H} is required — the plate is pasted into the painting pixel for pixel, never resized`);
119
+
120
+ return publishPlate({
121
+ out: run.out,
122
+ stageTag: prefix,
123
+ mode: 'background',
124
+ paintingPath: run.painting,
125
+ paintingBytes: painting.bytes,
126
+ source: from.source,
127
+ sourceDir: from.dir,
128
+ mask,
129
+ plate: pasteInside(painting.raster, got, mask),
130
+ model: { name: model, checkpoint_sha256: checkpoint, checkpoint_sha256_from: 'declared' },
131
+ params: run.graph === 'lama' ? { ...lama, mask_alpha_above: OPAQUE_ALPHA_ABOVE } : { ...core, mask_alpha_above: OPAQUE_ALPHA_ABOVE },
132
+ graph: PLATE_GRAPH_IDS[run.graph],
133
+ gpu,
134
+ });
135
+ }
package/src/config.ts CHANGED
@@ -100,7 +100,7 @@ import { REQUIREMENTS_SPEC, type RequirementsFile } from './requirements.ts';
100
100
  import { setupPaintingFilePath } from './setuppainting.ts';
101
101
  import { CLOTH_TARGETS, type ClothRecipes, RECIPE_STRENGTH, RIGC_DEFAULT_DAMPING, springStable } from './cloth.ts';
102
102
  import { GRID, MAX_SIDE } from './contour.ts';
103
- import { type Problem, refuseIfAny } from './errors.ts';
103
+ import { foldName, type Problem, refuseIfAny } from './errors.ts';
104
104
  import { edgeLength } from './length.ts';
105
105
  import { setFocusProblems } from './setfocus.ts';
106
106
  import { type EditorReceipts, SHA256_PATTERN, SHA256_RULE } from './editreceipt.ts';
@@ -2824,15 +2824,15 @@ function checkAlternates(
2824
2824
  ...baseSlots,
2825
2825
  ...explicitIslandSlotNames(baseSlots, meshes),
2826
2826
  ]);
2827
- const seenNames = new Map<string, number>();
2827
+ const seenNames = new Map<string, { index: number; name: string }>();
2828
2828
  (v as Json[]).forEach((entry, i) => {
2829
2829
  const at = `${p}[${i}]`;
2830
2830
  const e = c.object(at, entry, ['name'], ['source', 'full', 'head', 'parts', 'hides', 'head_box', 'cuts', 'skeleton', 'anchor', 'drawn', 'tips', 'front', 'layers', 'focus', 'base_anchor']);
2831
2831
  if (e === null) return;
2832
2832
  if ('name' in e && c.string(`${at}.name`, e.name)) {
2833
2833
  if (!partNameOk(e.name)) c.fail('CONFIG_PART_NAME', `${at}.name`, `names the set ${show(e.name)}; a set name is a file prefix, so it may not be empty, start with "." or hold a slash`);
2834
- if (seenNames.has(e.name)) c.fail('CONFIG_PART_UNIQUE', `${at}.name`, `names the set "${e.name}" again (first at alternates[${seenNames.get(e.name)}])`);
2835
- else seenNames.set(e.name, i);
2834
+ if (seenNames.has(foldName(e.name))) c.fail('CONFIG_PART_UNIQUE', `${at}.name`, `names the set "${e.name}" again (first at alternates[${seenNames.get(foldName(e.name))!.index}] ("${seenNames.get(foldName(e.name))!.name}"))`);
2835
+ else seenNames.set(foldName(e.name), { index: i, name: e.name });
2836
2836
  }
2837
2837
  for (const k of ['source', 'full', 'head', 'skeleton'] as const) {
2838
2838
  if (k in e) c.string(`${at}.${k}`, e[k]);
package/src/errors.ts CHANGED
@@ -65,3 +65,8 @@ export class PartsError extends Error {
65
65
  export function refuseIfAny(problems: readonly Problem[]): void {
66
66
  if (problems.length > 0) throw new PartsError(problems);
67
67
  }
68
+
69
+ /** Fold a file, part or set name for host-independent collision checks (NFC + ASCII lower case). */
70
+ export function foldName(name: string): string {
71
+ return name.normalize('NFC').toLowerCase();
72
+ }
package/src/parts.ts CHANGED
@@ -94,7 +94,7 @@ import { existsSync, readFileSync, writeFileSync } from 'node:fs';
94
94
  import { createHash } from 'node:crypto';
95
95
  import { dirname, join } from 'node:path';
96
96
  import { decodePngBytes } from './raster/png.ts';
97
- import { type Problem, refuseIfAny } from './errors.ts';
97
+ import { foldName, type Problem, refuseIfAny } from './errors.ts';
98
98
  import { KEYPOINT_NAMES, type PosePoints } from './skeleton.ts';
99
99
  import { readLayerTag } from './tags.ts';
100
100
  import { edgeLength } from './length.ts';
@@ -796,8 +796,8 @@ function checkParts(raw: unknown, path: string): PartsFile {
796
796
  }
797
797
  if (!isPartName(r.name)) {
798
798
  fail('PARTS_FIELD_TYPE', `${label} field "name"`, `is ${show(r.name)}; a file-name-safe part name is required`);
799
- } else if (seen.has(r.name.toLowerCase())) fail('PARTS_NAME_UNIQUE', label, `appears at parts[${seen.get(r.name.toLowerCase())!.index}] ("${seen.get(r.name.toLowerCase())!.name}") and parts[${i}] ("${r.name}")`);
800
- else seen.set(r.name.toLowerCase(), { index: i, name: r.name });
799
+ } else if (seen.has(foldName(r.name))) fail('PARTS_NAME_UNIQUE', label, `appears at parts[${seen.get(foldName(r.name))!.index}] ("${seen.get(foldName(r.name))!.name}") and parts[${i}] ("${r.name}")`);
800
+ else seen.set(foldName(r.name), { index: i, name: r.name });
801
801
  const from = typeof r.from === 'string' ? readFrom(r.from) : null;
802
802
  if (from === null) fail('PARTS_FROM_KNOWN', `${label} field "from"`, `is ${show(r.from)}; "<full|head>:<v3 tag>", "<full|head>:author:<label>" for a layer the author made, or "painting:<the part's own name>" for a patch, is required`);
803
803
  else if (from.run === 'painting' && from.patch !== r.name) {