dsh-context-kit 0.0.0-stage → 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 -0
- package/README.md +120 -3
- 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 +65 -6
- package/tuner.js +149 -0
package/package.json
CHANGED
|
@@ -1,6 +1,65 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "dsh-context-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "dsh-context-kit",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Save context tokens in DSH: a compaction-safe context index, and a tuner that lifts the auto-compaction trigger without freezing the preset — plus a Settings panel for both.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/LISHUANGXI1412/dsh-context-kit"
|
|
11
|
+
},
|
|
12
|
+
"author": "LISHUANGXI1412",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"dsh",
|
|
15
|
+
"dsh-plugin",
|
|
16
|
+
"context",
|
|
17
|
+
"compaction",
|
|
18
|
+
"agent-preset",
|
|
19
|
+
"tokens",
|
|
20
|
+
"index"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=22"
|
|
24
|
+
},
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./index.js",
|
|
27
|
+
"./tuner": "./tuner.js",
|
|
28
|
+
"./client": "./client/panel.js",
|
|
29
|
+
"./icon": "./icon.svg"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"index.js",
|
|
33
|
+
"tuner.js",
|
|
34
|
+
"lib/",
|
|
35
|
+
"bin/",
|
|
36
|
+
"client/",
|
|
37
|
+
"cordis.patch.yml",
|
|
38
|
+
"icon.svg",
|
|
39
|
+
"README.md",
|
|
40
|
+
"LICENSE"
|
|
41
|
+
],
|
|
42
|
+
"bin": {
|
|
43
|
+
"dsh-context-kit": "./bin/dsh-context-kit.mjs"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"test": "node --test"
|
|
47
|
+
},
|
|
48
|
+
"dsh": {
|
|
49
|
+
"bundle": {
|
|
50
|
+
"patch": "./cordis.patch.yml"
|
|
51
|
+
},
|
|
52
|
+
"client": {
|
|
53
|
+
"platform": "web",
|
|
54
|
+
"inject": [
|
|
55
|
+
"@deepseek-ai/dsh-client-runtime",
|
|
56
|
+
"@deepseek-ai/dsh-client-ui-slots",
|
|
57
|
+
"@deepseek-ai/dsh-client-ui-settings",
|
|
58
|
+
"@deepseek-ai/dsh-client-connection"
|
|
59
|
+
]
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"dependencies": {
|
|
63
|
+
"@deepseek-ai/dsh-typert-protocol": "0.2.1-rc.2"
|
|
64
|
+
}
|
|
65
|
+
}
|
package/tuner.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { createKit, resolveConfig, triggersFor } from './lib/kit-ops.mjs'
|
|
2
|
+
import { parseManagedBlocks } from './lib/tuner/tuner.mjs'
|
|
3
|
+
|
|
4
|
+
export const name = 'context-kit-tuner'
|
|
5
|
+
export const inject = ['tools']
|
|
6
|
+
|
|
7
|
+
const k = (tokens) => `${Math.round(tokens / 1024)}K`
|
|
8
|
+
|
|
9
|
+
function statusText(kit) {
|
|
10
|
+
const state = kit.describe()
|
|
11
|
+
const lines = [
|
|
12
|
+
`profile: ${state.profileDir}(${state.profileVia})`,
|
|
13
|
+
`补丁文件: ${state.patchPath}`,
|
|
14
|
+
`上游来源: ${state.sources.join(' | ')}`,
|
|
15
|
+
`上游预设: ${state.upstreamPresets.join(', ') || '<无>'}`,
|
|
16
|
+
'',
|
|
17
|
+
`已托管预设: ${state.managed.map((block) => `${block.presetId}@${block.sha}`).join(', ') || '<无>'}`,
|
|
18
|
+
`压缩阈值: ${state.knobs.thresholdRatio}(${k(state.triggers.thresholdTokens)} 触发)` +
|
|
19
|
+
` / 保留 ${state.knobs.retainRatio}(${k(state.triggers.retainTokens)})`,
|
|
20
|
+
]
|
|
21
|
+
for (const entry of state.entries) {
|
|
22
|
+
const where = !entry.previousSha
|
|
23
|
+
? '未生成(需 sync)'
|
|
24
|
+
: entry.drifted
|
|
25
|
+
? `上游已变 ${entry.previousSha}→${entry.upstreamSha},下次 sync 或启动时自动更新`
|
|
26
|
+
: '已是最新'
|
|
27
|
+
lines.push(` - ${entry.presetId}: ${where}`)
|
|
28
|
+
}
|
|
29
|
+
if (state.foreign.length > 0) {
|
|
30
|
+
lines.push('', `! 补丁里有手写覆盖: ${state.foreign.map((item) => `${item.presetId} 第 ${item.line} 行`).join(', ')}`)
|
|
31
|
+
lines.push(' 它们会让 sync 默认拒绝写入;需要接管时用 force + mode="replace"。')
|
|
32
|
+
}
|
|
33
|
+
if (state.problems.length > 0) {
|
|
34
|
+
lines.push('', '问题:')
|
|
35
|
+
for (const problem of state.problems) lines.push(` - ${problem}`)
|
|
36
|
+
}
|
|
37
|
+
lines.push('', '写盘后需要重启 DSH 生效;Settings → Context 面板可以直接改阈值。')
|
|
38
|
+
return lines.join('\n')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function apply(ctx, config = {}) {
|
|
42
|
+
const settings = resolveConfig(config)
|
|
43
|
+
const kit = createKit(config)
|
|
44
|
+
const log = (level, message) => {
|
|
45
|
+
try {
|
|
46
|
+
ctx?.logger?.[level]?.(message)
|
|
47
|
+
} catch {}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 面板的 RPC 服务依赖 @deepseek-ai/dsh-typert-protocol;缺了只丢面板,
|
|
51
|
+
// 工具与 CLI 必须照常工作(软失败)。
|
|
52
|
+
import('./lib/remote.mjs')
|
|
53
|
+
.then((module) => {
|
|
54
|
+
ctx.plugin(module.ContextKitRemoteService, { kit })
|
|
55
|
+
})
|
|
56
|
+
.catch((error) => {
|
|
57
|
+
log('warn', `context-kit: 面板服务未启动(${error?.message ?? error});工具与 CLI 不受影响`)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
ctx.tools.register({
|
|
61
|
+
name: 'preset_tuner',
|
|
62
|
+
description:
|
|
63
|
+
'调整 DSH 的 agent preset(预置)内部配置,例如把 compaction-basic 的自动压缩阈值提前,' +
|
|
64
|
+
'或往 preset 的某个 group 里加插件。做法是:读运行中 app(app.asar)里 shipped 的 preset 声明原文,' +
|
|
65
|
+
'原文逐字节保留、只插入你配置的少量行,生成托管块写进 profile 的 cordis.patch.yml。' +
|
|
66
|
+
'因此不会像手写覆盖那样冻结上游——DSH 升级后会按上游重新生成。' +
|
|
67
|
+
'action=status 报告上游是否漂移;preview 只返回将要写入的文本;sync 真正落盘(需重启 DSH 生效);' +
|
|
68
|
+
'clear 移除托管块。若 profile 里已有手写的同 id 覆盖,sync 默认拒绝写入,需 force + mode="replace" 接管。',
|
|
69
|
+
parameters: {
|
|
70
|
+
type: 'object',
|
|
71
|
+
properties: {
|
|
72
|
+
action: {
|
|
73
|
+
type: 'string',
|
|
74
|
+
enum: ['status', 'preview', 'sync', 'clear'],
|
|
75
|
+
description: 'status(只读报告)/ preview(只返回文本)/ sync(写盘)/ clear(移除托管块)',
|
|
76
|
+
},
|
|
77
|
+
force: {
|
|
78
|
+
type: 'boolean',
|
|
79
|
+
description: '与 mode="replace" 一起用:删除 profile 里手写的同 id preset 覆盖行,由本插件接管',
|
|
80
|
+
},
|
|
81
|
+
mode: {
|
|
82
|
+
type: 'string',
|
|
83
|
+
enum: ['append', 'replace'],
|
|
84
|
+
description: 'force 时必须为 replace;否则默认 append(遇到手写覆盖就中止)',
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
required: ['action'],
|
|
88
|
+
},
|
|
89
|
+
output: {
|
|
90
|
+
schema: { type: 'string' },
|
|
91
|
+
render: (_args, value) => [{ type: 'text', text: value }],
|
|
92
|
+
},
|
|
93
|
+
timeoutMs: 30000,
|
|
94
|
+
isConcurrencySafe: () => false,
|
|
95
|
+
async execute(args = {}) {
|
|
96
|
+
const action = String(args.action ?? 'status')
|
|
97
|
+
if (action === 'status') return statusText(kit)
|
|
98
|
+
if (action === 'preview') {
|
|
99
|
+
return [`将写入 ${kit.paths.patchPath}(预览,未落盘):`, '', kit.preview()].join('\n')
|
|
100
|
+
}
|
|
101
|
+
if (action === 'sync') {
|
|
102
|
+
const result = kit.sync({ force: Boolean(args.force), mode: args.mode === 'replace' ? 'replace' : 'append' })
|
|
103
|
+
if (!result.changed) return `已是最新,没有写入(${kit.paths.patchPath} 未变)。`
|
|
104
|
+
const drift = result.entries.filter((entry) => entry.drifted)
|
|
105
|
+
return [
|
|
106
|
+
`已写入 ${kit.paths.patchPath}`,
|
|
107
|
+
result.backup ? `备份: ${result.backup}` : '(原文件不存在,未产生备份)',
|
|
108
|
+
result.removedForeign ? `已接管并移除手写覆盖 ${result.removedForeign} 处` : '',
|
|
109
|
+
drift.length > 0
|
|
110
|
+
? `上游漂移已同步: ${drift.map((entry) => `${entry.presetId} ${entry.previousSha}→${entry.upstreamSha}`).join(', ')}`
|
|
111
|
+
: '',
|
|
112
|
+
'',
|
|
113
|
+
'重启 DSH 后生效(已存在的会话在重启后也会用新的 preset 版本)。',
|
|
114
|
+
]
|
|
115
|
+
.filter(Boolean)
|
|
116
|
+
.join('\n')
|
|
117
|
+
}
|
|
118
|
+
if (action === 'clear') {
|
|
119
|
+
const result = kit.clear()
|
|
120
|
+
if (!result.changed) return '没有托管块,未改动文件。'
|
|
121
|
+
return `已移除 ${result.removed} 个托管块。备份: ${result.backup ?? '<无>'}\n重启 DSH 后恢复上游默认行为。`
|
|
122
|
+
}
|
|
123
|
+
throw new Error(`未知 action: ${action}`)
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
if (settings.autoSync) {
|
|
128
|
+
queueMicrotask(() => {
|
|
129
|
+
try {
|
|
130
|
+
const active = kit.currentSettings()
|
|
131
|
+
if (Object.keys(active.presetTweaks).length === 0) return
|
|
132
|
+
const owned = new Set(parseManagedBlocks(kit.readPatch()).map((block) => block.presetId))
|
|
133
|
+
const missing = Object.keys(active.presetTweaks).filter((presetId) => !owned.has(presetId))
|
|
134
|
+
if (missing.length > 0) {
|
|
135
|
+
log('info', `context-kit: ${missing.join(', ')} 还没有托管块,先运行 preset_tuner(action="sync");本次不自动写入`)
|
|
136
|
+
return
|
|
137
|
+
}
|
|
138
|
+
const result = kit.sync()
|
|
139
|
+
if (result.changed) {
|
|
140
|
+
log('info', 'context-kit: 上游已变化,已按上游重新生成覆盖;重启后生效')
|
|
141
|
+
}
|
|
142
|
+
} catch (error) {
|
|
143
|
+
log('warn', `context-kit: 自动同步跳过(${error?.message ?? error})`)
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export { triggersFor }
|