dsh-context-kit 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +120 -120
- package/bin/dsh-context-kit.mjs +138 -138
- package/client/panel.js +302 -302
- package/cordis.patch.yml +21 -21
- package/icon.svg +13 -13
- package/index.js +385 -385
- package/lib/index/events.cjs +90 -90
- package/lib/index/render.cjs +177 -177
- package/lib/index/sources.cjs +286 -286
- package/lib/index/tokens.cjs +48 -48
- package/lib/kit-ops.mjs +230 -230
- package/lib/remote.mjs +55 -55
- package/lib/settings.mjs +69 -69
- package/lib/tuner/sync.mjs +140 -140
- package/lib/tuner/tuner.mjs +647 -647
- package/package.json +65 -65
- package/tuner.js +149 -149
package/client/panel.js
CHANGED
|
@@ -1,302 +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
|
-
})
|
|
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
CHANGED
|
@@ -1,21 +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: {}
|
|
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: {}
|