dsh-context-kit 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 +21 -0
- package/README.md +119 -2
- package/bin/dsh-context-kit.mjs +138 -0
- package/client/panel.js +302 -0
- package/cordis.patch.yml +21 -0
- package/icon.svg +13 -0
- package/index.js +385 -0
- package/lib/index/events.cjs +90 -0
- package/lib/index/render.cjs +177 -0
- package/lib/index/sources.cjs +286 -0
- package/lib/index/tokens.cjs +48 -0
- package/lib/kit-ops.mjs +230 -0
- package/lib/remote.mjs +55 -0
- package/lib/settings.mjs +69 -0
- package/lib/tuner/sync.mjs +140 -0
- package/lib/tuner/tuner.mjs +647 -0
- package/package.json +63 -4
- package/tuner.js +149 -0
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,120 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-context-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Save context tokens in DSH. One package, three parts:
|
|
4
|
+
|
|
5
|
+
| part | what it does |
|
|
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
|
+
| **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
|
+
| **Settings panel** (web) | A Context page: drag the trigger point, apply, preview, remove. No YAML editing. |
|
|
10
|
+
|
|
11
|
+
Why this combination: compaction is the lever that actually moves the bill. Compacting at 524K instead of
|
|
12
|
+
679K avoids re-sending the region above the trigger on every step; the index then makes sure the summary
|
|
13
|
+
that replaces that region still tells the model where things are. The two are complementary — the index
|
|
14
|
+
alone saves nothing on its own, and earlier compaction alone loses pointers.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
dsh plugin add dsh-context-kit
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Restart DSH. You get:
|
|
23
|
+
|
|
24
|
+
- two host plugins (`context-kit-index`, `context-kit-tuner`),
|
|
25
|
+
- a **Settings → Context** page,
|
|
26
|
+
- a `preset_tuner` tool,
|
|
27
|
+
- a `dsh-context-kit` CLI.
|
|
28
|
+
|
|
29
|
+
## Panel: Settings → Context
|
|
30
|
+
|
|
31
|
+
- **Compact at** — the fraction of the context window that triggers compaction (default `0.5` → ~524K).
|
|
32
|
+
- **Keep after compact** — how much survives compaction (default `0.16` → ~168K).
|
|
33
|
+
- **Save** writes `settings.json`, **Apply to profile** regenerates the profile override, and the page
|
|
34
|
+
tells you to restart. **Preview** shows the exact generated YAML before it is written.
|
|
35
|
+
- **Remove override** deletes the managed block and falls back to upstream defaults.
|
|
36
|
+
|
|
37
|
+
Saving is not the same as applying: the panel never touches YAML until you press Apply, and applying
|
|
38
|
+
never happens silently.
|
|
39
|
+
|
|
40
|
+
Where it stores things:
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
<profile>/dsh-context-kit/settings.json # what the panel edits
|
|
44
|
+
<profile>/cordis.patch.yml # managed block: generated, not hand-written
|
|
45
|
+
<profile>/dsh-context-kit/backups/ # every overwrite keeps the previous file (last 10)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Precedence: `settings.json` wins over the plugin's `config.presets` in the bundle/patch. In practice the
|
|
49
|
+
plugin row can stay empty (`presets: {}`) and the panel is the single place where numbers live — the
|
|
50
|
+
managed block in `cordis.patch.yml` always reflects what you last applied.
|
|
51
|
+
|
|
52
|
+
## Why the tuner exists (the freeze problem)
|
|
53
|
+
|
|
54
|
+
The live `compaction-basic` instance lives **inside the agent preset**, in a group that is `isolate`d.
|
|
55
|
+
A host-level plugin cannot reach that service, and a profile patch row `- id: compaction-basic` targets
|
|
56
|
+
an inactive leftover row — it looks right and does nothing.
|
|
57
|
+
|
|
58
|
+
Overriding a preset row replaces its `config` wholesale, so the only way in is to restate the whole
|
|
59
|
+
preset. Hand-writing that copy freezes the preset: from then on you stop receiving upstream preset
|
|
60
|
+
updates. The tuner instead reads the preset **out of the running app** (`resources/app.asar`) and
|
|
61
|
+
regenerates the restatement:
|
|
62
|
+
|
|
63
|
+
- upstream text is kept byte for byte,
|
|
64
|
+
- your changes are inserted as extra lines only,
|
|
65
|
+
- the result lives in a marked managed block,
|
|
66
|
+
- after a DSH update the next startup (or `Apply`) regenerates it from the new upstream preset.
|
|
67
|
+
|
|
68
|
+
Unit tests assert the insertion-only property against the real shipped presets, and a live check reads
|
|
69
|
+
this machine's `app.asar` end to end.
|
|
70
|
+
|
|
71
|
+
## CLI
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
dsh-context-kit status
|
|
75
|
+
dsh-context-kit set --threshold 0.5 --retain 0.16 --preset cordis
|
|
76
|
+
dsh-context-kit sync [--force --mode replace]
|
|
77
|
+
dsh-context-kit preview
|
|
78
|
+
dsh-context-kit clear
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
`--resources "<DSH install>/resources"` when it cannot find the app by itself.
|
|
82
|
+
`--force --mode replace` is required to take over a hand-written override; without it the tool refuses
|
|
83
|
+
and points at the line numbers.
|
|
84
|
+
|
|
85
|
+
## Tool
|
|
86
|
+
|
|
87
|
+
```text
|
|
88
|
+
preset_tuner(action="status" | "preview" | "sync" | "clear")
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
`status` is read-only and reports upstream drift; `sync` writes and tells you to restart.
|
|
92
|
+
|
|
93
|
+
## Safety
|
|
94
|
+
|
|
95
|
+
- **Fail closed.** If the upstream preset cannot be located, extracted, or modified safely (renamed
|
|
96
|
+
plugin, changed layout, key already set upstream), nothing is written.
|
|
97
|
+
- **Never edits upstream text**, only inserts.
|
|
98
|
+
- **Never silently masks upstream values**: a configured key that upstream already sets aborts the sync.
|
|
99
|
+
- **Refuses to eat hand-written overrides** unless explicitly forced.
|
|
100
|
+
- **Backups + idempotence**: unchanged input writes nothing, so it never creates restart noise.
|
|
101
|
+
|
|
102
|
+
## What it does not do (yet)
|
|
103
|
+
|
|
104
|
+
- It does not evict history itself. Batch eviction of old tool results is the next lever; the tuner
|
|
105
|
+
already supports adding a plugin into a preset group, which is where such an evictor has to live to
|
|
106
|
+
see the compaction service.
|
|
107
|
+
- The trigger estimate assumes the documented window (`contextWindow` 1048576, `maxOutputTokens` 256000,
|
|
108
|
+
`headroomTokens` 65536). Override them in the plugin config if your model differs.
|
|
109
|
+
|
|
110
|
+
## Rollback
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
dsh-context-kit clear # then restart
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Backups remain in `<profile>/dsh-context-kit/backups/`.
|
|
117
|
+
|
|
118
|
+
## License
|
|
119
|
+
|
|
120
|
+
MIT
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* dsh-context-kit CLI —— 在 app 之外跑 status / sync / clear / set。
|
|
4
|
+
* 面板与 Host 工具走的是同一套逻辑(lib/kit-ops.mjs),这里只是命令行外壳。
|
|
5
|
+
*
|
|
6
|
+
* dsh-context-kit status
|
|
7
|
+
* dsh-context-kit set --threshold 0.5 --retain 0.16 [--preset cordis]
|
|
8
|
+
* dsh-context-kit sync [--force --mode replace]
|
|
9
|
+
* dsh-context-kit preview
|
|
10
|
+
* dsh-context-kit clear
|
|
11
|
+
*
|
|
12
|
+
* app 位置默认取 --resources / DSH_APP_RESOURCES;profile 默认按模块路径或 ~/.dsh 推断。
|
|
13
|
+
*/
|
|
14
|
+
import { createKit, triggersFor } from '../lib/kit-ops.mjs'
|
|
15
|
+
|
|
16
|
+
function parseArgs(argv) {
|
|
17
|
+
const out = { _: [] }
|
|
18
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
19
|
+
const token = argv[i]
|
|
20
|
+
if (!token.startsWith('--')) {
|
|
21
|
+
out._.push(token)
|
|
22
|
+
continue
|
|
23
|
+
}
|
|
24
|
+
const key = token.slice(2)
|
|
25
|
+
const next = argv[i + 1]
|
|
26
|
+
if (next === undefined || next.startsWith('--')) {
|
|
27
|
+
out[key] = true
|
|
28
|
+
continue
|
|
29
|
+
}
|
|
30
|
+
out[key] = next
|
|
31
|
+
i += 1
|
|
32
|
+
}
|
|
33
|
+
return out
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const fail = (message) => {
|
|
37
|
+
console.error(`错误: ${message}`)
|
|
38
|
+
process.exit(1)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const args = parseArgs(process.argv.slice(2))
|
|
42
|
+
const command = args._[0] ?? 'status'
|
|
43
|
+
const fmt = (tokens) => `${Math.round(tokens / 1024)}K`
|
|
44
|
+
|
|
45
|
+
const config = {
|
|
46
|
+
profileDir: typeof args['profile-dir'] === 'string' ? args['profile-dir'] : '',
|
|
47
|
+
profile: typeof args.profile === 'string' ? args.profile : 'desktop',
|
|
48
|
+
appRoot: typeof args['app-root'] === 'string' ? args['app-root'] : typeof args.resources === 'string' ? args.resources : '',
|
|
49
|
+
autoSync: false,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let kit
|
|
53
|
+
try {
|
|
54
|
+
kit = createKit(config)
|
|
55
|
+
} catch (error) {
|
|
56
|
+
fail(error.message)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (command === 'status') {
|
|
60
|
+
const state = kit.describe()
|
|
61
|
+
console.log(`profile : ${state.profileDir}(${state.profileVia})`)
|
|
62
|
+
console.log(`补丁 : ${state.patchPath}`)
|
|
63
|
+
console.log(`上游 : ${state.sources.join(' | ')}`)
|
|
64
|
+
console.log(`预设 : ${state.upstreamPresets.join(', ')}`)
|
|
65
|
+
console.log(`托管块 : ${state.managed.map((block) => `${block.presetId}@${block.sha}`).join(', ') || '<无>'}`)
|
|
66
|
+
console.log(
|
|
67
|
+
`阈值 : ${state.knobs.thresholdRatio}(${fmt(state.triggers.thresholdTokens)} 触发)` +
|
|
68
|
+
` / 保留 ${state.knobs.retainRatio}(${fmt(state.triggers.retainTokens)})`,
|
|
69
|
+
)
|
|
70
|
+
for (const entry of state.entries) {
|
|
71
|
+
const where = !entry.previousSha ? '未生成' : entry.drifted ? `需重新 sync(${entry.previousSha}→${entry.upstreamSha})` : '已最新'
|
|
72
|
+
console.log(` - ${entry.presetId}: ${where}`)
|
|
73
|
+
}
|
|
74
|
+
for (const item of state.foreign) console.log(` ! 手写覆盖: ${item.presetId} 第 ${item.line} 行`)
|
|
75
|
+
for (const problem of state.problems) console.log(` ! ${problem}`)
|
|
76
|
+
process.exit(0)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (command === 'set') {
|
|
80
|
+
const threshold = Number(args.threshold ?? 0.5)
|
|
81
|
+
const retain = Number(args.retain ?? 0.16)
|
|
82
|
+
try {
|
|
83
|
+
const result = kit.saveCompaction({
|
|
84
|
+
presetId: typeof args.preset === 'string' ? args.preset : undefined,
|
|
85
|
+
thresholdRatio: threshold,
|
|
86
|
+
retainRatio: retain,
|
|
87
|
+
auto: args.auto !== 'false',
|
|
88
|
+
})
|
|
89
|
+
console.log(`已写入设置: ${result.presetId} threshold=${threshold} retain=${retain}`)
|
|
90
|
+
console.log(`触发点约 ${fmt(result.triggers.thresholdTokens)},压缩后保留约 ${fmt(result.triggers.retainTokens)}`)
|
|
91
|
+
console.log('接着运行 sync 把它写进 profile 补丁,然后重启 DSH。')
|
|
92
|
+
} catch (error) {
|
|
93
|
+
fail(error.message)
|
|
94
|
+
}
|
|
95
|
+
process.exit(0)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (command === 'preview') {
|
|
99
|
+
try {
|
|
100
|
+
console.log(kit.preview())
|
|
101
|
+
} catch (error) {
|
|
102
|
+
fail(error.message)
|
|
103
|
+
}
|
|
104
|
+
process.exit(0)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (command === 'sync') {
|
|
108
|
+
try {
|
|
109
|
+
const result = kit.sync({ force: Boolean(args.force), mode: args.mode === 'replace' ? 'replace' : 'append' })
|
|
110
|
+
if (!result.changed) {
|
|
111
|
+
console.log('已是最新,文件未改动。')
|
|
112
|
+
process.exit(0)
|
|
113
|
+
}
|
|
114
|
+
console.log(`已写入 ${result.path}`)
|
|
115
|
+
if (result.backup) console.log(`备份 ${result.backup}`)
|
|
116
|
+
if (result.removedForeign) console.log(`接管 移除手写覆盖 ${result.removedForeign} 处`)
|
|
117
|
+
for (const entry of result.entries) {
|
|
118
|
+
console.log(` - ${entry.presetId}: sha ${entry.upstreamSha}${entry.drifted ? `(漂移 ${entry.previousSha}→${entry.upstreamSha})` : ''}`)
|
|
119
|
+
}
|
|
120
|
+
console.log('重启 DSH 后生效。')
|
|
121
|
+
} catch (error) {
|
|
122
|
+
fail(error.message)
|
|
123
|
+
}
|
|
124
|
+
process.exit(0)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (command === 'clear') {
|
|
128
|
+
const result = kit.clear()
|
|
129
|
+
if (!result.changed) {
|
|
130
|
+
console.log('没有托管块。')
|
|
131
|
+
process.exit(0)
|
|
132
|
+
}
|
|
133
|
+
console.log(`已移除 ${result.removed} 个托管块${result.backup ? `,备份 ${result.backup}` : ''}。重启 DSH 后生效。`)
|
|
134
|
+
process.exit(0)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
fail(`未知命令: ${command}(可用: status | set | preview | sync | clear)`)
|
|
138
|
+
export { triggersFor }
|
package/client/panel.js
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
// dsh-context-kit 的 Settings 面板(Web 客户端插件)。
|
|
2
|
+
// 通过 ctx.connection.rpc 调宿主 contextKit 服务:status / save / sync / preview / clear。
|
|
3
|
+
window.__ModuleLoader__.load({
|
|
4
|
+
id: 'dsh-context-kit',
|
|
5
|
+
factory(require) {
|
|
6
|
+
const React = require('react')
|
|
7
|
+
const h = React.createElement
|
|
8
|
+
|
|
9
|
+
const EN = {
|
|
10
|
+
label: 'Context',
|
|
11
|
+
title: 'Context & Compaction',
|
|
12
|
+
subtitle: 'Save tokens by compacting earlier, without freezing the preset.',
|
|
13
|
+
threshold: 'Compact at',
|
|
14
|
+
retain: 'Keep after compact',
|
|
15
|
+
auto: 'Automatic compaction',
|
|
16
|
+
save: 'Save',
|
|
17
|
+
sync: 'Apply to profile',
|
|
18
|
+
preview: 'Preview',
|
|
19
|
+
clear: 'Remove override',
|
|
20
|
+
refresh: 'Refresh',
|
|
21
|
+
restart: 'Restart DSH to apply. Existing sessions pick up the new preset after restart.',
|
|
22
|
+
saved: 'Saved. Apply + restart to take effect.',
|
|
23
|
+
synced: 'Written to the profile patch.',
|
|
24
|
+
unchanged: 'Already up to date; nothing written.',
|
|
25
|
+
cleared: 'Override removed. Restart to fall back to upstream defaults.',
|
|
26
|
+
drift: 'Upstream preset changed',
|
|
27
|
+
upToDate: 'Upstream in sync',
|
|
28
|
+
missing: 'Not generated yet',
|
|
29
|
+
foreign: 'A hand-written override exists in your patch; remove it before applying.',
|
|
30
|
+
ofWindow: 'of the context window',
|
|
31
|
+
triggers: 'Compaction triggers at',
|
|
32
|
+
keeps: 'then keeps about',
|
|
33
|
+
profile: 'Profile',
|
|
34
|
+
patch: 'Patch file',
|
|
35
|
+
upstream: 'Upstream',
|
|
36
|
+
busy: 'Working…',
|
|
37
|
+
error: 'Error',
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const ZH = {
|
|
41
|
+
label: '上下文',
|
|
42
|
+
title: '上下文与压缩',
|
|
43
|
+
subtitle: '早点压缩来省 token,同时不冻结预设。',
|
|
44
|
+
threshold: '压缩触发于',
|
|
45
|
+
retain: '压缩后保留',
|
|
46
|
+
auto: '启用自动压缩',
|
|
47
|
+
save: '保存',
|
|
48
|
+
sync: '写入 profile',
|
|
49
|
+
preview: '预览',
|
|
50
|
+
clear: '移除覆盖',
|
|
51
|
+
refresh: '刷新',
|
|
52
|
+
restart: '重启 DSH 后生效;已存在的会话也会在重启后使用新的预设版本。',
|
|
53
|
+
saved: '已保存。写入并重启后生效。',
|
|
54
|
+
synced: '已写入 profile 补丁。',
|
|
55
|
+
unchanged: '已是最新,没有写入。',
|
|
56
|
+
cleared: '已移除覆盖,重启后回到上游默认。',
|
|
57
|
+
drift: '上游预设已变化',
|
|
58
|
+
upToDate: '与上游一致',
|
|
59
|
+
missing: '尚未生成',
|
|
60
|
+
foreign: '补丁里存在手写覆盖,请先移除再写入。',
|
|
61
|
+
ofWindow: '占上下文窗口',
|
|
62
|
+
triggers: '压缩触发点',
|
|
63
|
+
keeps: '压缩后约保留',
|
|
64
|
+
profile: 'Profile',
|
|
65
|
+
patch: '补丁文件',
|
|
66
|
+
upstream: '上游',
|
|
67
|
+
busy: '处理中…',
|
|
68
|
+
error: '出错',
|
|
69
|
+
}
|
|
70
|
+
|
|
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
|
+
`
|
|
90
|
+
|
|
91
|
+
function makeT(ctx) {
|
|
92
|
+
try {
|
|
93
|
+
ctx.locale.register('context-kit', { en: EN, zh: ZH })
|
|
94
|
+
} catch {}
|
|
95
|
+
let bound
|
|
96
|
+
try {
|
|
97
|
+
bound = ctx.locale.bind('context-kit')
|
|
98
|
+
} catch {}
|
|
99
|
+
return function t(key, fallback) {
|
|
100
|
+
try {
|
|
101
|
+
if (typeof bound === 'function') {
|
|
102
|
+
const value = bound(key)
|
|
103
|
+
if (typeof value === 'string' && value) return value
|
|
104
|
+
} else if (bound && typeof bound[key] === 'string') {
|
|
105
|
+
return bound[key]
|
|
106
|
+
}
|
|
107
|
+
} catch {}
|
|
108
|
+
let id = 'en'
|
|
109
|
+
try { id = ctx.locale.getLocale().id } catch {}
|
|
110
|
+
const dict = id === 'zh' ? ZH : EN
|
|
111
|
+
return dict[key] || fallback || key
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function makeApi(ctx) {
|
|
116
|
+
function call(method, args) {
|
|
117
|
+
return ctx.connection.rpc.call('/api', `contextKit/${method}`, { args: args || {} }, undefined).then(function (response) {
|
|
118
|
+
if (response && response.ok) return response.value
|
|
119
|
+
const message = response && response.error ? response.error.message : 'context kit remote call failed'
|
|
120
|
+
const error = new Error(message)
|
|
121
|
+
error.code = response && response.error ? response.error.code : 'REMOTE_FAILED'
|
|
122
|
+
throw error
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
status: function () { return call('status', {}) },
|
|
127
|
+
save: function (input) {
|
|
128
|
+
return call('save', {
|
|
129
|
+
presetId: input.presetId,
|
|
130
|
+
thresholdRatio: input.thresholdRatio,
|
|
131
|
+
retainRatio: input.retainRatio,
|
|
132
|
+
auto: input.auto,
|
|
133
|
+
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', {}) },
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const fmtK = function (tokens) {
|
|
143
|
+
if (!Number.isFinite(tokens)) return '—'
|
|
144
|
+
if (tokens >= 1024 * 1024) return (tokens / (1024 * 1024)).toFixed(1) + 'M'
|
|
145
|
+
return Math.round(tokens / 1024) + 'K'
|
|
146
|
+
}
|
|
147
|
+
const pct = function (ratio) { return Math.round(Number(ratio) * 1000) / 10 + '%' }
|
|
148
|
+
|
|
149
|
+
function Panel(props) {
|
|
150
|
+
const t = props.t
|
|
151
|
+
const api = props.api
|
|
152
|
+
const [state, setState] = React.useState(null)
|
|
153
|
+
const [threshold, setThreshold] = React.useState(0.5)
|
|
154
|
+
const [retain, setRetain] = React.useState(0.16)
|
|
155
|
+
const [busy, setBusy] = React.useState(false)
|
|
156
|
+
const [message, setMessage] = React.useState('')
|
|
157
|
+
const [error, setError] = React.useState('')
|
|
158
|
+
const [pre, setPre] = React.useState('')
|
|
159
|
+
|
|
160
|
+
const load = React.useCallback(function () {
|
|
161
|
+
return api.status().then(function (next) {
|
|
162
|
+
setState(next)
|
|
163
|
+
if (next && next.knobs) {
|
|
164
|
+
setThreshold(Number(next.knobs.thresholdRatio) || 0.5)
|
|
165
|
+
setRetain(Number(next.knobs.retainRatio) || 0.16)
|
|
166
|
+
}
|
|
167
|
+
}).catch(function (cause) { setError(String(cause && cause.message ? cause.message : cause)) })
|
|
168
|
+
}, [api])
|
|
169
|
+
|
|
170
|
+
React.useEffect(function () { load() }, [load])
|
|
171
|
+
|
|
172
|
+
const run = function (label, task) {
|
|
173
|
+
setBusy(true)
|
|
174
|
+
setError('')
|
|
175
|
+
setMessage('')
|
|
176
|
+
return task().then(function (result) {
|
|
177
|
+
if (label) setMessage(label)
|
|
178
|
+
return result
|
|
179
|
+
}).catch(function (cause) {
|
|
180
|
+
setError(String(cause && cause.message ? cause.message : cause))
|
|
181
|
+
}).then(function () { setBusy(false) })
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const presetId = state && state.presets && state.presets.length ? state.presets[0] : 'cordis'
|
|
185
|
+
const window2 = state && state.triggers ? state.triggers.contextWindow : 1048576
|
|
186
|
+
const triggerTokens = Math.floor(window2 * threshold)
|
|
187
|
+
const retainTokens = Math.floor(window2 * retain)
|
|
188
|
+
|
|
189
|
+
const entry = state && state.entries ? state.entries[0] : null
|
|
190
|
+
const stateText = !entry
|
|
191
|
+
? t('missing')
|
|
192
|
+
: entry.drifted
|
|
193
|
+
? t('drift') + ' · ' + entry.previousSha + ' → ' + entry.upstreamSha
|
|
194
|
+
: entry.previousSha
|
|
195
|
+
? t('upToDate') + ' · ' + entry.upstreamSha
|
|
196
|
+
: t('missing')
|
|
197
|
+
|
|
198
|
+
return h('div', { className: 'ck_wrap' },
|
|
199
|
+
h('div', { className: 'ck_head' },
|
|
200
|
+
h('h2', null, t('title')),
|
|
201
|
+
h('p', null, t('subtitle'))),
|
|
202
|
+
|
|
203
|
+
h('div', { className: 'ck_card' },
|
|
204
|
+
h('div', { className: 'ck_row' },
|
|
205
|
+
h('label', null, t('threshold')),
|
|
206
|
+
h('input', {
|
|
207
|
+
type: 'range', min: 0.2, max: 0.85, step: 0.01, value: threshold,
|
|
208
|
+
disabled: busy,
|
|
209
|
+
onChange: function (event) { setThreshold(Number(event.target.value)) },
|
|
210
|
+
}),
|
|
211
|
+
h('span', { className: 'ck_val' }, fmtK(triggerTokens)),
|
|
212
|
+
h('span', { className: 'ck_meta' }, pct(threshold) + ' ' + t('ofWindow'))),
|
|
213
|
+
h('div', { className: 'ck_row' },
|
|
214
|
+
h('label', null, t('retain')),
|
|
215
|
+
h('input', {
|
|
216
|
+
type: 'range', min: 0.04, max: Math.max(0.05, threshold - 0.02), step: 0.01, value: retain,
|
|
217
|
+
disabled: busy,
|
|
218
|
+
onChange: function (event) { setRetain(Number(event.target.value)) },
|
|
219
|
+
}),
|
|
220
|
+
h('span', { className: 'ck_val' }, fmtK(retainTokens)),
|
|
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)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return {
|
|
278
|
+
inject: ['slots', 'connection', 'locale'],
|
|
279
|
+
apply(ctx) {
|
|
280
|
+
const style = document.createElement('style')
|
|
281
|
+
style.dataset.plugin = 'dsh-context-kit'
|
|
282
|
+
style.textContent = CSS
|
|
283
|
+
document.head.appendChild(style)
|
|
284
|
+
ctx.effect(function () {
|
|
285
|
+
return function () { style.remove() }
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
const t = makeT(ctx)
|
|
289
|
+
const api = makeApi(ctx)
|
|
290
|
+
ctx.slots.inject('settings.section', function () {
|
|
291
|
+
return ctx.slots.register({
|
|
292
|
+
name: 'settings.section',
|
|
293
|
+
id: 'context-kit',
|
|
294
|
+
order: 46,
|
|
295
|
+
label: function () { return t('label', 'Context') },
|
|
296
|
+
inject: function () { return { api: api, t: t } },
|
|
297
|
+
}, Panel)
|
|
298
|
+
})
|
|
299
|
+
},
|
|
300
|
+
}
|
|
301
|
+
},
|
|
302
|
+
})
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# dsh-context-kit:一个包两个 Host 入口 + 一个 Settings 面板(面板由 package.json 的 dsh.client 声明)。
|
|
2
|
+
# - context-kit-index:压缩安全的上下文索引(system prompt 里一段「有什么、在哪」的指针)
|
|
3
|
+
# - context-kit-tuner:agent preset 内部配置调优(例如压缩阈值),只插入不冻结上游
|
|
4
|
+
- insert:
|
|
5
|
+
- id: context-kit-index
|
|
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
|
+
- id: context-kit-tuner
|
|
18
|
+
name: dsh-context-kit/tuner
|
|
19
|
+
config:
|
|
20
|
+
autoSync: true
|
|
21
|
+
presets: {}
|
package/icon.svg
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="dsh-preset-tuner">
|
|
2
|
+
<rect width="64" height="64" rx="14" fill="#0f172a"/>
|
|
3
|
+
<g stroke="#38bdf8" stroke-width="3" stroke-linecap="round">
|
|
4
|
+
<line x1="16" y1="20" x2="48" y2="20"/>
|
|
5
|
+
<line x1="16" y1="32" x2="48" y2="32"/>
|
|
6
|
+
<line x1="16" y1="44" x2="48" y2="44"/>
|
|
7
|
+
</g>
|
|
8
|
+
<g fill="#f8fafc">
|
|
9
|
+
<circle cx="24" cy="20" r="5"/>
|
|
10
|
+
<circle cx="40" cy="32" r="5"/>
|
|
11
|
+
<circle cx="28" cy="44" r="5"/>
|
|
12
|
+
</g>
|
|
13
|
+
</svg>
|