dsh-channel-feishu 0.9.6 → 0.9.8

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
@@ -5,6 +5,24 @@
5
5
 
6
6
  用的是你自己在飞书开放平台新建的自建应用(独立机器人),不和别的通道共用事件。
7
7
 
8
+ ## 为什么这么设计
9
+
10
+ 常见的 IM 桥接是「群机器人 + webhook」:消息先转发到某个中间服务,再把回复贴回群里。这条通道反着做,几个取舍是有意的:
11
+
12
+ - **一个应用只服务一个人**:自建应用 + 独立机器人,事件走长连接直达你本机,不经第三方中继,飞书侧也只是一条普通私聊;
13
+ - **会话是主体,聊天窗口只是入口**:一条通道绑定一个 DSH 会话,上下文、工具调用、产物都留在本地会话里,换设备、重开 app 都不丢;
14
+ - **要你拍板时发卡片,不靠解析自然语言**:结构化问答卡点选即回传,避免「回复 1 表示同意」这类靠猜的交互;
15
+ - **附件落在工作区**:图片和文件直接进 `<workspacePath>/attachments/`,Agent 用本地路径读取,不用去临时目录里翻;
16
+ - **无人值守优先**:会话激活即切权限预设,长任务不会卡在授权确认上;问答卡的回调也在通道内闭环,不需要开网页。
17
+
18
+ 代价是它只适合「一个人、一台机器、一个长期会话」的用法——这正是下面这条边界的由来。
19
+
20
+ ## 当前版本不支持群聊
21
+
22
+ 0.9.x 只支持**单聊**(你和机器人一对一)。把机器人拉进群不会工作:群消息的会话绑定关系不成立,插件按单聊逻辑处理会直接丢弃。
23
+
24
+ 群聊要解决的是另一个问题——群里谁在跟 Agent 说话、上下文怎么隔离、多人同时提问怎么排队。这些等实际反馈,不在当前版本里做。
25
+
8
26
  ## 它提供什么
9
27
 
10
28
  - **双向通道**:飞书私聊 → DSH 会话;一轮结束把答复自动发回原聊天,不需要模型调工具
@@ -25,7 +43,7 @@ dsh plugin add dsh-channel-feishu
25
43
 
26
44
  ```bash
27
45
  dsh plugin add https://github.com/Poy-Coder/dsh-channel-feishu
28
- dsh plugin add https://github.com/Poy-Coder/dsh-channel-feishu/releases/download/v0.9.6/dsh-channel-feishu-0.9.6.tgz
46
+ dsh plugin add https://github.com/Poy-Coder/dsh-channel-feishu/releases/download/v0.9.8/dsh-channel-feishu-0.9.8.tgz
29
47
  ```
30
48
 
31
49
  依赖必须用扁平结构装(非 ASCII 目录下 pnpm 的软链会把路径写坏):`pnpm install --node-linker=hoisted`。
@@ -146,6 +164,12 @@ owner 记在通道状态文件里,删掉状态文件里的 `owner` 字段即
146
164
  - 常见 `result`:`delivered`、`bootstrap`(自动建会话)、`owner-bound`、`dropped`(附 `reason`)、
147
165
  `card-sent`、`answered`、`card-update-failed`。
148
166
 
167
+ ## 路线图
168
+
169
+ - **多 IM**:通道层已经按 provider 抽开(`providers/registry.js`),接微信/企业微信只需实现同一份 provider 契约,会话与问答卡逻辑不用重写;
170
+ - **扫码配置**:目标是「扫一下就把 `appId` / `appSecret` / `defaultChatId` 写进凭据文件」,替掉现在跑 `scripts/setup.mjs` 这一步;
171
+ - **群聊**:视反馈决定,不在 0.9.x 范围内。
172
+
149
173
  ## 常见问题
150
174
 
151
175
  | 现象 | 处理 |
package/index.js CHANGED
@@ -368,8 +368,22 @@ function replyText(record) {
368
368
  return `${joined.slice(0, MAX_REPLY_CHARS)}\n\n…(内容过长,已截断)`
369
369
  }
370
370
 
371
- function summaryOf(text) {
372
- const line = String(text ?? '').split('\n').map((value) => value.trim()).find(Boolean) ?? 'DSH'
371
+ /**
372
+ * Last-resort plain-text rendering: a text message is not markdown, so the
373
+ * fallback strips the syntax instead of showing `**bold**` and `## heading`.
374
+ */
375
+ export function plainTextFromMarkdown(text) {
376
+ return String(text ?? '')
377
+ .replace(/^#{1,6}\s+/gm, '')
378
+ .replace(/\*\*([^*]+)\*\*/g, '$1')
379
+ .replace(/__([^_]+)__/g, '$1')
380
+ .replace(/`{1,3}([^`]+)`{1,3}/g, '$1')
381
+ .replace(/^\s*[-*]\s+/gm, '· ')
382
+ .replace(/^\s*>\s?/gm, '')
383
+ .trim()
384
+ }
385
+
386
+ function summaryOf(text) { const line = String(text ?? '').split('\n').map((value) => value.trim()).find(Boolean) ?? 'DSH'
373
387
  return line.length > 40 ? `${line.slice(0, 40)}…` : line
374
388
  }
375
389
 
@@ -471,6 +485,9 @@ function scheduleCardFlush(record, ctx, config) {
471
485
 
472
486
  const isRateLimited = (error) => /230020|frequency limit|rate limit/i.test(String(error?.message ?? error))
473
487
 
488
+ /** Feishu closes a streaming card after a while; further element writes get 300309. */
489
+ const isStreamingClosed = (error) => /300309|streaming mode is closed/i.test(String(error?.message ?? error))
490
+
474
491
  async function flushCard(record, ctx, config) {
475
492
  flushRan += 1
476
493
  const text = replyText(record)
@@ -513,9 +530,22 @@ async function flushCard(record, ctx, config) {
513
530
  logInbound(config, {
514
531
  event: 'card', result: 'rate-limited', sessionId: record.sessionId, turn: record.turn, sequence: card.sequence,
515
532
  })
533
+ } else if (isStreamingClosed(error) && (record.recreations ?? 0) < 3) {
534
+ // Feishu closes a streaming card after roughly ten minutes; a long turn
535
+ // then loses its card. Start a fresh one and keep writing there instead of
536
+ // degrading to a plain-text message (which would print raw markdown).
537
+ record.recreations = (record.recreations ?? 0) + 1
538
+ record.card = undefined
539
+ record.lastCardText = ''
540
+ record.backoffUntil = Date.now() + 1_000
541
+ logInbound(config, {
542
+ event: 'card', result: 'streaming-closed', sessionId: record.sessionId, turn: record.turn,
543
+ recreation: record.recreations, reason: errorText(error),
544
+ })
545
+ scheduleCardFlush(record, ctx, config)
516
546
  } else {
517
547
  record.cardFailed = true
518
- warnTo(ctx, `card text update failed, will fall back to text: ${errorText(error)}`)
548
+ warnTo(ctx, `card text update failed: ${errorText(error)}`)
519
549
  logInbound(config, {
520
550
  event: 'card', result: 'update-failed', sessionId: record.sessionId, turn: record.turn, reason: errorText(error),
521
551
  })
@@ -698,8 +728,18 @@ export async function finalizeTurn(ctx, config, sessionId, turn, reason) {
698
728
  })
699
729
  return
700
730
  } catch (error) {
701
- warnTo(ctx, `card finalize failed, falling back to text: ${errorText(error)}`)
731
+ warnTo(ctx, `card finalize failed: ${errorText(error)}`)
702
732
  logInbound(config, { event: 'card', result: 'close-failed', sessionId, turn, reason: errorText(error) })
733
+ // A dead streaming card (Feishu closes them after ~10 minutes) must not
734
+ // degrade to a plain-text message: markdown would show up as raw source.
735
+ // Post the answer as its own fresh card instead.
736
+ const posted = await postAnswerBelow(ctx, config, chatId, record, text)
737
+ if (posted) {
738
+ logInbound(config, {
739
+ event: 'card', result: 'reposted', sessionId, turn, cardId: posted, chars: text.length,
740
+ })
741
+ return
742
+ }
703
743
  }
704
744
  }
705
745
  }
@@ -715,7 +755,7 @@ export async function finalizeTurn(ctx, config, sessionId, turn, reason) {
715
755
  return
716
756
  }
717
757
  try {
718
- await sendChannelText(config, chatId, text)
758
+ await sendChannelText(config, chatId, plainTextFromMarkdown(text))
719
759
  logInbound(config, { event: 'reply', result: 'sent', sessionId, turn, chatId, chars: text.length })
720
760
  } catch (error) {
721
761
  warnTo(ctx, `reply delivery failed: ${errorText(error)}`)
@@ -2199,6 +2239,19 @@ export async function handleInboundEvent(ctx, config, data, seen) {
2199
2239
  return
2200
2240
  }
2201
2241
 
2242
+ // This channel is one conversation per bot: a group brings several speakers
2243
+ // into one session, which the current design does not model.
2244
+ if (inbound.chatType && inbound.chatType !== 'p2p') {
2245
+ log({
2246
+ event: 'inbound', result: 'dropped',
2247
+ reason: `chat_type=${inbound.chatType} (group chat is not supported)`,
2248
+ senderId: inbound.senderId, chatId: inbound.chatId,
2249
+ })
2250
+ warnTo(ctx, `ignored ${inbound.chatType} message: group chat is not supported`)
2251
+ markSeen()
2252
+ return
2253
+ }
2254
+
2202
2255
  const senderId = inbound.senderId
2203
2256
  const allowed = Array.isArray(config.allowedUsers)
2204
2257
  ? config.allowedUsers.map((value) => String(value)).filter(Boolean)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-channel-feishu",
3
- "version": "0.9.6",
3
+ "version": "0.9.8",
4
4
  "type": "module",
5
5
  "description": "Feishu/Lark (飞书) channel for DeepSeek Harness: a chat bound to a DSH session, with streaming cards and attachments.",
6
6
  "license": "MIT",
@@ -609,6 +609,7 @@ export async function downloadMessageFile(base, token, messageId, fileKey, fileN
609
609
  return {
610
610
  dedupeKey: String(event?.header?.event_id ?? ''),
611
611
  chatId: String(message.chat_id ?? ''),
612
+ chatType: String(message.chat_type ?? ''),
612
613
  senderId: String(sender.sender_id?.open_id ?? ''),
613
614
  senderType: String(sender.sender_type ?? ''),
614
615
  messageId: String(message.message_id ?? ''),
@@ -621,6 +622,7 @@ export async function downloadMessageFile(base, token, messageId, fileKey, fileN
621
622
  return {
622
623
  dedupeKey: String(event?.header?.event_id ?? ''),
623
624
  chatId: String(message.chat_id ?? ''),
625
+ chatType: String(message.chat_type ?? ''),
624
626
  senderId: String(sender.sender_id?.open_id ?? ''),
625
627
  senderType: String(sender.sender_type ?? ''),
626
628
  messageId: String(message.message_id ?? ''),