dsh-context-kit 0.2.0 → 0.3.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/README.md +10 -7
- package/client/panel.js +91 -0
- package/cordis.patch.yml +4 -14
- package/host.js +8 -0
- package/lib/remote.mjs +7 -1
- package/lib/tool-savings.mjs +78 -0
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
# dsh-context-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Manage the automatic-compaction threshold in DSH without freezing the agent preset.
|
|
4
4
|
|
|
5
5
|
| part | what it does |
|
|
6
6
|
| --- | --- |
|
|
7
|
-
| **context index** (host) | Keeps a small, stable system-prompt section listing the *pointers* that compaction would otherwise erase: recent tool artifacts and touched paths. Summaries lose content, not locations. |
|
|
8
7
|
| **preset tuner** (host) | Moves the automatic-compaction trigger earlier — and does it without freezing the agent preset, by regenerating the override from the preset that ships inside the app. |
|
|
9
8
|
| **Settings panel** (web) | A Context page: drag the trigger point, apply, preview, remove. No YAML editing. |
|
|
9
|
+
| **package entry** (host, `host.js`) | An intentionally empty plugin row that keeps `dsh-context-kit` itself present in the composed tree. DSH mounts a package's browser half by **package name**; with only subpath rows (`dsh-context-kit/tuner`) the Settings page silently fails to load. |
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
> The context index moved to **[dsh-token-trim](https://www.npmjs.com/package/dsh-token-trim)** in 0.3.0:
|
|
12
|
+
> that package now owns tool-result trimming, the context index and the “Savings” view. Install it next to
|
|
13
|
+
> this one if you want the pointer index back.
|
|
14
|
+
|
|
15
|
+
Why the tuner: compaction is the lever that actually moves the bill. Compacting at 410K instead of 679K
|
|
16
|
+
avoids re-sending the region above the trigger on every step.
|
|
15
17
|
|
|
16
18
|
## Install
|
|
17
19
|
|
|
@@ -21,11 +23,12 @@ dsh plugin add dsh-context-kit
|
|
|
21
23
|
|
|
22
24
|
Restart DSH. You get:
|
|
23
25
|
|
|
24
|
-
-
|
|
26
|
+
- the tuner host plugin (`context-kit-tuner`) plus the empty package row (`context-kit`),
|
|
25
27
|
- a **Settings → Context** page,
|
|
26
28
|
- a `preset_tuner` tool,
|
|
27
29
|
- a `dsh-context-kit` CLI.
|
|
28
30
|
|
|
31
|
+
|
|
29
32
|
## Panel: Settings → Context
|
|
30
33
|
|
|
31
34
|
- **Compact at** — the fraction of the context window that triggers compaction (default `0.5` → ~524K).
|
package/client/panel.js
CHANGED
|
@@ -60,6 +60,19 @@ window.__ModuleLoader__.load({
|
|
|
60
60
|
effectCurrent: 'current',
|
|
61
61
|
effectCoverage: 'data under the current threshold',
|
|
62
62
|
effectModelOnly: 'Too few steps ran under the current threshold — treat the estimate as a direction, not a result.',
|
|
63
|
+
toolTitle: 'Tool savings',
|
|
64
|
+
toolLinked: 'linked to token-trim',
|
|
65
|
+
toolStandalone: 'standalone (token-trim not loaded)',
|
|
66
|
+
toolNote: 'Read from the token-trim manifest. 0.306 token/char measured here; tool results were 40.7% of the audited bill. Not a bill.',
|
|
67
|
+
toolMissing: 'No token-trim manifest yet. Install dsh-token-trim to get tool-level summaries and truncation; this card then fills itself.',
|
|
68
|
+
toolEmpty: 'Manifest exists, but no result has been transformed yet.',
|
|
69
|
+
toolEvents: 'results',
|
|
70
|
+
toolTokens: 'est. tokens',
|
|
71
|
+
toolSaved: 'saved',
|
|
72
|
+
toolBill: 'est. bill',
|
|
73
|
+
toolView: 'Full session detail is the “Savings” View tab next to Chat / Trajectory / Stats.',
|
|
74
|
+
toolRun: 'Refresh',
|
|
75
|
+
toolSessions: 'sessions',
|
|
63
76
|
}
|
|
64
77
|
|
|
65
78
|
const ZH = {
|
|
@@ -116,6 +129,19 @@ window.__ModuleLoader__.load({
|
|
|
116
129
|
effectCurrent: '当前',
|
|
117
130
|
effectCoverage: '当前阈值下的实测',
|
|
118
131
|
effectModelOnly: '当前阈值下实际跑过的步数还太少 —— 这个估算只能当方向,不能当结论。',
|
|
132
|
+
toolTitle: '工具节省',
|
|
133
|
+
toolLinked: '已联动 token-trim',
|
|
134
|
+
toolStandalone: '独立统计(token-trim 未加载)',
|
|
135
|
+
toolNote: '来自 token-trim 的 manifest。0.306 token/字符为本机实测;工具结果占审计账单 40.7%。这不是账单。',
|
|
136
|
+
toolMissing: '还没有 token-trim 的 manifest。装 dsh-token-trim 后,这里会自动出现工具级摘要与截断的统计。',
|
|
137
|
+
toolEmpty: 'manifest 已存在,但还没有任何结果被改写。',
|
|
138
|
+
toolEvents: '次处理',
|
|
139
|
+
toolTokens: '约合 token',
|
|
140
|
+
toolSaved: '省下',
|
|
141
|
+
toolBill: '折算账单',
|
|
142
|
+
toolView: '完整的分会话明细在对话上方的「节省」视图标签(与 对话 / 轨迹 / 统计 并列)。',
|
|
143
|
+
toolRun: '刷新',
|
|
144
|
+
toolSessions: '个会话',
|
|
119
145
|
}
|
|
120
146
|
|
|
121
147
|
const CSS = [
|
|
@@ -136,6 +162,9 @@ window.__ModuleLoader__.load({
|
|
|
136
162
|
'.ck_warn { color: #d97706; }',
|
|
137
163
|
'.ck_err { color: #dc2626; }',
|
|
138
164
|
'.ck_pre { max-height: 220px; overflow: auto; font-size: 11px; background: rgba(127,127,127,.12); padding: 8px; border-radius: 8px; white-space: pre-wrap; }',
|
|
165
|
+
'.ck_badge { font-size: 10px; padding: 1px 6px; border-radius: 999px; background: rgba(37,99,235,.16); }',
|
|
166
|
+
'.ck_badge.off { background: rgba(127,127,127,.18); }',
|
|
167
|
+
'.ck_big { font-size: 18px; font-weight: 600; font-variant-numeric: tabular-nums; }',
|
|
139
168
|
].join('\n')
|
|
140
169
|
|
|
141
170
|
const fmtK = (tokens) => {
|
|
@@ -195,6 +224,11 @@ window.__ModuleLoader__.load({
|
|
|
195
224
|
clear: () => call('clear', {}),
|
|
196
225
|
sync: () => call('sync', { force: false, mode: 'append' }),
|
|
197
226
|
report: (days) => call('report', { days: days }),
|
|
227
|
+
toolSavings: () => call('toolSavings', {}),
|
|
228
|
+
probeTokenGuard: () =>
|
|
229
|
+
ctx.connection.rpc.call('/api', 'tokenTrim/status', { args: {} }, undefined).then(function (response) {
|
|
230
|
+
return Boolean(response && response.ok)
|
|
231
|
+
}),
|
|
198
232
|
save: (input) =>
|
|
199
233
|
call('save', {
|
|
200
234
|
presetId: input.presetId,
|
|
@@ -260,6 +294,40 @@ window.__ModuleLoader__.load({
|
|
|
260
294
|
...rows)
|
|
261
295
|
}
|
|
262
296
|
|
|
297
|
+
function ToolSavingsCard(props) {
|
|
298
|
+
const t = props.t
|
|
299
|
+
const data = props.data
|
|
300
|
+
const linked = props.linked
|
|
301
|
+
const rows = []
|
|
302
|
+
if (!data) {
|
|
303
|
+
rows.push(meta('…'))
|
|
304
|
+
} else if (!data.available) {
|
|
305
|
+
rows.push(meta(t('toolMissing')))
|
|
306
|
+
} else if (!data.events) {
|
|
307
|
+
rows.push(meta(t('toolEmpty')))
|
|
308
|
+
} else {
|
|
309
|
+
rows.push(h('div', { className: 'ck_row' },
|
|
310
|
+
h('span', { className: 'ck_big' }, '−' + Number(data.cutPct).toFixed(1) + '%'),
|
|
311
|
+
meta(fmtBig(data.orig) + ' → ' + fmtBig(data.vis) + ' chars')))
|
|
312
|
+
rows.push(h('div', { className: 'ck_row' },
|
|
313
|
+
meta(data.events + ' ' + t('toolEvents') + ' · ' + data.sessions + ' ' + t('toolSessions')),
|
|
314
|
+
meta(t('toolSaved') + ' ' + fmtBig(data.saved) + ' chars'),
|
|
315
|
+
meta(t('toolTokens') + ' ~' + fmtBig(data.tokens))))
|
|
316
|
+
for (const tool of data.tools || []) {
|
|
317
|
+
rows.push(meta(' ' + tool.name + ': ' + fmtBig(tool.saved) + ' chars / ' + tool.events))
|
|
318
|
+
}
|
|
319
|
+
rows.push(meta(t('toolView')))
|
|
320
|
+
rows.push(meta(t('toolNote')))
|
|
321
|
+
}
|
|
322
|
+
return h('div', { className: 'ck_card' },
|
|
323
|
+
h('div', { className: 'ck_row' },
|
|
324
|
+
h('label', null, t('toolTitle')),
|
|
325
|
+
h('span', { className: 'ck_badge' + (linked ? '' : ' off') }, linked ? t('toolLinked') : t('toolStandalone')),
|
|
326
|
+
h('button', { className: 'ck_btn', type: 'button', disabled: props.busy, onClick: props.onRun },
|
|
327
|
+
props.busy ? t('busy') : t('toolRun'))),
|
|
328
|
+
...rows)
|
|
329
|
+
}
|
|
330
|
+
|
|
263
331
|
function SliderRow(props) {
|
|
264
332
|
return h('div', { className: 'ck_row' },
|
|
265
333
|
h('label', null, props.label),
|
|
@@ -287,6 +355,9 @@ window.__ModuleLoader__.load({
|
|
|
287
355
|
const [error, setError] = React.useState('')
|
|
288
356
|
const [pre, setPre] = React.useState('')
|
|
289
357
|
const [report, setReport] = React.useState(null)
|
|
358
|
+
const [toolSavings, setToolSavings] = React.useState(null)
|
|
359
|
+
const [toolLinked, setToolLinked] = React.useState(false)
|
|
360
|
+
const [toolBusy, setToolBusy] = React.useState(false)
|
|
290
361
|
|
|
291
362
|
const load = React.useCallback(function () {
|
|
292
363
|
return api.status().then(function (next) {
|
|
@@ -302,6 +373,19 @@ window.__ModuleLoader__.load({
|
|
|
302
373
|
|
|
303
374
|
React.useEffect(function () { load() }, [load])
|
|
304
375
|
|
|
376
|
+
const loadTool = React.useCallback(function () {
|
|
377
|
+
setToolBusy(true)
|
|
378
|
+
return api.toolSavings().then(function (data) {
|
|
379
|
+
setToolSavings(data)
|
|
380
|
+
}).catch(function () {
|
|
381
|
+
setToolSavings({ available: false })
|
|
382
|
+
}).then(function () {
|
|
383
|
+
return api.probeTokenGuard().then(function (ok) { setToolLinked(Boolean(ok)) }).catch(function () { setToolLinked(false) })
|
|
384
|
+
}).then(function () { setToolBusy(false) })
|
|
385
|
+
}, [api])
|
|
386
|
+
|
|
387
|
+
React.useEffect(function () { loadTool() }, [loadTool])
|
|
388
|
+
|
|
305
389
|
const run = function (label, task) {
|
|
306
390
|
setBusy(true)
|
|
307
391
|
setError('')
|
|
@@ -406,6 +490,13 @@ window.__ModuleLoader__.load({
|
|
|
406
490
|
})
|
|
407
491
|
},
|
|
408
492
|
}),
|
|
493
|
+
h(ToolSavingsCard, {
|
|
494
|
+
t: t,
|
|
495
|
+
data: toolSavings,
|
|
496
|
+
linked: toolLinked,
|
|
497
|
+
busy: toolBusy,
|
|
498
|
+
onRun: loadTool,
|
|
499
|
+
}),
|
|
409
500
|
...infoRows)
|
|
410
501
|
}
|
|
411
502
|
|
package/cordis.patch.yml
CHANGED
|
@@ -1,19 +1,9 @@
|
|
|
1
|
-
# dsh-context-kit
|
|
2
|
-
#
|
|
3
|
-
# - context-kit-tuner:agent preset 内部配置调优(例如压缩阈值),只插入不冻结上游
|
|
1
|
+
# dsh-context-kit: 压缩阈值调优器(agent preset 内部配置),只插入、不冻结上游。
|
|
2
|
+
# 上下文索引已迁到 dsh-token-trim(0.3.0 起)。
|
|
4
3
|
- insert:
|
|
5
|
-
|
|
4
|
+
# 包名入口:只为让 dsh.client 客户端面板挂到组合树上(host.js 是空实现)。
|
|
5
|
+
- id: context-kit
|
|
6
6
|
name: dsh-context-kit
|
|
7
|
-
config:
|
|
8
|
-
enabled: true
|
|
9
|
-
injectMode: section
|
|
10
|
-
sectionName: index:index-page
|
|
11
|
-
sectionOrder: 10500
|
|
12
|
-
maxTokens: 1000
|
|
13
|
-
pathLimit: 40
|
|
14
|
-
artifactLimit: 20
|
|
15
|
-
targetChars: 48
|
|
16
|
-
manifestLines: 500
|
|
17
7
|
- id: context-kit-tuner
|
|
18
8
|
name: dsh-context-kit/tuner
|
|
19
9
|
config:
|
package/host.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// 仅用于让本包以“包名”出现在组合树里。
|
|
2
|
+
// DSH 的浏览器 roster(dsh.client 客户端半边)按包名挂载:如果组合树里只有
|
|
3
|
+
// `dsh-context-kit/tuner` 这样的子路径入口,客户端面板不会被加载。
|
|
4
|
+
// 真正的宿主逻辑在 ./tuner.js(压缩阈值调优);工具治理与上下文索引已迁到 dsh-token-trim。
|
|
5
|
+
export const name = 'dsh-context-kit'
|
|
6
|
+
export const inject = []
|
|
7
|
+
|
|
8
|
+
export function apply() {}
|
package/lib/remote.mjs
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { Remote, TypertRemoteService } from '@deepseek-ai/dsh-typert-protocol'
|
|
7
|
+
import { summarizeToolSavings } from './tool-savings.mjs'
|
|
7
8
|
|
|
8
9
|
/** 施加 Remote(name) 标记,兼容无装饰器的写法(与 dsh-memory-local 同款)。 */
|
|
9
10
|
export function markRemoteMethod(serviceClass, method, exportName = method) {
|
|
@@ -52,8 +53,13 @@ export class ContextKitRemoteService extends TypertRemoteService {
|
|
|
52
53
|
async report(days) {
|
|
53
54
|
return this.kit.report({ days: Number(days) || 7 })
|
|
54
55
|
}
|
|
56
|
+
|
|
57
|
+
/** 独立统计:不依赖 token-guard 插件,直接读它的 manifest。 */
|
|
58
|
+
toolSavings() {
|
|
59
|
+
return summarizeToolSavings()
|
|
60
|
+
}
|
|
55
61
|
}
|
|
56
62
|
|
|
57
|
-
for (const method of ['status', 'preview', 'save', 'sync', 'clear', 'report']) {
|
|
63
|
+
for (const method of ['status', 'preview', 'save', 'sync', 'clear', 'report', 'toolSavings']) {
|
|
58
64
|
markRemoteMethod(ContextKitRemoteService, method)
|
|
59
65
|
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具节省的「独立统计」:直接读 token-trim 的 manifest,不依赖 token-trim 插件在场。
|
|
3
|
+
* 与 token-trim 的 Settings 视图共享同一口径(0.306 token/字符,工具结果占账单 40.7%)。
|
|
4
|
+
*/
|
|
5
|
+
import { existsSync, readFileSync, statSync } from 'node:fs'
|
|
6
|
+
import { homedir } from 'node:os'
|
|
7
|
+
import path from 'node:path'
|
|
8
|
+
|
|
9
|
+
export const TOK_PER_CHAR = 0.306
|
|
10
|
+
export const TOOL_SHARE_PCT = 40.7
|
|
11
|
+
export const TOOL_TOKENS_BASELINE = 751715
|
|
12
|
+
|
|
13
|
+
export function defaultManifestPath() {
|
|
14
|
+
const current = path.join(homedir(), '.dsh', 'token-trim', 'manifest.jsonl')
|
|
15
|
+
const legacy = path.join(homedir(), '.dsh', 'token-guard', 'manifest.jsonl')
|
|
16
|
+
try {
|
|
17
|
+
if (!existsSync(current) && existsSync(legacy)) return legacy
|
|
18
|
+
} catch {}
|
|
19
|
+
return current
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function readRecords(file) {
|
|
23
|
+
try {
|
|
24
|
+
const text = readFileSync(file, 'utf8')
|
|
25
|
+
const lines = text.split('\n')
|
|
26
|
+
const out = []
|
|
27
|
+
for (const line of lines) {
|
|
28
|
+
if (!line) continue
|
|
29
|
+
try { out.push(JSON.parse(line)) } catch {}
|
|
30
|
+
}
|
|
31
|
+
return out
|
|
32
|
+
} catch {
|
|
33
|
+
return []
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const billPctOf = (savedChars) => (savedChars * TOK_PER_CHAR / TOOL_TOKENS_BASELINE) * TOOL_SHARE_PCT
|
|
38
|
+
|
|
39
|
+
export function summarizeToolSavings(manifestFile = defaultManifestPath()) {
|
|
40
|
+
const records = readRecords(manifestFile)
|
|
41
|
+
if (!records.length) return { available: false, manifest: manifestFile }
|
|
42
|
+
let orig = 0
|
|
43
|
+
let vis = 0
|
|
44
|
+
const tools = new Map()
|
|
45
|
+
const sessions = new Set()
|
|
46
|
+
let last = 0
|
|
47
|
+
for (const r of records) {
|
|
48
|
+
const o = Number(r.originalChars) || 0
|
|
49
|
+
const v = Number(r.visibleChars) || 0
|
|
50
|
+
orig += o
|
|
51
|
+
vis += v
|
|
52
|
+
if (r.session) sessions.add(String(r.session))
|
|
53
|
+
last = Math.max(last, Number(r.ts) || 0)
|
|
54
|
+
const name = typeof r.tool === 'string' ? r.tool : '(unknown)'
|
|
55
|
+
const t = tools.get(name) || { name, events: 0, saved: 0 }
|
|
56
|
+
t.events += 1
|
|
57
|
+
t.saved += Math.max(0, o - v)
|
|
58
|
+
tools.set(name, t)
|
|
59
|
+
}
|
|
60
|
+
let updatedAt = 0
|
|
61
|
+
try { updatedAt = statSync(manifestFile).mtimeMs } catch {}
|
|
62
|
+
const saved = Math.max(0, orig - vis)
|
|
63
|
+
return {
|
|
64
|
+
available: true,
|
|
65
|
+
manifest: manifestFile,
|
|
66
|
+
events: records.length,
|
|
67
|
+
sessions: sessions.size,
|
|
68
|
+
orig,
|
|
69
|
+
vis,
|
|
70
|
+
saved,
|
|
71
|
+
cutPct: orig ? Number(((1 - vis / orig) * 100).toFixed(1)) : 0,
|
|
72
|
+
tokens: Math.round(saved * TOK_PER_CHAR),
|
|
73
|
+
billPct: Number(billPctOf(saved).toFixed(2)),
|
|
74
|
+
last,
|
|
75
|
+
updatedAt,
|
|
76
|
+
tools: [...tools.values()].sort((a, b) => b.saved - a.saved).slice(0, 6),
|
|
77
|
+
}
|
|
78
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-context-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Compaction-threshold tuner for DeepSeek Harness: edits the agent preset (thresholdRatio / retainRatio) with provenance-safe patches, and shows measured usage. The context index now lives in dsh-token-trim.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"license": "MIT",
|
|
@@ -23,12 +23,14 @@
|
|
|
23
23
|
"node": ">=22"
|
|
24
24
|
},
|
|
25
25
|
"exports": {
|
|
26
|
-
".": "./
|
|
26
|
+
".": "./host.js",
|
|
27
|
+
"./index-host": "./index.js",
|
|
27
28
|
"./tuner": "./tuner.js",
|
|
28
29
|
"./client": "./client/panel.js",
|
|
29
30
|
"./icon": "./icon.svg"
|
|
30
31
|
},
|
|
31
32
|
"files": [
|
|
33
|
+
"host.js",
|
|
32
34
|
"index.js",
|
|
33
35
|
"tuner.js",
|
|
34
36
|
"lib/",
|