dsh-gallery 1.0.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 +143 -1
- package/README.md +18 -12
- package/client.js +390 -89
- package/host/activity.js +208 -0
- package/host/context.js +157 -25
- package/host/derive.js +21 -0
- package/host/http.js +174 -26
- package/host/regions.js +572 -0
- package/host/scan.js +75 -26
- package/host/tools.js +498 -26
- package/package.json +6 -3
package/host/http.js
CHANGED
|
@@ -24,7 +24,7 @@ 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
29
|
import { buildLinkedMaskIndex, framesFilePath } from './frames.js'
|
|
30
30
|
|
|
@@ -119,7 +119,7 @@ import { resolveReadable, serveMedia, contentTypeFor } from './media.js'
|
|
|
119
119
|
import { MARK_ACTIONS, readMarks, writeMarks, setMark, setMarksBatch, marksFilePath } from './marks.js'
|
|
120
120
|
import { readFeedback, setFeedback, setFeedbackBatch, ackFeedback, clearFeedback } from './feedback.js'
|
|
121
121
|
// 阶段 3 / C1:记下「人正在看哪张图」,单独一份临时状态文件
|
|
122
|
-
import { writeContext } from './context.js'
|
|
122
|
+
import { writeContext, writePanel } from './context.js'
|
|
123
123
|
// v0.16.4 A 档:把待处理反馈渲染成成稿文本(纯函数,不碰落盘数据)
|
|
124
124
|
import { makeFeedback, renderFeedbackText } from './feedback-text.js'
|
|
125
125
|
import { LABEL_CATEGORIES, readLabels, setLabel, setLabelsBatch, deleteLabels, labelsFilePath } from './labels.js'
|
|
@@ -175,31 +175,95 @@ function absoluteOf(roots, relPath) {
|
|
|
175
175
|
* ext —— 扩展名
|
|
176
176
|
* q —— 关键词(文件名 / 路径 / 对话标题 / 遮罩关联名)
|
|
177
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
|
+
|
|
178
226
|
/**
|
|
179
227
|
* 阶段 2:关键词命中打分(纯函数,导出给工具与测试复用)。
|
|
180
228
|
* 文件名 3 分 / 路径 2 分 / 对话标题 2 分 / 遮罩关联名 2 分,
|
|
181
229
|
* 再加 0~1 分的新鲜度;同分按时间倒序,再同分保持原顺序。
|
|
182
230
|
*/
|
|
231
|
+
/**
|
|
232
|
+
* 2026-11-16 第一档:关键词拆成多个词后再算分。
|
|
233
|
+
* 旧做法把整串当一个词去找(`hay.includes(整串)`):「门头 定稿」这种带空格的
|
|
234
|
+
* 输入必然落空。现在每个词单独算,命中词越多分越高,全中的排最前。
|
|
235
|
+
* 打分口径不变:文件名 3 / 路径 2 / 对话标题 2 / 遮罩来源名 2;每多命中一个词再加 4。
|
|
236
|
+
*/
|
|
183
237
|
export function relevanceScore(f, q, maskNameOf) {
|
|
184
|
-
const
|
|
185
|
-
if (
|
|
238
|
+
const terms = Array.isArray(q) ? q : splitQuery(q)
|
|
239
|
+
if (terms.length === 0) return 0
|
|
186
240
|
const name = String(f?.name ?? '').toLowerCase()
|
|
187
241
|
const rel = String(f?.relPath ?? '').toLowerCase()
|
|
188
242
|
const title = String(f?.sessionTitle ?? '').toLowerCase()
|
|
189
243
|
const mask = String((maskNameOf ? maskNameOf(f) : f?.maskOf?.sourceName) ?? '').toLowerCase()
|
|
190
244
|
let score = 0
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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
|
|
195
258
|
return score
|
|
196
259
|
}
|
|
197
260
|
|
|
198
261
|
/** 有关键词时按相关性排;没关键词时保持调用方给的顺序。 */
|
|
262
|
+
/** 有关键词时按相关性排;没关键词时保持调用方给的顺序。接受字符串或已拆好的词数组。 */
|
|
199
263
|
export function sortByRelevance(list, q, maskNameOf) {
|
|
200
|
-
const
|
|
264
|
+
const terms = Array.isArray(q) ? q : splitQuery(q)
|
|
201
265
|
const arr = Array.isArray(list) ? list.slice() : []
|
|
202
|
-
if (
|
|
266
|
+
if (terms.length === 0 || arr.length === 0) return arr
|
|
203
267
|
let min = Infinity
|
|
204
268
|
let max = -Infinity
|
|
205
269
|
for (const f of arr) {
|
|
@@ -210,7 +274,7 @@ export function sortByRelevance(list, q, maskNameOf) {
|
|
|
210
274
|
const span = max - min || 1
|
|
211
275
|
const scored = arr.map((f, i) => ({
|
|
212
276
|
f, i,
|
|
213
|
-
s: relevanceScore(f,
|
|
277
|
+
s: relevanceScore(f, terms, maskNameOf) + ((Number(f?.mtime) || 0) - min) / span,
|
|
214
278
|
}))
|
|
215
279
|
scored.sort((a, b) => (b.s - a.s)
|
|
216
280
|
|| ((Number(b.f?.mtime) || 0) - (Number(a.f?.mtime) || 0))
|
|
@@ -230,6 +294,28 @@ export async function applyAssetFilter(value, opts) {
|
|
|
230
294
|
marks = (await readMarks(settingsFile)).marks ?? {}
|
|
231
295
|
} catch { marks = {} }
|
|
232
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
|
+
|
|
233
319
|
const labelSet = filterLabel !== '' ? new Set([filterLabel]) : null
|
|
234
320
|
const categorySet = filterCategory !== '' ? new Set([filterCategory]) : null
|
|
235
321
|
|
|
@@ -241,13 +327,15 @@ export async function applyAssetFilter(value, opts) {
|
|
|
241
327
|
const ext = String(f?.extension ?? '')
|
|
242
328
|
if (filterExt !== '' && !ext.toLowerCase().endsWith(filterExt)) return false
|
|
243
329
|
|
|
244
|
-
if (
|
|
245
|
-
//
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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
|
|
251
339
|
}
|
|
252
340
|
|
|
253
341
|
const mark = marks[f?.relPath]
|
|
@@ -264,10 +352,13 @@ export async function applyAssetFilter(value, opts) {
|
|
|
264
352
|
}
|
|
265
353
|
}
|
|
266
354
|
|
|
267
|
-
const orderByQuery = (arr) => sortByRelevance(arr,
|
|
355
|
+
const orderByQuery = (arr) => sortByRelevance(arr, activeTerms)
|
|
268
356
|
|
|
269
357
|
const out = { ...value }
|
|
270
358
|
out.filtered = true
|
|
359
|
+
// 把实际用的词带回去:查图工具按同一套词排序,模糊兜底时两边口径才一致
|
|
360
|
+
out.queryTerms = activeTerms
|
|
361
|
+
out.queryFuzzy = queryFuzzy
|
|
271
362
|
|
|
272
363
|
if (Array.isArray(value?.groups)) {
|
|
273
364
|
const groups = []
|
|
@@ -656,6 +747,11 @@ export function registerOpsRoute(ctx, config) {
|
|
|
656
747
|
.sort((a, b) => b.length - a.length)[0] ?? ''
|
|
657
748
|
if (owner !== '') maskAbs = path.resolve(owner, maskRel.split('/').join(path.sep))
|
|
658
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
|
+
}
|
|
659
755
|
await writeContext(resolved.settingsFile, {
|
|
660
756
|
relPath: rel,
|
|
661
757
|
absPath: abs,
|
|
@@ -666,6 +762,7 @@ export function registerOpsRoute(ctx, config) {
|
|
|
666
762
|
maskAbsPath: maskAbs,
|
|
667
763
|
sessionId: String(raw?.sessionId ?? ''),
|
|
668
764
|
sessionTitle: String(raw?.sessionTitle ?? ''),
|
|
765
|
+
mtime: fileMtime,
|
|
669
766
|
})
|
|
670
767
|
return json({ ok: true })
|
|
671
768
|
} catch (error) {
|
|
@@ -673,6 +770,18 @@ export function registerOpsRoute(ctx, config) {
|
|
|
673
770
|
}
|
|
674
771
|
}
|
|
675
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
|
+
|
|
676
785
|
// 读取当前设置(含 root 来源、是否存在)
|
|
677
786
|
|
|
678
787
|
if (op === 'settings') {
|
|
@@ -1912,6 +2021,39 @@ export function registerOpsRoute(ctx, config) {
|
|
|
1912
2021
|
return abs
|
|
1913
2022
|
}
|
|
1914
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
|
+
|
|
1915
2057
|
/**
|
|
1916
2058
|
* 读会话标题 / 创建时间(派生资产登记用)。
|
|
1917
2059
|
*
|
|
@@ -2033,15 +2175,19 @@ export function registerOpsRoute(ctx, config) {
|
|
|
2033
2175
|
const opts = resolveOptions(config)
|
|
2034
2176
|
const sessionId = String(body?.sessionId ?? '')
|
|
2035
2177
|
const data = typeof body?.data === 'string' ? body.data : ''
|
|
2036
|
-
const ext = (typeof body?.ext === 'string' && body.ext !== '') ? body.ext : 'png'
|
|
2037
2178
|
if (data === '') {
|
|
2038
2179
|
return json({ ok: false, error: { code: 'bad-request', message: '缺少图片数据' } }, 400)
|
|
2039
2180
|
}
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
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)
|
|
2043
2186
|
}
|
|
2044
|
-
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)
|
|
2045
2191
|
if (dir === null) {
|
|
2046
2192
|
return json({ ok: false, error: { code: 'no-session-dir', message: '无法确定当前对话目录' } }, 400)
|
|
2047
2193
|
}
|
|
@@ -2089,7 +2235,8 @@ export function registerOpsRoute(ctx, config) {
|
|
|
2089
2235
|
if (srcStat.size > 2 * 1024 * 1024 * 1024) {
|
|
2090
2236
|
return json({ ok: false, error: { code: 'too-large', message: '文件超过 2 GB,粘贴被拒绝' } }, 400)
|
|
2091
2237
|
}
|
|
2092
|
-
|
|
2238
|
+
let dir = await resolveSessionDir(config, sessionId)
|
|
2239
|
+
if (dir === null) dir = await resolveFallbackDir(config, sessionId)
|
|
2093
2240
|
if (dir === null) {
|
|
2094
2241
|
return json({ ok: false, error: { code: 'no-session-dir', message: '无法确定当前对话目录' } }, 400)
|
|
2095
2242
|
}
|
|
@@ -2167,7 +2314,8 @@ export function registerOpsRoute(ctx, config) {
|
|
|
2167
2314
|
const opts = resolveOptions(config)
|
|
2168
2315
|
const sessionId = String(body?.sessionId ?? '')
|
|
2169
2316
|
const srcs = Array.isArray(body?.srcPaths) ? body.srcPaths : []
|
|
2170
|
-
|
|
2317
|
+
let dir = await resolveSessionDir(config, sessionId)
|
|
2318
|
+
if (dir === null) dir = await resolveFallbackDir(config, sessionId)
|
|
2171
2319
|
if (dir === null) {
|
|
2172
2320
|
return json({ ok: false, error: { code: 'no-session-dir', message: '无法确定当前对话目录' } }, 400)
|
|
2173
2321
|
}
|