dsh-context-kit 0.1.0 → 0.1.1
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/LICENSE +21 -21
- package/README.md +120 -120
- package/bin/dsh-context-kit.mjs +138 -138
- package/client/panel.js +302 -302
- package/cordis.patch.yml +21 -21
- package/icon.svg +13 -13
- package/index.js +385 -385
- package/lib/index/events.cjs +90 -90
- package/lib/index/render.cjs +177 -177
- package/lib/index/sources.cjs +286 -286
- package/lib/index/tokens.cjs +48 -48
- package/lib/kit-ops.mjs +230 -230
- package/lib/remote.mjs +55 -55
- package/lib/settings.mjs +69 -69
- package/lib/tuner/sync.mjs +140 -140
- package/lib/tuner/tuner.mjs +647 -647
- package/package.json +65 -65
- package/tuner.js +149 -149
package/lib/index/events.cjs
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
// events.cjs — 索引重建的触发条件(事件驱动,不做步数节流)
|
|
2
|
-
//
|
|
3
|
-
// 依据(sp-index-api,2026-11-14):deepseek-flash 的 systemPromptUpdate 是 "in-history",
|
|
4
|
-
// 即 system prompt 每变一次就 append 一条新的 system 消息,之后每个请求都要重发。
|
|
5
|
-
// 所以索引**只能在事件点重建**:会话开始、每次 compaction/summary 之后、手动调用。
|
|
6
|
-
//
|
|
7
|
-
// 真实会话日志(~/.dsh/sessions/**/session.v4.jsonl.zstd)里的压缩事件类型:
|
|
8
|
-
// compaction/start → { compactionId, turn }
|
|
9
|
-
// compaction/summary→ { compactionId, summary: [{type:'text', text:'## Primary Request …'}] }
|
|
10
|
-
// compaction/end → { compactionId, turn }
|
|
11
|
-
// compaction/prune → 历史裁剪(不是压缩总结,不触发重建)
|
|
12
|
-
|
|
13
|
-
const COMPACTION_TYPES = new Set(['compaction/start', 'compaction/summary', 'compaction/end'])
|
|
14
|
-
|
|
15
|
-
// 压缩总结模板的标题(来自真实 compaction/summary 内容)
|
|
16
|
-
const SUMMARY_MARKERS = [/##\s*Primary Request and Intent/i, /##\s*Key Technical Concepts/i, /##\s*Pending Tasks/i, /##\s*Optional Next Step/i]
|
|
17
|
-
|
|
18
|
-
function eventType(event) {
|
|
19
|
-
try {
|
|
20
|
-
const t = event?.type ?? event?.name ?? event?.kind
|
|
21
|
-
return typeof t === 'string' ? t : ''
|
|
22
|
-
} catch {
|
|
23
|
-
return ''
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/** 是否是压缩相关事件(不改变任何状态,纯判定)。 */
|
|
28
|
-
function isCompactionEvent(event) {
|
|
29
|
-
const type = eventType(event)
|
|
30
|
-
if (!type) return false
|
|
31
|
-
// 只认 start/summary/end。compaction/prune 是历史裁剪(一次会话几十次),
|
|
32
|
-
// 拿它触发重建会退化成"频繁重建",必须排除。
|
|
33
|
-
if (COMPACTION_TYPES.has(type)) return true
|
|
34
|
-
// 兼容别的命名(例如 compaction/complete),但排除 prune/error 这类非触发事件
|
|
35
|
-
if (/compaction\//i.test(type) && !/prune|error/i.test(type)) return true
|
|
36
|
-
return false
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** 同一次压缩的稳定 key,用来去重(start/summary/end 属于同一次)。 */
|
|
40
|
-
function compactionKey(event) {
|
|
41
|
-
try {
|
|
42
|
-
const id = event?.data?.compactionId ?? event?.compactionId
|
|
43
|
-
if (typeof id === 'string' && id) return id
|
|
44
|
-
const seq = event?.seq ?? event?.time
|
|
45
|
-
if (seq !== undefined && seq !== null) return `seq:${seq}`
|
|
46
|
-
return ''
|
|
47
|
-
} catch {
|
|
48
|
-
return ''
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** 32 位字符串哈希(用于给"压缩总结文本"去重)。 */
|
|
53
|
-
function hashText(text) {
|
|
54
|
-
let hash = 2166136261
|
|
55
|
-
const s = String(text ?? '')
|
|
56
|
-
for (let i = 0; i < s.length && i < 4096; i += 1) {
|
|
57
|
-
hash ^= s.charCodeAt(i)
|
|
58
|
-
hash = Math.imul(hash, 16777619)
|
|
59
|
-
}
|
|
60
|
-
return (hash >>> 0).toString(16)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 退化路径:拿不到 session 事件时,从 agent/pre-step 进入本步的消息里识别
|
|
65
|
-
* "压缩总结"(DSH 的总结模板标题)。识别成功返回稳定 key,否则 null。
|
|
66
|
-
*/
|
|
67
|
-
function detectSummaryText(messages) {
|
|
68
|
-
try {
|
|
69
|
-
if (!Array.isArray(messages)) return null
|
|
70
|
-
for (const message of messages) {
|
|
71
|
-
const blocks = message?.content
|
|
72
|
-
const text = typeof blocks === 'string' ? blocks : Array.isArray(blocks) ? blocks.filter((b) => b?.type === 'text' && typeof b.text === 'string').map((b) => b.text).join('\n') : ''
|
|
73
|
-
if (!text || text.length < 200) continue
|
|
74
|
-
if (SUMMARY_MARKERS.some((re) => re.test(text))) return hashText(text)
|
|
75
|
-
}
|
|
76
|
-
return null
|
|
77
|
-
} catch {
|
|
78
|
-
return null
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
module.exports = {
|
|
83
|
-
COMPACTION_TYPES,
|
|
84
|
-
SUMMARY_MARKERS,
|
|
85
|
-
eventType,
|
|
86
|
-
isCompactionEvent,
|
|
87
|
-
compactionKey,
|
|
88
|
-
hashText,
|
|
89
|
-
detectSummaryText,
|
|
90
|
-
}
|
|
1
|
+
// events.cjs — 索引重建的触发条件(事件驱动,不做步数节流)
|
|
2
|
+
//
|
|
3
|
+
// 依据(sp-index-api,2026-11-14):deepseek-flash 的 systemPromptUpdate 是 "in-history",
|
|
4
|
+
// 即 system prompt 每变一次就 append 一条新的 system 消息,之后每个请求都要重发。
|
|
5
|
+
// 所以索引**只能在事件点重建**:会话开始、每次 compaction/summary 之后、手动调用。
|
|
6
|
+
//
|
|
7
|
+
// 真实会话日志(~/.dsh/sessions/**/session.v4.jsonl.zstd)里的压缩事件类型:
|
|
8
|
+
// compaction/start → { compactionId, turn }
|
|
9
|
+
// compaction/summary→ { compactionId, summary: [{type:'text', text:'## Primary Request …'}] }
|
|
10
|
+
// compaction/end → { compactionId, turn }
|
|
11
|
+
// compaction/prune → 历史裁剪(不是压缩总结,不触发重建)
|
|
12
|
+
|
|
13
|
+
const COMPACTION_TYPES = new Set(['compaction/start', 'compaction/summary', 'compaction/end'])
|
|
14
|
+
|
|
15
|
+
// 压缩总结模板的标题(来自真实 compaction/summary 内容)
|
|
16
|
+
const SUMMARY_MARKERS = [/##\s*Primary Request and Intent/i, /##\s*Key Technical Concepts/i, /##\s*Pending Tasks/i, /##\s*Optional Next Step/i]
|
|
17
|
+
|
|
18
|
+
function eventType(event) {
|
|
19
|
+
try {
|
|
20
|
+
const t = event?.type ?? event?.name ?? event?.kind
|
|
21
|
+
return typeof t === 'string' ? t : ''
|
|
22
|
+
} catch {
|
|
23
|
+
return ''
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 是否是压缩相关事件(不改变任何状态,纯判定)。 */
|
|
28
|
+
function isCompactionEvent(event) {
|
|
29
|
+
const type = eventType(event)
|
|
30
|
+
if (!type) return false
|
|
31
|
+
// 只认 start/summary/end。compaction/prune 是历史裁剪(一次会话几十次),
|
|
32
|
+
// 拿它触发重建会退化成"频繁重建",必须排除。
|
|
33
|
+
if (COMPACTION_TYPES.has(type)) return true
|
|
34
|
+
// 兼容别的命名(例如 compaction/complete),但排除 prune/error 这类非触发事件
|
|
35
|
+
if (/compaction\//i.test(type) && !/prune|error/i.test(type)) return true
|
|
36
|
+
return false
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 同一次压缩的稳定 key,用来去重(start/summary/end 属于同一次)。 */
|
|
40
|
+
function compactionKey(event) {
|
|
41
|
+
try {
|
|
42
|
+
const id = event?.data?.compactionId ?? event?.compactionId
|
|
43
|
+
if (typeof id === 'string' && id) return id
|
|
44
|
+
const seq = event?.seq ?? event?.time
|
|
45
|
+
if (seq !== undefined && seq !== null) return `seq:${seq}`
|
|
46
|
+
return ''
|
|
47
|
+
} catch {
|
|
48
|
+
return ''
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** 32 位字符串哈希(用于给"压缩总结文本"去重)。 */
|
|
53
|
+
function hashText(text) {
|
|
54
|
+
let hash = 2166136261
|
|
55
|
+
const s = String(text ?? '')
|
|
56
|
+
for (let i = 0; i < s.length && i < 4096; i += 1) {
|
|
57
|
+
hash ^= s.charCodeAt(i)
|
|
58
|
+
hash = Math.imul(hash, 16777619)
|
|
59
|
+
}
|
|
60
|
+
return (hash >>> 0).toString(16)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 退化路径:拿不到 session 事件时,从 agent/pre-step 进入本步的消息里识别
|
|
65
|
+
* "压缩总结"(DSH 的总结模板标题)。识别成功返回稳定 key,否则 null。
|
|
66
|
+
*/
|
|
67
|
+
function detectSummaryText(messages) {
|
|
68
|
+
try {
|
|
69
|
+
if (!Array.isArray(messages)) return null
|
|
70
|
+
for (const message of messages) {
|
|
71
|
+
const blocks = message?.content
|
|
72
|
+
const text = typeof blocks === 'string' ? blocks : Array.isArray(blocks) ? blocks.filter((b) => b?.type === 'text' && typeof b.text === 'string').map((b) => b.text).join('\n') : ''
|
|
73
|
+
if (!text || text.length < 200) continue
|
|
74
|
+
if (SUMMARY_MARKERS.some((re) => re.test(text))) return hashText(text)
|
|
75
|
+
}
|
|
76
|
+
return null
|
|
77
|
+
} catch {
|
|
78
|
+
return null
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
module.exports = {
|
|
83
|
+
COMPACTION_TYPES,
|
|
84
|
+
SUMMARY_MARKERS,
|
|
85
|
+
eventType,
|
|
86
|
+
isCompactionEvent,
|
|
87
|
+
compactionKey,
|
|
88
|
+
hashText,
|
|
89
|
+
detectSummaryText,
|
|
90
|
+
}
|
package/lib/index/render.cjs
CHANGED
|
@@ -1,177 +1,177 @@
|
|
|
1
|
-
// render.cjs — 把"路径 + artifact"渲染成一段稳定的索引文本,并裁剪到 token 上限。
|
|
2
|
-
//
|
|
3
|
-
// 格式(设计草案 §3 + 2026-11-14 评审修正):
|
|
4
|
-
// [context index · 2 compactions]
|
|
5
|
-
// artifacts:
|
|
6
|
-
// a1b2c3 pwsh test/run.mjs 4.1K trunc
|
|
7
|
-
// 7a7644 read 99 / full 12.6K need
|
|
8
|
-
// d4e5f6 read src/serialize.mjs 11.9K need ×3
|
|
9
|
-
// … (+3 more)
|
|
10
|
-
// paths:
|
|
11
|
-
// E:\dsh的工作区\reports\eviction-plan.md
|
|
12
|
-
// … (+12 more)
|
|
13
|
-
// ↳ 原文:guard_query(id, pattern) 或 read ~/.dsh/token-guard/artifacts/<id>.txt
|
|
14
|
-
//
|
|
15
|
-
// 头部**不带 step**(A2):step 每步都变,会让"同值零事件"保护失效;
|
|
16
|
-
// step 只进审计。compactions 在一个压缩周期内是稳定的。
|
|
17
|
-
//
|
|
18
|
-
// 渲染是纯函数:同样的输入 → 同样的输出(可单测、可回放)。
|
|
19
|
-
const { estimateTokens } = require('./tokens.cjs')
|
|
20
|
-
|
|
21
|
-
function clampInt(value, fallback, min, max) {
|
|
22
|
-
const n = Number(value)
|
|
23
|
-
if (!Number.isFinite(n)) return fallback
|
|
24
|
-
return Math.max(min, Math.min(max, Math.floor(n)))
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// 把过长的目标列从"头部"压缩,保留尾部(文件名/级别更有信息)
|
|
28
|
-
function shorten(text, maxChars) {
|
|
29
|
-
if (typeof text !== 'string') return ''
|
|
30
|
-
if (text.length <= maxChars) return text
|
|
31
|
-
return `…${text.slice(-(maxChars - 1))}`
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function emptyResult(step, compactions) {
|
|
35
|
-
return {
|
|
36
|
-
text: '',
|
|
37
|
-
chars: 0,
|
|
38
|
-
estTokens: 0,
|
|
39
|
-
step,
|
|
40
|
-
compactions,
|
|
41
|
-
pathLines: 0,
|
|
42
|
-
artifactLines: 0,
|
|
43
|
-
droppedPaths: 0,
|
|
44
|
-
droppedArtifacts: 0,
|
|
45
|
-
totalPaths: 0,
|
|
46
|
-
totalArtifacts: 0,
|
|
47
|
-
overBudget: false,
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function renderBody({ paths, artifacts, totalPaths, totalArtifacts, compactions, targetChars, hint = true }) {
|
|
52
|
-
const lines = [`[context index · ${compactions} compaction${compactions === 1 ? '' : 's'}]`]
|
|
53
|
-
|
|
54
|
-
if (totalArtifacts > 0) {
|
|
55
|
-
lines.push('artifacts:')
|
|
56
|
-
if (artifacts.length === 0) {
|
|
57
|
-
lines.push(` … (+${totalArtifacts} more)`)
|
|
58
|
-
} else {
|
|
59
|
-
const targetWidth = artifacts.reduce((w, a) => Math.max(w, shorten(a.target, targetChars).length), 4)
|
|
60
|
-
for (const artifact of artifacts) {
|
|
61
|
-
const id = String(artifact.id).padEnd(6)
|
|
62
|
-
const tool = String(artifact.tool).slice(0, 6).padEnd(6)
|
|
63
|
-
const target = shorten(artifact.target ?? '', targetChars).padEnd(targetWidth)
|
|
64
|
-
const size = String(artifact.size ?? '—')
|
|
65
|
-
const dup = Number(artifact.dup) > 1 ? ` ×${artifact.dup}` : ''
|
|
66
|
-
const status = `${artifact.status ?? '—'}${dup}`
|
|
67
|
-
lines.push(` ${id} ${tool} ${target} ${size} ${status}`)
|
|
68
|
-
}
|
|
69
|
-
const dropped = totalArtifacts - artifacts.length
|
|
70
|
-
if (dropped > 0) lines.push(` … (+${dropped} more)`)
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (totalPaths > 0) {
|
|
75
|
-
lines.push('paths:')
|
|
76
|
-
if (paths.length === 0) {
|
|
77
|
-
lines.push(` … (+${totalPaths} more)`)
|
|
78
|
-
} else {
|
|
79
|
-
for (const p of paths) lines.push(` ${p}`)
|
|
80
|
-
const dropped = totalPaths - paths.length
|
|
81
|
-
if (dropped > 0) lines.push(` … (+${dropped} more)`)
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// 取回提示:没有它,id 对模型就是一串无意义的字符。
|
|
86
|
-
if (totalArtifacts > 0 && hint) {
|
|
87
|
-
lines.push('↳ 原文:guard_query(id, pattern) 或 read ~/.dsh/token-guard/artifacts/<id>.txt')
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return lines.join('\n')
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* 生成索引文本。
|
|
95
|
-
* @param {object} opts
|
|
96
|
-
* @param {string[]} opts.paths 最近使用在前,已归一化、已脱敏
|
|
97
|
-
* @param {object[]} opts.artifacts 最近在前 {id,tool,target,size,status,dup}
|
|
98
|
-
* @param {number} opts.step 只进审计,不进文本(A2)
|
|
99
|
-
* @param {number} opts.compactions
|
|
100
|
-
* @param {object} opts.limits {maxTokens, pathLimit, artifactLimit, targetChars, hint}
|
|
101
|
-
*/
|
|
102
|
-
function buildIndex(opts = {}) {
|
|
103
|
-
const { paths = [], artifacts = [], step = 0, compactions = 0 } = opts
|
|
104
|
-
const limits = opts.limits ?? {}
|
|
105
|
-
const maxTokens = clampInt(limits.maxTokens, 1000, 120, 100000)
|
|
106
|
-
const pathLimit = clampInt(limits.pathLimit, 40, 1, 200)
|
|
107
|
-
const artifactLimit = clampInt(limits.artifactLimit, 20, 1, 100)
|
|
108
|
-
const targetChars = clampInt(limits.targetChars, 48, 12, 120)
|
|
109
|
-
|
|
110
|
-
const safePaths = Array.isArray(paths) ? paths.filter((p) => typeof p === 'string' && p) : []
|
|
111
|
-
const safeArtifacts = Array.isArray(artifacts) ? artifacts.filter((a) => a && typeof a === 'object') : []
|
|
112
|
-
const totalPaths = safePaths.length
|
|
113
|
-
const totalArtifacts = safeArtifacts.length
|
|
114
|
-
|
|
115
|
-
if (totalPaths === 0 && totalArtifacts === 0) return emptyResult(step, compactions)
|
|
116
|
-
|
|
117
|
-
// 1) 条数上限(硬约束:paths ≤ pathLimit、artifacts ≤ artifactLimit)
|
|
118
|
-
let shownPaths = safePaths.slice(0, pathLimit)
|
|
119
|
-
let shownArtifacts = safeArtifacts.slice(0, artifactLimit)
|
|
120
|
-
|
|
121
|
-
// 2) token 上限:先裁路径(最不常用在尾部),再裁 artifact,直到放得下
|
|
122
|
-
for (;;) {
|
|
123
|
-
const text = renderBody({
|
|
124
|
-
paths: shownPaths,
|
|
125
|
-
artifacts: shownArtifacts,
|
|
126
|
-
totalPaths,
|
|
127
|
-
totalArtifacts,
|
|
128
|
-
compactions,
|
|
129
|
-
targetChars,
|
|
130
|
-
hint: limits.hint !== false,
|
|
131
|
-
})
|
|
132
|
-
const estTokens = estimateTokens(text)
|
|
133
|
-
if (estTokens <= maxTokens) {
|
|
134
|
-
return {
|
|
135
|
-
text,
|
|
136
|
-
chars: text.length,
|
|
137
|
-
estTokens,
|
|
138
|
-
step,
|
|
139
|
-
compactions,
|
|
140
|
-
pathLines: shownPaths.length,
|
|
141
|
-
artifactLines: shownArtifacts.length,
|
|
142
|
-
droppedPaths: totalPaths - shownPaths.length,
|
|
143
|
-
droppedArtifacts: totalArtifacts - shownArtifacts.length,
|
|
144
|
-
totalPaths,
|
|
145
|
-
totalArtifacts,
|
|
146
|
-
overBudget: false,
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
if (shownPaths.length > 0) {
|
|
150
|
-
const drop = Math.max(1, Math.ceil(shownPaths.length * 0.2))
|
|
151
|
-
shownPaths = shownPaths.slice(0, Math.max(0, shownPaths.length - drop))
|
|
152
|
-
continue
|
|
153
|
-
}
|
|
154
|
-
if (shownArtifacts.length > 0) {
|
|
155
|
-
const drop = Math.max(1, Math.ceil(shownArtifacts.length * 0.2))
|
|
156
|
-
shownArtifacts = shownArtifacts.slice(0, Math.max(0, shownArtifacts.length - drop))
|
|
157
|
-
continue
|
|
158
|
-
}
|
|
159
|
-
// 只剩两个 "(+N more)" 标记还是超预算:说明上限配得过小,硬性返回
|
|
160
|
-
return {
|
|
161
|
-
text,
|
|
162
|
-
chars: text.length,
|
|
163
|
-
estTokens,
|
|
164
|
-
step,
|
|
165
|
-
compactions,
|
|
166
|
-
pathLines: 0,
|
|
167
|
-
artifactLines: 0,
|
|
168
|
-
droppedPaths: totalPaths,
|
|
169
|
-
droppedArtifacts: totalArtifacts,
|
|
170
|
-
totalPaths,
|
|
171
|
-
totalArtifacts,
|
|
172
|
-
overBudget: true,
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
module.exports = { buildIndex, renderBody, shorten }
|
|
1
|
+
// render.cjs — 把"路径 + artifact"渲染成一段稳定的索引文本,并裁剪到 token 上限。
|
|
2
|
+
//
|
|
3
|
+
// 格式(设计草案 §3 + 2026-11-14 评审修正):
|
|
4
|
+
// [context index · 2 compactions]
|
|
5
|
+
// artifacts:
|
|
6
|
+
// a1b2c3 pwsh test/run.mjs 4.1K trunc
|
|
7
|
+
// 7a7644 read 99 / full 12.6K need
|
|
8
|
+
// d4e5f6 read src/serialize.mjs 11.9K need ×3
|
|
9
|
+
// … (+3 more)
|
|
10
|
+
// paths:
|
|
11
|
+
// E:\dsh的工作区\reports\eviction-plan.md
|
|
12
|
+
// … (+12 more)
|
|
13
|
+
// ↳ 原文:guard_query(id, pattern) 或 read ~/.dsh/token-guard/artifacts/<id>.txt
|
|
14
|
+
//
|
|
15
|
+
// 头部**不带 step**(A2):step 每步都变,会让"同值零事件"保护失效;
|
|
16
|
+
// step 只进审计。compactions 在一个压缩周期内是稳定的。
|
|
17
|
+
//
|
|
18
|
+
// 渲染是纯函数:同样的输入 → 同样的输出(可单测、可回放)。
|
|
19
|
+
const { estimateTokens } = require('./tokens.cjs')
|
|
20
|
+
|
|
21
|
+
function clampInt(value, fallback, min, max) {
|
|
22
|
+
const n = Number(value)
|
|
23
|
+
if (!Number.isFinite(n)) return fallback
|
|
24
|
+
return Math.max(min, Math.min(max, Math.floor(n)))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// 把过长的目标列从"头部"压缩,保留尾部(文件名/级别更有信息)
|
|
28
|
+
function shorten(text, maxChars) {
|
|
29
|
+
if (typeof text !== 'string') return ''
|
|
30
|
+
if (text.length <= maxChars) return text
|
|
31
|
+
return `…${text.slice(-(maxChars - 1))}`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function emptyResult(step, compactions) {
|
|
35
|
+
return {
|
|
36
|
+
text: '',
|
|
37
|
+
chars: 0,
|
|
38
|
+
estTokens: 0,
|
|
39
|
+
step,
|
|
40
|
+
compactions,
|
|
41
|
+
pathLines: 0,
|
|
42
|
+
artifactLines: 0,
|
|
43
|
+
droppedPaths: 0,
|
|
44
|
+
droppedArtifacts: 0,
|
|
45
|
+
totalPaths: 0,
|
|
46
|
+
totalArtifacts: 0,
|
|
47
|
+
overBudget: false,
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function renderBody({ paths, artifacts, totalPaths, totalArtifacts, compactions, targetChars, hint = true }) {
|
|
52
|
+
const lines = [`[context index · ${compactions} compaction${compactions === 1 ? '' : 's'}]`]
|
|
53
|
+
|
|
54
|
+
if (totalArtifacts > 0) {
|
|
55
|
+
lines.push('artifacts:')
|
|
56
|
+
if (artifacts.length === 0) {
|
|
57
|
+
lines.push(` … (+${totalArtifacts} more)`)
|
|
58
|
+
} else {
|
|
59
|
+
const targetWidth = artifacts.reduce((w, a) => Math.max(w, shorten(a.target, targetChars).length), 4)
|
|
60
|
+
for (const artifact of artifacts) {
|
|
61
|
+
const id = String(artifact.id).padEnd(6)
|
|
62
|
+
const tool = String(artifact.tool).slice(0, 6).padEnd(6)
|
|
63
|
+
const target = shorten(artifact.target ?? '', targetChars).padEnd(targetWidth)
|
|
64
|
+
const size = String(artifact.size ?? '—')
|
|
65
|
+
const dup = Number(artifact.dup) > 1 ? ` ×${artifact.dup}` : ''
|
|
66
|
+
const status = `${artifact.status ?? '—'}${dup}`
|
|
67
|
+
lines.push(` ${id} ${tool} ${target} ${size} ${status}`)
|
|
68
|
+
}
|
|
69
|
+
const dropped = totalArtifacts - artifacts.length
|
|
70
|
+
if (dropped > 0) lines.push(` … (+${dropped} more)`)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (totalPaths > 0) {
|
|
75
|
+
lines.push('paths:')
|
|
76
|
+
if (paths.length === 0) {
|
|
77
|
+
lines.push(` … (+${totalPaths} more)`)
|
|
78
|
+
} else {
|
|
79
|
+
for (const p of paths) lines.push(` ${p}`)
|
|
80
|
+
const dropped = totalPaths - paths.length
|
|
81
|
+
if (dropped > 0) lines.push(` … (+${dropped} more)`)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 取回提示:没有它,id 对模型就是一串无意义的字符。
|
|
86
|
+
if (totalArtifacts > 0 && hint) {
|
|
87
|
+
lines.push('↳ 原文:guard_query(id, pattern) 或 read ~/.dsh/token-guard/artifacts/<id>.txt')
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return lines.join('\n')
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 生成索引文本。
|
|
95
|
+
* @param {object} opts
|
|
96
|
+
* @param {string[]} opts.paths 最近使用在前,已归一化、已脱敏
|
|
97
|
+
* @param {object[]} opts.artifacts 最近在前 {id,tool,target,size,status,dup}
|
|
98
|
+
* @param {number} opts.step 只进审计,不进文本(A2)
|
|
99
|
+
* @param {number} opts.compactions
|
|
100
|
+
* @param {object} opts.limits {maxTokens, pathLimit, artifactLimit, targetChars, hint}
|
|
101
|
+
*/
|
|
102
|
+
function buildIndex(opts = {}) {
|
|
103
|
+
const { paths = [], artifacts = [], step = 0, compactions = 0 } = opts
|
|
104
|
+
const limits = opts.limits ?? {}
|
|
105
|
+
const maxTokens = clampInt(limits.maxTokens, 1000, 120, 100000)
|
|
106
|
+
const pathLimit = clampInt(limits.pathLimit, 40, 1, 200)
|
|
107
|
+
const artifactLimit = clampInt(limits.artifactLimit, 20, 1, 100)
|
|
108
|
+
const targetChars = clampInt(limits.targetChars, 48, 12, 120)
|
|
109
|
+
|
|
110
|
+
const safePaths = Array.isArray(paths) ? paths.filter((p) => typeof p === 'string' && p) : []
|
|
111
|
+
const safeArtifacts = Array.isArray(artifacts) ? artifacts.filter((a) => a && typeof a === 'object') : []
|
|
112
|
+
const totalPaths = safePaths.length
|
|
113
|
+
const totalArtifacts = safeArtifacts.length
|
|
114
|
+
|
|
115
|
+
if (totalPaths === 0 && totalArtifacts === 0) return emptyResult(step, compactions)
|
|
116
|
+
|
|
117
|
+
// 1) 条数上限(硬约束:paths ≤ pathLimit、artifacts ≤ artifactLimit)
|
|
118
|
+
let shownPaths = safePaths.slice(0, pathLimit)
|
|
119
|
+
let shownArtifacts = safeArtifacts.slice(0, artifactLimit)
|
|
120
|
+
|
|
121
|
+
// 2) token 上限:先裁路径(最不常用在尾部),再裁 artifact,直到放得下
|
|
122
|
+
for (;;) {
|
|
123
|
+
const text = renderBody({
|
|
124
|
+
paths: shownPaths,
|
|
125
|
+
artifacts: shownArtifacts,
|
|
126
|
+
totalPaths,
|
|
127
|
+
totalArtifacts,
|
|
128
|
+
compactions,
|
|
129
|
+
targetChars,
|
|
130
|
+
hint: limits.hint !== false,
|
|
131
|
+
})
|
|
132
|
+
const estTokens = estimateTokens(text)
|
|
133
|
+
if (estTokens <= maxTokens) {
|
|
134
|
+
return {
|
|
135
|
+
text,
|
|
136
|
+
chars: text.length,
|
|
137
|
+
estTokens,
|
|
138
|
+
step,
|
|
139
|
+
compactions,
|
|
140
|
+
pathLines: shownPaths.length,
|
|
141
|
+
artifactLines: shownArtifacts.length,
|
|
142
|
+
droppedPaths: totalPaths - shownPaths.length,
|
|
143
|
+
droppedArtifacts: totalArtifacts - shownArtifacts.length,
|
|
144
|
+
totalPaths,
|
|
145
|
+
totalArtifacts,
|
|
146
|
+
overBudget: false,
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (shownPaths.length > 0) {
|
|
150
|
+
const drop = Math.max(1, Math.ceil(shownPaths.length * 0.2))
|
|
151
|
+
shownPaths = shownPaths.slice(0, Math.max(0, shownPaths.length - drop))
|
|
152
|
+
continue
|
|
153
|
+
}
|
|
154
|
+
if (shownArtifacts.length > 0) {
|
|
155
|
+
const drop = Math.max(1, Math.ceil(shownArtifacts.length * 0.2))
|
|
156
|
+
shownArtifacts = shownArtifacts.slice(0, Math.max(0, shownArtifacts.length - drop))
|
|
157
|
+
continue
|
|
158
|
+
}
|
|
159
|
+
// 只剩两个 "(+N more)" 标记还是超预算:说明上限配得过小,硬性返回
|
|
160
|
+
return {
|
|
161
|
+
text,
|
|
162
|
+
chars: text.length,
|
|
163
|
+
estTokens,
|
|
164
|
+
step,
|
|
165
|
+
compactions,
|
|
166
|
+
pathLines: 0,
|
|
167
|
+
artifactLines: 0,
|
|
168
|
+
droppedPaths: totalPaths,
|
|
169
|
+
droppedArtifacts: totalArtifacts,
|
|
170
|
+
totalPaths,
|
|
171
|
+
totalArtifacts,
|
|
172
|
+
overBudget: true,
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
module.exports = { buildIndex, renderBody, shorten }
|