dsh-gallery 0.13.6 → 0.14.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/host/http.js CHANGED
@@ -9,6 +9,7 @@
9
9
  */
10
10
 
11
11
  import { promises as fsp } from 'node:fs'
12
+ import { createHash } from 'node:crypto'
12
13
  import path from 'node:path'
13
14
 
14
15
  import { ROUTE, MEDIA_ROUTE } from './constants.js'
@@ -19,9 +20,24 @@ import {
19
20
  } from './settings.js'
20
21
  import { scanGallery } from './scan.js'
21
22
  import { resolveReadable, serveMedia, contentTypeFor } from './media.js'
22
- import { MARK_ACTIONS, readMarks, writeMarks, setMark } from './marks.js'
23
+ import { MARK_ACTIONS, readMarks, writeMarks, setMark, setMarksBatch, marksFilePath } from './marks.js'
23
24
 
24
25
  /** reveal 相关的排查日志(失败/异常时便于定位,不影响功能)。 */
26
+ /**
27
+ * 把相对路径解析到 roots 内的绝对路径(v0.13.7,删除闸门用)。
28
+ * 解析不出 / 越界返回 null。
29
+ */
30
+ function resolveUnderRoots(roots, relPath) {
31
+ if (!Array.isArray(roots) || roots.length === 0) return null
32
+ for (const root of roots) {
33
+ if (typeof root !== "string" || root === "") continue
34
+ const abs = path.resolve(root, relPath)
35
+ if (!isInside(root, abs)) continue
36
+ return abs
37
+ }
38
+ return null
39
+ }
40
+
25
41
  function LOG_RESOLVE(label, detail) {
26
42
  if (process.env.DSH_GALLERY_DEBUG === '1') {
27
43
  try { console.info('dsh-gallery ' + label, JSON.stringify(detail)) } catch { /* 忽略 */ }
@@ -515,6 +531,177 @@ export function registerOpsRoute(ctx, config) {
515
531
  }
516
532
  }
517
533
 
534
+ if (op === 'marksBatch') {
535
+ /**
536
+ * 批量设 / 清标记(v0.13.7)。
537
+ *
538
+ * 一次写入搞定多个卡片,避免「批量确认 20 张 → 20 次读改写」。
539
+ * 仍然是**只改标记文件、绝不改写自动分类**(数据边界)。
540
+ *
541
+ * body: { updates: [{ path, mark }] } mark 为 null 表示清除
542
+ */
543
+ const updates = Array.isArray(body?.updates) ? body.updates : null
544
+ if (updates === null) {
545
+ return json({ ok: false, error: { code: 'bad-request', message: '缺少 updates 数组' } }, 400)
546
+ }
547
+ if (updates.length > 200) {
548
+ return json({ ok: false, error: { code: 'too-many', message: '单次最多 200 条' } }, 400)
549
+ }
550
+ for (const u of updates) {
551
+ if (typeof u?.path !== 'string' || u.path === '') {
552
+ return json({ ok: false, error: { code: 'bad-request', message: 'updates 里有非法 path' } }, 400)
553
+ }
554
+ const m = u.mark
555
+ if (m !== null && m !== undefined) {
556
+ if (typeof m !== 'object' || !MARK_ACTIONS.includes(m.action)) {
557
+ return json({ ok: false, error: { code: 'bad-request', message: 'action 不合法:' + String(m?.action) } }, 400)
558
+ }
559
+ }
560
+ }
561
+ try {
562
+ const settingsFile = resolveOptions(config).settingsFile
563
+ const next = await setMarksBatch(settingsFile, updates)
564
+ return json({ ok: true, marks: next, applied: updates.length })
565
+ } catch (error) {
566
+ return json({ ok: false, error: { code: 'marks-write-failed', message: String(error?.message ?? error) } }, 500)
567
+ }
568
+ }
569
+
570
+ if (op === 'deleteDiscarded') {
571
+ /**
572
+ * 清掉废稿(v0.13.7)—— **永久删除、不进回收站、不可逆**。
573
+ *
574
+ * 安全闸门(缺一不可):
575
+ * 1. 两阶段:先 dryRun 拿清单,人看完再 confirm 真删
576
+ * 2. 清单一致性:真删时用 digest 校验「删的就是刚才看过的那批」
577
+ * 3. 每批上限 10 个(超过由前端分批,服务端再兜一层)
578
+ * 4. 只删 roots 内的普通文件
579
+ * 5. 绝不删插件自己的数据文件(settings / marks)
580
+ * 6. 删完同步清掉对应 marks,并返回被清的路径供前端清反馈篮
581
+ *
582
+ * body: { paths: string[], dryRun?: boolean, confirm?: boolean, digest?: string }
583
+ */
584
+ const paths = Array.isArray(body?.paths) ? body.paths : null
585
+ if (paths === null || paths.length === 0) {
586
+ return json({ ok: false, error: { code: 'bad-request', message: '缺少 paths 数组' } }, 400)
587
+ }
588
+ if (paths.length > 10) {
589
+ return json({
590
+ ok: false,
591
+ error: { code: 'batch-too-large', message: '单批最多 10 个,请分批处理', count: paths.length },
592
+ }, 400)
593
+ }
594
+
595
+ const opts = resolveOptions(config)
596
+ const settingsFile = opts.settingsFile
597
+ const marksFile = marksFilePath(settingsFile)
598
+ const forbidden = new Set([settingsFile, marksFile, settingsFile + '.tmp', marksFile + '.tmp'])
599
+
600
+ // ---- 逐个解析并校验 ----
601
+ const entries = []
602
+ const rejected = []
603
+ for (const p of paths) {
604
+ const relPath = String(p ?? '')
605
+ if (relPath === '') { rejected.push({ path: relPath, reason: 'empty-path' }); continue }
606
+ let abs
607
+ try {
608
+ abs = path.isAbsolute(relPath) ? relPath : resolveUnderRoots(opts.roots, relPath)
609
+ } catch {
610
+ rejected.push({ path: relPath, reason: 'outside-roots' }); continue
611
+ }
612
+ if (abs === null || abs === undefined) {
613
+ rejected.push({ path: relPath, reason: 'outside-roots' }); continue
614
+ }
615
+ // 插件自身数据文件一律拒绝
616
+ if (forbidden.has(abs)) {
617
+ rejected.push({ path: relPath, reason: 'plugin-data-file' }); continue
618
+ }
619
+ let st
620
+ try {
621
+ st = await fsp.stat(abs)
622
+ } catch {
623
+ rejected.push({ path: relPath, reason: 'not-found' }); continue
624
+ }
625
+ if (!st.isFile()) {
626
+ rejected.push({ path: relPath, reason: 'not-a-file' }); continue
627
+ }
628
+ entries.push({ relPath, absolute: abs, size: st.size })
629
+ }
630
+
631
+ // 清单指纹:路径排序后 hash,确保「确认的就是看过的」
632
+ const digestOf = (list) => {
633
+ const h = createHash('sha1')
634
+ for (const e of [...list].map((x) => x.absolute).sort()) h.update(e)
635
+ return h.digest('hex')
636
+ }
637
+ const digest = digestOf(entries)
638
+
639
+ // ---- dryRun:只列清单,不动磁盘 ----
640
+ if (body?.dryRun === true) {
641
+ return json({
642
+ ok: true,
643
+ dryRun: true,
644
+ count: entries.length,
645
+ digest,
646
+ files: entries.map((e) => ({ path: e.relPath, absolute: e.absolute, size: e.size })),
647
+ rejected,
648
+ })
649
+ }
650
+
651
+ // ---- 真删:必须显式 confirm + digest 一致 ----
652
+ if (body?.confirm !== true) {
653
+ return json({ ok: false, error: { code: 'confirm-required', message: '缺少 confirm 标记' } }, 400)
654
+ }
655
+ if (typeof body?.digest !== 'string' || body.digest !== digest) {
656
+ return json({
657
+ ok: false,
658
+ error: { code: 'digest-mismatch', message: '文件清单已变化(可能被重新扫描或改动),请重新确认' },
659
+ }, 409)
660
+ }
661
+
662
+ // ---- 执行删除(任一批失败立即停,不自动重试)----
663
+ const deleted = []
664
+ const failed = []
665
+ for (const e of entries) {
666
+ try {
667
+ await fsp.unlink(e.absolute)
668
+ deleted.push(e.relPath)
669
+ } catch (error) {
670
+ failed.push({ path: e.relPath, reason: String(error?.message ?? error) })
671
+ }
672
+ }
673
+
674
+ // ---- 同步清掉这些路径的 marks ----
675
+ let marksLeft = {}
676
+ if (deleted.length > 0) {
677
+ try {
678
+ const cur = await readMarks(settingsFile)
679
+ const next = { ...cur.marks }
680
+ for (const rp of deleted) delete next[rp]
681
+ await writeMarks(settingsFile, next)
682
+ marksLeft = next
683
+ } catch (error) {
684
+ return json({
685
+ ok: false,
686
+ error: {
687
+ code: 'marks-cleanup-failed',
688
+ message: '文件已删但标记清理失败:' + String(error?.message ?? error),
689
+ deleted,
690
+ },
691
+ }, 500)
692
+ }
693
+ }
694
+
695
+ return json({
696
+ ok: failed.length === 0,
697
+ deleted,
698
+ failed,
699
+ rejected,
700
+ marks: marksLeft,
701
+ note: `永久删除 ${deleted.length} 个文件(不进回收站)`,
702
+ })
703
+ }
704
+
518
705
  if (op === 'reveal') {
519
706
  /**
520
707
  * 在资源管理器里定位 / 用系统程序打开。
package/host/marks.js CHANGED
@@ -106,3 +106,31 @@ export async function setMark(settingsFile, relPath, mark) {
106
106
  return next
107
107
  })
108
108
  }
109
+
110
+ /**
111
+ * 批量设 / 清标记(v0.13.7)—— 一次读改写 + 一次原子写。
112
+ *
113
+ * 为什么批量:批量确认 20 张卡时,逐个 setMark 会触发 20 次「读文件 → 改 → 写」,
114
+ * 既慢又增加并发写窗口。这里串行化一次搞定。
115
+ *
116
+ * @param updates - [{ path: string, mark: object|null }] mark=null 表示清除
117
+ * @returns 更新后的 marks 对象
118
+ */
119
+ export async function setMarksBatch(settingsFile, updates) {
120
+ return enqueue(async () => {
121
+ const current = await readMarks(settingsFile)
122
+ const next = { ...current.marks }
123
+ for (const u of updates) {
124
+ const relPath = String(u?.path ?? "")
125
+ if (relPath === "") continue
126
+ const mark = u?.mark
127
+ if (mark === null || mark === undefined) {
128
+ delete next[relPath]
129
+ } else {
130
+ next[relPath] = { ...mark, markedAt: mark.markedAt ?? Date.now() }
131
+ }
132
+ }
133
+ await writeMarks(settingsFile, next)
134
+ return next
135
+ })
136
+ }
package/host/protocol.js CHANGED
@@ -23,3 +23,60 @@ export const VIDEO_EXTS = ['.mp4', '.webm', '.mov', '.m4v', '.mkv', '.avi', '.og
23
23
 
24
24
  /** 音频扩展名。 */
25
25
  export const AUDIO_EXTS = ['.mp3', '.wav', '.m4a', '.aac', '.flac', '.ogg', '.oga', '.opus', '.wma']
26
+
27
+ /**
28
+ * 框选批注的坐标契约(v0.14.0)—— Host 与 Client 的唯一真源。
29
+ *
30
+ * 为什么用归一化坐标:批注要落在「图片的某个位置」上,而不是「屏幕上某个像素」。
31
+ * 写像素值的话,换个窗口尺寸 / 换个缩放比例就全错了。
32
+ * 用相对宽高的 0–1 比例,无论怎么缩放都能还原到同一块区域。
33
+ *
34
+ * 形状类型:
35
+ * rect —— 矩形框(最主要,圈出「这块不对」)
36
+ * arrow —— 箭头(指向具体某点)
37
+ * text —— 纯文字标注(钉在某位置,不带框)
38
+ */
39
+ export const ANNOTATION_SHAPES = ["rect", "arrow", "text"]
40
+
41
+ /**
42
+ * 一条批注的数据形态:
43
+ * {
44
+ * id: string, // 稳定 id(同一次会话内唯一)
45
+ * shape: "rect|arrow|text",
46
+ * x: number, y: number, // 起点,归一化 0–1
47
+ * w: number, h: number, // 宽高,归一化 0–1(text/arrow 可只用 x,y)
48
+ * note: string, // 人写的说明(可为空)
49
+ * frameMs: number|null, // 视频帧时间(图片为 null)
50
+ * }
51
+ *
52
+ * 约束:x/y/w/h 必须落在 [0,1];越界由写入端 clamp。
53
+ */
54
+ export const ANNOTATION_LIMITS = { min: 0, max: 1, maxNoteLength: 500, maxPerAsset: 50 }
55
+
56
+ /** 把任意数值 clamp 到 [0,1](批注坐标写入前必须过一遍)。 */
57
+ export function clamp01(v) {
58
+ const n = Number(v)
59
+ if (!Number.isFinite(n)) return 0
60
+ if (n < 0) return 0
61
+ if (n > 1) return 1
62
+ return n
63
+ }
64
+
65
+ /** 校验并规整一条批注;不合法返回 null。 */
66
+ export function normalizeAnnotation(a) {
67
+ if (a === null || typeof a !== "object") return null
68
+ const shape = ANNOTATION_SHAPES.includes(a.shape) ? a.shape : "rect"
69
+ const note = typeof a.note === "string" ? a.note.slice(0, ANNOTATION_LIMITS.maxNoteLength) : ""
70
+ const x = clamp01(a.x)
71
+ const y = clamp01(a.y)
72
+ let w = clamp01(a.w)
73
+ let h = clamp01(a.h)
74
+ // 宽高至少留一点,且不越过右/下边界
75
+ if (w > 0 && x + w > 1) w = 1 - x
76
+ if (h > 0 && y + h > 1) h = 1 - y
77
+ const frameMs = Number.isFinite(a.frameMs) ? Math.max(0, Math.floor(a.frameMs)) : null
78
+ return {
79
+ id: typeof a.id === "string" && a.id !== "" ? a.id : ("a" + Math.random().toString(36).slice(2, 10)),
80
+ shape, x, y, w, h, note, frameMs,
81
+ }
82
+ }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "dsh-gallery",
3
- "version": "0.13.6",
3
+ "version": "0.14.0",
4
4
  "type": "module",
5
- "description": "产出画廊:解决 DSH 只在任务结尾才列出产出、过程不可见的问题。实时监控产出目录,图片自动刷新、文件原生预览、音视频面板内直接播放(实测 127MB 可拖;GB 级未分档),还能一键截取视频当前帧,并可给产出打判定攒成一条决策草稿(人工确认后发送),任务进行中即可校验纠正。",
5
+ "description": "产出画廊:图片 / 文件 / 音视频实时可见与当场校验;按标记筛选、把「定版」真正锁住、批量处置;图上圈选批注攒成决策草稿,人工确认后送回对话。Output gallery for DeepSeek Harness: live image, file and media gallery with marker filters, persisted confirmation, batch actions, region annotation and human-approved decision drafts.",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git+https://github.com/Hope2023Dcr/dsh-gallery.git"
@@ -58,7 +58,13 @@
58
58
  "media-gallery",
59
59
  "video-preview",
60
60
  "audio-preview",
61
- "range-seek"
61
+ "range-seek",
62
+ "filter",
63
+ "deliverable",
64
+ "confirm",
65
+ "batch",
66
+ "region-selection",
67
+ "annotation"
62
68
  ],
63
69
  "publishConfig": {
64
70
  "access": "public",