ima2-gen 1.1.8 → 1.1.9
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/README.md +56 -27
- package/bin/commands/annotate.js +137 -0
- package/bin/commands/annotate.ts +118 -0
- package/bin/commands/cancel.js +37 -33
- package/bin/commands/cancel.ts +45 -0
- package/bin/commands/canvas-versions.js +91 -0
- package/bin/commands/canvas-versions.ts +80 -0
- package/bin/commands/cardnews.js +293 -0
- package/bin/commands/cardnews.ts +248 -0
- package/bin/commands/comfy.js +63 -0
- package/bin/commands/comfy.ts +54 -0
- package/bin/commands/config.js +270 -0
- package/bin/commands/config.ts +265 -0
- package/bin/commands/edit.js +97 -72
- package/bin/commands/edit.ts +116 -0
- package/bin/commands/gen.js +140 -118
- package/bin/commands/gen.ts +176 -0
- package/bin/commands/history.js +164 -0
- package/bin/commands/history.ts +145 -0
- package/bin/commands/ls.js +60 -42
- package/bin/commands/ls.ts +60 -0
- package/bin/commands/metadata.js +45 -0
- package/bin/commands/metadata.ts +36 -0
- package/bin/commands/multimode.js +159 -0
- package/bin/commands/multimode.ts +146 -0
- package/bin/commands/node.js +176 -0
- package/bin/commands/node.ts +157 -0
- package/bin/commands/observability.js +201 -0
- package/bin/commands/observability.ts +176 -0
- package/bin/commands/ping.js +26 -20
- package/bin/commands/ping.ts +29 -0
- package/bin/commands/prompt.js +506 -0
- package/bin/commands/prompt.ts +421 -0
- package/bin/commands/ps.js +78 -71
- package/bin/commands/ps.ts +78 -0
- package/bin/commands/session.js +308 -0
- package/bin/commands/session.ts +265 -0
- package/bin/commands/show.js +75 -40
- package/bin/commands/show.ts +69 -0
- package/bin/ima2.js +324 -310
- package/bin/ima2.ts +444 -0
- package/bin/lib/args.js +75 -66
- package/bin/lib/args.ts +73 -0
- package/bin/lib/browser-id.js +15 -0
- package/bin/lib/browser-id.ts +16 -0
- package/bin/lib/client.js +91 -83
- package/bin/lib/client.ts +109 -0
- package/bin/lib/error-hints.js +14 -17
- package/bin/lib/error-hints.ts +23 -0
- package/bin/lib/files.js +26 -28
- package/bin/lib/files.ts +39 -0
- package/bin/lib/output.js +44 -42
- package/bin/lib/output.ts +58 -0
- package/bin/lib/platform.js +60 -56
- package/bin/lib/platform.ts +97 -0
- package/bin/lib/sse.js +73 -0
- package/bin/lib/sse.ts +73 -0
- package/bin/lib/star-prompt.js +69 -76
- package/bin/lib/star-prompt.ts +97 -0
- package/bin/lib/storage-doctor.js +34 -35
- package/bin/lib/storage-doctor.ts +38 -0
- package/config.js +147 -243
- package/config.ts +331 -0
- package/docs/API.md +48 -8
- package/docs/CLI.md +190 -0
- package/docs/FAQ.ko.md +5 -5
- package/docs/FAQ.md +5 -5
- package/docs/README.ja.md +71 -25
- package/docs/README.ko.md +61 -24
- package/docs/README.zh-CN.md +73 -27
- package/lib/assetLifecycle.js +130 -130
- package/lib/assetLifecycle.ts +142 -0
- package/lib/canvasVersionStore.js +135 -153
- package/lib/canvasVersionStore.ts +181 -0
- package/lib/cardNewsGenerator.js +127 -142
- package/lib/cardNewsGenerator.ts +162 -0
- package/lib/cardNewsJobStore.js +78 -84
- package/lib/cardNewsJobStore.ts +107 -0
- package/lib/cardNewsManifestStore.js +88 -93
- package/lib/cardNewsManifestStore.ts +112 -0
- package/lib/cardNewsPlanner.js +157 -152
- package/lib/cardNewsPlanner.ts +180 -0
- package/lib/cardNewsPlannerClient.js +101 -98
- package/lib/cardNewsPlannerClient.ts +114 -0
- package/lib/cardNewsPlannerPrompt.js +56 -56
- package/lib/cardNewsPlannerPrompt.ts +60 -0
- package/lib/cardNewsPlannerSchema.js +231 -223
- package/lib/cardNewsPlannerSchema.ts +259 -0
- package/lib/cardNewsRoleTemplateStore.js +39 -41
- package/lib/cardNewsRoleTemplateStore.ts +47 -0
- package/lib/cardNewsTemplateStore.js +171 -175
- package/lib/cardNewsTemplateStore.ts +210 -0
- package/lib/codexDetect.js +44 -47
- package/lib/codexDetect.ts +69 -0
- package/lib/comfyBridge.js +164 -184
- package/lib/comfyBridge.ts +214 -0
- package/lib/db.js +41 -51
- package/lib/db.ts +166 -0
- package/lib/errorClassify.js +62 -78
- package/lib/errorClassify.ts +100 -0
- package/lib/generationErrors.js +140 -103
- package/lib/generationErrors.ts +125 -0
- package/lib/historyList.js +149 -147
- package/lib/historyList.ts +164 -0
- package/lib/imageMetadata.js +86 -89
- package/lib/imageMetadata.ts +111 -0
- package/lib/imageMetadataStore.js +46 -51
- package/lib/imageMetadataStore.ts +67 -0
- package/lib/imageModels.js +38 -45
- package/lib/imageModels.ts +52 -0
- package/lib/inflight.js +131 -150
- package/lib/inflight.ts +204 -0
- package/lib/localImportStore.js +87 -93
- package/lib/localImportStore.ts +111 -0
- package/lib/logger.js +105 -112
- package/lib/logger.ts +150 -0
- package/lib/nodeStore.js +65 -64
- package/lib/nodeStore.ts +81 -0
- package/lib/oauthLauncher.js +61 -59
- package/lib/oauthLauncher.ts +64 -0
- package/lib/oauthNormalize.js +15 -19
- package/lib/oauthNormalize.ts +30 -0
- package/lib/oauthProxy.js +834 -832
- package/lib/oauthProxy.ts +995 -0
- package/lib/openDirectory.js +41 -40
- package/lib/openDirectory.ts +45 -0
- package/lib/pngInfo.js +18 -20
- package/lib/pngInfo.ts +26 -0
- package/lib/promptImport/curatedSources.js +125 -129
- package/lib/promptImport/curatedSources.ts +139 -0
- package/lib/promptImport/discoveryRegistry.js +185 -203
- package/lib/promptImport/discoveryRegistry.ts +236 -0
- package/lib/promptImport/errors.js +10 -10
- package/lib/promptImport/errors.ts +18 -0
- package/lib/promptImport/githubDiscovery.js +209 -219
- package/lib/promptImport/githubDiscovery.ts +248 -0
- package/lib/promptImport/githubFolder.js +253 -259
- package/lib/promptImport/githubFolder.ts +308 -0
- package/lib/promptImport/githubSource.js +189 -200
- package/lib/promptImport/githubSource.ts +239 -0
- package/lib/promptImport/gptImageHints.js +49 -56
- package/lib/promptImport/gptImageHints.ts +68 -0
- package/lib/promptImport/parsePromptCandidates.js +108 -123
- package/lib/promptImport/parsePromptCandidates.ts +153 -0
- package/lib/promptImport/promptIndex.js +190 -208
- package/lib/promptImport/promptIndex.ts +248 -0
- package/lib/promptImport/rankPromptCandidates.js +46 -43
- package/lib/promptImport/rankPromptCandidates.ts +49 -0
- package/lib/providerOptions.js +31 -0
- package/lib/providerOptions.ts +41 -0
- package/lib/referenceImageCompress.js +51 -62
- package/lib/referenceImageCompress.ts +75 -0
- package/lib/refs.js +93 -81
- package/lib/refs.ts +117 -0
- package/lib/requestLogger.js +32 -38
- package/lib/requestLogger.ts +48 -0
- package/lib/responsesImageAdapter.js +351 -0
- package/lib/responsesImageAdapter.ts +352 -0
- package/lib/runtimePorts.js +71 -73
- package/lib/runtimePorts.ts +93 -0
- package/lib/sessionStore.js +179 -230
- package/lib/sessionStore.ts +272 -0
- package/lib/storageMigration.js +247 -245
- package/lib/storageMigration.ts +284 -0
- package/lib/styleSheet.js +86 -90
- package/lib/styleSheet.ts +128 -0
- package/lib/systemTrash.js +18 -0
- package/lib/systemTrash.ts +20 -0
- package/package.json +26 -10
- package/routes/annotations.js +76 -79
- package/routes/annotations.ts +95 -0
- package/routes/canvasVersions.js +50 -54
- package/routes/canvasVersions.ts +64 -0
- package/routes/cardNews.js +158 -171
- package/routes/cardNews.ts +183 -0
- package/routes/comfy.js +23 -31
- package/routes/comfy.ts +39 -0
- package/routes/edit.js +183 -214
- package/routes/edit.ts +230 -0
- package/routes/generate.js +269 -291
- package/routes/generate.ts +309 -0
- package/routes/health.js +102 -107
- package/routes/health.ts +114 -0
- package/routes/history.js +136 -144
- package/routes/history.ts +153 -0
- package/routes/imageImport.js +27 -27
- package/routes/imageImport.ts +33 -0
- package/routes/index.js +17 -17
- package/routes/index.ts +35 -0
- package/routes/metadata.js +60 -64
- package/routes/metadata.ts +71 -0
- package/routes/multimode.js +228 -263
- package/routes/multimode.ts +280 -0
- package/routes/nodes.js +378 -424
- package/routes/nodes.ts +455 -0
- package/routes/promptImport.js +284 -324
- package/routes/promptImport.ts +354 -0
- package/routes/prompts.js +333 -360
- package/routes/prompts.ts +379 -0
- package/routes/sessions.js +277 -285
- package/routes/sessions.ts +292 -0
- package/routes/storage.js +29 -31
- package/routes/storage.ts +39 -0
- package/server.js +189 -196
- package/server.ts +235 -0
- package/ui/dist/.vite/manifest.json +101 -0
- package/ui/dist/assets/CardNewsWorkspace-BJOCey7Z.js +2 -0
- package/ui/dist/assets/NodeCanvas-BZV40eAE.css +1 -0
- package/ui/dist/assets/NodeCanvas-C3dzYNsk.js +7 -0
- package/ui/dist/assets/PromptImportDialog-Dqu1VpUh.js +2 -0
- package/ui/dist/assets/PromptImportDiscoverySection-Dg8T9X0L.js +1 -0
- package/ui/dist/assets/PromptImportFolderSection-DBaqsFO4.js +1 -0
- package/ui/dist/assets/PromptLibraryPanel-p5QqR97M.js +2 -0
- package/ui/dist/assets/SettingsWorkspace-B5bSAZ6u.js +1 -0
- package/ui/dist/assets/index-C9cXwiWE.js +25 -0
- package/ui/dist/assets/index-CGMIkZXn.css +1 -0
- package/ui/dist/assets/index-Cvld7dUZ.js +1 -0
- package/ui/dist/index.html +2 -2
- package/assets/phase-a-bg-cleanup-test.png +0 -0
- package/assets/screenshot.png +0 -0
- package/assets/screenshots/classic-generate-light.png +0 -0
- package/assets/screenshots/node-graph-branching.png +0 -0
- package/assets/screenshots/settings-oauth-generation.png +0 -0
- package/assets/screenshots/settings-workspace.png +0 -0
- package/assets/screenshots/style-sheet-editor.png +0 -0
- package/integrations/comfyui/ima2_gen_bridge/__pycache__/__init__.cpython-313.pyc +0 -0
- package/integrations/comfyui/ima2_gen_bridge/__pycache__/nodes.cpython-313.pyc +0 -0
- package/ui/dist/assets/index-BDffwmLs.css +0 -1
- package/ui/dist/assets/index-D0fdHLkJ.js +0 -31
- package/ui/dist/assets/index-D0fdHLkJ.js.map +0 -1
package/routes/history.js
CHANGED
|
@@ -3,151 +3,143 @@ import { trashAsset, restoreAsset, deleteAssetPermanent } from "../lib/assetLife
|
|
|
3
3
|
import { getSessionTitleMap } from "../lib/sessionStore.js";
|
|
4
4
|
import { logError, logEvent } from "../lib/logger.js";
|
|
5
5
|
import { getDb } from "../lib/db.js";
|
|
6
|
-
|
|
7
6
|
export function registerHistoryRoutes(app, ctx) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
let favoriteSet = new Set();
|
|
26
|
-
if (browserId) {
|
|
27
|
-
const db = getDb();
|
|
28
|
-
const favRows = db.prepare("SELECT filename FROM gallery_favorites WHERE browser_id = ?").all(browserId);
|
|
29
|
-
favoriteSet = new Set(favRows.map((r) => r.filename));
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
let filtered = rows;
|
|
33
|
-
if (Number.isFinite(sinceTs)) {
|
|
34
|
-
filtered = filtered.filter((r) => r.createdAt > sinceTs);
|
|
35
|
-
}
|
|
36
|
-
if (Number.isFinite(beforeTs)) {
|
|
37
|
-
filtered = filtered.filter((r) => {
|
|
38
|
-
if (r.createdAt < beforeTs) return true;
|
|
39
|
-
if (r.createdAt === beforeTs && beforeFn) return r.filename < beforeFn;
|
|
40
|
-
return false;
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
if (sessionId) {
|
|
44
|
-
filtered = filtered.filter((r) => r.sessionId === sessionId);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const page = filtered.slice(0, limit).map((r) => ({ ...r, isFavorite: favoriteSet.has(r.filename) }));
|
|
48
|
-
const nextCursor = page.length === limit && filtered.length > limit
|
|
49
|
-
? { before: page[page.length - 1].createdAt, beforeFilename: page[page.length - 1].filename }
|
|
50
|
-
: null;
|
|
51
|
-
|
|
52
|
-
if (groupBy === "session") {
|
|
53
|
-
const groups = new Map();
|
|
54
|
-
const loose = [];
|
|
55
|
-
for (const row of page) {
|
|
56
|
-
if (row.sessionId) {
|
|
57
|
-
let group = groups.get(row.sessionId);
|
|
58
|
-
if (!group) {
|
|
59
|
-
group = { sessionId: row.sessionId, items: [], lastUsedAt: row.createdAt };
|
|
60
|
-
groups.set(row.sessionId, group);
|
|
7
|
+
app.get("/api/history", async (req, res) => {
|
|
8
|
+
try {
|
|
9
|
+
const limitRaw = parseInt(req.query.limit);
|
|
10
|
+
const limit = Math.min(Number.isFinite(limitRaw) && limitRaw > 0 ? limitRaw : ctx.config.history.defaultPageSize, ctx.config.history.maxPageCap);
|
|
11
|
+
const beforeTs = parseInt(req.query.before);
|
|
12
|
+
const beforeFn = typeof req.query.beforeFilename === "string" ? req.query.beforeFilename : null;
|
|
13
|
+
const sinceTs = parseInt(req.query.since);
|
|
14
|
+
const sessionId = typeof req.query.sessionId === "string" ? req.query.sessionId : null;
|
|
15
|
+
const groupBy = req.query.groupBy === "session" ? "session" : null;
|
|
16
|
+
const browserId = req.headers["x-ima2-browser-id"] || null;
|
|
17
|
+
const rows = await listHistoryRows(ctx.config.storage.generatedDir);
|
|
18
|
+
// Enrich with favorite status
|
|
19
|
+
let favoriteSet = new Set();
|
|
20
|
+
if (browserId) {
|
|
21
|
+
const db = getDb();
|
|
22
|
+
const favRows = db.prepare("SELECT filename FROM gallery_favorites WHERE browser_id = ?").all(browserId);
|
|
23
|
+
favoriteSet = new Set(favRows.map((r) => r.filename));
|
|
61
24
|
}
|
|
62
|
-
|
|
63
|
-
if (
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
25
|
+
let filtered = rows;
|
|
26
|
+
if (Number.isFinite(sinceTs)) {
|
|
27
|
+
filtered = filtered.filter((r) => r.createdAt > sinceTs);
|
|
28
|
+
}
|
|
29
|
+
if (Number.isFinite(beforeTs)) {
|
|
30
|
+
filtered = filtered.filter((r) => {
|
|
31
|
+
if (r.createdAt < beforeTs)
|
|
32
|
+
return true;
|
|
33
|
+
if (r.createdAt === beforeTs && beforeFn)
|
|
34
|
+
return r.filename < beforeFn;
|
|
35
|
+
return false;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
if (sessionId) {
|
|
39
|
+
filtered = filtered.filter((r) => r.sessionId === sessionId);
|
|
40
|
+
}
|
|
41
|
+
const page = filtered.slice(0, limit).map((r) => ({ ...r, isFavorite: favoriteSet.has(r.filename) }));
|
|
42
|
+
const nextCursor = page.length === limit && filtered.length > limit
|
|
43
|
+
? { before: page[page.length - 1].createdAt, beforeFilename: page[page.length - 1].filename }
|
|
44
|
+
: null;
|
|
45
|
+
if (groupBy === "session") {
|
|
46
|
+
const groups = new Map();
|
|
47
|
+
const loose = [];
|
|
48
|
+
for (const row of page) {
|
|
49
|
+
if (row.sessionId) {
|
|
50
|
+
let group = groups.get(row.sessionId);
|
|
51
|
+
if (!group) {
|
|
52
|
+
group = { sessionId: row.sessionId, items: [], lastUsedAt: row.createdAt };
|
|
53
|
+
groups.set(row.sessionId, group);
|
|
54
|
+
}
|
|
55
|
+
group.items.push(row);
|
|
56
|
+
if (row.createdAt > group.lastUsedAt)
|
|
57
|
+
group.lastUsedAt = row.createdAt;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
loose.push(row);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const titleMap = getSessionTitleMap(Array.from(groups.keys()));
|
|
64
|
+
const sessions = Array.from(groups.values())
|
|
65
|
+
.map((group) => ({
|
|
66
|
+
...group,
|
|
67
|
+
title: titleMap.get(group.sessionId) || null,
|
|
68
|
+
label: titleMap.get(group.sessionId) || group.sessionId.slice(0, 8),
|
|
69
|
+
}))
|
|
70
|
+
.sort((a, b) => b.lastUsedAt - a.lastUsedAt);
|
|
71
|
+
logEvent("history", "grouped", {
|
|
72
|
+
sessions: sessions.length,
|
|
73
|
+
loose: loose.length,
|
|
74
|
+
total: rows.length,
|
|
75
|
+
});
|
|
76
|
+
return res.json({ sessions, loose, total: rows.length, nextCursor });
|
|
77
|
+
}
|
|
78
|
+
res.json({ items: page, total: rows.length, nextCursor });
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
logError("history", "error", err);
|
|
82
|
+
res.status(500).json({ error: err.message });
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
app.delete("/api/history/:filename/permanent", async (req, res) => {
|
|
86
|
+
try {
|
|
87
|
+
const filename = decodeURIComponent(req.params.filename);
|
|
88
|
+
const result = await deleteAssetPermanent(ctx.rootDir, filename);
|
|
89
|
+
res.json(result);
|
|
90
|
+
}
|
|
91
|
+
catch (err) {
|
|
92
|
+
res.status(err.status || 500).json({ error: err.message, code: err.code });
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
app.delete("/api/history/:filename", async (req, res) => {
|
|
96
|
+
try {
|
|
97
|
+
const filename = decodeURIComponent(req.params.filename);
|
|
98
|
+
const result = await trashAsset(ctx.rootDir, filename);
|
|
99
|
+
res.json(result);
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
res.status(err.status || 500).json({ error: err.message, code: err.code });
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
app.post("/api/history/:filename/restore", async (req, res) => {
|
|
106
|
+
try {
|
|
107
|
+
const filename = decodeURIComponent(req.params.filename);
|
|
108
|
+
const trashId = typeof req.body?.trashId === "string" ? req.body.trashId : null;
|
|
109
|
+
if (!trashId)
|
|
110
|
+
return res.status(400).json({ error: "trashId required" });
|
|
111
|
+
const result = await restoreAsset(ctx.rootDir, trashId, filename);
|
|
112
|
+
res.json(result);
|
|
113
|
+
}
|
|
114
|
+
catch (err) {
|
|
115
|
+
res.status(err.status || 500).json({ error: err.message });
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
app.post("/api/history/favorite", async (req, res) => {
|
|
119
|
+
try {
|
|
120
|
+
const db = getDb();
|
|
121
|
+
const { filename } = req.body || {};
|
|
122
|
+
const browserId = req.headers["x-ima2-browser-id"];
|
|
123
|
+
if (!filename || typeof filename !== "string") {
|
|
124
|
+
return res.status(400).json({ error: "filename is required" });
|
|
125
|
+
}
|
|
126
|
+
if (!browserId || typeof browserId !== "string") {
|
|
127
|
+
return res.status(400).json({ error: "X-Ima2-Browser-Id header is required" });
|
|
128
|
+
}
|
|
129
|
+
const existing = db.prepare("SELECT id FROM gallery_favorites WHERE browser_id = ? AND filename = ?").get(browserId, filename);
|
|
130
|
+
if (existing) {
|
|
131
|
+
db.prepare("DELETE FROM gallery_favorites WHERE browser_id = ? AND filename = ?").run(browserId, filename);
|
|
132
|
+
res.json({ isFavorite: false });
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
const id = `${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
|
|
136
|
+
db.prepare("INSERT INTO gallery_favorites (id, browser_id, filename, favorited_at) VALUES (?, ?, ?, ?)").run(id, browserId, filename, Math.floor(Date.now() / 1000));
|
|
137
|
+
res.json({ isFavorite: true });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
logError("history", "favorite_error", err);
|
|
142
|
+
res.status(500).json({ error: err.message });
|
|
67
143
|
}
|
|
68
|
-
|
|
69
|
-
const sessions = Array.from(groups.values())
|
|
70
|
-
.map((group) => ({
|
|
71
|
-
...group,
|
|
72
|
-
title: titleMap.get(group.sessionId) || null,
|
|
73
|
-
label: titleMap.get(group.sessionId) || group.sessionId.slice(0, 8),
|
|
74
|
-
}))
|
|
75
|
-
.sort((a, b) => b.lastUsedAt - a.lastUsedAt);
|
|
76
|
-
logEvent("history", "grouped", {
|
|
77
|
-
sessions: sessions.length,
|
|
78
|
-
loose: loose.length,
|
|
79
|
-
total: rows.length,
|
|
80
|
-
});
|
|
81
|
-
return res.json({ sessions, loose, total: rows.length, nextCursor });
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
res.json({ items: page, total: rows.length, nextCursor });
|
|
85
|
-
} catch (err) {
|
|
86
|
-
logError("history", "error", err);
|
|
87
|
-
res.status(500).json({ error: err.message });
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
app.delete("/api/history/:filename/permanent", async (req, res) => {
|
|
92
|
-
try {
|
|
93
|
-
const filename = decodeURIComponent(req.params.filename);
|
|
94
|
-
const result = await deleteAssetPermanent(ctx.rootDir, filename);
|
|
95
|
-
res.json(result);
|
|
96
|
-
} catch (err) {
|
|
97
|
-
res.status(err.status || 500).json({ error: err.message, code: err.code });
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
app.delete("/api/history/:filename", async (req, res) => {
|
|
102
|
-
try {
|
|
103
|
-
const filename = decodeURIComponent(req.params.filename);
|
|
104
|
-
const result = await trashAsset(ctx.rootDir, filename);
|
|
105
|
-
res.json(result);
|
|
106
|
-
} catch (err) {
|
|
107
|
-
res.status(err.status || 500).json({ error: err.message, code: err.code });
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
app.post("/api/history/:filename/restore", async (req, res) => {
|
|
112
|
-
try {
|
|
113
|
-
const filename = decodeURIComponent(req.params.filename);
|
|
114
|
-
const trashId = typeof req.body?.trashId === "string" ? req.body.trashId : null;
|
|
115
|
-
if (!trashId) return res.status(400).json({ error: "trashId required" });
|
|
116
|
-
const result = await restoreAsset(ctx.rootDir, trashId, filename);
|
|
117
|
-
res.json(result);
|
|
118
|
-
} catch (err) {
|
|
119
|
-
res.status(err.status || 500).json({ error: err.message });
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
app.post("/api/history/favorite", async (req, res) => {
|
|
124
|
-
try {
|
|
125
|
-
const db = getDb();
|
|
126
|
-
const { filename } = req.body || {};
|
|
127
|
-
const browserId = req.headers["x-ima2-browser-id"];
|
|
128
|
-
|
|
129
|
-
if (!filename || typeof filename !== "string") {
|
|
130
|
-
return res.status(400).json({ error: "filename is required" });
|
|
131
|
-
}
|
|
132
|
-
if (!browserId || typeof browserId !== "string") {
|
|
133
|
-
return res.status(400).json({ error: "X-Ima2-Browser-Id header is required" });
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const existing = db.prepare("SELECT id FROM gallery_favorites WHERE browser_id = ? AND filename = ?").get(browserId, filename);
|
|
137
|
-
|
|
138
|
-
if (existing) {
|
|
139
|
-
db.prepare("DELETE FROM gallery_favorites WHERE browser_id = ? AND filename = ?").run(browserId, filename);
|
|
140
|
-
res.json({ isFavorite: false });
|
|
141
|
-
} else {
|
|
142
|
-
const id = `${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
|
|
143
|
-
db.prepare(
|
|
144
|
-
"INSERT INTO gallery_favorites (id, browser_id, filename, favorited_at) VALUES (?, ?, ?, ?)"
|
|
145
|
-
).run(id, browserId, filename, Math.floor(Date.now() / 1000));
|
|
146
|
-
res.json({ isFavorite: true });
|
|
147
|
-
}
|
|
148
|
-
} catch (err) {
|
|
149
|
-
logError("history", "favorite_error", err);
|
|
150
|
-
res.status(500).json({ error: err.message });
|
|
151
|
-
}
|
|
152
|
-
});
|
|
144
|
+
});
|
|
153
145
|
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { listHistoryRows } from "../lib/historyList.js";
|
|
2
|
+
import { trashAsset, restoreAsset, deleteAssetPermanent } from "../lib/assetLifecycle.js";
|
|
3
|
+
import { getSessionTitleMap } from "../lib/sessionStore.js";
|
|
4
|
+
import { logError, logEvent } from "../lib/logger.js";
|
|
5
|
+
import { getDb } from "../lib/db.js";
|
|
6
|
+
|
|
7
|
+
export function registerHistoryRoutes(app, ctx) {
|
|
8
|
+
app.get("/api/history", async (req, res) => {
|
|
9
|
+
try {
|
|
10
|
+
const limitRaw = parseInt(req.query.limit);
|
|
11
|
+
const limit = Math.min(
|
|
12
|
+
Number.isFinite(limitRaw) && limitRaw > 0 ? limitRaw : ctx.config.history.defaultPageSize,
|
|
13
|
+
ctx.config.history.maxPageCap,
|
|
14
|
+
);
|
|
15
|
+
const beforeTs = parseInt(req.query.before);
|
|
16
|
+
const beforeFn = typeof req.query.beforeFilename === "string" ? req.query.beforeFilename : null;
|
|
17
|
+
const sinceTs = parseInt(req.query.since);
|
|
18
|
+
const sessionId = typeof req.query.sessionId === "string" ? req.query.sessionId : null;
|
|
19
|
+
const groupBy = req.query.groupBy === "session" ? "session" : null;
|
|
20
|
+
const browserId = req.headers["x-ima2-browser-id"] || null;
|
|
21
|
+
|
|
22
|
+
const rows = await listHistoryRows(ctx.config.storage.generatedDir);
|
|
23
|
+
|
|
24
|
+
// Enrich with favorite status
|
|
25
|
+
let favoriteSet = new Set();
|
|
26
|
+
if (browserId) {
|
|
27
|
+
const db = getDb();
|
|
28
|
+
const favRows = db.prepare("SELECT filename FROM gallery_favorites WHERE browser_id = ?").all(browserId);
|
|
29
|
+
favoriteSet = new Set(favRows.map((r) => r.filename));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let filtered = rows;
|
|
33
|
+
if (Number.isFinite(sinceTs)) {
|
|
34
|
+
filtered = filtered.filter((r) => r.createdAt > sinceTs);
|
|
35
|
+
}
|
|
36
|
+
if (Number.isFinite(beforeTs)) {
|
|
37
|
+
filtered = filtered.filter((r) => {
|
|
38
|
+
if (r.createdAt < beforeTs) return true;
|
|
39
|
+
if (r.createdAt === beforeTs && beforeFn) return r.filename < beforeFn;
|
|
40
|
+
return false;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
if (sessionId) {
|
|
44
|
+
filtered = filtered.filter((r) => r.sessionId === sessionId);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const page = filtered.slice(0, limit).map((r) => ({ ...r, isFavorite: favoriteSet.has(r.filename) }));
|
|
48
|
+
const nextCursor = page.length === limit && filtered.length > limit
|
|
49
|
+
? { before: page[page.length - 1].createdAt, beforeFilename: page[page.length - 1].filename }
|
|
50
|
+
: null;
|
|
51
|
+
|
|
52
|
+
if (groupBy === "session") {
|
|
53
|
+
const groups = new Map();
|
|
54
|
+
const loose = [];
|
|
55
|
+
for (const row of page) {
|
|
56
|
+
if (row.sessionId) {
|
|
57
|
+
let group = groups.get(row.sessionId);
|
|
58
|
+
if (!group) {
|
|
59
|
+
group = { sessionId: row.sessionId, items: [], lastUsedAt: row.createdAt };
|
|
60
|
+
groups.set(row.sessionId, group);
|
|
61
|
+
}
|
|
62
|
+
group.items.push(row);
|
|
63
|
+
if (row.createdAt > group.lastUsedAt) group.lastUsedAt = row.createdAt;
|
|
64
|
+
} else {
|
|
65
|
+
loose.push(row);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const titleMap = getSessionTitleMap(Array.from(groups.keys()));
|
|
69
|
+
const sessions = Array.from(groups.values())
|
|
70
|
+
.map((group) => ({
|
|
71
|
+
...group,
|
|
72
|
+
title: titleMap.get(group.sessionId) || null,
|
|
73
|
+
label: titleMap.get(group.sessionId) || group.sessionId.slice(0, 8),
|
|
74
|
+
}))
|
|
75
|
+
.sort((a, b) => b.lastUsedAt - a.lastUsedAt);
|
|
76
|
+
logEvent("history", "grouped", {
|
|
77
|
+
sessions: sessions.length,
|
|
78
|
+
loose: loose.length,
|
|
79
|
+
total: rows.length,
|
|
80
|
+
});
|
|
81
|
+
return res.json({ sessions, loose, total: rows.length, nextCursor });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
res.json({ items: page, total: rows.length, nextCursor });
|
|
85
|
+
} catch (err) {
|
|
86
|
+
logError("history", "error", err);
|
|
87
|
+
res.status(500).json({ error: err.message });
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
app.delete("/api/history/:filename/permanent", async (req, res) => {
|
|
92
|
+
try {
|
|
93
|
+
const filename = decodeURIComponent(req.params.filename);
|
|
94
|
+
const result = await deleteAssetPermanent(ctx.rootDir, filename);
|
|
95
|
+
res.json(result);
|
|
96
|
+
} catch (err) {
|
|
97
|
+
res.status(err.status || 500).json({ error: err.message, code: err.code });
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
app.delete("/api/history/:filename", async (req, res) => {
|
|
102
|
+
try {
|
|
103
|
+
const filename = decodeURIComponent(req.params.filename);
|
|
104
|
+
const result = await trashAsset(ctx.rootDir, filename);
|
|
105
|
+
res.json(result);
|
|
106
|
+
} catch (err) {
|
|
107
|
+
res.status(err.status || 500).json({ error: err.message, code: err.code });
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
app.post("/api/history/:filename/restore", async (req, res) => {
|
|
112
|
+
try {
|
|
113
|
+
const filename = decodeURIComponent(req.params.filename);
|
|
114
|
+
const trashId = typeof req.body?.trashId === "string" ? req.body.trashId : null;
|
|
115
|
+
if (!trashId) return res.status(400).json({ error: "trashId required" });
|
|
116
|
+
const result = await restoreAsset(ctx.rootDir, trashId, filename);
|
|
117
|
+
res.json(result);
|
|
118
|
+
} catch (err) {
|
|
119
|
+
res.status(err.status || 500).json({ error: err.message });
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
app.post("/api/history/favorite", async (req, res) => {
|
|
124
|
+
try {
|
|
125
|
+
const db = getDb();
|
|
126
|
+
const { filename } = req.body || {};
|
|
127
|
+
const browserId = req.headers["x-ima2-browser-id"];
|
|
128
|
+
|
|
129
|
+
if (!filename || typeof filename !== "string") {
|
|
130
|
+
return res.status(400).json({ error: "filename is required" });
|
|
131
|
+
}
|
|
132
|
+
if (!browserId || typeof browserId !== "string") {
|
|
133
|
+
return res.status(400).json({ error: "X-Ima2-Browser-Id header is required" });
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const existing = db.prepare("SELECT id FROM gallery_favorites WHERE browser_id = ? AND filename = ?").get(browserId, filename);
|
|
137
|
+
|
|
138
|
+
if (existing) {
|
|
139
|
+
db.prepare("DELETE FROM gallery_favorites WHERE browser_id = ? AND filename = ?").run(browserId, filename);
|
|
140
|
+
res.json({ isFavorite: false });
|
|
141
|
+
} else {
|
|
142
|
+
const id = `${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
|
|
143
|
+
db.prepare(
|
|
144
|
+
"INSERT INTO gallery_favorites (id, browser_id, filename, favorited_at) VALUES (?, ?, ?, ?)"
|
|
145
|
+
).run(id, browserId, filename, Math.floor(Date.now() / 1000));
|
|
146
|
+
res.json({ isFavorite: true });
|
|
147
|
+
}
|
|
148
|
+
} catch (err) {
|
|
149
|
+
logError("history", "favorite_error", err);
|
|
150
|
+
res.status(500).json({ error: err.message });
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
}
|
package/routes/imageImport.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
1
|
import express from "express";
|
|
2
2
|
import { createLocalImport } from "../lib/localImportStore.js";
|
|
3
|
-
|
|
4
3
|
function decodeHeader(value) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return decodeURIComponent(value);
|
|
8
|
-
} catch {
|
|
9
|
-
return value;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function registerImageImportRoutes(app, ctx) {
|
|
14
|
-
const rawImage = express.raw({
|
|
15
|
-
type: ["image/png", "image/jpeg", "image/webp"],
|
|
16
|
-
limit: ctx.config.server.bodyLimit,
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
app.post("/api/history/import-local", rawImage, async (req, res) => {
|
|
4
|
+
if (typeof value !== "string" || !value)
|
|
5
|
+
return null;
|
|
20
6
|
try {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
res.status(201).json({ item });
|
|
26
|
-
} catch (err) {
|
|
27
|
-
res.status(err.status || 500).json({
|
|
28
|
-
error: err.message,
|
|
29
|
-
code: err.code || "IMPORT_FAILED",
|
|
30
|
-
});
|
|
7
|
+
return decodeURIComponent(value);
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return value;
|
|
31
11
|
}
|
|
32
|
-
|
|
12
|
+
}
|
|
13
|
+
export function registerImageImportRoutes(app, ctx) {
|
|
14
|
+
const rawImage = express.raw({
|
|
15
|
+
type: ["image/png", "image/jpeg", "image/webp"],
|
|
16
|
+
limit: ctx.config.server.bodyLimit,
|
|
17
|
+
});
|
|
18
|
+
app.post("/api/history/import-local", rawImage, async (req, res) => {
|
|
19
|
+
try {
|
|
20
|
+
const item = await createLocalImport(ctx, {
|
|
21
|
+
buffer: Buffer.isBuffer(req.body) ? req.body : Buffer.alloc(0),
|
|
22
|
+
originalFilename: decodeHeader(req.headers["x-ima2-original-filename"]),
|
|
23
|
+
});
|
|
24
|
+
res.status(201).json({ item });
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
res.status(err.status || 500).json({
|
|
28
|
+
error: err.message,
|
|
29
|
+
code: err.code || "IMPORT_FAILED",
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
33
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import { createLocalImport } from "../lib/localImportStore.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
|
+
export function registerImageImportRoutes(app, ctx) {
|
|
14
|
+
const rawImage = express.raw({
|
|
15
|
+
type: ["image/png", "image/jpeg", "image/webp"],
|
|
16
|
+
limit: ctx.config.server.bodyLimit,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
app.post("/api/history/import-local", rawImage, async (req, res) => {
|
|
20
|
+
try {
|
|
21
|
+
const item = await createLocalImport(ctx, {
|
|
22
|
+
buffer: Buffer.isBuffer(req.body) ? req.body : Buffer.alloc(0),
|
|
23
|
+
originalFilename: decodeHeader(req.headers["x-ima2-original-filename"]),
|
|
24
|
+
});
|
|
25
|
+
res.status(201).json({ item });
|
|
26
|
+
} catch (err) {
|
|
27
|
+
res.status(err.status || 500).json({
|
|
28
|
+
error: err.message,
|
|
29
|
+
code: err.code || "IMPORT_FAILED",
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
package/routes/index.js
CHANGED
|
@@ -14,22 +14,22 @@ import { registerAnnotationRoutes } from "./annotations.js";
|
|
|
14
14
|
import { registerCanvasVersionRoutes } from "./canvasVersions.js";
|
|
15
15
|
import { registerComfyRoutes } from "./comfy.js";
|
|
16
16
|
import { registerImageImportRoutes } from "./imageImport.js";
|
|
17
|
-
|
|
18
17
|
export function configureRoutes(app, ctx) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
18
|
+
registerHealthRoutes(app, ctx);
|
|
19
|
+
registerStorageRoutes(app, ctx);
|
|
20
|
+
registerMetadataRoutes(app, ctx);
|
|
21
|
+
registerHistoryRoutes(app, ctx);
|
|
22
|
+
registerAnnotationRoutes(app, ctx);
|
|
23
|
+
registerCanvasVersionRoutes(app, ctx);
|
|
24
|
+
registerImageImportRoutes(app, ctx);
|
|
25
|
+
registerComfyRoutes(app, ctx);
|
|
26
|
+
registerSessionRoutes(app, ctx);
|
|
27
|
+
registerEditRoutes(app, ctx);
|
|
28
|
+
registerNodeRoutes(app, ctx);
|
|
29
|
+
if (ctx.config.features.cardNews)
|
|
30
|
+
registerCardNewsRoutes(app, ctx);
|
|
31
|
+
registerMultimodeRoutes(app, ctx);
|
|
32
|
+
registerGenerateRoutes(app, ctx);
|
|
33
|
+
registerPromptRoutes(app, ctx);
|
|
34
|
+
registerPromptImportRoutes(app, ctx);
|
|
35
35
|
}
|
package/routes/index.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { registerHealthRoutes } from "./health.js";
|
|
2
|
+
import { registerHistoryRoutes } from "./history.js";
|
|
3
|
+
import { registerSessionRoutes } from "./sessions.js";
|
|
4
|
+
import { registerEditRoutes } from "./edit.js";
|
|
5
|
+
import { registerNodeRoutes } from "./nodes.js";
|
|
6
|
+
import { registerGenerateRoutes } from "./generate.js";
|
|
7
|
+
import { registerMultimodeRoutes } from "./multimode.js";
|
|
8
|
+
import { registerStorageRoutes } from "./storage.js";
|
|
9
|
+
import { registerCardNewsRoutes } from "./cardNews.js";
|
|
10
|
+
import { registerMetadataRoutes } from "./metadata.js";
|
|
11
|
+
import { registerPromptRoutes } from "./prompts.js";
|
|
12
|
+
import { registerPromptImportRoutes } from "./promptImport.js";
|
|
13
|
+
import { registerAnnotationRoutes } from "./annotations.js";
|
|
14
|
+
import { registerCanvasVersionRoutes } from "./canvasVersions.js";
|
|
15
|
+
import { registerComfyRoutes } from "./comfy.js";
|
|
16
|
+
import { registerImageImportRoutes } from "./imageImport.js";
|
|
17
|
+
|
|
18
|
+
export function configureRoutes(app, ctx) {
|
|
19
|
+
registerHealthRoutes(app, ctx);
|
|
20
|
+
registerStorageRoutes(app, ctx);
|
|
21
|
+
registerMetadataRoutes(app, ctx);
|
|
22
|
+
registerHistoryRoutes(app, ctx);
|
|
23
|
+
registerAnnotationRoutes(app, ctx);
|
|
24
|
+
registerCanvasVersionRoutes(app, ctx);
|
|
25
|
+
registerImageImportRoutes(app, ctx);
|
|
26
|
+
registerComfyRoutes(app, ctx);
|
|
27
|
+
registerSessionRoutes(app, ctx);
|
|
28
|
+
registerEditRoutes(app, ctx);
|
|
29
|
+
registerNodeRoutes(app, ctx);
|
|
30
|
+
if (ctx.config.features.cardNews) registerCardNewsRoutes(app, ctx);
|
|
31
|
+
registerMultimodeRoutes(app, ctx);
|
|
32
|
+
registerGenerateRoutes(app, ctx);
|
|
33
|
+
registerPromptRoutes(app, ctx);
|
|
34
|
+
registerPromptImportRoutes(app, ctx);
|
|
35
|
+
}
|