dsh-context-kit 0.1.1 → 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 +41 -9
- package/bin/dsh-context-kit.mjs +36 -1
- package/client/panel.js +341 -115
- package/cordis.patch.yml +4 -14
- package/host.js +8 -0
- package/lib/kit-ops.mjs +25 -2
- package/lib/remote.mjs +11 -1
- package/lib/tool-savings.mjs +78 -0
- package/lib/usage.mjs +448 -0
- package/package.json +6 -3
- package/tuner.js +67 -3
- package/vendor/fzstd/LICENSE +21 -0
- package/vendor/fzstd/index.mjs +753 -0
package/client/panel.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// dsh-context-kit 的 Settings 面板(Web 客户端插件)。
|
|
2
|
-
// 通过 ctx.connection.rpc 调宿主 contextKit 服务:status / save / sync / preview / clear。
|
|
2
|
+
// 通过 ctx.connection.rpc 调宿主 contextKit 服务:status / save / sync / preview / clear / report。
|
|
3
3
|
window.__ModuleLoader__.load({
|
|
4
4
|
id: 'dsh-context-kit',
|
|
5
5
|
factory(require) {
|
|
@@ -12,7 +12,6 @@ window.__ModuleLoader__.load({
|
|
|
12
12
|
subtitle: 'Save tokens by compacting earlier, without freezing the preset.',
|
|
13
13
|
threshold: 'Compact at',
|
|
14
14
|
retain: 'Keep after compact',
|
|
15
|
-
auto: 'Automatic compaction',
|
|
16
15
|
save: 'Save',
|
|
17
16
|
sync: 'Apply to profile',
|
|
18
17
|
preview: 'Preview',
|
|
@@ -35,6 +34,45 @@ window.__ModuleLoader__.load({
|
|
|
35
34
|
upstream: 'Upstream',
|
|
36
35
|
busy: 'Working…',
|
|
37
36
|
error: 'Error',
|
|
37
|
+
effectTitle: 'Measured effect',
|
|
38
|
+
effectHint: "Reads this machine's session logs. Nothing leaves the device.",
|
|
39
|
+
effectRun: 'Measure last 7 days',
|
|
40
|
+
effectRunning: 'Reading session logs…',
|
|
41
|
+
effectEmpty: 'Measuring… the first run decompresses recent sessions and may take a few seconds. Less than 7 days of history is fine — the window is capped at 7 days, not required.',
|
|
42
|
+
effectSessions: 'sessions',
|
|
43
|
+
effectSteps: 'steps',
|
|
44
|
+
effectCompactions: 'compactions',
|
|
45
|
+
effectHit: 'cache hit',
|
|
46
|
+
effectMiss: 'cache miss',
|
|
47
|
+
effectOut: 'output',
|
|
48
|
+
effectMissShare: 'miss share of cost',
|
|
49
|
+
effectContext: 'context (avg / peak)',
|
|
50
|
+
effectCost: 'cost (hit-equivalents)',
|
|
51
|
+
effectSavings: 'Estimated saving',
|
|
52
|
+
effectSavingsNote: 'estimate: replays your real per-step growth against the shipped default; not a bill',
|
|
53
|
+
effectRework: 'Rework signal',
|
|
54
|
+
effectReworkNote: 'repeated identical calls — the first sign compaction went too deep',
|
|
55
|
+
effectSkipped: 'skipped files',
|
|
56
|
+
effectWindow: 'window',
|
|
57
|
+
effectFiles: 'session logs read',
|
|
58
|
+
effectAt: 'measured at',
|
|
59
|
+
effectPolicies: 'observed policies',
|
|
60
|
+
effectCurrent: 'current',
|
|
61
|
+
effectCoverage: 'data under the current threshold',
|
|
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',
|
|
38
76
|
}
|
|
39
77
|
|
|
40
78
|
const ZH = {
|
|
@@ -43,7 +81,6 @@ window.__ModuleLoader__.load({
|
|
|
43
81
|
subtitle: '早点压缩来省 token,同时不冻结预设。',
|
|
44
82
|
threshold: '压缩触发于',
|
|
45
83
|
retain: '压缩后保留',
|
|
46
|
-
auto: '启用自动压缩',
|
|
47
84
|
save: '保存',
|
|
48
85
|
sync: '写入 profile',
|
|
49
86
|
preview: '预览',
|
|
@@ -66,27 +103,84 @@ window.__ModuleLoader__.load({
|
|
|
66
103
|
upstream: '上游',
|
|
67
104
|
busy: '处理中…',
|
|
68
105
|
error: '出错',
|
|
106
|
+
effectTitle: '实测效果',
|
|
107
|
+
effectHint: '只读本机会话日志,数据不出设备。',
|
|
108
|
+
effectRun: '统计最近 7 天',
|
|
109
|
+
effectRunning: '正在读会话日志…',
|
|
110
|
+
effectEmpty: '正在统计… 第一次要解压最近的会话,可能需要几秒。用不满 7 天也能统计 —— 7 天是窗口上限,不是前提。',
|
|
111
|
+
effectSessions: '个会话',
|
|
112
|
+
effectSteps: '步',
|
|
113
|
+
effectCompactions: '次压缩',
|
|
114
|
+
effectHit: '缓存命中',
|
|
115
|
+
effectMiss: '未命中',
|
|
116
|
+
effectOut: '输出',
|
|
117
|
+
effectMissShare: '未命中占成本',
|
|
118
|
+
effectContext: '上下文(均值 / 峰值)',
|
|
119
|
+
effectCost: '成本(命中当量)',
|
|
120
|
+
effectSavings: '估算节省',
|
|
121
|
+
effectSavingsNote: '估算:用你的真实每步增量回放官方默认阈值,不等于账单',
|
|
122
|
+
effectRework: '返工信号',
|
|
123
|
+
effectReworkNote: '重复的相同调用 —— 压缩压过头的第一信号',
|
|
124
|
+
effectSkipped: '跳过文件',
|
|
125
|
+
effectWindow: '窗口',
|
|
126
|
+
effectFiles: '个会话日志',
|
|
127
|
+
effectAt: '统计时间',
|
|
128
|
+
effectPolicies: '观测到的策略',
|
|
129
|
+
effectCurrent: '当前',
|
|
130
|
+
effectCoverage: '当前阈值下的实测',
|
|
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: '个会话',
|
|
69
145
|
}
|
|
70
146
|
|
|
71
|
-
const CSS =
|
|
72
|
-
.ck_wrap { display: flex; flex-direction: column; gap: 14px; }
|
|
73
|
-
.ck_head h2 { margin: 0 0 4px; font-size: 15px; }
|
|
74
|
-
.ck_head p { margin: 0; opacity: .7; font-size: 12px; }
|
|
75
|
-
.ck_card { border: 1px solid rgba(127,127,127,.25); border-radius: 10px; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
|
|
76
|
-
.ck_row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
|
|
77
|
-
.ck_row label { min-width: 110px; font-size: 12px; opacity: .8; }
|
|
78
|
-
.ck_row input[type=range] { flex: 1 1 160px; }
|
|
79
|
-
.ck_val { font-variant-numeric: tabular-nums; min-width: 72px; text-align: right; font-size: 12px; }
|
|
80
|
-
.ck_btns { display: flex; gap: 8px; flex-wrap: wrap; }
|
|
81
|
-
.ck_btn { border: 1px solid rgba(127,127,127,.35); background: transparent; border-radius: 8px; padding: 6px 12px; cursor: pointer; font-size: 12px; }
|
|
82
|
-
.ck_btn.primary { background: #2563eb; border-color: #2563eb; color: #fff; }
|
|
83
|
-
.ck_btn:disabled { opacity: .5; cursor: default; }
|
|
84
|
-
.ck_meta { font-size: 11px; opacity: .65; word-break: break-all; }
|
|
85
|
-
.ck_state { font-size: 12px; padding: 6px 8px; border-radius: 8px; background: rgba(127,127,127,.12); }
|
|
86
|
-
.ck_warn { color: #d97706; }
|
|
87
|
-
.ck_err { color: #dc2626; }
|
|
88
|
-
.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; }
|
|
89
|
-
|
|
147
|
+
const CSS = [
|
|
148
|
+
'.ck_wrap { display: flex; flex-direction: column; gap: 14px; }',
|
|
149
|
+
'.ck_head h2 { margin: 0 0 4px; font-size: 15px; }',
|
|
150
|
+
'.ck_head p { margin: 0; opacity: .7; font-size: 12px; }',
|
|
151
|
+
'.ck_card { border: 1px solid rgba(127,127,127,.25); border-radius: 10px; padding: 12px; display: flex; flex-direction: column; gap: 10px; }',
|
|
152
|
+
'.ck_row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }',
|
|
153
|
+
'.ck_row label { min-width: 110px; font-size: 12px; opacity: .8; }',
|
|
154
|
+
'.ck_row input[type=range] { flex: 1 1 160px; }',
|
|
155
|
+
'.ck_val { font-variant-numeric: tabular-nums; min-width: 72px; text-align: right; font-size: 12px; }',
|
|
156
|
+
'.ck_btns { display: flex; gap: 8px; flex-wrap: wrap; }',
|
|
157
|
+
'.ck_btn { border: 1px solid rgba(127,127,127,.35); background: transparent; border-radius: 8px; padding: 6px 12px; cursor: pointer; font-size: 12px; }',
|
|
158
|
+
'.ck_btn.primary { background: #2563eb; border-color: #2563eb; color: #fff; }',
|
|
159
|
+
'.ck_btn:disabled { opacity: .5; cursor: default; }',
|
|
160
|
+
'.ck_meta { font-size: 11px; opacity: .65; word-break: break-all; }',
|
|
161
|
+
'.ck_state { font-size: 12px; padding: 6px 8px; border-radius: 8px; background: rgba(127,127,127,.12); }',
|
|
162
|
+
'.ck_warn { color: #d97706; }',
|
|
163
|
+
'.ck_err { color: #dc2626; }',
|
|
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; }',
|
|
168
|
+
].join('\n')
|
|
169
|
+
|
|
170
|
+
const fmtK = (tokens) => {
|
|
171
|
+
if (!Number.isFinite(tokens)) return '—'
|
|
172
|
+
if (tokens >= 1024 * 1024) return (tokens / (1024 * 1024)).toFixed(1) + 'M'
|
|
173
|
+
return Math.round(tokens / 1024) + 'K'
|
|
174
|
+
}
|
|
175
|
+
const fmtBig = (value) => {
|
|
176
|
+
if (!Number.isFinite(value)) return '—'
|
|
177
|
+
if (value >= 1e9) return (value / 1e9).toFixed(2) + 'B'
|
|
178
|
+
if (value >= 1e6) return (value / 1e6).toFixed(1) + 'M'
|
|
179
|
+
if (value >= 1e3) return Math.round(value / 1e3) + 'K'
|
|
180
|
+
return String(Math.round(value))
|
|
181
|
+
}
|
|
182
|
+
const pct = (ratio) => Math.round(Number(ratio) * 1000) / 10 + '%'
|
|
183
|
+
const meta = (value) => h('div', { className: 'ck_meta' }, value)
|
|
90
184
|
|
|
91
185
|
function makeT(ctx) {
|
|
92
186
|
try {
|
|
@@ -106,7 +200,9 @@ window.__ModuleLoader__.load({
|
|
|
106
200
|
}
|
|
107
201
|
} catch {}
|
|
108
202
|
let id = 'en'
|
|
109
|
-
try {
|
|
203
|
+
try {
|
|
204
|
+
id = ctx.locale.getLocale().id
|
|
205
|
+
} catch {}
|
|
110
206
|
const dict = id === 'zh' ? ZH : EN
|
|
111
207
|
return dict[key] || fallback || key
|
|
112
208
|
}
|
|
@@ -114,7 +210,7 @@ window.__ModuleLoader__.load({
|
|
|
114
210
|
|
|
115
211
|
function makeApi(ctx) {
|
|
116
212
|
function call(method, args) {
|
|
117
|
-
return ctx.connection.rpc.call('/api',
|
|
213
|
+
return ctx.connection.rpc.call('/api', 'contextKit/' + method, { args: args || {} }, undefined).then(function (response) {
|
|
118
214
|
if (response && response.ok) return response.value
|
|
119
215
|
const message = response && response.error ? response.error.message : 'context kit remote call failed'
|
|
120
216
|
const error = new Error(message)
|
|
@@ -123,28 +219,130 @@ window.__ModuleLoader__.load({
|
|
|
123
219
|
})
|
|
124
220
|
}
|
|
125
221
|
return {
|
|
126
|
-
status:
|
|
127
|
-
|
|
128
|
-
|
|
222
|
+
status: () => call('status', {}),
|
|
223
|
+
preview: () => call('preview', {}),
|
|
224
|
+
clear: () => call('clear', {}),
|
|
225
|
+
sync: () => call('sync', { force: false, mode: 'append' }),
|
|
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
|
+
}),
|
|
232
|
+
save: (input) =>
|
|
233
|
+
call('save', {
|
|
129
234
|
presetId: input.presetId,
|
|
130
235
|
thresholdRatio: input.thresholdRatio,
|
|
131
236
|
retainRatio: input.retainRatio,
|
|
132
237
|
auto: input.auto,
|
|
133
238
|
group: input.group,
|
|
134
|
-
})
|
|
135
|
-
},
|
|
136
|
-
sync: function () { return call('sync', { force: false, mode: 'append' }) },
|
|
137
|
-
preview: function () { return call('preview', {}) },
|
|
138
|
-
clear: function () { return call('clear', {}) },
|
|
239
|
+
}),
|
|
139
240
|
}
|
|
140
241
|
}
|
|
141
242
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
243
|
+
function EffectCard(props) {
|
|
244
|
+
const t = props.t
|
|
245
|
+
const report = props.report
|
|
246
|
+
const rows = []
|
|
247
|
+
if (report) {
|
|
248
|
+
const total = report.totals
|
|
249
|
+
rows.push(meta(total.sessions + ' ' + t('effectSessions') + ' · ' + total.steps + ' ' + t('effectSteps') + ' · ' + total.compactions + ' ' + t('effectCompactions')))
|
|
250
|
+
if (report.policies && report.policies.length) {
|
|
251
|
+
rows.push(meta(t('effectPolicies')))
|
|
252
|
+
for (const policy of report.policies) {
|
|
253
|
+
const label = policy.triggerTokens === null ? '—' : fmtK(policy.triggerTokens)
|
|
254
|
+
rows.push(meta(' ' + label + ': ' + policy.totals.sessions + ' ' + t('effectSessions') + ' / ' + policy.totals.steps + ' ' + t('effectSteps') + (policy.isCurrent ? ' ← ' + t('effectCurrent') : '')))
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (report.coverage) {
|
|
258
|
+
rows.push(meta(t('effectCoverage') + ': ' + report.coverage.stepsUnderCurrent + ' ' + t('effectSteps') + ' (' + pct(report.coverage.sharePct / 100) + ')'))
|
|
259
|
+
}
|
|
260
|
+
rows.push(
|
|
261
|
+
h('div', { className: 'ck_row' },
|
|
262
|
+
meta(t('effectHit') + ' ' + fmtBig(total.hit)),
|
|
263
|
+
meta(t('effectMiss') + ' ' + fmtBig(total.miss)),
|
|
264
|
+
meta(t('effectOut') + ' ' + fmtBig(total.out))),
|
|
265
|
+
)
|
|
266
|
+
rows.push(
|
|
267
|
+
h('div', { className: 'ck_row' },
|
|
268
|
+
meta(t('effectMissShare') + ' ' + pct(total.missSharePct / 100)),
|
|
269
|
+
meta(t('effectContext') + ' ' + fmtK(total.promptAvg) + ' / ' + fmtK(total.promptMax)),
|
|
270
|
+
meta(t('effectCost') + ' ' + fmtBig(total.cost.total))),
|
|
271
|
+
)
|
|
272
|
+
if (report.savings) {
|
|
273
|
+
rows.push(
|
|
274
|
+
h('div', { className: 'ck_row' },
|
|
275
|
+
h('span', { className: 'ck_val' }, '−' + report.savings.savedPct.toFixed(1) + '%'),
|
|
276
|
+
meta(t('effectSavings') + ' · ' + t('effectSavingsNote'))),
|
|
277
|
+
)
|
|
278
|
+
if (report.coverage && report.coverage.isModelOnly) {
|
|
279
|
+
rows.push(h('div', { className: 'ck_meta ck_warn' }, t('effectModelOnly')))
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
rows.push(meta(t('effectRework') + ' ' + total.duplicates + ' / ' + total.distinctCalls + ' (' + pct(total.duplicatePct / 100) + ') · ' + t('effectReworkNote')))
|
|
283
|
+
if (report.skipped && report.skipped.length) rows.push(h('div', { className: 'ck_meta ck_warn' }, t('effectSkipped') + ': ' + report.skipped.length))
|
|
284
|
+
rows.push(meta(report.privacy))
|
|
285
|
+
} else {
|
|
286
|
+
rows.push(meta(t('effectEmpty')))
|
|
287
|
+
}
|
|
288
|
+
return h('div', { className: 'ck_card' },
|
|
289
|
+
h('div', { className: 'ck_row' },
|
|
290
|
+
h('label', null, t('effectTitle')),
|
|
291
|
+
meta(t('effectHint')),
|
|
292
|
+
h('button', { className: 'ck_btn', type: 'button', disabled: props.busy, onClick: props.onRun },
|
|
293
|
+
props.busy ? t('effectRunning') : t('effectRun'))),
|
|
294
|
+
...rows)
|
|
295
|
+
}
|
|
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
|
+
|
|
331
|
+
function SliderRow(props) {
|
|
332
|
+
return h('div', { className: 'ck_row' },
|
|
333
|
+
h('label', null, props.label),
|
|
334
|
+
h('input', {
|
|
335
|
+
type: 'range',
|
|
336
|
+
min: props.min,
|
|
337
|
+
max: props.max,
|
|
338
|
+
step: 0.01,
|
|
339
|
+
value: props.value,
|
|
340
|
+
disabled: props.disabled,
|
|
341
|
+
onChange: function (event) { props.onChange(Number(event.target.value)) },
|
|
342
|
+
}),
|
|
343
|
+
h('span', { className: 'ck_val' }, fmtK(props.tokens)),
|
|
344
|
+
h('span', { className: 'ck_meta' }, pct(props.value) + ' ' + props.ofWindow))
|
|
146
345
|
}
|
|
147
|
-
const pct = function (ratio) { return Math.round(Number(ratio) * 1000) / 10 + '%' }
|
|
148
346
|
|
|
149
347
|
function Panel(props) {
|
|
150
348
|
const t = props.t
|
|
@@ -156,6 +354,10 @@ window.__ModuleLoader__.load({
|
|
|
156
354
|
const [message, setMessage] = React.useState('')
|
|
157
355
|
const [error, setError] = React.useState('')
|
|
158
356
|
const [pre, setPre] = React.useState('')
|
|
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)
|
|
159
361
|
|
|
160
362
|
const load = React.useCallback(function () {
|
|
161
363
|
return api.status().then(function (next) {
|
|
@@ -164,11 +366,26 @@ window.__ModuleLoader__.load({
|
|
|
164
366
|
setThreshold(Number(next.knobs.thresholdRatio) || 0.5)
|
|
165
367
|
setRetain(Number(next.knobs.retainRatio) || 0.16)
|
|
166
368
|
}
|
|
167
|
-
}).catch(function (cause) {
|
|
369
|
+
}).catch(function (cause) {
|
|
370
|
+
setError(String(cause && cause.message ? cause.message : cause))
|
|
371
|
+
})
|
|
168
372
|
}, [api])
|
|
169
373
|
|
|
170
374
|
React.useEffect(function () { load() }, [load])
|
|
171
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
|
+
|
|
172
389
|
const run = function (label, task) {
|
|
173
390
|
setBusy(true)
|
|
174
391
|
setError('')
|
|
@@ -182,10 +399,9 @@ window.__ModuleLoader__.load({
|
|
|
182
399
|
}
|
|
183
400
|
|
|
184
401
|
const presetId = state && state.presets && state.presets.length ? state.presets[0] : 'cordis'
|
|
185
|
-
const
|
|
186
|
-
const triggerTokens = Math.floor(
|
|
187
|
-
const retainTokens = Math.floor(
|
|
188
|
-
|
|
402
|
+
const windowSize = state && state.triggers ? state.triggers.contextWindow : 1048576
|
|
403
|
+
const triggerTokens = Math.floor(windowSize * threshold)
|
|
404
|
+
const retainTokens = Math.floor(windowSize * retain)
|
|
189
405
|
const entry = state && state.entries ? state.entries[0] : null
|
|
190
406
|
const stateText = !entry
|
|
191
407
|
? t('missing')
|
|
@@ -195,83 +411,93 @@ window.__ModuleLoader__.load({
|
|
|
195
411
|
? t('upToDate') + ' · ' + entry.upstreamSha
|
|
196
412
|
: t('missing')
|
|
197
413
|
|
|
414
|
+
const configCard = h('div', { className: 'ck_card' },
|
|
415
|
+
h(SliderRow, {
|
|
416
|
+
label: t('threshold'), min: 0.2, max: 0.85, value: threshold,
|
|
417
|
+
tokens: triggerTokens, ofWindow: t('ofWindow'), disabled: busy,
|
|
418
|
+
onChange: setThreshold,
|
|
419
|
+
}),
|
|
420
|
+
h(SliderRow, {
|
|
421
|
+
label: t('retain'), min: 0.04, max: Math.max(0.05, threshold - 0.02), value: retain,
|
|
422
|
+
tokens: retainTokens, ofWindow: t('ofWindow'), disabled: busy,
|
|
423
|
+
onChange: setRetain,
|
|
424
|
+
}),
|
|
425
|
+
meta(t('triggers') + ' ' + fmtK(triggerTokens) + ' · ' + t('keeps') + ' ' + fmtK(retainTokens)),
|
|
426
|
+
h('div', { className: 'ck_btns' },
|
|
427
|
+
h('button', {
|
|
428
|
+
className: 'ck_btn primary', type: 'button', disabled: busy,
|
|
429
|
+
onClick: function () {
|
|
430
|
+
return run(t('saved'), function () {
|
|
431
|
+
return api.save({ presetId: presetId, thresholdRatio: threshold, retainRatio: retain, auto: true, group: 'compaction' }).then(load)
|
|
432
|
+
})
|
|
433
|
+
},
|
|
434
|
+
}, t('save')),
|
|
435
|
+
h('button', {
|
|
436
|
+
className: 'ck_btn', type: 'button', disabled: busy,
|
|
437
|
+
onClick: function () {
|
|
438
|
+
return run('', function () {
|
|
439
|
+
return api.sync().then(function (result) {
|
|
440
|
+
setMessage(result && result.changed ? t('synced') : t('unchanged'))
|
|
441
|
+
return load()
|
|
442
|
+
})
|
|
443
|
+
})
|
|
444
|
+
},
|
|
445
|
+
}, t('sync')),
|
|
446
|
+
h('button', {
|
|
447
|
+
className: 'ck_btn', type: 'button', disabled: busy,
|
|
448
|
+
onClick: function () {
|
|
449
|
+
return run('', function () {
|
|
450
|
+
return api.preview().then(function (result) { setPre(result && result.text ? result.text : '') })
|
|
451
|
+
})
|
|
452
|
+
},
|
|
453
|
+
}, t('preview')),
|
|
454
|
+
h('button', {
|
|
455
|
+
className: 'ck_btn', type: 'button', disabled: busy,
|
|
456
|
+
onClick: function () {
|
|
457
|
+
return run(t('cleared'), function () { return api.clear().then(load) })
|
|
458
|
+
},
|
|
459
|
+
}, t('clear')),
|
|
460
|
+
h('button', {
|
|
461
|
+
className: 'ck_btn', type: 'button', disabled: busy,
|
|
462
|
+
onClick: function () { return load() },
|
|
463
|
+
}, t('refresh'))))
|
|
464
|
+
|
|
465
|
+
const infoRows = []
|
|
466
|
+
if (state) infoRows.push(h('div', { className: 'ck_state' }, stateText))
|
|
467
|
+
if (state && state.foreign && state.foreign.length) infoRows.push(h('div', { className: 'ck_state ck_warn' }, t('foreign')))
|
|
468
|
+
if (message) infoRows.push(meta(message))
|
|
469
|
+
if (error) infoRows.push(h('div', { className: 'ck_meta ck_err' }, t('error') + ': ' + error))
|
|
470
|
+
if (state) infoRows.push(meta(t('profile') + ': ' + state.profileDir + ' · ' + t('patch') + ': ' + state.patchPath))
|
|
471
|
+
if (state) infoRows.push(meta(t('upstream') + ': ' + (state.sources || []).join(' | ')))
|
|
472
|
+
if (state && state.managed && state.managed.length) {
|
|
473
|
+
infoRows.push(meta(state.managed.map(function (block) { return block.presetId + '@' + block.sha }).join(', ')))
|
|
474
|
+
}
|
|
475
|
+
infoRows.push(meta(t('restart')))
|
|
476
|
+
if (pre) infoRows.push(h('pre', { className: 'ck_pre' }, pre))
|
|
477
|
+
|
|
198
478
|
return h('div', { className: 'ck_wrap' },
|
|
199
479
|
h('div', { className: 'ck_head' },
|
|
200
480
|
h('h2', null, t('title')),
|
|
201
481
|
h('p', null, t('subtitle'))),
|
|
202
|
-
|
|
203
|
-
h(
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
})
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
h('span', { className: 'ck_meta' }, pct(retain) + ' ' + t('ofWindow'))),
|
|
222
|
-
h('div', { className: 'ck_meta' },
|
|
223
|
-
t('triggers') + ' ' + fmtK(triggerTokens) + ' · ' + t('keeps') + ' ' + fmtK(retainTokens)),
|
|
224
|
-
h('div', { className: 'ck_btns' },
|
|
225
|
-
h('button', {
|
|
226
|
-
className: 'ck_btn primary', type: 'button', disabled: busy,
|
|
227
|
-
onClick: function () {
|
|
228
|
-
return run(t('saved'), function () {
|
|
229
|
-
return api.save({ presetId: presetId, thresholdRatio: threshold, retainRatio: retain, auto: true, group: 'compaction' })
|
|
230
|
-
.then(load)
|
|
231
|
-
})
|
|
232
|
-
},
|
|
233
|
-
}, t('save')),
|
|
234
|
-
h('button', {
|
|
235
|
-
className: 'ck_btn', type: 'button', disabled: busy,
|
|
236
|
-
onClick: function () {
|
|
237
|
-
return run('', function () {
|
|
238
|
-
return api.sync().then(function (result) {
|
|
239
|
-
setMessage(result && result.changed ? t('synced') : t('unchanged'))
|
|
240
|
-
return load()
|
|
241
|
-
})
|
|
242
|
-
})
|
|
243
|
-
},
|
|
244
|
-
}, t('sync')),
|
|
245
|
-
h('button', {
|
|
246
|
-
className: 'ck_btn', type: 'button', disabled: busy,
|
|
247
|
-
onClick: function () {
|
|
248
|
-
return run('', function () {
|
|
249
|
-
return api.preview().then(function (result) { setPre(result && result.text ? result.text : '') })
|
|
250
|
-
})
|
|
251
|
-
},
|
|
252
|
-
}, t('preview')),
|
|
253
|
-
h('button', {
|
|
254
|
-
className: 'ck_btn', type: 'button', disabled: busy,
|
|
255
|
-
onClick: function () {
|
|
256
|
-
return run(t('cleared'), function () { return api.clear().then(load) })
|
|
257
|
-
},
|
|
258
|
-
}, t('clear')),
|
|
259
|
-
h('button', {
|
|
260
|
-
className: 'ck_btn', type: 'button', disabled: busy,
|
|
261
|
-
onClick: function () { return load() },
|
|
262
|
-
}, t('refresh')))),
|
|
263
|
-
|
|
264
|
-
state ? h('div', { className: 'ck_state' }, stateText) : null,
|
|
265
|
-
state && state.foreign && state.foreign.length ? h('div', { className: 'ck_state ck_warn' }, t('foreign')) : null,
|
|
266
|
-
message ? h('div', { className: 'ck_meta' }, message) : null,
|
|
267
|
-
error ? h('div', { className: 'ck_meta ck_err' }, t('error') + ': ' + error) : null,
|
|
268
|
-
state ? h('div', { className: 'ck_meta' }, t('profile') + ': ' + state.profileDir + ' · ' + t('patch') + ': ' + state.patchPath) : null,
|
|
269
|
-
state ? h('div', { className: 'ck_meta' }, t('upstream') + ': ' + (state.sources || []).join(' | ')) : null,
|
|
270
|
-
state && state.managed && state.managed.length
|
|
271
|
-
? h('div', { className: 'ck_meta' }, state.managed.map(function (block) { return block.presetId + '@' + block.sha }).join(', '))
|
|
272
|
-
: null,
|
|
273
|
-
h('div', { className: 'ck_meta' }, t('restart')),
|
|
274
|
-
pre ? h('pre', { className: 'ck_pre' }, pre) : null)
|
|
482
|
+
configCard,
|
|
483
|
+
h(EffectCard, {
|
|
484
|
+
t: t,
|
|
485
|
+
report: report,
|
|
486
|
+
busy: busy,
|
|
487
|
+
onRun: function () {
|
|
488
|
+
return run('', function () {
|
|
489
|
+
return api.report(7).then(function (next) { setReport(next) })
|
|
490
|
+
})
|
|
491
|
+
},
|
|
492
|
+
}),
|
|
493
|
+
h(ToolSavingsCard, {
|
|
494
|
+
t: t,
|
|
495
|
+
data: toolSavings,
|
|
496
|
+
linked: toolLinked,
|
|
497
|
+
busy: toolBusy,
|
|
498
|
+
onRun: loadTool,
|
|
499
|
+
}),
|
|
500
|
+
...infoRows)
|
|
275
501
|
}
|
|
276
502
|
|
|
277
503
|
return {
|
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/kit-ops.mjs
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { existsSync, readFileSync } from 'node:fs'
|
|
10
|
+
import { homedir } from 'node:os'
|
|
10
11
|
import { join } from 'node:path'
|
|
11
12
|
|
|
12
13
|
import { commitSync, planSync } from './tuner/sync.mjs'
|
|
@@ -20,6 +21,7 @@ import {
|
|
|
20
21
|
PATCH_FILE,
|
|
21
22
|
} from './tuner/tuner.mjs'
|
|
22
23
|
import { readSettings, setCompaction, writeSettings } from './settings.mjs'
|
|
24
|
+
import { loadReportAsync } from './usage.mjs'
|
|
23
25
|
|
|
24
26
|
const KNOB_DEFAULTS = { auto: true, thresholdRatio: 0.5, retainRatio: 0.16 }
|
|
25
27
|
const CONTEXT_DEFAULTS = { contextWindow: 1048576, maxOutputTokens: 256000, headroomTokens: 65536 }
|
|
@@ -111,8 +113,9 @@ export function createKit(config = {}) {
|
|
|
111
113
|
|
|
112
114
|
/** settings.json 优先于插件 config:面板保存后不必重启就能被工具/CLI 看到。 */
|
|
113
115
|
const currentSettings = () => {
|
|
114
|
-
|
|
115
|
-
const
|
|
116
|
+
// 只读 settings.json,不解析 app.asar:CLI 的 report / set 在 app 之外也要能跑
|
|
117
|
+
const profile = profileOf()
|
|
118
|
+
const stored = readSettings(profile.dir)
|
|
116
119
|
const merged = { ...settings, presetTweaks: { ...settings.presetTweaks } }
|
|
117
120
|
for (const [presetId, value] of Object.entries(stored.presets ?? {})) {
|
|
118
121
|
merged.presetTweaks[presetId] = {
|
|
@@ -196,6 +199,26 @@ export function createKit(config = {}) {
|
|
|
196
199
|
return { ...result, removed: managed.length, restartRequired: result.changed }
|
|
197
200
|
},
|
|
198
201
|
|
|
202
|
+
/**
|
|
203
|
+
* 效果台账:读本机会话日志统计真实用量,并回放“当前阈值 vs 默认阈值”的节省估算。
|
|
204
|
+
* 只读本地文件;缓存放在 profile 目录下,第二次调用不再重解压。
|
|
205
|
+
*/
|
|
206
|
+
async report({ days = 7 } = {}) {
|
|
207
|
+
const profile = profileOf()
|
|
208
|
+
const active = currentSettings()
|
|
209
|
+
const focus = Object.keys(active.presetTweaks)[0] ?? 'cordis'
|
|
210
|
+
const knob = active.presetTweaks[focus]?.plugins?.['compaction-basic']?.config ?? {}
|
|
211
|
+
const home = text(process.env.DSH_HOME) || join(homedir(), '.dsh')
|
|
212
|
+
return loadReportAsync({
|
|
213
|
+
home,
|
|
214
|
+
cachePath: join(profile.dir, 'dsh-context-kit', 'usage-cache.json'),
|
|
215
|
+
days: Math.min(90, Math.max(1, Number(days) || 7)),
|
|
216
|
+
contextWindow: active.context.contextWindow,
|
|
217
|
+
thresholdRatio: finite(knob.thresholdRatio) ?? KNOB_DEFAULTS.thresholdRatio,
|
|
218
|
+
retainRatio: finite(knob.retainRatio) ?? KNOB_DEFAULTS.retainRatio,
|
|
219
|
+
})
|
|
220
|
+
},
|
|
221
|
+
|
|
199
222
|
/** 面板/CLI 保存:只写 settings.json(不写 YAML),返回最新状态。 */
|
|
200
223
|
saveCompaction(input = {}) {
|
|
201
224
|
const profile = profileOf()
|