ima2-gen 1.1.4 → 1.1.5
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/config.js +5 -0
- package/lib/db.js +41 -3
- package/lib/generationErrors.js +24 -0
- package/lib/historyList.js +14 -1
- package/lib/imageMetadata.js +107 -0
- package/lib/imageMetadataStore.js +67 -0
- package/lib/nodeStore.js +13 -1
- package/lib/oauthProxy.js +116 -12
- package/lib/refs.js +65 -2
- package/package.json +1 -1
- package/routes/generate.js +33 -2
- package/routes/history.js +42 -1
- package/routes/index.js +4 -0
- package/routes/metadata.js +71 -0
- package/routes/nodes.js +15 -1
- package/routes/prompts.js +379 -0
- package/ui/dist/assets/index-0SyTGr-u.js +25 -0
- package/ui/dist/assets/index-0SyTGr-u.js.map +1 -0
- package/ui/dist/assets/index-DfiV508Q.css +1 -0
- package/ui/dist/index.html +2 -2
- package/ui/dist/assets/index-DHeTnSPD.css +0 -1
- package/ui/dist/assets/index-fDTlOt4w.js +0 -23
- package/ui/dist/assets/index-fDTlOt4w.js.map +0 -1
package/config.js
CHANGED
|
@@ -74,6 +74,11 @@ export const config = {
|
|
|
74
74
|
},
|
|
75
75
|
limits: {
|
|
76
76
|
maxRefB64Bytes: pickInt(env.IMA2_MAX_REF_B64_BYTES, fileCfg.limits?.maxRefB64Bytes, 7 * 1024 * 1024),
|
|
77
|
+
maxMetadataReadB64Bytes: pickInt(
|
|
78
|
+
env.IMA2_MAX_METADATA_READ_B64_BYTES,
|
|
79
|
+
fileCfg.limits?.maxMetadataReadB64Bytes,
|
|
80
|
+
12 * 1024 * 1024,
|
|
81
|
+
),
|
|
77
82
|
maxRefCount: pickInt(env.IMA2_MAX_REF_COUNT, fileCfg.limits?.maxRefCount, 5),
|
|
78
83
|
maxParallel: pickInt(env.IMA2_MAX_PARALLEL, fileCfg.limits?.maxParallel, 8),
|
|
79
84
|
graphMaxNodes: pickInt(env.IMA2_GRAPH_MAX_NODES, fileCfg.limits?.graphMaxNodes, 500),
|
package/lib/db.js
CHANGED
|
@@ -96,12 +96,50 @@ function migrate(database) {
|
|
|
96
96
|
);
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
+
// ── Prompt Library (schema v4) ──
|
|
100
|
+
database.exec(`
|
|
101
|
+
CREATE TABLE IF NOT EXISTS prompt_folders (
|
|
102
|
+
id TEXT PRIMARY KEY,
|
|
103
|
+
parent_id TEXT NOT NULL,
|
|
104
|
+
name TEXT NOT NULL COLLATE NOCASE,
|
|
105
|
+
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
106
|
+
updated_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
107
|
+
UNIQUE(parent_id, name)
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
CREATE TABLE IF NOT EXISTS prompts (
|
|
111
|
+
id TEXT PRIMARY KEY,
|
|
112
|
+
folder_id TEXT NOT NULL DEFAULT '__root__',
|
|
113
|
+
name TEXT NOT NULL,
|
|
114
|
+
text TEXT NOT NULL,
|
|
115
|
+
tags TEXT,
|
|
116
|
+
mode TEXT,
|
|
117
|
+
is_favorite INTEGER NOT NULL DEFAULT 0,
|
|
118
|
+
favorited_at INTEGER,
|
|
119
|
+
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
120
|
+
updated_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
121
|
+
FOREIGN KEY (folder_id) REFERENCES prompt_folders(id) ON DELETE SET DEFAULT
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
CREATE TABLE IF NOT EXISTS gallery_favorites (
|
|
125
|
+
id TEXT PRIMARY KEY,
|
|
126
|
+
browser_id TEXT NOT NULL,
|
|
127
|
+
filename TEXT NOT NULL,
|
|
128
|
+
favorited_at INTEGER NOT NULL DEFAULT (unixepoch()),
|
|
129
|
+
UNIQUE(browser_id, filename)
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
INSERT OR IGNORE INTO prompt_folders (id, parent_id, name) VALUES
|
|
133
|
+
('__root__', '__root__', '__root__'),
|
|
134
|
+
('__trash__', '__root__', '__trash__');
|
|
135
|
+
`);
|
|
136
|
+
|
|
99
137
|
const row = database.prepare("SELECT value FROM _meta WHERE key = 'schema_version'").get();
|
|
100
138
|
if (!row) {
|
|
101
|
-
database.prepare("INSERT INTO _meta (key, value) VALUES ('schema_version', '
|
|
102
|
-
} else if (row.value !== "
|
|
139
|
+
database.prepare("INSERT INTO _meta (key, value) VALUES ('schema_version', '4')").run();
|
|
140
|
+
} else if (row.value !== "4") {
|
|
103
141
|
database
|
|
104
|
-
.prepare("UPDATE _meta SET value = '
|
|
142
|
+
.prepare("UPDATE _meta SET value = '4' WHERE key = 'schema_version'")
|
|
105
143
|
.run();
|
|
106
144
|
}
|
|
107
145
|
}
|
package/lib/generationErrors.js
CHANGED
|
@@ -12,6 +12,19 @@ const PASSTHROUGH_CODES = new Set([
|
|
|
12
12
|
|
|
13
13
|
const SAFETY_CODES = new Set(["SAFETY_REFUSAL", "MODERATION_REFUSED", "moderation_blocked"]);
|
|
14
14
|
|
|
15
|
+
function has4kSize(size) {
|
|
16
|
+
if (typeof size !== "string") return false;
|
|
17
|
+
const [w, h] = size.split("x").map((part) => Number(part));
|
|
18
|
+
return Number.isFinite(w) && Number.isFinite(h) && Math.max(w, h) >= 3840;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function diagnosticReasonFrom(err) {
|
|
22
|
+
if (typeof err?.diagnosticReason === "string" && err.diagnosticReason) return err.diagnosticReason;
|
|
23
|
+
if (Number(err?.referenceMismatchCount) > 0) return "reference_mime_mismatch_candidate";
|
|
24
|
+
if (has4kSize(err?.size)) return "experimental_4k_empty_response";
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
15
28
|
export function errorCodeFrom(err) {
|
|
16
29
|
if (!err) return "UNKNOWN";
|
|
17
30
|
const upstreamCode = classifyUpstreamErrorCode(err.upstreamCode);
|
|
@@ -86,6 +99,17 @@ export function normalizeGenerationFailure(lastErr, options = {}) {
|
|
|
86
99
|
if (lastErr.size) err.size = lastErr.size;
|
|
87
100
|
if (lastErr.quality) err.quality = lastErr.quality;
|
|
88
101
|
if (lastErr.model) err.model = lastErr.model;
|
|
102
|
+
if (typeof lastErr.eventCount === "number") err.eventCount = lastErr.eventCount;
|
|
103
|
+
if (lastErr.eventTypes) err.eventTypes = lastErr.eventTypes;
|
|
104
|
+
if (typeof lastErr.refsCount === "number") err.refsCount = lastErr.refsCount;
|
|
105
|
+
if (typeof lastErr.inputImageCount === "number") err.inputImageCount = lastErr.inputImageCount;
|
|
106
|
+
if (Array.isArray(lastErr.referenceDiagnostics)) err.referenceDiagnostics = lastErr.referenceDiagnostics;
|
|
107
|
+
if (typeof lastErr.referenceMismatchCount === "number") err.referenceMismatchCount = lastErr.referenceMismatchCount;
|
|
108
|
+
if (lastErr.retryKind) err.retryKind = lastErr.retryKind;
|
|
109
|
+
if (typeof lastErr.referencesDroppedOnRetry === "boolean") err.referencesDroppedOnRetry = lastErr.referencesDroppedOnRetry;
|
|
110
|
+
if (typeof lastErr.developerPromptDroppedOnRetry === "boolean") err.developerPromptDroppedOnRetry = lastErr.developerPromptDroppedOnRetry;
|
|
111
|
+
const diagnosticReason = diagnosticReasonFrom(lastErr);
|
|
112
|
+
if (diagnosticReason) err.diagnosticReason = diagnosticReason;
|
|
89
113
|
return err;
|
|
90
114
|
}
|
|
91
115
|
// Unrecognized errors → UNKNOWN (do not pretend they are safety refusals)
|
package/lib/historyList.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { mkdir, readFile, readdir, stat } from "fs/promises";
|
|
2
2
|
import { dirname, join } from "path";
|
|
3
3
|
import { config } from "../config.js";
|
|
4
|
+
import { readEmbeddedImageMetadataFromFile } from "./imageMetadataStore.js";
|
|
4
5
|
|
|
5
6
|
async function listImageFiles(baseDir) {
|
|
6
7
|
const out = [];
|
|
@@ -28,7 +29,7 @@ export async function listHistoryRows(baseDir = config.storage.generatedDir) {
|
|
|
28
29
|
const setRows = await listCardNewsSetRows(baseDir);
|
|
29
30
|
const rows = await Promise.all(imgs.map(async ({ full, rel, name }) => {
|
|
30
31
|
const st = await stat(full).catch(() => null);
|
|
31
|
-
const meta = await
|
|
32
|
+
const meta = await readImageMetadata(full, rel);
|
|
32
33
|
return {
|
|
33
34
|
filename: rel,
|
|
34
35
|
url: `/generated/${rel.split("/").map(encodeURIComponent).join("/")}`,
|
|
@@ -81,6 +82,18 @@ async function readImageSidecar(full, rel) {
|
|
|
81
82
|
return null;
|
|
82
83
|
}
|
|
83
84
|
|
|
85
|
+
async function readImageMetadata(full, rel) {
|
|
86
|
+
const sidecar = await readImageSidecar(full, rel);
|
|
87
|
+
if (sidecar) return sidecar;
|
|
88
|
+
try {
|
|
89
|
+
const embedded = await readEmbeddedImageMetadataFromFile(full);
|
|
90
|
+
return embedded.metadata;
|
|
91
|
+
} catch (e) {
|
|
92
|
+
if (e.code !== "ENOENT") console.warn("[history] embedded metadata read fail:", rel, e.message);
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
84
97
|
async function listCardNewsSetRows(baseDir) {
|
|
85
98
|
const root = join(baseDir, "cardnews");
|
|
86
99
|
const entries = await readdir(root, { withFileTypes: true }).catch(() => []);
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
export const IMA2_METADATA_SCHEMA = "ima2.generation.v1";
|
|
2
|
+
export const IMA2_XMP_NAMESPACE = "https://github.com/lidge-jun/ima2-gen/ns/1.0/";
|
|
3
|
+
export const IMA2_XMP_PROPERTY = "GenerationMetadata";
|
|
4
|
+
export const MAX_EMBEDDED_METADATA_CHARS = 64 * 1024;
|
|
5
|
+
|
|
6
|
+
function isPlainObject(value) {
|
|
7
|
+
return value != null && typeof value === "object" && !Array.isArray(value);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function stringOrNull(value, max = 4000) {
|
|
11
|
+
if (typeof value !== "string") return null;
|
|
12
|
+
return value.slice(0, max);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function numberOrNull(value) {
|
|
16
|
+
return Number.isFinite(value) ? value : null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function xmlEscape(value) {
|
|
20
|
+
return String(value)
|
|
21
|
+
.replace(/&/g, "&")
|
|
22
|
+
.replace(/"/g, """)
|
|
23
|
+
.replace(/</g, "<")
|
|
24
|
+
.replace(/>/g, ">");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function xmlUnescape(value) {
|
|
28
|
+
return String(value)
|
|
29
|
+
.replace(/"/g, "\"")
|
|
30
|
+
.replace(/</g, "<")
|
|
31
|
+
.replace(/>/g, ">")
|
|
32
|
+
.replace(/&/g, "&");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function buildIma2MetadataPayload(meta = {}, context = {}) {
|
|
36
|
+
const payload = {
|
|
37
|
+
schema: IMA2_METADATA_SCHEMA,
|
|
38
|
+
app: "ima2-gen",
|
|
39
|
+
version: stringOrNull(context.version, 80) || null,
|
|
40
|
+
createdAt: numberOrNull(meta.createdAt) || Date.now(),
|
|
41
|
+
kind: stringOrNull(meta.kind, 80),
|
|
42
|
+
prompt: stringOrNull(meta.prompt),
|
|
43
|
+
userPrompt: stringOrNull(meta.userPrompt) || stringOrNull(meta.prompt),
|
|
44
|
+
revisedPrompt: stringOrNull(meta.revisedPrompt),
|
|
45
|
+
promptMode: meta.promptMode === "direct" ? "direct" : "auto",
|
|
46
|
+
quality: stringOrNull(meta.quality, 40),
|
|
47
|
+
size: stringOrNull(meta.size, 40),
|
|
48
|
+
format: stringOrNull(meta.format, 20),
|
|
49
|
+
moderation: stringOrNull(meta.moderation, 40),
|
|
50
|
+
model: stringOrNull(meta.model, 80),
|
|
51
|
+
provider: stringOrNull(meta.provider, 40),
|
|
52
|
+
sessionId: stringOrNull(meta.sessionId, 120),
|
|
53
|
+
nodeId: stringOrNull(meta.nodeId, 120),
|
|
54
|
+
parentNodeId: stringOrNull(meta.parentNodeId, 120),
|
|
55
|
+
clientNodeId: stringOrNull(meta.clientNodeId, 120),
|
|
56
|
+
requestId: stringOrNull(meta.requestId, 160),
|
|
57
|
+
refsCount: Number.isFinite(meta.refsCount) ? meta.refsCount : 0,
|
|
58
|
+
webSearchCalls: Number.isFinite(meta.webSearchCalls) ? meta.webSearchCalls : 0,
|
|
59
|
+
styleSheetApplied: Boolean(meta.styleSheetApplied),
|
|
60
|
+
};
|
|
61
|
+
return Object.fromEntries(Object.entries(payload).filter(([, value]) => value !== undefined));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function normalizeEmbeddedMetadata(value) {
|
|
65
|
+
if (!isPlainObject(value)) return null;
|
|
66
|
+
if (value.schema !== IMA2_METADATA_SCHEMA || value.app !== "ima2-gen") return null;
|
|
67
|
+
return buildIma2MetadataPayload(value, { version: value.version });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function buildIma2Xmp(metadataPayload) {
|
|
71
|
+
const normalized = normalizeEmbeddedMetadata(metadataPayload);
|
|
72
|
+
if (!normalized) {
|
|
73
|
+
const err = new Error("Invalid ima2 metadata payload");
|
|
74
|
+
err.code = "IMAGE_METADATA_INVALID";
|
|
75
|
+
throw err;
|
|
76
|
+
}
|
|
77
|
+
const json = JSON.stringify(normalized);
|
|
78
|
+
if (json.length > MAX_EMBEDDED_METADATA_CHARS) {
|
|
79
|
+
const err = new Error("ima2 metadata payload is too large");
|
|
80
|
+
err.code = "IMAGE_METADATA_TOO_LARGE";
|
|
81
|
+
throw err;
|
|
82
|
+
}
|
|
83
|
+
const escaped = xmlEscape(json);
|
|
84
|
+
return [
|
|
85
|
+
"<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>",
|
|
86
|
+
"<x:xmpmeta xmlns:x=\"adobe:ns:meta/\">",
|
|
87
|
+
"<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">",
|
|
88
|
+
`<rdf:Description xmlns:ima2="${IMA2_XMP_NAMESPACE}" ima2:${IMA2_XMP_PROPERTY}="${escaped}"/>`,
|
|
89
|
+
"</rdf:RDF>",
|
|
90
|
+
"</x:xmpmeta>",
|
|
91
|
+
"<?xpacket end=\"w\"?>",
|
|
92
|
+
].join("");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function parseIma2Xmp(xmpString) {
|
|
96
|
+
if (typeof xmpString !== "string" || xmpString.length === 0) return null;
|
|
97
|
+
const attrPattern = new RegExp(`ima2:${IMA2_XMP_PROPERTY}="([^"]*)"`);
|
|
98
|
+
const attrMatch = attrPattern.exec(xmpString);
|
|
99
|
+
const raw = attrMatch?.[1];
|
|
100
|
+
if (!raw) return null;
|
|
101
|
+
try {
|
|
102
|
+
const parsed = JSON.parse(xmlUnescape(raw));
|
|
103
|
+
return normalizeEmbeddedMetadata(parsed);
|
|
104
|
+
} catch {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import sharp from "sharp";
|
|
3
|
+
import { buildIma2MetadataPayload, buildIma2Xmp, parseIma2Xmp } from "./imageMetadata.js";
|
|
4
|
+
|
|
5
|
+
const SUPPORTED_FORMATS = new Set(["png", "jpeg", "jpg", "webp"]);
|
|
6
|
+
|
|
7
|
+
export function normalizeImageMetadataFormat(format) {
|
|
8
|
+
const normalized = String(format || "").toLowerCase();
|
|
9
|
+
if (normalized === "jpg") return "jpeg";
|
|
10
|
+
return normalized;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function isSupportedMetadataFormat(format) {
|
|
14
|
+
return SUPPORTED_FORMATS.has(String(format || "").toLowerCase());
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function embedImageMetadata(buffer, format, metadata, context = {}) {
|
|
18
|
+
const normalizedFormat = normalizeImageMetadataFormat(format);
|
|
19
|
+
if (!isSupportedMetadataFormat(normalizedFormat)) {
|
|
20
|
+
const err = new Error(`Unsupported image metadata format: ${format}`);
|
|
21
|
+
err.code = "IMAGE_METADATA_UNSUPPORTED_FORMAT";
|
|
22
|
+
throw err;
|
|
23
|
+
}
|
|
24
|
+
const payload = buildIma2MetadataPayload(metadata, context);
|
|
25
|
+
const xmp = buildIma2Xmp(payload);
|
|
26
|
+
const next = await sharp(buffer)
|
|
27
|
+
.toFormat(normalizedFormat)
|
|
28
|
+
.withXmp(xmp)
|
|
29
|
+
.toBuffer();
|
|
30
|
+
return { buffer: next, embedded: true, metadata: payload };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function embedImageMetadataBestEffort(buffer, format, metadata, context = {}) {
|
|
34
|
+
try {
|
|
35
|
+
return await embedImageMetadata(buffer, format, metadata, context);
|
|
36
|
+
} catch (error) {
|
|
37
|
+
return {
|
|
38
|
+
buffer,
|
|
39
|
+
embedded: false,
|
|
40
|
+
warning: error?.message || "metadata embedding failed",
|
|
41
|
+
code: error?.code || "IMAGE_METADATA_EMBED_FAILED",
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function readEmbeddedImageMetadata(buffer) {
|
|
47
|
+
const meta = await sharp(buffer, { failOn: "none" }).metadata();
|
|
48
|
+
const xmpString = meta.xmpAsString || (meta.xmp ? meta.xmp.toString("utf8") : "");
|
|
49
|
+
const xmp = parseIma2Xmp(xmpString);
|
|
50
|
+
if (xmp) return { metadata: xmp, source: "xmp", warnings: [] };
|
|
51
|
+
|
|
52
|
+
for (const comment of meta.comments || []) {
|
|
53
|
+
const text = comment?.text || "";
|
|
54
|
+
const parsed = parseIma2Xmp(text);
|
|
55
|
+
if (parsed) return { metadata: parsed, source: "png-comment", warnings: [] };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
metadata: null,
|
|
60
|
+
source: null,
|
|
61
|
+
warnings: ["No ima2 metadata found in this image."],
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function readEmbeddedImageMetadataFromFile(path) {
|
|
66
|
+
return readEmbeddedImageMetadata(await readFile(path));
|
|
67
|
+
}
|
package/lib/nodeStore.js
CHANGED
|
@@ -2,6 +2,7 @@ import { writeFile, readFile, access, mkdir } from "fs/promises";
|
|
|
2
2
|
import { join, resolve, sep } from "path";
|
|
3
3
|
import { randomBytes } from "crypto";
|
|
4
4
|
import { config } from "../config.js";
|
|
5
|
+
import { embedImageMetadataBestEffort } from "./imageMetadataStore.js";
|
|
5
6
|
|
|
6
7
|
export function newNodeId() {
|
|
7
8
|
return "n_" + randomBytes(config.ids.nodeHexBytes).toString("hex");
|
|
@@ -11,7 +12,18 @@ export async function saveNode(rootDir, { nodeId, b64, meta, ext = "png", genera
|
|
|
11
12
|
void rootDir;
|
|
12
13
|
const filename = `${nodeId}.${ext}`;
|
|
13
14
|
await mkdir(generatedDir, { recursive: true });
|
|
14
|
-
|
|
15
|
+
const imageMeta = {
|
|
16
|
+
...meta,
|
|
17
|
+
kind: meta?.kind || "node",
|
|
18
|
+
nodeId: meta?.nodeId || nodeId,
|
|
19
|
+
format: meta?.format || ext,
|
|
20
|
+
};
|
|
21
|
+
const rawBuffer = Buffer.from(b64, "base64");
|
|
22
|
+
const embedded = await embedImageMetadataBestEffort(rawBuffer, ext, imageMeta);
|
|
23
|
+
if (!embedded.embedded) {
|
|
24
|
+
console.warn("[nodeStore] metadata embed skipped:", embedded.warning);
|
|
25
|
+
}
|
|
26
|
+
await writeFile(join(generatedDir, filename), embedded.buffer);
|
|
15
27
|
await writeFile(join(generatedDir, filename + ".json"), JSON.stringify(meta, null, 2));
|
|
16
28
|
return { filename };
|
|
17
29
|
}
|
package/lib/oauthProxy.js
CHANGED
|
@@ -3,6 +3,7 @@ import { config } from "../config.js";
|
|
|
3
3
|
import { logEvent } from "./logger.js";
|
|
4
4
|
import { classifyUpstreamError, classifyUpstreamErrorCode } from "./errorClassify.js";
|
|
5
5
|
import { compressReferenceB64ForOAuth } from "./referenceImageCompress.js";
|
|
6
|
+
import { detectImageMimeFromB64, safeReferenceDiagnostics } from "./refs.js";
|
|
6
7
|
|
|
7
8
|
const RESEARCH_SUFFIX = config.oauth.researchSuffix;
|
|
8
9
|
|
|
@@ -41,6 +42,51 @@ export function buildEditResearchTextPrompt(userPrompt, mode) {
|
|
|
41
42
|
return `Edit this image: ${userPrompt}${RESEARCH_SUFFIX}${AUTO_PROMPT_FIDELITY_SUFFIX}`;
|
|
42
43
|
}
|
|
43
44
|
|
|
45
|
+
function summarizeEventTypes(eventTypes = {}) {
|
|
46
|
+
const entries = Object.entries(eventTypes || {});
|
|
47
|
+
const countFor = (needle) =>
|
|
48
|
+
entries.reduce((sum, [key, value]) => sum + (key.includes(needle) && Number.isFinite(value) ? value : 0), 0);
|
|
49
|
+
return {
|
|
50
|
+
eventTypeCount: entries.length,
|
|
51
|
+
eventTypeKeys: entries.slice(0, 12).map(([key]) => key).join(","),
|
|
52
|
+
imageEventCount: countFor("image"),
|
|
53
|
+
partialEventCount: countFor("partial"),
|
|
54
|
+
completedEventCount: countFor("completed"),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function supportedImageMime(mime) {
|
|
59
|
+
return mime === "image/png" || mime === "image/jpeg" || mime === "image/webp";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function normalizeReferenceForOAuth(ref, index) {
|
|
63
|
+
const b64 = typeof ref === "string" ? ref : ref?.b64;
|
|
64
|
+
const declaredMime = typeof ref === "object" && ref ? ref.declaredMime || null : null;
|
|
65
|
+
const detectedMime = typeof ref === "object" && ref
|
|
66
|
+
? ref.detectedMime || detectImageMimeFromB64(b64)
|
|
67
|
+
: detectImageMimeFromB64(b64);
|
|
68
|
+
const warnings = Array.isArray(ref?.warnings) ? [...ref.warnings] : [];
|
|
69
|
+
if (declaredMime && detectedMime && declaredMime !== detectedMime && !warnings.includes("mime_mismatch")) {
|
|
70
|
+
warnings.push("mime_mismatch");
|
|
71
|
+
}
|
|
72
|
+
const requestMime = supportedImageMime(detectedMime)
|
|
73
|
+
? detectedMime
|
|
74
|
+
: supportedImageMime(declaredMime)
|
|
75
|
+
? declaredMime
|
|
76
|
+
: "image/png";
|
|
77
|
+
return {
|
|
78
|
+
index,
|
|
79
|
+
b64,
|
|
80
|
+
declaredMime,
|
|
81
|
+
detectedMime,
|
|
82
|
+
requestMime,
|
|
83
|
+
b64Chars: typeof b64 === "string" ? b64.length : 0,
|
|
84
|
+
approxBytes: Number.isFinite(ref?.approxBytes) ? ref.approxBytes : null,
|
|
85
|
+
source: ref?.source || (declaredMime ? "dataUrl" : "rawBase64"),
|
|
86
|
+
warnings,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
44
90
|
function getOAuthUrl(ctx = {}) {
|
|
45
91
|
return ctx.oauthUrl || `http://127.0.0.1:${config.oauth.proxyPort}`;
|
|
46
92
|
}
|
|
@@ -293,16 +339,29 @@ export async function generateViaOAuth(
|
|
|
293
339
|
];
|
|
294
340
|
|
|
295
341
|
const textPrompt = buildUserTextPrompt(prompt, mode);
|
|
296
|
-
const
|
|
342
|
+
const referenceInputs = references.map(normalizeReferenceForOAuth);
|
|
343
|
+
const referenceDiagnostics = safeReferenceDiagnostics(referenceInputs);
|
|
344
|
+
const referenceMismatchCount = referenceDiagnostics.filter((ref) => ref.warnings.includes("mime_mismatch")).length;
|
|
345
|
+
const userContent = referenceInputs.length
|
|
297
346
|
? [
|
|
298
|
-
...
|
|
347
|
+
...referenceInputs.map(({ b64, requestMime }) => ({
|
|
299
348
|
type: "input_image",
|
|
300
|
-
image_url: `data
|
|
349
|
+
image_url: `data:${requestMime};base64,${b64}`,
|
|
301
350
|
})),
|
|
302
351
|
{ type: "input_text", text: textPrompt },
|
|
303
352
|
]
|
|
304
353
|
: textPrompt;
|
|
305
354
|
|
|
355
|
+
if (referenceInputs.length > 0) {
|
|
356
|
+
logEvent("oauth", "reference_diagnostics", {
|
|
357
|
+
requestId,
|
|
358
|
+
refsCount: referenceInputs.length,
|
|
359
|
+
referenceMismatchCount,
|
|
360
|
+
refDetectedMimes: [...new Set(referenceDiagnostics.map((ref) => ref.detectedMime).filter(Boolean))].join(","),
|
|
361
|
+
refDeclaredMimes: [...new Set(referenceDiagnostics.map((ref) => ref.declaredMime).filter(Boolean))].join(","),
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
|
|
306
365
|
const res = await fetchOAuth(`${oauthUrl}/v1/responses`, {
|
|
307
366
|
method: "POST",
|
|
308
367
|
headers: { "Content-Type": "application/json", Accept: "text/event-stream" },
|
|
@@ -357,10 +416,20 @@ export async function generateViaOAuth(
|
|
|
357
416
|
scope: "oauth",
|
|
358
417
|
onPartialImage: options.onPartialImage,
|
|
359
418
|
});
|
|
360
|
-
logEvent("oauth", "stream_end", {
|
|
419
|
+
logEvent("oauth", "stream_end", {
|
|
420
|
+
requestId,
|
|
421
|
+
events: eventCount,
|
|
422
|
+
hasImage: !!imageB64,
|
|
423
|
+
...summarizeEventTypes(eventTypes),
|
|
424
|
+
});
|
|
361
425
|
|
|
362
426
|
if (!imageB64) {
|
|
363
|
-
logEvent("oauth", "retry_json", {
|
|
427
|
+
logEvent("oauth", "retry_json", {
|
|
428
|
+
requestId,
|
|
429
|
+
retryKind: "prompt_only",
|
|
430
|
+
referencesDroppedOnRetry: referenceInputs.length > 0,
|
|
431
|
+
developerPromptDroppedOnRetry: true,
|
|
432
|
+
});
|
|
364
433
|
const retryRes = await fetchOAuth(`${oauthUrl}/v1/responses`, {
|
|
365
434
|
method: "POST",
|
|
366
435
|
headers: { "Content-Type": "application/json" },
|
|
@@ -376,9 +445,23 @@ export async function generateViaOAuth(
|
|
|
376
445
|
const json = await retryRes.json();
|
|
377
446
|
for (const item of json.output || []) {
|
|
378
447
|
if (item.type === "image_generation_call" && item.result) {
|
|
379
|
-
logEvent("oauth", "retry_image", {
|
|
448
|
+
logEvent("oauth", "retry_image", {
|
|
449
|
+
requestId,
|
|
450
|
+
imageChars: item.result.length,
|
|
451
|
+
retryKind: "prompt_only",
|
|
452
|
+
referencesDroppedOnRetry: referenceInputs.length > 0,
|
|
453
|
+
});
|
|
380
454
|
const retryRevised = typeof item.revised_prompt === "string" ? item.revised_prompt : null;
|
|
381
|
-
return {
|
|
455
|
+
return {
|
|
456
|
+
b64: item.result,
|
|
457
|
+
usage: json.usage,
|
|
458
|
+
webSearchCalls,
|
|
459
|
+
revisedPrompt: retryRevised,
|
|
460
|
+
retryKind: "prompt_only",
|
|
461
|
+
referencesDroppedOnRetry: referenceInputs.length > 0,
|
|
462
|
+
developerPromptDroppedOnRetry: true,
|
|
463
|
+
initialEventCount: eventCount,
|
|
464
|
+
};
|
|
382
465
|
}
|
|
383
466
|
}
|
|
384
467
|
} else {
|
|
@@ -397,6 +480,13 @@ export async function generateViaOAuth(
|
|
|
397
480
|
emptyErr.size = size;
|
|
398
481
|
emptyErr.quality = quality;
|
|
399
482
|
emptyErr.model = model;
|
|
483
|
+
emptyErr.refsCount = referenceInputs.length;
|
|
484
|
+
emptyErr.inputImageCount = referenceInputs.length;
|
|
485
|
+
emptyErr.referenceDiagnostics = referenceDiagnostics;
|
|
486
|
+
emptyErr.referenceMismatchCount = referenceMismatchCount;
|
|
487
|
+
emptyErr.retryKind = "prompt_only";
|
|
488
|
+
emptyErr.referencesDroppedOnRetry = referenceInputs.length > 0;
|
|
489
|
+
emptyErr.developerPromptDroppedOnRetry = true;
|
|
400
490
|
throw emptyErr;
|
|
401
491
|
}
|
|
402
492
|
|
|
@@ -417,8 +507,8 @@ export async function editViaOAuth(prompt, imageB64, quality, size, moderation =
|
|
|
417
507
|
});
|
|
418
508
|
const references = Array.isArray(options.references) ? options.references : [];
|
|
419
509
|
const referenceImagesForRequest = await Promise.all(
|
|
420
|
-
references.map((
|
|
421
|
-
compressReferenceB64ForOAuth(b64, {
|
|
510
|
+
references.map((ref) =>
|
|
511
|
+
compressReferenceB64ForOAuth(typeof ref === "string" ? ref : ref?.b64, {
|
|
422
512
|
maxB64Bytes: ctx.config?.limits?.maxRefB64Bytes,
|
|
423
513
|
force: true,
|
|
424
514
|
}),
|
|
@@ -486,11 +576,25 @@ export async function editViaOAuth(prompt, imageB64, quality, size, moderation =
|
|
|
486
576
|
|
|
487
577
|
if (requestId) setJobPhase(requestId, "streaming");
|
|
488
578
|
|
|
489
|
-
const { imageB64: resultB64, usage, revisedPrompt, webSearchCalls } = await readImageStream(res, {
|
|
579
|
+
const { imageB64: resultB64, usage, revisedPrompt, webSearchCalls, eventCount, eventTypes } = await readImageStream(res, {
|
|
490
580
|
scope: "oauth-edit",
|
|
491
581
|
requestId,
|
|
492
582
|
});
|
|
493
|
-
logEvent("oauth-edit", "stream_end", {
|
|
583
|
+
logEvent("oauth-edit", "stream_end", {
|
|
584
|
+
requestId,
|
|
585
|
+
events: eventCount,
|
|
586
|
+
hasImage: !!resultB64,
|
|
587
|
+
...summarizeEventTypes(eventTypes),
|
|
588
|
+
});
|
|
494
589
|
if (resultB64) return { b64: resultB64, usage, revisedPrompt, webSearchCalls };
|
|
495
|
-
|
|
590
|
+
const emptyErr = new Error("No image data received from OAuth edit");
|
|
591
|
+
emptyErr.eventCount = eventCount;
|
|
592
|
+
emptyErr.eventTypes = eventTypes;
|
|
593
|
+
emptyErr.size = size;
|
|
594
|
+
emptyErr.quality = quality;
|
|
595
|
+
emptyErr.model = model;
|
|
596
|
+
emptyErr.refsCount = references.length;
|
|
597
|
+
emptyErr.inputImageCount = 1 + references.length;
|
|
598
|
+
emptyErr.parentImagePresent = true;
|
|
599
|
+
throw emptyErr;
|
|
496
600
|
}
|
package/lib/refs.js
CHANGED
|
@@ -4,6 +4,51 @@
|
|
|
4
4
|
import { config } from "../config.js";
|
|
5
5
|
|
|
6
6
|
const BASE64_RE = /^[A-Za-z0-9+/]+=*$/;
|
|
7
|
+
const DATA_URL_RE = /^data:([^;,]+);base64,/i;
|
|
8
|
+
|
|
9
|
+
function approxBase64Bytes(b64) {
|
|
10
|
+
try {
|
|
11
|
+
return Buffer.from(b64, "base64").length;
|
|
12
|
+
} catch {
|
|
13
|
+
return Math.floor((b64.length * 3) / 4);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function detectImageMimeFromB64(b64) {
|
|
18
|
+
let buf;
|
|
19
|
+
try {
|
|
20
|
+
buf = Buffer.from(b64, "base64");
|
|
21
|
+
} catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
if (buf.length >= 4 && buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4e && buf[3] === 0x47) {
|
|
25
|
+
return "image/png";
|
|
26
|
+
}
|
|
27
|
+
if (buf.length >= 3 && buf[0] === 0xff && buf[1] === 0xd8 && buf[2] === 0xff) {
|
|
28
|
+
return "image/jpeg";
|
|
29
|
+
}
|
|
30
|
+
if (
|
|
31
|
+
buf.length >= 12 &&
|
|
32
|
+
buf.toString("ascii", 0, 4) === "RIFF" &&
|
|
33
|
+
buf.toString("ascii", 8, 12) === "WEBP"
|
|
34
|
+
) {
|
|
35
|
+
return "image/webp";
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function safeReferenceDiagnostics(refDetails = []) {
|
|
41
|
+
if (!Array.isArray(refDetails)) return [];
|
|
42
|
+
return refDetails.map((ref) => ({
|
|
43
|
+
index: ref.index,
|
|
44
|
+
declaredMime: ref.declaredMime || null,
|
|
45
|
+
detectedMime: ref.detectedMime || null,
|
|
46
|
+
b64Chars: ref.b64Chars,
|
|
47
|
+
approxBytes: ref.approxBytes,
|
|
48
|
+
source: ref.source,
|
|
49
|
+
warnings: ref.warnings || [],
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
7
52
|
|
|
8
53
|
export function validateAndNormalizeRefs(references, {
|
|
9
54
|
maxCount = config.limits.maxRefCount,
|
|
@@ -16,12 +61,15 @@ export function validateAndNormalizeRefs(references, {
|
|
|
16
61
|
return { error: `references may not exceed ${maxCount} items`, code: "REF_TOO_MANY" };
|
|
17
62
|
}
|
|
18
63
|
const out = [];
|
|
64
|
+
const refDetails = [];
|
|
19
65
|
for (let i = 0; i < references.length; i++) {
|
|
20
66
|
const r = references[i];
|
|
21
67
|
if (typeof r !== "string") {
|
|
22
68
|
return { error: `references[${i}] must be a string`, code: "REF_NOT_STRING" };
|
|
23
69
|
}
|
|
24
|
-
const
|
|
70
|
+
const dataUrlMatch = r.match(DATA_URL_RE);
|
|
71
|
+
const declaredMime = dataUrlMatch?.[1]?.toLowerCase() || null;
|
|
72
|
+
const b64 = r.replace(DATA_URL_RE, "");
|
|
25
73
|
if (!b64) return { error: `references[${i}] is empty`, code: "REF_EMPTY" };
|
|
26
74
|
if (b64.length > maxB64Bytes) {
|
|
27
75
|
return { error: `references[${i}] exceeds ${maxB64Bytes} bytes`, code: "REF_TOO_LARGE" };
|
|
@@ -29,7 +77,22 @@ export function validateAndNormalizeRefs(references, {
|
|
|
29
77
|
if (!BASE64_RE.test(b64)) {
|
|
30
78
|
return { error: `references[${i}] is not valid base64`, code: "REF_NOT_BASE64" };
|
|
31
79
|
}
|
|
80
|
+
const detectedMime = detectImageMimeFromB64(b64);
|
|
81
|
+
const warnings = [];
|
|
82
|
+
if (declaredMime && detectedMime && declaredMime !== detectedMime) {
|
|
83
|
+
warnings.push("mime_mismatch");
|
|
84
|
+
}
|
|
32
85
|
out.push(b64);
|
|
86
|
+
refDetails.push({
|
|
87
|
+
index: i,
|
|
88
|
+
b64,
|
|
89
|
+
declaredMime,
|
|
90
|
+
detectedMime,
|
|
91
|
+
b64Chars: b64.length,
|
|
92
|
+
approxBytes: approxBase64Bytes(b64),
|
|
93
|
+
source: declaredMime ? "dataUrl" : "rawBase64",
|
|
94
|
+
warnings,
|
|
95
|
+
});
|
|
33
96
|
}
|
|
34
|
-
return { refs: out };
|
|
97
|
+
return { refs: out, refDetails, referenceDiagnostics: safeReferenceDiagnostics(refDetails) };
|
|
35
98
|
}
|