dsh-im-lark 0.1.0 → 0.1.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
@@ -28,9 +28,10 @@
28
28
  - **面板**:Web 端插件页扫码一键创建应用,或手动填 App ID / App Secret;看连接状态、开关收发/核心接入、改消息行为;独立模式可调回合内插话、会话工作区、问题/审批提醒与长程任务提醒
29
29
 
30
30
  > 提问卡片(勾选器 + 提交 + 「其他」输入)在独立模式与核心模式都已于真实飞书租户验收:
31
- > 单选/多选靠卡片文案区分,提交后回传勾选项与「其他」输入。已知差异:独立模式勾了多项
32
- > 会回一句提示并保留待答,核心模式的作答由核心校验,被拒时**没有可见反馈**(只有插件
33
- > 调试日志)。`Typing` 表情与卡片排版随租户配置略有差异,以实际表现为准。
31
+ > 单选/多选靠卡片文案区分,提交后回传勾选项与「其他」输入。两种模式的作答被拒都有可见
32
+ > 反馈:还能答的卡会**整卡重画**并写上原因(飞书的表单提交是一次性的,只发一句提示
33
+ > 会让卡片点不动),可以重新勾选提交;已经不能再答的卡直接改成失效态、去掉勾选框。
34
+ > `Typing` 表情与卡片排版随租户配置略有差异,以实际表现为准。
34
35
 
35
36
  ## 安装
36
37
 
@@ -58,6 +58,28 @@ Web 与飞书可能同时有人在等;两边都答会重复或矛盾;审批
58
58
  > 真机验收:多选勾两项提交两项都回传;单选勾一项提交回传一项。
59
59
  > 参考:[表单容器](https://open.feishu.cn/document/feishu-cards/card-json-v2-components/containers/form-container)、
60
60
  > [勾选器](https://open.feishu.cn/document/feishu-cards/card-json-v2-components/interactive-components/checker)。
61
+ >
62
+ > 2026-11-22 修订(核心模式):核心模式的卡片作答原先只在被拒时写调试日志,用户侧
63
+ > 表现为「勾完点提交没反应」。现在对**每一次拒绝**都说明原因:能继续答的卡(例如单选
64
+ > 勾了两项)**整卡重画**——保留勾选框与提交按钮、附上原因;已经不能再答的
65
+ > (`unknown` / `expired` / `wrong-bridge` / `not-delivered`)改成失效态、去掉勾选框并
66
+ > 清掉待作答;`settled`(已经答过)保持静默。
67
+ >
68
+ > 为什么必须重画而不是发一句提示:实测(2026-11-22,真实租户)提交过的卡再点「提交」
69
+ > 收不到回调——第一版只补了一句文字提示,用户报「提示收到了,但此后不能再答」。官方把
70
+ > 「表单提交后更新为已提交卡片」列为全量更新的典型场景,正是这个用法
71
+ > ([更新卡片](https://open.feishu.cn/document/feishu-cards/update-feishu-card))。
72
+ > 「提交后这张卡的控件一律失效」是实测结论,未在文档里找到明文,按现象记录。
73
+ > 独立模式的提醒卡同样改为重画(`questions.ts` 的 `#noteChoice`)。
74
+ >
75
+ > 只有卡不在手里(重启丢了记录、回调缺 id)才退化成一条文字提示,同卡同原因只发一次。
76
+ >
77
+ > 同次排查还挖出第二个原因:入站去重键只看按钮的 `value`(`{d,q}`),而同一张卡每次
78
+ > 提交的 `value` 完全一样——勾选项在 `form_value` 里。于是「勾两项被拒 → 改勾一项重新
79
+ > 提交」的第二次回调被当成平台重推丢掉,用户侧同样表现为「此后不能再答」。现在去重键
80
+ > 带上 `form_value` 与 `option`;没有 `event_id` 时只覆盖 5s 重推窗口,窗口之外允许同一
81
+ > 张卡再次提交(平台重推仍由待作答状态兜底:第二次会得到 `settled`,静默丢弃)。
82
+ > 单测覆盖:同一张卡改选项再提交能处理、同一份回调在窗口内只处理一次。
61
83
 
62
84
  ## 备选
63
85
 
@@ -8,7 +8,7 @@
8
8
  * (核心侧对「先注册后确定账号」没有宽容度)。
9
9
  */
10
10
  import { DEFAULT_MAX_TEXT_CHARS } from "../shared/limits.js";
11
- import { InteractionStore, answerFromAction, renderAnsweredCard, renderDeliveryCard, renderSupersededCard, cardFits } from "./cards.js";
11
+ import { InteractionStore, answerFromAction, renderAnsweredCard, renderDeliveryCard, renderRejectedCard, renderRetryCard, renderSupersededCard, cardFits } from "./cards.js";
12
12
  import { OutboundChannel } from "./outbound.js";
13
13
  import { PROTOCOL_V1 } from "./protocol.js";
14
14
  /** 能力声明:与方案设计 6.3 一致,测试里会用核心的 Schema 再校验一次。 */
@@ -31,6 +31,22 @@ const CAPABILITIES = {
31
31
  outbound: Object.freeze({ requiresPriorInbound: false, images: true }),
32
32
  };
33
33
  export const LARK_CAPABILITIES = Object.freeze(CAPABILITIES);
34
+ /**
35
+ * 作答被拒的可见说明:核心模式没有独立模式那条提示通道,拒绝如果只写调试日志,
36
+ * 用户看到的就是「点了没反应」。文案对齐独立模式(`questions.ts` 的提示)。
37
+ */
38
+ const REJECT_NOTES = {
39
+ invalid: '这张卡的作答信息不完整,请在 Web 端作答。',
40
+ 'not-owner': '这张卡只认主人作答,请在 Web 端作答。',
41
+ unknown: '这张卡已经失效(插件重启过),不再接收作答;请在 Web 端重新作答。',
42
+ expired: '这张卡已经过期,请在 Web 端重新作答。',
43
+ 'wrong-bridge': '这张卡不属于当前接入的机器人,请在 Web 端作答。',
44
+ 'not-delivered': '这张卡没有真正发出去,请在 Web 端作答。',
45
+ 'not-allowed': '审批要在 Web 端处理,飞书里不代答。',
46
+ 'invalid-answer': '没认出这次选择:单选题只能勾一项,勾选(或填「其他」)后点「提交」。',
47
+ };
48
+ /** 这些原因意味着卡不能再答:改成失效态,别留一排还能点的勾选框。 */
49
+ const TERMINAL_REJECTS = new Set(['unknown', 'expired', 'wrong-bridge', 'not-delivered']);
34
50
  /** 已交付记录与流式修订的内存上限:长跑进程不能让它们无限涨。 */
35
51
  const MAX_DELIVERED = 2_000;
36
52
  /** 入站引用表与「收件人 → 会话」表的上限。 */
@@ -219,13 +235,53 @@ export function createLarkBridge(deps) {
219
235
  }
220
236
  interactions.markAnswered(deliveryId);
221
237
  }
238
+ /**
239
+ * 作答被拒时说清原因。
240
+ *
241
+ * 核心模式没有独立模式那条提示通道:拒绝如果只写调试日志,用户看到的就是「点了没反应」。
242
+ * 能答的卡要**重画**——飞书的表单提交是一次性的,提交过的卡不会再触发回调,只发一句
243
+ * 提示等于把这张卡废掉;不能再答的直接改成失效态。都改写同一张卡,所以不会刷屏;
244
+ * 只有卡不在手里(重启丢了记录、回调缺 id)才退化成一条文字提示,同卡同原因只发一次。
245
+ */
246
+ async function noteRejected(action, reason, deliveryId) {
247
+ if (reason === 'settled')
248
+ return; // 已经答过了,再提示只会吵。
249
+ const note = REJECT_NOTES[reason];
250
+ const terminal = TERMINAL_REJECTS.has(reason);
251
+ const record = deliveryId === undefined ? undefined : delivered.get(deliveryId);
252
+ if (record !== undefined) {
253
+ const card = terminal ? renderRejectedCard(record.delivery, note) : renderRetryCard(record.delivery, note);
254
+ try {
255
+ await deps.api.patchCard({ messageId: record.messageId, card });
256
+ if (terminal && deliveryId !== undefined)
257
+ interactions.delete(deliveryId);
258
+ return;
259
+ }
260
+ catch (error) {
261
+ logger.debug?.('卡片改写失败,改为发提示', errorText(error));
262
+ }
263
+ }
264
+ const key = `note:${action.messageId}:${reason}`;
265
+ const receipt = await outbound.deliver({ deliveryId: key, chatId: action.chatId, text: note }).catch((error) => {
266
+ logger.debug?.('作答被拒的提示没发出去', errorText(error));
267
+ return undefined;
268
+ });
269
+ if (receipt !== undefined && receipt.errorCode !== undefined) {
270
+ logger.debug?.('作答被拒的提示发送失败', `${receipt.errorCode} ${receipt.error ?? ''}`);
271
+ }
272
+ }
222
273
  async function applyCardAction(action) {
274
+ const deliveryId = typeof action.value.d === 'string' ? action.value.d : undefined;
223
275
  const result = answerFromAction(action.value, action.option, action.formValues, action.operatorOpenId, interactions, now());
224
- if ('reason' in result)
276
+ if ('reason' in result) {
277
+ await noteRejected(action, result.reason, deliveryId);
225
278
  return { accepted: false, reason: result.reason, ...(result.detail === undefined ? {} : { detail: result.detail }) };
279
+ }
226
280
  const accepted = await answer(result);
227
- if (!accepted.accepted)
281
+ if (!accepted.accepted) {
282
+ await noteRejected(action, accepted.reason, deliveryId);
228
283
  return accepted;
284
+ }
229
285
  interactions.markAnswered(result.deliveryId);
230
286
  const record = delivered.get(result.deliveryId);
231
287
  if (record === undefined)
@@ -174,6 +174,30 @@ export function renderSupersededCard(delivery) {
174
174
  body.elements.push({ tag: 'markdown', content: '_这条已过期,已被新卡片替代。_', text_size: 'notation' });
175
175
  return card;
176
176
  }
177
+ /**
178
+ * 作答被拒但还能重来的卡:保留勾选框与提交按钮,附上原因。
179
+ *
180
+ * 飞书的表单提交是一次性的——提交之后这张卡的控件不会再触发回调,只有整卡改写才
181
+ * 回到可用状态(官方「表单提交后更新为已提交卡片」的同一套做法)。所以拒绝时不能只
182
+ * 发一句提示,得把这张卡本身重画一遍。
183
+ */
184
+ export function renderRetryCard(delivery, note) {
185
+ const card = renderDeliveryCard(delivery, { interactive: true });
186
+ const body = card.body;
187
+ body.elements.push({ tag: 'markdown', content: `_**${note}**_`, text_size: 'notation' });
188
+ return card;
189
+ }
190
+ /**
191
+ * 作答被拒且这张卡不能再答的终态:去掉按钮,写清为什么。
192
+ *
193
+ * 留着一排还能点的勾选框,用户只会反复点、反复被拒,还以为插件坏了。
194
+ */
195
+ export function renderRejectedCard(delivery, note) {
196
+ const card = renderDeliveryCard(delivery, { interactive: false });
197
+ const body = card.body;
198
+ body.elements.push({ tag: 'markdown', content: `_${note}_`, text_size: 'notation' });
199
+ return card;
200
+ }
177
201
  /**
178
202
  * 未作答的交互卡。
179
203
  *
@@ -23,6 +23,8 @@ const TEXT_MAX = 20_000;
23
23
  const REACTION_TYPING = 'Typing';
24
24
  const REACTION_DONE = 'DONE';
25
25
  const KNOWN_IMAGE_TYPES = new Set(['image/png', 'image/jpeg', 'image/webp', 'image/gif']);
26
+ /** 卡片回调的重推窗口:没有 `event_id` 时用它兜住平台重推,窗口之外允许同一张卡再提交。 */
27
+ const CARD_RETRY_WINDOW_MS = 5_000;
26
28
  export class InboundPipeline {
27
29
  #api;
28
30
  #bridge;
@@ -100,7 +102,12 @@ export class InboundPipeline {
100
102
  this.#logger.debug?.('非主人卡片回调,忽略', action.operatorOpenId);
101
103
  return;
102
104
  }
103
- const key = `card:${action.eventId || `${action.messageId}:${JSON.stringify(action.value)}`}`;
105
+ // 去重键要带上**表单值**:同一张卡每次提交的 `value` 都一样,勾选项在 `form_value` 里。
106
+ // 只按 `value` 去重会把「改一项重新提交」当成平台重推丢掉——真机上表现为「提示收到了,
107
+ // 但此后不能再答」。没有 `event_id` 时也只覆盖重推窗口(几秒),窗口之外允许再提交。
108
+ const stamp = Math.floor(this.#now() / CARD_RETRY_WINDOW_MS);
109
+ const key = `card:${action.eventId ||
110
+ `${action.messageId}:${JSON.stringify(action.value)}:${JSON.stringify(action.formValues ?? null)}:${String(action.option ?? '')}:${stamp}`}`;
104
111
  if (!this.#seen.claim(key))
105
112
  return;
106
113
  this.#enqueue(async () => {
@@ -259,11 +259,11 @@ export class QuestionBroker {
259
259
  return false;
260
260
  const answer = answerFromCard(question, action.value, action.option, action.formValues);
261
261
  if (answer === null) {
262
- this.#noteChoice(`提醒 #${pending.no} 还没选择任何选项,勾选后再点「提交」。`);
262
+ this.#noteChoice(pending, `提醒 #${pending.no} 还没选择任何选项,勾选后再点「提交」。`);
263
263
  return true;
264
264
  }
265
265
  if (question.multiSelect !== true && answer.selected.length > 1) {
266
- this.#noteChoice(`提醒 #${pending.no} 是单选题,只能勾选一项,请重新提交。`);
266
+ this.#noteChoice(pending, `提醒 #${pending.no} 是单选题,只能勾选一项,请重新提交。`);
267
267
  return true;
268
268
  }
269
269
  pending.resolve({ answers: [answer] });
@@ -272,8 +272,26 @@ export class QuestionBroker {
272
272
  this.#closeCard(pending, `已在飞书作答:${chosen}`);
273
273
  return true;
274
274
  }
275
- /** 提交不合规时回一句提示,待答保留(卡片还能再提交)。 */
276
- #noteChoice(message) {
275
+ /**
276
+ * 提交不合规:把同一张卡连同原因重画一遍,待答保留。
277
+ *
278
+ * 飞书的表单提交是一次性的——提交过的卡不会再触发回调,只回一句文字会让卡片表面
279
+ * 还在、实际点不动。没有卡片或卡 id 时才退回文字提示。
280
+ */
281
+ #noteChoice(pending, message) {
282
+ const cardMessageId = pending.cardMessageId;
283
+ const question = pending.questions[0];
284
+ if (cardMessageId !== undefined && this.#options.patchCard !== undefined && question !== undefined) {
285
+ this.#options.logger?.info?.('卡片作答不合规,已重发卡片', message);
286
+ try {
287
+ const card = renderReminderCard({ reminderId: pending.id, no: pending.no, question, note: message });
288
+ void this.#options.patchCard(cardMessageId, card).catch(() => { });
289
+ return;
290
+ }
291
+ catch {
292
+ // 卡片装不下就退回文字:提示不能因为体积发不出去。
293
+ }
294
+ }
277
295
  this.#options.logger?.info?.('卡片作答不合规,已提示主人', message);
278
296
  void this.#options.send(message).catch(() => { });
279
297
  }
@@ -34,6 +34,9 @@ export function renderReminderCard(input) {
34
34
  const instruction = question.multiSelect === true
35
35
  ? '多选:可以勾选多项,选好后点「提交」。'
36
36
  : '单选:只能勾选一项,选好后点「提交」。';
37
+ if (input.note !== undefined && input.note !== '') {
38
+ elements.push({ tag: 'markdown', content: `_**${input.note}**_`, text_size: 'notation' });
39
+ }
37
40
  elements.push({ tag: 'markdown', content: instruction, text_size: 'notation' }, {
38
41
  tag: 'form',
39
42
  name: 'ask_form',
@@ -5,7 +5,7 @@
5
5
  * 发布时三处同步——package.json、锁文件(`npm version --no-git-tag-version`)、
6
6
  * 这里。
7
7
  */
8
- export const PLUGIN_VERSION = '0.1.0';
8
+ export const PLUGIN_VERSION = '0.1.1';
9
9
  /** 传输层实现版本:协议客户端行为变化时递增,与插件版本解耦。 */
10
10
  export const CHANNEL_VERSION = '1';
11
11
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-im-lark",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "description": "把飞书(Lark)接入 DeepSeek Harness 的 IM 桥插件",
6
6
  "license": "MIT",