dsh-token-trim 0.0.0-stage → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 LISHUANGXI1412
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,119 @@
1
- # Temporary Holding Version
1
+ # dsh-token-trim
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Trim tool results **before they enter the model context** — structural summaries, head/tail caps, repeat
4
+ deltas, and an artifact spill you can search back. Ships with a context index and a **measured savings view**
5
+ inside the conversation.
6
+
7
+ > Not a proxy, not a middleware you have to trust: every rewrite is reproducible, the original is always on
8
+ > disk, and the model can read it back with `guard_query`.
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ dsh plugin add dsh-token-trim
14
+ ```
15
+
16
+ Restart DSH. You get:
17
+
18
+ - `token-trim` (host): rewrites tool results on `tools/post-execute`;
19
+ - `token-trim-index` (host): a small system-prompt section listing recent artifacts and touched paths;
20
+ - **Savings** view tab next to Chat / Trajectory / Stats, plus the `guard_query` tool.
21
+
22
+ ## What it does
23
+
24
+ | rule | applies to | behaviour |
25
+ | --- | --- | --- |
26
+ | **structural summary** | JSON-shaped results from `cordis_inspect_*`, `plugin_manager`, `grep`, `skill`, `list_agents`, CUA window dumps | keeps the shape (keys, counts, ids), replaces verbose bodies with pointers |
27
+ | **head/tail cap** | long text from `pwsh`, `job_output`, … | keeps the first 72% and last 28% of the cap, inserts an artifact pointer + line count |
28
+ | **repeat delta** | any tool in `deltaTools` | if the same tool+args runs again and the diff is < 60% of the full text, send only the diff |
29
+ | **unchanged pointer** | same as above | sends `[unchanged · same as #N · artifact <id>]` |
30
+
31
+ Everything else is left alone: `edit`, `write`, `todo`, sub-agent messages, images, multi-block results,
32
+ reasoning, and any result under `minChars` (default 2000).
33
+
34
+ ## Measured on the author's machine
35
+
36
+ 19 days of real sessions, 279 rewrites, not a benchmark script:
37
+
38
+ | metric | value |
39
+ | --- | --- |
40
+ | characters rewritten | 3,493,915 → 876,875 (**−74.9%**) |
41
+ | tokens not re-sent | ≈ 800K |
42
+ | tools affected | `pwsh` 163, `cordis_inspect_query` 58, `plugin_manager` 20, `grep` 9, `read` 14, … |
43
+ | bill estimate (compaction tuning × this layer) | **−41.4%** (offline replay) |
44
+ | suspected rework deducted | 16 reruns, **−0.13%** |
45
+ | **net bill estimate** | **−41.3%** |
46
+
47
+ Read those two numbers carefully:
48
+
49
+ - **−74.9%** is the character cut **only over the results this plugin actually rewrote**. It is not the bill:
50
+ small results, the system prompt, tool definitions and reasoning are outside the hook.
51
+ - **−41.4%** is a bill-level model: earlier compaction (−31%) composed with this rule layer (−15% of the bill,
52
+ measured by replaying 1,518 real tool results), minus rework. It is a replay estimate, not an invoice.
53
+ - The Savings tab shows the same split: gross, suspected-rework deduction, net — so you can see when the
54
+ plugin starts to hurt instead of help.
55
+
56
+ ## Safety model
57
+
58
+ 1. **Fail open.** Any exception, any unexpected shape, any non-text result → the original result passes
59
+ through untouched. A bug cannot block a tool.
60
+ 2. **Single text block only.** Images, structured values and multi-block results are never rewritten.
61
+ 3. **Originals are always written first**, to `~/.dsh/token-trim/artifacts/<sha16>.txt`, and the replacement
62
+ carries that id.
63
+ 4. **`guard_query(id, pattern)`** searches inside the original (substring or `/regex/`, with context lines).
64
+ An artifact can be fetched **twice**; after that the tool returns the full text capped at 12K chars —
65
+ because a third fetch costs more context than it saves.
66
+ 5. **Audit trail.** Every transformation appends one line to `~/.dsh/token-trim/manifest.jsonl`
67
+ (tool, mode, chars in/out, artifact id, session id).
68
+ 6. **State is per agent.** Repeat-delta and unchanged detection are keyed by `session + tool + args`, so a
69
+ sub-agent never receives "unchanged" for a result it never saw.
70
+
71
+ ## What we tried and rejected (kept, so you don't repeat it)
72
+
73
+ - **Truncating `read` results.** Two real sessions, both truncated `read`s triggered a follow-up
74
+ `offset/limit` read: saved 2.3K tokens, cost ~50K in extra steps. `read` is now off by default
75
+ (`caps.read: 0`).
76
+ - **LLM summarization of logs.** Gold-recall 77.2% against the required 93.8%. Summaries lose the one line
77
+ you need. Logs are capped, never summarized.
78
+ - **Sending "unchanged" across agents** — a real bug, fixed by the agent key above.
79
+ - **Model-based trimming (a local 4B).** Projected 3–6 points of bill for a resident 4B service; shelved.
80
+
81
+ ## Configuration
82
+
83
+ Patch row `token-trim` in the DSH profile:
84
+
85
+ ```yaml
86
+ - id: token-trim
87
+ name: dsh-token-trim
88
+ config:
89
+ enabled: true
90
+ dryRun: false # record what would happen, rewrite nothing
91
+ minChars: 2000
92
+ maxFetches: 2
93
+ caps:
94
+ pwsh: 4000
95
+ cordis_inspect_query: 12000
96
+ read: 0 # 0 disables a cap
97
+ ```
98
+
99
+ `dryRun: true` is the honest way to try it: the Savings tab fills up, the context does not change.
100
+
101
+ ## Savings view
102
+
103
+ The **Savings** tab (next to Chat / Trajectory / Stats) shows, per window and per session:
104
+
105
+ - net bill estimate with the rework deduction spelled out;
106
+ - measured usage read from `dsh-context-kit`'s session cache when that plugin is installed
107
+ (hit ×1 / miss ×10 / output ×15);
108
+ - this plugin's own char cut, per tool and per session;
109
+ - the last transformations with artifact ids.
110
+
111
+ ## Companion package
112
+
113
+ `dsh-token-trim` owns the runtime savings: tool trimming, the context index, the view.
114
+ **`dsh-context-kit`** separately owns compaction-threshold tuning (Settings → Context). Install both for the
115
+ full effect; either works alone.
116
+
117
+ ## License
118
+
119
+ MIT
@@ -0,0 +1,468 @@
1
+ // dsh-token-trim 的会话视图(Web 客户端插件)。
2
+ // 位置:对话上方的 View 标签行(conversation.view),与「对话 / 轨迹 / 统计」并列。
3
+ // 数据经 ctx.connection.rpc 调宿主 tokenTrim 服务(方法无参数,筛选在客户端)。
4
+ window.__ModuleLoader__.load({
5
+ id: 'dsh-token-trim',
6
+ factory(require) {
7
+ const React = require('react')
8
+ const h = React.createElement
9
+
10
+ const EN = {
11
+ label: 'Savings',
12
+ title: 'Tool savings',
13
+ subtitle: 'What token-trim removed before tool results entered the model context.',
14
+ here: 'This session',
15
+ all: 'All sessions',
16
+ allTime: 'All time',
17
+ days7: '7 days',
18
+ days30: '30 days',
19
+ refresh: 'Refresh',
20
+ busy: 'Loading…',
21
+ error: 'Error',
22
+ events: 'results rewritten',
23
+ chars: 'characters',
24
+ saved: 'saved',
25
+ tokens: 'est. tokens',
26
+ denom: 'The percentage below is measured only over the tool results token-trim actually rewrote. Small results, the system prompt, and reasoning are outside the hook.',
27
+ billNote: 'Bill-level estimate (offline replay, 2026-11-15): compaction tuning ≈ −31%, this rule layer ≈ −15%, multiplied ≈ −41%. Modelled, no rework cost.',
28
+ billTitle: 'Bill saved (estimate)',
29
+ netTitle: 'Bill saved (net estimate)',
30
+ gross: 'gross',
31
+ rework: 'rework',
32
+ reworkTitle: 'Rework (suspected)',
33
+ reworkSignal: 'repeats',
34
+ reworkCost: 'cost (hit-equivalents)',
35
+ reworkNote: 'Same rewritten result re-run within 30 minutes in one session, charged at the average step cost. This is the deduction applied to the headline.',
36
+ reworkDuplicates: 'Bill-wide duplicate calls (not attributed):',
37
+ billMeasured: 'Measured usage',
38
+ billMeasuredNote: 'Read from the dsh-context-kit session-log cache: hit ×1, miss ×10, output ×15 (DeepSeek price ratio). This is the real usage side, not a projection.',
39
+ billSessions: 'sessions',
40
+ billSteps: 'steps',
41
+ billCompactions: 'compactions',
42
+ billHit: 'cache hit',
43
+ billMiss: 'miss',
44
+ billOut: 'output',
45
+ billCost: 'cost (hit-equivalents)',
46
+ billUnavailable: 'No measured bill yet — install dsh-context-kit so its session-log cache exists, then this card fills itself.',
47
+ billUpdated: 'cache updated',
48
+ pluginsTitle: 'This plugin, measured',
49
+ tools: 'By tool',
50
+ sessions: 'By session',
51
+ recent: 'Recent transformations',
52
+ count: 'n',
53
+ cut: 'cut',
54
+ last: 'last seen',
55
+ legacy: 'Before session tracking',
56
+ empty: 'Nothing rewritten yet in this session. token-trim only records results it actually changed.',
57
+ none: 'No manifest yet — the plugin is idle, or dryRun never wrote anything.',
58
+ dry: 'dryRun is ON: nothing is being rewritten, so these numbers are hypothetical.',
59
+ where: 'Originals',
60
+ tool: 'tool',
61
+ mode: 'mode',
62
+ focus: 'Focus',
63
+ allFocus: 'clear',
64
+ }
65
+
66
+ const ZH = {
67
+ label: '节省',
68
+ title: '工具节省',
69
+ subtitle: 'token-trim 在工具结果进入模型上下文之前,替我们砍掉了什么。',
70
+ here: '本会话',
71
+ all: '全部会话',
72
+ allTime: '全部',
73
+ days7: '7 天',
74
+ days30: '30 天',
75
+ refresh: '刷新',
76
+ busy: '读取中…',
77
+ error: '出错',
78
+ events: '条结果被改写',
79
+ chars: '字符',
80
+ saved: '省下',
81
+ tokens: '约合 token',
82
+ denom: '下面的百分比,分母只是「token-trim 实际改写过的工具结果」。小结果、系统提示、推理都不在钩子里。',
83
+ billNote: '账单级估算(离线回放,2026-11-15):压缩阈值调优约 −31%,本规则层约 −15%,相乘约 −41%。是模型,未扣返工。',
84
+ billTitle: '账单节省(估算)',
85
+ netTitle: '账单净节省(估算)',
86
+ gross: '毛节省',
87
+ rework: '返工扣减',
88
+ reworkTitle: '返工(疑似)',
89
+ reworkSignal: '次重跑',
90
+ reworkCost: '成本(命中当量)',
91
+ reworkNote: '同一会话里,同一条被改写的结果在 30 分钟内又被重跑,按平均步成本计费。这就是上面大字里扣掉的那部分。',
92
+ reworkDuplicates: '全账单重复调用(未归因):',
93
+ billMeasured: '实测用量',
94
+ billMeasuredNote: '来自 dsh-context-kit 的会话日志缓存:命中 ×1、未命中 ×10、输出 ×15(DeepSeek 价格比)。这是真实用量侧,不是推算。',
95
+ billSessions: '个会话',
96
+ billSteps: '步',
97
+ billCompactions: '次压缩',
98
+ billHit: '缓存命中',
99
+ billMiss: '未命中',
100
+ billOut: '输出',
101
+ billCost: '成本(命中当量)',
102
+ billUnavailable: '还没有实测账单 —— 装上 dsh-context-kit 生成会话日志缓存后,这张卡会自动填上。',
103
+ billUpdated: '缓存更新于',
104
+ pluginsTitle: '本插件实测',
105
+ tools: '按工具',
106
+ sessions: '按会话',
107
+ recent: '最近变换',
108
+ count: '次数',
109
+ cut: '削减',
110
+ last: '最近',
111
+ legacy: '会话统计上线前',
112
+ empty: '本会话还没有被改写的工具结果。token-trim 只记录真正改过的结果。',
113
+ none: '还没有 manifest —— 插件空闲,或 dryRun 从未写过盘。',
114
+ dry: 'dryRun 已开启:内容没有被改写,这些数字只是“如果真改会怎样”。',
115
+ where: '原文位置',
116
+ tool: '工具',
117
+ mode: '模式',
118
+ focus: '聚焦',
119
+ allFocus: '取消',
120
+ }
121
+
122
+ const CSS = [
123
+ '.tg_view { display: flex; flex-direction: column; gap: 14px; padding: 18px 22px 40px; max-width: 960px; box-sizing: border-box; }',
124
+ '.tg_head h2 { margin: 0 0 4px; font-size: 16px; }',
125
+ '.tg_head p { margin: 0; opacity: .7; font-size: 12px; }',
126
+ '.tg_row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }',
127
+ '.tg_card { border: 1px solid rgba(127,127,127,.25); border-radius: 10px; padding: 12px; display: flex; flex-direction: column; gap: 10px; }',
128
+ '.tg_cardhead { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }',
129
+ '.tg_cardhead strong { font-size: 13px; }',
130
+ '.tg_seg { display: inline-flex; border: 1px solid rgba(127,127,127,.35); border-radius: 8px; overflow: hidden; }',
131
+ '.tg_seg button { border: 0; background: transparent; color: inherit; padding: 4px 10px; font-size: 12px; cursor: pointer; }',
132
+ '.tg_seg button.on { background: rgba(37,99,235,.18); font-weight: 600; }',
133
+ '.tg_btn { border: 1px solid rgba(127,127,127,.35); background: transparent; color: inherit; border-radius: 8px; padding: 4px 10px; cursor: pointer; font-size: 12px; }',
134
+ '.tg_btn:disabled { opacity: .5; cursor: default; }',
135
+ '.tg_big { font-size: 24px; font-weight: 600; font-variant-numeric: tabular-nums; }',
136
+ '.tg_meta { font-size: 11px; opacity: .65; }',
137
+ '.tg_note { font-size: 11px; opacity: .6; line-height: 1.5; }',
138
+ '.tg_warn { font-size: 12px; color: #d97706; }',
139
+ '.tg_err { font-size: 12px; color: #dc2626; }',
140
+ '.tg_table { width: 100%; border-collapse: collapse; font-size: 12px; }',
141
+ '.tg_table th, .tg_table td { text-align: left; padding: 4px 6px; border-bottom: 1px solid rgba(127,127,127,.18); font-variant-numeric: tabular-nums; }',
142
+ '.tg_table th { font-weight: 600; opacity: .75; }',
143
+ '.tg_table td.num, .tg_table th.num { text-align: right; }',
144
+ '.tg_table tr.clickable { cursor: pointer; }',
145
+ '.tg_table tr.clickable:hover td { background: rgba(127,127,127,.08); }',
146
+ '.tg_table tr.on td { background: rgba(37,99,235,.12); }',
147
+ '.tg_ev { display: flex; gap: 8px; align-items: baseline; font-size: 12px; padding: 3px 0; border-bottom: 1px solid rgba(127,127,127,.12); }',
148
+ '.tg_ev .t { min-width: 74px; opacity: .65; font-variant-numeric: tabular-nums; }',
149
+ '.tg_ev .tg_target { opacity: .8; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1 1 140px; }',
150
+ '.tg_chip { font-size: 10px; padding: 1px 6px; border-radius: 999px; background: rgba(127,127,127,.18); }',
151
+ '.tg_chip.warn { background: rgba(217,119,6,.18); }',
152
+ ].join('\n')
153
+
154
+ const fmtBig = (value) => {
155
+ if (!Number.isFinite(value)) return '—'
156
+ if (value >= 1e9) return (value / 1e9).toFixed(2) + 'B'
157
+ if (value >= 1e6) return (value / 1e6).toFixed(1) + 'M'
158
+ if (value >= 1e3) return Math.round(value / 1e3) + 'K'
159
+ return String(Math.round(value))
160
+ }
161
+ const pct = (value) => (Number.isFinite(value) ? value.toFixed(1) + '%' : '—')
162
+ const timeOf = (ts) => {
163
+ if (!ts) return '—'
164
+ const d = new Date(ts)
165
+ const pad = (n) => String(n).padStart(2, '0')
166
+ return d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate()) + ' ' + pad(d.getHours()) + ':' + pad(d.getMinutes())
167
+ }
168
+ const shortId = (id) => {
169
+ const s = String(id || '')
170
+ if (s === 'legacy') return s
171
+ return s.length > 10 ? s.slice(0, 8) : s
172
+ }
173
+ const meta = (value) => h('div', { className: 'tg_meta' }, value)
174
+ const TOK_PER_CHAR = 0.306
175
+
176
+ const cache = { at: 0, data: null, loading: null }
177
+ function loadReport(api, force) {
178
+ if (!force && cache.data && Date.now() - cache.at < 15000) return Promise.resolve(cache.data)
179
+ if (cache.loading) return cache.loading
180
+ cache.loading = api.report()
181
+ .then(function (data) { cache.at = Date.now(); cache.data = data; cache.loading = null; return data })
182
+ .catch(function (cause) { cache.loading = null; throw cause })
183
+ return cache.loading
184
+ }
185
+
186
+ function makeT(ctx) {
187
+ try {
188
+ ctx.locale.register('token-trim', { en: EN, zh: ZH })
189
+ } catch {}
190
+ let bound
191
+ try {
192
+ bound = ctx.locale.bind('token-trim')
193
+ } catch {}
194
+ return function t(key, fallback) {
195
+ try {
196
+ if (typeof bound === 'function') {
197
+ const value = bound(key)
198
+ if (typeof value === 'string' && value) return value
199
+ } else if (bound && typeof bound[key] === 'string') {
200
+ return bound[key]
201
+ }
202
+ } catch {}
203
+ let id = 'en'
204
+ try {
205
+ id = ctx.locale.getLocale().id
206
+ } catch {}
207
+ const dict = id === 'zh' ? ZH : EN
208
+ return dict[key] || fallback || key
209
+ }
210
+ }
211
+
212
+ function makeApi(ctx) {
213
+ function call(method, args) {
214
+ return ctx.connection.rpc.call('/api', 'tokenTrim/' + method, { args: args || {} }, undefined).then(function (response) {
215
+ if (response && response.ok) return response.value
216
+ const message = response && response.error ? response.error.message : 'token guard remote call failed'
217
+ const error = new Error(message)
218
+ error.code = response && response.error ? response.error.code : 'REMOTE_FAILED'
219
+ throw error
220
+ })
221
+ }
222
+ return {
223
+ report: () => call('report', {}),
224
+ status: () => call('status', {}),
225
+ bill: () => call('bill', {}),
226
+ }
227
+ }
228
+
229
+ function viewModel(data, focus, days) {
230
+ const cutoff = days > 0 ? Date.now() - days * 86400000 : 0
231
+ const sessions = (data.sessions || []).filter(function (s) { return !cutoff || s.last >= cutoff })
232
+ const selected = focus ? sessions.find(function (s) { return s.id === focus }) || null : null
233
+ const pool = selected ? [selected] : sessions
234
+ const totals = { events: 0, orig: 0, vis: 0, saved: 0 }
235
+ const toolMap = new Map()
236
+ for (const s of pool) {
237
+ totals.events += s.events
238
+ totals.orig += s.orig
239
+ totals.vis += s.vis
240
+ totals.saved += s.saved
241
+ for (const tool of s.tools || []) {
242
+ const cur = toolMap.get(tool.name) || { name: tool.name, events: 0, saved: 0 }
243
+ cur.events += tool.events
244
+ cur.saved += tool.saved
245
+ toolMap.set(tool.name, cur)
246
+ }
247
+ }
248
+ totals.cutPct = totals.orig ? Number(((1 - totals.vis / totals.orig) * 100).toFixed(1)) : 0
249
+ totals.tokens = Math.round(totals.saved * TOK_PER_CHAR)
250
+ const events = (data.events || []).filter(function (e) {
251
+ if (cutoff && e.ts < cutoff) return false
252
+ return selected ? e.session === selected.id : true
253
+ })
254
+ return {
255
+ sessions,
256
+ selected,
257
+ totals,
258
+ tools: [...toolMap.values()].sort(function (a, b) { return b.saved - a.saved }),
259
+ events,
260
+ }
261
+ }
262
+
263
+ function Seg(props) {
264
+ const options = [
265
+ { value: 0, label: props.t('allTime') },
266
+ { value: 7, label: props.t('days7') },
267
+ { value: 30, label: props.t('days30') },
268
+ ]
269
+ return h('div', { className: 'tg_seg' }, options.map((opt) =>
270
+ h('button', {
271
+ key: opt.value,
272
+ type: 'button',
273
+ className: props.value === opt.value ? 'on' : '',
274
+ onClick: function () { props.onChange(opt.value) },
275
+ }, opt.label),
276
+ ))
277
+ }
278
+
279
+ function Panel(props) {
280
+ const t = props.t
281
+ const api = props.api
282
+ const sessionId = props.sessionId || ''
283
+ const [data, setData] = React.useState(null)
284
+ const [bill, setBill] = React.useState(null)
285
+ const [days, setDays] = React.useState(0)
286
+ const [focus, setFocus] = React.useState('')
287
+ const [busy, setBusy] = React.useState(false)
288
+ const [error, setError] = React.useState('')
289
+ const [status, setStatus] = React.useState(null)
290
+
291
+ const load = React.useCallback(function (force) {
292
+ setBusy(true)
293
+ setError('')
294
+ return Promise.all([loadReport(api, force), api.status().catch(function () { return null }), api.bill().catch(function () { return null })])
295
+ .then(function (results) { setData(results[0]); if (results[1]) setStatus(results[1]); if (results[2]) setBill(results[2]) })
296
+ .catch(function (cause) { setError(String(cause && cause.message ? cause.message : cause)) })
297
+ .then(function () { setBusy(false) })
298
+ }, [api])
299
+
300
+ React.useEffect(function () { load(false) }, [load])
301
+
302
+ const model = data ? viewModel(data, focus, days) : null
303
+ const rows = []
304
+
305
+ if (error) rows.push(h('div', { className: 'tg_err' }, t('error') + ': ' + error))
306
+ if (status && status.dryRun) rows.push(h('div', { className: 'tg_warn' }, t('dry')))
307
+ if (data && !data.available) rows.push(meta(t('none')))
308
+
309
+ if (model) {
310
+ const scope = focus ? shortId(focus) : t('all')
311
+ const totalPct = bill && Number.isFinite(bill.totalPct) ? bill.totalPct : 41.4
312
+ const compactionPct = bill && Number.isFinite(bill.compactionPct) ? bill.compactionPct : 31
313
+ const rulesPct = bill && Number.isFinite(bill.rulesPct) ? bill.rulesPct : 15
314
+
315
+ // 1) 账单净节省(毛节省 − 疑似返工)
316
+ const netPct = bill && Number.isFinite(bill.netPct) ? bill.netPct : totalPct
317
+ const reworkPct = bill && bill.rework && Number.isFinite(bill.rework.pctOfBaseline) ? bill.rework.pctOfBaseline : 0
318
+ rows.push(h('div', { className: 'tg_card' },
319
+ h('div', { className: 'tg_row' },
320
+ h('span', { className: 'tg_big' }, '−' + netPct.toFixed(1) + '%'),
321
+ h('span', { className: 'tg_meta' }, t('netTitle'))),
322
+ h('div', { className: 'tg_row' },
323
+ meta(t('gross') + ' −' + totalPct.toFixed(1) + '%'),
324
+ meta(t('rework') + ' −' + reworkPct.toFixed(2) + '%'),
325
+ meta('压缩阈值 −' + compactionPct + '% × 规则层 −' + rulesPct + '%')),
326
+ h('div', { className: 'tg_note' }, t('billNote'))))
327
+
328
+ // 2) 实测用量(来自会话日志缓存)
329
+ if (bill && bill.available && bill.totals) {
330
+ const bt = bill.totals
331
+ rows.push(h('div', { className: 'tg_card' },
332
+ h('div', { className: 'tg_cardhead' }, h('strong', null, t('billMeasured'))),
333
+ h('div', { className: 'tg_row' },
334
+ meta(bt.sessions + ' ' + t('billSessions') + ' · ' + bt.steps + ' ' + t('billSteps') + ' · ' + bt.compactions + ' ' + t('billCompactions')),
335
+ meta(t('billCost') + ' ' + fmtBig(bt.cost))),
336
+ h('div', { className: 'tg_row' },
337
+ meta(t('billHit') + ' ' + fmtBig(bt.hit)),
338
+ meta(t('billMiss') + ' ' + fmtBig(bt.miss)),
339
+ meta(t('billOut') + ' ' + fmtBig(bt.out))),
340
+ h('div', { className: 'tg_note' }, t('billMeasuredNote')),
341
+ bill.updatedAt ? meta(t('billUpdated') + ' ' + timeOf(bill.updatedAt)) : null))
342
+ } else {
343
+ rows.push(h('div', { className: 'tg_card' },
344
+ h('div', { className: 'tg_cardhead' }, h('strong', null, t('billMeasured'))),
345
+ meta(t('billUnavailable'))))
346
+ }
347
+
348
+ // 3) 返工(疑似),计入上方扣减
349
+ if (bill && bill.rework) {
350
+ const rw = bill.rework
351
+ rows.push(h('div', { className: 'tg_card' },
352
+ h('div', { className: 'tg_cardhead' }, h('strong', null, t('reworkTitle'))),
353
+ h('div', { className: 'tg_row' },
354
+ meta(rw.suspected + ' ' + t('reworkSignal') + ' · ' + rw.windowMinutes + ' min'),
355
+ meta(t('reworkCost') + ' ' + fmtBig(rw.costEquiv)),
356
+ meta('−' + Number(rw.pctOfBaseline || 0).toFixed(2) + '%')),
357
+ h('div', { className: 'tg_note' }, t('reworkNote')),
358
+ bill.totals ? meta(t('reworkDuplicates') + ' ' + fmtBig(bill.totals.duplicates) + ' / ' + fmtBig(bill.totals.distinctCalls)) : null))
359
+ }
360
+
361
+ // 4) 本插件实测(字符口径)
362
+ rows.push(h('div', { className: 'tg_card' },
363
+ h('div', { className: 'tg_cardhead' }, h('strong', null, t('pluginsTitle'))),
364
+ h('div', { className: 'tg_row' },
365
+ h('span', { className: 'tg_big' }, '−' + pct(model.totals.cutPct)),
366
+ h('span', { className: 'tg_meta' }, scope + ' · ' + model.totals.events + ' ' + t('events')),
367
+ h('span', { className: 'tg_meta' }, fmtBig(model.totals.orig) + ' → ' + fmtBig(model.totals.vis) + ' ' + t('chars'))),
368
+ h('div', { className: 'tg_row' },
369
+ meta(t('saved') + ' ' + fmtBig(model.totals.saved) + ' ' + t('chars')),
370
+ meta(t('tokens') + ' ~' + fmtBig(model.totals.tokens))),
371
+ h('div', { className: 'tg_note' }, t('denom'))))
372
+
373
+ rows.push(h('div', { className: 'tg_card' },
374
+ h('div', { className: 'tg_cardhead' }, h('strong', null, t('tools'))),
375
+ h('table', { className: 'tg_table' },
376
+ h('thead', null, h('tr', null,
377
+ h('th', null, t('tool')),
378
+ h('th', { className: 'num' }, t('count')),
379
+ h('th', { className: 'num' }, t('saved')))),
380
+ h('tbody', null, model.tools.slice(0, 8).map((tool) =>
381
+ h('tr', { key: tool.name },
382
+ h('td', null, tool.name),
383
+ h('td', { className: 'num' }, tool.events),
384
+ h('td', { className: 'num' }, fmtBig(tool.saved))))))))
385
+
386
+ rows.push(h('div', { className: 'tg_card' },
387
+ h('div', { className: 'tg_cardhead' },
388
+ h('strong', null, t('sessions')),
389
+ focus ? h('button', { className: 'tg_btn', type: 'button', onClick: function () { setFocus('') } }, t('allFocus')) : null,
390
+ !focus && sessionId ? h('button', { className: 'tg_btn', type: 'button', onClick: function () { setFocus(sessionId) } }, t('here')) : null),
391
+ h('table', { className: 'tg_table' },
392
+ h('thead', null, h('tr', null,
393
+ h('th', null, ''),
394
+ h('th', { className: 'num' }, t('count')),
395
+ h('th', { className: 'num' }, t('saved')),
396
+ h('th', null, t('last')))),
397
+ h('tbody', null, model.sessions.slice(0, 12).map((row) =>
398
+ h('tr', {
399
+ key: row.id,
400
+ className: 'clickable' + (model.selected && model.selected.id === row.id ? ' on' : ''),
401
+ onClick: function () { setFocus(model.selected && model.selected.id === row.id ? '' : row.id) },
402
+ },
403
+ h('td', null, row.id === 'legacy' ? t('legacy') : shortId(row.id)),
404
+ h('td', { className: 'num' }, row.events),
405
+ h('td', { className: 'num' }, fmtBig(row.saved)),
406
+ h('td', { className: 'tg_meta' }, timeOf(row.last))))))))
407
+
408
+ rows.push(h('div', { className: 'tg_card' },
409
+ h('div', { className: 'tg_cardhead' }, h('strong', null, t('recent'))),
410
+ ...model.events.slice(0, 15).map(function (row, index) {
411
+ return h('div', { className: 'tg_ev', key: index },
412
+ h('span', { className: 't' }, timeOf(row.ts).slice(5)),
413
+ h('span', { className: 'tg_chip' }, row.tool + ' · ' + row.mode),
414
+ h('span', { className: 'tg_meta' }, fmtBig(row.orig) + '→' + fmtBig(row.vis)),
415
+ h('span', { className: 'tg_target' }, row.target || ''),
416
+ row.artifact ? h('span', { className: 'tg_meta' }, row.artifact) : null,
417
+ row.dryRun ? h('span', { className: 'tg_chip warn' }, 'dry') : null)
418
+ })))
419
+
420
+ if (status) {
421
+ const where = []
422
+ if (status.artifactDir) where.push(meta(status.artifactDir))
423
+ if (status.manifest) where.push(meta(status.manifest))
424
+ if (where.length) rows.push(h('div', { className: 'tg_card' },
425
+ h('div', { className: 'tg_cardhead' }, h('strong', null, t('where'))),
426
+ ...where))
427
+ }
428
+ }
429
+
430
+ return h('div', { className: 'tg_view' },
431
+ h('div', { className: 'tg_head' },
432
+ h('h2', null, t('title')),
433
+ h('p', null, t('subtitle'))),
434
+ h('div', { className: 'tg_row' },
435
+ h(Seg, { t: t, value: days, onChange: setDays }),
436
+ h('button', { className: 'tg_btn', type: 'button', disabled: busy, onClick: function () { return load(true) } },
437
+ busy ? t('busy') : t('refresh'))),
438
+ ...rows)
439
+ }
440
+
441
+ return {
442
+ inject: ['slots', 'connection', 'locale'],
443
+ apply(ctx) {
444
+ const style = document.createElement('style')
445
+ style.dataset.plugin = 'dsh-token-trim'
446
+ style.textContent = CSS
447
+ document.head.appendChild(style)
448
+ ctx.effect(function () {
449
+ return function () { style.remove() }
450
+ })
451
+
452
+ const t = makeT(ctx)
453
+ const api = makeApi(ctx)
454
+
455
+ // View 标签行:与「对话 / 轨迹 / 统计」并列
456
+ ctx.slots.inject('conversation.view', function () {
457
+ return ctx.slots.register({
458
+ name: 'conversation.view',
459
+ id: 'token-trim',
460
+ order: 50,
461
+ label: function () { return t('label', 'Savings') },
462
+ inject: function () { return { api: api, t: t } },
463
+ }, Panel)
464
+ })
465
+ },
466
+ }
467
+ },
468
+ })
@@ -0,0 +1,23 @@
1
+ # dsh-token-trim: 一个包两个 Host 入口 + 一个会话视图(视图由 package.json 的 dsh.client 声明)。
2
+ # - token-trim:工具结果进上下文前的确定性治理(摘要 / 截断 / 重跑差异 / 原文落盘 / guard_query 取回)
3
+ # - token-trim-index:上下文索引(在 system prompt 里给一段「有什么、在哪」的指针)
4
+ - insert:
5
+ - id: token-trim
6
+ name: dsh-token-trim
7
+ config:
8
+ enabled: true
9
+ dryRun: false
10
+ minChars: 2000
11
+ maxFetches: 2
12
+ - id: token-trim-index
13
+ name: dsh-token-trim/index-host
14
+ config:
15
+ enabled: true
16
+ injectMode: section
17
+ sectionName: index:index-page
18
+ sectionOrder: 10500
19
+ maxTokens: 1000
20
+ pathLimit: 40
21
+ artifactLimit: 20
22
+ targetChars: 48
23
+ manifestLines: 500
package/icon.svg ADDED
@@ -0,0 +1,6 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
2
+ <path d="M12 3.2 19 6v5.1c0 4.2-2.8 7.3-7 9.7-4.2-2.4-7-5.5-7-9.7V6z"/>
3
+ <path d="M8.6 10.4h6.8"/>
4
+ <path d="M8.6 13.6h4.2"/>
5
+ <path d="m15.4 15.2 1.8-1.6-1.8-1.6" opacity=".75"/>
6
+ </svg>