comfy-native-apps 0.0.0-stage → 1.0.1

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/package.json CHANGED
@@ -1,6 +1,48 @@
1
1
  {
2
2
  "name": "comfy-native-apps",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.1",
4
+ "description": "A 9-tool MCP server that runs ComfyUI's native .app.json workflow apps and manages the render queue, with zero plugin dependency.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "hpx",
8
+ "homepage": "https://github.com/hpx502766238/comfy-native-apps#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/hpx502766238/comfy-native-apps.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/hpx502766238/comfy-native-apps/issues"
15
+ },
16
+ "keywords": [
17
+ "mcp",
18
+ "model-context-protocol",
19
+ "comfyui",
20
+ "comfyui-mcp",
21
+ "image-generation",
22
+ "video-generation",
23
+ "standalone",
24
+ "local-llm",
25
+ "small-model"
26
+ ],
27
+ "bin": {
28
+ "comfy-native-apps": "comfy_native_apps.mjs"
29
+ },
30
+ "engines": {
31
+ "node": ">=18"
32
+ },
33
+ "files": [
34
+ "comfy_native_apps.mjs",
35
+ "native_lib.mjs",
36
+ "preview_png.mjs",
37
+ "drive_safe.mjs",
38
+ "assert_media.mjs",
39
+ "MCP-CONFIG.zh-CN.md"
40
+ ],
41
+ "scripts": {
42
+ "test": "node drive_safe.mjs"
43
+ },
44
+ "publishConfig": {
45
+ "access": "public",
46
+ "registry": "https://registry.npmjs.org/"
47
+ }
48
+ }
@@ -0,0 +1,198 @@
1
+ // Downscale a PNG so it can be attached inline without blowing up a client's
2
+ // context. ComfyUI's SaveImage writes plain 8-bit, non-interlaced PNG (RGB or
3
+ // RGBA), which is all this decodes; any other PNG (16-bit, interlaced, …) is
4
+ // left untouched rather than risking a broken preview. Encoding uses the
5
+ // standard per-row filter heuristic plus zlib level 9, so it stays dependency
6
+ // free — no sharp / canvas / ImageMagick on the MCP host.
7
+ import zlib from 'node:zlib';
8
+
9
+ const SIG = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
10
+ const CHANNELS = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 };
11
+
12
+ const CRC = (() => {
13
+ const t = new Int32Array(256);
14
+ for (let n = 0; n < 256; n++) {
15
+ let c = n;
16
+ for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
17
+ t[n] = c;
18
+ }
19
+ return t;
20
+ })();
21
+
22
+ function crc32(buf) {
23
+ let c = -1;
24
+ for (let i = 0; i < buf.length; i++) c = CRC[(c ^ buf[i]) & 0xff] ^ (c >>> 8);
25
+ return (c ^ -1) >>> 0;
26
+ }
27
+
28
+ function paeth(a, b, c) {
29
+ const p = a + b - c;
30
+ const pa = Math.abs(p - a), pb = Math.abs(p - b), pc = Math.abs(p - c);
31
+ return pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
32
+ }
33
+
34
+ function readChunks(buf) {
35
+ const out = new Map();
36
+ for (let off = 8; off + 12 <= buf.length;) {
37
+ const len = buf.readUInt32BE(off);
38
+ const type = buf.toString('latin1', off + 4, off + 8);
39
+ if (off + 12 + len > buf.length) return null;
40
+ const data = buf.subarray(off + 8, off + 8 + len);
41
+ if (type === 'IDAT') {
42
+ const list = out.get('IDAT') ?? [];
43
+ list.push(data);
44
+ out.set('IDAT', list);
45
+ } else if (type === 'IHDR' || type === 'PLTE') out.set(type, data);
46
+ off += 12 + len;
47
+ }
48
+ return out;
49
+ }
50
+
51
+ function unfilter(raw, w, h, bpp, stride) {
52
+ const out = Buffer.alloc(stride * h);
53
+ for (let y = 0; y < h; y++) {
54
+ const ft = raw[y * (stride + 1)];
55
+ if (ft > 4) return null;
56
+ const src = raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1));
57
+ const row = out.subarray(y * stride, (y + 1) * stride);
58
+ const prev = y ? out.subarray((y - 1) * stride, y * stride) : null;
59
+ for (let x = 0; x < stride; x++) {
60
+ const a = x >= bpp ? row[x - bpp] : 0;
61
+ const b = prev ? prev[x] : 0;
62
+ const c = prev && x >= bpp ? prev[x - bpp] : 0;
63
+ const v = src[x] + (ft === 0 ? 0 : ft === 1 ? a : ft === 2 ? b : ft === 3 ? (a + b) >> 1 : paeth(a, b, c));
64
+ row[x] = v & 0xff;
65
+ }
66
+ }
67
+ return out;
68
+ }
69
+
70
+ // RGB triples, alpha dropped: these are previews of opaque photographs, and
71
+ // keeping the alpha channel only inflates the encoded size.
72
+ function toRgb(px, w, h, color, palette) {
73
+ const n = w * h;
74
+ const rgb = Buffer.alloc(n * 3);
75
+ if (color === 2 || color === 6) {
76
+ const step = color === 6 ? 4 : 3;
77
+ for (let i = 0; i < n; i++) {
78
+ rgb[i * 3] = px[i * step];
79
+ rgb[i * 3 + 1] = px[i * step + 1];
80
+ rgb[i * 3 + 2] = px[i * step + 2];
81
+ }
82
+ } else if (color === 3) {
83
+ for (let i = 0; i < n; i++) {
84
+ const p = px[i] * 3;
85
+ rgb[i * 3] = palette[p];
86
+ rgb[i * 3 + 1] = palette[p + 1];
87
+ rgb[i * 3 + 2] = palette[p + 2];
88
+ }
89
+ } else {
90
+ for (let i = 0; i < n; i++) {
91
+ const g = px[i * (color === 4 ? 2 : 1)];
92
+ rgb[i * 3] = rgb[i * 3 + 1] = rgb[i * 3 + 2] = g;
93
+ }
94
+ }
95
+ return rgb;
96
+ }
97
+
98
+ function decode(buf) {
99
+ if (!buf.subarray(0, 8).equals(SIG)) return null;
100
+ const c = readChunks(buf);
101
+ const ihdr = c?.get('IHDR');
102
+ if (!ihdr || ihdr.length < 13) return null;
103
+ const w = ihdr.readUInt32BE(0), h = ihdr.readUInt32BE(4);
104
+ const depth = ihdr[8], color = ihdr[9], interlace = ihdr[12];
105
+ if (!w || !h || depth !== 8 || interlace !== 0 || !CHANNELS[color]) return null;
106
+ const palette = c.get('PLTE');
107
+ if (color === 3 && !palette) return null;
108
+ const stride = w * CHANNELS[color];
109
+ const raw = zlib.inflateSync(Buffer.concat(c.get('IDAT')), { maxOutputLength: stride * h + h });
110
+ const px = unfilter(raw, w, h, Math.max(1, Math.ceil(CHANNELS[color] * depth / 8)), stride);
111
+ return px ? { w, h, rgb: toRgb(px, w, h, color, palette) } : null;
112
+ }
113
+
114
+ function downscale(src, w, h, tw, th) {
115
+ const out = Buffer.alloc(tw * th * 3);
116
+ const xs = new Int32Array(tw + 1), ys = new Int32Array(th + 1);
117
+ for (let x = 0; x <= tw; x++) xs[x] = Math.floor(x * w / tw);
118
+ for (let y = 0; y <= th; y++) ys[y] = Math.floor(y * h / th);
119
+ for (let y = 0; y < th; y++) {
120
+ for (let x = 0; x < tw; x++) {
121
+ let r = 0, g = 0, b = 0, n = 0;
122
+ for (let sy = ys[y]; sy < ys[y + 1]; sy++) {
123
+ for (let sx = xs[x], o = sy * w * 3 + sx * 3; sx < xs[x + 1]; sx++, o += 3) {
124
+ r += src[o]; g += src[o + 1]; b += src[o + 2]; n++;
125
+ }
126
+ }
127
+ const o = (y * tw + x) * 3;
128
+ out[o] = Math.round(r / n);
129
+ out[o + 1] = Math.round(g / n);
130
+ out[o + 2] = Math.round(b / n);
131
+ }
132
+ }
133
+ return out;
134
+ }
135
+
136
+ function encode(w, h, rgb) {
137
+ const stride = w * 3;
138
+ const raw = Buffer.alloc((stride + 1) * h);
139
+ const prev = Buffer.alloc(stride);
140
+ const cand = Array.from({ length: 5 }, () => Buffer.alloc(stride));
141
+ for (let y = 0; y < h; y++) {
142
+ const row = rgb.subarray(y * stride, (y + 1) * stride);
143
+ let best = 0, bestSum = Infinity;
144
+ for (let f = 0; f < 5; f++) {
145
+ const c = cand[f];
146
+ let sum = 0;
147
+ for (let x = 0; x < stride; x++) {
148
+ const a = x >= 3 ? row[x - 3] : 0;
149
+ const b = prev[x];
150
+ const cc = x >= 3 ? prev[x - 3] : 0;
151
+ const v = (row[x] - (f === 0 ? 0 : f === 1 ? a : f === 2 ? b : f === 3 ? (a + b) >> 1 : paeth(a, b, cc))) & 0xff;
152
+ c[x] = v;
153
+ sum += v < 128 ? v : 256 - v;
154
+ }
155
+ if (sum < bestSum) { bestSum = sum; best = f; }
156
+ }
157
+ raw[y * (stride + 1)] = best;
158
+ cand[best].copy(raw, y * (stride + 1) + 1);
159
+ row.copy(prev);
160
+ }
161
+ const ihdr = Buffer.alloc(13);
162
+ ihdr.writeUInt32BE(w, 0);
163
+ ihdr.writeUInt32BE(h, 4);
164
+ ihdr[8] = 8;
165
+ ihdr[9] = 2;
166
+ const chunk = (type, data) => {
167
+ const out = Buffer.alloc(12 + data.length);
168
+ out.writeUInt32BE(data.length, 0);
169
+ out.write(type, 4, 'latin1');
170
+ data.copy(out, 8);
171
+ out.writeUInt32BE(crc32(out.subarray(4, 8 + data.length)), 8 + data.length);
172
+ return out;
173
+ };
174
+ return Buffer.concat([
175
+ SIG,
176
+ chunk('IHDR', ihdr),
177
+ chunk('IDAT', zlib.deflateSync(raw, { level: 9 })),
178
+ chunk('IEND', Buffer.alloc(0)),
179
+ ]);
180
+ }
181
+
182
+ // Returns the re-encoded PNG bytes, or null when the image is already inside
183
+ // both budgets (nothing worth doing) or its format is not supported.
184
+ export function shrinkPng(buf, { maxSide = 1024, maxPixels = 1100000, maxBytes = 1500000 } = {}) {
185
+ const img = decode(buf);
186
+ if (!img) return null;
187
+ let scale = Math.min(1, maxSide / Math.max(img.w, img.h), Math.sqrt(maxPixels / (img.w * img.h)));
188
+ if (scale >= 1 && buf.length <= maxBytes) return null;
189
+ for (let attempt = 0; attempt < 3; attempt++) {
190
+ const tw = Math.max(1, Math.round(img.w * scale));
191
+ const th = Math.max(1, Math.round(img.h * scale));
192
+ const out = encode(tw, th, scale >= 1 ? img.rgb : downscale(img.rgb, img.w, img.h, tw, th));
193
+ if (out.length <= maxBytes || (tw <= 64 && th <= 64)) return out;
194
+ // Still too heavy at this size: shrink further and try again.
195
+ scale *= Math.max(0.5, Math.min(0.9, Math.sqrt(maxBytes / out.length) * 0.95));
196
+ }
197
+ return null;
198
+ }