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.
- package/CHANGELOG.md +385 -0
- package/LICENSE +674 -0
- package/README.md +417 -2
- package/SECURITY.md +68 -0
- package/client/index.js +985 -0
- package/cordis.patch.yml +4 -0
- package/icon.svg +6 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +66 -4
- package/src/config.js +141 -0
- package/src/cut.js +54 -0
- package/src/diagnostics.js +71 -0
- package/src/errors.js +43 -0
- package/src/exporter.js +149 -0
- package/src/http-auth.js +183 -0
- package/src/index.js +122 -0
- package/src/judge.js +350 -0
- package/src/log.js +39 -0
- package/src/registry.js +219 -0
- package/src/repair.js +75 -0
- package/src/routes.js +349 -0
- package/src/selection.js +154 -0
- package/src/semantic.js +45 -0
- package/src/store.js +272 -0
- package/src/tools.js +392 -0
- package/src/topics.js +108 -0
- package/src/zstd.js +82 -0
package/src/tools.js
ADDED
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
/** 工具定义:输入→输出尽量纯粹,异常统一转成文本(不抛出,避免打断 agent 轮次)。 */
|
|
2
|
+
import { ERR, SessionSplitError, asSplitError } from './errors.js'
|
|
3
|
+
import { planCut, toPairs } from './cut.js'
|
|
4
|
+
import { buildDiagnostics } from './diagnostics.js'
|
|
5
|
+
import { createSession } from './exporter.js'
|
|
6
|
+
import { describeRuns, parseRanges, selectionCoverage, selectionFromArgs } from './selection.js'
|
|
7
|
+
import { segmentByModel } from './semantic.js'
|
|
8
|
+
import { coverage, extractTurns, segment } from './topics.js'
|
|
9
|
+
|
|
10
|
+
// 兼容旧引用:routes.js 与既有测试从 tools.js 取 parseRanges
|
|
11
|
+
export { parseRanges }
|
|
12
|
+
|
|
13
|
+
/** JSON Schema 构造(DSH 要求根节点必须是完整 schema)。 */
|
|
14
|
+
function schema(properties, required = []) {
|
|
15
|
+
const out = { type: 'object', properties, additionalProperties: false }
|
|
16
|
+
if (required.length > 0) out.required = required
|
|
17
|
+
return out
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const str = (description) => ({ type: 'string', description })
|
|
21
|
+
const bool = (description) => ({ type: 'boolean', description })
|
|
22
|
+
const MODES = ['auto', 'model', 'keyword']
|
|
23
|
+
|
|
24
|
+
/** 千分位,便于人读。 */
|
|
25
|
+
function fmt(n) {
|
|
26
|
+
return Number(n).toLocaleString('en-US')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 把 handler 包成统一形状:抛异常 → 返回 "错误[CODE]:说明"。 */
|
|
30
|
+
const TOOL_TIMEOUT_MS = 60_000
|
|
31
|
+
|
|
32
|
+
function defineTool({ name, description, parameters, handler }) {
|
|
33
|
+
return {
|
|
34
|
+
name,
|
|
35
|
+
description,
|
|
36
|
+
parameters,
|
|
37
|
+
timeoutMs: TOOL_TIMEOUT_MS,
|
|
38
|
+
// 宿主期望 render 返回内容块数组([{type:'text', text}]),不是裸字符串
|
|
39
|
+
output: {
|
|
40
|
+
schema: { type: 'string' },
|
|
41
|
+
render: (_args, value) => [{ type: 'text', text: String(value ?? '') }],
|
|
42
|
+
},
|
|
43
|
+
async execute(args, exec) {
|
|
44
|
+
try {
|
|
45
|
+
if (exec?.signal?.aborted) return `错误[ABORTED]:调用已取消`
|
|
46
|
+
return String(await handler(args ?? {}, exec))
|
|
47
|
+
} catch (err) {
|
|
48
|
+
const split = asSplitError(err)
|
|
49
|
+
return `错误[${split.code}]:${split.message}`
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 创建全部工具定义。
|
|
57
|
+
* @param {object} deps 依赖注入
|
|
58
|
+
* @param {ReturnType<typeof import('./config.js').resolveConfig>} deps.cfg 已校验配置
|
|
59
|
+
* @param {(file: string) => { header: object, events: object[] }} deps.readSession 会话读取器
|
|
60
|
+
* @param {{ debug: Function, info: Function, warn: Function, error: Function }} deps.log 日志器
|
|
61
|
+
* @param {() => string[]} [deps.getToolNames] 工具名读取器(诊断用)
|
|
62
|
+
* @param {object|null} [deps.judge] 判断模型(不可用为 null)
|
|
63
|
+
*/
|
|
64
|
+
/**
|
|
65
|
+
* 创建分段解析器:auto/model 优先用判断模型,失败按需回退关键词。
|
|
66
|
+
* @param {{cfg: object, log: object, judge: object|null}} deps
|
|
67
|
+
*/
|
|
68
|
+
export function createResolveTopics({ cfg, log, judge }) {
|
|
69
|
+
/**
|
|
70
|
+
* 解析分段:按 mode 决定用模型还是关键词;auto 模式下模型失败自动回退。
|
|
71
|
+
* @param {Array<{turn:number,user:string,assistant:string}>} items
|
|
72
|
+
* @param {string} mode auto | model | keyword
|
|
73
|
+
* @param {{signal?:AbortSignal}} [exec]
|
|
74
|
+
*/
|
|
75
|
+
return async function resolveTopics(items, mode, exec) {
|
|
76
|
+
const useModel = mode === 'model' || mode === 'auto'
|
|
77
|
+
if (useModel && judge) {
|
|
78
|
+
try {
|
|
79
|
+
const segs = await segmentByModel(items, judge, cfg, exec?.signal)
|
|
80
|
+
return { segs, note: `判断模型:${judge.describe()}` }
|
|
81
|
+
} catch (err) {
|
|
82
|
+
const split = asSplitError(err)
|
|
83
|
+
if (mode === 'model') throw split
|
|
84
|
+
log.warn('模型分段失败,回退关键词', { code: split.code })
|
|
85
|
+
return {
|
|
86
|
+
segs: segment(items, cfg.topicRules, cfg),
|
|
87
|
+
note: `关键词(模型分段失败:${split.code},已回退)`,
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (mode === 'model') {
|
|
92
|
+
throw new SessionSplitError(ERR.JUDGE_UNAVAILABLE, '没有可用的判断模型(检查配置或 DSH_SESSION_SPLIT_JUDGE)')
|
|
93
|
+
}
|
|
94
|
+
return { segs: segment(items, cfg.topicRules, cfg), note: '关键词规则' }
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 创建全部工具定义。
|
|
100
|
+
* @param {object} deps
|
|
101
|
+
* @param {object} deps.cfg 已校验配置
|
|
102
|
+
* @param {Function} deps.readSession 会话读取器
|
|
103
|
+
* @param {object} deps.log 日志器
|
|
104
|
+
* @param {object|null} deps.judge 判断模型
|
|
105
|
+
* @param {Function} [deps.getToolNames] 工具名读取器
|
|
106
|
+
* @param {{register: Function, unregister: Function, archive: Function}|null} [deps.registrar] 会话登记器
|
|
107
|
+
*/
|
|
108
|
+
export function createTools({ cfg, readSession, log, getToolNames, judge, registrar }) {
|
|
109
|
+
const resolveTopics = createResolveTopics({ cfg, log, judge })
|
|
110
|
+
/** 登记新会话:服务优先,失败回退文件。 */
|
|
111
|
+
const registerNew = async (cwd, id) => {
|
|
112
|
+
if (registrar) await registrar.register(cwd, id)
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* 归档原会话:只登记归档集合,绝不重命名目录(改名会让 DSH 的会话列表整份读不出来)。
|
|
116
|
+
* @param {string} cwd 工作目录
|
|
117
|
+
* @param {string} sessionId 原会话 id
|
|
118
|
+
* @returns {Promise<{ok:true, mode:string}|{ok:false, reason:string}>}
|
|
119
|
+
*/
|
|
120
|
+
const archiveOld = async (cwd, sessionId) => {
|
|
121
|
+
if (!registrar || typeof registrar.archive !== 'function') {
|
|
122
|
+
return { ok: false, reason: '没有可用的会话登记器' }
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
return { ok: true, mode: await registrar.archive(cwd, sessionId) }
|
|
126
|
+
} catch (err) {
|
|
127
|
+
const reason = err?.message ? err.message : String(err)
|
|
128
|
+
log?.warn?.('归档原会话失败:', reason)
|
|
129
|
+
return { ok: false, reason }
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return [
|
|
134
|
+
defineTool({
|
|
135
|
+
name: 'session_topics',
|
|
136
|
+
description:
|
|
137
|
+
'把会话按主题自动分段,返回主题清单、轮次区间与字数,并给出覆盖率校验。mode=auto 优先用判断模型,失败自动回退关键词。',
|
|
138
|
+
parameters: schema(
|
|
139
|
+
{
|
|
140
|
+
sessionId: str('会话 id 或前缀'),
|
|
141
|
+
mode: { type: 'string', enum: MODES, description: 'auto(默认)| model | keyword' },
|
|
142
|
+
},
|
|
143
|
+
['sessionId'],
|
|
144
|
+
),
|
|
145
|
+
async handler(args, exec) {
|
|
146
|
+
const session = requireSession(args.sessionId)
|
|
147
|
+
const mode = MODES.includes(args.mode) ? args.mode : 'auto'
|
|
148
|
+
const { events } = readSession(session.file)
|
|
149
|
+
const items = extractTurns(events)
|
|
150
|
+
const { segs, note } = await resolveTopics(items, mode, exec)
|
|
151
|
+
const cov = coverage(items, segs)
|
|
152
|
+
const lines = [`主题分段(${note}):`]
|
|
153
|
+
for (const s of segs) lines.push(` ${s.label} 轮次 ${s.from}-${s.to} ${fmt(s.chars)} 字`)
|
|
154
|
+
lines.push('')
|
|
155
|
+
const verdict = cov.ok ? '一致 ✔' : '不一致 ✘'
|
|
156
|
+
lines.push(
|
|
157
|
+
`覆盖率:轮次 ${cov.coveredTurns}/${cov.totalTurns},` +
|
|
158
|
+
`字数 ${fmt(cov.coveredChars)}/${fmt(cov.totalChars)} → ${verdict}`,
|
|
159
|
+
)
|
|
160
|
+
log.debug('session_topics', { sessionId: session.dirName, segs: segs.length, mode })
|
|
161
|
+
return lines.join('\n')
|
|
162
|
+
},
|
|
163
|
+
}),
|
|
164
|
+
|
|
165
|
+
defineTool({
|
|
166
|
+
name: 'session_export',
|
|
167
|
+
description:
|
|
168
|
+
'把选中的轮次导出成独立会话(一问一答结构)。ranges 为连续区间("0-51,52-87");' +
|
|
169
|
+
'turns 为任意轮次集合("3,7-9,20"),用于精确挑选散落在各处的同主题内容;二者只能给一个。' +
|
|
170
|
+
'dryRun=true 只预览不写入;不给 ranges/turns 时按主题分段给出计划。',
|
|
171
|
+
parameters: schema(
|
|
172
|
+
{
|
|
173
|
+
sessionId: str('源会话 id 或前缀'),
|
|
174
|
+
ranges: str('连续区间,如 "0-51,52-87";与 turns 互斥'),
|
|
175
|
+
turns: str('任意轮次集合,如 "3,7-9,20";与 ranges 互斥'),
|
|
176
|
+
title: str('新会话标题(可选)'),
|
|
177
|
+
dryRun: bool('true = 只预览,不写文件'),
|
|
178
|
+
cut: bool('true = 导出后把选中轮次从源会话剪掉(原会话归档,主会话只剩其余部分)'),
|
|
179
|
+
mode: { type: 'string', enum: MODES, description: 'dryRun 自动分段时的模式:auto | model | keyword' },
|
|
180
|
+
},
|
|
181
|
+
['sessionId'],
|
|
182
|
+
),
|
|
183
|
+
async handler(args, exec) {
|
|
184
|
+
const session = requireSession(args.sessionId)
|
|
185
|
+
const { header, events } = readSession(session.file)
|
|
186
|
+
const items = extractTurns(events)
|
|
187
|
+
const dryRun = args.dryRun === true
|
|
188
|
+
const selection = selectionFromArgs(args, items.length, cfg)
|
|
189
|
+
|
|
190
|
+
if (!selection) {
|
|
191
|
+
if (!dryRun) {
|
|
192
|
+
throw new SessionSplitError(ERR.RANGE_INVALID, '需要 ranges 或 turns,例如 0-51 或 3,7-9,20')
|
|
193
|
+
}
|
|
194
|
+
const mode = MODES.includes(args.mode) ? args.mode : 'auto'
|
|
195
|
+
const { segs, note } = await resolveTopics(items, mode, exec)
|
|
196
|
+
const totalTurns = segs.reduce((n, s) => n + (s.to - s.from + 1), 0)
|
|
197
|
+
const totalChars = segs.reduce((n, s) => n + s.chars, 0)
|
|
198
|
+
const lines = [`预览(${note});未写入任何文件:`]
|
|
199
|
+
for (const s of segs) {
|
|
200
|
+
lines.push(` ${s.from}-${s.to} ${s.label} ${s.to - s.from + 1} 轮 ${fmt(s.chars)} 字`)
|
|
201
|
+
}
|
|
202
|
+
lines.push('')
|
|
203
|
+
lines.push(`合计:${segs.length} 段,${totalTurns} 轮,${fmt(totalChars)} 字;将新建 ${segs.length} 个会话`)
|
|
204
|
+
lines.push('确认后按段调用 session_export(填 ranges 或 turns)即可写入。')
|
|
205
|
+
return lines.join('\n')
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const cov = selectionCoverage(items, selection.indices)
|
|
209
|
+
const frag = selection.runs.length
|
|
210
|
+
|
|
211
|
+
if (dryRun) {
|
|
212
|
+
const lines = [
|
|
213
|
+
'预览(未写入):',
|
|
214
|
+
` 选中 ${cov.selectedTurns} 轮 / ${fmt(cov.selectedChars)} 字;` +
|
|
215
|
+
`${frag} 个连续块(${describeRuns(selection.runs)})`,
|
|
216
|
+
]
|
|
217
|
+
if (selection.normalized) lines.push(' 已合并重叠或相邻的输入项')
|
|
218
|
+
if (frag > 1) lines.push(' 注意:选中轮次不连续,导出会话会缺少中间上下文')
|
|
219
|
+
const willCreate = args.cut === true ? 2 : 1
|
|
220
|
+
lines.push(
|
|
221
|
+
` 排除 ${cov.excludedTurns} 轮 / ${fmt(cov.excludedChars)} 字;` +
|
|
222
|
+
`选中 + 排除 = 源会话 ${cov.totalTurns} 轮 / ${fmt(cov.totalChars)} 字;` +
|
|
223
|
+
`将新建 ${willCreate} 个会话`,
|
|
224
|
+
)
|
|
225
|
+
if (args.cut === true) {
|
|
226
|
+
const plan = planCut(items, selection.runs)
|
|
227
|
+
lines.push(
|
|
228
|
+
` 同时剪除:主会话保留 ${plan.remainingTurns} 轮 / ${fmt(plan.remainingChars)} 字,原会话归档`,
|
|
229
|
+
)
|
|
230
|
+
}
|
|
231
|
+
lines.push('确认后去掉 dryRun 再调用一次即可写入。')
|
|
232
|
+
return lines.join('\n')
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const cutPlan = args.cut === true ? planCut(items, selection.runs) : null
|
|
236
|
+
if (cutPlan && cutPlan.remainingTurns === 0) {
|
|
237
|
+
throw new SessionSplitError(ERR.EMPTY_RESULT, '剪除后主会话为空,已拒绝执行')
|
|
238
|
+
}
|
|
239
|
+
const cwd = header.cwd ?? process.cwd()
|
|
240
|
+
const pairs = selection.indices.map((i) => [items[i].user, items[i].assistant])
|
|
241
|
+
const made = createSession({ cwd, pairs, title: args.title })
|
|
242
|
+
await registerNew(cwd, made.id)
|
|
243
|
+
log.info('导出成功', { from: session.dirName, to: made.id, turns: made.turns })
|
|
244
|
+
const lines = [`已导出 ${made.turns} 轮(${frag} 段)→ ${made.id}`]
|
|
245
|
+
if (frag > 1) lines.push('提示:选中轮次不连续,导出的会话缺少中间上下文。')
|
|
246
|
+
if (cutPlan) {
|
|
247
|
+
const main = createSession({
|
|
248
|
+
cwd,
|
|
249
|
+
pairs: toPairs(cutPlan.keep),
|
|
250
|
+
title: `${args.title ?? '主会话'}(已剪除独立段)`,
|
|
251
|
+
})
|
|
252
|
+
await registerNew(cwd, main.id)
|
|
253
|
+
const archived = await archiveOld(cwd, session.dirName)
|
|
254
|
+
lines.push(`已重建主会话 ${main.turns} 轮 → ${main.id}`)
|
|
255
|
+
if (archived.ok) {
|
|
256
|
+
lines.push('原会话已归档(不再出现在会话列表,文件保持原样可随时取消归档)')
|
|
257
|
+
} else {
|
|
258
|
+
lines.push(`原会话未归档:${archived.reason}`)
|
|
259
|
+
lines.push('它仍会出现在会话列表里;可在列表里右键「归档」,或等它空闲后重试。')
|
|
260
|
+
}
|
|
261
|
+
lines.push('注意:如果目标会话正在其它标签页打开,请关闭后重新打开列表。')
|
|
262
|
+
log.info('导出并剪除完成', { from: session.dirName, to: made.id, main: main.id })
|
|
263
|
+
}
|
|
264
|
+
return lines.join('\n')
|
|
265
|
+
},
|
|
266
|
+
}),
|
|
267
|
+
|
|
268
|
+
defineTool({
|
|
269
|
+
name: 'session_cut',
|
|
270
|
+
description:
|
|
271
|
+
'把选中的轮次从会话中剪掉:用剩余轮次重建一个新会话。ranges 连续区间或 turns 任意轮次集合(二者互斥)。' +
|
|
272
|
+
'默认只预览,dryRun=false 才执行;archive=true 时归档原会话。注意:重建后的会话只保留文字。',
|
|
273
|
+
parameters: schema(
|
|
274
|
+
{
|
|
275
|
+
sessionId: str('源会话 id 或前缀'),
|
|
276
|
+
ranges: str('要剪掉的连续区间,如 "78-92";与 turns 互斥'),
|
|
277
|
+
turns: str('要剪掉的任意轮次,如 "3,7-9";与 ranges 互斥'),
|
|
278
|
+
dryRun: bool('true = 只预览(默认)'),
|
|
279
|
+
archive: bool('true = 归档原会话'),
|
|
280
|
+
title: str('重建后的会话标题(可选)'),
|
|
281
|
+
},
|
|
282
|
+
['sessionId'],
|
|
283
|
+
),
|
|
284
|
+
async handler(args) {
|
|
285
|
+
const session = requireSession(args.sessionId)
|
|
286
|
+
const { header, events } = readSession(session.file)
|
|
287
|
+
const items = extractTurns(events)
|
|
288
|
+
const selection = selectionFromArgs(args, items.length, cfg)
|
|
289
|
+
if (!selection) {
|
|
290
|
+
throw new SessionSplitError(ERR.RANGE_INVALID, '需要 ranges 或 turns,例如 78-92 或 3,7-9')
|
|
291
|
+
}
|
|
292
|
+
const plan = planCut(items, selection.runs)
|
|
293
|
+
if (plan.remainingTurns === 0) {
|
|
294
|
+
throw new SessionSplitError(ERR.EMPTY_RESULT, '不能剪掉整个会话')
|
|
295
|
+
}
|
|
296
|
+
const cwd = header.cwd ?? process.cwd()
|
|
297
|
+
const spec = String(args.turns ?? args.ranges ?? '')
|
|
298
|
+
const lines = [
|
|
299
|
+
`剪除计划:${spec}(${selection.runs.length} 段)`,
|
|
300
|
+
` 剪掉 ${plan.removedTurns} 轮 / ${fmt(plan.removedChars)} 字`,
|
|
301
|
+
` 保留 ${plan.remainingTurns} 轮 / ${fmt(plan.remainingChars)} 字`,
|
|
302
|
+
' 注意:重建会话只保留文字,工具调用记录不会带入。',
|
|
303
|
+
]
|
|
304
|
+
const dryRun = args.dryRun !== false
|
|
305
|
+
if (dryRun) {
|
|
306
|
+
lines.push('')
|
|
307
|
+
lines.push('预览(未写入);确认后传 dryRun:false 执行。')
|
|
308
|
+
return lines.join('\n')
|
|
309
|
+
}
|
|
310
|
+
const made = createSession({ cwd, pairs: toPairs(plan.keep), title: args.title })
|
|
311
|
+
await registerNew(cwd, made.id)
|
|
312
|
+
lines.push('')
|
|
313
|
+
lines.push(`已重建会话:${made.id}(${made.turns} 轮)`)
|
|
314
|
+
if (args.archive === true) {
|
|
315
|
+
const archived = await archiveOld(cwd, session.dirName)
|
|
316
|
+
if (archived.ok) {
|
|
317
|
+
lines.push('原会话已归档(不再出现在会话列表,文件保持原样可随时取消归档)')
|
|
318
|
+
} else {
|
|
319
|
+
lines.push(`原会话未归档:${archived.reason}`)
|
|
320
|
+
lines.push('它仍会出现在会话列表里;可在列表里右键「归档」,或等它空闲后重试。')
|
|
321
|
+
}
|
|
322
|
+
lines.push('注意:如果目标会话正在其它标签页打开,请关闭后重新打开列表。')
|
|
323
|
+
} else {
|
|
324
|
+
lines.push('原会话未改动;确认无误后可再执行归档或手动删除。')
|
|
325
|
+
}
|
|
326
|
+
log.info('会话剪除完成', { from: session.dirName, to: made.id, removed: plan.removedTurns })
|
|
327
|
+
return lines.join('\n')
|
|
328
|
+
},
|
|
329
|
+
}),
|
|
330
|
+
|
|
331
|
+
defineTool({
|
|
332
|
+
name: 'session_coverage',
|
|
333
|
+
description:
|
|
334
|
+
'核对「选中 + 排除 = 原对话」:ranges 连续区间或 turns 任意轮次集合(二者互斥),按轮次与字数双维度校验。',
|
|
335
|
+
parameters: schema(
|
|
336
|
+
{
|
|
337
|
+
sessionId: str('会话 id 或前缀'),
|
|
338
|
+
ranges: str('要核对的连续区间,如 "0-159";与 turns 互斥'),
|
|
339
|
+
turns: str('要核对的任意轮次,如 "3,7-9,20";与 ranges 互斥'),
|
|
340
|
+
},
|
|
341
|
+
['sessionId'],
|
|
342
|
+
),
|
|
343
|
+
handler(args) {
|
|
344
|
+
const session = requireSession(args.sessionId)
|
|
345
|
+
const { events } = readSession(session.file)
|
|
346
|
+
const items = extractTurns(events)
|
|
347
|
+
const selection = selectionFromArgs(args, items.length, cfg)
|
|
348
|
+
if (!selection) {
|
|
349
|
+
throw new SessionSplitError(ERR.RANGE_INVALID, '需要 ranges 或 turns,例如 0-159 或 3,7-9')
|
|
350
|
+
}
|
|
351
|
+
const cov = selectionCoverage(items, selection.indices)
|
|
352
|
+
const verdict = cov.ok ? '一致 ✔' : '不一致 ✘'
|
|
353
|
+
const lines = [
|
|
354
|
+
`源会话 ${cov.totalTurns} 轮 / ${fmt(cov.totalChars)} 字`,
|
|
355
|
+
`选中 ${cov.selectedTurns} 轮 / ${fmt(cov.selectedChars)} 字(${selection.runs.length} 个连续块)`,
|
|
356
|
+
`排除 ${cov.excludedTurns} 轮 / ${fmt(cov.excludedChars)} 字`,
|
|
357
|
+
`合计 ${cov.selectedTurns + cov.excludedTurns} 轮 / ` +
|
|
358
|
+
`${fmt(cov.selectedChars + cov.excludedChars)} 字 → ${verdict}`,
|
|
359
|
+
]
|
|
360
|
+
if (cov.complete) lines.push('(选中即全量)')
|
|
361
|
+
return lines.join('\n')
|
|
362
|
+
},
|
|
363
|
+
}),
|
|
364
|
+
defineTool({
|
|
365
|
+
name: 'session_diag',
|
|
366
|
+
description: '输出插件自检信息:路径、配置、缓存命中率、判断模型状态与开关,用于排障。',
|
|
367
|
+
parameters: schema({}, []),
|
|
368
|
+
handler() {
|
|
369
|
+
return buildDiagnostics({
|
|
370
|
+
cfg,
|
|
371
|
+
readerStats: () => readSession.stats?.(),
|
|
372
|
+
toolNames: getToolNames ? getToolNames() : [],
|
|
373
|
+
judge: judge ? judge.describe() : '不可用(回退关键词规则)',
|
|
374
|
+
})
|
|
375
|
+
},
|
|
376
|
+
}),
|
|
377
|
+
]
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// 延迟导入,避免 tools.js 与 store.js 形成初始化环
|
|
381
|
+
let resolveSessionFn = null
|
|
382
|
+
/**
|
|
383
|
+
* 注入会话解析函数(由插件入口调用,避免循环依赖)。
|
|
384
|
+
* @param {(id: string) => any} fn
|
|
385
|
+
*/
|
|
386
|
+
export function __setResolver(fn) {
|
|
387
|
+
resolveSessionFn = fn
|
|
388
|
+
}
|
|
389
|
+
function requireSession(id) {
|
|
390
|
+
if (!resolveSessionFn) throw new SessionSplitError(ERR.SESSION_NOT_FOUND, '会话解析器未初始化')
|
|
391
|
+
return resolveSessionFn(id)
|
|
392
|
+
}
|
package/src/topics.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/** 主题分段:把事件流抽成轮次文本,按规则分类并平滑成粗粒度段落。 */
|
|
2
|
+
|
|
3
|
+
export const OTHER_LABEL = '其他'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 抽取每一轮的用户文本与助手文本。
|
|
7
|
+
* @param {Array<Record<string, any>>} events
|
|
8
|
+
* @returns {Array<{turn:number, user:string, assistant:string}>}
|
|
9
|
+
*/
|
|
10
|
+
export function extractTurns(events) {
|
|
11
|
+
const starts = []
|
|
12
|
+
events.forEach((e, i) => {
|
|
13
|
+
if (e?.type === 'turn/start') starts.push(i)
|
|
14
|
+
})
|
|
15
|
+
return starts.map((from, index) => {
|
|
16
|
+
const to = index + 1 < starts.length ? starts[index + 1] : events.length
|
|
17
|
+
const slice = events.slice(from, to)
|
|
18
|
+
const user = slice
|
|
19
|
+
.filter((e) => e?.type === 'user/message')
|
|
20
|
+
.map((e) => textOf(e.data))
|
|
21
|
+
.filter(Boolean)
|
|
22
|
+
.join('\n\n')
|
|
23
|
+
const assistant = slice
|
|
24
|
+
.filter((e) => e?.type === 'assistant/message')
|
|
25
|
+
.map((e) => textOf(e.data?.message))
|
|
26
|
+
.filter(Boolean)
|
|
27
|
+
.join('\n\n')
|
|
28
|
+
return { turn: index, user, assistant }
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function textOf(msg) {
|
|
33
|
+
if (!msg || !Array.isArray(msg.content)) return ''
|
|
34
|
+
return msg.content
|
|
35
|
+
.filter((c) => c && c.type === 'text' && typeof c.text === 'string')
|
|
36
|
+
.map((c) => c.text)
|
|
37
|
+
.join('\n')
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 单轮分类:用户原文优先,助手文本只看前 400 字。 */
|
|
41
|
+
export function classify(item, rules) {
|
|
42
|
+
const haystack = (item.user || '') + '\n' + (item.assistant || '').slice(0, 400)
|
|
43
|
+
for (const { label, re } of rules) {
|
|
44
|
+
if (re.test(haystack)) return label
|
|
45
|
+
}
|
|
46
|
+
return OTHER_LABEL
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 原始分段(相邻同标签合并)。 */
|
|
50
|
+
function rawSegments(items, rules) {
|
|
51
|
+
const out = []
|
|
52
|
+
for (const item of items) {
|
|
53
|
+
const label = classify(item, rules)
|
|
54
|
+
const chars = item.user.length + item.assistant.length
|
|
55
|
+
const last = out[out.length - 1]
|
|
56
|
+
if (last && last.label === label) {
|
|
57
|
+
last.to = item.turn
|
|
58
|
+
last.chars += chars
|
|
59
|
+
} else {
|
|
60
|
+
out.push({ label, from: item.turn, to: item.turn, chars })
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return out
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 平滑分段:短段并入相邻段,避免关键词抖动切碎。
|
|
68
|
+
* @param {Array<{turn:number,user:string,assistant:string}>} items
|
|
69
|
+
* @param {Array<{label:string,re:RegExp}>} rules
|
|
70
|
+
* @param {{minSegmentTurns:number, minSegmentChars:number}} cfg
|
|
71
|
+
*/
|
|
72
|
+
export function segment(items, rules, cfg) {
|
|
73
|
+
const merged = []
|
|
74
|
+
for (const seg of rawSegments(items, rules)) {
|
|
75
|
+
const small = seg.to - seg.from + 1 < cfg.minSegmentTurns || seg.chars < cfg.minSegmentChars
|
|
76
|
+
const prev = merged[merged.length - 1]
|
|
77
|
+
if (!prev) {
|
|
78
|
+
merged.push({ ...seg })
|
|
79
|
+
continue
|
|
80
|
+
}
|
|
81
|
+
if (small || prev.label === seg.label) {
|
|
82
|
+
prev.to = seg.to
|
|
83
|
+
prev.chars += seg.chars
|
|
84
|
+
continue
|
|
85
|
+
}
|
|
86
|
+
merged.push({ ...seg })
|
|
87
|
+
}
|
|
88
|
+
return merged
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 覆盖率:分段总和是否等于会话总量。
|
|
93
|
+
* @param {Array<{turn:number,user:string,assistant:string}>} items
|
|
94
|
+
* @param {Array<{from:number,to:number,chars:number}>} segs
|
|
95
|
+
*/
|
|
96
|
+
export function coverage(items, segs) {
|
|
97
|
+
const totalTurns = items.length
|
|
98
|
+
const totalChars = items.reduce((n, i) => n + i.user.length + i.assistant.length, 0)
|
|
99
|
+
const coveredTurns = segs.reduce((n, s) => n + (s.to - s.from + 1), 0)
|
|
100
|
+
const coveredChars = segs.reduce((n, s) => n + s.chars, 0)
|
|
101
|
+
return {
|
|
102
|
+
totalTurns,
|
|
103
|
+
totalChars,
|
|
104
|
+
coveredTurns,
|
|
105
|
+
coveredChars,
|
|
106
|
+
ok: coveredTurns === totalTurns && coveredChars === totalChars,
|
|
107
|
+
}
|
|
108
|
+
}
|
package/src/zstd.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DSH 会话日志的 zstd 编解码。
|
|
3
|
+
*
|
|
4
|
+
* 关键事实:DSH 的 `.jsonl.zstd` 是「多帧」结构——第一帧只放 header,
|
|
5
|
+
* 后续帧是事件批次。Node 的 zstdDecompressSync 只解第一帧,
|
|
6
|
+
* 因此这里按帧魔数(0x28 B5 2F FD)逐段解压再拼接。
|
|
7
|
+
*/
|
|
8
|
+
import zlib from 'node:zlib'
|
|
9
|
+
|
|
10
|
+
const MAGIC = Buffer.from([0x28, 0xb5, 0x2f, 0xfd])
|
|
11
|
+
|
|
12
|
+
/** 找出所有帧起点。 */
|
|
13
|
+
export function frameOffsets(buf) {
|
|
14
|
+
const out = []
|
|
15
|
+
let at = buf.indexOf(MAGIC, 0)
|
|
16
|
+
while (at !== -1) {
|
|
17
|
+
out.push(at)
|
|
18
|
+
at = buf.indexOf(MAGIC, at + 4)
|
|
19
|
+
}
|
|
20
|
+
return out
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 解压全部分帧。
|
|
25
|
+
* @param {Buffer} buf
|
|
26
|
+
* @returns {Buffer} 拼接后的完整内容
|
|
27
|
+
*/
|
|
28
|
+
export function decodeAll(buf) {
|
|
29
|
+
const starts = frameOffsets(buf)
|
|
30
|
+
if (starts.length === 0) {
|
|
31
|
+
// 没有魔数:交给 zlib 自己判断(可能是不带帧头的裸数据)
|
|
32
|
+
return zlib.zstdDecompressSync(buf)
|
|
33
|
+
}
|
|
34
|
+
if (starts.length === 1) return zlib.zstdDecompressSync(buf)
|
|
35
|
+
const parts = []
|
|
36
|
+
for (let i = 0; i < starts.length; i++) {
|
|
37
|
+
const from = starts[i]
|
|
38
|
+
const to = i + 1 < starts.length ? starts[i + 1] : buf.length
|
|
39
|
+
try {
|
|
40
|
+
parts.push(zlib.zstdDecompressSync(buf.subarray(from, to)))
|
|
41
|
+
} catch {
|
|
42
|
+
// 尾帧可能正被写入方追加,截断帧直接跳过,保留已解出的部分
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return Buffer.concat(parts)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 压成单帧(用于测试和普通数据)。 */
|
|
49
|
+
export function compress(buf) {
|
|
50
|
+
return zlib.zstdCompressSync(buf)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 生成合法的会话文件内容:header 单独一帧,事件另起一帧。
|
|
55
|
+
* @param {Record<string, unknown>} header
|
|
56
|
+
* @param {Record<string, unknown>[]} events
|
|
57
|
+
*/
|
|
58
|
+
export function encodeSession(header, events) {
|
|
59
|
+
const head = Buffer.from(JSON.stringify(header) + '\n', 'utf8')
|
|
60
|
+
const rest = Buffer.from(events.map((e) => JSON.stringify(e)).join('\n') + '\n', 'utf8')
|
|
61
|
+
return Buffer.concat([compress(head), compress(rest)])
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 解析会话内容,容忍尾帧截断。
|
|
66
|
+
* @param {Buffer} buf
|
|
67
|
+
*/
|
|
68
|
+
export function decodeSession(buf) {
|
|
69
|
+
const text = decodeAll(buf).toString('utf8')
|
|
70
|
+
const lines = text.split('\n').filter((l) => l.trim() !== '')
|
|
71
|
+
if (lines.length === 0) return { header: null, events: [] }
|
|
72
|
+
const header = JSON.parse(lines[0])
|
|
73
|
+
const events = []
|
|
74
|
+
for (const line of lines.slice(1)) {
|
|
75
|
+
try {
|
|
76
|
+
events.push(JSON.parse(line))
|
|
77
|
+
} catch {
|
|
78
|
+
// 最后一行可能被截断;忽略
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return { header, events }
|
|
82
|
+
}
|