dsh-plugin-dingtalk-bridge 0.4.1 → 0.4.3

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
@@ -185,6 +185,7 @@ npm i -g dsh-plugin-dingtalk-bridge@0.4.0 # 或装到 profile 的 dependencies
185
185
  | 点授权报错 `'dws' 不是内部或外部命令` / `spawn dws ENOENT` | 这台机器没装 dws。点「钉钉授权」时插件会先拦住并提示去安装;在提示卡或设置页点 **安装 dws** 即可:有 npm 走全局安装,**没有 npm 也能装**(直接下载 npm 包解压,自带解压器,并借用 DSH 自带的 Node 跑 `install.js`)。装完自动重新检测并开始接收。 |
186
186
  | 想升级 dws | 点提示卡或设置页的 **检查更新**(只查 npm 上的版本),发现新版后点 **更新到 vX**;npm 源慢就改 `dwsRegistry`(如 `https://registry.npmmirror.com/`)。 |
187
187
  | 卸载插件再装回来,又说「未安装 dws」 | 0.4.1 起会自动找回:`dws --version` 失败时插件会依次探测「配置的 `dwsPath` → 插件数据目录 `dws-runtime/…/vendor` → Node 同目录 `node_modules` → `%APPDATA%\npm` / `Program Files\nodejs`(macOS/Linux 为 `/usr/local/lib`、`/opt/homebrew/lib`、`/usr/lib`)→ PATH → `npm root -g`」,命中就写回 `dwsPath` 并落盘。装在哪、怎么彻底重装见使用说明 **10.0.1**;设置页「dws 路径」一行显示实际使用的可执行文件。 |
188
+ | 点了「退出」之后提示「未检测到 dws 命令」 | 0.4.2 已修:退出登录只清登录态,不再把 `tool.ok` 置为 false。状态行显示黄点「未登录钉钉」,直接点「钉钉授权」重新登录即可。判断口诀:黄点「未登录钉钉」= 可以点授权;红点「dws 未就绪」= 才需要装 dws。详见使用说明 **10.1.3**。 |
188
189
  | 点「钉钉授权」后没有二维码 | **默认 loopback 模式本来就没有二维码**——弹框里是授权说明 + **打开钉钉授权页面** 按钮,浏览器会自动弹出授权页;没弹出就点 **复制链接**。只有 `loginMode=device` 才显示二维码。 |
189
190
  | 授权页要求输入授权码 | 说明 `loginMode` 是 `device`(设备流)。改回 `loopback` 并重启 DSH,即浏览器授权、无需授权码。 |
190
191
  | 每授权一次「实时监听中 · N 个组织」就多一个 | 钉钉登录态按组织累积。`loginReset: true`(默认)会在重新授权前先 `dws auth logout --yes` 退出全部旧组织,只保留本次登录的组织;想把现有组织清干净就点面板右上 **退出** 再重新授权。 |
@@ -206,12 +207,13 @@ npm i -g dsh-plugin-dingtalk-bridge@0.4.0 # 或装到 profile 的 dependencies
206
207
  | 来源(群 / 发送人)太多,胶囊被截断吗 | 不会。来源条只占一行:前 5 个 + 「还有 N 个 ▾」,点开是完整面板(可搜索、内滚、清除筛选),选中的来源置顶。 |
207
208
  | AI 回复好像只看一条消息,太片面 | 每个来源有独立滑动窗口(默认最近 5 条,`windowMessages` 可调),含对方连续发言与我发过的回复;没攒满时用钉钉最新 10 条补齐。刚换新联系人时上下文少是正常的。 |
208
209
  | 接口返回 401 / 403 | 401:钉钉登录态过期,点 **钉钉授权** 重新登录。403:被 DSH 的 Host/Origin 护栏拒绝(跨站或会话失效),刷新页面重开面板,不要绕过前端直接调接口。 |
210
+ | 发送报「请求参数不能为空」/ 输入框里是一大段 JSON | 0.4.3 已修:模型返回 `reply: ""`(常见于看不见图片)时不再把整段 JSON 当草稿;卡片改为「需人工确认」并提示原因,输入框等你手写,宿主也会在发送前拦下「像 JSON 的正文」。 |
209
211
 
210
212
  ## 开发与自测
211
213
 
212
214
  ```bash
213
215
  npm run check # node --check 全部源码
214
- npm test # 单元测试:76 个用例,使用临时 DSH_HOME,不写真实状态
216
+ npm test # 单元测试:94 个用例,使用临时 DSH_HOME,不写真实状态
215
217
  npm run smoke:index # 校验 index.js 契约:Config 校验、prefix 路由注册、effect 数量、清理
216
218
  npm run smoke # 真机监听冒烟:dws 版本、profile、listener 状态、GET /state
217
219
  npm run smoke:login # 两种授权模式:loopback(链接含 127.0.0.1、无授权码)→ device(授权码 + PNG 二维码)
package/client.js CHANGED
@@ -24,7 +24,7 @@ window.__ModuleLoader__.load({
24
24
  const POLL_MS = 5000
25
25
  const SOFT_POLL_MS = 30000
26
26
  // 与 package.json 的 version 保持一致:宿主上报的 build 前缀对不上,说明 DSH 还在跑旧宿主代码。
27
- const CLIENT_BUILD = '0.4.1'
27
+ const CLIENT_BUILD = '0.4.3'
28
28
  // 源码仓库(GitHub):Star 与 Issue 入口都指向这里
29
29
  const REPO_URL = 'https://github.com/gcggcg/dsh-plugin-dingtalk-bridge'
30
30
  const ISSUES_URL = `${REPO_URL}/issues`
@@ -954,6 +954,14 @@ window.__ModuleLoader__.load({
954
954
  )
955
955
  : null,
956
956
 
957
+ showComposer && !suggesting && suggestion === '' && message.suggestion && message.suggestion.ok === false
958
+ ? h(
959
+ 'div',
960
+ { className: 'dsh-dt-hint', 'data-no-suggestion': '1', style: { marginTop: 8, color: C.warn } },
961
+ `模型没给出可发送的建议:${message.suggestion.reason || '需要你手动回复'}`,
962
+ )
963
+ : null,
964
+
957
965
  showComposer && !suggesting
958
966
  ? h(
959
967
  'div',
@@ -965,7 +973,13 @@ window.__ModuleLoader__.load({
965
973
  value,
966
974
  suggestion,
967
975
  disabled: busy,
968
- placeholder: replied ? '已回复,可继续补充…' : message.kind === 'group' && groupReplyAt !== false ? `回复 @${message.sender},Tab 补全建议…` : '输入回复,Tab 补全建议…',
976
+ placeholder: replied
977
+ ? '已回复,可继续补充…'
978
+ : suggestion !== ''
979
+ ? message.kind === 'group' && groupReplyAt !== false
980
+ ? `回复 @${message.sender},Tab 补全建议…`
981
+ : '输入回复,Tab 补全建议…'
982
+ : '模型没给出建议,请手动输入回复…',
969
983
  onChange: setValue,
970
984
  onSubmit: submit,
971
985
  ariaLabel: `回复 ${message.sender}`,
@@ -1368,9 +1382,11 @@ window.__ModuleLoader__.load({
1368
1382
  ? `正在安装 dws${install.targetVersion ? ` v${install.targetVersion}` : ''}${currentStep ? ` · ${currentStep.label}` : ''}`
1369
1383
  : failed
1370
1384
  ? 'dws 安装 / 更新失败'
1371
- : ready
1372
- ? `发现新版本 dws v${latest}(当前 v${current})`
1373
- : '未检测到 dws 命令'
1385
+ : status === 'missing'
1386
+ ? '未检测到 dws 命令'
1387
+ : ready
1388
+ ? `发现新版本 dws v${latest}(当前 v${current})`
1389
+ : 'dws 暂时不可用'
1374
1390
 
1375
1391
  const body = installing
1376
1392
  ? `第 ${Math.min(doneCount + 1, steps.length || 1)}/${steps.length || 1} 步${currentStep && currentStep.detail ? ` · ${currentStep.detail}` : ''} · 已用时 ${elapsed}s。装完会自动重新检测并开始接收消息。`
@@ -2191,7 +2207,7 @@ window.__ModuleLoader__.load({
2191
2207
  value,
2192
2208
  suggestion,
2193
2209
  disabled: busy,
2194
- placeholder: '输入回复,Tab 补全建议…',
2210
+ placeholder: suggestion !== '' ? '输入回复,Tab 补全建议…' : '模型没给出建议,请手动输入回复…',
2195
2211
  onChange: setValue,
2196
2212
  onSubmit: send,
2197
2213
  ariaLabel: '确认回复内容',
@@ -1483,6 +1483,21 @@ dws profile list -f json # 查看当前组织与 status(只有 active 会
1483
1483
 
1484
1484
  如果你还在旧版本:重启 DSH 升级到 0.4.0 即可;临时绕过办法是先用命令行登录一次(`dws auth login --no-browser --yes`),有了登录组织后授权入口就能用了。
1485
1485
 
1486
+ ### 10.1.3 点了「退出」之后提示「未检测到 dws 命令」(0.4.2 已修)
1487
+
1488
+ **现象**:dws 明明装着、刚刚还在正常收消息,点右上角 **退出** 之后,状态行变成「dws 未就绪 · 已退出钉钉登录」,下面还冒出「未检测到 dws 命令」的安装卡,像是 dws 被卸载了。
1489
+
1490
+ **原因**:旧版 `logoutAll()` 在退出登录时顺手把 `tool.ok` 置成了 `false`(还把「已退出登录」这句提示塞进了 `tool.error`),界面据此判定「dws 坏了」—— 和 [10.1.2](#1012-点钉钉授权提示-dws-not-ready040-已修) 是同一类问题:把「登录态」和「dws 是否可用」混成一个开关。
1491
+
1492
+ **现在(0.4.2 起)**:
1493
+
1494
+ - 退出登录只清 **登录态**(`profiles` / `currentProfile` / `selfName`)并停掉监听,`tool.ok` 保持不变;
1495
+ - 状态行显示黄点 **未登录钉钉 · 已退出钉钉登录,点击右上角「钉钉授权」重新登录**;
1496
+ - 安装卡不再出现,直接点 **钉钉授权** 就能重新扫码登录;
1497
+ - 只有 `dws --version` 真的失败时,才会出现「未检测到 dws 命令」的安装卡。
1498
+
1499
+ 判断口诀:**「未登录钉钉」是黄点、可以点授权;「dws 未就绪」是红点、才需要装 dws。**
1500
+
1486
1501
  ### 10.2 顶部显示「监听异常」或一直「正在连接钉钉…」
1487
1502
 
1488
1503
  先点面板右上角 **重启监听**(等价于重跑 `dws --version` + 重建全部监听)。仍然不行时在终端核对订阅状态:
@@ -1747,6 +1762,32 @@ node scripts/duplicate-smoke.mjs <你的 openDingTalkId>
1747
1762
 
1748
1763
  > 拆分的判断权在模型:内容确实只有一句话时它会保持单条,这是刻意设计,避免把一句话拆成多条骚扰对方。
1749
1764
 
1765
+ ### 10.22 发送时报「请求参数不能为空」/ 输入框里出现一大段 JSON
1766
+
1767
+ 0.4.3 起已修复,遇到时先确认版本(设置页「宿主版本」≥ `0.4.3`)。
1768
+
1769
+ **现象**:消息卡片显示「发送失败:请求参数不能为空」,或者输入框里的灰色建议是一整段
1770
+ `{"reply":"","confidence":0.0,"needsHuman":true,…}`。
1771
+
1772
+ **原因**:模型按约定返回了 JSON,但 `reply` 是空串(最常见于「对方只发了图片,而当前模型不支持看图」)。
1773
+ 旧版本会把这段 JSON 当成草稿塞进输入框;而 dws 发消息时会把正文包进 `{"content": …}` 的结构化消息体,
1774
+ 正文本身就是 JSON 时服务端解析出空内容,于是报「请求参数不能为空」。
1775
+
1776
+ **现在的行为**:
1777
+
1778
+ | 环节 | 0.4.3 的处理 |
1779
+ | --- | --- |
1780
+ | 解析模型输出 | `reply` 为空时不再退化成「原文草稿」,而是标记 `format: 'json-empty'`、`needsHuman: true`,保留模型的 `reason` |
1781
+ | 消息卡片 | 状态显示 **需人工确认**(不是刺眼的「处理失败」),并提示「模型没给出可发送的建议:<原因>」 |
1782
+ | 输入框 | 灰字建议为空,placeholder 变为「模型没给出建议,请手动输入回复…」,「发送」按钮在你输入内容前保持禁用 |
1783
+ | 发送前兜底 | 如果正文仍像模型原始 JSON,宿主会直接拦下并提示「回复内容看起来是模型的原始 JSON,不是能直接发送的文本」,不会再打到钉钉 |
1784
+
1785
+ **你可以怎么做**:
1786
+
1787
+ 1. 直接手写一句回复(图片内容需要你自己看一眼);
1788
+ 2. 想让它自己看图,就换一个支持图片输入的模型(设置页「图片识别」一行会显示当前模型是否支持);
1789
+ 3. 点「换一条建议」重试,有时模型第二次会给出文字版回复。
1790
+
1750
1791
  ## 11. 隐私与安全说明
1751
1792
 
1752
1793
  ### 11.1 数据落在哪里
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-plugin-dingtalk-bridge",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "钉钉私信 / 群 @ 消息实时接收、按发送人记录来源、AI 建议回复与自动回复插件",
@@ -21,7 +21,7 @@ import { buildSuggestPrompt, generateSuggestion, formatClock } from './suggest.j
21
21
 
22
22
  export const ROUTE_PREFIX = '/api/dingtalk-bridge'
23
23
  /** 每次改动宿主行为就换个标记:界面与 http-debug.log 都会带上,用来确认「跑的是新代码」。 */
24
- export const HOST_BUILD = '0.4.1-20261117T1810'
24
+ export const HOST_BUILD = '0.4.3-20261118T1410'
25
25
 
26
26
  /** 私信(单聊)全部消息。 */
27
27
  const O2O_KEY = 'user_im_message_receive_o2o_all'
@@ -56,6 +56,27 @@ export function looksLikeMissingCommand(text) {
56
56
  )
57
57
  }
58
58
 
59
+ /**
60
+ * 判断一段回复正文是不是「模型返回的原始 JSON」。
61
+ *
62
+ * 为什么需要:dws 发消息时会把 `--text` 的值包进 `{"content": …}` 的结构化消息体,
63
+ * 如果正文本身就是一段 JSON(且没有 content 字段),服务端解析后内容为空,
64
+ * 就会报「请求参数不能为空」——用户完全看不懂这个错。
65
+ */
66
+ export function looksLikeJsonEnvelope(text) {
67
+ const raw = String(text ?? '').trim()
68
+ if (!raw.startsWith('{') || !raw.endsWith('}')) return false
69
+ try {
70
+ const payload = JSON.parse(raw)
71
+ if (!payload || typeof payload !== 'object' || Array.isArray(payload)) return false
72
+ return ['reply', 'replies', 'needsHuman', 'needs_human', 'confidence', 'reason'].some(
73
+ (key) => key in payload,
74
+ )
75
+ } catch {
76
+ return false
77
+ }
78
+ }
79
+
59
80
  function asBool(value, fallback) {
60
81
  return typeof value === 'boolean' ? value : fallback
61
82
  }
@@ -1115,12 +1136,12 @@ export class DingTalkBridgeService {
1115
1136
  this.state.profiles = []
1116
1137
  this.state.currentProfile = ''
1117
1138
  this.state.selfName = ''
1118
- this.tool = {
1119
- ok: false,
1120
- version: this.tool.version,
1121
- error: ok ? '已退出钉钉登录,点击「钉钉授权」重新登录' : `退出登录未成功:${describeFailure(result)}`,
1122
- checkedAt: Date.now(),
1123
- }
1139
+ // 退出登录只影响「登录态」,跟 dws 本身能不能用无关:
1140
+ // 以前这里把 tool.ok 置成 false,界面就会显示「dws 未就绪 / 未检测到 dws 命令」,
1141
+ // 让人以为 dws 没装,还会去点「安装 dws」。现在只改登录提示。
1142
+ this.state.loginNotice = ok
1143
+ ? '已退出钉钉登录,点击右上角「钉钉授权」重新登录'
1144
+ : `退出登录未成功:${describeFailure(result)}`
1124
1145
 
1125
1146
  if (!quiet) {
1126
1147
  this.login = {
@@ -2202,10 +2223,12 @@ export class DingTalkBridgeService {
2202
2223
 
2203
2224
  // 手动触发的「换一条建议 / 生成建议」只更新建议本身,绝不代替用户发送。
2204
2225
  if (manual) {
2205
- current.status = result.ok ? 'suggested' : 'failed'
2206
- current.needsHuman = false
2226
+ // 「模型按格式回了、但 reply 是空串」(例如它看不见图片)不是错误,
2227
+ // 只是需要人来写,所以标成 needs-human,而不是刺眼的「处理失败」。
2228
+ current.status = result.ok ? 'suggested' : result.format === 'json-empty' ? 'needs-human' : 'failed'
2229
+ current.needsHuman = result.ok ? false : result.format === 'json-empty' && this.config.notifyModal
2207
2230
  current.dismissed = false
2208
- current.error = result.ok ? '' : result.reason
2231
+ current.error = result.ok ? '' : result.format === 'json-empty' ? '' : result.reason
2209
2232
  this.broadcast()
2210
2233
  this.saveState().catch(() => {})
2211
2234
  return current
@@ -2225,9 +2248,10 @@ export class DingTalkBridgeService {
2225
2248
  })
2226
2249
  }
2227
2250
  } else {
2228
- current.status = result.ok ? 'suggested' : 'failed'
2251
+ // 自动回复没开:有建议就等人确认(suggested),模型明确说「给不出」就标 needs-human。
2252
+ current.status = result.ok ? 'suggested' : result.format === 'json-empty' ? 'needs-human' : 'failed'
2229
2253
  current.needsHuman = false
2230
- current.error = result.ok ? '' : result.reason
2254
+ current.error = result.ok ? '' : result.format === 'json-empty' ? '' : result.reason
2231
2255
  // 自动回复在生成过程中被关闭、或这条消息早于本次开启时间:只留建议,不自动发送。
2232
2256
  if (this.state.autoReplyTouched && result.ok && !current.autoCancelled) {
2233
2257
  current.autoCancelled = !this.state.autoReply || !this.canAutoReply(current)
@@ -2245,6 +2269,24 @@ export class DingTalkBridgeService {
2245
2269
  const body = composeReplyBody({ record, text, replyPrefix: this.config.replyPrefix, atSender })
2246
2270
  if (body === '') return false
2247
2271
 
2272
+ // 防呆:模型偶尔会把整段 JSON 当正文返回(例如 reply 为空、只有 reason 的情况)。
2273
+ // 那串东西发到钉钉会被当成结构化消息体,服务端直接报「请求参数不能为空」,
2274
+ // 所以在这里拦下,换成一句人能看懂的错误。
2275
+ if (looksLikeJsonEnvelope(body)) {
2276
+ record.reply = {
2277
+ text: body.slice(0, 400),
2278
+ at: Date.now(),
2279
+ atSender,
2280
+ ok: false,
2281
+ error: '回复内容看起来是模型的原始 JSON,不是能直接发送的文本。请点「换一条建议」,或手动输入回复后再发送。',
2282
+ auto,
2283
+ }
2284
+ record.status = 'failed'
2285
+ this.broadcast()
2286
+ await this.saveState().catch(() => {})
2287
+ return false
2288
+ }
2289
+
2248
2290
  // 自动回复在「模型思考的这几秒」里可能已被用户关掉,或这条消息早于本次开启时间:
2249
2291
  // 发之前再判一次状态,避免关掉开关后仍然把回复发出去。
2250
2292
  if (auto && !this.canAutoReply(record)) {
@@ -105,6 +105,19 @@ export function parseSuggestion(text) {
105
105
  format: 'json',
106
106
  }
107
107
  }
108
+
109
+ // 模型按约定返回了 JSON,但 reply 是空串(典型:只有图片、它看不见图)。
110
+ // 这时绝不能把整段 JSON 当成草稿塞进输入框——那串东西既不是人话,
111
+ // 发到钉钉还会被当成结构化消息体,报「请求参数不能为空」。
112
+ return {
113
+ ok: false,
114
+ reply: '',
115
+ replies: [],
116
+ confidence: clampConfidence(payload.confidence ?? payload.score),
117
+ needsHuman: true,
118
+ reason: String(payload.reason ?? payload.note ?? '模型没有给出可发送的回复内容,需要你手动回复').slice(0, 240),
119
+ format: 'json-empty',
120
+ }
108
121
  }
109
122
 
110
123
  const fallback = cleanReply(raw)