dsh-gallery 0.14.1 → 0.14.3

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.
@@ -1,206 +1,231 @@
1
- // GENERATED from src/shared/feedback-text.js — 请勿手改
2
- /**
3
- * 反馈契约 v1 —— 纯函数,Host 与 Client 双端复用。
4
- *
5
- * 由 scripts/build.mjs 沿用 protocol.js 的做法:
6
- * · 剥掉 export 注入 client bundle
7
- * · 生成 host/feedback-text.js 供 Host 侧 import
8
- *
9
- * 设计原则(调研结论,见 docs/migration.md 与调研报告):
10
- * 1. 反馈是**契约**不是自由文本:带锚点(relPath)、动作类型、理由、目标会话。
11
- * 2. **攒批 + 一次发送**:多条标记合成一条消息,不逐条发。
12
- * 3. **人确认闸门**:本模块只负责「产出草稿文本」,发送由用户在 composer 里点。
13
- * 4. **显式 changeOnly / keep**:生成类工作流实证「只改 X、其余锁死」远好于「再改改」。
14
- * 5. 逐条用 `Annotation N:` 编号,便于模型逐条回应(参考 dsh-quote-annotate-pro 契约)。
15
- */
16
-
17
- /** 首版 6 个动作。值即契约里的 action 字符串,不要随意改(已写进 marks 文件)。 */
18
- export const ACTIONS = [
19
- 'approve',
20
- 'reject',
21
- 'finalize',
22
- 'redo-one-dim',
23
- 'regenerate-selected',
24
- 'set-reference',
25
- ]
26
-
27
- /** 动作 → 中文动词(生成草稿正文用)。 */
28
- export const ACTION_LABELS_ZH = {
29
- approve: '通过',
30
- reject: '不要这张',
31
- finalize: '已定稿',
32
- 'redo-one-dim': '单维重做',
33
- 'regenerate-selected': '选择性重生成',
34
- 'set-reference': '设为参照',
35
- }
36
-
37
- /** 动作 → 英文动词。 */
38
- export const ACTION_LABELS_EN = {
39
- approve: 'approve',
40
- reject: 'reject',
41
- finalize: 'deliver',
42
- 'redo-one-dim': 'redo one dimension',
43
- 'regenerate-selected': 'regenerate',
44
- 'set-reference': 'set as reference',
45
- }
46
-
47
- /** 契约版本。检查项会核对。 */
48
- export const SCHEMA = 1
49
-
50
- /** 来源标识。 */
51
- export const SOURCE = 'dsh-gallery'
52
-
53
- /** 常用「只改」维度候选(给 UI 做快捷输入,不是强制枚举)。 */
54
- export const CHANGE_CANDIDATES = [
55
- '色调', '亮度', '材质', '构图', '透视', '比例', '文字', '光线', '风格', '细节',
56
- ]
57
-
58
- /** 常用的「锁死」项候选。 */
59
- export const KEEP_CANDIDATES = [
60
- '构图', '人物位置', '主体比例', '透视角度', '文字内容', '整体风格', '配色基调',
61
- ]
62
-
63
- /**
64
- * 构造一条反馈项。
65
- * @param item - { relPath, category, action, reason?, changeOnly?, keep?, preserve?, region?, params? }
66
- * @returns 规范化后的 feedback item
67
- */
68
- export function makeItem(item) {
69
- const out = {
70
- relPath: String(item?.relPath ?? ''),
71
- category: String(item?.category ?? 'unknown'),
72
- action: ACTIONS.includes(item?.action) ? item.action : 'approve',
73
- }
74
- if (typeof item?.reason === 'string' && item.reason.trim() !== '') out.reason = item.reason.trim()
75
- if (Array.isArray(item?.changeOnly) && item.changeOnly.length > 0) {
76
- out.changeOnly = item.changeOnly.map((x) => String(x)).filter((x) => x !== '')
77
- }
78
- if (Array.isArray(item?.keep) && item.keep.length > 0) {
79
- out.keep = item.keep.map((x) => String(x)).filter((x) => x !== '')
80
- }
81
- if (Array.isArray(item?.preserve) && item.preserve.length > 0) {
82
- out.preserve = item.preserve.map((x) => String(x)).filter((x) => x !== '')
83
- }
84
- if (item?.region !== undefined && item.region !== null) out.region = item.region
85
- if (item?.params !== undefined && item.params !== null) out.params = item.params
86
- return out
87
- }
88
-
89
- /**
90
- * 构造一份反馈草稿(契约对象)。
91
- * @param args - { sessionId, items, status? }
92
- */
93
- export function makeFeedback(args) {
94
- return {
95
- schema: SCHEMA,
96
- source: SOURCE,
97
- sessionId: String(args?.sessionId ?? ''),
98
- status: args?.status === 'sent' ? 'sent' : 'draft',
99
- items: (Array.isArray(args?.items) ? args.items : []).map(makeItem),
100
- createdAt: typeof args?.createdAt === 'number' ? args.createdAt : Date.now(),
101
- }
102
- }
103
-
104
- /** 文件名取路径末段。 */
105
- function baseName(relPath) {
106
- const s = String(relPath ?? '')
107
- const i = s.lastIndexOf('/')
108
- return i >= 0 ? s.slice(i + 1) : s
109
- }
110
-
111
- /**
112
- * 把契约渲染成**人类与模型都能读的草稿正文**。
113
- *
114
- * 关键要求(调研结论 4):每条必须同时写「只改 X」与「其余锁死」,
115
- * 否则模型容易把整张重画。
116
- *
117
- * @param feedback - makeFeedback() 的产物
118
- * @param options - { locale?: 'zh'|'en', targetSessionTitle?: string }
119
- * @returns 纯文本(可直接 setDraft 进 composer)
120
- */
121
- export function renderFeedbackText(feedback, options) {
122
- const locale = options?.locale === 'en' ? 'en' : 'zh'
123
- const labels = locale === 'en' ? ACTION_LABELS_EN : ACTION_LABELS_ZH
124
- const items = Array.isArray(feedback?.items) ? feedback.items : []
125
-
126
- if (items.length === 0) {
127
- return locale === 'en'
128
- ? '(No items in the feedback basket.)'
129
- : '(反馈篮里还没有条目。)'
130
- }
131
-
132
- const lines = []
133
- if (locale === 'en') {
134
- lines.push(`[Feedback from dsh-gallery · ${items.length} item(s)]`)
135
- lines.push('')
136
- lines.push('Reply item by item using the `Annotation N:` prefix, so each decision maps to one annotation.')
137
- lines.push('')
138
- } else {
139
- lines.push(`【产出画廊反馈 · 共 ${items.length} 条】`)
140
- lines.push('')
141
- lines.push('请按 `Annotation 1:`、`Annotation 2:` … 逐条回应,让每条决定都对应到一个批注。')
142
- lines.push('')
143
- }
144
-
145
- items.forEach((item, idx) => {
146
- const n = idx + 1
147
- const name = baseName(item.relPath)
148
- const hasRegion = item.region !== undefined && item.region !== null
149
- // 带框选区域的条目来自「框选批注」:动词写清楚是「只改这个框里的东西」,
150
- // 比抽象动作名(单维重做)更贴近用户真正想说的话。
151
- const verb = hasRegion
152
- ? (locale === 'en' ? 'Change the boxed area only' : '按框选区域修改')
153
- : (labels[item.action] ?? item.action)
154
- lines.push(`Annotation ${n}:${verb} —— ${name}`)
155
- lines.push(`${locale === 'en' ? 'Path' : '路径'}:${item.relPath}`)
156
- if (item.reason !== undefined) {
157
- lines.push(`${locale === 'en' ? 'Reason' : '理由'}:${item.reason}`)
158
- }
159
- if (Array.isArray(item.changeOnly) && item.changeOnly.length > 0) {
160
- lines.push(locale === 'en'
161
- ? `changeOnly: ${item.changeOnly.join(' / ')}`
162
- : `只改:${item.changeOnly.join(' / ')}`)
163
- }
164
- if (Array.isArray(item.keep) && item.keep.length > 0) {
165
- lines.push(locale === 'en'
166
- ? `keep locked: ${item.keep.join(' / ')}`
167
- : `锁死(其余不动):${item.keep.join(' / ')}`)
168
- }
169
- if (Array.isArray(item.preserve) && item.preserve.length > 0) {
170
- lines.push(locale === 'en'
171
- ? `preserve: ${item.preserve.join(' / ')}`
172
- : `保留:${item.preserve.join(' / ')}`)
173
- }
174
- if (hasRegion) {
175
- // 人话百分比,别丢 JSON 给模型看:左上角 + 宽高,相对整张图
176
- const pct = (v) => {
177
- const num = Number(v)
178
- return Number.isFinite(num) ? Math.round(Math.max(0, Math.min(1, num)) * 100) : 0
179
- }
180
- const r = item.region
181
- lines.push(locale === 'en'
182
- ? `Region: left ${pct(r.x)}%, top ${pct(r.y)}%, width ${pct(r.w)}%, height ${pct(r.h)}% (relative to the whole image)`
183
- : `区域:左 ${pct(r.x)}%、上 ${pct(r.y)}%,宽 ${pct(r.w)}%、高 ${pct(r.h)}%(相对整张图)`)
184
- }
185
- if (item.params !== undefined && item.params !== null) {
186
- lines.push(`${locale === 'en' ? 'Params' : '参数'}:${JSON.stringify(item.params)}`)
187
- }
188
- lines.push('')
189
- })
190
-
191
- // 收口指令:把「锁死」原则再强调一次(防模型整张重画)
192
- if (locale === 'en') {
193
- lines.push('Unless an annotation says otherwise, keep everything else unchanged — do not regenerate wholesale.')
194
- } else {
195
- lines.push('除上述明确要改的部分外,其余全部保持原样,不要整张重做。')
196
- }
197
-
198
- return lines.join('\n')
199
- }
200
-
201
- /**
202
- * 把契约序列化成 JSON(导出 / 落盘 / 审计用,与渲染同一个 shape)。
203
- */
204
- export function serializeFeedback(feedback) {
205
- return JSON.stringify(feedback, null, 2)
206
- }
1
+ // GENERATED from src/shared/feedback-text.js — 请勿手改
2
+ /**
3
+ * 反馈契约 v1 —— 纯函数,Host 与 Client 双端复用。
4
+ *
5
+ * 由 scripts/build.mjs 沿用 protocol.js 的做法:
6
+ * · 剥掉 export 注入 client bundle
7
+ * · 生成 host/feedback-text.js 供 Host 侧 import
8
+ *
9
+ * 设计原则(调研结论,见 docs/migration.md 与调研报告):
10
+ * 1. 反馈是**契约**不是自由文本:带锚点(relPath)、动作类型、理由、目标会话。
11
+ * 2. **攒批 + 一次发送**:多条标记合成一条消息,不逐条发。
12
+ * 3. **人确认闸门**:本模块只负责「产出草稿文本」,发送由用户在 composer 里点。
13
+ * 4. **显式 changeOnly / keep**:生成类工作流实证「只改 X、其余锁死」远好于「再改改」。
14
+ * 5. 逐条用 `Annotation N:` 编号,便于模型逐条回应(参考 dsh-quote-annotate-pro 契约)。
15
+ */
16
+
17
+ /** 首版 6 个动作。值即契约里的 action 字符串,不要随意改(已写进 marks 文件)。 */
18
+ export const ACTIONS = [
19
+ 'approve',
20
+ 'reject',
21
+ 'finalize',
22
+ 'redo-one-dim',
23
+ 'regenerate-selected',
24
+ 'set-reference',
25
+ // v0.14.2 手动调整标签的三条反馈语义(旧 6 个保留以兼容历史 marks)
26
+ 'finalize-asset',
27
+ 'reject-delivery',
28
+ 'set-label',
29
+ ]
30
+
31
+ /** 动作 → 中文动词(生成草稿正文用)。 */
32
+ export const ACTION_LABELS_ZH = {
33
+ approve: '通过',
34
+ reject: '不要这张',
35
+ finalize: '已定稿',
36
+ 'redo-one-dim': '单维重做',
37
+ 'regenerate-selected': '选择性重生成',
38
+ 'set-reference': '设为参照',
39
+ 'finalize-asset': '终止枚举,以此图为定稿',
40
+ 'reject-delivery': '交付不合格',
41
+ 'set-label': '调整产出标签',
42
+ }
43
+
44
+ /** 动作 → 英文动词。 */
45
+ export const ACTION_LABELS_EN = {
46
+ approve: 'approve',
47
+ reject: 'reject',
48
+ finalize: 'deliver',
49
+ 'redo-one-dim': 'redo one dimension',
50
+ 'regenerate-selected': 'regenerate',
51
+ 'set-reference': 'set as reference',
52
+ 'finalize-asset': 'Stop enumerating; this asset is final',
53
+ 'reject-delivery': 'Delivery rejected',
54
+ 'set-label': 'Adjust asset label',
55
+ }
56
+
57
+ /** 契约版本。检查项会核对。 */
58
+ export const SCHEMA = 1
59
+
60
+ /** 来源标识。 */
61
+ export const SOURCE = 'dsh-gallery'
62
+
63
+ /** 常用「只改」维度候选(给 UI 做快捷输入,不是强制枚举)。 */
64
+ export const CHANGE_CANDIDATES = [
65
+ '色调', '亮度', '材质', '构图', '透视', '比例', '文字', '光线', '风格', '细节',
66
+ ]
67
+
68
+ /** 常用的「锁死」项候选。 */
69
+ export const KEEP_CANDIDATES = [
70
+ '构图', '人物位置', '主体比例', '透视角度', '文字内容', '整体风格', '配色基调',
71
+ ]
72
+
73
+ /**
74
+ * 构造一条反馈项。
75
+ * @param item - { relPath, category, action, reason?, changeOnly?, keep?, preserve?, region?, params? }
76
+ * @returns 规范化后的 feedback item
77
+ */
78
+ export function makeItem(item) {
79
+ const out = {
80
+ relPath: String(item?.relPath ?? ''),
81
+ category: String(item?.category ?? 'unknown'),
82
+ action: ACTIONS.includes(item?.action) ? item.action : 'approve',
83
+ }
84
+ if (typeof item?.reason === 'string' && item.reason.trim() !== '') out.reason = item.reason.trim()
85
+ if (Array.isArray(item?.changeOnly) && item.changeOnly.length > 0) {
86
+ out.changeOnly = item.changeOnly.map((x) => String(x)).filter((x) => x !== '')
87
+ }
88
+ if (Array.isArray(item?.keep) && item.keep.length > 0) {
89
+ out.keep = item.keep.map((x) => String(x)).filter((x) => x !== '')
90
+ }
91
+ if (Array.isArray(item?.preserve) && item.preserve.length > 0) {
92
+ out.preserve = item.preserve.map((x) => String(x)).filter((x) => x !== '')
93
+ }
94
+ if (item?.region !== undefined && item.region !== null) out.region = item.region
95
+ if (item?.params !== undefined && item.params !== null) out.params = item.params
96
+ // v0.14.3 截帧来源:随草稿一起带出,模型才知道反馈针对的是哪一帧
97
+ if (item?.frameOf !== undefined && item?.frameOf !== null && typeof item.frameOf === 'object') {
98
+ out.frameOf = {
99
+ videoName: String(item.frameOf.videoName ?? ''),
100
+ videoRelPath: String(item.frameOf.videoRelPath ?? ''),
101
+ timecode: String(item.frameOf.timecode ?? ''),
102
+ frameMs: Number(item.frameOf.frameMs) || 0,
103
+ }
104
+ }
105
+ return out
106
+ }
107
+
108
+ /**
109
+ * 构造一份反馈草稿(契约对象)。
110
+ * @param args - { sessionId, items, status? }
111
+ */
112
+ export function makeFeedback(args) {
113
+ return {
114
+ schema: SCHEMA,
115
+ source: SOURCE,
116
+ sessionId: String(args?.sessionId ?? ''),
117
+ status: args?.status === 'sent' ? 'sent' : 'draft',
118
+ items: (Array.isArray(args?.items) ? args.items : []).map(makeItem),
119
+ createdAt: typeof args?.createdAt === 'number' ? args.createdAt : Date.now(),
120
+ }
121
+ }
122
+
123
+ /** 文件名取路径末段。 */
124
+ function baseName(relPath) {
125
+ const s = String(relPath ?? '')
126
+ const i = s.lastIndexOf('/')
127
+ return i >= 0 ? s.slice(i + 1) : s
128
+ }
129
+
130
+ /**
131
+ * 把契约渲染成**人类与模型都能读的草稿正文**。
132
+ *
133
+ * 关键要求(调研结论 4):每条必须同时写「只改 X」与「其余锁死」,
134
+ * 否则模型容易把整张重画。
135
+ *
136
+ * @param feedback - makeFeedback() 的产物
137
+ * @param options - { locale?: 'zh'|'en', targetSessionTitle?: string }
138
+ * @returns 纯文本(可直接 setDraft 进 composer)
139
+ */
140
+ export function renderFeedbackText(feedback, options) {
141
+ const locale = options?.locale === 'en' ? 'en' : 'zh'
142
+ const labels = locale === 'en' ? ACTION_LABELS_EN : ACTION_LABELS_ZH
143
+ const items = Array.isArray(feedback?.items) ? feedback.items : []
144
+
145
+ if (items.length === 0) {
146
+ return locale === 'en'
147
+ ? '(No items in the feedback basket.)'
148
+ : '(反馈篮里还没有条目。)'
149
+ }
150
+
151
+ const lines = []
152
+ if (locale === 'en') {
153
+ lines.push(`[Feedback from dsh-gallery · ${items.length} item(s)]`)
154
+ lines.push('')
155
+ lines.push('Reply item by item using the `Annotation N:` prefix, so each decision maps to one annotation.')
156
+ lines.push('')
157
+ } else {
158
+ lines.push(`【产出画廊反馈 · 共 ${items.length} 条】`)
159
+ lines.push('')
160
+ lines.push('请按 `Annotation 1:`、`Annotation 2:` … 逐条回应,让每条决定都对应到一个批注。')
161
+ lines.push('')
162
+ }
163
+
164
+ items.forEach((item, idx) => {
165
+ const n = idx + 1
166
+ const name = baseName(item.relPath)
167
+ const hasRegion = item.region !== undefined && item.region !== null
168
+ // 带框选区域的条目来自「框选批注」:动词写清楚是「只改这个框里的东西」,
169
+ // 比抽象动作名(单维重做)更贴近用户真正想说的话。
170
+ const verb = hasRegion
171
+ ? (locale === 'en' ? 'Change the boxed area only' : '按框选区域修改')
172
+ : (labels[item.action] ?? item.action)
173
+ lines.push(`Annotation ${n}:${verb} —— ${name}`)
174
+ lines.push(`${locale === 'en' ? 'Path' : '路径'}:${item.relPath}`)
175
+ if (item.frameOf !== undefined && item.frameOf !== null) {
176
+ const f = item.frameOf
177
+ lines.push(locale === 'en'
178
+ ? `Frame source: taken from "${f.videoName}" at ${f.timecode} (${f.frameMs} ms)`
179
+ : `帧来源:取自视频《${f.videoName}》${f.timecode}(第 ${f.frameMs} 毫秒)`)
180
+ }
181
+ if (item.reason !== undefined) {
182
+ lines.push(`${locale === 'en' ? 'Reason' : '理由'}:${item.reason}`)
183
+ }
184
+ if (Array.isArray(item.changeOnly) && item.changeOnly.length > 0) {
185
+ lines.push(locale === 'en'
186
+ ? `changeOnly: ${item.changeOnly.join(' / ')}`
187
+ : `只改:${item.changeOnly.join(' / ')}`)
188
+ }
189
+ if (Array.isArray(item.keep) && item.keep.length > 0) {
190
+ lines.push(locale === 'en'
191
+ ? `keep locked: ${item.keep.join(' / ')}`
192
+ : `锁死(其余不动):${item.keep.join(' / ')}`)
193
+ }
194
+ if (Array.isArray(item.preserve) && item.preserve.length > 0) {
195
+ lines.push(locale === 'en'
196
+ ? `preserve: ${item.preserve.join(' / ')}`
197
+ : `保留:${item.preserve.join(' / ')}`)
198
+ }
199
+ if (hasRegion) {
200
+ // 人话百分比,别丢 JSON 给模型看:左上角 + 宽高,相对整张图
201
+ const pct = (v) => {
202
+ const num = Number(v)
203
+ return Number.isFinite(num) ? Math.round(Math.max(0, Math.min(1, num)) * 100) : 0
204
+ }
205
+ const r = item.region
206
+ lines.push(locale === 'en'
207
+ ? `Region: left ${pct(r.x)}%, top ${pct(r.y)}%, width ${pct(r.w)}%, height ${pct(r.h)}% (relative to the whole image)`
208
+ : `区域:左 ${pct(r.x)}%、上 ${pct(r.y)}%,宽 ${pct(r.w)}%、高 ${pct(r.h)}%(相对整张图)`)
209
+ }
210
+ if (item.params !== undefined && item.params !== null) {
211
+ lines.push(`${locale === 'en' ? 'Params' : '参数'}:${JSON.stringify(item.params)}`)
212
+ }
213
+ lines.push('')
214
+ })
215
+
216
+ // 收口指令:把「锁死」原则再强调一次(防模型整张重画)
217
+ if (locale === 'en') {
218
+ lines.push('Unless an annotation says otherwise, keep everything else unchanged — do not regenerate wholesale.')
219
+ } else {
220
+ lines.push('除上述明确要改的部分外,其余全部保持原样,不要整张重做。')
221
+ }
222
+
223
+ return lines.join('\n')
224
+ }
225
+
226
+ /**
227
+ * 把契约序列化成 JSON(导出 / 落盘 / 审计用,与渲染同一个 shape)。
228
+ */
229
+ export function serializeFeedback(feedback) {
230
+ return JSON.stringify(feedback, null, 2)
231
+ }
package/host/frames.js ADDED
@@ -0,0 +1,115 @@
1
+ /**
2
+ * 视频截帧登记(v0.14.3):独立文件 dsh-gallery-frames.json。
3
+ *
4
+ * 为什么需要它:
5
+ * 画廊按「会话日志里出现过的资产路径」判断某个文件属于哪个对话。
6
+ * 新截出来的 PNG 不可能出现在历史日志里,直接写进磁盘会被归到
7
+ * 「历史资产」组,老板在当前对话里根本找不到,也就没法给它写反馈。
8
+ * 这里只登记 { 文件 → 来源视频 / 帧时间 / 所属对话 },不写宿主会话日志,
9
+ * 扫描时据此把截帧挂回当前对话。
10
+ *
11
+ * 与 marks.json(反馈篮)、labels.json(手动标签)分开:
12
+ * · 帧归属是**文件身份**,不随清空反馈篮或改标签而消失;
13
+ * · 文件被删掉后留下孤儿记录无害(扫描只对存在的生效)。
14
+ *
15
+ * 数据形态:
16
+ * {
17
+ * "schema": 1,
18
+ * "frames": {
19
+ * "<relPath>": {
20
+ * "sessionId": "...", "sessionTitle": "", "sessionCreatedAt": 0,
21
+ * "videoRelPath": "...", "videoName": "...", "frameMs": 60000,
22
+ * "timecode": "00-01-00-000", "savedAt": 1700000000000
23
+ * }
24
+ * }
25
+ * }
26
+ *
27
+ * 键用 relPath(与 marks / labels 一致),文件被移动后不误命中。
28
+ */
29
+
30
+ import { promises as fsp } from 'node:fs'
31
+ import path from 'node:path'
32
+
33
+ const FRAMES_FILENAME = 'dsh-gallery-frames.json'
34
+
35
+ /** 进程内写队列:把「读-改-写」串起来,避免连续截帧互相覆盖。 */
36
+ let writeChain = Promise.resolve()
37
+
38
+ function enqueue(fn) {
39
+ const next = writeChain.then(fn, fn)
40
+ writeChain = next.catch(() => {})
41
+ return next
42
+ }
43
+
44
+ export function framesFilePath(settingsFile) {
45
+ return path.join(path.dirname(settingsFile), FRAMES_FILENAME)
46
+ }
47
+
48
+ /** 读登记。文件不存在 / 格式坏都返回空对象,绝不抛错。 */
49
+ export async function readFrames(settingsFile) {
50
+ try {
51
+ const raw = await fsp.readFile(framesFilePath(settingsFile), 'utf8')
52
+ const parsed = JSON.parse(raw)
53
+ const frames = (parsed !== null && typeof parsed === 'object' &&
54
+ typeof parsed.frames === 'object' && parsed.frames !== null) ? parsed.frames : {}
55
+ return { schema: 1, frames }
56
+ } catch {
57
+ return { schema: 1, frames: {} }
58
+ }
59
+ }
60
+
61
+ /** 原子写(tmp → rename),tmp 名带 pid + 随机数,并发不互踩。 */
62
+ async function writeFrames(settingsFile, frames) {
63
+ const file = framesFilePath(settingsFile)
64
+ const tmp = `${file}.tmp-${process.pid}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`
65
+ await fsp.mkdir(path.dirname(file), { recursive: true })
66
+ await fsp.writeFile(tmp, JSON.stringify({ schema: 1, frames: frames ?? {} }, null, 2))
67
+ await fsp.rename(tmp, file)
68
+ }
69
+
70
+ /** 登记一帧(同 relPath 覆盖,避免重复截同一帧留下多条)。 */
71
+ export async function registerFrame(settingsFile, frame) {
72
+ return enqueue(async () => {
73
+ const current = await readFrames(settingsFile)
74
+ const next = { ...current.frames, [String(frame.relPath)]: { ...frame, savedAt: Date.now() } }
75
+ await writeFrames(settingsFile, next)
76
+ return next
77
+ })
78
+ }
79
+
80
+ /** 文件被删除后清掉登记,保持文件干净。 */
81
+ export async function deleteFrames(settingsFile, relPaths) {
82
+ if (!Array.isArray(relPaths) || relPaths.length === 0) return {}
83
+ return enqueue(async () => {
84
+ const current = await readFrames(settingsFile)
85
+ const next = { ...current.frames }
86
+ for (const p of relPaths) delete next[String(p)]
87
+ await writeFrames(settingsFile, next)
88
+ return next
89
+ })
90
+ }
91
+
92
+ /**
93
+ * 帧时间码:HH-MM-SS-mmm。
94
+ * 用「-」而不是「:」,Windows 文件名不允许冒号;精确到毫秒,
95
+ * 同一秒内连截多帧也能分清先后。
96
+ */
97
+ export function frameTimecode(frameMs) {
98
+ const total = Math.max(0, Math.round(Number(frameMs) || 0))
99
+ const hh = String(Math.floor(total / 3600000)).padStart(2, '0')
100
+ const mm = String(Math.floor((total % 3600000) / 60000)).padStart(2, '0')
101
+ const ss = String(Math.floor((total % 60000) / 1000)).padStart(2, '0')
102
+ const ms = String(total % 1000).padStart(3, '0')
103
+ return `${hh}-${mm}-${ss}-${ms}`
104
+ }
105
+
106
+ /**
107
+ * 截帧文件名:`<视频名>_帧_<时间码>.png`。
108
+ * 例:`卖斋翁_EP01_这是救命钱_帧_00-01-00-500.png`
109
+ * 视频名里 Windows 不允许的字符一律换掉,并限长避免超长路径。
110
+ */
111
+ export function makeFrameName(videoName, frameMs, suffix = '') {
112
+ const raw = String(videoName ?? 'video').replace(/\.[^.]+$/, '')
113
+ const safe = raw.replace(/[\\/:*?"<>|\r\n\t]/g, '_').replace(/\s+/g, ' ').trim().slice(0, 80) || 'video'
114
+ return `${safe}_帧_${frameTimecode(frameMs)}${suffix}.png`
115
+ }
package/host/groups.js CHANGED
@@ -3,8 +3,12 @@
3
3
  *
4
4
  * S1 拆分自 index.js(行为零变化)。
5
5
  */
6
+ import { stateOfSession } from './session-index.js'
6
7
 
7
- export function buildGroups(summary, maxPerGroup = 120, currentSessionId = '') {
8
+ /**
9
+ * @param states - readSessionStates() 的结果;只用于给分组加状态标注,不参与排序。
10
+ */
11
+ export function buildGroups(summary, maxPerGroup = 120, currentSessionId = '', states = null) {
8
12
  if (!Array.isArray(summary) || summary.length === 0) return []
9
13
 
10
14
  const groups = new Map()
@@ -31,6 +35,8 @@ export function buildGroups(summary, maxPerGroup = 120, currentSessionId = '') {
31
35
  // 客户端卡片要靠这两个字段判断渲染类型(v0.10.1)
32
36
  extension: item.extension ?? ('.' + item.relPath.split('/').pop().split('.').pop().toLowerCase()),
33
37
  ...(item.mediaType !== undefined ? { mediaType: item.mediaType } : {}),
38
+ // 截帧来源(v0.14.3):反馈篮据此显示「取自《视频》00-01-00-500 帧」
39
+ ...(item.frameOf !== undefined ? { frameOf: item.frameOf } : {}),
34
40
  })
35
41
  if (item.sessionId === null && item.mtime > g.createdAt) g.createdAt = item.mtime
36
42
  }
@@ -44,11 +50,15 @@ export function buildGroups(summary, maxPerGroup = 120, currentSessionId = '') {
44
50
  }
45
51
  // 只回传前 maxPerGroup 条明细(客户端滚动到底再按需请求)
46
52
  const returned = g.files.slice(0, maxPerGroup)
53
+ // 当前打开的会话:客户端据此置顶并默认展开(0.14.1 原行为,不动)
54
+ const isCurrent = currentSessionId !== '' && g.sessionId === currentSessionId
47
55
  out.push({
48
56
  sessionId: g.sessionId,
49
57
  isHistory: g.sessionId === null,
50
- // 当前打开的会话:客户端据此置顶并默认展开
51
- isCurrent: currentSessionId !== '' && g.sessionId === currentSessionId,
58
+ isCurrent,
59
+ // 只作展示:归档 / 已删除时客户端取消「打开对话」并改成状态标注。
60
+ // 当前对话永远算 active,避免注册表尚未落盘时被误标成已删除。
61
+ sessionState: isCurrent ? 'active' : stateOfSession(g.sessionId, states),
52
62
  title: g.title,
53
63
  createdAt: g.createdAt,
54
64
  fileCount: g.files.length,