dsh-aiyayi 1.5.32 → 1.5.34
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 +56 -1
- package/README.md +2 -2
- package/cordis.patch.yml +9 -0
- package/lib/feishu-logic.js +247 -1
- package/lib/feishu.js +165 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
> **维护约定**:新版本发布时在顶部加条目。本文件由 README 正文拆出(2026-11-09 重构):版本条目与踩坑记录原文照录,数字、时间、日期一律不改(量化保真是本插件的规矩)。版本事实以 npm registry 为准。
|
|
4
4
|
>
|
|
5
|
-
> **npm 发布一览(2026-11-11 核对 registry,`npm view dsh-aiyayi versions --json`)**:latest = **1.5.28**(2026-11-11 发布;**初版记为 1.5.27,1.5.29 发版时据 registry 更正**)。已发布:1.0.0、…、1.5.7、**1.5.14**、**1.5.15**、**1.5.16**、**1.5.17**、**1.5.20**、**1.5.21**、**1.5.22**、**1.5.23**、**1.5.24**、**1.5.25**、**1.5.26**、**1.5.27**、**1.5.28**;跳号 1.3.0、1.4.1、1.4.2、1.5.0、1.5.1、1.5.2、1.5.3、**1.5.8–1.5.13** 从未发布到 npm(本地开发线:1.5.8–1.5.11 飞书桥接、1.5.12 思考纪律约束、1.5.13 内置技能同步+feishu-setup 骨架);1.5.14 已发布(坑 #58–#67 修复);1.5.15 已发布(扫码接入改造);1.5.16 已发布(feishu-setup 技能脚本入包);1.5.17 已发布(坑 #68 重入保护修复);**1.5.18 / 1.5.19 也从未发布到 npm**(见下方 1.5.18 / 1.5.19 条目——它们的代码最终随 1.5.20 一起进 npm);**1.5.20 已发布**(飞书正文改走 interactive 卡片 + lark_md,含 1.5.19 附件收发与问答卡片、1.5.20 markdown 渲染,以及 create-bot v6 权限集中化);**1.5.21 已发布**(修飞书回复正文首尾空白行,坑 #72);**1.5.22 已发布**(多应用多长连接改造+chat 归属机制,见下方 1.5.22 条目);**1.5.23–1.5.25 已发布**(230001 边界/补 import/补 feishuCode 挖码三轮装版断言增量修复,见下方对应条目);**1.5.26 已发布**(回发多文本段覆盖丢段修复,坑 #73,见下方 1.5.26 条目);**1.5.27 已发布**(微信通道并入,默认关闭,见下方 1.5.27 条目);**1.5.28 已发布**(PowerShell 版本体检 + 微信通道收尾,见下方 1.5.28 条目);**1.5.29 已发布**(发布包排除 `test/` + 测试残留空目录清理,见下方 1.5.29 条目);**1.5.30 已发布**(create-bot 注释改用 `<记忆根>` 占位,见下方 1.5.30 条目);**1.5.31 已发布**(PowerShell 体检 `major:null` 根因修复:探测链补上 powershell.exe 一级,见下方 1.5.31 条目);**1.5.32
|
|
5
|
+
> **npm 发布一览(2026-11-11 核对 registry,`npm view dsh-aiyayi versions --json`)**:latest = **1.5.28**(2026-11-11 发布;**初版记为 1.5.27,1.5.29 发版时据 registry 更正**)。已发布:1.0.0、…、1.5.7、**1.5.14**、**1.5.15**、**1.5.16**、**1.5.17**、**1.5.20**、**1.5.21**、**1.5.22**、**1.5.23**、**1.5.24**、**1.5.25**、**1.5.26**、**1.5.27**、**1.5.28**;跳号 1.3.0、1.4.1、1.4.2、1.5.0、1.5.1、1.5.2、1.5.3、**1.5.8–1.5.13** 从未发布到 npm(本地开发线:1.5.8–1.5.11 飞书桥接、1.5.12 思考纪律约束、1.5.13 内置技能同步+feishu-setup 骨架);1.5.14 已发布(坑 #58–#67 修复);1.5.15 已发布(扫码接入改造);1.5.16 已发布(feishu-setup 技能脚本入包);1.5.17 已发布(坑 #68 重入保护修复);**1.5.18 / 1.5.19 也从未发布到 npm**(见下方 1.5.18 / 1.5.19 条目——它们的代码最终随 1.5.20 一起进 npm);**1.5.20 已发布**(飞书正文改走 interactive 卡片 + lark_md,含 1.5.19 附件收发与问答卡片、1.5.20 markdown 渲染,以及 create-bot v6 权限集中化);**1.5.21 已发布**(修飞书回复正文首尾空白行,坑 #72);**1.5.22 已发布**(多应用多长连接改造+chat 归属机制,见下方 1.5.22 条目);**1.5.23–1.5.25 已发布**(230001 边界/补 import/补 feishuCode 挖码三轮装版断言增量修复,见下方对应条目);**1.5.26 已发布**(回发多文本段覆盖丢段修复,坑 #73,见下方 1.5.26 条目);**1.5.27 已发布**(微信通道并入,默认关闭,见下方 1.5.27 条目);**1.5.28 已发布**(PowerShell 版本体检 + 微信通道收尾,见下方 1.5.28 条目);**1.5.29 已发布**(发布包排除 `test/` + 测试残留空目录清理,见下方 1.5.29 条目);**1.5.30 已发布**(create-bot 注释改用 `<记忆根>` 占位,见下方 1.5.30 条目);**1.5.31 已发布**(PowerShell 体检 `major:null` 根因修复:探测链补上 powershell.exe 一级,见下方 1.5.31 条目);**1.5.32 已发布**(行为变更:首次提示同时告知微信 ClawBot 能力 + 「对接默认落到发起对接的会话」固化为 persona 准则/文档;不含路由代码改动,见下方 1.5.32 条目);**1.5.33 已发布**(同一准则落成代码:未绑定 chat 三档复用,不再无条件新建会话,见下方 1.5.33 条目);**1.5.34 为本次发版版**(纯测试修复:`presence-clock` 跨午夜偶发失败,`lib/` 零改动,见下方 1.5.34 条目;其 npm 发布状态以 registry 为准)。
|
|
6
6
|
>
|
|
7
7
|
> **环境标识约定(2026-11-10 新增)**:写版本条目/坑条目时,**顺手记录当时的 智能体工具-智能体模式-模型供应商-模型**(如 **DSH-创造模式-loomy讯飞-GLM5.3FLASH**),写在条目内(紧跟 git 记录行、或坑条目首行),方便回溯该条目是在哪套智能体/模型环境下产出与验证的。
|
|
8
8
|
|
|
@@ -10,6 +10,61 @@
|
|
|
10
10
|
|
|
11
11
|
---
|
|
12
12
|
|
|
13
|
+
## 1.5.34(2026-11-12 修复:`presence-clock.test.mjs` 跨午夜偶发失败——**只改测试,无运行时行为变化**)
|
|
14
|
+
|
|
15
|
+
- 环境:DSH-执行智能体(DeepSeek Harness 子会话,模型名未确证)
|
|
16
|
+
- 症状:`node --test "test/*.test.mjs"` 偶尔报 `test/presence-clock.test.mjs` 失败,重跑即过。2026-11-11→12 午夜实跑中招一次(1.5.33 发版前那次全量跑)。
|
|
17
|
+
- **根因(实测确认)**:旧判据拿「取样的 `new Date()`」与「注入的时间」比**年月日是否相同**(`sameDay`)。而注入发生在 `presence.js:1195` 的 `const now = Date.now()`(handler 内部),取样在 handler 返回之后——只要这两个点跨过 `00:00:00`,注入写 `23:59:59`、取样已是次日 `00:00:00`,`sameDay` 必挂,**而插件行为完全正常**。这是测试自身的日历日判据缺陷,跨午夜必挂、别的时刻永远不会暴露。
|
|
18
|
+
- 修法(**只动 `test/`,`lib/` 一行未改**):判据换成**夹逼窗口**——注入时刻必须落在「调用前 `beforeMs` → 调用后 `afterMs`」之间(epoch 毫秒比较,附加 2s 容差抗时钟微调)。跨午夜天然成立:`23:59:59` 与 `00:00:01` 的 epoch 差就是 2 秒,不涉及"同一天"这种日历概念。
|
|
19
|
+
- 同时把**断言做强**了(不只换判据,还补了三条纯格式正确性校验,都不依赖"现在几点"):① 星期字符必须与注入日期自洽(`getDay()` 对得上);② 时区偏移必须与运行环境一致(支持 `+05:30` 这类半小时时区);③ 注入时刻必须落在夹逼窗口内(偏差实测 −67ms)。
|
|
20
|
+
- **取证(不是只跑一次通过)**:新增独立脚本 `test/presence-midnight-proof.mjs`(文件名不带 `.test.mjs`,**不进全量通配**,不参与 158 项):把 `globalThis.Date` 打桩,让真实注入发生在 `23:59:59.600`,再把桩时钟推过午夜到 `00:00:00.400`,跑真实 pre-step 注入,然后用两套判据同时判:
|
|
21
|
+
- 旧判据 `sameDay` → **失败**(复现那次偶发);
|
|
22
|
+
- 新判据 夹逼窗口 → **通过**。
|
|
23
|
+
- 实跑输出:注入行 `[时间] 现在 2026-11-12 23:59:59 周四 (UTC+08:00)`,调用窗口 `[1794499199600, 1794499200799]`,`PROVEN:真实注入在 23:59:59,取样跨到次日 00:00:00 —— 旧判据挂、新判据过`,exit 0。
|
|
24
|
+
- 主测试内另加 5 条**自证 + 反证**(不依赖桩时钟,任何时刻都跑):(a) 构造 23:59:59 / 次日 00:00:01 一对时间戳,旧判据说"同一天"必须为假;(b) 新判据必须为真;(c) 两者偏差 ≤2s;(d) 把窗口挪到两天后,新判据必须报错;(e) 注入年份写错一年,新判据必须报错——**证明新断言不是"改成永远成立"**。
|
|
25
|
+
- 为什么不把打桩那套塞进主测试:`globalThis.Date` 一改就影响同进程所有模块,会让其它断言也跑在假时钟下。所以做成独立脚本单独进程跑,只在取证时手动执行。
|
|
26
|
+
- 验证(2026-11-12 本机实跑):
|
|
27
|
+
- 全量单测 `node --test "test/*.test.mjs"`:**158 项全通过 / 0 失败**(与 1.5.33 基线同为 158,本版不新增 test 通配条目)。
|
|
28
|
+
- `node test/presence-midnight-proof.mjs` → `PROVEN`,exit 0。
|
|
29
|
+
- 新文件均无 BOM。
|
|
30
|
+
- 版本号三处对齐:`package.json` version、本条目、profile `cordis.patch.yml` 里 `aiyayi-feishu` 的 `config.reloadTag` 均为 `1.5.34`(profile 已先备份为 `cordis.patch.yml.bak-1.5.33-20261112-002902`)。
|
|
31
|
+
- **运行时零改动**:`lib/` 下所有文件与 1.5.33 逐字节相同(`git diff --stat` 只有 `test/` 两个文件 + 版本号文件)。装 1.5.34 与装 1.5.33 行为完全一致,本版只为让单测在跨午夜时不再误报。
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## 1.5.33(2026-11-11 行为变更:「对接默认落到当前会话」从准则落成代码——未绑定 chat 三档复用)
|
|
36
|
+
|
|
37
|
+
- 环境:DSH-执行智能体(DeepSeek Harness 子会话,模型名未确证)
|
|
38
|
+
- 需求(李浩 2026-11-11 原话):「对接机器人都默认当前对话的会话吧,别新建会话了,除非用户主动要求」;澄清后语义 = **绑定的对象是「发起对接的那个会话」**,不是「新建一个会话再绑过去」。后续追加:「这个默认机器人会话对接规则需要固化到 aiyayi 安装包里」。
|
|
39
|
+
- 1.5.32 只把这条写进 persona 准则与文档(零代码),**本版落成实际代码行为**。
|
|
40
|
+
- **改动落点**:`lib/feishu.js` 的 `onIncomingMessage()` 里 `action === 'new'` 分支原先「无条件新建会话并接入」,现改为三档决策;决策逻辑抽在纯函数 `lib/feishu-logic.js` 的 `decideUnboundChatRouting()`,候选采集在 `collectActiveSessionCandidates()`。
|
|
41
|
+
- **三档(零歧义才自动,绝不瞎猜)**:
|
|
42
|
+
1. **恰好 1 个活跃真人会话** → `mappings[chatId] = 那个会话`,回「已接到你当前的会话:X。之后这个飞书对话的消息都会投到这里。」,**不新建**;
|
|
43
|
+
2. **多个活跃** → **不绑不建**,回编号列表让用户选,格式 `1. 会话名(工作区:xxx)`,并明说「我不替你猜该投哪个」;用户回 `2` / `选2` / `第2个` / `绑定会话 2` 即接入。**刻意不挑"最近那个"**——会话各带自己的上下文,投错比多问一句糟糕得多(这是需求方特别强调的点);
|
|
44
|
+
3. **0 个活跃** → 才新建,并照实回「没有可复用的活跃会话,已给你新建并接入 X」,**不谎称接在原会话上**。
|
|
45
|
+
- **候选排除规则**(每条对应一个真实串味风险,纯函数 `normalizeSessionCandidates()` 统一负责):子智能体(`header.origin === 'subagent'` 或带 `header.parentSession`)、微信通道会话(`weixin-` 前缀,1.5.28 串味教训)、飞书自建会话(`feishu-` 前缀,那是给别的 chat 的)、**已被别的 chat 绑定的会话**(否则两个 chat 抢同一会话),以及重复 id。
|
|
46
|
+
- **开关**:`feishu.reuseActiveSession` 默认 `true`;**只有布尔 `false` 才回退** 1.5.32 旧行为(一律新建)——字符串 `"false"` / `0` / `null` 都当开启,防 YAML 误配把默认规则悄悄关掉。
|
|
47
|
+
- **显式要求优先**:用户说「新建会话 X」(`routeMessage` 返回 `name` 非空)时直接新建、跳过三档复用——那是主动要求。
|
|
48
|
+
- **待选状态**:多活跃时的候选列表落盘 `feishu/pending-choice.json`,TTL **10 分钟**;过期或越界都会明确回一句,不静默吞。只认纯序号——`绑定会话 sess-abc` 这种真 id 交给 `routeMessage` 的 bind 分支,不被抢。
|
|
49
|
+
- **按 chatId 分键存**(`{ chatId: { candidates, createdAt } }`),不是全局单条——这是写完后审 diff 时实测出来的真缺陷:初版只有一份全局待选,宿主同时挂两个飞书 chat 时,**chat B 的「2」会把 chat A 的候选消费掉,并把 B 接到 A 选的会话上**。现在读写清全部带 chatId,两个 chat 的待选并存、各选各的,有专门用例覆盖(`c7`)。
|
|
50
|
+
- **已绑定 chat 零影响(硬底线)**:`routeMessage()` 里 `mappings[chatId]` 存在即 `action:'route'`,**先于一切判断**,三档决策只在 `action==='new' && name==null` 时才进入。线上 `oc_f327…` 那条绑定不受任何影响,有专门的回归断言(见下)。
|
|
51
|
+
- **数据来源(实测 asar 核对,非推测)**:
|
|
52
|
+
- 活跃会话:`ctx.get('sessions').list()`(header 带 `origin`/`parentSession`/`cwd`);
|
|
53
|
+
- 会话标题:`ctx.get('sessionTitle').get(session)` → 折叠快照的 `?.title` 才是字符串;
|
|
54
|
+
- 工作区:`ctx.get('workspaceRegistry').list()` → `{id, title, path, sessionIds}`,一次拿全工作区名与路径(比从 agent 上摸 cwd 靠谱,asar:9045243 的 `workspaces()` 同源);
|
|
55
|
+
- 取不到标题 → `未命名会话(<ID 前 8 位>)`;取不到工作区 → 列表行不拼括号(不留空的「(工作区:)」)。
|
|
56
|
+
- `inject` 补 `'sessions'`(其余两个服务早已在 inject 里)。
|
|
57
|
+
- **验证(2026-11-11 本机实跑)**:
|
|
58
|
+
- 全量单测 `node --test "test/*.test.mjs"`:**158 项全通过 / 0 失败**(1.5.32 基线 157,新增 `test/feishu-reuse-session.test.mjs` 一组)。
|
|
59
|
+
- 新测试覆盖父级点名的五种情况:**(1) 1 个会话 → reuse**、**(2) 多个 → choose**(含列表渲染格式、超过 8 个截断并说明余数、文案含"不替你猜")、**(3) 0 个 → new**(文案照实说新建、不许出现"接到你当前"字样)、**(4) 已绑定 chat 行为完全不变**(`oc_f327…` 回归:普通消息 / 有其它活跃会话 / 说「接入飞书」三种情形一律 route 原会话,且候选 0/1/3 个时都不改投)、**(5) `reuseActiveSession=false` 回退**(含"只有布尔 false 才算关"的一串取值断言)。
|
|
60
|
+
- 另覆盖:候选排除五类 + 去重、未命名占位带 ID 前 8 位、`workspaceTitle` 缺失回落 `cwd`、选号回复六种写法、真 id 不被抢、越界/过期/无待选/坏 JSON 四种边界、`pending-choice.json` 落盘读回与清空、以及 10 条静态接线核对(含"选号消费必须排在 routeMessage 之前"、"显式新建必须在复用决策之前分流")。
|
|
61
|
+
- `lib/feishu.js` 单独做语法解析(把两个宿主包 import 换成本地模块后 `node --check`)通过——它依赖 `@larksuiteoapi/node-sdk` 与 `@deepseek-ai/dsh-llm`,不便直接 import 做端到端,理由同 1.5.32 条目(坑 #76 同片雷区)。
|
|
62
|
+
- persona 体检测试从 27 条扩到 32 条,新增断言:1 个直接接过去 / 多个给列表 / 不许替用户挑最近那个 / 0 个才新建 / `feishu.reuseActiveSession` 开关名在位。
|
|
63
|
+
- 版本号三处对齐:`package.json` version、本条目、profile `cordis.patch.yml` 里 `aiyayi-feishu` 的 `config.reloadTag` 均为 `1.5.33`(reloadTag 语义同上;仓里那份 patch 没有 reloadTag,别改错)。
|
|
64
|
+
- **未包含(留给以后,优先级低)**:微信侧「`allowlist` 显式放行但还没绑定」的首条消息仍会新建会话——空白名单下微信只放行已绑定用户,能走到这条分支的是极边缘情况,为避免把本版拖大,本版不动它。
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
13
68
|
## 1.5.32(2026-11-11 行为变更:首次提示同时告知微信 ClawBot 能力;「对接默认落到发起对接的会话」固化为准则)
|
|
14
69
|
|
|
15
70
|
- 环境:DSH-执行智能体(DeepSeek Harness 子会话,模型名未确证)
|
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
| 新版本提醒 | 自写插件 `aiyayi-updates`(本包 `lib/updates.js`) | 每个会话第一个回合查一次 npm(registry.npmjs.org,失败落 npmmirror;成功结果缓存 6h、失败 30min 后重试):本地版本比 latest 旧时,往消息尾部追加 `[更新提示]` 快照,她会用自己的口吻顺口提一句。同一远端版本只提醒一次(updates.json 落盘去重,跨重启不重复念叨);检查失败/离线一律静默。**判据:`~\\.dsh\\aiyayi\\updates.json` 不存在 = 从没发现过新版(本地已是最新);她答不上"自己是什么版本"是设计边界(升级判断靠被动推送),已由 presence 的版本自述补上** |
|
|
23
23
|
| 飞书桥接 | 自写插件 `aiyayi-feishu`(本包 `lib/feishu.js`,1.5.8 起)+ 纯逻辑 `lib/feishu-logic.js` | 长连接 WebSocket 接入飞书机器人(SDK `@larksuiteoapi/node-sdk`,无需公网)。**2026-11-10 扫码改造**:新会话说「接入飞书」→ 引导出待接入目标 → 手机飞书扫码进 bot 单聊 → `bot_p2p_chat_entered_v1` 事件自动绑定(30 分钟时效);凭证 `feishu/config.json` 存记忆根(**不入 npm 包**)。**发附件需权限 `im:resource`**(后台真实标识「获取与上传图片或文件资源」,免审)——开发者后台搜 **`im:resource:upload` 为 0 条,后台不存在这个独立 scope**,真名就是 `im:resource`(详见坑 #70 / #71) |
|
|
24
24
|
| 微信桥接 | 自写插件 `aiyayi-weixin`(本包 `lib/weixin.js` + `lib/weixin/`,1.5.27 起) | 微信 **ClawBot 机器人**通道(腾讯官方 iLink 协议,长轮询,无需公网)。**默认关闭**:profile patch 里 `weixin.enabled` 必须显式 `true` 才加载,`weixin.accounts` 写 `[{accountId, credentialRef}]`,凭证先 `weixin_login` 扫码拿(二维码 121 秒寿命)。工具 `weixin_status` / `weixin_login` / `weixin_bind` / `weixin_unbind`。`allowlist` 留空 = **只放行已显式绑定的用户**(与官方相反,陌生消息丢弃)。**没有 weixin-setup 技能** |
|
|
25
|
-
| **对接落到哪个会话(默认规则,1.5.32
|
|
25
|
+
| **对接落到哪个会话(默认规则,1.5.32 固化 + 1.5.33 三档落地)** | `lib/feishu.js` 的 `pending-bind` + `lib/feishu-logic.js` 的 `decideUnboundChatRouting()` + `lib/weixin/accounts.js` 的 pending-bind | **对机器人默认落到「发起对接的那个会话」,能复用就复用、不新建**:会话 A 里说「接入飞书/微信」→ 待接入目标记的就是 A → 扫码/绑定成功后该 chat 绑回 A,聊天接着聊。只有用户明确说「新建会话」才新建;chat 从没走过对接、也没有待接入目标时程序会新建一个会话接住(**兜底,非首选**)。**1.5.33 起**一个从没绑过的 chat 发来第一条消息时按三档自动处理:① 恰好 1 个活跃真人会话 → 直接接过去并告知;② 多个活跃 → 不绑不建,回编号列表(`1. 会话名(工作区:xxx)`)让用户选,**不替他挑"最近那个"**(会话各带上下文,投错比多问一句糟糕);③ 0 个 → 才新建并明说。开关 `feishu.reuseActiveSession` 默认 `true`,设 `false` 回退 1.5.32 旧行为(一律新建)。这条同时写进 persona【飞书】【微信】两段,属行为约定而非可选配置 |
|
|
26
26
|
| 其他 | time-context、compaction 组(提前压缩,见坑 #14/#16)、tool-ask-user | 她没有 bash/文件/技能/搜索/网络工具,想动手只能派活;纯查资料走 `retriever` 检索员,不惊动小伙伴 |
|
|
27
27
|
|
|
28
28
|
## 安装与启用
|
|
@@ -73,7 +73,7 @@ npm 包为平铺结构(仓库若保留 bundle/ 子目录,发布时把其内
|
|
|
73
73
|
- `lib/doc.js` — aiyayi-doc 文档解析插件(1.4.0,挂检索员 preset;ZIP/XML 最小解析零依赖 + 动态加载 shipped libreoffice-kit 做 pdf 页图与旧版转换)
|
|
74
74
|
- `lib/own-tools.js` — 她的工具名单单一真源(注册处登记、放行处判定,防手写白名单漂移)
|
|
75
75
|
- `lib/feishu.js` — aiyayi-feishu 飞书桥接宿主插件(1.5.8 起;跨会话服务,长连接收消息、按指令路由到 aiyayi 会话、回发;凭读取记忆根 `feishu/config.json`)
|
|
76
|
-
- `lib/feishu-logic.js` —
|
|
76
|
+
- `lib/feishu-logic.js` — 飞书纯逻辑(路由/映射/文本抽取/三档复用决策,无外部依赖,可单测;test/feishu-route.test.mjs、test/feishu-reuse-session.test.mjs 覆盖)
|
|
77
77
|
- `skills/feishu-setup/` — 内置技能(1.5.13 起随包自动同步到 DSH 技能根):飞书机器人安装/验证/排障全流程(含 2026-11-10 扫码接入改造说明;脚本 `scripts/create-bot.js` v5 等由技能同步维护)
|
|
78
78
|
- `README.md` — 本文档
|
|
79
79
|
- `icon.svg` — 头像
|
package/cordis.patch.yml
CHANGED
|
@@ -133,6 +133,15 @@
|
|
|
133
133
|
才新建。若这个 chat 从来没走过对接、也没有待接入目标,程序会新建一个会话
|
|
134
134
|
接住(**这是兜底、不是首选**);这种情况照实说"给你新建了会话 X",
|
|
135
135
|
别谎称接在了原会话上。
|
|
136
|
+
**一个从没绑过的 chat 发来第一条消息时,飞书插件按三档自动处理**(1.5.33 起):
|
|
137
|
+
① 恰好 1 个活跃真人会话 → 直接接过去,回一句"已接到你当前的会话:X";
|
|
138
|
+
② 多个活跃 → **不绑不建**,回一条编号列表(`1. 会话名(工作区:xxx)`)让他选,
|
|
139
|
+
他回「2」或「绑定会话 2」就接过去——**别替他挑"最近那个"**,会话之间带着
|
|
140
|
+
各自的上下文,投错比多问一句糟糕得多;
|
|
141
|
+
③ 0 个活跃 → 才新建,并明说"给你新建了会话 X"。
|
|
142
|
+
这三档是插件自动做的,你别抢着替他绑;他要是问"为什么给我列表/为什么新建了",
|
|
143
|
+
照这三档解释。配置项 `feishu.reuseActiveSession` 默认 `true`,
|
|
144
|
+
设 `false` 回到旧行为(一律新建)。
|
|
136
145
|
|
|
137
146
|
【微信】
|
|
138
147
|
除飞书外,还有一条**微信 ClawBot 机器人**通道(aiyayi-weixin 插件,腾讯官方
|
package/lib/feishu-logic.js
CHANGED
|
@@ -243,7 +243,10 @@ export function extractText(message) {
|
|
|
243
243
|
* → { action: 'new', name }
|
|
244
244
|
* 其余(聊天正文):
|
|
245
245
|
* chat 已有映射 → { action: 'route' }
|
|
246
|
-
* chat 无映射 → { action: 'new', name: null }
|
|
246
|
+
* chat 无映射 → { action: 'new', name: null }
|
|
247
|
+
* (1.5.33 起**不再无条件新建**:调用方拿到 action:'new' 且 name 为 null 时,
|
|
248
|
+
* 先走 decideUnboundChatRouting() 三档复用;只有 0 个可复用会话才真新建。
|
|
249
|
+
* 用户明确说「新建会话 X」时 name 非空,调用方直接新建、跳过复用。)
|
|
247
250
|
* @param {string} text - 用户消息纯文本。
|
|
248
251
|
* @param {object} mappings - 会话映射表。
|
|
249
252
|
* @param {string} chatId - 当前 chat。
|
|
@@ -413,6 +416,249 @@ export function matchSkipIntent(text) {
|
|
|
413
416
|
return /^(?:不用|不需要|不接|算了|跳过|暂时不用|先不用)/i.test((text || '').trim())
|
|
414
417
|
}
|
|
415
418
|
|
|
419
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
420
|
+
// 1.5.33「对接默认落到当前会话」——未绑定 chat 的三档复用决策
|
|
421
|
+
//
|
|
422
|
+
// 背景:1.5.32 及以前,未绑定 chat 发一条普通消息 → routeMessage 返回
|
|
423
|
+
// action:'new' → 无条件新建会话。用户明确要求改成「默认复用,别新建」:
|
|
424
|
+
// 恰好 1 个活跃真人会话 → 直接绑过去(不新建)
|
|
425
|
+
// 多个活跃 → 不绑不建,回编号列表让用户选(**不猜"最近那个"**:
|
|
426
|
+
// 会话之间带上下文,投错比多问一句糟糕得多)
|
|
427
|
+
// 0 个 → 才新建(唯一可行兜底),并明说"给你新建了"
|
|
428
|
+
// 已绑定 chat 的路径完全不经过这里——那条是 route 分支,不受任何影响。
|
|
429
|
+
// 开关 feishu.reuseActiveSession 默认 true;设 false 回退 1.5.32 旧行为。
|
|
430
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
431
|
+
|
|
432
|
+
/** 会话名缺失时的占位文案。 */
|
|
433
|
+
export const UNNAMED_SESSION_LABEL = '未命名会话'
|
|
434
|
+
|
|
435
|
+
/** 列表里最多展示多少个候选会话(再多就翻不动手机屏幕了)。 */
|
|
436
|
+
export const MAX_SESSION_CANDIDATES = 8
|
|
437
|
+
|
|
438
|
+
/** 会话 ID 短形态(前 8 位),用于列表里辨认同名会话。 */
|
|
439
|
+
export function shortSessionId(sessionId) {
|
|
440
|
+
const id = String(sessionId || '')
|
|
441
|
+
return id.length > 8 ? id.slice(0, 8) : id
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* 归一化「活跃真人会话」候选列表——把宿主侧的原始数据压成纯数据结构,
|
|
446
|
+
* 让决策函数可单测(不依赖 ctx / agents / workspaceRegistry)。
|
|
447
|
+
*
|
|
448
|
+
* 排除规则(每一条都有血债,别随手放宽):
|
|
449
|
+
* - 子智能体(header.origin==='subagent' 或带 header.parentSession):不是对话对象;
|
|
450
|
+
* - 微信通道会话(`weixin-` 前缀):它已自带手机可达性,再绑飞书 chat 是串味;
|
|
451
|
+
* - 飞书自己建的会话(`feishu-` 前缀):那是给别的 chat 用的,拿来复用=两个 chat
|
|
452
|
+
* 抢同一个会话,消息互相串;
|
|
453
|
+
* - 已经绑到**别的** chat 的会话:同上;
|
|
454
|
+
* - 空白/未命名且没有活动痕迹的:无从判断,不猜(由调用方决定是否纳入,默认纳入,
|
|
455
|
+
* 因为"未命名会话"也可能是刚开就说话的那个)。
|
|
456
|
+
*
|
|
457
|
+
* @param {object} input
|
|
458
|
+
* @param {Array<{sessionId: string, title?: string|null, cwd?: string|null,
|
|
459
|
+
* workspaceTitle?: string|null, origin?: string|null,
|
|
460
|
+
* parentSession?: string|null, isSubagent?: boolean}>} input.sessions
|
|
461
|
+
* @param {Record<string,string>} [input.mappings] chat_id → sessionId(已绑定表)
|
|
462
|
+
* @param {string} [input.chatId] 当前 chat(自己的绑定不算"被别人占")
|
|
463
|
+
* @returns {Array<{sessionId: string, title: string, workspace: string|null, cwd: string|null,
|
|
464
|
+
* label: string}>}
|
|
465
|
+
*/
|
|
466
|
+
export function normalizeSessionCandidates(input = {}) {
|
|
467
|
+
const sessions = Array.isArray(input.sessions) ? input.sessions : []
|
|
468
|
+
const mappings = input.mappings && typeof input.mappings === 'object' ? input.mappings : {}
|
|
469
|
+
const selfChatId = String(input.chatId || '')
|
|
470
|
+
// 被别人占住的会话 id 集合(排除当前 chat 自己的绑定)。
|
|
471
|
+
const takenByOthers = new Set()
|
|
472
|
+
for (const [chatId, sid] of Object.entries(mappings)) {
|
|
473
|
+
if (chatId === selfChatId) continue
|
|
474
|
+
if (typeof sid === 'string' && sid) takenByOthers.add(sid)
|
|
475
|
+
}
|
|
476
|
+
const out = []
|
|
477
|
+
const seen = new Set()
|
|
478
|
+
for (const item of sessions) {
|
|
479
|
+
const sessionId = String(item?.sessionId || item?.id || '')
|
|
480
|
+
if (!sessionId || seen.has(sessionId)) continue
|
|
481
|
+
if (item?.isSubagent) continue
|
|
482
|
+
const origin = item?.origin ?? null
|
|
483
|
+
const parentSession = item?.parentSession ?? null
|
|
484
|
+
if (origin === 'subagent' || typeof parentSession === 'string') continue
|
|
485
|
+
if (/^weixin-/.test(sessionId)) continue
|
|
486
|
+
if (/^feishu-/.test(sessionId)) continue
|
|
487
|
+
if (takenByOthers.has(sessionId)) continue
|
|
488
|
+
seen.add(sessionId)
|
|
489
|
+
const rawTitle = typeof item?.title === 'string' ? item.title.trim() : ''
|
|
490
|
+
// 会话名缺失/为空 → 占位 + ID 前 8 位(别在列表里留空,用户没法选)。
|
|
491
|
+
const title = rawTitle || `${UNNAMED_SESSION_LABEL}(${shortSessionId(sessionId)})`
|
|
492
|
+
const workspace =
|
|
493
|
+
(typeof item?.workspaceTitle === 'string' && item.workspaceTitle.trim()) ||
|
|
494
|
+
(typeof item?.cwd === 'string' && item.cwd.trim()) ||
|
|
495
|
+
null
|
|
496
|
+
out.push({
|
|
497
|
+
sessionId,
|
|
498
|
+
title,
|
|
499
|
+
workspace,
|
|
500
|
+
cwd: typeof item?.cwd === 'string' ? item.cwd : null,
|
|
501
|
+
// 列表行:`1. 会话名(工作区:xxx)` 的正文部分(不含序号)。
|
|
502
|
+
label: workspace ? `${title}(工作区:${workspace})` : title,
|
|
503
|
+
})
|
|
504
|
+
}
|
|
505
|
+
return out
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* 三档决策(纯函数):一个未绑定 chat 的普通消息该投到哪。
|
|
510
|
+
*
|
|
511
|
+
* @param {object} input
|
|
512
|
+
* @param {boolean} [input.reuseActiveSession] 开关(feishu.reuseActiveSession,缺省 true)。
|
|
513
|
+
* @param {Array} [input.candidates] normalizeSessionCandidates() 的输出。
|
|
514
|
+
* @returns {{action: 'reuse'|'choose'|'new', sessionId?: string, candidates?: Array,
|
|
515
|
+
* reason: string}}
|
|
516
|
+
* reuse — 恰好 1 个候选:直接绑过去(sessionId 就是它);
|
|
517
|
+
* choose — 多个候选:不绑不建,回编号列表让用户选;
|
|
518
|
+
* new — 0 个候选(或开关关闭):才新建会话。
|
|
519
|
+
*/
|
|
520
|
+
export function decideUnboundChatRouting(input = {}) {
|
|
521
|
+
// 开关:只有显式 false 才回退旧行为(字符串 "false"/0 都当 true——防误配把
|
|
522
|
+
// 默认规则悄悄关掉;要关就写 YAML 布尔 false)。
|
|
523
|
+
const reuse = input.reuseActiveSession !== false
|
|
524
|
+
const candidates = Array.isArray(input.candidates) ? input.candidates : []
|
|
525
|
+
if (!reuse) return { action: 'new', reason: 'reuse-disabled' }
|
|
526
|
+
if (candidates.length === 0) return { action: 'new', reason: 'no-active-session' }
|
|
527
|
+
if (candidates.length === 1) {
|
|
528
|
+
return { action: 'reuse', sessionId: candidates[0].sessionId, candidates, reason: 'single-active-session' }
|
|
529
|
+
}
|
|
530
|
+
// 多个:截断展示,但候选全集保证至少列出 MAX 个 + 一句"还有 N 个"。
|
|
531
|
+
const shown = candidates.slice(0, MAX_SESSION_CANDIDATES)
|
|
532
|
+
return { action: 'choose', candidates: shown, total: candidates.length, reason: 'multiple-active-sessions' }
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/** 渲染编号列表正文(`1. 会话名(工作区:xxx)`,一行一个)。 */
|
|
536
|
+
export function renderSessionCandidateList(candidates, total = null) {
|
|
537
|
+
const list = Array.isArray(candidates) ? candidates : []
|
|
538
|
+
const lines = list.map((item, index) => `${index + 1}. ${item.label ?? item.title ?? item.sessionId}`)
|
|
539
|
+
const count = typeof total === 'number' ? total : list.length
|
|
540
|
+
if (count > list.length) {
|
|
541
|
+
lines.push(`……另有 ${count - list.length} 个未列出。`)
|
|
542
|
+
}
|
|
543
|
+
return lines.join('\n')
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/** 「已接到当前会话」告知文案。 */
|
|
547
|
+
export function buildReusedSessionNotice(candidate) {
|
|
548
|
+
const name = candidate?.label || candidate?.title || candidate?.sessionId || ''
|
|
549
|
+
return `已接到你当前的会话:${name}。之后这个飞书对话的消息都会投到这里。`
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/** 「多个活跃会话,请选一个」文案(引导用户用已有的 `绑定会话 <id>` 指令)。 */
|
|
553
|
+
export function buildChooseSessionPrompt(candidates, total = null) {
|
|
554
|
+
const count = typeof total === 'number' ? total : (Array.isArray(candidates) ? candidates.length : 0)
|
|
555
|
+
const head = `现在有 ${count} 个活跃会话,我不替你猜该投哪个。回一句「绑定会话 <编号>」我就接过去(也可以说「新建会话 <名字>」另开一个):`
|
|
556
|
+
return `${head}\n${renderSessionCandidateList(candidates, count)}`
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** 「给你新建了会话」告知文案(兜底路径必须照实说,别谎称接在原会话上)。 */
|
|
560
|
+
export function buildNewSessionNotice(sessionId, title = null) {
|
|
561
|
+
const name = title ? `${title}(${sessionId})` : sessionId
|
|
562
|
+
return `没有可复用的活跃会话,已给你新建并接入 ${name}。`
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// ─── 多活跃时「等用户选一个」的待选状态 ────────────────────────────────────
|
|
566
|
+
// 多个候选时不能替用户猜,于是把候选列表落盘,等他回「绑定会话 2」或直接回「2」。
|
|
567
|
+
// 有时效(跟 pending-bind 同量级),过期就不再消费——否则一周后一句「2」
|
|
568
|
+
// 会被当成选会话,把闲聊投到不相干的会话里。
|
|
569
|
+
//
|
|
570
|
+
// **按 chatId 分键**(不是全局单条):宿主可能同时挂着多个飞书 chat,
|
|
571
|
+
// 若只存"最新一份",chat B 的「2」会把 chat A 的候选消费掉、把 B 接到 A 选的会话上。
|
|
572
|
+
|
|
573
|
+
/** 待选目标文件名。 */
|
|
574
|
+
const PENDING_CHOICE_FILE = 'pending-choice.json'
|
|
575
|
+
/** 待选有效期:10 分钟(够他看完列表回一句,又不至于久到误伤后面的数字消息)。 */
|
|
576
|
+
export const PENDING_CHOICE_TTL_MS = 10 * 60 * 1000
|
|
577
|
+
|
|
578
|
+
/** 读整张表 { chatId: { candidates, createdAt } };不存在或损坏返回 {}。 */
|
|
579
|
+
function readPendingChoiceTable(root) {
|
|
580
|
+
const file = join(feishuDir(root), PENDING_CHOICE_FILE)
|
|
581
|
+
if (!existsSync(file)) return {}
|
|
582
|
+
try {
|
|
583
|
+
const parsed = JSON.parse(stripBom(readFileSync(file, 'utf8')))
|
|
584
|
+
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : {}
|
|
585
|
+
} catch {
|
|
586
|
+
return {}
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* 读某个 chat 的待选目标;没有返回 null。
|
|
592
|
+
* @param {string} root 记忆根
|
|
593
|
+
* @param {string} chatId 飞书 chat_id(必须匹配,否则视为无待选)
|
|
594
|
+
*/
|
|
595
|
+
export function readPendingChoice(root, chatId) {
|
|
596
|
+
if (!chatId) return null
|
|
597
|
+
const entry = readPendingChoiceTable(root)[String(chatId)]
|
|
598
|
+
if (!entry || typeof entry !== 'object' || !Array.isArray(entry.candidates)) return null
|
|
599
|
+
return entry
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/** 写某个 chat 的待选目标(保留其它 chat 的待选)。 */
|
|
603
|
+
export function writePendingChoice(root, chatId, pending) {
|
|
604
|
+
const id = String(chatId || '')
|
|
605
|
+
if (!id) throw new Error('writePendingChoice: chatId 必填')
|
|
606
|
+
const table = readPendingChoiceTable(root)
|
|
607
|
+
table[id] = { ...pending, chatId: id }
|
|
608
|
+
const dir = ensureFeishuDir(root)
|
|
609
|
+
writeFileSync(join(dir, PENDING_CHOICE_FILE), JSON.stringify(table, null, 2), 'utf8')
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/** 只清某个 chat 的待选目标(不碰别的 chat)。 */
|
|
613
|
+
export function clearPendingChoice(root, chatId) {
|
|
614
|
+
const id = String(chatId || '')
|
|
615
|
+
if (!id) return
|
|
616
|
+
const table = readPendingChoiceTable(root)
|
|
617
|
+
if (!(id in table)) return
|
|
618
|
+
delete table[id]
|
|
619
|
+
const dir = ensureFeishuDir(root)
|
|
620
|
+
writeFileSync(join(dir, PENDING_CHOICE_FILE), JSON.stringify(table, null, 2), 'utf8')
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* 解析「选第几个」的回复。
|
|
625
|
+
* 认:`2`、`选2`、`第2个`、`绑定会话 2`、`接入 2`(**只认纯序号**;
|
|
626
|
+
* `绑定会话 sess-abc` 这种真 id 交给 routeMessage 的 bind 分支,这里不抢)。
|
|
627
|
+
* @param {string} text 用户消息正文。
|
|
628
|
+
* @param {object|null} pending readPendingChoice(root, chatId) 的结果(已按 chat 过滤)。
|
|
629
|
+
* @param {{now?: number, ttlMs?: number}} [opts]
|
|
630
|
+
* @returns {{kind: 'select', sessionId: string, label: string, index: number}
|
|
631
|
+
* | {kind: 'expired', count: number}
|
|
632
|
+
* | {kind: 'out-of-range', count: number}
|
|
633
|
+
* | null} null = 不是选会话的回复(照常走路由)。
|
|
634
|
+
*/
|
|
635
|
+
export function parseChoiceReply(text, pending, opts = {}) {
|
|
636
|
+
if (!pending || !Array.isArray(pending.candidates) || pending.candidates.length === 0) return null
|
|
637
|
+
const t = String(text || '').trim()
|
|
638
|
+
if (!t) return null
|
|
639
|
+
// 纯序号 / 选N / 第N个 / 绑定会话 N / 接入 N
|
|
640
|
+
const m = /^(?:选|第)?\s*(\d{1,2})\s*(?:个|号)?$/.exec(t) ||
|
|
641
|
+
/^(?:绑定会话|绑定|接入会话|接入|切换到会话|切换)\s*(\d{1,2})$/.exec(t)
|
|
642
|
+
if (!m) return null
|
|
643
|
+
const index = Number(m[1])
|
|
644
|
+
const now = typeof opts.now === 'number' ? opts.now : Date.now()
|
|
645
|
+
const ttl = typeof opts.ttlMs === 'number' ? opts.ttlMs : PENDING_CHOICE_TTL_MS
|
|
646
|
+
const createdAt = Number(pending.createdAt ?? 0)
|
|
647
|
+
if (!createdAt || now - createdAt > ttl) {
|
|
648
|
+
return { kind: 'expired', count: pending.candidates.length }
|
|
649
|
+
}
|
|
650
|
+
if (!Number.isInteger(index) || index < 1 || index > pending.candidates.length) {
|
|
651
|
+
return { kind: 'out-of-range', count: pending.candidates.length }
|
|
652
|
+
}
|
|
653
|
+
const chosen = pending.candidates[index - 1]
|
|
654
|
+
return {
|
|
655
|
+
kind: 'select',
|
|
656
|
+
sessionId: String(chosen.sessionId || ''),
|
|
657
|
+
label: chosen.label || chosen.title || String(chosen.sessionId || ''),
|
|
658
|
+
index,
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
416
662
|
/** 去掉 UTF-8 BOM(\uFEFF 或 EF BB BF)。 */
|
|
417
663
|
function stripBom(text) {
|
|
418
664
|
if (text.charCodeAt(0) === 0xfeff) return text.slice(1)
|
package/lib/feishu.js
CHANGED
|
@@ -35,15 +35,20 @@ import { registerOwnTool } from './own-tools.js'
|
|
|
35
35
|
import {
|
|
36
36
|
applyCardAction,
|
|
37
37
|
buildNewSessionGuideText,
|
|
38
|
+
buildNewSessionNotice,
|
|
38
39
|
buildQuestionCard,
|
|
39
40
|
buildQuestionText,
|
|
41
|
+
buildChooseSessionPrompt,
|
|
42
|
+
buildReusedSessionNotice,
|
|
40
43
|
buildWeixinOnboardText,
|
|
41
44
|
clearPendingBind,
|
|
42
45
|
collectIncomingAttachments,
|
|
43
46
|
composeAnswerBatch,
|
|
44
47
|
composeDeliveryText,
|
|
45
48
|
createTokenManager,
|
|
49
|
+
clearPendingChoice,
|
|
46
50
|
decideNewSessionGuide,
|
|
51
|
+
decideUnboundChatRouting,
|
|
47
52
|
ensureFeishuDir,
|
|
48
53
|
extractFeishuErrorCode,
|
|
49
54
|
extractText,
|
|
@@ -53,6 +58,10 @@ import {
|
|
|
53
58
|
matchOnboardIntent,
|
|
54
59
|
matchSkipIntent,
|
|
55
60
|
matchWeixinIntent,
|
|
61
|
+
normalizeSessionCandidates,
|
|
62
|
+
parseChoiceReply,
|
|
63
|
+
readPendingChoice,
|
|
64
|
+
writePendingChoice,
|
|
56
65
|
mergeConfig,
|
|
57
66
|
migrateConfigFile,
|
|
58
67
|
normalizeApps,
|
|
@@ -85,6 +94,8 @@ export const inject = [
|
|
|
85
94
|
'sessionTitle',
|
|
86
95
|
'workspaceRegistry',
|
|
87
96
|
'tools',
|
|
97
|
+
// 1.5.33:枚举活跃会话(ctx.get('sessions').list())——未绑定 chat 的三档复用决策要用。
|
|
98
|
+
'sessions',
|
|
88
99
|
// 坑 #67:建会话要照 webhook 模板取默认模型选择,缺了建出的会话可能没有模型。
|
|
89
100
|
'agentDefaultModel',
|
|
90
101
|
]
|
|
@@ -642,6 +653,43 @@ async function onIncomingMessage(
|
|
|
642
653
|
}
|
|
643
654
|
|
|
644
655
|
const mappings = readMappings(root) // 每次重读,避免多 chat 并发写丢
|
|
656
|
+
|
|
657
|
+
// 1.5.33:先消费「多活跃会话 → 等用户选一个」的待选回复。
|
|
658
|
+
// 必须排在 routeMessage 之前:否则用户回的「2」会被当普通正文走 new 分支。
|
|
659
|
+
// 只认纯序号(`2` / `选2` / `绑定会话 2`);`绑定会话 sess-abc` 这种真 id 不抢。
|
|
660
|
+
try {
|
|
661
|
+
const choice = parseChoiceReply(rawText, readPendingChoice(root, chatId))
|
|
662
|
+
if (choice?.kind === 'select') {
|
|
663
|
+
clearPendingChoice(root, chatId)
|
|
664
|
+
const agent = getAgent(ctx, choice.sessionId)
|
|
665
|
+
if (!agent) {
|
|
666
|
+
await replyToChat(
|
|
667
|
+
channel,
|
|
668
|
+
chatId,
|
|
669
|
+
messageId,
|
|
670
|
+
`会话 ${choice.label} 已经不在运行了。再说一句我把最新列表发给你。`,
|
|
671
|
+
)
|
|
672
|
+
return
|
|
673
|
+
}
|
|
674
|
+
const refreshed = readMappings(root)
|
|
675
|
+
refreshed[chatId] = choice.sessionId
|
|
676
|
+
writeMappings(root, refreshed)
|
|
677
|
+
await replyToChat(channel, chatId, messageId, `已把本对话接入:${choice.label}。`)
|
|
678
|
+
return
|
|
679
|
+
}
|
|
680
|
+
if (choice?.kind === 'expired') {
|
|
681
|
+
clearPendingChoice(root, chatId)
|
|
682
|
+
await replyToChat(channel, chatId, messageId, '那份会话列表已经过期了。再说一句我重新给你列一遍。')
|
|
683
|
+
return
|
|
684
|
+
}
|
|
685
|
+
if (choice?.kind === 'out-of-range') {
|
|
686
|
+
await replyToChat(channel, chatId, messageId, `没有第 ${String(rawText).trim()} 个,列表里一共 ${choice.count} 个。`)
|
|
687
|
+
return
|
|
688
|
+
}
|
|
689
|
+
} catch (error) {
|
|
690
|
+
ctx.logger.warn(`aiyayi-feishu: 处理会话选择失败(继续按普通消息路由): ${String(error)}`)
|
|
691
|
+
}
|
|
692
|
+
|
|
645
693
|
const decision = routeMessage(rawText, mappings, chatId)
|
|
646
694
|
|
|
647
695
|
// 1.5.18 附件收件:非 bind 分支收集下载附件,把结果并进投递文本。
|
|
@@ -699,12 +747,58 @@ async function onIncomingMessage(
|
|
|
699
747
|
return
|
|
700
748
|
}
|
|
701
749
|
|
|
702
|
-
// action === 'new'
|
|
703
|
-
|
|
750
|
+
// action === 'new':1.5.33 起先走三档复用决策,只有确实没有可复用会话才新建。
|
|
751
|
+
// decision.name 非空 = 用户明确说「新建会话 X」,那是显式要求,直接新建(跳过复用)。
|
|
752
|
+
if (decision.name) {
|
|
753
|
+
const sessionId = await createAiyayiSession(ctx, merged, decision.name, root)
|
|
754
|
+
const refreshed = readMappings(root)
|
|
755
|
+
refreshed[chatId] = sessionId
|
|
756
|
+
writeMappings(root, refreshed)
|
|
757
|
+
await replyToChat(channel, chatId, messageId, `已新建并接入 aiyayi 会话 ${sessionId}。`)
|
|
758
|
+
await deliverToSession(ctx, sessionId, messageId, deliveryText)
|
|
759
|
+
return
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
const candidates = collectActiveSessionCandidates(ctx, root, chatId)
|
|
763
|
+
const routing = decideUnboundChatRouting({
|
|
764
|
+
reuseActiveSession: merged.reuseActiveSession,
|
|
765
|
+
candidates,
|
|
766
|
+
})
|
|
767
|
+
ctx.logger.info(
|
|
768
|
+
`aiyayi-feishu: 未绑定 chat=${chatId} 走三档复用决策 action=${routing.action} reason=${routing.reason} 候选=${candidates.length}`,
|
|
769
|
+
)
|
|
770
|
+
|
|
771
|
+
if (routing.action === 'reuse') {
|
|
772
|
+
const chosen = routing.candidates[0]
|
|
773
|
+
const refreshed = readMappings(root)
|
|
774
|
+
refreshed[chatId] = routing.sessionId
|
|
775
|
+
writeMappings(root, refreshed)
|
|
776
|
+
await replyToChat(channel, chatId, messageId, buildReusedSessionNotice(chosen))
|
|
777
|
+
await deliverToSession(ctx, routing.sessionId, messageId, deliveryText)
|
|
778
|
+
return
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
if (routing.action === 'choose') {
|
|
782
|
+
// 多活跃:不绑不建,回编号列表让他选。待选状态**按 chatId 分键**落盘,等他回「2」。
|
|
783
|
+
writePendingChoice(root, chatId, {
|
|
784
|
+
candidates: routing.candidates,
|
|
785
|
+
createdAt: Date.now(),
|
|
786
|
+
})
|
|
787
|
+
await replyToChat(
|
|
788
|
+
channel,
|
|
789
|
+
chatId,
|
|
790
|
+
messageId,
|
|
791
|
+
buildChooseSessionPrompt(routing.candidates, routing.total),
|
|
792
|
+
)
|
|
793
|
+
return
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
// 0 个候选 / 开关关闭:唯一可行兜底——新建,并照实说"给你新建了"。
|
|
797
|
+
const sessionId = await createAiyayiSession(ctx, merged, null, root)
|
|
704
798
|
const refreshed = readMappings(root)
|
|
705
799
|
refreshed[chatId] = sessionId
|
|
706
800
|
writeMappings(root, refreshed)
|
|
707
|
-
await replyToChat(channel, chatId, messageId,
|
|
801
|
+
await replyToChat(channel, chatId, messageId, buildNewSessionNotice(sessionId))
|
|
708
802
|
await deliverToSession(ctx, sessionId, messageId, deliveryText)
|
|
709
803
|
}
|
|
710
804
|
|
|
@@ -719,6 +813,74 @@ function getAgent(ctx, sessionId) {
|
|
|
719
813
|
}
|
|
720
814
|
}
|
|
721
815
|
|
|
816
|
+
/**
|
|
817
|
+
* 采集「活跃真人会话」候选(1.5.33)。
|
|
818
|
+
*
|
|
819
|
+
* 来源三处互补,任一处不可用都只降级、不抛:
|
|
820
|
+
* · `ctx.get('sessions').list()` —— 宿主权威活跃会话表(带 header:origin/parentSession/cwd);
|
|
821
|
+
* · `ctx.sessionTitle.get(session)` —— 折叠后的标题快照(`?.title` 才是字符串);
|
|
822
|
+
* · `ctx.workspaceRegistry.list()` —— `{id, title, path, sessionIds}`,一次拿全
|
|
823
|
+
* 工作区名与路径(比从 agent 上摸 cwd 靠谱,实测 asar:9045243 `workspaces()` 同源)。
|
|
824
|
+
*
|
|
825
|
+
* 排除规则统一由纯函数 normalizeSessionCandidates() 负责(子智能体 / weixin- / feishu- /
|
|
826
|
+
* 已被别的 chat 占住),这里只做「取原始数据 + 尽力补标题和工作区」。
|
|
827
|
+
*
|
|
828
|
+
* @returns {Array} normalizeSessionCandidates() 的输出。
|
|
829
|
+
*/
|
|
830
|
+
function collectActiveSessionCandidates(ctx, root, chatId) {
|
|
831
|
+
const sessions = []
|
|
832
|
+
try {
|
|
833
|
+
const store = ctx.get('sessions')
|
|
834
|
+
const list = typeof store?.list === 'function' ? store.list() : []
|
|
835
|
+
for (const session of Array.isArray(list) ? list : []) {
|
|
836
|
+
const header = session?.header ?? {}
|
|
837
|
+
const sessionId = String(session?.id ?? header.id ?? '')
|
|
838
|
+
if (!sessionId) continue
|
|
839
|
+
let title = null
|
|
840
|
+
try {
|
|
841
|
+
// sessionTitle.get(session) → { title, messageSeqs, source, eventSeq, updatedAt } 或 undefined
|
|
842
|
+
const snapshot = ctx.get('sessionTitle')?.get?.(session)
|
|
843
|
+
if (snapshot && typeof snapshot.title === 'string') title = snapshot.title
|
|
844
|
+
} catch {
|
|
845
|
+
// 标题读不出来不影响决策(退"未命名会话 + ID 前 8 位")。
|
|
846
|
+
}
|
|
847
|
+
sessions.push({
|
|
848
|
+
sessionId,
|
|
849
|
+
title,
|
|
850
|
+
cwd: typeof header.cwd === 'string' ? header.cwd : null,
|
|
851
|
+
origin: header.origin ?? null,
|
|
852
|
+
parentSession: header.parentSession ?? null,
|
|
853
|
+
})
|
|
854
|
+
}
|
|
855
|
+
} catch (error) {
|
|
856
|
+
ctx.logger.warn(`aiyayi-feishu: 枚举活跃会话失败(按 0 个处理,将走新建兜底): ${String(error)}`)
|
|
857
|
+
return []
|
|
858
|
+
}
|
|
859
|
+
// 工作区名:sessionId → 工作区标题(workspaceRegistry.list() 每条带 sessionIds)。
|
|
860
|
+
const workspaceBySession = new Map()
|
|
861
|
+
try {
|
|
862
|
+
const registry = ctx.get('workspaceRegistry')
|
|
863
|
+
const workspaces = typeof registry?.list === 'function' ? registry.list() : []
|
|
864
|
+
for (const workspace of Array.isArray(workspaces) ? workspaces : []) {
|
|
865
|
+
const label =
|
|
866
|
+
(typeof workspace?.title === 'string' && workspace.title.trim()) ||
|
|
867
|
+
(typeof workspace?.path === 'string' && workspace.path.trim()) ||
|
|
868
|
+
null
|
|
869
|
+
if (!label) continue
|
|
870
|
+
for (const sid of Array.isArray(workspace?.sessionIds) ? workspace.sessionIds : []) {
|
|
871
|
+
workspaceBySession.set(String(sid), label)
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
} catch (error) {
|
|
875
|
+
ctx.logger.warn(`aiyayi-feishu: 读工作区列表失败(列表里将只显示会话名): ${String(error)}`)
|
|
876
|
+
}
|
|
877
|
+
for (const item of sessions) {
|
|
878
|
+
const label = workspaceBySession.get(item.sessionId)
|
|
879
|
+
if (label) item.workspaceTitle = label
|
|
880
|
+
}
|
|
881
|
+
return normalizeSessionCandidates({ sessions, mappings: readMappings(root), chatId })
|
|
882
|
+
}
|
|
883
|
+
|
|
722
884
|
/**
|
|
723
885
|
* 把文本投进目标会话(跟 dsh-webhook 同一通道)。
|
|
724
886
|
* @returns {Promise<boolean>} true=已投进在运行的会话。
|