ima2-gen 1.1.6 → 1.1.8

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 (51) hide show
  1. package/.env.example +5 -0
  2. package/README.md +3 -0
  3. package/assets/phase-a-bg-cleanup-test.png +0 -0
  4. package/config.js +111 -0
  5. package/docs/FAQ.ko.md +20 -0
  6. package/docs/FAQ.md +20 -0
  7. package/docs/README.ko.md +3 -0
  8. package/docs/README.zh-CN.md +3 -0
  9. package/integrations/comfyui/ima2_gen_bridge/README.md +88 -0
  10. package/integrations/comfyui/ima2_gen_bridge/__init__.py +3 -0
  11. package/integrations/comfyui/ima2_gen_bridge/__pycache__/__init__.cpython-313.pyc +0 -0
  12. package/integrations/comfyui/ima2_gen_bridge/__pycache__/nodes.cpython-313.pyc +0 -0
  13. package/integrations/comfyui/ima2_gen_bridge/nodes.py +238 -0
  14. package/lib/canvasVersionStore.js +181 -0
  15. package/lib/cardNewsPlannerClient.js +4 -2
  16. package/lib/comfyBridge.js +214 -0
  17. package/lib/db.js +14 -0
  18. package/lib/historyList.js +4 -0
  19. package/lib/imageMetadata.js +4 -0
  20. package/lib/imageModels.js +20 -0
  21. package/lib/localImportStore.js +111 -0
  22. package/lib/oauthProxy.js +88 -38
  23. package/lib/pngInfo.js +26 -0
  24. package/lib/promptImport/curatedSources.js +139 -0
  25. package/lib/promptImport/discoveryRegistry.js +236 -0
  26. package/lib/promptImport/errors.js +16 -0
  27. package/lib/promptImport/githubDiscovery.js +248 -0
  28. package/lib/promptImport/githubFolder.js +308 -0
  29. package/lib/promptImport/githubSource.js +239 -0
  30. package/lib/promptImport/gptImageHints.js +68 -0
  31. package/lib/promptImport/parsePromptCandidates.js +153 -0
  32. package/lib/promptImport/promptIndex.js +248 -0
  33. package/lib/promptImport/rankPromptCandidates.js +49 -0
  34. package/package.json +3 -2
  35. package/routes/annotations.js +95 -0
  36. package/routes/canvasVersions.js +64 -0
  37. package/routes/comfy.js +39 -0
  38. package/routes/edit.js +73 -4
  39. package/routes/generate.js +16 -2
  40. package/routes/imageImport.js +33 -0
  41. package/routes/index.js +10 -0
  42. package/routes/multimode.js +18 -1
  43. package/routes/nodes.js +25 -3
  44. package/routes/promptImport.js +354 -0
  45. package/ui/dist/assets/index-BDffwmLs.css +1 -0
  46. package/ui/dist/assets/index-D0fdHLkJ.js +31 -0
  47. package/ui/dist/assets/index-D0fdHLkJ.js.map +1 -0
  48. package/ui/dist/index.html +6 -3
  49. package/ui/dist/assets/index-3X-6VjbF.css +0 -1
  50. package/ui/dist/assets/index-DPSq9qEs.js +0 -31
  51. package/ui/dist/assets/index-DPSq9qEs.js.map +0 -1
@@ -0,0 +1,248 @@
1
+ import { createHash } from "node:crypto";
2
+ import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
3
+ import { dirname } from "node:path";
4
+ import { getCuratedSource, getDefaultSearchSources, listCuratedSources } from "./curatedSources.js";
5
+ import { buildGitHubRawFileSource, fetchGitHubSource } from "./githubSource.js";
6
+ import { parsePromptCandidates } from "./parsePromptCandidates.js";
7
+ import { extractGptImageHints } from "./gptImageHints.js";
8
+ import { rankPromptCandidates } from "./rankPromptCandidates.js";
9
+ import {
10
+ getDefaultReviewedDiscoverySources,
11
+ getReviewedDiscoverySource,
12
+ listReviewedDiscoverySources,
13
+ } from "./discoveryRegistry.js";
14
+
15
+ const INDEX_VERSION = 1;
16
+ const EXTRACTOR_VERSION = 2;
17
+
18
+ function limitsFromCtx(ctx) {
19
+ return {
20
+ maxFileBytesForPreview: ctx.config.limits.promptImportMaxFileBytes,
21
+ maxPromptCandidatesPerFile: ctx.config.limits.promptImportMaxCandidatesPerFile,
22
+ maxPromptCandidatesPerImport: ctx.config.limits.promptImportMaxCandidatesPerImport,
23
+ fetchTimeoutMs: ctx.config.limits.promptImportFetchTimeoutMs,
24
+ maxCandidateChars: ctx.config.limits.promptImportMaxCandidateChars,
25
+ minCandidateChars: ctx.config.limits.promptImportMinCandidateChars,
26
+ maxSourceCharsScanned: ctx.config.limits.promptImportMaxSourceCharsScanned,
27
+ maxRepoIndexFiles: ctx.config.limits.promptImportMaxRepoIndexFiles,
28
+ searchLimit: ctx.config.limits.promptImportCuratedSearchLimit,
29
+ ttlMs: ctx.config.limits.promptImportIndexCacheTtlMs,
30
+ };
31
+ }
32
+
33
+ function cacheFile(ctx) {
34
+ return ctx.config.storage.promptImportIndexCacheFile;
35
+ }
36
+
37
+ function sourceFileId(source, path) {
38
+ return `github:${source.repo}@${source.defaultRef}:${path}`;
39
+ }
40
+
41
+ function hashId(...parts) {
42
+ return createHash("sha256").update(parts.join("\0")).digest("hex");
43
+ }
44
+
45
+ async function readCache(ctx) {
46
+ try {
47
+ const parsed = JSON.parse(await readFile(cacheFile(ctx), "utf8"));
48
+ if (parsed.version !== INDEX_VERSION) return { version: INDEX_VERSION, sources: {} };
49
+ return { version: INDEX_VERSION, sources: parsed.sources || {} };
50
+ } catch {
51
+ return { version: INDEX_VERSION, sources: {} };
52
+ }
53
+ }
54
+
55
+ async function writeCache(ctx, cache) {
56
+ const file = cacheFile(ctx);
57
+ await mkdir(dirname(file), { recursive: true });
58
+ const tmp = `${file}.${process.pid}.${Date.now()}.tmp`;
59
+ await writeFile(tmp, JSON.stringify(cache, null, 2));
60
+ await rename(tmp, file);
61
+ }
62
+
63
+ function sourceTags(source, fileSource) {
64
+ return [
65
+ ...fileSource.tags,
66
+ `source:${source.id}`,
67
+ `license:${source.licenseSpdx}`,
68
+ `trust:${source.trustTier}`,
69
+ source.requiresAttribution ? "attribution-required" : null,
70
+ ].filter(Boolean);
71
+ }
72
+
73
+ function indexedCandidate({ candidate, source, fileSource, fileIndex, index }) {
74
+ const scoreHints = extractGptImageHints(candidate.text);
75
+ const headingPath = candidate.headingPath || candidate.name || "";
76
+ const candidateId = hashId(fileIndex.sourceFileId, fileIndex.contentHash, headingPath, String(candidate.ordinal || index + 1));
77
+ const tags = [...new Set([...(candidate.tags || []), ...sourceTags(source, fileSource)])];
78
+ return {
79
+ ...candidate,
80
+ id: candidateId,
81
+ candidateId,
82
+ name: candidate.name,
83
+ text: candidate.text,
84
+ textPreview: candidate.textPreview || candidate.text.slice(0, 220),
85
+ tags,
86
+ warnings: [...new Set([...(candidate.warnings || []), ...scoreHints.warnings])],
87
+ source: {
88
+ kind: "github",
89
+ owner: source.owner,
90
+ repo: source.name,
91
+ ref: source.defaultRef,
92
+ path: fileSource.path,
93
+ htmlUrl: fileSource.htmlUrl,
94
+ sourceId: source.id,
95
+ },
96
+ sourceFileId: fileIndex.sourceFileId,
97
+ headingPath,
98
+ ordinal: candidate.ordinal || index + 1,
99
+ promptHash: candidate.promptHash || hashId(candidate.text.trim().toLowerCase()),
100
+ scoreHints,
101
+ };
102
+ }
103
+
104
+ async function indexSource(ctx, sourceId) {
105
+ const source = getCuratedSource(sourceId) || await getReviewedDiscoverySource(ctx, sourceId);
106
+ if (!source || source.trustTier === "manual-review") {
107
+ return { source, indexedFiles: 0, candidateCount: 0, warnings: ["curated-source-unavailable"] };
108
+ }
109
+ if (String(source.defaultRef || "").includes("/")) {
110
+ return { source, indexedFiles: 0, candidateCount: 0, warnings: ["discovery-default-branch-unsupported"] };
111
+ }
112
+ if (!Array.isArray(source.allowedPaths) || source.allowedPaths.length === 0) {
113
+ return { source, indexedFiles: 0, candidateCount: 0, warnings: ["discovery-requires-paths"] };
114
+ }
115
+
116
+ const limits = limitsFromCtx(ctx);
117
+ const warnings = [];
118
+ const files = [];
119
+ const candidates = [];
120
+ const allowedPaths = source.allowedPaths.slice(0, limits.maxRepoIndexFiles);
121
+
122
+ for (const path of allowedPaths) {
123
+ try {
124
+ const fileSource = buildGitHubRawFileSource({
125
+ owner: source.owner,
126
+ repo: source.name,
127
+ ref: source.defaultRef,
128
+ path,
129
+ });
130
+ const fetched = await fetchGitHubSource(fileSource, limits);
131
+ const fileIndex = {
132
+ sourceFileId: sourceFileId(source, path),
133
+ owner: source.owner,
134
+ repo: source.name,
135
+ ref: source.defaultRef,
136
+ path,
137
+ extension: fileSource.extension,
138
+ contentHash: fetched.contentHash,
139
+ etag: fetched.etag,
140
+ sizeBytes: fetched.sizeBytes,
141
+ licenseSpdx: source.licenseSpdx,
142
+ htmlUrl: fileSource.htmlUrl,
143
+ indexedAt: new Date().toISOString(),
144
+ lastFetchStatus: "ok",
145
+ promptCandidateCount: 0,
146
+ extractorVersion: EXTRACTOR_VERSION,
147
+ };
148
+ const parsed = parsePromptCandidates({
149
+ text: fetched.text,
150
+ filename: path,
151
+ source: { kind: "github", owner: source.owner, repo: source.name, ref: source.defaultRef, path, htmlUrl: fileSource.htmlUrl },
152
+ tags: sourceTags(source, fileSource),
153
+ limits,
154
+ });
155
+ const indexed = parsed.map((candidate, index) => indexedCandidate({ candidate, source, fileSource, fileIndex, index }));
156
+ fileIndex.promptCandidateCount = indexed.length;
157
+ files.push(fileIndex);
158
+ candidates.push(...indexed);
159
+ } catch (error) {
160
+ warnings.push(`${path}: ${error?.message || "index failed"}`);
161
+ }
162
+ }
163
+
164
+ return {
165
+ source,
166
+ indexedFiles: files.length,
167
+ candidateCount: candidates.length,
168
+ warnings,
169
+ entry: {
170
+ source,
171
+ files,
172
+ candidates,
173
+ refreshedAt: Date.now(),
174
+ },
175
+ };
176
+ }
177
+
178
+ function isFresh(entry, ttlMs) {
179
+ return entry?.refreshedAt && Date.now() - entry.refreshedAt < ttlMs;
180
+ }
181
+
182
+ async function ensureSearchCache(ctx) {
183
+ const cache = await readCache(ctx);
184
+ const limits = limitsFromCtx(ctx);
185
+ const sources = [
186
+ ...getDefaultSearchSources(),
187
+ ...await getDefaultReviewedDiscoverySources(ctx),
188
+ ];
189
+ let changed = false;
190
+ const warnings = [];
191
+
192
+ for (const source of sources) {
193
+ if (isFresh(cache.sources[source.id], limits.ttlMs)) continue;
194
+ const result = await indexSource(ctx, source.id);
195
+ if (result.entry) {
196
+ cache.sources[source.id] = result.entry;
197
+ changed = true;
198
+ }
199
+ warnings.push(...result.warnings);
200
+ }
201
+ if (changed) await writeCache(ctx, cache);
202
+ return { cache, warnings };
203
+ }
204
+
205
+ export async function refreshCuratedSource(ctx, sourceId) {
206
+ const cache = await readCache(ctx);
207
+ const result = await indexSource(ctx, sourceId);
208
+ if (result.entry) {
209
+ cache.sources[sourceId] = result.entry;
210
+ await writeCache(ctx, cache);
211
+ }
212
+ return {
213
+ source: result.source,
214
+ indexedFiles: result.indexedFiles,
215
+ candidateCount: result.candidateCount,
216
+ warnings: result.warnings,
217
+ };
218
+ }
219
+
220
+ export async function searchCuratedPrompts(ctx, { q = "", sourceIds, limit } = {}) {
221
+ const { cache, warnings } = await ensureSearchCache(ctx);
222
+ const limits = limitsFromCtx(ctx);
223
+ const defaultSources = [
224
+ ...getDefaultSearchSources(),
225
+ ...await getDefaultReviewedDiscoverySources(ctx),
226
+ ];
227
+ const allowedIds = Array.isArray(sourceIds) && sourceIds.length
228
+ ? new Set(sourceIds)
229
+ : new Set(defaultSources.map((source) => source.id));
230
+ const candidates = Object.values(cache.sources)
231
+ .filter((entry) => allowedIds.has(entry.source.id))
232
+ .flatMap((entry) => entry.candidates || []);
233
+ const results = rankPromptCandidates({
234
+ candidates,
235
+ query: q,
236
+ limit: Math.min(Number(limit) || limits.searchLimit, limits.searchLimit),
237
+ });
238
+ const sources = [
239
+ ...listCuratedSources(),
240
+ ...await listReviewedDiscoverySources(ctx),
241
+ ];
242
+ return { results, sources, warnings };
243
+ }
244
+
245
+ export async function getPromptImportSources(ctx) {
246
+ const reviewed = ctx ? await listReviewedDiscoverySources(ctx) : [];
247
+ return { sources: [...listCuratedSources(), ...reviewed] };
248
+ }
@@ -0,0 +1,49 @@
1
+ function tokenize(value) {
2
+ return String(value || "")
3
+ .toLowerCase()
4
+ .split(/[^a-z0-9가-힣-]+/i)
5
+ .map((token) => token.trim())
6
+ .filter(Boolean);
7
+ }
8
+
9
+ function includesAny(values, terms) {
10
+ const haystack = values.map((value) => String(value || "").toLowerCase());
11
+ return terms.some((term) => haystack.some((value) => value.includes(term)));
12
+ }
13
+
14
+ export function rankPromptCandidates({ candidates, query, limit }) {
15
+ const terms = tokenize(query);
16
+ const boundedLimit = Math.max(1, Math.min(Number(limit) || 50, 100));
17
+ const ranked = candidates.map((candidate) => {
18
+ const text = String(candidate.text || "").toLowerCase();
19
+ const name = String(candidate.name || "").toLowerCase();
20
+ const tags = Array.isArray(candidate.tags) ? candidate.tags : [];
21
+ const hints = candidate.scoreHints || {};
22
+ const hintValues = [
23
+ ...(hints.modelHints || []),
24
+ ...(hints.generationSurfaceHints || []),
25
+ ...(hints.taskHints || []),
26
+ ...(hints.sizeHints || []),
27
+ ...(hints.qualityHints || []),
28
+ ];
29
+ let score = 0;
30
+
31
+ if (terms.length === 0) score += 1;
32
+ for (const term of terms) {
33
+ if (name.includes(term)) score += 18;
34
+ if (includesAny(tags, [term])) score += 12;
35
+ if (includesAny(hintValues, [term])) score += 14;
36
+ if (text.includes(term)) score += 5;
37
+ if (String(candidate.sourceFileId || "").toLowerCase().includes(term)) score += 4;
38
+ }
39
+ if (tags.some((tag) => tag === "trust:curated")) score += 5;
40
+ if (candidate.warnings?.length) score -= candidate.warnings.length * 3;
41
+
42
+ return { ...candidate, score };
43
+ });
44
+
45
+ return ranked
46
+ .filter((candidate) => terms.length === 0 || candidate.score > 0)
47
+ .sort((a, b) => b.score - a.score || String(a.name).localeCompare(String(b.name)))
48
+ .slice(0, boundedLimit);
49
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ima2-gen",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "description": "Local OAuth image generation studio with classic and node workflows",
5
5
  "type": "module",
6
6
  "bin": {
@@ -18,7 +18,7 @@
18
18
  "test:package-install": "node --test tests/package-install-smoke.mjs",
19
19
  "setup": "node bin/ima2.js setup",
20
20
  "prepublishOnly": "npm test && npm run build && npm run test:package-install && npm run lint:pkg",
21
- "lint:pkg": "node -e \"const p=require('./package.json'); const req=['name','version','bin']; for(const k of req){if(!p[k])throw new Error('missing '+k)} const mustInclude=['bin/','lib/','routes/','server.js']; for(const f of mustInclude){if(!p.files.includes(f))throw new Error('files[] must include '+f)}\"",
21
+ "lint:pkg": "node -e \"const p=require('./package.json'); const req=['name','version','bin']; for(const k of req){if(!p[k])throw new Error('missing '+k)} const mustInclude=['bin/','lib/','routes/','integrations/','server.js']; for(const f of mustInclude){if(!p.files.includes(f))throw new Error('files[] must include '+f)}\"",
22
22
  "release:patch": "npm version patch && npm publish && git push origin main --tags",
23
23
  "release:minor": "npm version minor && npm publish && git push origin main --tags",
24
24
  "release:major": "npm version major && npm publish && git push origin main --tags"
@@ -39,6 +39,7 @@
39
39
  "bin/",
40
40
  "lib/",
41
41
  "routes/",
42
+ "integrations/",
42
43
  "ui/dist/",
43
44
  "docs/",
44
45
  "assets/",
@@ -0,0 +1,95 @@
1
+ import { getDb } from "../lib/db.js";
2
+
3
+ const MAX_ANNOTATION_PAYLOAD_CHARS = 256 * 1024;
4
+
5
+ function getBrowserId(req) {
6
+ const browserId = req.headers["x-ima2-browser-id"];
7
+ return typeof browserId === "string" && browserId.trim() ? browserId.trim() : null;
8
+ }
9
+
10
+ function isSafeFilename(filename) {
11
+ return (
12
+ typeof filename === "string" &&
13
+ filename.length > 0 &&
14
+ filename.length <= 240 &&
15
+ !filename.includes("..") &&
16
+ !filename.startsWith("/") &&
17
+ !filename.includes("\\")
18
+ );
19
+ }
20
+
21
+ function normalizePayload(value) {
22
+ const payload = value?.annotations ?? value;
23
+ if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
24
+ return { error: "annotations payload is required" };
25
+ }
26
+ const paths = Array.isArray(payload.paths) ? payload.paths : [];
27
+ const boxes = Array.isArray(payload.boxes) ? payload.boxes : [];
28
+ const memos = Array.isArray(payload.memos) ? payload.memos : [];
29
+ const normalized = { paths, boxes, memos };
30
+ const text = JSON.stringify(normalized);
31
+ if (text.length > MAX_ANNOTATION_PAYLOAD_CHARS) {
32
+ return { error: "annotations payload is too large" };
33
+ }
34
+ return { payload: normalized, text };
35
+ }
36
+
37
+ export function registerAnnotationRoutes(app) {
38
+ app.get("/api/annotations/:filename", (req, res) => {
39
+ try {
40
+ const browserId = getBrowserId(req);
41
+ const filename = decodeURIComponent(req.params.filename);
42
+ if (!browserId) return res.status(400).json({ error: "X-Ima2-Browser-Id header is required" });
43
+ if (!isSafeFilename(filename)) return res.status(400).json({ error: "invalid filename" });
44
+
45
+ const row = getDb()
46
+ .prepare("SELECT payload FROM image_annotations WHERE browser_id = ? AND filename = ?")
47
+ .get(browserId, filename);
48
+ const annotations = row ? JSON.parse(row.payload) : null;
49
+ res.json({ annotations });
50
+ } catch (err) {
51
+ res.status(500).json({ error: err.message });
52
+ }
53
+ });
54
+
55
+ app.put("/api/annotations/:filename", (req, res) => {
56
+ try {
57
+ const browserId = getBrowserId(req);
58
+ const filename = decodeURIComponent(req.params.filename);
59
+ if (!browserId) return res.status(400).json({ error: "X-Ima2-Browser-Id header is required" });
60
+ if (!isSafeFilename(filename)) return res.status(400).json({ error: "invalid filename" });
61
+
62
+ const normalized = normalizePayload(req.body);
63
+ if (normalized.error) return res.status(400).json({ error: normalized.error });
64
+
65
+ const id = `${browserId}:${filename}`;
66
+ getDb().prepare(`
67
+ INSERT INTO image_annotations (id, browser_id, filename, payload, schema_version, updated_at)
68
+ VALUES (?, ?, ?, ?, 1, unixepoch())
69
+ ON CONFLICT(browser_id, filename) DO UPDATE SET
70
+ payload = excluded.payload,
71
+ schema_version = excluded.schema_version,
72
+ updated_at = unixepoch()
73
+ `).run(id, browserId, filename, normalized.text);
74
+ res.json({ ok: true });
75
+ } catch (err) {
76
+ res.status(500).json({ error: err.message });
77
+ }
78
+ });
79
+
80
+ app.delete("/api/annotations/:filename", (req, res) => {
81
+ try {
82
+ const browserId = getBrowserId(req);
83
+ const filename = decodeURIComponent(req.params.filename);
84
+ if (!browserId) return res.status(400).json({ error: "X-Ima2-Browser-Id header is required" });
85
+ if (!isSafeFilename(filename)) return res.status(400).json({ error: "invalid filename" });
86
+
87
+ getDb()
88
+ .prepare("DELETE FROM image_annotations WHERE browser_id = ? AND filename = ?")
89
+ .run(browserId, filename);
90
+ res.json({ ok: true });
91
+ } catch (err) {
92
+ res.status(500).json({ error: err.message });
93
+ }
94
+ });
95
+ }
@@ -0,0 +1,64 @@
1
+ import express from "express";
2
+ import { createCanvasVersion, updateCanvasVersion } from "../lib/canvasVersionStore.js";
3
+
4
+ function decodeHeader(value) {
5
+ if (typeof value !== "string" || !value) return null;
6
+ try {
7
+ return decodeURIComponent(value);
8
+ } catch {
9
+ return value;
10
+ }
11
+ }
12
+
13
+ function getRequestBuffer(req) {
14
+ return Buffer.isBuffer(req.body) ? req.body : Buffer.alloc(0);
15
+ }
16
+
17
+ function getPrompt(req) {
18
+ return decodeHeader(req.headers["x-ima2-canvas-prompt"]);
19
+ }
20
+
21
+ export function registerCanvasVersionRoutes(app, ctx) {
22
+ const rawPng = express.raw({ type: "image/png", limit: ctx.config.server.bodyLimit });
23
+
24
+ app.post("/api/canvas-versions", rawPng, async (req, res) => {
25
+ try {
26
+ const sourceFilename =
27
+ typeof req.query.sourceFilename === "string"
28
+ ? req.query.sourceFilename
29
+ : decodeHeader(req.headers["x-ima2-canvas-source-filename"]);
30
+ const item = await createCanvasVersion(ctx, {
31
+ sourceFilename,
32
+ prompt: getPrompt(req),
33
+ buffer: getRequestBuffer(req),
34
+ });
35
+ res.status(201).json({ item });
36
+ } catch (err) {
37
+ res.status(err.status || 500).json({
38
+ error: err.message,
39
+ code: err.code || "CANVAS_VERSION_SAVE_FAILED",
40
+ });
41
+ }
42
+ });
43
+
44
+ app.put("/api/canvas-versions/:filename", rawPng, async (req, res) => {
45
+ try {
46
+ const filename = decodeURIComponent(req.params.filename);
47
+ const sourceFilename =
48
+ typeof req.query.sourceFilename === "string"
49
+ ? req.query.sourceFilename
50
+ : decodeHeader(req.headers["x-ima2-canvas-source-filename"]);
51
+ const item = await updateCanvasVersion(ctx, filename, {
52
+ sourceFilename,
53
+ prompt: getPrompt(req),
54
+ buffer: getRequestBuffer(req),
55
+ });
56
+ res.json({ item });
57
+ } catch (err) {
58
+ res.status(err.status || 500).json({
59
+ error: err.message,
60
+ code: err.code || "CANVAS_VERSION_SAVE_FAILED",
61
+ });
62
+ }
63
+ });
64
+ }
@@ -0,0 +1,39 @@
1
+ import { exportImageToComfy, isComfyBridgeError } from "../lib/comfyBridge.js";
2
+
3
+ const ALLOWED_BODY_KEYS = new Set(["filename"]);
4
+
5
+ function hasExactBodyShape(body) {
6
+ if (!body || typeof body !== "object" || Array.isArray(body)) return false;
7
+ const keys = Object.keys(body);
8
+ return keys.length === 1 && ALLOWED_BODY_KEYS.has(keys[0]) && typeof body.filename === "string";
9
+ }
10
+
11
+ function errorPayload(code, message) {
12
+ return {
13
+ ok: false,
14
+ error: { code, message },
15
+ };
16
+ }
17
+
18
+ export function registerComfyRoutes(app, ctx) {
19
+ app.post("/api/comfy/export-image", async (req, res) => {
20
+ try {
21
+ if (!hasExactBodyShape(req.body)) {
22
+ return res.status(400).json(errorPayload(
23
+ "COMFY_IMAGE_INVALID",
24
+ "Request body must contain exactly one filename.",
25
+ ));
26
+ }
27
+ const result = await exportImageToComfy(ctx, { filename: req.body.filename });
28
+ return res.json(result);
29
+ } catch (error) {
30
+ if (isComfyBridgeError(error)) {
31
+ return res.status(error.status).json(errorPayload(error.code, error.message));
32
+ }
33
+ return res.status(502).json(errorPayload(
34
+ "COMFY_UPLOAD_FAILED",
35
+ "Could not upload image to ComfyUI.",
36
+ ));
37
+ }
38
+ });
39
+ }
package/routes/edit.js CHANGED
@@ -4,9 +4,10 @@ import { randomBytes } from "crypto";
4
4
  import { editViaOAuth } from "../lib/oauthProxy.js";
5
5
  import { classifyUpstreamError } from "../lib/errorClassify.js";
6
6
  import { normalizeOAuthParams } from "../lib/oauthNormalize.js";
7
- import { normalizeImageModel } from "../lib/imageModels.js";
7
+ import { normalizeImageModel, normalizeReasoningEffort } from "../lib/imageModels.js";
8
8
  import { startJob, finishJob } from "../lib/inflight.js";
9
9
  import { logEvent, logError } from "../lib/logger.js";
10
+ import { hasPngAlphaChannel, parsePngInfo } from "../lib/pngInfo.js";
10
11
 
11
12
  function validateModeration(ctx, moderation) {
12
13
  if (typeof moderation !== "string" || !ctx.config.oauth.validModeration.has(moderation)) {
@@ -15,6 +16,49 @@ function validateModeration(ctx, moderation) {
15
16
  return { moderation };
16
17
  }
17
18
 
19
+ const MAX_EDIT_MASK_BYTES = 16 * 1024 * 1024;
20
+ const BASE64_RE = /^[A-Za-z0-9+/]+={0,2}$/;
21
+
22
+ function stripPngDataUrl(value) {
23
+ if (typeof value !== "string") return "";
24
+ return value.replace(/^data:image\/png;base64,/, "");
25
+ }
26
+
27
+ function decodePngDataUrl(value, invalidCode, pngCode) {
28
+ const b64 = stripPngDataUrl(value).replace(/\s+/g, "");
29
+ if (!b64 || b64.length % 4 !== 0 || !BASE64_RE.test(b64)) {
30
+ return { error: "image must be valid base64", code: invalidCode };
31
+ }
32
+ const buffer = Buffer.from(b64, "base64");
33
+ if (buffer.length === 0 || buffer.toString("base64").replace(/=+$/, "") !== b64.replace(/=+$/, "")) {
34
+ return { error: "image must be valid base64", code: invalidCode };
35
+ }
36
+ const info = parsePngInfo(buffer);
37
+ if (info.error) return { error: "image must be a PNG image", code: pngCode };
38
+ return { b64, buffer, info };
39
+ }
40
+
41
+ function validateEditMask(imageB64, mask) {
42
+ if (mask == null) return { mask: null, maskBytes: 0 };
43
+ if (typeof mask !== "string" || mask.length === 0) {
44
+ return { error: "mask must be a PNG data URL or base64 string", code: "INVALID_EDIT_MASK" };
45
+ }
46
+ const maskCheck = decodePngDataUrl(mask, "INVALID_EDIT_MASK_BASE64", "INVALID_EDIT_MASK_PNG");
47
+ if (maskCheck.error) return maskCheck;
48
+ if (maskCheck.buffer.length > MAX_EDIT_MASK_BYTES) {
49
+ return { error: "mask is too large", code: "EDIT_MASK_TOO_LARGE" };
50
+ }
51
+ if (!hasPngAlphaChannel(maskCheck.info)) {
52
+ return { error: "mask PNG must include an alpha channel", code: "EDIT_MASK_NO_ALPHA" };
53
+ }
54
+ const imageCheck = decodePngDataUrl(imageB64, "INVALID_EDIT_IMAGE_BASE64", "INVALID_EDIT_IMAGE_PNG");
55
+ if (imageCheck.error) return imageCheck;
56
+ if (imageCheck.info.width !== maskCheck.info.width || imageCheck.info.height !== maskCheck.info.height) {
57
+ return { error: "mask dimensions must match image dimensions", code: "EDIT_MASK_DIMENSION_MISMATCH" };
58
+ }
59
+ return { mask: maskCheck.b64, maskBytes: maskCheck.buffer.length };
60
+ }
61
+
18
62
  export function registerEditRoutes(app, ctx) {
19
63
  app.post("/api/edit", async (req, res) => {
20
64
  const requestId = typeof req.body?.requestId === "string" ? req.body.requestId : req.id;
@@ -26,12 +70,15 @@ export function registerEditRoutes(app, ctx) {
26
70
  const {
27
71
  prompt,
28
72
  image: imageB64,
73
+ mask: rawMask,
29
74
  quality: rawQuality = "medium",
30
75
  size = "1024x1024",
31
76
  moderation = "low",
32
77
  provider = "oauth",
33
78
  mode: promptMode = "auto",
34
79
  model: rawModel,
80
+ reasoningEffort: rawReasoningEffort,
81
+ webSearchEnabled: rawWebSearchEnabled = true,
35
82
  } = req.body;
36
83
  const { quality, warnings: qualityWarnings } = normalizeOAuthParams({ provider, quality: rawQuality });
37
84
  const modelCheck = normalizeImageModel(ctx, rawModel);
@@ -42,6 +89,15 @@ export function registerEditRoutes(app, ctx) {
42
89
  return res.status(modelCheck.status).json({ error: modelCheck.error, code: modelCheck.code });
43
90
  }
44
91
  const imageModel = modelCheck.model;
92
+ const reasoningCheck = normalizeReasoningEffort(ctx, rawReasoningEffort);
93
+ if (reasoningCheck.error) {
94
+ finishStatus = "error";
95
+ finishHttpStatus = reasoningCheck.status;
96
+ finishErrorCode = reasoningCheck.code;
97
+ return res.status(reasoningCheck.status).json({ error: reasoningCheck.error, code: reasoningCheck.code });
98
+ }
99
+ const reasoningEffort = reasoningCheck.effort;
100
+ const webSearchEnabled = rawWebSearchEnabled !== false;
45
101
  const sessionId = typeof req.body?.sessionId === "string" ? req.body.sessionId : null;
46
102
  const normalizedPromptMode = promptMode === "direct" ? "direct" : "auto";
47
103
 
@@ -64,6 +120,13 @@ export function registerEditRoutes(app, ctx) {
64
120
  finishErrorCode = "INVALID_EDIT_INPUT";
65
121
  return res.status(400).json({ error: "Prompt and image are required" });
66
122
  }
123
+ const maskCheck = validateEditMask(imageB64, rawMask);
124
+ if (maskCheck.error) {
125
+ finishStatus = "error";
126
+ finishHttpStatus = 400;
127
+ finishErrorCode = maskCheck.code;
128
+ return res.status(400).json({ error: maskCheck.error, code: maskCheck.code });
129
+ }
67
130
  const moderationCheck = validateModeration(ctx, moderation);
68
131
  if (moderationCheck.error) {
69
132
  finishStatus = "error";
@@ -89,10 +152,13 @@ export function registerEditRoutes(app, ctx) {
89
152
  sessionId,
90
153
  promptChars: typeof prompt === "string" ? prompt.length : 0,
91
154
  promptMode: normalizedPromptMode,
155
+ webSearchEnabled,
92
156
  inputImageChars: typeof imageB64 === "string" ? imageB64.length : 0,
157
+ maskPresent: Boolean(maskCheck.mask),
158
+ maskBytes: maskCheck.maskBytes ?? 0,
93
159
  });
94
160
  const startTime = Date.now();
95
- const { b64: resultB64, usage, revisedPrompt } = await editViaOAuth(
161
+ const { b64: resultB64, usage, revisedPrompt, webSearchCalls = 0 } = await editViaOAuth(
96
162
  prompt,
97
163
  imageB64,
98
164
  quality,
@@ -101,7 +167,7 @@ export function registerEditRoutes(app, ctx) {
101
167
  normalizedPromptMode,
102
168
  ctx,
103
169
  requestId,
104
- { model: imageModel },
170
+ { model: imageModel, reasoningEffort, webSearchEnabled, mask: maskCheck.mask },
105
171
  );
106
172
 
107
173
  const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
@@ -122,7 +188,8 @@ export function registerEditRoutes(app, ctx) {
122
188
  kind: "edit",
123
189
  createdAt: Date.now(),
124
190
  usage: usage || null,
125
- webSearchCalls: 0,
191
+ webSearchCalls,
192
+ webSearchEnabled,
126
193
  };
127
194
  await writeFile(join(ctx.config.storage.generatedDir, filename + ".json"), JSON.stringify(meta)).catch(() => {});
128
195
  finishHttpStatus = 200;
@@ -145,6 +212,8 @@ export function registerEditRoutes(app, ctx) {
145
212
  warnings: qualityWarnings,
146
213
  revisedPrompt: revisedPrompt || null,
147
214
  promptMode: normalizedPromptMode,
215
+ webSearchCalls,
216
+ webSearchEnabled,
148
217
  });
149
218
  } catch (err) {
150
219
  const fallbackCode = err.code || classifyUpstreamError(err.message);