dsh-plugin-dingtalk-bridge 0.4.0 → 0.4.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.md CHANGED
@@ -26,7 +26,7 @@
26
26
  - **Star / Issue 入口**:面板右上角「使用说明」旁边有 **★ Star** 与 **提交 Issue**,说明卡底部也有同样的链接与 npm 入口,方便反馈问题。
27
27
  - **彩色插件图标**:侧边栏与面板标题使用 3D 机器人图标,宿主提供 `GET /icon` 路由,加载失败时回退单色 SVG。
28
28
  - **授权与去重**:默认 `loginMode=loopback`(浏览器授权,**不需要授权码**);`loginReset=true` 时重新授权会先 `dws auth logout --yes` 退出旧组织,只保留本次登录的组织;也支持手动「退出」。
29
- - **dws 自动检测 / 安装 / 更新**:插件启动时只做检测,不会偷偷装东西。如果这台机器没有 `dws`,面板顶部会出现一条提示,点 **安装 dws** 即自动安装:有 npm 时执行 `npm install -g dingtalk-workspace-cli`(含 npm 12 会拦掉的 `install.js` 准备步骤),**没有 npm 时直接下载 npm 包 tarball 解压到插件数据目录**(自带纯 Node 解压器,不依赖系统 tar),两条路径都会自动定位 `vendor/dws.exe`。已装的话点 **检查更新** 能看到最新版本,再点 **更新到 vX** 升级。收件箱提示卡与设置页按钮都能触发,npm 源可用 `dwsRegistry` 换成国内镜像。
29
+ - **dws 自动检测 / 安装 / 更新**:插件启动时只做检测,不会偷偷装东西。如果这台机器没有 `dws`,面板顶部会出现一条提示,点 **安装 dws** 即自动安装:有 npm 时执行 `npm install -g dingtalk-workspace-cli`(含 npm 12 会拦掉的 `install.js` 准备步骤),**没有 npm 时直接下载 npm 包 tarball 解压到插件数据目录**(自带纯 Node 解压器,不依赖系统 tar),两条路径都会自动定位 `vendor/dws.exe`。已装的话点 **检查更新** 能看到最新版本,再点 **更新到 vX** 升级。收件箱提示卡与设置页按钮都能触发,npm 源可用 `dwsRegistry` 换成国内镜像。安装过程分五步展示进度与耗时,tarball 会本地缓存,同版本重装秒完成;**卸载插件再装回来也不会误报「未安装」**——配置路径不可用时会自动去插件数据目录、npm 全局目录、PATH 里找一遍。
30
30
  - **安装进度可见**:安装时卡片会显示 **五步清单**(读取版本信息 → 下载安装包 → 解压 → 准备可执行文件 → 校验版本),每步带 ✓/◐/○ 与耗时,下载实时显示 MB 与百分比,标题还带「已用时 Ns」;同一版本重复安装直接命中 `dws-cache` 缓存。
31
31
  - **「没登录」不等于「dws 坏了」**:dws 可用但还没登录时,面板显示黄点「未登录钉钉 · 点右上角「钉钉授权」」,授权入口照常可用(0.4.0 修掉了旧版 `dws not ready` 死循环)。
32
32
  - **演练模式**:`dryRun=true` 时只预览将发送的内容(`dws --dry-run`),不真正发到钉钉,适合先试跑。
@@ -124,7 +124,7 @@ npm i -g dsh-plugin-dingtalk-bridge@0.4.0 # 或装到 profile 的 dependencies
124
124
  | 配置 | 类型 / 默认 | 说明 |
125
125
  | --- | --- | --- |
126
126
  | `enabled` | bool / `true` | 总开关,关闭后不启动事件监听、不注册路由。 |
127
- | `dwsPath` | string / `dws` | dws 可执行文件路径;已在 PATH 里就留 `dws`。安装成功后插件会自动指向 npm 全局包里的 `vendor/dws.exe`。 |
127
+ | `dwsPath` | string / `dws` | dws 可执行文件路径;已在 PATH 里就留 `dws`。安装成功后插件会自动指向 `vendor/dws.exe`(并写进 `state.json`);这个值不可用时插件会自动在常见安装位置里找回已装的 dws。 |
128
128
  | `dwsRegistry` | string / `https://registry.npmjs.org/` | 自动安装 / 更新 dws 时用的 npm 源,只在点「安装 dws」「更新 dws」时生效,可换国内镜像。 |
129
129
  | `profiles` | string[] / `[]` | 只监听这些组织(profile 标识);留空表示跟随 dws 当前登录账号。 |
130
130
  | `listenDirect` | bool / `true` | 是否接收私信。 |
@@ -184,6 +184,7 @@ npm i -g dsh-plugin-dingtalk-bridge@0.4.0 # 或装到 profile 的 dependencies
184
184
  | --- | --- |
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
+ | 卸载插件再装回来,又说「未安装 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 路径」一行显示实际使用的可执行文件。 |
187
188
  | 点「钉钉授权」后没有二维码 | **默认 loopback 模式本来就没有二维码**——弹框里是授权说明 + **打开钉钉授权页面** 按钮,浏览器会自动弹出授权页;没弹出就点 **复制链接**。只有 `loginMode=device` 才显示二维码。 |
188
189
  | 授权页要求输入授权码 | 说明 `loginMode` 是 `device`(设备流)。改回 `loopback` 并重启 DSH,即浏览器授权、无需授权码。 |
189
190
  | 每授权一次「实时监听中 · N 个组织」就多一个 | 钉钉登录态按组织累积。`loginReset: true`(默认)会在重新授权前先 `dws auth logout --yes` 退出全部旧组织,只保留本次登录的组织;想把现有组织清干净就点面板右上 **退出** 再重新授权。 |
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.0'
27
+ const CLIENT_BUILD = '0.4.1'
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`
@@ -298,7 +298,7 @@ window.__ModuleLoader__.load({
298
298
  margin:12px 20px;box-sizing:border-box}
299
299
  .dsh-dt-kv{display:grid;grid-template-columns:150px 1fr;gap:8px 14px;font-size:12.5px;margin-top:12px}
300
300
  .dsh-dt-kv dt{color:${C.sub}}
301
- .dsh-dt-kv dd{margin:0;color:${C.text};word-break:break-word}
301
+ .dsh-dt-kv dd{margin:0;color:${C.text};word-break:break-word;overflow-wrap:anywhere}
302
302
  @keyframes dsh-dt-pulse{0%,100%{opacity:1}50%{opacity:.4}}
303
303
  @keyframes dsh-dt-fade{from{opacity:0}to{opacity:1}}
304
304
  @keyframes dsh-dt-rise{from{opacity:0;transform:translateY(10px) scale(.99)}to{opacity:1;transform:none}}
@@ -2560,6 +2560,10 @@ window.__ModuleLoader__.load({
2560
2560
  : 'npm 全局安装'
2561
2561
  : '—',
2562
2562
  ],
2563
+ [
2564
+ 'dws 路径',
2565
+ state && state.dwsPath ? state.dwsPath : '—',
2566
+ ],
2563
2567
  [
2564
2568
  'npm 安装源',
2565
2569
  state && state.install && state.install.source ? state.install.source : 'https://registry.npmjs.org/',
@@ -1399,6 +1399,36 @@ dws 版本 1.0.68
1399
1399
  - 想换国内镜像:配置项 `dwsRegistry` 改成 `https://registry.npmmirror.com/`(两条安装路径都会用它)。
1400
1400
  - 安装成功后插件会把 `vendor/dws.exe` 的绝对路径写进 `state.json`,**重启后自动沿用**(前提是你没有在配置里显式指定 `dwsPath`),所以即使安装目录不在 PATH 里也能一直用。
1401
1401
  - 设置页「安装方式」一行会显示本次用的是 **npm 全局安装** 还是 **免 npm(直接下载安装包解压)**。
1402
+ - **卸载插件再装回来也不会再误报「未安装」**(0.4.1 起):配置的路径不可用时,插件会自动去下面这些位置找一遍,找到就写回 `dwsPath` 并落盘 —— 详见 [10.0.1](#1001-dws-到底装在哪为什么卸载重装后会提示未安装)。
1403
+
1404
+ #### 10.0.1 dws 到底装在哪?为什么卸载重装后会提示「未安装」
1405
+
1406
+ **装在哪,取决于安装时有没有 npm:**
1407
+
1408
+ | 安装路径 | 实际落点 | 特点 |
1409
+ | --- | --- | --- |
1410
+ | 有 npm(走 `npm install -g`) | npm 全局目录:Windows 一般是 `%APPDATA%\npm\node_modules\dingtalk-workspace-cli\vendor\dws.exe`;macOS/Linux 一般是 `/usr/local/lib/node_modules/…` 或 `/opt/homebrew/lib/node_modules/…` | 和手动 `npm i -g` 装的一样,**全局可用**,但 npm 全局目录通常**不在 PATH 里** |
1411
+ | 没有 npm(或 npm 装失败) | 插件数据目录:`<DSH_HOME>/integrations/dingtalk-bridge/dws-runtime/node_modules/dingtalk-workspace-cli/vendor/dws.exe` | **不污染全局**,卸载插件/删数据目录就一起没了 |
1412
+
1413
+ 另外 tarball 会缓存一份在 `<DSH_HOME>/integrations/dingtalk-bridge/dws-cache/`,所以同版本重装不需要重新下载。
1414
+
1415
+ **为什么卸载插件再装回来会说「未安装」**(0.4.0 及更早):
1416
+
1417
+ 1. 插件记住的只是 `state.json` 里的 `dwsPath` 绝对路径;
1418
+ 2. 卸载插件(或清理数据目录)会把 `state.json` 一起删掉,路径记忆就丢了;
1419
+ 3. 插件退回默认值 `dws`,也就是「去 PATH 里找」——可 npm 全局目录一般不在 PATH 里;
1420
+ 4. 于是 `dws --version` 失败 → 误判成「未安装」(其实文件还在)。
1421
+
1422
+ **0.4.1 起会自动找回**:`dws --version` 失败时,插件按顺序探测下面这些位置,第一个能读出 `x.y.z` 的就直接采用并写回 `dwsPath`:
1423
+
1424
+ 1. 配置项 `dwsPath`(如果显式填了);
1425
+ 2. 插件数据目录的 `dws-runtime/node_modules/dingtalk-workspace-cli/vendor/`;
1426
+ 3. Node 同目录的 `node_modules/…/vendor/`、`../lib/node_modules/…/vendor/`;
1427
+ 4. Windows:`%APPDATA%\npm\node_modules`、`Program Files\nodejs\node_modules`、`Program Files (x86)\nodejs\node_modules`;macOS/Linux:`/usr/local/lib`、`/opt/homebrew/lib`、`/usr/lib`;
1428
+ 5. 最后才是 PATH 里的 `dws`;
1429
+ 6. 还找不到就用 `npm root -g` 问一次 npm 的全局目录。
1430
+
1431
+ **怎么确认它到底在哪**:设置页 →「dws 路径」一行显示的就是当前实际使用的可执行文件绝对路径(`/state` 里也有 `dwsPath`、`dwsRuntimeDir`、`dwsCacheDir` 三个字段)。想彻底重装:先点「退出」清登录态,再删掉上表对应的目录,然后点「安装 dws」。
1402
1432
 
1403
1433
  ### 10.1 顶部显示「dws 未就绪」
1404
1434
 
@@ -1895,6 +1925,9 @@ dws chat +messages-send --as user --group cidAbCdEf123456= --text "收到,下
1895
1925
  | `lastError` | string | 最近一次失败请求的摘要,如 `POST /api/dingtalk-bridge/action → 403` |
1896
1926
  | `install` | object | dws 安装 / 更新状态:`status`(`unknown`/`missing`/`installing`/`ready`/`error`)、`mode`(`npm` 或 `tarball`)、`currentVersion`、`latestVersion`、`updateAvailable`、`targetVersion`、`source`(npm 源)、`log`(失败日志尾部)、`latestError`、`steps`(`[{ key, label, status, detail, ms }]`,`status` 为 `pending`/`running`/`done`/`error`/`skipped`)、`startedAt`/`finishedAt`(毫秒时间戳,界面据此算「已用时」)、`checkedAt`。见 [10.0](#100-dws-没安装--想升级036-起支持免-npm) |
1897
1927
  | `loginNotice` | string | 「还没登录钉钉」的提示文案(与 `tool.ok` 分开:没登录不是 dws 故障)。为空表示已有可用登录组织。见 [10.1.2](#1012-点钉钉授权提示-dws-not-ready040-已修) |
1928
+ | `dwsPath` | string | 当前实际使用的 dws 可执行文件(可能是绝对路径,也可能就是 `dws`)。设置页「dws 路径」显示它;卸载重装后由自动探测回填。见 [10.0.1](#1001-dws-到底装在哪为什么卸载重装后会提示未安装) |
1929
+ | `dwsRuntimeDir` | string | 免 npm 安装路径的包目录(`<数据目录>/dws-runtime/node_modules/dingtalk-workspace-cli`) |
1930
+ | `dwsCacheDir` | string | tarball 缓存目录(`<数据目录>/dws-cache`),同版本重装直接命中 |
1898
1931
 
1899
1932
  ### 12.3 关键常量
1900
1933
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-plugin-dingtalk-bridge",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
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.0-20261117T1645'
24
+ export const HOST_BUILD = '0.4.1-20261117T1810'
25
25
 
26
26
  /** 私信(单聊)全部消息。 */
27
27
  const O2O_KEY = 'user_im_message_receive_o2o_all'
@@ -651,6 +651,68 @@ export class DingTalkBridgeService {
651
651
  return join(this.dataDir(), 'dws-runtime', 'node_modules', DWS_PACKAGE)
652
652
  }
653
653
 
654
+ /** 平台上的 dws 可执行文件名。 */
655
+ dwsExeName() {
656
+ return process.platform === 'win32' ? 'dws.exe' : 'dws'
657
+ }
658
+
659
+ /**
660
+ * 可能藏着 dws 的位置(按「便宜的先试」排序)。
661
+ * 为什么要这份清单:dws 可能装在 npm 全局目录、也可能装在插件数据目录,
662
+ * 而全局目录通常**不在 PATH 里**。卸载插件再装回来时 state.json 里的 dwsPath 会丢,
663
+ * 只靠 PATH 就会误报「未安装」——所以启动时要把这些位置都探一遍。
664
+ */
665
+ dwsCandidates() {
666
+ const exe = this.dwsExeName()
667
+ const vendor = (root) => join(root, DWS_PACKAGE, 'vendor', exe)
668
+ const list = []
669
+ const configured = String(this.config.dwsPath ?? '').trim()
670
+ if (configured !== '' && configured !== 'dws') list.push(configured)
671
+ list.push(join(this.runtimePackageDir(), 'vendor', exe))
672
+ const nodeDir = dirname(process.execPath)
673
+ list.push(vendor(join(nodeDir, 'node_modules')))
674
+ list.push(vendor(join(nodeDir, '..', 'lib', 'node_modules')))
675
+ if (process.platform === 'win32') {
676
+ if (process.env.APPDATA) list.push(vendor(join(process.env.APPDATA, 'npm', 'node_modules')))
677
+ const programFiles = process.env.ProgramFiles ?? 'C:\\Program Files'
678
+ list.push(vendor(join(programFiles, 'nodejs', 'node_modules')))
679
+ const programFilesX86 = process.env['ProgramFiles(x86)'] ?? 'C:\\Program Files (x86)'
680
+ list.push(vendor(join(programFilesX86, 'nodejs', 'node_modules')))
681
+ } else {
682
+ for (const root of ['/usr/local/lib/node_modules', '/opt/homebrew/lib/node_modules', '/usr/lib/node_modules']) {
683
+ list.push(vendor(root))
684
+ }
685
+ }
686
+ // 最后才试 PATH 里的 dws(配置里显式写了别的路径时也兜底)。
687
+ list.push('dws')
688
+ return [...new Set(list.filter((item) => item !== ''))]
689
+ }
690
+
691
+ /** 挨个试候选路径,返回第一个能跑出 `x.y.z` 的。 */
692
+ async discoverDws() {
693
+ for (const candidate of this.dwsCandidates()) {
694
+ // 明确是文件路径的,先看文件在不在——省掉一次进程启动(找不到时这步最费时间)。
695
+ if (candidate !== 'dws' && !existsSync(candidate)) continue
696
+ const probe = await runDws(candidate, ['--version'], { timeoutMs: 8000 })
697
+ const version = /(\d+\.\d+\.\d+)/.exec(`${probe.stdout}\n${probe.stderr}`)?.[1] || ''
698
+ if (probe.code === 0 && version !== '') return { command: candidate, version }
699
+ }
700
+ // 还有一处:npm 全局目录(prefix 可能不在上面这些常见位置)。
701
+ const npm = await this.findNpm()
702
+ if (npm !== '') {
703
+ const root = await this.npmGlobalRoot(npm)
704
+ if (root !== '') {
705
+ const candidate = join(root, DWS_PACKAGE, 'vendor', this.dwsExeName())
706
+ if (existsSync(candidate)) {
707
+ const probe = await runDws(candidate, ['--version'], { timeoutMs: 8000 })
708
+ const version = /(\d+\.\d+\.\d+)/.exec(`${probe.stdout}\n${probe.stderr}`)?.[1] || ''
709
+ if (probe.code === 0 && version !== '') return { command: candidate, version }
710
+ }
711
+ }
712
+ }
713
+ return null
714
+ }
715
+
654
716
 
655
717
  /** `npm root -g`:全局 node_modules,用来找安装后的包目录。 */
656
718
  async npmGlobalRoot(npm) {
@@ -920,7 +982,7 @@ export class DingTalkBridgeService {
920
982
  return this.installDws(target)
921
983
  }
922
984
 
923
- async checkTool() {
985
+ async checkTool({ discover = true } = {}) {
924
986
  const result = await runDws(this.config.dwsPath, ['--version'], { timeoutMs: 15000 })
925
987
  const output = `${result.stdout}\n${result.stderr}`.trim()
926
988
  const version = /(\d+\.\d+\.\d+)/.exec(output)?.[1] || ''
@@ -930,6 +992,17 @@ export class DingTalkBridgeService {
930
992
  error: result.code === 0 ? '' : describeFailure(result),
931
993
  checkedAt: Date.now(),
932
994
  }
995
+ // 配置里的路径不可用时,去常见位置找一遍:卸载重装后 state.json 会丢 dwsPath,
996
+ // 而 npm 全局目录通常不在 PATH 里,不主动找就会误报「未安装」。
997
+ if (!this.tool.ok && discover) {
998
+ const found = await this.discoverDws()
999
+ if (found && found.command !== this.config.dwsPath) {
1000
+ this.config.dwsPath = found.command
1001
+ await this.saveState()
1002
+ this.noteRequest('DISCOVER', ROUTE_PREFIX, 200, found.command)
1003
+ return this.checkTool({ discover: false })
1004
+ }
1005
+ }
933
1006
  const missing = !this.tool.ok && looksLikeMissingCommand(output)
934
1007
  const current = version || this.install.currentVersion || ''
935
1008
  this.install = {
@@ -2674,6 +2747,9 @@ export class DingTalkBridgeService {
2674
2747
  started: this.started,
2675
2748
  tool: this.tool,
2676
2749
  install: this.install,
2750
+ dwsPath: this.config.dwsPath,
2751
+ dwsRuntimeDir: this.runtimePackageDir(),
2752
+ dwsCacheDir: join(this.dataDir(), 'dws-cache'),
2677
2753
  selfName: this.state.selfName,
2678
2754
  currentProfile: this.state.currentProfile,
2679
2755
  profiles: this.state.profiles,