dsh-echo-guard 0.1.0 → 0.2.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/README.en.md +38 -5
- package/README.md +49 -6
- package/cordis.patch.yml +22 -6
- package/lib/core.mjs +40 -8
- package/lib/index.mjs +154 -5
- package/package.json +1 -1
package/README.en.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# dsh-echo-guard
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/dsh-echo-guard)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
|
|
3
6
|
A DeepSeek Harness (DSH) plugin that removes filler-word flooding from the **model-visible reasoning history** — and leaves everything else verbatim.
|
|
4
7
|
|
|
5
8
|
## What it treats
|
|
@@ -27,24 +30,54 @@ The event log is never modified, so "what the model remembers" and "what the log
|
|
|
27
30
|
|
|
28
31
|
## Install
|
|
29
32
|
|
|
33
|
+
From npm:
|
|
34
|
+
|
|
30
35
|
```bash
|
|
31
|
-
pnpm add
|
|
36
|
+
pnpm add dsh-echo-guard
|
|
32
37
|
```
|
|
33
38
|
|
|
34
|
-
|
|
39
|
+
Or install it from the DSH plugin manager with the spec `dsh-echo-guard@^0.2.0`.
|
|
40
|
+
|
|
41
|
+
For local development use `link:`: add a `link:` dependency to the profile's `package.json` and include it in `dsh.profile.bundles`.
|
|
35
42
|
|
|
36
43
|
Configure via `cordis.patch.yml`. **Start with `dryRun: true`** for a day.
|
|
37
44
|
|
|
38
45
|
## Fillers
|
|
39
46
|
|
|
40
47
|
```yaml
|
|
41
|
-
fillers: ["好", "嗯"]
|
|
42
|
-
fillers: ["OK", "let me go"] # English, case-insensitive
|
|
48
|
+
fillers: ["好", "嗯", "OK", "let me go"] # two Chinese, two English — default since 0.2.1
|
|
43
49
|
```
|
|
44
50
|
|
|
45
51
|
- **Counting** includes occurrences inside words (`好` inside `好像`, `OK` inside `Okay`).
|
|
46
52
|
- **Deletion** only removes standalone ones: preceded by a non-word char, followed by punctuation/space/EOL.
|
|
47
53
|
- CJK uses character boundaries, Latin uses alphanumeric boundaries; multi-word phrases are removed whole.
|
|
54
|
+
- Drop a word from `fillers` to stop treating it as filler.
|
|
55
|
+
|
|
56
|
+
### Per-word thresholds (0.2.0)
|
|
57
|
+
|
|
58
|
+
```yaml
|
|
59
|
+
fillers: ["好", "嗯", "OK", "let me go"]
|
|
60
|
+
fillerMinCounts:
|
|
61
|
+
"好": 5
|
|
62
|
+
"嗯": 3
|
|
63
|
+
"OK": 5
|
|
64
|
+
"let me go": 3
|
|
65
|
+
burstMinCount: 5 # fallback for words without their own threshold
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Each word is counted and judged on its own; they never add up. `好` ×4 + `嗯` ×2 is **not** a hit, because neither reached its line. If only one word crosses, only that word is removed.
|
|
69
|
+
|
|
70
|
+
## Manual pass: clear the last turn (0.2.0)
|
|
71
|
+
|
|
72
|
+
The automatic pass waits for the count. `/strip-last` clears the previous turn right now, in the session you type it in. Other processes (a phone UI, a bot) can drop a request file instead; the plugin writes `.result.json` back.
|
|
73
|
+
|
|
74
|
+
```yaml
|
|
75
|
+
manual:
|
|
76
|
+
enabled: true
|
|
77
|
+
requestFile: "C:\\Users\\you\\.dsh\\strip-last.json" # omit = slash command only
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Three boundaries: only the **most recent pure-reasoning step**; never a tool-call step; and **never the turn that is currently running** — replacing a message that is still streaming would orphan its tool result and break the transcript.
|
|
48
81
|
|
|
49
82
|
## Receipt
|
|
50
83
|
|
|
@@ -63,7 +96,7 @@ Hence the threshold of 5, aimed at the "one per sentence" drift.
|
|
|
63
96
|
|
|
64
97
|
## Version and testing
|
|
65
98
|
|
|
66
|
-
**Version 0.1.0
|
|
99
|
+
**Version 0.2.1.** (0.1.0 → 0.2.0: per-word thresholds; manual `/strip-last` and request-file entry. 0.2.0 → 0.2.1: `OK` and `let me go` added to the default filler list.)
|
|
67
100
|
|
|
68
101
|
Verified on 2026-12-05 with:
|
|
69
102
|
|
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# dsh-echo-guard
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/dsh-echo-guard)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
|
|
3
6
|
一个 DeepSeek Harness(DSH)插件:把「收束词灌水」从**模型可见的思考历史**里删掉,其余一个字不动。
|
|
4
7
|
|
|
5
8
|
## 它治什么
|
|
@@ -29,11 +32,15 @@
|
|
|
29
32
|
|
|
30
33
|
## 装法
|
|
31
34
|
|
|
35
|
+
从 npm 装:
|
|
36
|
+
|
|
32
37
|
```bash
|
|
33
|
-
pnpm add
|
|
38
|
+
pnpm add dsh-echo-guard
|
|
34
39
|
```
|
|
35
40
|
|
|
36
|
-
在
|
|
41
|
+
在 DSH 的插件管理器里装也行,spec 写 `dsh-echo-guard@^0.2.0`。
|
|
42
|
+
|
|
43
|
+
本地改代码时用 `link:`:在 profile 的 `package.json` 里加一条 `link:` 依赖,并把它加进 `dsh.profile.bundles`。
|
|
37
44
|
|
|
38
45
|
配置见 `cordis.patch.yml`。
|
|
39
46
|
|
|
@@ -42,8 +49,7 @@ pnpm add link:/path/to/dsh-echo-guard
|
|
|
42
49
|
## 词表
|
|
43
50
|
|
|
44
51
|
```yaml
|
|
45
|
-
fillers: ["好", "嗯"]
|
|
46
|
-
fillers: ["OK", "let me go"] # 英文,大小写不敏感
|
|
52
|
+
fillers: ["好", "嗯", "OK", "let me go"] # 中英各两个,0.2.1 起的默认
|
|
47
53
|
```
|
|
48
54
|
|
|
49
55
|
规则:
|
|
@@ -51,6 +57,43 @@ fillers: ["OK", "let me go"] # 英文,大小写不敏感
|
|
|
51
57
|
- **计数**算全部:`好` 会数进「好像」,`OK` 会数进 `Okay`。
|
|
52
58
|
- **删除**只删单独成句的:前面不是词、后面是标点/空白/行尾。
|
|
53
59
|
- 中文按汉字边界判,英文按字母数字边界判;多词短语(`let me go`)整句删。
|
|
60
|
+
- 不想要哪个词,从 `fillers` 里删掉就行——计数和删除都只认名单里的。
|
|
61
|
+
|
|
62
|
+
### 每个词各算各的(0.2.0)
|
|
63
|
+
|
|
64
|
+
```yaml
|
|
65
|
+
fillers: ["好", "嗯", "OK", "let me go"]
|
|
66
|
+
fillerMinCounts:
|
|
67
|
+
"好": 5
|
|
68
|
+
"嗯": 3
|
|
69
|
+
"OK": 5
|
|
70
|
+
"let me go": 3
|
|
71
|
+
burstMinCount: 5 # 没单独配的词用这个
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
各算各的,**不互相凑数**:「好 4 个 + 嗯 2 个」不算命中,因为两边都没到自己的线。
|
|
75
|
+
命中哪个只删哪个——嗯够 3 但好只有 2,就只删嗯,好一个不动。
|
|
76
|
+
|
|
77
|
+
为什么不合计:合起来算会让两个词互相掩护,各自其实都没到线也会被清。
|
|
78
|
+
|
|
79
|
+
## 手动清上一轮(0.2.0)
|
|
80
|
+
|
|
81
|
+
自动那条要攒够次数。有时候不用等:想现在就清掉上一条,用 `/strip-last`。
|
|
82
|
+
|
|
83
|
+
- 在 DSH 里打字 `/strip-last` 就清当前这个框的上一轮;
|
|
84
|
+
- 别的进程(比如手机上的门)可以写一个请求文件,插件读它、清完把结果写回 `.result.json`:
|
|
85
|
+
|
|
86
|
+
```yaml
|
|
87
|
+
manual:
|
|
88
|
+
enabled: true
|
|
89
|
+
requestFile: "C:\\Users\\you\\.dsh\\strip-last.json" # 不填 = 只留斜杠命令
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
三条边界:
|
|
93
|
+
|
|
94
|
+
- 只看**最近的那一条纯思考步**,不是整个历史;
|
|
95
|
+
- 带工具调用的步不碰(那是整步替换的活,不归这个插件);
|
|
96
|
+
- **正在跑的那一轮不碰**——消息还在流上时换掉,工具结果回来会找不到调用,历史就坏了。
|
|
54
97
|
|
|
55
98
|
## 回执
|
|
56
99
|
|
|
@@ -71,7 +114,7 @@ fillers: ["OK", "let me go"] # 英文,大小写不敏感
|
|
|
71
114
|
|
|
72
115
|
## 版本与验证
|
|
73
116
|
|
|
74
|
-
**版本 0.1.0
|
|
117
|
+
**版本 0.2.1。**(0.1.0 → 0.2.0:每个词各算各的门槛;新增手动清上一轮 `/strip-last` 和请求文件入口。0.2.0 → 0.2.1:默认词表加上 `OK`、`let me go`。)
|
|
75
118
|
|
|
76
119
|
在以下环境实测通过(2026-12-05):
|
|
77
120
|
|
|
@@ -83,7 +126,7 @@ fillers: ["OK", "let me go"] # 英文,大小写不敏感
|
|
|
83
126
|
- 原始 `assistant/message` 事件仍在事件流里,一个字没动;
|
|
84
127
|
- 命中的 5 段全是「纯思考 + 正文」、零工具调用——带工具调用的步按设计跳过;
|
|
85
128
|
- 另一个会话的文件没有被写过(`onlySessions` 生效)。
|
|
86
|
-
- 单测:`node --test test/core.test.mjs`,
|
|
129
|
+
- 单测:`node --test test/core.test.mjs`,16 个用例全过。
|
|
87
130
|
|
|
88
131
|
**未验证**:两个插件同时命中同一步的交叉场景。设计上不重叠(带工具调用的步本插件直接跳过),但确实没实测到。
|
|
89
132
|
|
package/cordis.patch.yml
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
- insert:
|
|
2
2
|
- id: echo-guard
|
|
3
3
|
name: dsh-echo-guard
|
|
4
|
-
#
|
|
5
|
-
inject: [ agents ]
|
|
4
|
+
# agents:要读 agent.session;commands:0.2.0 起的 /strip-last 手动清靠它注册。
|
|
5
|
+
inject: [ agents, commands ]
|
|
6
6
|
config:
|
|
7
7
|
enabled: true
|
|
8
8
|
|
|
9
|
-
#
|
|
10
|
-
#
|
|
9
|
+
# 哪些词算「收束词灌水」。0.2.1 起默认带中英各两个;不想要的从名单里删掉即可。
|
|
10
|
+
# 计数算全部(「好像」里的「好」、「Okay」里的「OK」也算),删除只删单独成句的那一个。
|
|
11
11
|
fillers:
|
|
12
12
|
- "好"
|
|
13
13
|
- "嗯"
|
|
14
|
+
- "OK"
|
|
15
|
+
- "let me go"
|
|
14
16
|
|
|
15
|
-
#
|
|
17
|
+
# 每个词各算各的门槛,不互相凑数。
|
|
18
|
+
# 例:好 够 5 次才算,嗯 够 3 次就算;「好 4 + 嗯 2」不算命中。
|
|
19
|
+
fillerMinCounts:
|
|
20
|
+
"好": 5
|
|
21
|
+
"嗯": 3
|
|
22
|
+
"OK": 5
|
|
23
|
+
"let me go": 3
|
|
24
|
+
|
|
25
|
+
# 没在 fillerMinCounts 里单独配的词,用这个数。
|
|
16
26
|
burstMinCount: 5
|
|
17
27
|
|
|
18
28
|
# 删完在历史里留的那一行,可配:
|
|
@@ -30,5 +40,11 @@
|
|
|
30
40
|
# 先观察:true 时只写日志、不改历史。建议先跑一天。
|
|
31
41
|
dryRun: true
|
|
32
42
|
|
|
33
|
-
#
|
|
43
|
+
# 手动「清上一轮」(0.2.0):斜杠命令 /strip-last 一直挂着;
|
|
44
|
+
# 要给别的进程(比如手机上的门)用,就填一个请求文件的绝对路径。
|
|
45
|
+
manual:
|
|
46
|
+
enabled: true
|
|
47
|
+
# requestFile: "C:\\Users\\you\\.dsh\\strip-last.json"
|
|
48
|
+
|
|
49
|
+
# 日志(每次命中一条)
|
|
34
50
|
# debugFile: "./echo-guard.log"
|
package/lib/core.mjs
CHANGED
|
@@ -8,7 +8,10 @@
|
|
|
8
8
|
// - **删除**只删单独成句的那一个:前后都不是词的一部分才删。
|
|
9
9
|
// 这条区分是整套逻辑的关键,别简化。
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
// 默认词表:中文两个 + 英文两个。英文那两个原本是「让人自己加」,
|
|
12
|
+
// 0.2.1 起放进默认——这个包是给外面的人用的,英文思考里 OK / let me go 一样常见。
|
|
13
|
+
// 不想删哪个,从 fillers 里去掉就行(计数和删除都只认名单里的)。
|
|
14
|
+
export const DEFAULT_FILLERS = ['好', '嗯', 'OK', 'let me go']
|
|
12
15
|
|
|
13
16
|
// 跟着被删词一起带走的标点(中文、中文全角、英文常见)
|
|
14
17
|
const TRAILING_PUNCT = '[,,、。.!!??;;::.…")”’]'
|
|
@@ -53,6 +56,23 @@ export function countFillers(text, fillers = DEFAULT_FILLERS) {
|
|
|
53
56
|
return n
|
|
54
57
|
}
|
|
55
58
|
|
|
59
|
+
/**
|
|
60
|
+
* 每个词各算各的次数。
|
|
61
|
+
* 为什么分开算:中文里「好」和「嗯」的密度不一样,合成一个门槛会互相掩护——
|
|
62
|
+
* 三个「好」加三个「嗯」凑成六次,各自其实都没到线。
|
|
63
|
+
* @returns {Record<string, number>}
|
|
64
|
+
*/
|
|
65
|
+
export function countPerWord(text, fillers = DEFAULT_FILLERS) {
|
|
66
|
+
const out = {}
|
|
67
|
+
if (typeof text !== 'string' || text === '') return out
|
|
68
|
+
for (const word of fillers) {
|
|
69
|
+
if (!word) continue
|
|
70
|
+
const re = new RegExp(escapeRe(word), isCJKWord(word) ? 'g' : 'gi')
|
|
71
|
+
out[word] = (text.match(re) || []).length
|
|
72
|
+
}
|
|
73
|
+
return out
|
|
74
|
+
}
|
|
75
|
+
|
|
56
76
|
/**
|
|
57
77
|
* 只删单独成句的那些,其余原样。
|
|
58
78
|
* @returns {{text:string, removed:number, counted:number}}
|
|
@@ -86,21 +106,33 @@ export function stripFillers(text, fillers = DEFAULT_FILLERS) {
|
|
|
86
106
|
|
|
87
107
|
/**
|
|
88
108
|
* 在一段文字里找「这些词扎堆出现」的位置。
|
|
109
|
+
* 每个词按自己的门槛算(没配就用 minCount),命中哪个只算哪个,不互相凑数。
|
|
89
110
|
* @param {string} text
|
|
90
111
|
* @param {string[]} fillers
|
|
91
|
-
* @param {{minCount?:number}} [cfg]
|
|
92
|
-
* @returns {null | {firstAt:number, count:number}}
|
|
112
|
+
* @param {{minCount?:number, minCounts?:Record<string,number>}} [cfg]
|
|
113
|
+
* @returns {null | {firstAt:number, count:number, words:string[]}}
|
|
93
114
|
*/
|
|
94
115
|
export function findBurst(text, fillers = DEFAULT_FILLERS, cfg = {}) {
|
|
95
116
|
if (typeof text !== 'string' || text === '') return null
|
|
96
|
-
const
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
117
|
+
const dflt = Number.isSafeInteger(cfg.minCount) ? cfg.minCount : 5
|
|
118
|
+
const mins = cfg.minCounts && typeof cfg.minCounts === 'object' ? cfg.minCounts : {}
|
|
119
|
+
const counts = countPerWord(text, fillers)
|
|
120
|
+
const words = []
|
|
121
|
+
let count = 0
|
|
100
122
|
for (const word of fillers) {
|
|
101
123
|
if (!word) continue
|
|
124
|
+
const n = counts[word] || 0
|
|
125
|
+
const min = Number.isSafeInteger(mins[word]) ? mins[word] : dflt
|
|
126
|
+
if (n >= min) {
|
|
127
|
+
words.push(word)
|
|
128
|
+
count += n
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (words.length === 0) return null
|
|
132
|
+
let firstAt = -1
|
|
133
|
+
for (const word of words) {
|
|
102
134
|
const i = text.indexOf(word)
|
|
103
135
|
if (i >= 0 && (firstAt < 0 || i < firstAt)) firstAt = i
|
|
104
136
|
}
|
|
105
|
-
return { firstAt, count }
|
|
137
|
+
return { firstAt, count, words }
|
|
106
138
|
}
|
package/lib/index.mjs
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
import fs from 'node:fs'
|
|
16
|
-
import { stripFillers, countFillers, DEFAULT_FILLERS } from './core.mjs'
|
|
16
|
+
import { stripFillers, countFillers, findBurst, DEFAULT_FILLERS } from './core.mjs'
|
|
17
17
|
|
|
18
18
|
export const name = 'echo-guard'
|
|
19
19
|
|
|
@@ -58,6 +58,81 @@ function hasToolCall(message) {
|
|
|
58
58
|
return Array.isArray(message?.content) && message.content.some((b) => b?.type === 'tool-call')
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
/** 会话 id -> 最近的 agent 句柄。手动入口要用它:命令和门都只能拿到 id。 */
|
|
62
|
+
const LIVE_AGENTS = new Map()
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 这一轮有没有在跑、它是第几轮。
|
|
66
|
+
* 读法用 `session.seq` + `session.eventAt(i)`——这是宿主暴露的只读接口,别换成 session.events,
|
|
67
|
+
* 那个在运行时不是数组(2026-12-05 实测)。
|
|
68
|
+
* 拿不到就当作在跑、轮次未知(什么都不许动)。
|
|
69
|
+
* @returns {{live:boolean, turn:number|null, unknown:boolean}}
|
|
70
|
+
*/
|
|
71
|
+
export function activeTurn(session) {
|
|
72
|
+
const len = session?.seq
|
|
73
|
+
if (!Number.isSafeInteger(len)) return { live: true, turn: null, unknown: true }
|
|
74
|
+
for (let i = len - 1; i >= 0; i -= 1) {
|
|
75
|
+
let ev
|
|
76
|
+
try { ev = session.eventAt?.(i) } catch { ev = undefined }
|
|
77
|
+
if (ev === undefined || ev === null) continue
|
|
78
|
+
if (ev.type === 'turn/end') return { live: false, turn: null, unknown: false }
|
|
79
|
+
if (ev.type === 'turn/start') return { live: true, turn: ev.data?.turn ?? null, unknown: false }
|
|
80
|
+
}
|
|
81
|
+
return { live: false, turn: null, unknown: false }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 手动清「上一轮」:从最近往前找第一条纯思考步,删掉单独成句的收束词,其余原文照录。
|
|
86
|
+
*
|
|
87
|
+
* 只碰纯思考步(没有 tool-call 的)——带工具调用的整步替换不归这个插件管,
|
|
88
|
+
* 这也是它和 thinking-guard 能同时装的原因。
|
|
89
|
+
* 正在跑的那一轮、以及拿不到状态的时候,一律不动。
|
|
90
|
+
*
|
|
91
|
+
* @returns {{ok:true, seq:number, deleted:number, counted:number} | {ok:false, why:string}}
|
|
92
|
+
*/
|
|
93
|
+
export function stripLastTurn(agent, cfg) {
|
|
94
|
+
const session = agent?.session
|
|
95
|
+
if (session === undefined || session === null) return { ok: false, why: '这个会话拿不到' }
|
|
96
|
+
const turn = activeTurn(session)
|
|
97
|
+
if (turn.unknown) return { ok: false, why: '读不到这一轮的状态,先不动' }
|
|
98
|
+
const nodes = [...(session.surface?.nodes ?? [])]
|
|
99
|
+
for (let i = nodes.length - 1; i >= 0; i -= 1) {
|
|
100
|
+
const seq = nodes[i]
|
|
101
|
+
const event = session.eventAt(seq)
|
|
102
|
+
if (event === undefined || event === null) continue
|
|
103
|
+
if (event.type !== 'assistant/message' || event.surfaceOp !== 'append') continue
|
|
104
|
+
const message = session.deriveEventMessage(event)
|
|
105
|
+
if (message === null || !Array.isArray(message.content)) continue
|
|
106
|
+
const block = reasoningOf(message)
|
|
107
|
+
if (block === null) continue
|
|
108
|
+
const burst = findBurst(block.text, cfg.fillers, { minCount: cfg.burstMinCount, minCounts: cfg.fillerMinCounts })
|
|
109
|
+
if (burst === null) continue
|
|
110
|
+
if (hasToolCall(message)) {
|
|
111
|
+
return { ok: false, why: '这一条带工具调用,echo-guard 只清纯思考步' }
|
|
112
|
+
}
|
|
113
|
+
if (turn.live && !(Number.isSafeInteger(turn.turn) && Number.isSafeInteger(event?.data?.turn) && event.data.turn < turn.turn)) {
|
|
114
|
+
return { ok: false, why: '这一步还在跑,等它落地再清' }
|
|
115
|
+
}
|
|
116
|
+
const stripped = stripFillers(block.text, burst.words)
|
|
117
|
+
if (stripped.removed === 0) return { ok: false, why: '上一轮里没有单独成句的收束词' }
|
|
118
|
+
const speech = speechOf(message)
|
|
119
|
+
let body = cfg.receipt
|
|
120
|
+
if (stripped.text !== '') body = body ? `${body}\n\n${stripped.text}` : stripped.text
|
|
121
|
+
if (speech !== '') body = `${body}\n\n此处说出口的话(原文照录):\n${speech}`
|
|
122
|
+
session.append('user/message', {
|
|
123
|
+
id: `echo-guard-manual-${seq}-${Date.now()}`,
|
|
124
|
+
role: 'user',
|
|
125
|
+
source: { kind: 'plugin:echo-guard' },
|
|
126
|
+
content: [{ type: 'text', text: body }]
|
|
127
|
+
}, {
|
|
128
|
+
surfaceOp: { op: 'replace', startSeq: seq, endSeq: seq },
|
|
129
|
+
sourceEventSeqs: [seq]
|
|
130
|
+
})
|
|
131
|
+
return { ok: true, seq, deleted: stripped.removed, counted: stripped.counted }
|
|
132
|
+
}
|
|
133
|
+
return { ok: false, why: '上一轮里没有可清的收束词' }
|
|
134
|
+
}
|
|
135
|
+
|
|
61
136
|
export function apply(ctx, config = {}) {
|
|
62
137
|
if (typeof config.debugFile === 'string' && config.debugFile.length > 0) DEBUG_FILE = config.debugFile
|
|
63
138
|
if (config.enabled === false) return
|
|
@@ -65,13 +140,29 @@ export function apply(ctx, config = {}) {
|
|
|
65
140
|
const cfg = {
|
|
66
141
|
fillers: Array.isArray(config.fillers) && config.fillers.length ? config.fillers.map(String) : DEFAULT_FILLERS,
|
|
67
142
|
burstMinCount: Number.isSafeInteger(config.burstMinCount) ? config.burstMinCount : 5,
|
|
143
|
+
// 每个词各算各的门槛;没配的词用 burstMinCount。
|
|
144
|
+
// 例:{ "好": 5, "嗯": 3 } —— 好 够 5 次才算,嗯 够 3 次就算,不互相凑。
|
|
145
|
+
// 默认给英文两个也配了:OK 跟「好」同级,"let me go" 这种整句短语门槛低一档。
|
|
146
|
+
fillerMinCounts: {
|
|
147
|
+
好: 5,
|
|
148
|
+
嗯: 3,
|
|
149
|
+
OK: 5,
|
|
150
|
+
'let me go': 3,
|
|
151
|
+
...(config.fillerMinCounts && typeof config.fillerMinCounts === 'object' ? config.fillerMinCounts : {})
|
|
152
|
+
},
|
|
68
153
|
receipt: typeof config.receipt === 'string' ? config.receipt : DEFAULT_RECEIPT,
|
|
69
154
|
// 删完剩不到两成就放弃:整段都是收束词时删光只会剩个空块,不如留着。
|
|
70
155
|
stripMinKeepRatio: typeof config.stripMinKeepRatio === 'number' ? config.stripMinKeepRatio : 0.2,
|
|
71
156
|
// 只对这些会话生效(空 = 全部)。填会话 id 片段。
|
|
72
157
|
onlySessions: Array.isArray(config.onlySessions) ? config.onlySessions.map(String) : [],
|
|
73
158
|
includeSubagents: config.includeSubagents !== false,
|
|
74
|
-
dryRun: config.dryRun === true
|
|
159
|
+
dryRun: config.dryRun === true,
|
|
160
|
+
// 手动「清上一轮」(0.2.0 起):命令 + 请求文件。
|
|
161
|
+
// requestFile 留空 = 不监听请求文件,只留斜杠命令。
|
|
162
|
+
manual: {
|
|
163
|
+
enabled: config.manual?.enabled !== false,
|
|
164
|
+
requestFile: typeof config.manual?.requestFile === 'string' ? config.manual.requestFile : ''
|
|
165
|
+
}
|
|
75
166
|
}
|
|
76
167
|
|
|
77
168
|
const state = new WeakMap() // session -> { seen: Set<seq> }
|
|
@@ -85,6 +176,7 @@ export function apply(ctx, config = {}) {
|
|
|
85
176
|
const rawId = String(session.id ?? '')
|
|
86
177
|
const inList = cfg.onlySessions.length === 0 || cfg.onlySessions.some((p) => rawId.includes(p))
|
|
87
178
|
if (!inList) return next()
|
|
179
|
+
LIVE_AGENTS.set(rawId, agent)
|
|
88
180
|
|
|
89
181
|
let s = state.get(session)
|
|
90
182
|
if (s === undefined) {
|
|
@@ -123,13 +215,17 @@ export function apply(ctx, config = {}) {
|
|
|
123
215
|
continue
|
|
124
216
|
}
|
|
125
217
|
|
|
126
|
-
const
|
|
127
|
-
|
|
218
|
+
const burst = findBurst(block.text, cfg.fillers, {
|
|
219
|
+
minCount: cfg.burstMinCount,
|
|
220
|
+
minCounts: cfg.fillerMinCounts
|
|
221
|
+
})
|
|
222
|
+
if (burst === null) {
|
|
128
223
|
s.seen.add(seq)
|
|
129
224
|
continue
|
|
130
225
|
}
|
|
226
|
+
const counted = burst.count
|
|
131
227
|
|
|
132
|
-
const stripped = stripFillers(block.text,
|
|
228
|
+
const stripped = stripFillers(block.text, burst.words)
|
|
133
229
|
if (stripped.text !== '' && stripped.text.length < block.text.length * cfg.stripMinKeepRatio) {
|
|
134
230
|
s.seen.add(seq)
|
|
135
231
|
dbg(`跳过 seq ${seq}:删完只剩 ${stripped.text.length}/${block.text.length}`)
|
|
@@ -173,4 +269,57 @@ export function apply(ctx, config = {}) {
|
|
|
173
269
|
}
|
|
174
270
|
return next()
|
|
175
271
|
})
|
|
272
|
+
|
|
273
|
+
// ── 手动「清上一轮」(0.2.0)──────────────────────────────────────────
|
|
274
|
+
// 两个入口,同一个动作:
|
|
275
|
+
// ① DSH 里打 `/strip-last`;
|
|
276
|
+
// ② 别的进程(比如手机上的门)写一个请求文件,插件读它、清完把结果写回去。
|
|
277
|
+
// 手动不看次数门槛;只碰纯思考步;正在跑的那一轮不动。
|
|
278
|
+
if (cfg.manual.enabled) {
|
|
279
|
+
try {
|
|
280
|
+
ctx.commands?.register?.({
|
|
281
|
+
name: 'strip-last',
|
|
282
|
+
description: '清掉上一轮纯思考步里单独成句的收束词,其余原文照录',
|
|
283
|
+
handler: ({ agent }) => {
|
|
284
|
+
const r = stripLastTurn(agent, cfg)
|
|
285
|
+
if (!r.ok) return { kind: 'error', text: r.why }
|
|
286
|
+
return { kind: 'success', text: `清掉 ${r.deleted} 个(seq ${r.seq}),其余原文照录。原文仍在会话日志里。` }
|
|
287
|
+
}
|
|
288
|
+
})
|
|
289
|
+
dbg('已注册斜杠命令 /strip-last')
|
|
290
|
+
} catch (error) {
|
|
291
|
+
dbg(`注册命令失败:${String(error)}`)
|
|
292
|
+
ctx.logger?.warn?.(`[${name}] 注册 /strip-last 失败:${String(error)}`)
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const requestFile = cfg.manual.requestFile
|
|
296
|
+
if (typeof requestFile === 'string' && requestFile.length > 0) {
|
|
297
|
+
const resultFile = requestFile.replace(/\.json$/i, '.result.json')
|
|
298
|
+
ctx.effect?.(() => {
|
|
299
|
+
const timer = setInterval(() => {
|
|
300
|
+
try {
|
|
301
|
+
if (!fs.existsSync(requestFile)) return
|
|
302
|
+
// 请求文件可能是别的进程写的,先去掉 BOM(PowerShell 的 UTF-8 带 BOM)。
|
|
303
|
+
const raw = fs.readFileSync(requestFile, 'utf8').replace(/^\uFEFF/, '')
|
|
304
|
+
let request = null
|
|
305
|
+
let parseError = null
|
|
306
|
+
try { request = JSON.parse(raw) } catch (error) { parseError = error }
|
|
307
|
+
try { fs.unlinkSync(requestFile) } catch { /* 被别的进程抢先删了也无所谓 */ }
|
|
308
|
+
if (parseError !== null) {
|
|
309
|
+
fs.writeFileSync(resultFile, JSON.stringify({ at: new Date().toISOString(), ok: false, why: `请求文件格式不对:${String(parseError)}` }), 'utf8')
|
|
310
|
+
return
|
|
311
|
+
}
|
|
312
|
+
const agent = LIVE_AGENTS.get(String(request?.sessionId ?? ''))
|
|
313
|
+
const result = agent ? stripLastTurn(agent, cfg) : { ok: false, why: '这个会话还没跑过一轮(插件手里没有它的句柄)' }
|
|
314
|
+
fs.writeFileSync(resultFile, JSON.stringify({ at: new Date().toISOString(), ...result }), 'utf8')
|
|
315
|
+
dbg(`请求文件:${JSON.stringify(result)}`)
|
|
316
|
+
} catch (error) {
|
|
317
|
+
dbg(`请求文件处理失败:${String(error)}`)
|
|
318
|
+
try { fs.writeFileSync(resultFile, JSON.stringify({ at: new Date().toISOString(), ok: false, why: String(error) }), 'utf8') } catch { /* 结果写不进去就只能丢 */ }
|
|
319
|
+
}
|
|
320
|
+
}, 800)
|
|
321
|
+
return () => clearInterval(timer)
|
|
322
|
+
})
|
|
323
|
+
}
|
|
324
|
+
}
|
|
176
325
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-echo-guard",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Strips filler-word bursts out of the model-visible reasoning history — only standalone occurrences, everything else verbatim. DSH plugin.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.mjs",
|