dsh-repeat-guard 0.1.3 → 0.1.5

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.md CHANGED
@@ -1,166 +1,100 @@
1
1
  # dsh-repeat-guard
2
2
 
3
- 拦截思考段的"复读"退化:模型在思考里陷入反复输出"好。""执行。"这类碎片空话时,掐断本次生成,并让本轮继续往下跑,而不是停下来等用户输入。
4
-
5
- | 项 | 说明 |
6
- |---|---|
7
- | 类型 | dsh bundle,含宿主侧插件与客户端设置页 |
8
- | 监听 | `llm/stream`、`agent/turn-stopping` |
9
- | 入口 | `lib/index.js`(`src/index.ts`)、`lib/client.js`(`src/client.tsx`) |
10
- | 可调项 | 拦截短句表、连续命中阈值(设置 → 复读打断) |
11
- | 分发 | npm 包 `dsh-repeat-guard` |
12
-
13
- ---
14
-
15
- ## 它解决什么问题
16
-
17
- 模型退化时,思考段里会出现整行都是空话的情况,如"好。""执行。""OK." "Let me go."。
18
-
19
- 这类思考本身不含信息,但会一直持续到 token 耗尽,把一次本可以正常完成的任务拖垮。
20
-
21
- ## 检测范围
22
-
23
- **只看思考段(`reasoning-delta`),正文(`text-delta`)不参与判定,原样透传。**
24
-
25
- 复读退化先出现在思考段,那时正文往往还没开始产出;等正文也碎掉再掐,思考段已经白烧了一遍。
26
-
27
- ## 判据:查表
28
-
29
- 判定不做任何"形状归纳",只查一张表:**思考里有没有独占一行的表项**。
30
-
31
- 最初试过两条归纳式判据,都被证伪:
32
-
33
- 1. **数碎片重复了几次**("好,好,好,好")。它不看句子结构,只看重复次数,于是"然后,然后,然后,"(逗号被当作片段分隔符)这类正常句子会被判成复读。
34
- 2. **按形状判断空话行**(中文去标点后 ≤2 字、英文 ≤3 词)。形状规则必然外溢:"解。""豫。"这种被换行切开的半截词也满足形状,实测在大段正常思考里频繁误杀。
35
-
36
- 表项是具体的、可增删的、可审计的,不会外溢。规则只有一条:
3
+ > 解决大模型用你的token唱歌的糟糕问题
37
4
 
5
+ 我们发现部分模型(尤其是`deepseek v4.1 flash`)很容易在长上下文时进入复读状态。
6
+ dsh社区中称呼这种情况为“唱歌”,典型的表现如下所示:
38
7
  ```
39
- 整行去掉首尾空白 → 转小写 → 查表
8
+ Let me do it.
9
+ Let me run.
10
+ Let me go.
11
+ Let me do it.
12
+ ...
40
13
  ```
41
-
42
- | 输入 | 结果 |
43
- |---|---|
44
- | `好。` `好的。` `执行。` `写。` `明白。` | 拦(表内) |
45
- | `OK.` `Sure.` `Let me go.` `Got it.` | 拦(表内) |
46
- | `查一下。` `这需要我。` | 不拦(表外) |
47
- | `A.` `1.` | 不拦(表外) |
48
- | `然后,` `注意:` `好的,` | 不拦(表外) |
49
- | `好?` `好!` `好` `OK` | 不拦(表外) |
50
- | `我需要检查实现。\n然后重新编译。` | 不拦(各行都在表外) |
51
-
52
- 表项**连标点一起写**(`'好的。'` 对应思考里的 `好的。`),英文条目一律写小写。分割单位是单个换行,空行会把连续计数打断。
53
-
54
- **表可以在设置里改**(见下面「可调项」);`src/config.ts` 的 `DEFAULT_FRAGMENTS` 是出厂默认值。
55
-
56
- 已知边界:判据只看"有没有这样一行",不看上下文。所以"用户想让我改代码。\n好的。"这种**前面有实质内容、结尾又跟一句空话**的思考也会被拦。这是"按行判定"的直接结果。
57
-
58
- ## 连续命中阈值
59
-
60
- 阈值 `threshold` 是"连着几行命中才拦":
61
-
62
- | 值 | 行为 |
63
- |---|---|
64
- | 1(默认) | 任意一行命中即拦 |
65
- | n | 连着 n 行都是表项(**句子可以各不相同**)才拦;中间夹一行不命中的就重新计数 |
66
-
67
- 阈值只在**同一次生成内**计数,不跨轮次。
68
-
69
- ## 命中在思考段末尾时不拦
70
-
71
- 命中之后,插件再往下探一格:
72
-
73
- - 后面还是 `reasoning-delta` → 思考确实在继续复读,**拦**;
74
- - 后面换成正文、工具调用,或直接收尾(`block-end` / `usage` / `finish` / 流结束)→ 说明命中行本来就是这段思考的最后一句,**不拦**,原样透传。
75
-
76
- 判据是**思考段是否还在往下写**,不是整个响应是否结束——思考以一句"好。"收尾、接着写正文,属正常收尾。
77
-
78
- ## 工作原理
79
-
80
- 1. 包一层 `llm/stream`(模型调用的流式 waterfall),逐 chunk 观察 `reasoning-delta`,累积本次响应已产出的思考文本。
81
- 2. 命中退化时,把触发退化的那个 delta 照常放行,补一个 `block-end` 闭合思考块,再补一个 `finish(stop)`,然后结束流。对 agent-loop 而言这就是一次正常完成:已生成的思考照常落盘,本轮该走什么流程就走什么流程。
82
- 3. 提前结束流时显式向下游传播 `iterator.return()`,否则上游 HTTP 流会继续跑到结束,供应商照常计满 token,且连接悬挂。
83
- 4. 光截断只会让本轮就此结束、停下来等用户输入。所以再挂一个 `agent/turn-stopping` 监听器,在本轮边界提交之前调 `agent.steer(...)` 推一条输入,本轮就会接着再跑一步。
84
-
85
- 辅助调用(上下文压缩、会话标题等带 `purpose` 的调用)不参与检测。
86
-
87
- 三种结局各有日志,便于事后核对误杀:
88
-
14
+ 如果在预设或者`AGENTS.md`中约束使用中文思考,则会变成如下的场景:
89
15
  ```
90
- [repeat-guard] 检出思考段复读,已掐断本次生成 | 会话=… | 命中行="好的。"
91
- [repeat-guard] 命中行位于思考段末尾,未拦截 | 会话=… | 命中行="好的。"
92
- [repeat-guard] turn-stopping:会话 … 续跑一步
16
+ 好。
17
+ 做。
18
+ 开始。
19
+ 做。
20
+ 嗯。
21
+ 做。
22
+ 我必须开始工作了。
23
+ 好。
24
+ 做。
25
+ ...
93
26
  ```
27
+ 配合`deepseek v4.1 flash`高达 300tok/s 的恐怖输出速率,模型可能会以极为可观的速度消耗token,而不产生任何有效输出。
94
28
 
95
- ## 掐断之后怎么让本轮继续
29
+ 这通常是比较隐蔽的,尤其是在用户没有注意模型的思考过程的时候。用户实际上只会看到思考过程快速闪过很短的句子,回答好像很正常。
96
30
 
97
- dsh 的 turn 循环(`dsh-agent-loop/lib/index.js:966-973`):
31
+ 在上下文长度达到 300k 以上的时候,该现象会变得十分频繁。
98
32
 
99
- ```js
100
- if (turnEnds && this.inbox.nextStep.length === 0) {
101
- await this.dispatch.serial("agent/turn-stopping", { turn, signal });
102
- }
103
- if (turnEnds && this.inbox.nextStep.length === 0) break; // 重读收件箱
33
+ 不仅如此,我们也观察到`deepseek v4.1 flash`有着在已经思考到需要工具调用的时候输出一个短句,接下来又进入另一个方向的复杂思考过程的倾向。如下所示:
34
+ ```
35
+ 我已经收集到了足够的信息,接下来开始操作。先修改xxx文件。
36
+ 做。
37
+ 但是我还发现仓库里有其它的内容。注意到用户提到xxxxx
38
+ ...
104
39
  ```
105
40
 
106
- `agent/turn-stopping` 在 `break` 之前被 `await`,之后收件箱会被**重新读一次**。所以只要监听器往 `inbox.nextStep` 里推入东西,本轮就不 break,而是再跑一步。官方对该事件的说明也写明了这个用法,先例见 `dsh-hooks-claude-code/lib/index.js:300`。
41
+ 显然这不是我们想要的行为。既然模型已经清楚下一步的工作是什么,就应该直接开始工具调用。
107
42
 
108
- 注意:
43
+ 我们还注意到上述问题只出现于思考过程而不出现于回答内容。
44
+ ## 此插件的功能
109
45
 
110
- - **做不到"这一步当作没发生"。** 被截断的 assistant 消息在 `step()` 里已经 `session.append` 落盘,dsh 没有回滚一步的机制;`step()` 唯一返回 `null`(循环继续)的出口要求本步真的产生了 tool-call,插件够不着。所以这里的语义是"接着再跑一步",会话里会留下被截断的思考记录。
111
- - **续跑没有次数配额。** 有标记就推,同一轮里反复退化就反复续跑。
46
+ 此插件致力于解决上述模型复读和无端长思考的问题。它会持续检测模型的输出,如果思考过程出现了一个**白名单**中的短句且单独成段,且模型在输出这个短句后既然有继续思考的意图,我们就会强行中断对话,并发送一句提醒,让模型直接开始下一轮的输出。
112
47
 
113
- ## 可调项
48
+ 例如
49
+ ```
50
+ [深度思考]
51
+ 直接检查服务器状态和崩溃日志。
114
52
 
115
- 在 **设置 → 复读打断** 里改,保存后宿主侧立即按新配置判定,不用重启。
53
+ 关键:先看最新日志和 crash-reports。
116
54
 
117
- | 项 | 含义 |
118
- |---|---|
119
- | 拦截短句 | 判为复读的空话短句表,一行一句,连标点一起写 |
120
- | 连续命中次数 | 连着几行命中才拦;默认 1 = 发现即拦 |
55
+ 执行。 <--打断
121
56
 
122
- 配置存在 dsh 的 settings 服务里(命名空间 `repeat-guard`),落盘位置由 dsh 决定。
57
+ [复读拦截]
58
+ 你上一段思考退化成了碎片复读(反复输出"好。"一类的空话),已被系统截断。
59
+ 请直接继续执行下一步,不要再输出任何确认语、寒暄或空话。
123
60
 
124
- ## 续跑时推给模型的输入
61
+ [深度思考]
62
+ 直接调用工具,检查服务器状态和崩溃日志。
125
63
 
126
- ```
127
- [复读拦截]
128
- 你上一段思考退化成了碎片复读(反复输出"好。"一类的空话),已被系统截断。
129
- 请直接继续执行下一步,不要再输出任何确认语、寒暄或空话。
64
+ [tool calls]
65
+ ssh -o ConnectTimeout=10 -o BatchMode=yes ...
66
+ ...
130
67
  ```
131
68
 
132
- 这条消息带 `source.kind = 'plugin'`,客户端会渲染成折叠的 context 行,不是用户气泡。
69
+ ## 工作原理
133
70
 
134
- ## 构建
71
+ 逐 chunk 观察 `reasoning-delta`,检查思考文本。
135
72
 
136
- ```bash
137
- npm install
138
- npm run build # tsc 产出 lib/*.js,再用 esbuild 打 lib/client.js
139
- ```
73
+ 独立段命中白名单时,向模型传递中断请求,掐断模型输出的片段,并补充结束标志,伪装成正常结束的思考过程。
140
74
 
141
- 两步各有各的产物:
75
+ 向模型发送一条注入提示词,以恢复`agent-loop`。
142
76
 
143
- - `tsc` 编译 `src/*.ts` → `lib/*.js`,宿主侧入口就是 `lib/index.js`(真正被 dsh import 的部分)。
144
- - `scripts/build-client.mjs` 用 esbuild 把 `src/client.tsx` 打成**单文件** `lib/client.js`,外面套一层 dsh 客户端模块系统要求的包装:
77
+ 用户会通过注入提示词这个步骤看到大模型被中断的过程,因此步骤可能会变得很多。这是不得不付出的代价。
145
78
 
146
- ```js
147
- window.__ModuleLoader__.load({ id: "dsh-repeat-guard", factory: function (require) { … } });
148
- ```
79
+ > 虽然我们可以将下一步思考的过程接到上一轮打断的位置以伪装成什么都没发生过的样子,但是一方面这种做法有风险,另一方面不是很有利于缓存命中。更何况,这也能帮助用户判断插件是否在正常工作。
149
80
 
150
- `id` 必须是包名,`factory` 的返回值就是该包的客户端模块导出。`react` 等平台基座模块由模块系统提供,构建时设为 external,运行时从 `factory` 的 `require` 参数取。
81
+ ## 可配置选项
151
82
 
152
- `lib/` **不进版本库**:`npm publish` 前由 `prepack` 钩子现构建,随包发布(`files` 字段已含 `lib`)。这样"改了 `src/` 忘了 build"不会让旧产物静默跟着提交——代价是构建失败时发不出去,这是有意的。
83
+ 在 **设置 → 复读打断** 里查看,保存后生效,无需重启。
153
84
 
154
- ## 发布
85
+ | 项 | 含义 |
86
+ |---|---|
87
+ | 累计拦截 | 掐断次数(有清零按钮) |
88
+ | 拦截短句 | 触发打断的白名单,需要加上标点 |
89
+ | 连续命中次数\* | 连续命中的阈值,如果为2就需要连续出现两个短句才触发 |
90
+ | 行内重复检测\* | 打开后"好。好。好。"这种挤在一行里的重复也算命中。 |
91
+ | 续跑指令 | 截断后推给模型的提示词 |
92
+ | 折叠行摘要 | 注入段用户可见的简要信息 |
155
93
 
156
- ```bash
157
- npm login --registry=https://registry.npmjs.org
158
- npm publish --registry=https://registry.npmjs.org
159
- ```
94
+ > \* 连续的意思是中间没有长段。如果配置成2就只会拦截循环,默认配置连一个短句后的分支思考过程也会拦下。
160
95
 
161
- **两条命令都别省 `--registry`。** npm 的凭据是**按源绑定**的:`npm login` 登的是哪个源,`~/.npmrc` 里就只在那个源下记一条 `//registry.npmjs.org/:_authToken`。所以 `npm config get registry` 一旦被切到 npmmirror 这类只读镜像(它本身也发不上去),不带参数的 `npm publish` 会直接报 `need auth`。
96
+ > \*deepseek不存在行内重复问题,这个选项是为 qwen 35B 这样的模型准备的。
162
97
 
163
- 发布前不必手动构建,`prepack` 会跑一次 `npm run build`;tsc 或 esbuild 报错则发布中止。
164
98
 
165
99
  ## 安装到 dsh
166
100
 
@@ -168,71 +102,6 @@ npm publish --registry=https://registry.npmjs.org
168
102
  dsh plugin --profile web add dsh-repeat-guard
169
103
  ```
170
104
 
171
- 本插件是一个 **dsh bundle**——包根带 `cordis.patch.yml`,由 `package.json` 的 `dsh.bundle.patch` 声明。装进 profile 时它会自动进入 `dsh.profile.bundles` 分层栈,**不需要改 profile 自己的 `cordis.patch.yml`**。客户端半靠 `package.json` 的 `dsh.client` 声明被自动发现,同样不用改 profile。
172
-
173
- 装完**必须重启 dsh 进程**。`patchReload: live` 只重载已有的层,不会加载新增的层——实测编辑后运行中的进程既不加载也不报错。
174
-
175
- 机制(dsh 源码):`dsh plugin` 是 pnpm 转发器(`dsh/lib/plugin-*.js`),在 profile 目录跑 `pnpm add` 后按**安装后的真实包名**调和 `dsh.profile.bundles`,判定条件就是 `dsh.bundle.patch` 是否为 undefined(没有就印一句 "declares no dsh.bundle — installed as a plain dependency, not a profile layer")。加载时 `dsh-app-boot` 的 `loadProfileDirectory` 对该字段做 `join(packageDir, declared)` 并当 overlay patch 读;声明缺失直接抛错。
176
-
177
- **只走 npm 分发。** git 安装(`add github:...`)会让 pnpm 拦下依赖的构建脚本,装出来的包没有 `lib/index.js`,除非安装者手工去 profile 的 `pnpm-workspace.yaml` 加 `allowBuilds` 白名单——那等于把本机的构建配置摊派给每个使用者。本地目录安装同理,只适合开发调试,不作为分发方式。
178
-
179
- 验证是否加载:
180
-
181
- ```bash
182
- journalctl --user -u dsh-web --no-pager | grep -a repeat-guard
183
- # [repeat-guard] 已加载,复读拦截生效
184
- ```
185
-
186
- ⚠️ 插件加载失败会让 **dsh 启动直接失败**,而 dsh-web 是本机 GUI 的唯一通道。改完先离线验证再重启:用假上下文 import 产物调一次入口,或喂一段真实 chunk 序列。
187
-
188
- ## 依赖
189
-
190
- dsh 会把 bundle 声明的 `dependencies` 与 `peerDependencies` 从安装目录软链进 profile(`dsh-app-boot` 的 `healProfileModuleFallback`,依赖名取自 `profileDependencyNames(manifest)`,注释原文是 "dependency names that may be imported by a loader-visible plugin")。软链落在 `~/.dsh/profiles/node_modules/`,Node 按常规向上查找即可解析到。
191
-
192
- dsh 的 profile 同时带一份 `.npmrc`,写着 `auto-install-peers=false`(注释:core packages come from the CLI dependency tree; a profile must never resolve its own copy)——所以声明 `peerDependencies` 不会让 profile 自己再装一份宿主包。
193
-
194
- 所以:
195
-
196
- - **宿主提供的包写 `peerDependencies`**:`@deepseek-ai/cordis`、`dsh-llm`、`dsh-agent`、`dsh-settings`、`schemastery`、`dsh-client-ui-renderer`、`dsh-client-ui-settings`,以及客户端侧由平台基座提供的 `react`。它们不在 profile 里重复安装,用 dsh 自带的那份。
197
- - **类型一律从官方引**,不在本地重抄。`Context`、`StreamChunk`、`GenerateOptions`、`Agent`、`SettingsScope` 都是 dsh 导出的;本地抄一份只会在 dsh 升级后于运行时暴露字段对不上,官方声明则会在 `tsc` 阶段直接报错。
198
- - `src/types.ts` 只放本插件自有的类型(当前是 `GuardState`)。
199
- - 部分 dsh 包的类型只通过 module augmentation 生效(如 `Context.settings`、`Context.slots`、`Context.settingsScope`),要显式 `import type {} from '…'` 触发加载,否则 `tsc` 会报"属性不存在"。
200
- - dsh 的 loader 是原生 ESM import,**没有转译层**,运行时读到的永远是编译产物——改完 `src/` 必须重新构建。`lib/` 只随 npm 包发布,不进版本库。
201
-
202
- ## 代码约定
203
-
204
- | 项 | 约定 |
205
- |---|---|
206
- | 缩进 / 引号 / 分号 | 2 空格、单引号、语句末分号 |
207
- | 行宽 | 目标 100 字符,上限 120(中英混排按字符数计) |
208
- | 控制语句 | **一律带花括号**,单行 `if`、`continue`、`break` 也不例外,不写 `if (x) return;` |
209
- | 文件 | 一个文件只干一件事:类型声明 / 配置 / 判定 / 续跑 / 单个监听器 / 装配,各占一个文件 |
210
- | 函数 | 不超过 50 行;超了就先看能不能按职责拆开 |
211
- | 注释 | 一律中文;导出函数与判定函数配 `@param` / `@returns`,文件内小工具函数用单行注释即可 |
212
-
213
- 排版细节已固化在 `.editorconfig`。花括号、文件职责、函数行数这三条没有配置项可表达,只能靠人工遵守。
214
-
215
- ## 目录结构
216
-
217
- ```
218
- .
219
- ├── src/
220
- │ ├── index.ts 宿主侧入口:装配状态,注册两个监听器(只做装配)
221
- │ ├── types.ts 本插件自有的类型(dsh 的接口一律从 @deepseek-ai/* 引)
222
- │ ├── config.ts 配置:settings 命名空间、默认值、schema、取当前值
223
- │ ├── detect.ts 复读判定:查表 + 连续计数,不碰会话状态
224
- │ ├── resume.ts 续跑:注入文案、消息构造、推送
225
- │ ├── stream-guard.ts llm/stream 监听器:思考段检测与掐断
226
- │ ├── turn-stopping-guard.ts agent/turn-stopping 监听器:让本轮继续
227
- │ └── client.tsx 客户端设置页:注册 settings.section
228
- ├── scripts/
229
- │ └── build-client.mjs esbuild 打包客户端半 → lib/client.js
230
- ├── lib/ 构建产物(已 gitignore),dsh 加载 lib/index.js 与 lib/client.js
231
- ├── cordis.patch.yml bundle 层声明:把本插件挂进 profile 树
232
- ├── package.json
233
- └── tsconfig.json
234
- ```
235
-
236
105
  ## 许可
237
106
 
238
107
  MIT,见 `LICENSE`。
package/lib/client.js CHANGED
@@ -36,6 +36,7 @@ var __dshClientBundle = (() => {
36
36
  var SETTINGS_NS = "repeat-guard";
37
37
  var THRESHOLD_MIN = 1;
38
38
  var THRESHOLD_MAX = 9;
39
+ var FIELD = `height:36px;box-sizing:border-box;padding:0 12px;border:1px solid var(--dsw-alias-border-l2);border-radius:12px;background:transparent;color:var(--dsw-alias-label-primary);font-family:inherit;font-size:14px;line-height:22px`;
39
40
  var CSS = `
40
41
  .rg-root{display:flex;flex-direction:column;width:100%}
41
42
  .rg-head{display:flex;align-items:center;gap:12px;padding:2px 0 14px}
@@ -57,6 +58,11 @@ var __dshClientBundle = (() => {
57
58
  .rg-arrow{display:inline-flex;justify-content:center;align-items:center;width:17px;height:12px;padding:0;border:none;border-radius:3px;cursor:pointer;color:var(--dsw-alias-label-primary);background:color-mix(in srgb, var(--dsw-alias-bg-layer-1) 75%, transparent)}
58
59
  .rg-arrow:hover:not(:disabled){background:var(--dsw-alias-bg-layer-1)}
59
60
  .rg-arrow:disabled{color:var(--dsw-alias-label-caption);cursor:default}
61
+ .rg-switch{position:relative;flex:none;width:40px;height:24px;padding:0;border:none;border-radius:12px;cursor:pointer;background:var(--dsw-alias-bg-module-platform);transition:background .15s ease}
62
+ .rg-switch::after{content:"";position:absolute;top:3px;left:3px;width:18px;height:18px;border-radius:50%;background:var(--dsw-alias-label-primary-foreground);transition:transform .15s ease}
63
+ .rg-switch[aria-checked="true"]{background:var(--dsw-alias-brand-primary)}
64
+ .rg-switch[aria-checked="true"]::after{transform:translateX(16px)}
65
+ .rg-count{display:inline-flex;align-items:center;justify-content:center;min-width:72px;height:36px;padding:0 14px;box-sizing:border-box;border-radius:18px;background:var(--dsw-alias-bg-module-platform);color:var(--dsw-alias-label-primary);font-size:14px;line-height:22px;font-variant-numeric:tabular-nums}
60
66
  .rg-list{display:flex;flex-direction:column;gap:2px;width:100%;max-height:300px;overflow-y:auto;padding:4px;border-radius:12px;background:var(--dsw-alias-bg-module-platform);box-sizing:border-box}
61
67
  .rg-item{display:flex;align-items:center;gap:8px;padding:5px 6px 5px 12px;border-radius:8px;font-size:14px;line-height:22px;color:var(--dsw-alias-label-primary)}
62
68
  .rg-item:hover{background:var(--dsw-alias-interactive-bg-hover)}
@@ -65,8 +71,10 @@ var __dshClientBundle = (() => {
65
71
  .rg-remove:hover{background:var(--dsw-alias-interactive-bg-hover-danger);color:var(--dsw-alias-state-error-primary)}
66
72
  .rg-empty{padding:10px 12px;color:var(--dsw-alias-label-caption);font-size:13px;line-height:20px}
67
73
  .rg-addRow{display:flex;align-items:center;gap:8px;width:100%}
68
- .rg-input{flex:1;min-width:0;height:36px;box-sizing:border-box;padding:0 12px;border:1px solid var(--dsw-alias-border-l2);border-radius:12px;background:transparent;color:var(--dsw-alias-label-primary);font-family:inherit;font-size:14px;line-height:22px}
69
- .rg-input:focus{outline:none;border-color:var(--dsw-alias-border-l4)}
74
+ .rg-input{flex:1;min-width:0;${FIELD}}
75
+ .rg-summaryInput{flex:none;width:260px;${FIELD}}
76
+ .rg-textarea{width:100%;min-height:84px;box-sizing:border-box;padding:10px 12px;border:1px solid var(--dsw-alias-border-l2);border-radius:12px;background:transparent;color:var(--dsw-alias-label-primary);font-family:inherit;font-size:14px;line-height:22px;resize:vertical}
77
+ .rg-input:focus,.rg-summaryInput:focus,.rg-textarea:focus{outline:none;border-color:var(--dsw-alias-border-l4)}
70
78
  .rg-button{cursor:pointer;flex:none;height:32px;padding:0 14px;border:none;border-radius:16px;background:var(--dsw-alias-interactive-bg-hover);color:var(--dsw-alias-label-primary);font-family:inherit;font-size:14px;line-height:22px}
71
79
  .rg-button:hover{background:var(--dsw-alias-interactive-bg-active)}
72
80
  `;
@@ -137,6 +145,21 @@ var __dshClientBundle = (() => {
137
145
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "rg-stepperUnit", children: unit })
138
146
  ] });
139
147
  }
148
+ function Switch({ checked, label, onChange }) {
149
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
150
+ "button",
151
+ {
152
+ type: "button",
153
+ role: "switch",
154
+ "aria-checked": checked,
155
+ "aria-label": label,
156
+ className: "rg-switch",
157
+ onClick: () => {
158
+ onChange(!checked);
159
+ }
160
+ }
161
+ );
162
+ }
140
163
  function FragmentList({ items, draft, onDraft, onAdd, onRemove }) {
141
164
  function handleKey(event) {
142
165
  if (event.key === "Enter") {
@@ -189,9 +212,12 @@ var __dshClientBundle = (() => {
189
212
  ] })
190
213
  ] });
191
214
  }
192
- function Form({ initial, onSave }) {
193
- const [items, setItems] = (0, import_react.useState)(() => initial.fragments ?? []);
194
- const [count, setCount] = (0, import_react.useState)(() => initial.threshold ?? 1);
215
+ function Form({ initial, count, onReset, onSave }) {
216
+ const [items, setItems] = (0, import_react.useState)(initial.fragments);
217
+ const [limit, setLimit] = (0, import_react.useState)(initial.threshold);
218
+ const [inlineRepeat, setInlineRepeat] = (0, import_react.useState)(initial.inlineRepeat);
219
+ const [resumeText, setResumeText] = (0, import_react.useState)(initial.resumeText);
220
+ const [resumeSummary, setResumeSummary] = (0, import_react.useState)(initial.resumeSummary);
195
221
  const [draft, setDraft] = (0, import_react.useState)("");
196
222
  const [note, setNote] = (0, import_react.useState)("");
197
223
  function add() {
@@ -209,33 +235,77 @@ var __dshClientBundle = (() => {
209
235
  }
210
236
  function save() {
211
237
  setNote("\u4FDD\u5B58\u4E2D\u2026");
212
- onSave(items, count).then(() => {
238
+ onSave({
239
+ fragments: [...items],
240
+ threshold: limit,
241
+ inlineRepeat,
242
+ resumeText,
243
+ resumeSummary,
244
+ count
245
+ }).then(() => {
213
246
  setNote("\u5DF2\u4FDD\u5B58");
214
247
  }).catch((error) => {
215
248
  setNote(`\u4FDD\u5B58\u5931\u8D25\uFF1A${String(error)}`);
216
249
  });
217
250
  }
251
+ function reset() {
252
+ onReset().then(() => {
253
+ setNote("\u8BA1\u6570\u5DF2\u6E05\u96F6");
254
+ }).catch((error) => {
255
+ setNote(`\u6E05\u96F6\u5931\u8D25\uFF1A${String(error)}`);
256
+ });
257
+ }
218
258
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "rg-root", children: [
219
259
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "rg-head", children: [
220
260
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "rg-headTitle", children: "\u590D\u8BFB\u6253\u65AD" }),
221
261
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "rg-headNote", children: note }),
222
262
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", className: "rg-save", onClick: save, children: "\u4FDD\u5B58" })
223
263
  ] }),
264
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "rg-row", children: [
265
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "rg-rowText", children: [
266
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "rg-rowTitle", children: "\u7D2F\u8BA1\u62E6\u622A" }),
267
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "rg-rowDesc", children: "\u91CD\u590D\u6253\u65AD\u8BA1\u6570" })
268
+ ] }),
269
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { className: "rg-control", children: [
270
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { className: "rg-count", children: [
271
+ count,
272
+ " \u6B21"
273
+ ] }),
274
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", className: "rg-button", onClick: reset, children: "\u6E05\u96F6" })
275
+ ] })
276
+ ] }),
224
277
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "rg-row", children: [
225
278
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "rg-rowText", children: [
226
279
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "rg-rowTitle", children: "\u8FDE\u7EED\u547D\u4E2D\u6B21\u6570" }),
227
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "rg-rowDesc", children: "\u8FDE\u7740\u51E0\u4E2A\u77ED\u53E5\u884C\u547D\u4E2D\u624D\u62E6\u622A\uFF1B\u586B 1 \u8868\u793A\u53D1\u73B0\u5373\u62E6" })
280
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "rg-rowDesc", children: "\u5FAA\u73AF\u6B21\u6570\u4E0A\u9650\uFF1B\u586B 1 \u8868\u793A\u53D1\u73B0\u5373\u62E6" })
228
281
  ] }),
229
282
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
230
283
  Stepper,
231
284
  {
232
- value: count,
285
+ value: limit,
233
286
  min: THRESHOLD_MIN,
234
287
  max: THRESHOLD_MAX,
235
288
  unit: "\u6B21",
236
289
  label: "\u8FDE\u7EED\u547D\u4E2D\u6B21\u6570",
237
290
  onChange: (next) => {
238
- setCount(next);
291
+ setLimit(next);
292
+ setNote("");
293
+ }
294
+ }
295
+ )
296
+ ] }),
297
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "rg-row", children: [
298
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "rg-rowText", children: [
299
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "rg-rowTitle", children: "\u884C\u5185\u91CD\u590D\u68C0\u6D4B" }),
300
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "rg-rowDesc", children: '\u62E6\u622A\u7C7B\u4F3C"\u597D\u3002\u597D\u3002\u597D\u3002"\u7684\u884C\u5185\u5FAA\u73AF\uFF1B\u5173\u95ED\u65F6\u53EA\u68C0\u6D4B\u72EC\u7ACB\u6BB5\u843D\u3002' })
301
+ ] }),
302
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
303
+ Switch,
304
+ {
305
+ checked: inlineRepeat,
306
+ label: "\u884C\u5185\u91CD\u590D\u68C0\u6D4B",
307
+ onChange: (next) => {
308
+ setInlineRepeat(next);
239
309
  setNote("");
240
310
  }
241
311
  }
@@ -244,7 +314,7 @@ var __dshClientBundle = (() => {
244
314
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "rg-row rg-row--stack", children: [
245
315
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "rg-rowText", children: [
246
316
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "rg-rowTitle", children: "\u62E6\u622A\u77ED\u53E5" }),
247
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "rg-rowDesc", children: "\u4E00\u884C\u4E00\u53E5\uFF0C\u8FDE\u6807\u70B9\u4E00\u8D77\u5199\uFF1B\u53EA\u7B97\u72EC\u5360\u4E00\u884C\u7684\u6574\u884C\u5339\u914D" })
317
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "rg-rowDesc", children: "\u6807\u70B9\u4E0D\u53EF\u7701\u7565" })
248
318
  ] }),
249
319
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
250
320
  FragmentList,
@@ -256,6 +326,40 @@ var __dshClientBundle = (() => {
256
326
  onRemove: remove
257
327
  }
258
328
  )
329
+ ] }),
330
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "rg-row rg-row--stack", children: [
331
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "rg-rowText", children: [
332
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "rg-rowTitle", children: "\u62E6\u622A\u63D0\u793A\u8BCD" }),
333
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "rg-rowDesc", children: "\u622A\u65AD\u540E\u4E0A\u4E0B\u6587\u6CE8\u5165\u7684\u63D0\u793A\u8BCD" })
334
+ ] }),
335
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
336
+ "textarea",
337
+ {
338
+ className: "rg-textarea",
339
+ value: resumeText,
340
+ onChange: (event) => {
341
+ setResumeText(event.target.value);
342
+ setNote("");
343
+ }
344
+ }
345
+ )
346
+ ] }),
347
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "rg-row", children: [
348
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "rg-rowText", children: [
349
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "rg-rowTitle", children: "\u62E6\u622A\u6458\u8981" }),
350
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "rg-rowDesc", children: "\u4E0A\u4E0B\u6587\u6CE8\u5165\u7684\u7B80\u4ECB\u4FE1\u606F\uFF0C\u4EC5\u7528\u6237\u53EF\u89C1" })
351
+ ] }),
352
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
353
+ "input",
354
+ {
355
+ className: "rg-summaryInput",
356
+ value: resumeSummary,
357
+ onChange: (event) => {
358
+ setResumeSummary(event.target.value);
359
+ setNote("");
360
+ }
361
+ }
362
+ )
259
363
  ] })
260
364
  ] });
261
365
  }
@@ -279,9 +383,16 @@ var __dshClientBundle = (() => {
279
383
  Form,
280
384
  {
281
385
  initial: value,
282
- onSave: async (fragments, threshold) => {
283
- await scope.set("fragments", [...fragments]);
284
- await scope.set("threshold", threshold);
386
+ count: value.count,
387
+ onReset: async () => {
388
+ await scope.set("count", 0);
389
+ },
390
+ onSave: async (next) => {
391
+ await scope.set("fragments", next.fragments);
392
+ await scope.set("threshold", next.threshold);
393
+ await scope.set("inlineRepeat", next.inlineRepeat);
394
+ await scope.set("resumeText", next.resumeText);
395
+ await scope.set("resumeSummary", next.resumeSummary);
285
396
  }
286
397
  }
287
398
  );
package/lib/config.d.ts CHANGED
@@ -1,8 +1,12 @@
1
1
  /**
2
- * 插件配置:拦截短句表与连续命中阈值。
2
+ * 插件配置:拦截短句表、连续命中阈值、行内重复检测、续跑注入的正文与摘要,
3
+ * 外加一个运行时计数。
3
4
  *
4
5
  * 配置放在 dsh 的 settings 服务里(命名空间 repeat-guard):宿主侧在这边注册
5
- * schema,客户端设置页写同一个命名空间。判定时现取,改完立即生效,不用重启。
6
+ * schema,客户端设置页写同一个命名空间。判定与续跑时现取,改完立即生效,不用重启。
7
+ *
8
+ * 计数也寄在同一个命名空间里:宿主侧每次掐断写一次,客户端读同一个值、清零就是
9
+ * 把它写成 0。插件加载时先把它归零,所以口径是"自本次启动或上次清零以来"。
6
10
  */
7
11
  import type { Context } from '@deepseek-ai/cordis';
8
12
  /** settings 命名空间。客户端设置页必须写同一个值。 */
@@ -11,18 +15,37 @@ export declare const SETTINGS_NS = "repeat-guard";
11
15
  export declare const DEFAULT_FRAGMENTS: readonly string[];
12
16
  /** 默认阈值:1 表示发现即拦。 */
13
17
  export declare const DEFAULT_THRESHOLD = 1;
14
- /** 一份生效中的配置,已编译成判定时直接可用的形式。 */
18
+ /** 默认关闭行内重复检测:只认独占一行的整句。 */
19
+ export declare const DEFAULT_INLINE_REPEAT = false;
20
+ /** 默认的续跑指令正文,截断后推给模型。 */
21
+ export declare const DEFAULT_RESUME_TEXT: string;
22
+ /** 默认的折叠行摘要;客户端拿它渲染折叠的 context 行,不是用户气泡。 */
23
+ export declare const DEFAULT_RESUME_SUMMARY = "\u590D\u8BFB\u5DF2\u622A\u65AD\uFF1A\u8BF7\u7EE7\u7EED\u6267\u884C";
24
+ /** 判定与续跑用得上的一份配置。 */
15
25
  export interface RepeatGuardConfig {
16
26
  /** 短句表,已全部小写化。 */
17
27
  readonly fragments: ReadonlySet<string>;
18
- /** 连续命中多少行才拦。 */
28
+ /** 连续命中多少次才拦。 */
19
29
  readonly threshold: number;
30
+ /** 是否把"整行由多个表项拼成"也算命中。 */
31
+ readonly inlineRepeat: boolean;
32
+ /** 截断后推给模型的指令正文。 */
33
+ readonly resumeText: string;
34
+ /** 注入消息的折叠行摘要。 */
35
+ readonly resumeSummary: string;
20
36
  }
21
37
  /** 取当前配置。 */
22
38
  export type ConfigSource = () => RepeatGuardConfig;
39
+ /** 宿主侧要用到的两件事:取配置、记一次拦截。 */
40
+ export interface GuardRuntime {
41
+ /** 取当前配置。 */
42
+ readonly read: ConfigSource;
43
+ /** 记一次拦截,把计数写回 settings。 */
44
+ readonly countHit: () => void;
45
+ }
23
46
  /**
24
- * 注册配置命名空间,并返回取当前配置的函数。
47
+ * 注册配置命名空间,返回宿主侧要用的运行时句柄。
25
48
  * @param ctx - 宿主 cordis 上下文。
26
- * @returns 每次调用都返回最新配置。
49
+ * @returns 取配置与记数两个入口。
27
50
  */
28
- export declare function createConfigSource(ctx: Context): ConfigSource;
51
+ export declare function createRuntime(ctx: Context): GuardRuntime;