dsh-gallery 0.17.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2002 -1698
- package/README.md +59 -21
- package/client.js +800 -241
- package/host/activity.js +208 -0
- package/host/context.js +219 -0
- package/host/derive.js +21 -0
- package/host/feedback-text.js +81 -1
- package/host/frames.js +34 -0
- package/host/groups.js +5 -0
- package/host/http.js +404 -30
- package/host/protocol.js +403 -1
- package/host/regions.js +572 -0
- package/host/scan.js +158 -42
- package/host/tools.js +816 -0
- package/index.js +6 -2
- package/package.json +104 -101
package/host/http.js
CHANGED
|
@@ -24,8 +24,9 @@ import { scanGallery, resolveSessionScope, parentDirOf } from './scan.js'
|
|
|
24
24
|
import { locateSessionDir, readSessionLogText } from './session.js'
|
|
25
25
|
import { extractSessionTitleFast } from './session-index.js'
|
|
26
26
|
import { DEFAULT_SESSIONS_DIR } from './constants.js'
|
|
27
|
-
import { makeDerivedName, bumpName, pngBufferFrom, tmpPathFor } from './derive.js'
|
|
27
|
+
import { makeDerivedName, bumpName, pngBufferFrom, imageBufferFrom, tmpPathFor } from './derive.js'
|
|
28
28
|
import { readClipboardFilePaths, writeClipboardFilePaths, internalClipboardPaths, pasteFilesIntoDir } from './clipboard.js'
|
|
29
|
+
import { buildLinkedMaskIndex, framesFilePath } from './frames.js'
|
|
29
30
|
|
|
30
31
|
/**
|
|
31
32
|
* 列表请求的短缓存 + 并发去重(加载提速,不改变返回内容与分组顺序)。
|
|
@@ -37,12 +38,91 @@ import { readClipboardFilePaths, writeClipboardFilePaths, internalClipboardPaths
|
|
|
37
38
|
const LIST_CACHE_MS = 4000
|
|
38
39
|
const listCache = new Map()
|
|
39
40
|
const listInflight = new Map()
|
|
41
|
+
// 2026-11-15:全局分组的全量明细留在服务端,响应只带每组前 60 张。
|
|
42
|
+
// 前端点「再显示 60 张」时用 groupMore 分批取,避免一次传 2 MB 拖慢加载。
|
|
43
|
+
const GROUP_PAGE = 60
|
|
44
|
+
const GROUPS_FULL_CACHE_MS = 10 * 60 * 1000
|
|
45
|
+
const groupsFullCache = new Map()
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 缓存键带上了派生登记指纹之后,每画一次遮罩就会换一个新键 ——
|
|
49
|
+
* 旧条目再也没人取,不清就会一直堆着(每次全局扫描的全量分组都不小)。
|
|
50
|
+
* 这里限制条目数:超了按写入时间淘汰最旧的,同时顺手清掉过期的。
|
|
51
|
+
*/
|
|
52
|
+
const GROUPS_FULL_CACHE_MAX = 24
|
|
53
|
+
function pruneGroupsFullCache(now) {
|
|
54
|
+
for (const [k, v] of groupsFullCache) {
|
|
55
|
+
if (now - v.at > GROUPS_FULL_CACHE_MS) groupsFullCache.delete(k)
|
|
56
|
+
}
|
|
57
|
+
while (groupsFullCache.size > GROUPS_FULL_CACHE_MAX) {
|
|
58
|
+
const oldest = groupsFullCache.keys().next()
|
|
59
|
+
if (oldest.done === true) break
|
|
60
|
+
groupsFullCache.delete(oldest.value)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 派生登记的「指纹」:frames / labels / marks 三个文件的修改时间拼起来。
|
|
66
|
+
*
|
|
67
|
+
* 🔴 为什么必须有这个(2026-11-15 老板实测报的 bug):
|
|
68
|
+
* 缓存键原来只带 settingsFile(dsh-gallery.json)的 mtime。可是**画一张遮罩**
|
|
69
|
+
* 写的是 dsh-gallery-frames.json —— settingsFile 没变 → 缓存键没变 →
|
|
70
|
+
* 4 秒内(全局分组缓存更是 10 分钟)返回的仍是「画遮罩之前」的旧快照。
|
|
71
|
+
* 表现:产出台「当前对话」视图已见 12 张(素材 8 / 过程稿 4),
|
|
72
|
+
* 全局资产里置顶的「当前对话」组还停在「素材 3 / 过程稿 3」,
|
|
73
|
+
* 两个视图的图对不上、数字也对不上。
|
|
74
|
+
*
|
|
75
|
+
* 只取 mtime、不读内容:一个 stat 很便宜,且足以捕捉「有没有被改过」。
|
|
76
|
+
* 文件不存在时给 0,仍能正常拼键。
|
|
77
|
+
*/
|
|
78
|
+
async function derivedStamp(resolved) {
|
|
79
|
+
const files = [
|
|
80
|
+
framesFilePath(resolved.settingsFile),
|
|
81
|
+
labelsFilePath(resolved.settingsFile),
|
|
82
|
+
marksFilePath(resolved.settingsFile),
|
|
83
|
+
]
|
|
84
|
+
const parts = []
|
|
85
|
+
for (const f of files) {
|
|
86
|
+
try {
|
|
87
|
+
parts.push(Math.trunc((await fsp.stat(f)).mtimeMs))
|
|
88
|
+
} catch {
|
|
89
|
+
parts.push(0)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return parts.join('-')
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** 全量分组缓存的键:配置 / 根目录 / 设置文件 / 派生登记变了都必须换一份。 */
|
|
96
|
+
function groupsCacheKey(kind, scope, sessionId, resolved, settingsStamp, derived) {
|
|
97
|
+
return JSON.stringify([kind, scope, sessionId, resolved.roots,
|
|
98
|
+
resolved.windowHours, resolved.maxDepth, resolved.maxItems, settingsStamp,
|
|
99
|
+
derived ?? ''])
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** 只裁每组明细,不动整组计数:counts / fileCount 仍是全量,顶部数字才对得上。 */
|
|
103
|
+
function slimGroups(value, pageSize) {
|
|
104
|
+
if (!Array.isArray(value?.groups)) return value
|
|
105
|
+
return {
|
|
106
|
+
...value,
|
|
107
|
+
groups: value.groups.map((g) => {
|
|
108
|
+
const files = Array.isArray(g.files) ? g.files.slice(0, pageSize) : []
|
|
109
|
+
return {
|
|
110
|
+
...g,
|
|
111
|
+
files,
|
|
112
|
+
returnedCount: files.length,
|
|
113
|
+
truncated: (Number(g.fileCount) || 0) > files.length,
|
|
114
|
+
}
|
|
115
|
+
}),
|
|
116
|
+
}
|
|
117
|
+
}
|
|
40
118
|
import { resolveReadable, serveMedia, contentTypeFor } from './media.js'
|
|
41
119
|
import { MARK_ACTIONS, readMarks, writeMarks, setMark, setMarksBatch, marksFilePath } from './marks.js'
|
|
42
120
|
import { readFeedback, setFeedback, setFeedbackBatch, ackFeedback, clearFeedback } from './feedback.js'
|
|
121
|
+
// 阶段 3 / C1:记下「人正在看哪张图」,单独一份临时状态文件
|
|
122
|
+
import { writeContext, writePanel } from './context.js'
|
|
43
123
|
// v0.16.4 A 档:把待处理反馈渲染成成稿文本(纯函数,不碰落盘数据)
|
|
44
124
|
import { makeFeedback, renderFeedbackText } from './feedback-text.js'
|
|
45
|
-
import { LABEL_CATEGORIES, readLabels, setLabel, setLabelsBatch, deleteLabels } from './labels.js'
|
|
125
|
+
import { LABEL_CATEGORIES, readLabels, setLabel, setLabelsBatch, deleteLabels, labelsFilePath } from './labels.js'
|
|
46
126
|
import {
|
|
47
127
|
registerFrame, deleteFrames, frameTimecode, makeFrameName, makeMaskName, maskPolygonMeta,
|
|
48
128
|
} from './frames.js'
|
|
@@ -92,15 +172,121 @@ function absoluteOf(roots, relPath) {
|
|
|
92
172
|
* 筛选维度(AND 组合):
|
|
93
173
|
* label / category —— 生效分类(手动标签优先,与台面显示一致)
|
|
94
174
|
* action —— 判定动作(来自 marks)
|
|
95
|
-
* confirmed —— 是否已确认(来自 marks 的 confirmed 字段)
|
|
96
175
|
* ext —— 扩展名
|
|
97
|
-
* q ——
|
|
176
|
+
* q —— 关键词(文件名 / 路径 / 对话标题 / 遮罩关联名)
|
|
177
|
+
*/
|
|
178
|
+
/**
|
|
179
|
+
* 2026-11-16 第一档:把用户/模型给的关键词拆成多个词。
|
|
180
|
+
* 支持空格、逗号、顿号、分号、竖线、斜杠分隔;没有分隔符时整串当一个词。
|
|
181
|
+
* 返回小写、去重、最多 8 段,避免一段超长输入把打分拖慢。
|
|
182
|
+
*/
|
|
183
|
+
export function splitQuery(q) {
|
|
184
|
+
const rawq = String(q ?? '').toLowerCase().trim()
|
|
185
|
+
if (rawq === '') return []
|
|
186
|
+
const seen = new Set()
|
|
187
|
+
const out = []
|
|
188
|
+
for (const piece of rawq.split(/[\s,,、;;|/\\]+/)) {
|
|
189
|
+
const term = piece.trim()
|
|
190
|
+
if (term === '' || seen.has(term)) continue
|
|
191
|
+
seen.add(term)
|
|
192
|
+
out.push(term)
|
|
193
|
+
if (out.length >= 8) break
|
|
194
|
+
}
|
|
195
|
+
return out
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* 中文连着写没有分隔符时(如「门头定稿」),整串在文件名里通常不存在。
|
|
200
|
+
* 这时退化成「两字一组」:门头 / 头定 / 定稿,任一组命中就算候选。
|
|
201
|
+
* 只在整词一个都没命中时才启用,绝不会把整库都放进来。
|
|
202
|
+
*/
|
|
203
|
+
export function bigramFallback(term) {
|
|
204
|
+
const s = String(term ?? '').trim()
|
|
205
|
+
if (s.length < 3) return []
|
|
206
|
+
const seen = new Set()
|
|
207
|
+
const out = []
|
|
208
|
+
for (let i = 0; i + 2 <= s.length; i += 1) {
|
|
209
|
+
const gram = s.slice(i, i + 2)
|
|
210
|
+
if (seen.has(gram)) continue
|
|
211
|
+
seen.add(gram)
|
|
212
|
+
out.push(gram)
|
|
213
|
+
if (out.length >= 6) break
|
|
214
|
+
}
|
|
215
|
+
return out
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** 一条资产能被搜到的全部文本(一次拼好,多词复用)。 */
|
|
219
|
+
export function haystackOf(f) {
|
|
220
|
+
return [
|
|
221
|
+
f?.relPath, f?.name, f?.extension, f?.sessionTitle,
|
|
222
|
+
f?.maskOf?.sourceName, f?.maskOf?.sourceRelPath,
|
|
223
|
+
].filter((x) => typeof x === 'string' && x !== '').join(' ').toLowerCase()
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* 阶段 2:关键词命中打分(纯函数,导出给工具与测试复用)。
|
|
228
|
+
* 文件名 3 分 / 路径 2 分 / 对话标题 2 分 / 遮罩关联名 2 分,
|
|
229
|
+
* 再加 0~1 分的新鲜度;同分按时间倒序,再同分保持原顺序。
|
|
98
230
|
*/
|
|
231
|
+
/**
|
|
232
|
+
* 2026-11-16 第一档:关键词拆成多个词后再算分。
|
|
233
|
+
* 旧做法把整串当一个词去找(`hay.includes(整串)`):「门头 定稿」这种带空格的
|
|
234
|
+
* 输入必然落空。现在每个词单独算,命中词越多分越高,全中的排最前。
|
|
235
|
+
* 打分口径不变:文件名 3 / 路径 2 / 对话标题 2 / 遮罩来源名 2;每多命中一个词再加 4。
|
|
236
|
+
*/
|
|
237
|
+
export function relevanceScore(f, q, maskNameOf) {
|
|
238
|
+
const terms = Array.isArray(q) ? q : splitQuery(q)
|
|
239
|
+
if (terms.length === 0) return 0
|
|
240
|
+
const name = String(f?.name ?? '').toLowerCase()
|
|
241
|
+
const rel = String(f?.relPath ?? '').toLowerCase()
|
|
242
|
+
const title = String(f?.sessionTitle ?? '').toLowerCase()
|
|
243
|
+
const mask = String((maskNameOf ? maskNameOf(f) : f?.maskOf?.sourceName) ?? '').toLowerCase()
|
|
244
|
+
let score = 0
|
|
245
|
+
let hit = 0
|
|
246
|
+
for (const term of terms) {
|
|
247
|
+
let local = 0
|
|
248
|
+
if (name.includes(term)) local += 3
|
|
249
|
+
if (rel.includes(term)) local += 2
|
|
250
|
+
if (title.includes(term)) local += 2
|
|
251
|
+
if (mask !== '' && mask.includes(term)) local += 2
|
|
252
|
+
if (local > 0) {
|
|
253
|
+
hit += 1
|
|
254
|
+
score += local
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
score += Math.max(0, hit - 1) * 4
|
|
258
|
+
return score
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** 有关键词时按相关性排;没关键词时保持调用方给的顺序。 */
|
|
262
|
+
/** 有关键词时按相关性排;没关键词时保持调用方给的顺序。接受字符串或已拆好的词数组。 */
|
|
263
|
+
export function sortByRelevance(list, q, maskNameOf) {
|
|
264
|
+
const terms = Array.isArray(q) ? q : splitQuery(q)
|
|
265
|
+
const arr = Array.isArray(list) ? list.slice() : []
|
|
266
|
+
if (terms.length === 0 || arr.length === 0) return arr
|
|
267
|
+
let min = Infinity
|
|
268
|
+
let max = -Infinity
|
|
269
|
+
for (const f of arr) {
|
|
270
|
+
const t = Number(f?.mtime) || 0
|
|
271
|
+
if (t < min) min = t
|
|
272
|
+
if (t > max) max = t
|
|
273
|
+
}
|
|
274
|
+
const span = max - min || 1
|
|
275
|
+
const scored = arr.map((f, i) => ({
|
|
276
|
+
f, i,
|
|
277
|
+
s: relevanceScore(f, terms, maskNameOf) + ((Number(f?.mtime) || 0) - min) / span,
|
|
278
|
+
}))
|
|
279
|
+
scored.sort((a, b) => (b.s - a.s)
|
|
280
|
+
|| ((Number(b.f?.mtime) || 0) - (Number(a.f?.mtime) || 0))
|
|
281
|
+
|| (a.i - b.i))
|
|
282
|
+
return scored.map((x) => x.f)
|
|
283
|
+
}
|
|
284
|
+
|
|
99
285
|
// v0.16.4: 导出以便真跑测试(纯函数,不改行为)
|
|
100
286
|
export async function applyAssetFilter(value, opts) {
|
|
101
287
|
const {
|
|
102
288
|
filterLabel, filterCategory, filterAction, filterExt,
|
|
103
|
-
|
|
289
|
+
filterQuery, filterLimit, settingsFile,
|
|
104
290
|
} = opts
|
|
105
291
|
|
|
106
292
|
let marks = {}
|
|
@@ -108,6 +294,28 @@ export async function applyAssetFilter(value, opts) {
|
|
|
108
294
|
marks = (await readMarks(settingsFile)).marks ?? {}
|
|
109
295
|
} catch { marks = {} }
|
|
110
296
|
|
|
297
|
+
// 2026-11-16 第一档:先按整词匹配;整库一个都没命中、且是中文连着写时,
|
|
298
|
+
// 才退化成「两字一组」再试一次。这样「门头定稿」能命中「门头_效果图_定稿.png」,
|
|
299
|
+
// 但不会因为模糊而把一堆无关资产塞进结果。
|
|
300
|
+
const terms = splitQuery(filterQuery)
|
|
301
|
+
let activeTerms = terms
|
|
302
|
+
let queryFuzzy = false
|
|
303
|
+
if (terms.length === 1 && terms[0].length >= 3) {
|
|
304
|
+
const probe = []
|
|
305
|
+
if (Array.isArray(value?.groups)) {
|
|
306
|
+
for (const g of value.groups) if (Array.isArray(g?.files)) probe.push(...g.files)
|
|
307
|
+
}
|
|
308
|
+
if (Array.isArray(value?.items)) probe.push(...value.items)
|
|
309
|
+
if (Array.isArray(value?.files)) probe.push(...value.files)
|
|
310
|
+
if (probe.length > 0 && !probe.some((f) => haystackOf(f).includes(terms[0]))) {
|
|
311
|
+
const grams = bigramFallback(terms[0])
|
|
312
|
+
if (grams.length > 0) {
|
|
313
|
+
activeTerms = grams
|
|
314
|
+
queryFuzzy = true
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
111
319
|
const labelSet = filterLabel !== '' ? new Set([filterLabel]) : null
|
|
112
320
|
const categorySet = filterCategory !== '' ? new Set([filterCategory]) : null
|
|
113
321
|
|
|
@@ -119,14 +327,19 @@ export async function applyAssetFilter(value, opts) {
|
|
|
119
327
|
const ext = String(f?.extension ?? '')
|
|
120
328
|
if (filterExt !== '' && !ext.toLowerCase().endsWith(filterExt)) return false
|
|
121
329
|
|
|
122
|
-
if (
|
|
123
|
-
|
|
124
|
-
|
|
330
|
+
if (activeTerms.length > 0) {
|
|
331
|
+
// 第一档:拆成多个词后「任一命中」即留下;多词全中靠打分往前排,
|
|
332
|
+
// 不再要求文件名里原样出现整串关键词(那样多词必然落空)。
|
|
333
|
+
const hay = haystackOf(f)
|
|
334
|
+
let hit = false
|
|
335
|
+
for (const term of activeTerms) {
|
|
336
|
+
if (hay.includes(term)) { hit = true; break }
|
|
337
|
+
}
|
|
338
|
+
if (!hit) return false
|
|
125
339
|
}
|
|
126
340
|
|
|
127
341
|
const mark = marks[f?.relPath]
|
|
128
342
|
if (filterAction !== '' && String(mark?.action ?? '') !== filterAction) return false
|
|
129
|
-
if (filterConfirmed !== null && (mark?.confirmed === true) !== filterConfirmed) return false
|
|
130
343
|
|
|
131
344
|
return true
|
|
132
345
|
}
|
|
@@ -135,18 +348,22 @@ export async function applyAssetFilter(value, opts) {
|
|
|
135
348
|
const mark = marks[f?.relPath]
|
|
136
349
|
return {
|
|
137
350
|
...f,
|
|
138
|
-
confirmed: mark?.confirmed === true,
|
|
139
351
|
markAction: String(mark?.action ?? ''),
|
|
140
352
|
}
|
|
141
353
|
}
|
|
142
354
|
|
|
355
|
+
const orderByQuery = (arr) => sortByRelevance(arr, activeTerms)
|
|
356
|
+
|
|
143
357
|
const out = { ...value }
|
|
144
358
|
out.filtered = true
|
|
359
|
+
// 把实际用的词带回去:查图工具按同一套词排序,模糊兜底时两边口径才一致
|
|
360
|
+
out.queryTerms = activeTerms
|
|
361
|
+
out.queryFuzzy = queryFuzzy
|
|
145
362
|
|
|
146
363
|
if (Array.isArray(value?.groups)) {
|
|
147
364
|
const groups = []
|
|
148
365
|
for (const g of value.groups) {
|
|
149
|
-
const files = (Array.isArray(g.files) ? g.files : []).filter(keep).map(decorate)
|
|
366
|
+
const files = orderByQuery((Array.isArray(g.files) ? g.files : []).filter(keep).map(decorate))
|
|
150
367
|
if (files.length === 0) continue
|
|
151
368
|
groups.push({
|
|
152
369
|
...g,
|
|
@@ -164,11 +381,11 @@ export async function applyAssetFilter(value, opts) {
|
|
|
164
381
|
}
|
|
165
382
|
|
|
166
383
|
if (Array.isArray(value?.items)) {
|
|
167
|
-
out.items = value.items.filter(keep).map(decorate).slice(0, filterLimit)
|
|
384
|
+
out.items = orderByQuery(value.items.filter(keep).map(decorate)).slice(0, filterLimit)
|
|
168
385
|
}
|
|
169
386
|
|
|
170
387
|
if (Array.isArray(value?.files)) {
|
|
171
|
-
out.files = value.files.filter(keep).map(decorate).slice(0, filterLimit)
|
|
388
|
+
out.files = orderByQuery(value.files.filter(keep).map(decorate)).slice(0, filterLimit)
|
|
172
389
|
out.total = out.files.length
|
|
173
390
|
}
|
|
174
391
|
|
|
@@ -391,23 +608,24 @@ export function registerOpsRoute(ctx, config) {
|
|
|
391
608
|
const filterCategory = typeof body?.category === 'string' ? body.category : ''
|
|
392
609
|
const filterAction = typeof body?.action === 'string' ? body.action : ''
|
|
393
610
|
const filterExt = typeof body?.ext === 'string' ? body.ext.toLowerCase() : ''
|
|
394
|
-
const filterConfirmed = body?.confirmed === true ? true
|
|
395
|
-
: body?.confirmed === false ? false : null
|
|
396
611
|
const filterQuery = typeof body?.q === 'string' ? body.q.trim().toLowerCase() : ''
|
|
397
612
|
const filterLimit = Math.max(1, Math.min(500, Math.trunc(Number(body?.limit)) || 100))
|
|
398
613
|
const hasFilter = filterLabel !== '' || filterCategory !== '' || filterAction !== ''
|
|
399
|
-
|| filterExt !== '' ||
|
|
614
|
+
|| filterExt !== '' || filterQuery !== ''
|
|
400
615
|
|
|
401
616
|
// 配置 / 隐藏列表写在 settingsFile 里,它的 mtime 变了就让缓存失效。
|
|
402
617
|
// sessionId 必须在键里:切对话后不能复用上一会话的分组结果。
|
|
403
618
|
let settingsStamp = 0
|
|
404
619
|
try { settingsStamp = (await fsp.stat(resolved.settingsFile)).mtimeMs } catch { /* 文件还不存在 */ }
|
|
620
|
+
// 2026-11-15:登记文件(画遮罩 / 粘贴 / 裁剪 / 改标签 / 判定)也会改变归属与分类,
|
|
621
|
+
// 不纳入键就会返回旧快照 —— 详见 derivedStamp() 的注释。
|
|
622
|
+
const derived = await derivedStamp(resolved)
|
|
405
623
|
const cacheKey = JSON.stringify([
|
|
406
624
|
kind, scope, sessionId, body?.page ?? 1,
|
|
407
625
|
resolved.roots, resolved.windowHours, resolved.maxDepth,
|
|
408
|
-
resolved.maxItems, settingsStamp,
|
|
626
|
+
resolved.maxItems, settingsStamp, derived,
|
|
409
627
|
filterLabel, filterCategory, filterAction, filterExt,
|
|
410
|
-
|
|
628
|
+
filterQuery, filterLimit,
|
|
411
629
|
])
|
|
412
630
|
const hit = listCache.get(cacheKey)
|
|
413
631
|
if (hit !== undefined && Date.now() - hit.at < LIST_CACHE_MS) return json(hit.value)
|
|
@@ -417,15 +635,24 @@ export function registerOpsRoute(ctx, config) {
|
|
|
417
635
|
const task = scanGallery(config, kind, scope, sessionId, body?.page)
|
|
418
636
|
.then((value) => {
|
|
419
637
|
// ── v0.16.4:按条件过滤 ────────────────────────────
|
|
420
|
-
//
|
|
638
|
+
// 判定动作来自 marks 文件;手动标签已在 scan 阶段
|
|
421
639
|
// 覆写到 item.category,所以这里直接用 category 即可。
|
|
422
640
|
if (!hasFilter) {
|
|
423
|
-
|
|
424
|
-
|
|
641
|
+
// 全量明细留在服务端缓存,响应瘦身:每组只带前 60 张。
|
|
642
|
+
if (scope === 'global' && Array.isArray(value.groups)) {
|
|
643
|
+
groupsFullCache.set(
|
|
644
|
+
groupsCacheKey(kind, scope, sessionId, resolved, settingsStamp, derived),
|
|
645
|
+
{ at: Date.now(), groups: value.groups },
|
|
646
|
+
)
|
|
647
|
+
pruneGroupsFullCache(Date.now())
|
|
648
|
+
}
|
|
649
|
+
const slim = slimGroups(value, GROUP_PAGE)
|
|
650
|
+
listCache.set(cacheKey, { at: Date.now(), value: slim })
|
|
651
|
+
return slim
|
|
425
652
|
}
|
|
426
653
|
return applyAssetFilter(value, {
|
|
427
654
|
filterLabel, filterCategory, filterAction, filterExt,
|
|
428
|
-
|
|
655
|
+
filterQuery, filterLimit,
|
|
429
656
|
settingsFile: resolved.settingsFile,
|
|
430
657
|
}).then((filtered) => {
|
|
431
658
|
listCache.set(cacheKey, { at: Date.now(), value: filtered })
|
|
@@ -446,6 +673,115 @@ export function registerOpsRoute(ctx, config) {
|
|
|
446
673
|
|
|
447
674
|
|
|
448
675
|
|
|
676
|
+
// 全局分组:点「再显示 60 张」时取下一批明细。
|
|
677
|
+
// 全量明细留在服务端内存,不重扫盘、不再传 2 MB 大包。
|
|
678
|
+
if (op === 'groupMore') {
|
|
679
|
+
try {
|
|
680
|
+
const kind = body?.kind === 'file' ? 'file'
|
|
681
|
+
: body?.kind === 'media' ? 'media'
|
|
682
|
+
: 'image'
|
|
683
|
+
const sessionId = typeof body?.sessionId === 'string' ? body.sessionId : ''
|
|
684
|
+
const groupKey = String(body?.groupKey ?? '')
|
|
685
|
+
const offset = Math.max(0, Math.trunc(Number(body?.offset)) || 0)
|
|
686
|
+
const limit = Math.max(1, Math.min(GROUP_PAGE, Math.trunc(Number(body?.limit)) || GROUP_PAGE))
|
|
687
|
+
let settingsStamp = 0
|
|
688
|
+
try { settingsStamp = (await fsp.stat(resolved.settingsFile)).mtimeMs } catch { /* 文件还不存在 */ }
|
|
689
|
+
// 与 list 用同一把尺子:登记文件变了,缓存必须换一份,否则「再显示 N 张」会回旧快照
|
|
690
|
+
const derived = await derivedStamp(resolved)
|
|
691
|
+
const gKey = groupsCacheKey(kind, 'global', sessionId, resolved, settingsStamp, derived)
|
|
692
|
+
let entry = groupsFullCache.get(gKey)
|
|
693
|
+
if (entry === undefined || Date.now() - entry.at > GROUPS_FULL_CACHE_MS) {
|
|
694
|
+
const fresh = await scanGallery(config, kind, 'global', sessionId, 1)
|
|
695
|
+
entry = { at: Date.now(), groups: Array.isArray(fresh.groups) ? fresh.groups : [] }
|
|
696
|
+
groupsFullCache.set(gKey, entry)
|
|
697
|
+
pruneGroupsFullCache(Date.now())
|
|
698
|
+
}
|
|
699
|
+
const keyOf = (g) => String(g?.sessionId ?? '__history__')
|
|
700
|
+
const group = entry.groups.find((g) => keyOf(g) === groupKey)
|
|
701
|
+
if (group === undefined) return json({ ok: true, files: [], total: 0, hasMore: false })
|
|
702
|
+
const all = Array.isArray(group.files) ? group.files : []
|
|
703
|
+
const files = all.slice(offset, offset + limit)
|
|
704
|
+
return json({
|
|
705
|
+
ok: true,
|
|
706
|
+
files,
|
|
707
|
+
total: Number(group.fileCount) || all.length,
|
|
708
|
+
hasMore: offset + files.length < all.length,
|
|
709
|
+
})
|
|
710
|
+
} catch (error) {
|
|
711
|
+
return json({ ok: false, error: { code: 'group-more-failed', message: String(error?.message ?? error) } }, 500)
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
// 阶段 3 / C1:前端点开大图时把这张图同步过来,Agent 才答得出
|
|
716
|
+
// 「我刚看的那张是什么」。只读状态,不碰 marks / labels / feedback。
|
|
717
|
+
if (op === 'setContext') {
|
|
718
|
+
try {
|
|
719
|
+
const raw = (body?.item !== null && typeof body?.item === 'object') ? body.item : null
|
|
720
|
+
const rel = String(raw?.relPath ?? '')
|
|
721
|
+
if (rel === '') return json({ ok: false, error: { code: 'bad-context', message: '缺少 relPath' } }, 400)
|
|
722
|
+
|
|
723
|
+
// 绝对路径优先用前端给的(Host rootOfAbs 的权威值);没给就按真实所属根拼。
|
|
724
|
+
let abs = String(raw?.absPath ?? '')
|
|
725
|
+
if (abs === '') {
|
|
726
|
+
for (const r of resolved.roots) {
|
|
727
|
+
const candidate = path.resolve(String(r ?? ''), rel.split('/').join(path.sep))
|
|
728
|
+
try { await fsp.stat(candidate); abs = candidate; break } catch { /* 换下一个根 */ }
|
|
729
|
+
}
|
|
730
|
+
if (abs === '' && resolved.roots.length > 0) {
|
|
731
|
+
abs = path.resolve(String(resolved.roots[0]), rel.split('/').join(path.sep))
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
let maskRel = String(raw?.maskOf?.relPath ?? raw?.maskPath ?? '')
|
|
735
|
+
if (maskRel === '') {
|
|
736
|
+
// 原图 / 彩色填充预览图本身不带 maskOf:按登记反查配套主遮罩,
|
|
737
|
+
// 口径与前端双击、拖图写进输入框完全一致。
|
|
738
|
+
try {
|
|
739
|
+
maskRel = (await buildLinkedMaskIndex(resolved.settingsFile)).get(rel) ?? ''
|
|
740
|
+
} catch { /* 读登记失败只当没有,绝不影响看图 */ }
|
|
741
|
+
}
|
|
742
|
+
let maskAbs = String(raw?.maskAbsPath ?? '')
|
|
743
|
+
if (maskAbs === '' && maskRel !== '' && abs !== '') {
|
|
744
|
+
const owner = resolved.roots
|
|
745
|
+
.map((r) => path.resolve(String(r ?? '')))
|
|
746
|
+
.filter((r) => abs === r || abs.startsWith(r + path.sep))
|
|
747
|
+
.sort((a, b) => b.length - a.length)[0] ?? ''
|
|
748
|
+
if (owner !== '') maskAbs = path.resolve(owner, maskRel.split('/').join(path.sep))
|
|
749
|
+
}
|
|
750
|
+
// 第二档:文件自身改动时间也带上,活跃度排序要用它
|
|
751
|
+
let fileMtime = Number(raw?.mtime) || 0
|
|
752
|
+
if (fileMtime <= 0 && abs !== '') {
|
|
753
|
+
try { fileMtime = (await fsp.stat(abs)).mtimeMs } catch { fileMtime = 0 }
|
|
754
|
+
}
|
|
755
|
+
await writeContext(resolved.settingsFile, {
|
|
756
|
+
relPath: rel,
|
|
757
|
+
absPath: abs,
|
|
758
|
+
name: String(raw?.name ?? rel.split('/').pop() ?? ''),
|
|
759
|
+
category: String(raw?.category ?? 'unknown'),
|
|
760
|
+
categoryManual: raw?.categoryManual === true,
|
|
761
|
+
maskPath: maskRel,
|
|
762
|
+
maskAbsPath: maskAbs,
|
|
763
|
+
sessionId: String(raw?.sessionId ?? ''),
|
|
764
|
+
sessionTitle: String(raw?.sessionTitle ?? ''),
|
|
765
|
+
mtime: fileMtime,
|
|
766
|
+
})
|
|
767
|
+
return json({ ok: true })
|
|
768
|
+
} catch (error) {
|
|
769
|
+
return json({ ok: false, error: { code: 'context-failed', message: String(error?.message ?? error) } }, 500)
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
// 第二档:面板此刻长什么样(视图 / 类型 / 页码 / 总数 / 组数 / 分类开关)。
|
|
774
|
+
// 只记界面状态,不碰任何资产;人切视图、改筛选时由前端防抖上报。
|
|
775
|
+
if (op === 'setPanel') {
|
|
776
|
+
try {
|
|
777
|
+
const saved = await writePanel(resolved.settingsFile, body?.panel ?? null)
|
|
778
|
+
if (saved === null) return json({ ok: false, error: { code: 'bad-panel', message: '缺少 panel' } }, 400)
|
|
779
|
+
return json({ ok: true, panel: saved })
|
|
780
|
+
} catch (error) {
|
|
781
|
+
return json({ ok: false, error: { code: 'panel-failed', message: String(error?.message ?? error) } }, 500)
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
449
785
|
// 读取当前设置(含 root 来源、是否存在)
|
|
450
786
|
|
|
451
787
|
if (op === 'settings') {
|
|
@@ -1081,7 +1417,6 @@ export function registerOpsRoute(ctx, config) {
|
|
|
1081
1417
|
// v0.16.4:带上分类与归属会话 —— Agent 拿到就能判断该改哪个文件、归哪条对话
|
|
1082
1418
|
category: String(item.category ?? 'unknown'),
|
|
1083
1419
|
sessionId: String(item.sessionId ?? ''),
|
|
1084
|
-
confirmed: mark?.confirmed === true,
|
|
1085
1420
|
}
|
|
1086
1421
|
const flatAction = String(item.action ?? '')
|
|
1087
1422
|
const annots = Array.isArray(item.annotations)
|
|
@@ -1686,6 +2021,39 @@ export function registerOpsRoute(ctx, config) {
|
|
|
1686
2021
|
return abs
|
|
1687
2022
|
}
|
|
1688
2023
|
|
|
2024
|
+
/**
|
|
2025
|
+
* 新对话「空状态」粘贴兜底目录(2026-11-15,本机补丁,未进 1.0.0 发布包)。
|
|
2026
|
+
*
|
|
2027
|
+
* 背景:resolveSessionDir 靠会话日志里出现过的路径反推落点目录;新对话还没有
|
|
2028
|
+
* 任何产出时反推不出来,返回 null,三个粘贴入口直接报「无法确定当前对话目录」,
|
|
2029
|
+
* 形成死锁 —— 想放素材进来,却因为里面没素材而不让放。
|
|
2030
|
+
*
|
|
2031
|
+
* 兜底策略:落在第一个真实存在的产出根下 `_对话素材/<会话短id>/`。
|
|
2032
|
+
* · 目录在 root 内 → rootOfAbs / relPath / 扫描都正常,粘进去的文件看得见;
|
|
2033
|
+
* · 带会话短 id → 多个新对话各落各的,互不串;
|
|
2034
|
+
* · 正常解析出的目录优先,这里只在拿不到时才用,不改 resolveSessionDir 原语义;
|
|
2035
|
+
* · 归属仍靠调用方 registerDerivedAsset(sessionId) + 扫描 frameByRel 收进当前对话。
|
|
2036
|
+
*/
|
|
2037
|
+
async function resolveFallbackDir(config, sessionId) {
|
|
2038
|
+
const opts = resolveOptions(config)
|
|
2039
|
+
const roots = (opts.roots ?? []).filter((r) => typeof r === "string" && r !== "")
|
|
2040
|
+
if (roots.length === 0) return null
|
|
2041
|
+
// 会话短 id:清洗到安全字符集,空则给固定名,避免路径注入
|
|
2042
|
+
const sidRaw = typeof sessionId === "string" ? sessionId : ""
|
|
2043
|
+
const sid = sidRaw.replace(/[^A-Za-z0-9_-]/g, "").slice(0, 12) || "current"
|
|
2044
|
+
for (const root of roots) {
|
|
2045
|
+
try {
|
|
2046
|
+
const absRoot = path.resolve(root)
|
|
2047
|
+
const st = await fsp.stat(absRoot)
|
|
2048
|
+
if (!st.isDirectory()) continue
|
|
2049
|
+
const dir = path.join(absRoot, "_对话素材", sid)
|
|
2050
|
+
if (!isInside(absRoot, dir)) continue
|
|
2051
|
+
return dir
|
|
2052
|
+
} catch { /* 这个根不可用,试下一个 */ }
|
|
2053
|
+
}
|
|
2054
|
+
return null
|
|
2055
|
+
}
|
|
2056
|
+
|
|
1689
2057
|
/**
|
|
1690
2058
|
* 读会话标题 / 创建时间(派生资产登记用)。
|
|
1691
2059
|
*
|
|
@@ -1807,15 +2175,19 @@ export function registerOpsRoute(ctx, config) {
|
|
|
1807
2175
|
const opts = resolveOptions(config)
|
|
1808
2176
|
const sessionId = String(body?.sessionId ?? '')
|
|
1809
2177
|
const data = typeof body?.data === 'string' ? body.data : ''
|
|
1810
|
-
const ext = (typeof body?.ext === 'string' && body.ext !== '') ? body.ext : 'png'
|
|
1811
2178
|
if (data === '') {
|
|
1812
2179
|
return json({ ok: false, error: { code: 'bad-request', message: '缺少图片数据' } }, 400)
|
|
1813
2180
|
}
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
2181
|
+
// 2026-11-15 放宽:剪贴板位图可能是 JPEG / WebP / GIF / BMP,不再只认 PNG;
|
|
2182
|
+
// 扩展名以真实魔数为准(不信前端传的 ext),避免后缀与内容不符。
|
|
2183
|
+
const img = imageBufferFrom(data)
|
|
2184
|
+
if (img === null) {
|
|
2185
|
+
return json({ ok: false, error: { code: 'bad-image', message: '图片数据为空、超过 24 MB,或不是 PNG / JPG / WebP / GIF / BMP' } }, 400)
|
|
1817
2186
|
}
|
|
1818
|
-
const
|
|
2187
|
+
const buf = img.buf
|
|
2188
|
+
const ext = img.ext
|
|
2189
|
+
let dir = await resolveSessionDir(config, sessionId)
|
|
2190
|
+
if (dir === null) dir = await resolveFallbackDir(config, sessionId)
|
|
1819
2191
|
if (dir === null) {
|
|
1820
2192
|
return json({ ok: false, error: { code: 'no-session-dir', message: '无法确定当前对话目录' } }, 400)
|
|
1821
2193
|
}
|
|
@@ -1863,7 +2235,8 @@ export function registerOpsRoute(ctx, config) {
|
|
|
1863
2235
|
if (srcStat.size > 2 * 1024 * 1024 * 1024) {
|
|
1864
2236
|
return json({ ok: false, error: { code: 'too-large', message: '文件超过 2 GB,粘贴被拒绝' } }, 400)
|
|
1865
2237
|
}
|
|
1866
|
-
|
|
2238
|
+
let dir = await resolveSessionDir(config, sessionId)
|
|
2239
|
+
if (dir === null) dir = await resolveFallbackDir(config, sessionId)
|
|
1867
2240
|
if (dir === null) {
|
|
1868
2241
|
return json({ ok: false, error: { code: 'no-session-dir', message: '无法确定当前对话目录' } }, 400)
|
|
1869
2242
|
}
|
|
@@ -1941,7 +2314,8 @@ export function registerOpsRoute(ctx, config) {
|
|
|
1941
2314
|
const opts = resolveOptions(config)
|
|
1942
2315
|
const sessionId = String(body?.sessionId ?? '')
|
|
1943
2316
|
const srcs = Array.isArray(body?.srcPaths) ? body.srcPaths : []
|
|
1944
|
-
|
|
2317
|
+
let dir = await resolveSessionDir(config, sessionId)
|
|
2318
|
+
if (dir === null) dir = await resolveFallbackDir(config, sessionId)
|
|
1945
2319
|
if (dir === null) {
|
|
1946
2320
|
return json({ ok: false, error: { code: 'no-session-dir', message: '无法确定当前对话目录' } }, 400)
|
|
1947
2321
|
}
|