dsh-topic-split 0.0.0-stage → 0.12.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.
@@ -0,0 +1,154 @@
1
+ /** 轮次选择:把「连续区间 ranges」与「任意轮次集合 turns」统一成有序、去重、相邻合并的连续块。
2
+ *
3
+ * 为什么需要 turns:主题在会话里是交错的(同一主题会散落在几十个位置),
4
+ * 只用连续区间导出时,要么漏掉同主题的散点,要么把无关轮次一起带出去。
5
+ */
6
+
7
+ import { ERR, SessionSplitError } from './errors.js'
8
+
9
+ /**
10
+ * 解析 "0-51,52-87" 形式的区间串(`end` 表示最后一轮)。
11
+ * @param {string} spec 例如 "0-51,52-87" 或 "78-end"
12
+ * @param {number} totalTurns 会话总轮次
13
+ * @param {{maxRanges:number}} cfg 已校验配置
14
+ * @returns {Array<[number, number]>}
15
+ */
16
+ export function parseRanges(spec, totalTurns, cfg) {
17
+ const text = String(spec ?? '').trim()
18
+ if (!text) throw new SessionSplitError(ERR.RANGE_INVALID, '需要 ranges,例如 0-51 或 0-51,52-87')
19
+ const parts = text.split(',').map((p) => p.trim()).filter(Boolean)
20
+ if (parts.length > cfg.maxRanges) {
21
+ throw new SessionSplitError(ERR.RANGE_TOO_MANY, `区间数量过多(上限 ${cfg.maxRanges} 段)`)
22
+ }
23
+ return parts.map((part) => {
24
+ const m = /^(\d+)\s*-\s*(\d+|end)$/.exec(part)
25
+ if (!m) throw new SessionSplitError(ERR.RANGE_INVALID, `区间格式不对:${part}`)
26
+ const from = Number(m[1])
27
+ const to = m[2] === 'end' ? totalTurns - 1 : Number(m[2])
28
+ if (from > to) throw new SessionSplitError(ERR.RANGE_INVALID, `区间起点大于终点:${part}`)
29
+ if (to >= totalTurns) throw new SessionSplitError(ERR.RANGE_INVALID, `区间超出范围(共 ${totalTurns} 轮):${part}`)
30
+ return [from, to]
31
+ })
32
+ }
33
+
34
+ /** 把有序轮次下标合并成连续块(相邻即合并)。 */
35
+ export function toRuns(indices) {
36
+ const runs = []
37
+ for (const n of indices) {
38
+ const last = runs[runs.length - 1]
39
+ if (last && n === last[1] + 1) last[1] = n
40
+ else runs.push([n, n])
41
+ }
42
+ return runs
43
+ }
44
+
45
+ /**
46
+ * 解析 turns:逗号分隔,每项为单轮 `N` 或区间 `A-B`。
47
+ * @param {string} spec 例如 "3,7-9,20"
48
+ * @param {number} totalTurns 会话总轮次
49
+ * @param {{maxRanges:number}} cfg 已校验配置
50
+ * @returns {{runs:Array<[number,number]>, indices:number[], normalized:boolean}}
51
+ */
52
+ export function parseTurnSpec(spec, totalTurns, cfg) {
53
+ const text = String(spec ?? '').trim()
54
+ if (!text) throw new SessionSplitError(ERR.TURNS_INVALID, '需要 turns,例如 3,7-9,20')
55
+ const parts = text.split(',').map((p) => p.trim()).filter(Boolean)
56
+ if (parts.length === 0) throw new SessionSplitError(ERR.TURNS_INVALID, 'turns 为空')
57
+ if (parts.length > cfg.maxRanges) {
58
+ throw new SessionSplitError(ERR.RANGE_TOO_MANY, `轮次项过多(上限 ${cfg.maxRanges} 项)`)
59
+ }
60
+ const picked = []
61
+ for (const part of parts) {
62
+ const m = /^(\d+)(?:\s*-\s*(\d+))?$/.exec(part)
63
+ if (!m) throw new SessionSplitError(ERR.TURNS_INVALID, `轮次格式不对:${part}(应为 3 或 3-5)`)
64
+ const from = Number(m[1])
65
+ const to = m[2] === undefined ? from : Number(m[2])
66
+ if (from > to) throw new SessionSplitError(ERR.TURNS_INVALID, `轮次起点大于终点:${part}`)
67
+ if (to >= totalTurns) {
68
+ throw new SessionSplitError(
69
+ ERR.TURNS_INVALID,
70
+ `轮次超出范围(共 ${totalTurns} 轮,下标 0..${totalTurns - 1}):${part}`,
71
+ )
72
+ }
73
+ for (let i = from; i <= to; i++) picked.push(i)
74
+ }
75
+ return normalize(picked, parts.length)
76
+ }
77
+
78
+ /**
79
+ * 把区间展开成规范化选择(重叠区间会被合并,避免重复计数)。
80
+ * @param {Array<[number, number]>} ranges
81
+ * @returns {{runs:Array<[number,number]>, indices:number[], normalized:boolean}}
82
+ */
83
+ export function selectionFromRanges(ranges) {
84
+ const picked = []
85
+ for (const [from, to] of ranges) {
86
+ for (let i = from; i <= to; i++) picked.push(i)
87
+ }
88
+ return normalize(picked, ranges.length)
89
+ }
90
+
91
+ /**
92
+ * 解析工具参数中的 turns / ranges(二者互斥)。
93
+ * @param {Record<string, unknown>} args 工具参数
94
+ * @param {number} totalTurns 会话总轮次
95
+ * @param {{maxRanges:number}} cfg 已校验配置
96
+ * @returns {{source:string, runs:Array<[number,number]>, indices:number[], normalized:boolean}|null}
97
+ */
98
+ export function selectionFromArgs(args, totalTurns, cfg) {
99
+ const text = (value) => (typeof value === 'string' ? value.trim() : '')
100
+ const turns = text(args?.turns)
101
+ const rangesText = text(args?.ranges)
102
+ if (turns && rangesText) {
103
+ throw new SessionSplitError(ERR.TURNS_INVALID, 'ranges 与 turns 只能给一个')
104
+ }
105
+ if (turns) return { source: 'turns', ...parseTurnSpec(turns, totalTurns, cfg) }
106
+ if (rangesText) return { source: 'ranges', ...selectionFromRanges(parseRanges(rangesText, totalTurns, cfg)) }
107
+ return null
108
+ }
109
+
110
+ function normalize(picked, inputCount) {
111
+ const indices = [...new Set(picked)].sort((a, b) => a - b)
112
+ if (indices.length === 0) throw new SessionSplitError(ERR.TURNS_INVALID, '选择为空')
113
+ const runs = toRuns(indices)
114
+ return { runs, indices, normalized: runs.length !== inputCount }
115
+ }
116
+
117
+ /**
118
+ * 二维覆盖率:选中 + 排除 = 全部(轮次与字数双维度)。
119
+ * @param {Array<{user:string, assistant:string}>} items 全部轮次
120
+ * @param {number[]} indices 被选中的轮次下标
121
+ * @returns {object} 覆盖率明细
122
+ */
123
+ export function selectionCoverage(items, indices) {
124
+ const totalTurns = items.length
125
+ let totalChars = 0
126
+ for (const item of items) totalChars += item.user.length + item.assistant.length
127
+ let selectedChars = 0
128
+ for (const i of indices) selectedChars += items[i].user.length + items[i].assistant.length
129
+ const excludedTurns = totalTurns - indices.length
130
+ const excludedChars = totalChars - selectedChars
131
+ return {
132
+ totalTurns,
133
+ totalChars,
134
+ selectedTurns: indices.length,
135
+ selectedChars,
136
+ excludedTurns,
137
+ excludedChars,
138
+ complete: indices.length === totalTurns,
139
+ ok: indices.length + excludedTurns === totalTurns && selectedChars + excludedChars === totalChars,
140
+ }
141
+ }
142
+
143
+ /**
144
+ * 人类可读的连续块描述,例如 "1, 5-6, 9"。
145
+ * @param {Array<[number, number]>} runs
146
+ * @param {number} [limit] 最多显示多少个块
147
+ */
148
+ export function describeRuns(runs, limit = 12) {
149
+ const shown = runs
150
+ .slice(0, limit)
151
+ .map(([from, to]) => (from === to ? `${from}` : `${from}-${to}`))
152
+ .join(', ')
153
+ return runs.length > limit ? `${shown} …(共 ${runs.length} 段)` : shown
154
+ }
@@ -0,0 +1,45 @@
1
+ /** 语义分段:模型给边界 → 结构校验 → 短段平滑 → 补字数。失败交给上层回退。 */
2
+ import { segmentByJudge } from './judge.js'
3
+
4
+ /** 计算区间字数。 */
5
+ function charsOf(items, from, to) {
6
+ let n = 0
7
+ for (let i = from; i <= to; i++) n += items[i].user.length + items[i].assistant.length
8
+ return n
9
+ }
10
+
11
+ /**
12
+ * 把过短的段并入相邻段(优先并入前一段),避免模型切得太碎。
13
+ * @param {Array<{turn:number,user:string,assistant:string}>} items
14
+ * @param {Array<{label:string, from:number, to:number}>} segs
15
+ * @param {{minSegmentTurns:number, minSegmentChars:number}} cfg
16
+ * @returns {Array<{label:string, from:number, to:number, chars:number}>}
17
+ */
18
+ export function smoothSegments(items, segs, cfg) {
19
+ const merged = []
20
+ for (const seg of segs) {
21
+ const chars = charsOf(items, seg.from, seg.to)
22
+ const small = seg.to - seg.from + 1 < cfg.minSegmentTurns || chars < cfg.minSegmentChars
23
+ const prev = merged[merged.length - 1]
24
+ if (prev && small) {
25
+ prev.to = seg.to
26
+ prev.chars += chars
27
+ continue
28
+ }
29
+ merged.push({ ...seg, chars })
30
+ }
31
+ return merged.map((seg) => ({ ...seg }))
32
+ }
33
+
34
+ /**
35
+ * 用判断模型分段。
36
+ * @param {Array<{turn:number,user:string,assistant:string}>} items
37
+ * @param {{complete:Function, kind:string}} judge
38
+ * @param {object} cfg
39
+ * @param {AbortSignal} [signal]
40
+ * @returns {Promise<Array<{label:string, from:number, to:number, chars:number}>>}
41
+ */
42
+ export async function segmentByModel(items, judge, cfg, signal) {
43
+ const raw = await segmentByJudge(judge, items, cfg, signal)
44
+ return smoothSegments(items, raw, cfg)
45
+ }
package/src/store.js ADDED
@@ -0,0 +1,272 @@
1
+ /** 会话存储层:路径、注册表、扫描、解析缓存。全部只读源会话。 */
2
+ import { randomUUID } from 'node:crypto'
3
+ import {
4
+ existsSync,
5
+ mkdirSync,
6
+ readFileSync,
7
+ readdirSync,
8
+ renameSync,
9
+ statSync,
10
+ unlinkSync,
11
+ writeFileSync,
12
+ } from 'node:fs'
13
+ import { homedir } from 'node:os'
14
+ import { dirname, join, resolve, sep } from 'node:path'
15
+
16
+ import { ERR, SessionSplitError } from './errors.js'
17
+ import { decodeSession } from './zstd.js'
18
+
19
+ const ID_RE = /^[A-Za-z0-9][A-Za-z0-9-]{0,80}$/
20
+
21
+ /** DSH 根目录(尊重 DSH_HOME 覆盖)。 */
22
+ export function dshHome() {
23
+ return process.env.DSH_HOME || join(homedir(), '.dsh')
24
+ }
25
+
26
+ /** 会话根目录。 */
27
+ export function sessionsRoot() {
28
+ return join(dshHome(), 'sessions')
29
+ }
30
+
31
+ /** workspace 注册表路径。 */
32
+ export function registryPath() {
33
+ return join(dshHome(), 'storages', 'workspace.json')
34
+ }
35
+
36
+ /**
37
+ * 项目目录键:与 DSH 的 `projectKey` 完全一致。
38
+ *
39
+ * 规则:分隔符(`/` `\` `:`)折叠为 `-`;不安全码元转成 `~XXXX`(大写十六进制);
40
+ * 前缀 `--`、去掉前导 `-`、截断到 251 字符、后缀 `--`;空路径回退 `root`。
41
+ * 跨平台正确性依赖它:如果这里和 DSH 不一致,导出的会话会落到错误的目录。
42
+ * @param {string} cwd 会话的工作目录
43
+ * @returns {string}
44
+ */
45
+ export function slugFor(cwd) {
46
+ const raw = String(cwd ?? '')
47
+ let readable = ''
48
+ let separatorRun = false
49
+ for (let i = 0; i < raw.length; i++) {
50
+ const code = raw.charCodeAt(i)
51
+ const ch = String.fromCharCode(code)
52
+ if (ch === '/' || ch === '\\' || ch === ':') {
53
+ if (!separatorRun) readable += '-'
54
+ separatorRun = true
55
+ } else if (ch !== '~' && /^[A-Za-z0-9._-]$/.test(ch)) {
56
+ readable += ch
57
+ separatorRun = false
58
+ } else {
59
+ readable += '~' + code.toString(16).toUpperCase().padStart(4, '0')
60
+ separatorRun = false
61
+ }
62
+ }
63
+ return `--${(readable.replace(/^-+/, '') || 'root').slice(0, 251)}--`
64
+ }
65
+
66
+ /** 校验并规范化会话 id(只允许字母数字与连字符)。 */
67
+ export function assertSessionId(id) {
68
+ const value = String(id ?? '')
69
+ if (!ID_RE.test(value)) {
70
+ throw new SessionSplitError(ERR.SESSION_ID_INVALID, `会话 id 非法:${value || '(空)'}`)
71
+ }
72
+ return value
73
+ }
74
+
75
+ /**
76
+ * 按平台语义比较两个路径:Windows 大小写不敏感,POSIX 敏感。
77
+ * @param {string} a 路径 A
78
+ * @param {string} b 路径 B
79
+ * @param {NodeJS.Platform} [platform] 平台标识(测试可注入)
80
+ * @returns {boolean}
81
+ */
82
+ export function samePath(a, b, platform = process.platform) {
83
+ const left = String(a ?? '').trim()
84
+ const right = String(b ?? '').trim()
85
+ // 空路径不能被 resolve 成当前目录,否则会误匹配
86
+ if (!left || !right) return false
87
+ const l = resolve(left)
88
+ const r = resolve(right)
89
+ return platform === 'win32' ? l.toLowerCase() === r.toLowerCase() : l === r
90
+ }
91
+
92
+ /**
93
+ * 判断 target 是否位于 root 之内(含等于)。
94
+ * @param {string} root 根目录
95
+ * @param {string} target 目标路径
96
+ * @param {NodeJS.Platform} [platform] 平台标识(测试可注入)
97
+ * @returns {boolean}
98
+ */
99
+ export function isInsideRoot(root, target, platform = process.platform) {
100
+ const r = platform === 'win32' ? resolve(root).toLowerCase() : resolve(root)
101
+ const t = platform === 'win32' ? resolve(target).toLowerCase() : resolve(target)
102
+ return t === r || t.startsWith(r + sep)
103
+ }
104
+
105
+ /**
106
+ * 确保路径位于会话根目录内,防止路径穿越。
107
+ * @param {string} file 待校验路径
108
+ * @param {NodeJS.Platform} [platform] 平台标识(测试可注入)
109
+ * @returns {string} 规范化后的绝对路径
110
+ */
111
+ export function assertInsideRoot(file, platform = process.platform) {
112
+ const root = resolve(sessionsRoot())
113
+ const target = resolve(file)
114
+ if (!isInsideRoot(root, target, platform)) {
115
+ throw new SessionSplitError(ERR.PATH_OUTSIDE_ROOT, `路径越界:${target}`)
116
+ }
117
+ return target
118
+ }
119
+
120
+ /** 列出所有会话文件(不解析)。 */
121
+ export function listSessions() {
122
+ const root = sessionsRoot()
123
+ if (!existsSync(root)) return []
124
+ const out = []
125
+ for (const ws of readdirSync(root)) {
126
+ const wsDir = join(root, ws)
127
+ let st
128
+ try {
129
+ st = statSync(wsDir)
130
+ } catch {
131
+ continue
132
+ }
133
+ if (!st.isDirectory()) continue
134
+ for (const dirName of readdirSync(wsDir)) {
135
+ if (!dirName.startsWith('session-')) continue
136
+ const file = join(wsDir, dirName, 'session.v4.jsonl.zstd')
137
+ if (!existsSync(file)) continue
138
+ try {
139
+ const s = statSync(file)
140
+ out.push({ workspace: ws, dirName, file, size: s.size, mtime: s.mtimeMs })
141
+ } catch {
142
+ /* 忽略被删除的条目 */
143
+ }
144
+ }
145
+ }
146
+ return out.sort((a, b) => b.mtime - a.mtime)
147
+ }
148
+
149
+ /** 按 id 或前缀找会话。 */
150
+ export function resolveSession(idOrPrefix) {
151
+ const needle = assertSessionId(idOrPrefix)
152
+ const all = listSessions()
153
+ const exact = all.find((s) => s.dirName === needle)
154
+ if (exact) return exact
155
+ const byPrefix = all.filter(
156
+ (s) => s.dirName.startsWith(needle) || s.dirName.slice('session-'.length).startsWith(needle),
157
+ )
158
+ if (byPrefix.length === 1) return byPrefix[0]
159
+ if (byPrefix.length > 1) {
160
+ throw new SessionSplitError(ERR.SESSION_ID_INVALID, `前缀 ${needle} 匹配到 ${byPrefix.length} 个会话,请写完整 id`)
161
+ }
162
+ throw new SessionSplitError(ERR.SESSION_NOT_FOUND, `找不到会话:${needle}`)
163
+ }
164
+
165
+ /** 带 mtime+size 校验的解析缓存。 */
166
+ export function createSessionReader(cfg) {
167
+ const cache = new Map()
168
+ const stats = { hits: 0, misses: 0, lastParseMs: 0 }
169
+ const reader = function readSession(file) {
170
+ const target = assertInsideRoot(file)
171
+ const st = statSync(target)
172
+ if (st.size > cfg.maxCompressedBytes) {
173
+ throw new SessionSplitError(ERR.FILE_TOO_LARGE, `会话文件过大(${Math.round(st.size / 1024 / 1024)} MB),已跳过`)
174
+ }
175
+ const hit = cache.get(target)
176
+ if (hit && hit.mtimeMs === st.mtimeMs && hit.size === st.size) {
177
+ stats.hits += 1
178
+ return hit.value
179
+ }
180
+
181
+ const startedAt = Date.now()
182
+ const parsed = decodeSession(readFileSync(target))
183
+ stats.misses += 1
184
+ stats.lastParseMs = Date.now() - startedAt
185
+ if (!parsed.header) throw new SessionSplitError(ERR.FILE_CORRUPT, `会话文件为空或损坏:${target}`)
186
+ const inflated = JSON.stringify(parsed).length
187
+ if (inflated > cfg.maxInflatedBytes) {
188
+ throw new SessionSplitError(ERR.FILE_TOO_LARGE, `会话解析后过大(${Math.round(inflated / 1024 / 1024)} MB),已跳过`)
189
+ }
190
+ if (cache.size >= cfg.cacheEntries) cache.delete(cache.keys().next().value)
191
+ cache.set(target, { mtimeMs: st.mtimeMs, size: st.size, value: parsed })
192
+ return parsed
193
+ }
194
+ reader.stats = () => ({ ...stats, size: cache.size })
195
+ return reader
196
+ }
197
+
198
+ /** 读取 workspace 注册表(缺失时返回空结构)。 */
199
+ export function readRegistry() {
200
+ const file = registryPath()
201
+ if (!existsSync(file)) return { global: {}, tables: { workspaces: {} } }
202
+ return JSON.parse(readFileSync(file, 'utf8'))
203
+ }
204
+
205
+ /** 原子写:先写 .tmp 再 rename,避免半截文件。 */
206
+ export function atomicWrite(file, buf) {
207
+ mkdirSync(dirname(file), { recursive: true })
208
+ const tmp = file + '.tmp'
209
+ writeFileSync(tmp, buf)
210
+ try {
211
+ renameSync(tmp, file)
212
+ } catch (err) {
213
+ try {
214
+ unlinkSync(tmp)
215
+ } catch {
216
+ /* ignore */
217
+ }
218
+ throw new SessionSplitError(ERR.IO_FAILED, `写入失败:${err.message}`)
219
+ }
220
+ }
221
+
222
+ /** 原子写回注册表。 */
223
+ export function writeRegistry(data) {
224
+ atomicWrite(registryPath(), Buffer.from(JSON.stringify(data, null, 2), 'utf8'))
225
+ }
226
+
227
+ /** 在注册表里登记新会话(必要时新建 workspace 条目)。 */
228
+ export function registerSession(cwd, sessionId) {
229
+ const reg = readRegistry()
230
+ reg.tables ??= { workspaces: {} }
231
+ reg.tables.workspaces ??= {}
232
+ let target = Object.values(reg.tables.workspaces).find((w) => samePath(w?.path, cwd))
233
+ if (!target) {
234
+ const id = randomUUID()
235
+ const now = new Date().toISOString()
236
+ target = {
237
+ path: cwd,
238
+ title: String(cwd).split(/[\\/]/).pop() || cwd,
239
+ sessionIds: [],
240
+ createdAt: now,
241
+ updatedAt: now,
242
+ }
243
+ reg.tables.workspaces[id] = target
244
+ reg.global ??= {}
245
+ reg.global.workspaceIds = [...(reg.global.workspaceIds ?? []), id]
246
+ }
247
+ target.sessionIds = [sessionId, ...(target.sessionIds ?? []).filter((i) => i !== sessionId)]
248
+ target.updatedAt = new Date().toISOString()
249
+ writeRegistry(reg)
250
+ }
251
+
252
+ /**
253
+ * 从注册表里移除一个会话(归档后调用,避免列表出现悬空条目)。
254
+ * @param {string} cwd 工作目录
255
+ * @param {string} sessionId 会话 id
256
+ */
257
+ export function unregisterSession(cwd, sessionId) {
258
+ const reg = readRegistry()
259
+ const workspaces = reg?.tables?.workspaces
260
+ if (!workspaces) return
261
+ let changed = false
262
+ for (const ws of Object.values(workspaces)) {
263
+ if (!samePath(ws?.path, cwd) || !Array.isArray(ws.sessionIds)) continue
264
+ const next = ws.sessionIds.filter((id) => id !== sessionId)
265
+ if (next.length !== ws.sessionIds.length) {
266
+ ws.sessionIds = next
267
+ ws.updatedAt = new Date().toISOString()
268
+ changed = true
269
+ }
270
+ }
271
+ if (changed) writeRegistry(reg)
272
+ }