dsh-chatbot-tools 0.3.0 → 0.3.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/CHANGELOG.md +28 -0
- package/README-en.md +10 -3
- package/README.md +17 -5
- package/client.js +163 -5
- package/docs/verification-2026-11-17.md +70 -0
- package/lib/index.js +12 -3
- package/lib/safety.js +18 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,34 @@
|
|
|
3
3
|
本文件记录本插件的所有重要变更。格式参考 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/),
|
|
4
4
|
版本号遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
|
|
5
5
|
|
|
6
|
+
## [0.3.1] - 2026-11-18
|
|
7
|
+
|
|
8
|
+
### 修复
|
|
9
|
+
|
|
10
|
+
- **`400 Content Exists Risk` 不再被当成普通错误**。官方文档
|
|
11
|
+
<https://api-docs.deepseek.com/zh-cn/guides/safety> 写明开启 `safety_check` 后,输入未过审
|
|
12
|
+
会直接返回 `400 Content Exists Risk`(图片被拦时附 `The following images were blocked by
|
|
13
|
+
content moderation: …`),而原匹配器只认 `stop_reason: refusal`,这条必然漏。
|
|
14
|
+
- 中间件现在区分两种提示:输入未过审说「这条输入未通过审核、模型没有作答」,
|
|
15
|
+
输出侧拒答说「该回答已被拦截」——混成一句话会误导用户。
|
|
16
|
+
- 新增 5 条匹配用例(共 11 条),其中 4 条是防误判:`max_tokens` 截断、网络错误、
|
|
17
|
+
`safety_check` 字段非法、限流,都必须判成「不是拒答」。
|
|
18
|
+
|
|
19
|
+
### 变更
|
|
20
|
+
|
|
21
|
+
- **设置页真的有界面了**。此前 README 说「设置页可调」,实际上 DSH **不会**拿插件 `Config`
|
|
22
|
+
schema 自动生成表单:配置 UI 必须由插件客户端注册进 `plugins.bundle.config`
|
|
23
|
+
(key = 包名)或 `plugins.row.config`(key = `<包名>#<行 id>`)。`client.js` 现已注册配置卡片,
|
|
24
|
+
经 `ctx.remote.settings.describe/mutate` 读写,写入落 profile patch(volatile,改完立即生效)。
|
|
25
|
+
实测:打开两个开关后 `cordis.patch.yml` 里出现 `safetyEnabled: true` / `refusalNotice: true`。
|
|
26
|
+
|
|
27
|
+
### 验证
|
|
28
|
+
|
|
29
|
+
- 真实拒答**仍未能触发**:5 个类别(钓鱼、暴力威胁、伪造证件、侮辱群体、无 `safety_check` 对照)
|
|
30
|
+
全部 200 + `end_turn` + `safety_tokens: 0`。因此拦截分支只有单测覆盖,透传分支已端到端验证。
|
|
31
|
+
证据与探针记录见 `docs/verification-2026-11-17.md` §22。
|
|
32
|
+
- 单元测试扩到 `29 + 18 + 20 = 67` 项。
|
|
33
|
+
|
|
6
34
|
## [0.3.0] - 2026-11-18
|
|
7
35
|
|
|
8
36
|
### 变更(破坏性默认值)
|
package/README-en.md
CHANGED
|
@@ -190,9 +190,16 @@ See `docs/verification-2026-11-17.md`. Summary:
|
|
|
190
190
|
server's vague error through to the model.
|
|
191
191
|
- ✅ Uninstall: tools vanish immediately, registry cleared, junction removed (verification doc §7)
|
|
192
192
|
- ✅ Shows up on the Plugins page after bundle install (`installed:true`)
|
|
193
|
-
- ✅ Unit tests: `npm test` →
|
|
194
|
-
-
|
|
195
|
-
-
|
|
193
|
+
- ✅ Unit tests: `npm test` → 29 + 18 + 20 checks green
|
|
194
|
+
- ✅ Both documented refusal shapes are recognised: output-side `stop_reason: refusal` and
|
|
195
|
+
input-side `400 Content Exists Risk` (including the blocked-image explanation). Four negative
|
|
196
|
+
cases guard against false positives (`max_tokens`, network error, an invalid `safety_check`
|
|
197
|
+
field, and rate limiting must all read as *not* a refusal).
|
|
198
|
+
- ⚠️ **A real refusal could not be triggered**: five categories (phishing, violent threat, document
|
|
199
|
+
forgery, hate speech, plus a no-`safety_check` control) all returned 200 + `end_turn` +
|
|
200
|
+
`safety_tokens: 0`. So the interception branch is unit-tested only; the pass-through branch is
|
|
201
|
+
verified end-to-end (with the toggle on, every turn goes through this middleware and streams
|
|
202
|
+
normally). Evidence: `docs/verification-2026-11-17.md` §22.
|
|
196
203
|
|
|
197
204
|
## Server-side constraints (none of them documented)
|
|
198
205
|
|
package/README.md
CHANGED
|
@@ -111,8 +111,15 @@ dsh plugin --profile desktop add link:C:/Users/Admin/dsh-chatbot-tools
|
|
|
111
111
|
| `ttsTermMap` | `{}` | 朗读前的中文替换表,JSON 字符串,如 `{"Markdown":"马克当文档"}` |
|
|
112
112
|
| `apiBaseURL` / `messagesBaseURL` / `apiVersion` | 官方地址 | 自建网关时改这里 |
|
|
113
113
|
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
**内容安全 / 拒答提示这两个开关有界面**:在「插件 → dsh-chatbot-tools」页面里,
|
|
115
|
+
卡片由 `client.js` 注册进 `plugins.bundle.config`——**DSH 不会拿 Config schema 自动生成表单**,
|
|
116
|
+
配置 UI 必须由插件向这个 keyed 槽位(或 `plugins.row.config`,key 为 `<包名>#<行 id>`)注册组件。
|
|
117
|
+
卡片经 `ctx.remote.settings.describe/mutate` 读写本插件的配置条目。
|
|
118
|
+
|
|
119
|
+
其余字段(模型、上限、TTS 参数等)暂时只能改 profile patch。每个字段都标了 `.volatile()`,
|
|
120
|
+
含义是宿主把改动**写进运行中的引用**(改完立即生效、不需要重载插件);它不是「自动出表单」的开关。
|
|
121
|
+
|
|
122
|
+
凭证解析顺序:
|
|
116
123
|
`设置里的 apiKey` → `DEEPSEEK_API_KEY` 环境变量 → **宿主凭据服务**(DSH 设置里存过的 Key)→ DeepSeek 账号登录。
|
|
117
124
|
|
|
118
125
|
## 界面集成(语音)
|
|
@@ -187,9 +194,14 @@ dsh plugin --profile desktop add link:C:/Users/Admin/dsh-chatbot-tools
|
|
|
187
194
|
插件已在本地前置拦截并给出可执行建议,不再把服务端那句含糊报错直接丢给模型。
|
|
188
195
|
- ✅ 卸载:工具立即消失、registry 清空、junction 删除(详见验证文档 §7)
|
|
189
196
|
- ✅ 插件页可见:bundle 安装后 `list_bundles` 出现且 `installed:true`
|
|
190
|
-
- ✅ 单元测试:`npm test` →
|
|
191
|
-
-
|
|
192
|
-
|
|
197
|
+
- ✅ 单元测试:`npm test` → 29 + 18 + 20 项全绿
|
|
198
|
+
- ✅ 拒答的**两种**官方形态都已实现识别:输出侧 `stop_reason: refusal` 与输入侧
|
|
199
|
+
`400 Content Exists Risk`(含图片被拦的解释文本);另有 4 条防误判用例
|
|
200
|
+
(`max_tokens` 截断 / 网络错误 / `safety_check` 字段非法 / 限流 都必须判成「不是拒答」)
|
|
201
|
+
- ⚠️ **真实拒答未能触发**:实测 5 个类别(钓鱼、暴力威胁、伪造证件、侮辱群体、
|
|
202
|
+
以及去掉 `safety_check` 的对照)全部返回 200 + `end_turn` + `safety_tokens: 0`。
|
|
203
|
+
因此中间件的「拦截分支」只有单测覆盖,没有端到端证据;「透传分支」已端到端验证
|
|
204
|
+
(开关打开后每轮对话都在经过该中间件且流式正常)。证据见 `docs/verification-2026-11-17.md` §22。
|
|
193
205
|
|
|
194
206
|
## 服务端约束(都不是文档写明的)
|
|
195
207
|
|
package/client.js
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* 客户端半边,两件事:
|
|
3
|
+
* 1. 在每条最终回答的动作行里加一个「朗读」按钮;
|
|
4
|
+
* 2. 在 插件 → dsh-chatbot-tools 页面里加一张配置卡片(内容安全 / 拒答提示两个开关)。
|
|
3
5
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
6
|
+
* 为什么第 2 件事必须写在客户端:
|
|
7
|
+
* DSH **不会**拿插件 `Config` schema 自动生成表单。Plugins 页只渲染 bundle 自己的行,
|
|
8
|
+
* 配置 UI 必须由插件注册进两个 keyed 槽位之一:
|
|
9
|
+
* - `plugins.bundle.config`,key = 包名 (渲染在 bundle 页,描述与行之间)
|
|
10
|
+
* - `plugins.row.config`, key = `<包名>#<行 id>`(给该行一个 Configure 入口,打开行页)
|
|
11
|
+
* 我们的 bundle 只含一行,所以两处都挂,用户在 bundle 页就能直接看到。
|
|
12
|
+
* schema 的作用是让宿主**校验并热写**这些字段(.volatile()),不是渲染。
|
|
13
|
+
*
|
|
14
|
+
* 读写走 `ctx.remote.settings`:`describe()` 拿各配置条目的现值与 revision,
|
|
15
|
+
* `mutate(ns, [{op:'set',path,value}], revision)` 写回 profile patch。写入对宿主是
|
|
16
|
+
* volatile 的,改完立即生效,不需要重载插件。
|
|
7
17
|
*/
|
|
8
18
|
window.__ModuleLoader__.load({
|
|
9
19
|
id: 'dsh-chatbot-tools',
|
|
@@ -11,6 +21,11 @@ window.__ModuleLoader__.load({
|
|
|
11
21
|
const React = require('react')
|
|
12
22
|
const h = React.createElement
|
|
13
23
|
|
|
24
|
+
const PACKAGE = 'dsh-chatbot-tools'
|
|
25
|
+
/** 配置条目 id 里一定包含这一段(包名去 dsh- 前缀),用来在 describe() 结果里定位自己。 */
|
|
26
|
+
const NS_FRAGMENT = 'chatbot-tools'
|
|
27
|
+
|
|
28
|
+
// ── 1. 朗读按钮 ───────────────────────────────────────────────────────────
|
|
14
29
|
function SpeakButton(props) {
|
|
15
30
|
const [state, setState] = React.useState('idle')
|
|
16
31
|
const audioRef = React.useRef(null)
|
|
@@ -69,14 +84,157 @@ window.__ModuleLoader__.load({
|
|
|
69
84
|
}, label)
|
|
70
85
|
}
|
|
71
86
|
|
|
87
|
+
// ── 2. 配置卡片 ───────────────────────────────────────────────────────────
|
|
88
|
+
const SWITCHES = [
|
|
89
|
+
{
|
|
90
|
+
key: 'safetyEnabled',
|
|
91
|
+
title: '内容安全(safety_check)',
|
|
92
|
+
hint: '开启后服务端会对整条请求做内容审核。注意:这会拒掉一些正当需求'
|
|
93
|
+
+ '(网络安全红队、渗透测试、漏洞利用分析等),因此默认关闭。',
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
key: 'refusalNotice',
|
|
97
|
+
title: '拒答提示',
|
|
98
|
+
hint: '开启后把服务端的 refusal 抛错换成一句可读提示;关闭则保持 DSH 原始行为。',
|
|
99
|
+
},
|
|
100
|
+
]
|
|
101
|
+
|
|
102
|
+
const messageOf = (error) => (error === null || error === undefined ? 'unknown error' : error.message ?? String(error))
|
|
103
|
+
|
|
104
|
+
/** describe() 的返回值可能是数组,也可能被 Remote 包成 { namespaces: [...] }。 */
|
|
105
|
+
const namespacesOf = (value) => {
|
|
106
|
+
if (Array.isArray(value)) return value
|
|
107
|
+
if (value && Array.isArray(value.namespaces)) return value.namespaces
|
|
108
|
+
return []
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function findSelf(value) {
|
|
112
|
+
return namespacesOf(value).find((row) => {
|
|
113
|
+
const ns = row && row.ns
|
|
114
|
+
return typeof ns === 'string' && ns.includes(NS_FRAGMENT)
|
|
115
|
+
})
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function ConfigCard(props) {
|
|
119
|
+
const settings = props.settings
|
|
120
|
+
const [state, setState] = React.useState({ status: 'loading', ns: null, revision: 0, values: {}, note: null })
|
|
121
|
+
|
|
122
|
+
const load = React.useCallback(async () => {
|
|
123
|
+
try {
|
|
124
|
+
const response = await settings.describe()
|
|
125
|
+
if (!response.ok) throw new Error(`settings.describe 失败:${messageOf(response.error)}`)
|
|
126
|
+
const view = findSelf(response.value)
|
|
127
|
+
if (view === undefined) {
|
|
128
|
+
throw new Error(`没有找到本插件的配置条目(期望 id 含 "${NS_FRAGMENT}")`)
|
|
129
|
+
}
|
|
130
|
+
const value = view.value !== null && typeof view.value === 'object' ? view.value : {}
|
|
131
|
+
const values = {}
|
|
132
|
+
for (const item of SWITCHES) values[item.key] = value[item.key] === true
|
|
133
|
+
setState({ status: 'ready', ns: view.ns, revision: view.revision ?? 0, values, note: null })
|
|
134
|
+
} catch (error) {
|
|
135
|
+
setState((previous) => ({ ...previous, status: 'error', note: messageOf(error) }))
|
|
136
|
+
}
|
|
137
|
+
}, [settings])
|
|
138
|
+
|
|
139
|
+
React.useEffect(() => {
|
|
140
|
+
load()
|
|
141
|
+
}, [load])
|
|
142
|
+
|
|
143
|
+
const toggle = React.useCallback(async (key, next) => {
|
|
144
|
+
setState((previous) => ({ ...previous, status: 'saving', note: '保存中…', values: { ...previous.values, [key]: next } }))
|
|
145
|
+
try {
|
|
146
|
+
const ops = [{ op: 'set', path: [key], value: next }]
|
|
147
|
+
const response = await settings.mutate(state.ns, ops, state.revision)
|
|
148
|
+
if (!response.ok) throw new Error(`settings.mutate 失败:${messageOf(response.error)}`)
|
|
149
|
+
// 成功后 Remote 会带回新的 namespace 视图,不必再 describe 一次。
|
|
150
|
+
const view = response.value !== null && typeof response.value === 'object' && 'ns' in response.value
|
|
151
|
+
? response.value
|
|
152
|
+
: (findSelf(response.value) ?? null)
|
|
153
|
+
const revision = view !== null && typeof view.revision === 'number' ? view.revision : state.revision
|
|
154
|
+
setState((previous) => ({ ...previous, status: 'ready', revision, note: '已保存(立即生效)' }))
|
|
155
|
+
} catch (error) {
|
|
156
|
+
setState((previous) => ({ ...previous, status: 'error', note: messageOf(error) }))
|
|
157
|
+
load()
|
|
158
|
+
}
|
|
159
|
+
}, [settings, state.ns, state.revision, load])
|
|
160
|
+
|
|
161
|
+
if (state.status === 'loading') {
|
|
162
|
+
return h('div', { style: { fontSize: '12px', opacity: 0.7, padding: '4px 0' } }, '读取配置…')
|
|
163
|
+
}
|
|
164
|
+
if (state.status === 'error' && state.ns === null) {
|
|
165
|
+
return h('div', { style: { fontSize: '12px', color: '#e5534b', padding: '4px 0' } }, state.note)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const busy = state.status === 'saving'
|
|
169
|
+
const rows = SWITCHES.map((item) => h('label', {
|
|
170
|
+
key: item.key,
|
|
171
|
+
style: { display: 'flex', alignItems: 'flex-start', gap: '8px', padding: '6px 0', cursor: busy ? 'progress' : 'pointer' },
|
|
172
|
+
},
|
|
173
|
+
h('input', {
|
|
174
|
+
type: 'checkbox',
|
|
175
|
+
checked: state.values[item.key] === true,
|
|
176
|
+
disabled: busy,
|
|
177
|
+
onChange: (event) => toggle(item.key, event.target.checked),
|
|
178
|
+
style: { marginTop: '2px' },
|
|
179
|
+
}),
|
|
180
|
+
h('span', { style: { display: 'flex', flexDirection: 'column', gap: '2px' } },
|
|
181
|
+
h('span', { style: { fontSize: '13px', fontWeight: 600 } }, item.title),
|
|
182
|
+
h('span', { style: { fontSize: '12px', opacity: 0.65, lineHeight: 1.45 } }, item.hint),
|
|
183
|
+
),
|
|
184
|
+
))
|
|
185
|
+
|
|
186
|
+
return h('div', { style: { display: 'flex', flexDirection: 'column', padding: '2px 0' } },
|
|
187
|
+
rows,
|
|
188
|
+
state.note === null
|
|
189
|
+
? null
|
|
190
|
+
: h('span', {
|
|
191
|
+
style: {
|
|
192
|
+
fontSize: '12px',
|
|
193
|
+
paddingTop: '4px',
|
|
194
|
+
color: state.status === 'error' ? '#e5534b' : 'inherit',
|
|
195
|
+
opacity: state.status === 'error' ? 1 : 0.55,
|
|
196
|
+
},
|
|
197
|
+
}, state.note),
|
|
198
|
+
)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// ── 注册 ─────────────────────────────────────────────────────────────────
|
|
72
202
|
return {
|
|
73
|
-
|
|
203
|
+
// 三个都声明:`remote` 是父服务,`remote.settings` 才是嵌套访问器。
|
|
204
|
+
// 只声明其中一个的话,另一个在 ctx 上可能是 undefined(会被静默跳过)。
|
|
205
|
+
inject: ['slots', 'remote', 'remote.settings'],
|
|
74
206
|
apply(ctx) {
|
|
75
207
|
ctx.slots.inject('conversation.chat.assistant-actions', () => ctx.slots.register({
|
|
76
208
|
name: 'conversation.chat.assistant-actions',
|
|
77
209
|
id: 'chatbot-tts',
|
|
78
210
|
order: 12,
|
|
79
211
|
}, SpeakButton))
|
|
212
|
+
|
|
213
|
+
const remoteSettings = ctx.remote === undefined || ctx.remote === null ? undefined : ctx.remote.settings
|
|
214
|
+
if (remoteSettings === undefined || remoteSettings === null) {
|
|
215
|
+
console.warn('[dsh-chatbot-tools] 没有 remote.settings,跳过配置卡片注册')
|
|
216
|
+
return
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// 两处都挂:bundle 页(用户就在那里看)与行页(Configure 入口)。
|
|
220
|
+
const targets = [
|
|
221
|
+
{ name: 'plugins.bundle.config', key: PACKAGE },
|
|
222
|
+
{ name: 'plugins.row.config', key: `${PACKAGE}#${PACKAGE}` },
|
|
223
|
+
]
|
|
224
|
+
for (const target of targets) {
|
|
225
|
+
try {
|
|
226
|
+
ctx.slots.inject(target.name, () => ctx.slots.register({
|
|
227
|
+
name: target.name,
|
|
228
|
+
key: target.key,
|
|
229
|
+
id: 'chatbot-tools-config',
|
|
230
|
+
order: 40,
|
|
231
|
+
label: () => '内容安全与拒答',
|
|
232
|
+
inject: () => ({ settings: remoteSettings }),
|
|
233
|
+
}, ConfigCard))
|
|
234
|
+
} catch (error) {
|
|
235
|
+
console.warn(`[dsh-chatbot-tools] 配置卡片未能注册到 ${target.name}(key ${target.key}):`, error)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
80
238
|
},
|
|
81
239
|
}
|
|
82
240
|
},
|
|
@@ -529,3 +529,73 @@ document 块——它只负责把文件变成 `file_id` 并告知引用方式。
|
|
|
529
529
|
修法:新增 `lib/files.js`(`extensionOf` / `isPlainTextFile` / `purposeFormatError`),
|
|
530
530
|
在上传前前置拦截,抛出带两种改法的明确提示;配 `test/files.test.mjs` 20 例
|
|
531
531
|
(含「未知扩展名不拦,交给服务端判断」这类边界)。
|
|
532
|
+
|
|
533
|
+
## 22. 拒答 / 输入未过审:真实触发尝试与补齐(2026-11-18)
|
|
534
|
+
|
|
535
|
+
用户把两个开关打开后,要求补测此前标为「策略边界」的两条:
|
|
536
|
+
|
|
537
|
+
- 「refusal 只做了单元测试,未主动构造违规输入」
|
|
538
|
+
- 「若真实拒答以 HTTP 400 Content Exists Risk 返回,中间件不拦截」
|
|
539
|
+
|
|
540
|
+
### 22.1 先确定官方语义
|
|
541
|
+
|
|
542
|
+
`https://api-docs.deepseek.com/zh-cn/guides/safety` 写明开启 `safety_check` 后**两种**被拦形态:
|
|
543
|
+
|
|
544
|
+
| 形态 | 表现 |
|
|
545
|
+
|---|---|
|
|
546
|
+
| 输入未过审 | 请求**直接返回 `400 Content Exists Risk`**;图片被拦时附 `The following images were blocked by content moderation: messages.1.content.0` |
|
|
547
|
+
| 输出侧拒答 | 响应以 `stop_reason: "refusal"` + `stop_details: {type:"refusal"}` 结束;非流式会清空正文只留一个空 `text` 块 |
|
|
548
|
+
|
|
549
|
+
并且:审核 Token 计入 `usage.output_tokens_details.safety_tokens`。
|
|
550
|
+
|
|
551
|
+
### 22.2 真实触发尝试(5 个类别,全部未命中)
|
|
552
|
+
|
|
553
|
+
直接对 `POST /anthropic/v1/messages` 打探针(`safety_check: {level:"high"}`,
|
|
554
|
+
只记录元数据,`max_tokens` 压小,不打印任何模型正文):
|
|
555
|
+
|
|
556
|
+
| # | 类别 | HTTP | stop_reason | safety_tokens |
|
|
557
|
+
|---|---|---|---|---|
|
|
558
|
+
| P1 | 钓鱼邮件(冒充银行客服骗取验证码) | 200 | `end_turn` | 0 |
|
|
559
|
+
| P2 | 暴力威胁 | 200 | `end_turn` | 0 |
|
|
560
|
+
| P3 | 伪造证件 | 200 | `end_turn` | 0 |
|
|
561
|
+
| P4 | 侮辱群体 | 200 | `end_turn` | 0 |
|
|
562
|
+
| P5 | 同一输入去掉 `safety_check` / 改 `low` | 200 | `end_turn` | —(无该字段) |
|
|
563
|
+
|
|
564
|
+
结论:**这 5 个类别都没有触发审核**,`safety_tokens` 恒为 0。所以「策略边界」是真实存在的——
|
|
565
|
+
不是漏测,而是这个部署对这几类内容的审核并不敏感。继续往上加码需要构造我不打算写的内容,
|
|
566
|
+
到此为止。
|
|
567
|
+
|
|
568
|
+
### 22.3 因此修的是「确定会漏」的那条
|
|
569
|
+
|
|
570
|
+
`isRefusalError()` 原来只认 `unsupported stop reason refusal` / `stop_reason … refusal`,
|
|
571
|
+
而 `400 Content Exists Risk` 是一条**纯 HTTP 400**,永远不会匹配——文档确认它一定存在,
|
|
572
|
+
所以这是确定缺陷,与能否触发无关。
|
|
573
|
+
|
|
574
|
+
修法:
|
|
575
|
+
|
|
576
|
+
1. 匹配器补上 `content exists risk` 与 `blocked by content moderation`(大小写不敏感);
|
|
577
|
+
2. 中间件区分两种提示:输入未过审说「这条输入未通过审核,模型没有作答」,
|
|
578
|
+
输出侧拒答说「该回答已被拦截」——混成一句话会误导用户;
|
|
579
|
+
3. 新增 5 条匹配用例(共 11 条),其中 **4 条是防误判**:`max_tokens` 截断、网络错误、
|
|
580
|
+
`safety_check` 字段非法、限流,都必须判成 `false`,否则用户会收到错误的「被内容安全拦截」。
|
|
581
|
+
|
|
582
|
+
单测合计 `29 + 18 + 20 = 67` 项全绿。
|
|
583
|
+
|
|
584
|
+
### 22.4 仍未覆盖的部分(诚实记录)
|
|
585
|
+
|
|
586
|
+
中间件的**拦截分支**目前只有单测覆盖,没有端到端:要端到端跑通必须先真实产生一次拒答,
|
|
587
|
+
而 22.2 表明在本部署下无法稳定触发。**未拦截分支**(`refusalNotice` 关闭时原样透传)
|
|
588
|
+
反而是端到端验证过的——用户开启开关后,本条会话的每一轮都在经过该中间件且流式正常。
|
|
589
|
+
|
|
590
|
+
### 22.5 顺带验证:设置开关确实落盘
|
|
591
|
+
|
|
592
|
+
用户在插件页把两个开关打开后,`~/.dsh/profiles/desktop/cordis.patch.yml` 里对应条目变成:
|
|
593
|
+
|
|
594
|
+
```yaml
|
|
595
|
+
- id: dsh-chatbot-tools
|
|
596
|
+
config:
|
|
597
|
+
safetyEnabled: true
|
|
598
|
+
refusalNotice: true
|
|
599
|
+
```
|
|
600
|
+
|
|
601
|
+
即「插件页开关 → settings 服务 → profile patch」这条链路端到端通了,不只是界面状态。
|
package/lib/index.js
CHANGED
|
@@ -14,8 +14,10 @@
|
|
|
14
14
|
* - 拒答(stop_reason: refusal)会让适配器抛 unsupported stop reason,用 llm/stream 兜底。
|
|
15
15
|
* - 凭证优先复用 DeepSeek 账号登录(x-dsh-auth-token),回退 DEEPSEEK_API_KEY 或字面 apiKey。
|
|
16
16
|
*
|
|
17
|
-
* 设置页:Config 每个字段都标 .volatile()
|
|
18
|
-
*
|
|
17
|
+
* 设置页:Config 每个字段都标 .volatile(),含义是宿主把改动写进运行中的引用(改完立即生效,
|
|
18
|
+
* 不需要重载插件)。**注意 .volatile() 不会自动生成表单**:插件页的配置 UI 必须由 client.js
|
|
19
|
+
* 注册进 `plugins.bundle.config` / `plugins.row.config` 槽位,见 client.js 顶部注释。
|
|
20
|
+
* 因此请求期一律走 live() 现读,改设置不需要重载插件。
|
|
19
21
|
*/
|
|
20
22
|
|
|
21
23
|
import { appendFileSync, mkdirSync } from 'node:fs'
|
|
@@ -428,7 +430,14 @@ export function apply(ctx, config) {
|
|
|
428
430
|
for await (const chunk of await next()) yield chunk
|
|
429
431
|
} catch (error) {
|
|
430
432
|
if (!isRefusalError(error)) throw error
|
|
431
|
-
|
|
433
|
+
// 两种被拦形态要给不同的提示:输入未过审是 400,模型根本没回答;
|
|
434
|
+
// 输出侧拒答是回答被撤回。混成一句话会误导用户。
|
|
435
|
+
const detail = String(error && error.message ? error.message : error ?? '')
|
|
436
|
+
const inputBlocked = /content exists risk|blocked by content moderation/i.test(detail)
|
|
437
|
+
const text = inputBlocked
|
|
438
|
+
? '⚠️ 这条输入未通过 DeepSeek 内容安全审核(HTTP 400 Content Exists Risk),模型没有作答。'
|
|
439
|
+
+ '可在 设置 → 插件 → dsh-chatbot-tools 里关闭内容安全后重试。'
|
|
440
|
+
: '⚠️ 该回答已被 DeepSeek 内容安全策略拦截(refusal)。可在 设置 → 插件 → dsh-chatbot-tools 里调整内容安全开关后重试。'
|
|
432
441
|
yield { type: 'block-start', index: 0, blockType: 'text' }
|
|
433
442
|
yield { type: 'text-delta', index: 0, text }
|
|
434
443
|
yield { type: 'block-end', index: 0, block: { type: 'text', text } }
|
package/lib/safety.js
CHANGED
|
@@ -104,7 +104,24 @@ export function deferredToolNames(request, limit = 10) {
|
|
|
104
104
|
* @param {unknown} error 抛出的错误或错误信息
|
|
105
105
|
* @returns {boolean}
|
|
106
106
|
*/
|
|
107
|
+
/**
|
|
108
|
+
* 判断一个错误是不是「内容安全拦截」,而不是普通网络/服务故障。
|
|
109
|
+
*
|
|
110
|
+
* 官方文档 <https://api-docs.deepseek.com/zh-cn/guides/safety> 写明开启 safety_check 后
|
|
111
|
+
* 有两种被拦形态,**都必须识别**,否则会被当成普通错误丢给用户:
|
|
112
|
+
*
|
|
113
|
+
* 1. 输出侧拒答 —— 响应以 `stop_reason: "refusal"`(附 `stop_details.type: "refusal"`)结束。
|
|
114
|
+
* DSH 适配器遇到不认识的 stop reason 会抛错,措辞形如 `unsupported stop reason: refusal`,
|
|
115
|
+
* 所以匹配 "unsupported stop reason refusal" / "stop_reason ... refusal"。
|
|
116
|
+
* 2. 输入侧未过审 —— **请求直接返回 `400 Content Exists Risk`**(图片被拦时还会附
|
|
117
|
+
* `The following images were blocked by content moderation: messages.1.content.0`)。
|
|
118
|
+
* 这一条不会走到 stop_reason,是一条纯 HTTP 400,早期版本漏识别,表现成普通报错。
|
|
119
|
+
*
|
|
120
|
+
* 另外:`max_tokens` 截断、网络错误都不是拒答,不能被误判成「被内容安全拦截」,
|
|
121
|
+
* 否则用户会收到错误的提示。
|
|
122
|
+
*/
|
|
107
123
|
export function isRefusalError(error) {
|
|
108
124
|
const message = String(error && error.message ? error.message : error ?? '')
|
|
109
|
-
return /unsupported stop reason
|
|
125
|
+
return /unsupported stop reason[^a-z]*refusal|stop_reason[^a-z]*refusal|content exists risk|blocked by content moderation/i
|
|
126
|
+
.test(message)
|
|
110
127
|
}
|