@asd2233996/dsh-auto-context 0.0.0-stage → 1.15.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.en.md +113 -0
- package/README.md +250 -2
- package/cordis.patch.yml +23 -0
- package/docs/product-introduction.md +282 -0
- package/docs/structured-kb-notes.md +179 -0
- package/lib/actions.js +2758 -0
- package/lib/extract.js +1647 -0
- package/lib/graph.js +1628 -0
- package/lib/groups.js +307 -0
- package/lib/impl.js +170 -0
- package/lib/index.js +43 -0
- package/lib/kb-tools.js +384 -0
- package/lib/kb.js +254 -0
- package/lib/kg-archive.js +1042 -0
- package/lib/kg-store.js +1427 -0
- package/lib/recall.js +62 -0
- package/lib/scan.js +2537 -0
- package/lib/segments.js +660 -0
- package/lib/store.js +365 -0
- package/lib/util.js +64 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +101 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ASD-2233996
|
|
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.en.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# dsh-auto-context
|
|
2
|
+
|
|
3
|
+
**Fully automatic, AI-driven context management for [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) — no UI, no buttons, no manual operations.**
|
|
4
|
+
|
|
5
|
+
[中文 README](README.md) · [MIT License](LICENSE)
|
|
6
|
+
|
|
7
|
+
After installing it, your context usage simply stays low. The plugin keeps the native
|
|
8
|
+
"Conversation" and "Trajectory" tabs untouched: no new pages, buttons, or toggles are added.
|
|
9
|
+
In the background it splits the history that is about to be sent to the model into coarse
|
|
10
|
+
"stage cards", asks the model to judge the importance of each card, and prunes outdated
|
|
11
|
+
content — **the session log and the human-visible conversation remain complete**. The model
|
|
12
|
+
can recall pruned originals on demand with `context_recall`, and query project-level durable
|
|
13
|
+
facts in a structured knowledge base with five `knowledge_*` tools.
|
|
14
|
+
|
|
15
|
+
## What it is for
|
|
16
|
+
|
|
17
|
+
Keep **one entire engineering session inside a single conversation** while context usage stays low (below 40%) — no more switching conversations or maintaining a workspace-level handoff document so the next conversation can pick up where the previous one stopped.
|
|
18
|
+
|
|
19
|
+
The classic workaround is a workspace handoff document that every conversation rewrites. This plugin automates the whole process:
|
|
20
|
+
|
|
21
|
+
- only the **current turn and the most recent turn** are kept in raw form; older turns no longer enter the context;
|
|
22
|
+
- older turns are **distilled by the AI into durable facts** and written into a **structured knowledge base** (long-term memory);
|
|
23
|
+
- turn detection, fact extraction and knowledge-base design are **delegated to the model** — stronger models need less specification; weaker models may need more explicit rules; for a strong model, a rough structure is enough;
|
|
24
|
+
- the knowledge base is exposed to the agent **as tools** (`knowledge_index` → `knowledge_digest` → `knowledge_read` → `knowledge_search` → `knowledge_evidence`) and queried on demand instead of being pasted into the context.
|
|
25
|
+
|
|
26
|
+
**Fully automatic**: nothing to configure or click after installing — no UI, no buttons, no toggles.
|
|
27
|
+
|
|
28
|
+
> Tested on DeepSeek Harness **0.2.1-rc.2**; other versions are untested. `engines.dsh` declares `>=0.2.0 <0.3.0` for an installation-time version check.
|
|
29
|
+
|
|
30
|
+
## Features
|
|
31
|
+
|
|
32
|
+
- **Zero UI, zero operations** — install it and forget it; no tabs, buttons, panels, or HTTP routes.
|
|
33
|
+
- **Automatic per-turn evaluation** — after every `turn/end` (2.5s debounce) it runs a zero-LLM pruning pass.
|
|
34
|
+
- **AI-judged importance** — important / keep / noisy; important cards are pinned automatically, the current turn is always protected.
|
|
35
|
+
- **Safe pruning** — prunes by importance and size, replacing pruned surfaces with a one-line placeholder; the original session events are never deleted.
|
|
36
|
+
- **Structured knowledge base** — older turns are archived into an entry marker and distilled into project-level facts organized as *project → page → section → entry*.
|
|
37
|
+
- **Recall on demand** — `context_recall` returns the full original text of pruned cards.
|
|
38
|
+
- **Fail-open** — background failures are logged only; they never block or interrupt the conversation.
|
|
39
|
+
|
|
40
|
+
## Install
|
|
41
|
+
|
|
42
|
+
Requirements: DeepSeek Harness `>=0.2.0 <0.3.0`, Node.js `^22.19.0 || >=24.0.0`.
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
# from GitHub
|
|
46
|
+
dsh plugin --profile web add github:ASD-2233996/dsh-auto-context
|
|
47
|
+
# restart dsh afterwards (bundle plugins are composed at startup)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Or from a local checkout:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
git clone https://github.com/ASD-2233996/dsh-auto-context.git
|
|
54
|
+
dsh plugin --profile web add ./dsh-auto-context
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Verify with `dsh --profile web --dump-config | grep dsh-auto-context`.
|
|
58
|
+
|
|
59
|
+
## How it works
|
|
60
|
+
|
|
61
|
+
| Trigger | When | Action | Cost |
|
|
62
|
+
|---|---|---|---|
|
|
63
|
+
| ① | after every turn (`turn/end`, 2.5s debounce) | zero-cost pruning evaluation using existing analysis | no LLM |
|
|
64
|
+
| ② | context usage above `autoPruneTargetPercent` (default 40%) | AI classification, then prune back to target | one classification call (hash-cached) |
|
|
65
|
+
| ②' | idle longer than `autoPruneIdleAfterMs` (default 5 min) | prune further down to `autoPruneIdleTargetPercent` (default 25%) | no LLM |
|
|
66
|
+
| ③ | cumulative usage above `autoScanAfterTokens` (default 10M) | fallback AI analysis | same as ② |
|
|
67
|
+
|
|
68
|
+
After fact extraction, older turns are folded into a constant-size archive marker; the model
|
|
69
|
+
only sees that marker on subsequent requests. The original events stay in the session log.
|
|
70
|
+
|
|
71
|
+
## Configuration
|
|
72
|
+
|
|
73
|
+
Override any key in your profile's `cordis.patch.yml` (all keys have safe defaults):
|
|
74
|
+
|
|
75
|
+
```yaml
|
|
76
|
+
- id: context-manager
|
|
77
|
+
name: context-manager
|
|
78
|
+
config:
|
|
79
|
+
autoPrune: true
|
|
80
|
+
autoPruneTargetPercent: 40
|
|
81
|
+
autoPruneIdleTargetPercent: 25
|
|
82
|
+
autoPruneIdleAfterMs: 300000
|
|
83
|
+
autoScanAfterTokens: 10000000
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Full key reference: see the Chinese README.
|
|
87
|
+
|
|
88
|
+
## Knowledge base tools
|
|
89
|
+
|
|
90
|
+
`knowledge_index` → `knowledge_digest(scope)` → `knowledge_read(id)` → `knowledge_search(query)`
|
|
91
|
+
→ `knowledge_evidence(ids)`, plus `context_recall` for pruned originals. All are model tools;
|
|
92
|
+
nothing is exposed to the user interface.
|
|
93
|
+
|
|
94
|
+
## Data and privacy
|
|
95
|
+
|
|
96
|
+
- Writes only to `$DSH_HOME/context-manager/`; never to your workspace.
|
|
97
|
+
- No HTTP routes, no browser half, no new network requests.
|
|
98
|
+
- Atomic writes with `.bak` backups; session logs are never modified.
|
|
99
|
+
|
|
100
|
+
## Tests
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
npm install
|
|
104
|
+
npm test
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
215 test cases covering pruning, archiving, the knowledge base, tools, and host wiring.
|
|
108
|
+
The host packages (`@deepseek-ai/*`) are pinned in `devDependencies`, so tests run without a
|
|
109
|
+
pre-installed harness.
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
[MIT](LICENSE) © 2026 ASD-2233996
|
package/README.md
CHANGED
|
@@ -1,3 +1,251 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-auto-context(上下文管理)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](LICENSE)
|
|
4
|
+
[](package.json)
|
|
5
|
+
[](https://github.com/deepseek-ai/deepseek-harness)
|
|
6
|
+
[](https://github.com/ASD-2233996/dsh-auto-context/actions/workflows/ci.yml)
|
|
7
|
+
|
|
8
|
+
**中文** | [English](README.en.md)
|
|
9
|
+
|
|
10
|
+
DeepSeek Harness 的正式(持久化)插件:**完全自动、交给 AI 的上下文精简,没有界面。**
|
|
11
|
+
|
|
12
|
+
对话页面保持宿主原生的**「对话」「轨迹」两个 tab,不新增任何页面、按钮或开关。**
|
|
13
|
+
插件在后台把「即将发给云端的历史」切成粗粒度的阶段卡,由 AI 判定每张卡的重要度,
|
|
14
|
+
自动精简掉过时内容——**会话记录与人类可见的对话完整保留**。模型可以用 `context_recall`
|
|
15
|
+
按需取回被精简的原文,也可以用 5 个 `knowledge_*` 工具检索结构化信息库里的项目级持久事实。
|
|
16
|
+
|
|
17
|
+
> **一句话**:装上它,你什么都不用管——上下文占用会一直保持在低位。
|
|
18
|
+
|
|
19
|
+
## 产品简介
|
|
20
|
+
|
|
21
|
+
**设计初衷**:让你**在一个对话里跑完一整轮工程**,并且整个过程上下文占用始终保持在低位(低于 40%)——不再需要频繁更换对话,也不需要靠「工作区交接文档」把上一轮对话的结论搬到新对话。
|
|
22
|
+
|
|
23
|
+
传统方案通常是维护一份**工作区级别**的交接文档,每个对话都去修改它;本插件把这个过程完全自动化:
|
|
24
|
+
|
|
25
|
+
- **只保留当前轮与最近一轮**的原始信息,更早的轮次不再进入上下文;
|
|
26
|
+
- 更早轮次由智能体**自动抽取事实**,写入**结构化信息库**(长期记忆);
|
|
27
|
+
- 轮次如何判定、事实如何抽取、信息库如何组织,**具体细节都交给 AI**——模型越强效果越好;如果使用性能较差的模型,可以在这些环节补充更多规范;模型足够强时,只保留一个大致结构即可,不必给太多约束;
|
|
28
|
+
- 信息库以**工具**的形式交给 agent(`knowledge_index` → `knowledge_digest` → `knowledge_read` → `knowledge_search` → `knowledge_evidence`),按需检索,而不是把整个信息库塞进上下文。
|
|
29
|
+
|
|
30
|
+
**完全自动**:装完插件后不需要任何操作即可启用——没有界面、没有按钮、没有开关。
|
|
31
|
+
|
|
32
|
+
> 兼容性:本插件在 DeepSeek Harness **0.2.1-rc.2** 上开发与测试,其他版本尚未验证;`package.json` 的 `engines.dsh` 声明为 `>=0.2.0 <0.3.0`,安装时会做基础版本检查。
|
|
33
|
+
|
|
34
|
+
> ## 设计哲学(2026-12-02 用户定稿 · 常驻上下文)
|
|
35
|
+
>
|
|
36
|
+
> 考虑到 AI 模型的极大进步,**用户不应该介入上下文管理,而是完全自动化**;
|
|
37
|
+
> 而且这个自动化要**交给 AI**,而不是写死规则与阈值——哪怕现在效果不好,
|
|
38
|
+
> 随着基模迅速进步,未来效果也会足够好。**越好的 AI 给的约束越少,而越应该只指明明确的方向。**
|
|
39
|
+
>
|
|
40
|
+
> 我们要做的是:**用户只要装上这个插件,就会发现他们的上下文占用一直都不高。**
|
|
41
|
+
>
|
|
42
|
+
> 这段话同时写进了:
|
|
43
|
+
> 1. 宿主注入给模型的 `systemPrompt` 常驻上下文(`lib/impl.js` 的 `AUTONOMY_PHILOSOPHY`);
|
|
44
|
+
> 2. 本 README 顶部(后续开发的常驻方向标);
|
|
45
|
+
> 3. 本插件「无界面、零操作」的形态本身(没有 tab、按钮或面板)。
|
|
46
|
+
|
|
47
|
+
## 安装
|
|
48
|
+
|
|
49
|
+
**要求**
|
|
50
|
+
|
|
51
|
+
- [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) `>=0.2.0 <0.3.0`
|
|
52
|
+
- Node.js `^22.19.0 || >=24.0.0`
|
|
53
|
+
|
|
54
|
+
**从 GitHub 安装(推荐)**
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
dsh plugin --profile web add github:ASD-2233996/dsh-auto-context
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
然后重启 dsh。本插件是 bundle 插件(`dsh.bundle.patch`),组合层在启动时装配,需要重启一次才生效;
|
|
61
|
+
之后升级版本同样重启即可。
|
|
62
|
+
|
|
63
|
+
**从本地目录安装(开发调试)**
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
git clone https://github.com/ASD-2233996/dsh-auto-context.git
|
|
67
|
+
dsh plugin --profile web add ./dsh-auto-context
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**验证安装**
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
# 应能看到 dsh-auto-context 这一层
|
|
74
|
+
dsh --profile web --dump-config | grep -n "dsh-auto-context"
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
或查看 profile 的 `package.json`:`dsh.profile.bundles` 里出现 `dsh-auto-context`。
|
|
78
|
+
|
|
79
|
+
> 插件本体没有构建步骤,`lib/` 就是可直接运行的产物;也可以把它当作普通 cordis 插件,
|
|
80
|
+
> 在 profile 的 `cordis.patch.yml` 中手动挂载 `- id: context-manager` 行。
|
|
81
|
+
|
|
82
|
+
## 特性
|
|
83
|
+
|
|
84
|
+
- **零界面、零操作**:不新增 tab / 按钮 / 面板 / HTTP 接口,装上即生效;
|
|
85
|
+
- **每轮结束自动评估**:`turn/end` 后 2.5s 防抖执行一次自动精简评估(用已有分析,零 LLM 调用);
|
|
86
|
+
- **AI 判定重要度**:important / keep / noisy 三档,重要的自动进常驻区,当前轮永远受保护;
|
|
87
|
+
- **自动精简**:按重要度从低到高、从大卡开始丢弃,surface 用一行占位替换,**原始会话完整保留**;
|
|
88
|
+
- **结构化信息库(长期记忆)**:旧轮次折叠为归档 marker,并把项目级持久事实提炼进
|
|
89
|
+
「项目 → 页面 → 分节 → 条目」的信息库,模型按需检索;
|
|
90
|
+
- **随时可恢复**:`context_recall` 按卡片 id 取回被精简的完整原文;
|
|
91
|
+
- **失败静默(fail-open)**:任何后台失败只记日志,绝不阻塞对话、绝不打扰用户。
|
|
92
|
+
|
|
93
|
+
## 触发条件(用户零介入)
|
|
94
|
+
|
|
95
|
+
| # | 触发条件 | 行为 | 成本 |
|
|
96
|
+
|---|---|---|---|
|
|
97
|
+
| ① | **每轮结束**(`turn/end`,2.5s 防抖) | 执行一次自动精简评估:用已有分析把「可丢 + 非常驻 + 非当前轮」的卡按重要度压回目标内;低于目标则只记录检查时间 | 零 LLM |
|
|
98
|
+
| ② | **占用超过 `autoPruneTargetPercent`**(默认 40%) | 立即触发 AI 分析,分析完成后自动精简到目标以内 | 一次分类调用(有哈希缓存,只分析新卡) |
|
|
99
|
+
| ②' | **空闲超过 `autoPruneIdleAfterMs`**(默认 5 分钟) | 用户停手后把占用进一步压到 `autoPruneIdleTargetPercent`(默认 25%)——停得越久占用越低 | 零 LLM(用已有分析) |
|
|
100
|
+
| ③ | 累计用量越过 `autoScanAfterTokens`(默认 1000 万) | 兜底触发一次 AI 分析(兼容旧语义) | 同上 |
|
|
101
|
+
|
|
102
|
+
> 注意:① 与 ② 是**组合关系**——轮末先用已有分析做一次零成本评估:
|
|
103
|
+
> 占用超过 `autoPruneTargetPercent`(默认 40%)时才按重要度丢卡;
|
|
104
|
+
> 低于目标时不会为了「把已经很低的占用再压低」而误删有价值的历史。
|
|
105
|
+
> 触发条件 ①/②/②' 均有测试覆盖(见「测试」)。
|
|
106
|
+
|
|
107
|
+
## 自动流程
|
|
108
|
+
|
|
109
|
+
1. **自动跟踪**:任何会话在第一次 `turn/end` 时自动 `tracked`(无需用户打开任何面板)。
|
|
110
|
+
2. **AI 判定**:扫描时模型给每张阶段卡打 importance(important / keep / noisy)与种类;
|
|
111
|
+
AI 判为重要的分组自动进入常驻区(`autoPins`),当前轮卡片受保护。
|
|
112
|
+
3. **自动精简**:`pruneToTarget(target)` 把可丢卡按 tokens 从大到小丢弃(surface 占位替换,
|
|
113
|
+
原始会话事件完整保留),直到占用回到目标。
|
|
114
|
+
4. **每轮自动归档(KG)**:事实抽取成功后,把最近一轮之外的旧轮次折叠为信息库入口 marker
|
|
115
|
+
(`kgAutoArchive`,默认开),这是「每轮结束」时占用大幅下降的主要来源。
|
|
116
|
+
5. **丢卡后补抽信息库**:自动精简触发丢卡后,会再补一次 KG 抽取
|
|
117
|
+
(`triggerKgBackfill` → `scheduleKgExtract` → AI 抽取 → 写信息库 + 自动归档)。关键在于
|
|
118
|
+
**KG 抽取的数据源是原始会话事件而不是 surface 卡片**,所以被精简轮次的信息仍可被 AI
|
|
119
|
+
提炼为持久事实入库;`context_recall` 也能随时取回被精简卡片的原文。
|
|
120
|
+
6. **记录状态**:每次自动精简写入 `state.autoPrune`(时间 / 节省 tokens / 卡片数 / 前后占比 /
|
|
121
|
+
失败原因),供诊断与测试使用(无界面展示)。
|
|
122
|
+
|
|
123
|
+
失败语义:常驻/当前轮保护导致压不到目标时只记录 `autoPrune.error` 并写 warning 日志,
|
|
124
|
+
**绝不打扰用户、也不影响对话**(fail-open)。
|
|
125
|
+
|
|
126
|
+
## 配置(`cordis.patch.yml` 行 config,全部有安全默认值)
|
|
127
|
+
|
|
128
|
+
| 键 | 默认 | 说明 |
|
|
129
|
+
|---|---|---|
|
|
130
|
+
| `autoPrune` | `true` | 后台自动精简总开关 |
|
|
131
|
+
| `autoPruneTargetPercent` | `40` | **活跃水位**(对话进行中,每轮结束评估时压到这个值,10~95) |
|
|
132
|
+
| `autoPruneIdleTargetPercent` | `25` | **空闲水位**(用户停 `autoPruneIdleAfterMs` 无对话后压到这个值,5~95) |
|
|
133
|
+
| `autoPruneIdleAfterMs` | `300000` | 空闲精简触发前等待(毫秒,默认 5 分钟;0 关闭) |
|
|
134
|
+
| `autoScanAfterTokens` | `10_000_000` | 兜底 AI 分析阈值(触发条件 ③) |
|
|
135
|
+
| `autoScanOnOpen` | `true` | 兼容保留(无界面后没有调用入口,实际不再生效) |
|
|
136
|
+
| `protectLastTurn` | `true` | 保护当前轮卡片不被精简 |
|
|
137
|
+
| `maxPinned` / `autoPinMax` | `100` / `5` | 常驻区与 AI 自动固定上限 |
|
|
138
|
+
| `kgEnabled` / `kgAutoExtract` / `kgAutoArchive` | 均 `true` | 信息库长期记忆与每轮自动归档 |
|
|
139
|
+
|
|
140
|
+
在 profile 的 `cordis.patch.yml` 中覆盖,例如:
|
|
141
|
+
|
|
142
|
+
```yaml
|
|
143
|
+
- id: context-manager
|
|
144
|
+
name: context-manager
|
|
145
|
+
config:
|
|
146
|
+
autoPruneTargetPercent: 30
|
|
147
|
+
autoPruneIdleTargetPercent: 20
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## 结构化信息库(替代原知识图谱)
|
|
151
|
+
|
|
152
|
+
设计蓝本:[`docs/structured-kb-notes.md`](docs/structured-kb-notes.md)(调研 Memory Bank /
|
|
153
|
+
Basic Memory / repo-brain 等 GitHub 方案后的学习笔记 + 实现设计)。
|
|
154
|
+
产品介绍:[`docs/product-introduction.md`](docs/product-introduction.md)。
|
|
155
|
+
|
|
156
|
+
信息库把 AI 提炼的项目级持久事实组织为**「项目 → 页面(模块/主题)→ 分节 → 条目」**:
|
|
157
|
+
|
|
158
|
+
- 页面 = `scopePath`(如 `context-manager/host`),标题取路径最后一段。
|
|
159
|
+
- 分节固定 8 类:需求与目标 / 约束与规则 / 项目结构 / 关键决策 / 交付结果 / 接口契约 /
|
|
160
|
+
约定与规范 / **当前状态**。
|
|
161
|
+
- 条目带 `confidence / status / validFrom / source`(`validFrom` 为后续「做梦机制」的时间限定铺路)。
|
|
162
|
+
- 存放沿用原 KG 位置:`$DSH_HOME/context-manager/kg/graphs/<projectKey>.json`(不写用户工作区)。
|
|
163
|
+
|
|
164
|
+
### 模型工具(不向用户展示,供 AI 使用)
|
|
165
|
+
|
|
166
|
+
访问路径:`index(目录)→ digest(某页分节)→ read(整页)→ search(兜底)→ evidence(原文)`。
|
|
167
|
+
|
|
168
|
+
| 工具 | 作用 |
|
|
169
|
+
|---|---|
|
|
170
|
+
| `knowledge_index` | 信息库目录:每页一行(标题/分节计数/更新时间) |
|
|
171
|
+
| `knowledge_digest(scope)` | 某页的结构化分节摘要 |
|
|
172
|
+
| `knowledge_read(id)` | 读完整页面(全部条目 + 相关页) |
|
|
173
|
+
| `knowledge_search(query)` | 关键词检索条目(兜底,带页面/分节归属) |
|
|
174
|
+
| `knowledge_evidence(ids)` | 取条目的原文证据摘录(有界) |
|
|
175
|
+
| `context_recall` | 按卡片 id 取回被精简的原文(有界) |
|
|
176
|
+
|
|
177
|
+
实现:`lib/kb.js`(纯函数视图层,输入现有 kg 输出信息库视图)+ `lib/kb-tools.js`(5 个工具)。
|
|
178
|
+
|
|
179
|
+
## 插件结构
|
|
180
|
+
|
|
181
|
+
```
|
|
182
|
+
dsh-auto-context/
|
|
183
|
+
├── lib/
|
|
184
|
+
│ ├── index.js # 插件入口 shim:name / inject / 动态加载 impl
|
|
185
|
+
│ ├── impl.js # Host 装配:注册模型工具、注入静态 systemPrompt、监听 turn/end
|
|
186
|
+
│ ├── segments.js # surface → 卡片模型(纯计算)
|
|
187
|
+
│ ├── groups.js # 步骤单元 → 阶段卡(确定性切分)
|
|
188
|
+
│ ├── actions.js # 精简事务、扫描调度、自动精简、recall/信息库查询
|
|
189
|
+
│ ├── store.js # 每会话派生缓存(AI 结果 / 自动精简状态 / 开关)落盘
|
|
190
|
+
│ ├── scan.js # 一次性 AI 分类、短标题、简介
|
|
191
|
+
│ ├── recall.js # context_recall 模型工具
|
|
192
|
+
│ ├── graph.js # KG 事实图(边计算 / 检索 / 序列化)
|
|
193
|
+
│ ├── extract.js # AI 事实抽取
|
|
194
|
+
│ ├── kg-store.js # 项目级 KG 持久化
|
|
195
|
+
│ ├── kg-archive.js # 旧轮次折叠为归档 marker(纯函数)
|
|
196
|
+
│ ├── kb.js # 结构化信息库视图(纯函数)
|
|
197
|
+
│ └── kb-tools.js # 5 个 knowledge_* 模型工具
|
|
198
|
+
├── tests/ # node --test 测试(含 Host 装配与端到端验证)
|
|
199
|
+
├── docs/ # 设计笔记与产品介绍
|
|
200
|
+
├── locale/ # 插件列表展示文案(无界面后只用于标题/描述)
|
|
201
|
+
└── cordis.patch.yml # 插件挂载与行配置
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## 数据与存储(无 HTTP 接口、无浏览器半)
|
|
205
|
+
|
|
206
|
+
- 数据目录:`$DSH_HOME/context-manager/`
|
|
207
|
+
- `sessions/<sha256(sessionId) 前 32 位>.json`:每会话派生缓存(AI 结果、自动精简状态、开关),原子写 + `.bak`;
|
|
208
|
+
- `context-manager.log`:追加式操作审计(只记动作与 id,不记对话正文);
|
|
209
|
+
- `kg/graphs/<projectKey>.json`:项目级信息库数据(含 `.bak` 与 `.corrupt-<ts>` 隔离文件)。
|
|
210
|
+
- **没有任何 HTTP 接口**:插件不调用 `ctx.webServer`,不注册路由;不对用户暴露页面或操作入口。
|
|
211
|
+
- 对外只通过宿主服务:注册模型工具(`context_recall` + 5 个 `knowledge_*`)、注入两段静态
|
|
212
|
+
`systemPrompt`(设计哲学 + 信息库使用说明)、监听 `session/event` 的 `turn/end` 做后台自动化。
|
|
213
|
+
- **不写用户工作区、不新增网络请求**;原始会话日志与人类可见的对话完整保留。
|
|
214
|
+
|
|
215
|
+
## 测试
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
npm install
|
|
219
|
+
npm test
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
- 共 **215** 个用例,覆盖扫描/精简/归档/信息库/工具/宿主装配全链路;
|
|
223
|
+
- `tests/verification.test.mjs`:宿主行为全量验证(含自动精简/空闲精简/每轮自动精简等端到端用例);
|
|
224
|
+
- `tests/impl.test.mjs`:Host 半装配(动态加载、模型工具注册、`turn/end` 自动分析);
|
|
225
|
+
- 其余按模块拆分:`groups` / `scan` / `extract` / `graph` / `kg-store` / `kg-archive` /
|
|
226
|
+
`kb` / `kb-tools` 等纯函数单测。
|
|
227
|
+
|
|
228
|
+
测试会动态 import 宿主包(`@deepseek-ai/*`)。仓库已在 `devDependencies` 中固定了对应版本,
|
|
229
|
+
因此 `npm install` 后可直接运行,不要求本机预先装好 DSH。
|
|
230
|
+
|
|
231
|
+
## 兼容性
|
|
232
|
+
|
|
233
|
+
| 依赖 | 版本 |
|
|
234
|
+
|---|---|
|
|
235
|
+
| DeepSeek Harness | `>=0.2.0 <0.3.0`(`engines.dsh`) |
|
|
236
|
+
| Node.js | `^22.19.0 || >=24.0.0` |
|
|
237
|
+
|
|
238
|
+
插件只依赖宿主公开的 `tools` / `sessions` / `tokenMeter` / `sessionProjections` / `llm` /
|
|
239
|
+
`systemPrompt` 服务,不依赖任何第三方运行时依赖。
|
|
240
|
+
|
|
241
|
+
## 贡献
|
|
242
|
+
|
|
243
|
+
欢迎 Issue / PR。提交前请确保:
|
|
244
|
+
|
|
245
|
+
1. `npm install && npm test` 全绿(215 个用例);
|
|
246
|
+
2. 不新增 HTTP 接口 / 浏览器半 / 网络请求;
|
|
247
|
+
3. 不把任何用户数据写入工作区或提交进仓库。
|
|
248
|
+
|
|
249
|
+
## 许可证
|
|
250
|
+
|
|
251
|
+
[MIT](LICENSE) © 2026 ASD-2233996
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# context-manager — 完全自动、交给 AI 的上下文精简(无界面,2026-12-02 定稿)。
|
|
2
|
+
#
|
|
3
|
+
# 设计哲学:用户不介入上下文管理,完全自动化,决策交给 AI 而不是规则阈值;
|
|
4
|
+
# 目标是「用户装上之后上下文占用一直都不高」,所以本插件不新增任何 tab。
|
|
5
|
+
#
|
|
6
|
+
# Host 半:卡片分段与定价、后台自动精简(autoPrune)、AI 分类摘要(跟随当前
|
|
7
|
+
# 对话模型)、旧轮自动归档为结构化信息库、模型工具 context_recall 与
|
|
8
|
+
# knowledge_*(结构化信息库长期记忆)。无 HTTP 接口、无界面。
|
|
9
|
+
#
|
|
10
|
+
# 触发条件(用户零介入):
|
|
11
|
+
# ① 每轮结束 → 执行一次自动精简评估(低于目标只记录,超目标压回);
|
|
12
|
+
# ② 占用超过 autoPruneTargetPercent → 触发 AI 分析后自动精简到目标;
|
|
13
|
+
# ③ 累计用量越过 autoScanAfterTokens → 兜底触发一次 AI 分析。
|
|
14
|
+
#
|
|
15
|
+
# 可配置项(行 config,均有安全默认值):
|
|
16
|
+
# autoPrune: true # 后台自动精简总开关
|
|
17
|
+
# autoPruneTargetPercent: 40 # 活跃水位(10~95)
|
|
18
|
+
# autoPruneIdleTargetPercent: 25 # 空闲水位(5~95)
|
|
19
|
+
# autoPruneIdleAfterMs: 300000 # 空闲多久后触发(毫秒,0=关闭)
|
|
20
|
+
# autoScanAfterTokens: 10000000 # 累积用量触发 AI 分析的阈值(兜底)
|
|
21
|
+
- insert:
|
|
22
|
+
- id: context-manager
|
|
23
|
+
name: context-manager
|