dsh-im-lark 0.1.5 → 0.1.7

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.en.md CHANGED
@@ -22,12 +22,12 @@ The two modes are mutually exclusive: the core is used only when "Use my super a
22
22
  - **Quoted replies**: answers quote the message that triggered them, in DMs and groups alike
23
23
  - **Processing feedback**: an incoming message gets a `Get` reaction first; `DONE` is added to the original message only after the answer is actually delivered, and the two stay side by side. Being queued is not completion (the core queues the message and runs the task asynchronously). In core mode the plugin follows the core's `completes` markers (stall notices, dispatch receipts and transfer confirmations never carry them; cancelled, failed, approval-rejected or token-limit-truncated rounds do not carry them either, because they have no normal answer; older cores do not send them at all). Standalone mode aligns reactions per turn, and reminder answers are marked at settlement time (when only some questions are answered the reminder is still waiting, so no `DONE`). On failure only `Get` remains — never a false "done". Each message is marked once. Feishu has no official "typing" API
24
24
  - **Card updates**: streaming answers update the same card in place; after platform limits (14 days, 5 QPS per card) a new card is sent
25
- - **Interactive cards**: in core mode, **single-question** single- and multi-select questions are answered in Feishu and the result is returned to the core; in standalone mode single questions are also sent as cards (single and multi select are both checkboxes, distinguished by copy, and must be submitted), everything else stays a text reminder. Multi-question and approval prompts are notice-only and must be answered in the Web UI (the core requires all questions in one submission, which card callbacks cannot do per question); both support an "other" input
25
+ - **Interactive cards**: in core mode, single- and multi-select questions are answered in Feishu and the result is returned to the core. **Several questions at once still use one card**: every question lives in the same form and one submit button answers them all (the core requires all answers in one submission); controls are namespaced by question index, questions without options take free text through an input, and a missing answer or a multi-ticked single-select points at the offending question and redraws the card. Standalone mode sends single questions as cards too. **Approvals** are never answered in IM — the card points at the Web UI; when questions exceed the card component or size limit the bridge sends a plain-text card with the questions, options and reply format, so the user can just reply in words. Both modes support an "other" input
26
26
  - **Groups**: only messages that @mention the bot are handled (only the @-scoped group permissions are requested); the whole group is never read
27
27
  - **Task reminders**: in standalone mode, a long turn (30 minutes by default) sends a reminder with elapsed time and conclusion when it ends; a failed or externally aborted turn is reported immediately with the original error. A stop pressed by the owner is not a failure
28
28
  - **Panel**: create an app with one scan on the Web plugin page, or enter App ID / App Secret manually; see connection state, toggle send/receive and core mode, change message behaviour; in standalone mode, adjust in-turn steering, session workspace, question/approval reminders and long-task reminders
29
29
 
30
- > Question cards (checkboxes + submit + "other" input) have been verified in a real Feishu tenant in both standalone and core mode: single and multi select are distinguished by card copy, and a submission returns the checked options plus the "other" input. Rejected answers give visible feedback in both modes: a card that can still be answered is **re-rendered whole** with the reason (Feishu form submissions are one-shot; a text-only hint would leave the card unclickable) so you can re-select and submit; a card that can no longer be answered becomes invalid and loses its checkboxes. `Get` / `DONE` reactions and card layout vary slightly with tenant settings; what you see is what the platform does.
30
+ > Question cards have been verified in a real Feishu tenant in both standalone and core mode, in **both shapes — one question and several questions inside one form**: single and multi select are distinguished by card copy, several questions are laid out as ①②③ with bold headings, and a submission returns the checked options plus the "other" input per question. Rejected answers give visible feedback in both modes: a card that can still be answered is **re-rendered whole** with the reason (Feishu form submissions are one-shot; a text-only hint would leave the card unclickable), previously checked and typed content is restored, and you can adjust and submit again; a card that can no longer be answered becomes invalid and loses its checkboxes. `Get` / `DONE` reactions and card layout vary slightly with tenant settings; what you see is what the platform does.
31
31
 
32
32
  ## Install
33
33
 
@@ -93,7 +93,7 @@ Static config lives in the profile patch; restart to apply:
93
93
  maxText: 2000 # max characters per message; longer text is split automatically
94
94
  typingIndicator: true # Get on receipt; DONE on the original message after delivery (Get is never removed)
95
95
  replyStatus: false # append model, elapsed time and tokens to the answer
96
- directBusyMode: steer # a message arriving mid-turn in standalone mode: steer / queue (legacy interrupt migrates to steer)
96
+ directBusyMode: steer # all standalone sends while the target turn is running: steer / queue (legacy interrupt migrates to steer; /queue always queues)
97
97
  directWorkspaceId: '' # workspace id for standalone sessions; empty uses the auto-created default
98
98
  questionReminders: true # standalone: send a Feishu reminder when a question or approval is waiting
99
99
  questionDelayMinutes: 5 # how long to wait before reminding (0–120): answering in the Web UI within this window avoids it
@@ -115,9 +115,10 @@ Items editable in the panel are written to `$DSH_HOME/.dsh-im-lark/config.json`
115
115
  | `/status` | Current session and runtime state |
116
116
  | `/stop` | Abort the running turn |
117
117
  | `/trace` | Model and elapsed time of the last answer |
118
- | `/send <index, 0, or session id> <text>` | Send to that session; its answer comes back here. `0` is the front session. Images allowed |
118
+ | `/send <index, 0, or session id> <text>` | Send to that session; its answer comes back here. `0` is the front session. Images allowed; mid-turn behavior follows the steer setting |
119
+ | `/queue <index, 0, or session id> <text>` | Same as `/send`, but always queues until the target's current turn ends |
119
120
 
120
- `/send` is queued, so it does not interrupt the target session's running turn; sending to the current session behaves like a normal message. A `/switch` binding and its prefix survive restarts and are cleared by `/new`; after switching to a non-front session, replies are prefixed with `**session name>**`, and returning to the front session (index `0`) drops it. The front session is always index `0` and is omitted from `/sessions`, so returning to it does not produce a self-referential list. Switching is refused while that session still has a `/send` waiting for its answer. If an older `direct.json` has no front-session record, `/switch 0` asks you to start a new session with `/new`.
121
+ `/send` behaves like a normal message: while the target session is running a turn, the panel's steer setting decides whether it joins that turn or queues. `/queue` takes the same arguments but always queues until the current turn ends, regardless of the setting. A `/switch` binding and its prefix survive restarts and are cleared by `/new`; after switching to a non-front session, replies are prefixed with `**session name>**`, and returning to the front session (index `0`) drops it. The front session is always index `0` and is omitted from `/sessions`, so returning to it does not produce a self-referential list. Switching is refused while a delivery is still waiting for its answer. If an older `direct.json` has no front-session record, `/switch 0` asks you to start a new session with `/new`.
121
122
 
122
123
  ## CLI and slash command
123
124
 
@@ -131,7 +132,7 @@ The recipient is always the "owner": in standalone mode the first person who DMs
131
132
  - Standalone: while a question or tool approval is waiting for the owner, the plugin sends a Feishu reminder (delay configurable): single questions with options are answered by tapping the card; multi-question, approval and free-form input still use text (reply with a number or the option text). Long-task completion reminders remain the core's job.
132
133
  - Standalone: received files only get a notice; their contents are not fed into the session. Images are attached as input.
133
134
  - In core mode `DONE` first translates the inbound `messageId` from the core into a platform message id; that table keeps only the latest 500 entries (bounded to avoid unbounded memory): after a plugin restart, or when a message is reported complete more than 500 inbound messages later, the id cannot be resolved and no `DONE` is added — this only affects the completion reaction, never answer delivery.
134
- - IM cards only carry single-question, option-based interactions; multi-question, approval and free-form input go back to the Web UI, avoiding cards that look clickable but can never be completed.
135
+ - IM cards carry a whole group of questions (protocol limit: 10) in one submission, so a prompt never splits into several cards and never leaves a card that looks clickable but can never be completed. Approvals still go back to the Web UI; beyond 200 components or 28 KB the delivery degrades to a plain-text card that still lists the questions and options.
135
136
 
136
137
  ## Limits (platform-determined, not implementation choices)
137
138
 
package/README.md CHANGED
@@ -23,18 +23,21 @@
23
23
  - **引用回复**:回答引用触发它的那条消息,单聊与群里都成立
24
24
  - **处理反馈**:收到消息先打 `Get`(收到)表情,**回答真正送达后**才在原消息追加 `DONE`,两个标记并排保留——入队不算完成(核心把消息入队后任务才异步跑);核心模式按核心的 `completes` 完成标记打(停滞、派发回执、转投确认等都不带;取消、失败、审批被拒、超出 token 上限而中断的回合也不带,因为它们没有正常答复;旧核心不打),独立模式按回合对齐,提醒作答在**结算**后当场打(多题只答一半时提醒还在等,不盖 `DONE`);失败只保留 `Get`,不留「已完成」的假信号;同一消息只补一次。飞书没有官方「正在输入」接口
25
25
  - **卡片更新**:流式回答原地更新同一张卡片;超过平台限制(14 天、单卡 5 QPS)后改发新卡
26
- - **交互卡片**:核心模式的**单问题**单选题与多选题在飞书里勾选后提交,结果交回核心;
27
- 独立模式的单题提问同样发卡片(单选/多选都是勾选器,靠文案区分,必须点提交),其余仍走
28
- 文字提醒。多问题与审批只发提示,请在 Web 端作答(核心要求一次提交全部问题,卡片回调
29
- 做不到分题提交);两者都支持「其他」输入
26
+ - **交互卡片**:核心模式的单选题与多选题在飞书里勾选后提交,结果交回核心。**一次问多个问题
27
+ 也只用一张卡**:所有题目收在同一张表单里,一个「提交」按钮一次答完(核心要求一次提交全部
28
+ 问题),控件按题号区分,无选项的题用输入框自由作答;少答或单选勾多项会指出是第几题并重画
29
+ 卡片。独立模式的单题提问同样发卡片。**审批**不代答,只提示去 Web 端处理;题目多到超过卡片
30
+ 组件/体积上限时改发纯文本卡片,把题目、选项与回复格式一并写出,可直接回复文字作答。两种模式都
31
+ 支持「其他」输入
30
32
  - **群聊**:只响应 @机器人 的消息(群聊权限只申请 @ 系列),不读全群
31
33
  - **任务提醒**:独立模式下,回合跑得久(默认超过 30 分钟)会在结束时发一条带耗时与结论的提醒;回合失败或被外部中止时立刻提醒并带上原始错误。主人自己按的停止不算失败
32
34
  - **面板**:Web 端插件页扫码一键创建应用,或手动填 App ID / App Secret;看连接状态、开关收发/核心接入、改消息行为;独立模式可调回合内插话、会话工作区、问题/审批提醒与长程任务提醒
33
35
 
34
- > 提问卡片(勾选器 + 提交 + 「其他」输入)在独立模式与核心模式都已于真实飞书租户验收:
35
- > 单选/多选靠卡片文案区分,提交后回传勾选项与「其他」输入。两种模式的作答被拒都有可见
36
- > 反馈:还能答的卡会**整卡重画**并写上原因(飞书的表单提交是一次性的,只发一句提示
37
- > 会让卡片点不动),可以重新勾选提交;已经不能再答的卡直接改成失效态、去掉勾选框。
36
+ > 提问卡片已在独立模式与核心模式的真实飞书租户验收,**单题与多题收进同一张表单**两种形态
37
+ > 都跑通了:单选/多选靠卡片文案区分,多题按 ①②③ 排开、题干加粗,提交后按题号回传勾选项
38
+ > 与「其他」输入。两种模式的作答被拒都有可见反馈:还能答的卡会**整卡重画**并写上原因
39
+ > (飞书的表单提交是一次性的,只发一句提示会让卡片点不动),已勾选与已填内容会回填,可以
40
+ > 改完再提交;已经不能再答的卡直接改成失效态、去掉勾选框。
38
41
  > `Get` / `DONE` 表情与卡片排版随租户配置略有差异,以实际表现为准。
39
42
 
40
43
  ## 安装
@@ -104,7 +107,7 @@ dsh plugin --profile web add file:/绝对路径/dsh-im-lark
104
107
  maxText: 2000 # 单条消息字符上限,超出自动切段
105
108
  typingIndicator: true # 收到打 Get;回答送达后原消息追加 DONE(Get 不撤)
106
109
  replyStatus: false # 回答末尾附加模型、耗时、token
107
- directBusyMode: steer # 独立会话里回合进行中又来消息:steer 插话 / queue 排队(旧值 interrupt 会迁移成 steer)
110
+ directBusyMode: steer # 独立模式所有发送在目标回合进行中:steer 插话 / queue 排队(旧值 interrupt 迁移成 steer;/queue 固定排队)
108
111
  directWorkspaceId: '' # 独立会话的工作区 id;留空用自动创建的默认工作区
109
112
  questionReminders: true # 独立模式下有问题/授权在等时发飞书提醒
110
113
  questionDelayMinutes: 5 # 提醒前等多久(0–120):这段时间在 Web 端答完就不打扰
@@ -126,9 +129,10 @@ dsh plugin --profile web add file:/绝对路径/dsh-im-lark
126
129
  | `/status` | 当前会话与运行状态 |
127
130
  | `/stop` | 中止正在跑的回合 |
128
131
  | `/trace` | 查看上一条回答的模型与耗时 |
129
- | `/send <序号、0 或会话 id> <内容>` | 发到指定会话,那一轮的回答回到这里;`0` 是前台会话;可附图 |
132
+ | `/send <序号、0 或会话 id> <内容>` | 发到指定会话,那一轮的回答回到这里;`0` 是前台会话;可附图;回合正在跑时按「回合内插话」设置处理 |
133
+ | `/queue <序号、0 或会话 id> <内容>` | 与 `/send` 相同,但始终排队到目标当前回合之后 |
130
134
 
131
- `/send` 用排队提交,不打断目标会话正在跑的回合;目标是当前会话时与直接发消息一致。`/switch` 的换绑与前缀会跨重启保留,`/new` 会清掉;切到非前台会话后,回复前会加 `**会话名>**`,切回前台(序号 `0`)后前缀消失。前台会话固定为序号 `0`,不出现在 `/sessions` 里,避免回到前台后列表里又套一个自己。目标会话还有 `/send` 在等回答时不允许切换。旧版 `direct.json` 没有前台记录时,`/switch 0` 会提示用 `/new` 重新建会话。
135
+ `/send` 与普通消息一样,在目标会话正在跑回合时按面板的「回合内插话」设置决定插进当前回合还是排队;`/queue` 参数相同,但始终排队到当前回合之后,不受该设置影响。`/switch` 的换绑与前缀会跨重启保留,`/new` 会清掉;切到非前台会话后,回复前会加 `**会话名>**`,切回前台(序号 `0`)后前缀消失。前台会话固定为序号 `0`,不出现在 `/sessions` 里,避免回到前台后列表里又套一个自己。目标会话还有投递在等回答时不允许切换。旧版 `direct.json` 没有前台记录时,`/switch 0` 会提示用 `/new` 重新建会话。
132
136
 
133
137
  ## 命令行与斜杠命令
134
138
 
@@ -147,8 +151,9 @@ dsh plugin --profile web add file:/绝对路径/dsh-im-lark
147
151
  - 核心模式的 `DONE` 要先把握手的入站 `messageId` 翻成平台消息 id,这张表只留最近 500 条
148
152
  (有界,防常驻内存增长):插件重启后,或一条消息在 500 条之后才被核心报完成,
149
153
  就翻不到平台 id、不打 `DONE`——只影响完成表情,不影响答案送达。
150
- - IM 卡片只承载单问题、有选项的交互;多问题/审批/自由输入交回 Web 端,避免出现
151
- 「点了按钮但永远答不完」的卡。
154
+ - IM 卡片一次承载整组题目(协议上限 10 题):多题共用一张表单一次提交,既不会出现
155
+ 「点了按钮但永远答不完」的卡,也不会把一次提问拆成多张卡。审批仍交回 Web 端;超过
156
+ 200 组件或 28 KB 时降级为纯文本卡片,题目与选项照常发出。
152
157
 
153
158
  ## 限制(平台决定,不是实现取舍)
154
159
 
@@ -1,5 +1,7 @@
1
1
  # 0008 独立模式 /send 按会话序号投递
2
2
 
3
+ > 提交模式部分已被 [0011](0011-独立模式send遵循插话设置与queue命令.md) 取代:`/send` 现在遵循「回合内插话」,固定排队用 `/queue`。序号快照、回答回收与超时等决定仍有效。
4
+
3
5
  ## 背景
4
6
 
5
7
  独立模式最初的 `/send <文本>` 只是把文本回显到当前聊天,与核心模式的
@@ -0,0 +1,29 @@
1
+ # 0011 独立模式 /send 遵循插话设置与 /queue 固定排队
2
+
3
+ ## 背景
4
+
5
+ 0008 让 `/send` 固定用 `queue` 提交,面板的「回合内插话」只影响普通消息和发到当前会话的 `/send`。主人希望同一个开关覆盖所有发送,同时保留一个明确不打断目标回合的投递方式。
6
+
7
+ ## 决策
8
+
9
+ - 「回合内插话」作用于独立模式的所有发送:普通消息、`/send` 到当前会话、`/send` 到其它会话。目标回合正在跑时,`steer` 插进当前回合,`queue` 排到下一轮。
10
+ - 新增 `/queue <序号、0 或会话 id> <内容>`,参数与 `/send` 相同,固定用 `queue`,不受面板设置影响。
11
+ - `/send` 与 `/queue` 共用会话定位、图片、回答观察与 `DONE` 对齐,只有提交模式不同。排队且目标正在跑时跳过上一轮的 `turn/end`;插话时当前 `turn/end` 就是这一条的回答。
12
+ - 排队一律按「先跳过目标当前那一轮」对齐(不看列表快照是否说在跑):目标要么先发 `turn/start`,要么这一轮 `turn/end` 直接被跳过,快照过期也不会把别人的回答当成自己的。
13
+ - 当前绑定会话的「正在跑」以 `turn/start`/`turn/end` 维护的状态为准(比列表快照新):排队消息在提交时已知目标在跑,就不随那一轮打 `DONE`,等它自己那轮结束再补。
14
+ - 面板提示写明 `/queue` 始终排队;两个命令在等回答时都拒绝同目标的第二条投递与 `/switch`。
15
+
16
+ ## 后果
17
+
18
+ - 已知限制一:`steer` 的判断依赖提交前一刻的运行状态;列表说空闲、提交时目标已在跑(Web 端刚发起)时,这一条仍可能被并进别人的回合,回答与 `DONE` 归属只能靠事件顺序推断。宿主事件没有可对账的 prompt id(`turn/start` 只有 `{turn}`),这一层无法消除。
19
+ - 已知限制二:排队消息的 `waiting` 是提交前读到「正在跑」时打的一次性标记。若那一轮恰好在提交等待期间结束,标记会留到它自己那轮结束时才被清掉,表现为这条漏打 `DONE`(漏打优于误打);根治可用事件里已有的 `turn` 号对账(`turn/start` 与 `turn/end` 的 data 都带 `turn`),当前实现未用。
20
+
21
+ - 默认 `steer` 下,`/send` 到正在跑的其它会话也会插话,可能改变目标当前回合;需要不打断时用 `/queue`。
22
+ - `/queue` 保留 0008 的固定排队语义;0008 关于序号快照、回答回收、超时的其余决定继续有效。
23
+ - 主人可以用命令显式选择是否排队,不必先改面板设置。
24
+
25
+ ## 备选
26
+
27
+ - 保留 `/send` 固定 `queue`,只把设置扩到普通消息:不满足「所有发送」。
28
+ - 用 `/send --queue` 参数:与「第一个空白前是会话引用」的解析冲突,也不直观。
29
+ - 新增一个独立的 `/queue` 会话命令:参数与 `/send` 重复,未采用。
@@ -15,6 +15,7 @@
15
15
  | [0008](0008-独立模式send按会话序号投递.md) | 独立模式 `/send` 按会话序号投递 | 已接受 |
16
16
  | [0009](0009-独立模式switch切换并标记回复来源.md) | 独立模式 `/switch` 切换会话并标记回复来源 | 已接受 |
17
17
  | [0010](0010-独立模式前台会话与switch-0.md) | 独立模式的前台会话与 `/switch 0` | 已接受 |
18
+ | [0011](0011-独立模式send遵循插话设置与queue命令.md) | 独立模式 `/send` 遵循插话设置,新增 `/queue` | 已接受 |
18
19
 
19
20
  新增决策时:编号连续、标题用「做了什么」而不是「为什么」、状态只有
20
21
  `提议 / 已接受 / 已废弃 / 被 NNNN 取代`。改主意不要改旧 ADR 的结论,追加新的一条
package/lib/client.js CHANGED
@@ -147,7 +147,7 @@ var en_default = {
147
147
  "direct.summaryCore": "Switched to the super agent; these settings no longer apply",
148
148
  "direct.groupSession": "Session",
149
149
  "direct.steer": "Steer the running turn",
150
- "direct.steerHint": "A message sent mid-turn joins the current turn; off means it queues until the turn ends.",
150
+ "direct.steerHint": "Regular messages and /send join a running turn; turn this off to queue until the turn ends. /queue always queues and ignores this switch.",
151
151
  "direct.workspace": "Session workspace",
152
152
  "direct.workspaceHint": "Where new sessions are created; send /new to apply.",
153
153
  "direct.workspaceDefault": "Default (Lark session)",
@@ -259,7 +259,7 @@ var zh_default = {
259
259
  "direct.summaryCore": "\u5DF2\u5207\u6362\u5230\u8D85\u7EA7\u52A9\u7406\uFF0C\u6B64\u5904\u914D\u7F6E\u4E0D\u518D\u751F\u6548",
260
260
  "direct.groupSession": "\u4F1A\u8BDD",
261
261
  "direct.steer": "\u56DE\u5408\u5185\u63D2\u8BDD",
262
- "direct.steerHint": "\u56DE\u5408\u6B63\u5728\u8DD1\u65F6\u53C8\u53D1\u6D88\u606F\uFF0C\u76F4\u63A5\u63D2\u8FDB\u5F53\u524D\u56DE\u5408\uFF1B\u5173\u6389\u5219\u6392\u961F\u7B49\u8FD9\u4E00\u8F6E\u7ED3\u675F\u3002",
262
+ "direct.steerHint": "\u666E\u901A\u6D88\u606F\u4E0E /send \u5728\u76EE\u6807\u56DE\u5408\u6B63\u5728\u8DD1\u65F6\u76F4\u63A5\u63D2\u8FDB\u5F53\u524D\u56DE\u5408\uFF1B\u5173\u6389\u5219\u6392\u961F\u7B49\u8FD9\u4E00\u8F6E\u7ED3\u675F\u3002/queue \u59CB\u7EC8\u6392\u961F\uFF0C\u4E0D\u53D7\u6B64\u5F00\u5173\u5F71\u54CD\u3002",
263
263
  "direct.workspace": "\u4F1A\u8BDD\u5DE5\u4F5C\u533A",
264
264
  "direct.workspaceHint": "\u65B0\u4F1A\u8BDD\u5EFA\u5728\u54EA\u4E2A\u5DE5\u4F5C\u533A\uFF1B\u6539\u5B8C\u53D1 /new \u751F\u6548\u3002",
265
265
  "direct.workspaceDefault": "\u9ED8\u8BA4\uFF08\u98DE\u4E66\u4F1A\u8BDD\uFF09",
@@ -0,0 +1,30 @@
1
+ /**
2
+ * 桥的文本与错误辅助:卡片装不下时的降级文本、协议回执的错误截断与错误码。
3
+ *
4
+ * 独立成模块,让 `bridge.ts` 只留协议语义;这些函数不持有状态。
5
+ */
6
+ import { interactionText } from "./question-text.js";
7
+ /**
8
+ * 卡片装不下时退回的文本内容(由 outbound 渲染成 markdown 卡片)。
9
+ *
10
+ * 交互必须一起带上——降级后用户只能文字作答,不给题目和选项就没法答;
11
+ * 标题由 outbound 的卡片头渲染,正文不再重复 `#`。
12
+ */
13
+ export function renderFallbackText(delivery) {
14
+ const interaction = delivery.interaction === undefined
15
+ ? ''
16
+ : interactionText(delivery.interaction, { markdown: false });
17
+ return [delivery.body, delivery.detail ?? '', interaction]
18
+ .filter((part) => part !== '')
19
+ .join('\n\n');
20
+ }
21
+ /** 协议规定 `Receipt.error` 最多 500 字符;超长会让核心解析失败并按 uncertain 重投。 */
22
+ function truncateError(error) {
23
+ return error.length > 500 ? `${error.slice(0, 497)}...` : error;
24
+ }
25
+ export function failure(deliveryId, errorCode, error) {
26
+ return { deliveryId, status: 'failed', messagesSent: 0, errorCode, error: truncateError(error) };
27
+ }
28
+ export function errorText(error) {
29
+ return error instanceof Error ? error.message : String(error);
30
+ }
@@ -8,7 +8,9 @@
8
8
  * (核心侧对「先注册后确定账号」没有宽容度)。
9
9
  */
10
10
  import { DEFAULT_MAX_TEXT_CHARS } from "../shared/limits.js";
11
- import { InteractionStore, answerFromAction, renderAnsweredCard, renderDeliveryCard, renderRejectedCard, renderRetryCard, renderSupersededCard, cardFits } from "./cards.js";
11
+ import { InteractionStore, MAX_ANSWER_QUESTIONS, answerFromAction, isAnswerable, renderAnsweredCard, renderDeliveryCard, renderRejectedCard, renderRetryCard, renderSupersededCard, cardFits } from "./cards.js";
12
+ import { errorText, failure, renderFallbackText } from "./bridge-text.js";
13
+ import { extensionOf } from "./media.js";
12
14
  import { OutboundChannel } from "./outbound.js";
13
15
  import { PROTOCOL_V1 } from "./protocol.js";
14
16
  /** 能力声明:与方案设计 6.3 一致,测试里会用核心的 Schema 再校验一次。 */
@@ -20,7 +22,8 @@ const CAPABILITIES = {
20
22
  multiChoice: true,
21
23
  freeText: true,
22
24
  maxOptions: 20,
23
- maxQuestions: 1,
25
+ // 多题收进同一张表单一次提交,所以能答满协议上限(`maxQuestions` schema 封顶 10)。
26
+ maxQuestions: MAX_ANSWER_QUESTIONS,
24
27
  updateAfterAnswer: 'any',
25
28
  }),
26
29
  edit: Object.freeze({ scope: 'any', minIntervalMs: 2_000, maxLifetimeMs: 14 * 24 * 60 * 60 * 1000 }),
@@ -43,7 +46,7 @@ const REJECT_NOTES = {
43
46
  'wrong-bridge': '这张卡不属于当前接入的机器人,请在 Web 端作答。',
44
47
  'not-delivered': '这张卡没有真正发出去,请在 Web 端作答。',
45
48
  'not-allowed': '审批要在 Web 端处理,飞书里不代答。',
46
- 'invalid-answer': '没认出这次选择:单选题只能勾一项,勾选(或填「其他」)后点「提交」。',
49
+ 'invalid-answer': '没认出这次选择:单选题只能勾一项,选好后点「提交」。',
47
50
  };
48
51
  /** 这些原因意味着卡不能再答:改成失效态,别留一排还能点的勾选框。 */
49
52
  const TERMINAL_REJECTS = new Set(['unknown', 'expired', 'wrong-bridge', 'not-delivered']);
@@ -143,9 +146,11 @@ export function createLarkBridge(deps) {
143
146
  }));
144
147
  const card = renderDeliveryCard(delivery, { interactive: delivery.interaction !== undefined });
145
148
  const useCard = cardFits(card);
146
- // 交互卡装不下时明确降级并回失败码:只发纯文本再回「已送达」会让核心白等作答。
149
+ // 交互卡装不下时明确降级:内容照发,回执里带 error 说明;状态仍是 sent,避免核心重投。
147
150
  const interactionDropped = delivery.interaction !== undefined && !useCard;
148
- const fallbackNote = interactionDropped ? '\n\n(内容超过卡片上限,交互已降级为纯文本;请在 Web 端作答。)' : '';
151
+ const fallbackNote = interactionDropped
152
+ ? '\n\n(内容超过卡片上限,交互已降级为纯文本卡片;可直接回复文字作答,或到 Web 端处理。)'
153
+ : '';
149
154
  const replyTarget = delivery.inReplyTo === undefined ? undefined : inbound.get(delivery.inReplyTo);
150
155
  let receipt;
151
156
  try {
@@ -191,7 +196,7 @@ export function createLarkBridge(deps) {
191
196
  if (delivery.interaction !== undefined &&
192
197
  messageId !== undefined &&
193
198
  useCard &&
194
- delivery.interaction.questions.length === 1) {
199
+ isAnswerable(delivery.interaction)) {
195
200
  // 同一交付被重投时不要把它从「已作答」打回待作答。
196
201
  const previous = interactions.get(deliveryId);
197
202
  interactions.add({
@@ -214,9 +219,9 @@ export function createLarkBridge(deps) {
214
219
  ...(receipt.messageIds.length === 0 ? {} : { bridgeMessageIds: receipt.messageIds }),
215
220
  messagesSent: receipt.messagesSent,
216
221
  };
217
- // 内容已经送达,只是交互改在 Web 端作答;回成功避免核心重投或换路由。
222
+ // 内容已经送达,只是交互改在文字上作答;回成功避免核心重投或换路由。
218
223
  return interactionDropped
219
- ? { ...sent, error: '交互卡片超过平台上限,已降级为纯文本;请在 Web 端作答' }
224
+ ? { ...sent, error: '交互卡片超过平台上限,已降级为纯文本卡片;可在飞书回复文字作答,或到 Web 端处理' }
220
225
  : sent;
221
226
  }
222
227
  /**
@@ -279,14 +284,17 @@ export function createLarkBridge(deps) {
279
284
  * 提示等于把这张卡废掉;不能再答的直接改成失效态。都改写同一张卡,所以不会刷屏;
280
285
  * 只有卡不在手里(重启丢了记录、回调缺 id)才退化成一条文字提示,同卡同原因只发一次。
281
286
  */
282
- async function noteRejected(action, reason, deliveryId) {
287
+ async function noteRejected(action, reason, deliveryId, detail) {
283
288
  if (reason === 'settled')
284
289
  return; // 已经答过了,再提示只会吵。
285
- const note = REJECT_NOTES[reason];
290
+ // 具体原因(第几题没答、第几题多勾了)比通用文案有用,有就用它。
291
+ const note = detail !== undefined && detail !== '' ? detail : REJECT_NOTES[reason];
286
292
  const terminal = TERMINAL_REJECTS.has(reason);
287
293
  const record = deliveryId === undefined ? undefined : delivered.get(deliveryId);
288
294
  if (record !== undefined) {
289
- const card = terminal ? renderRejectedCard(record.delivery, note) : renderRetryCard(record.delivery, note);
295
+ const card = terminal
296
+ ? renderRejectedCard(record.delivery, note)
297
+ : renderRetryCard(record.delivery, note, action.formValues);
290
298
  try {
291
299
  await deps.api.patchCard({ messageId: record.messageId, card });
292
300
  if (terminal && deliveryId !== undefined)
@@ -308,9 +316,16 @@ export function createLarkBridge(deps) {
308
316
  }
309
317
  async function applyCardAction(action) {
310
318
  const deliveryId = typeof action.value.d === 'string' ? action.value.d : undefined;
311
- const result = answerFromAction(action.value, action.option, action.formValues, action.operatorOpenId, interactions, now());
319
+ // 审批卡不登记待作答(见 isAnswerable),回调到这里就没有记录;不先认出来会
320
+ // 被当成「已失效」并提示重启过,与实际原因(IM 不代答审批)相差很远。
321
+ const sentRecord = deliveryId === undefined ? undefined : delivered.get(deliveryId);
322
+ if (sentRecord?.delivery.interaction?.questions.some((question) => question.intent === 'approval') === true) {
323
+ await noteRejected(action, 'not-allowed', deliveryId);
324
+ return { accepted: false, reason: 'not-allowed', detail: '审批请在 Web 端处理' };
325
+ }
326
+ const result = answerFromAction(action.value, action.formValues, action.operatorOpenId, interactions, now());
312
327
  if ('reason' in result) {
313
- await noteRejected(action, result.reason, deliveryId);
328
+ await noteRejected(action, result.reason, deliveryId, result.detail);
314
329
  return { accepted: false, reason: result.reason, ...(result.detail === undefined ? {} : { detail: result.detail }) };
315
330
  }
316
331
  const accepted = await answer(result);
@@ -385,29 +400,3 @@ function remember(map, key, value, max) {
385
400
  map.delete(oldest.value);
386
401
  }
387
402
  }
388
- /** 协议规定 `Receipt.error` 最多 500 字符;超长会让核心解析失败并按 uncertain 重投。 */
389
- function truncateError(error) {
390
- return error.length > 500 ? `${error.slice(0, 497)}...` : error;
391
- }
392
- function failure(deliveryId, errorCode, error) {
393
- return { deliveryId, status: 'failed', messagesSent: 0, errorCode, error: truncateError(error) };
394
- }
395
- /** 卡片装不下时退回「标题 + 正文」:由 outbound 按文本分条。 */
396
- function renderFallbackText(delivery) {
397
- return [`# ${delivery.title}`, delivery.body, delivery.detail ?? ''].filter((part) => part !== '').join('\n\n');
398
- }
399
- function errorText(error) {
400
- return error instanceof Error ? error.message : String(error);
401
- }
402
- function extensionOf(mediaType) {
403
- switch (mediaType) {
404
- case 'image/jpeg':
405
- return 'jpg';
406
- case 'image/webp':
407
- return 'webp';
408
- case 'image/gif':
409
- return 'gif';
410
- default:
411
- return 'png';
412
- }
413
- }