dsh-feishu-lark-notify 0.27.0 → 0.28.0

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
@@ -197,6 +197,23 @@ is not stolen by `日报`; order is exact → startsWith → includes.
197
197
  - Several matches → the candidates are listed, you refine it; **it never guesses**
198
198
  - Don't want to remember titles → `@最近 内容` (also `@上一个` / `@上次` / `@last`) goes to whatever received last
199
199
 
200
+ ### Sticky target: stop repeating the number
201
+
202
+ Once you have named a target with @title or a number, **plain messages keep going there**:
203
+
204
+ `
205
+ @前端日报 帮我跑下构建 → remembers 前端日报
206
+ 再跑一下测试 → still 前端日报
207
+ 2 换个会话做事 → switches to #2 and sticks to it
208
+ 放开 → releases it
209
+ `
210
+
211
+ | Detail | Notes |
212
+ |---|---|
213
+ | Basis | The session that last received a command (lastRoutedSessionId) |
214
+ | Expiry | If it is closed or reclaimed the route falls back automatically |
215
+ | Disable | inbound.stickyTarget: false (env DSH_NOTIFY_INBOUND_STICKY=false) |
216
+
200
217
  ### Control: stop and status
201
218
 
202
219
  Feishu is not only an input channel — it can intervene:
@@ -261,7 +278,7 @@ The bot replies with **non-archived** sessions, running ones first:
261
278
 
262
279
  | Case | Behaviour |
263
280
  |---|---|
264
- | No number | Goes to the session chosen on the page, else the most recent one |
281
+ | No number | Goes to **the session you last targeted explicitly** (sticky); with no history it falls back to the page selection / most recent root |
265
282
  | Number points at a **dormant** session | Woken with `agents.resume` (plus the model that session last used), then delivered |
266
283
  | Wake-up fails | The host's original error is echoed back — nothing is dropped silently |
267
284
  | More than 10 minutes since `列表` | Numbers expire; send `列表` again. They are pinned to the order you received, so re-listing beats misdelivering |
package/README.zh.md CHANGED
@@ -193,6 +193,23 @@ DSH 里同时开着一堆会话时,飞书里发一句话默认只交给「当
193
193
  - 不想记标题 → `@最近 内容`(也认 `@上一个` / `@上次` / `@last`)交给**上次那个**会话
194
194
  - 只 `@` 了标题没写内容 → 提示补上内容
195
195
 
196
+ ### 粘性目标:不用每次都带编号
197
+
198
+ 一旦你用 @标题 或编号指定过目标,**之后不带任何前缀的普通消息继续发给它**:
199
+
200
+ `
201
+ @前端日报 帮我跑下构建 → 记住「前端日报」
202
+ 再跑一下测试 → 还是「前端日报」,不用再写一遍
203
+ 2 换个会话做事 → 切到编号 2,并粘住它
204
+ 放开 → 解除粘性,回到「配置页选定 / 最近根会话」
205
+ `
206
+
207
+ | 说明 | 细节 |
208
+ |---|---|
209
+ | 依据 | 上次真的投递成功的会话(lastRoutedSessionId) |
210
+ | 失效 | 那个会话被关掉/回收后自动退回,不会投进死目标 |
211
+ | 关闭 | inbound.stickyTarget: false(或环境变量 DSH_NOTIFY_INBOUND_STICKY=false) |
212
+
196
213
  ### 控制:停止 / 状态
197
214
 
198
215
  飞书不只是能「下指令」,也能**插手**:
@@ -293,7 +310,7 @@ DSH 里同时开着一堆会话时,飞书里发一句话默认只交给「当
293
310
 
294
311
  | 情况 | 行为 |
295
312
  |---|---|
296
- | 不带编号 | 交给配置页选定的默认会话;没选就交给最近一个 |
313
+ | 不带编号 | 交给**上次你显式指定的会话**(粘性目标);没有历史时退回配置页选定的 / 最近的根会话 |
297
314
  | 编号指向**休眠**会话 | 自动唤醒(`agents.resume` + 该会话上次用的模型),唤醒后照常投递 |
298
315
  | 唤醒失败 | 回执里带上**宿主的原始错误**,不会静默丢消息 |
299
316
  | 距上次「列表」超过 10 分钟 | 编号失效,提示重新发「列表」——编号绑定的是发清单那一刻的顺序,过期重发比投错安全 |
@@ -143,6 +143,7 @@
143
143
  | `commandPrefix` | `#dsh` | 指令前缀,默认 `#dsh` | `DSH_NOTIFY_INBOUND_PREFIX` |
144
144
  | `maxPerMinute` | `6` | 每个会话独立的 60 秒窗口;`0` = 不限制,最大 600 | `DSH_NOTIFY_INBOUND_MAX_PER_MINUTE` |
145
145
  | `sessionId` | `''` | 指令交给哪个会话;空 = 最近一个根会话 | `DSH_NOTIFY_INBOUND_SESSION_ID` |
146
+ | `stickyTarget` | `true` | 普通消息发给「上次显式指定的会话」(`@标题` / 编号 / 新建);关掉则退回「最近的根会话」 | `DSH_NOTIFY_INBOUND_STICKY` |
146
147
  | `requireMentionInGroups` | `true` | 群里要 @ 机器人才响应(私聊永远不需要);关掉则群里任何消息都处理 | `DSH_NOTIFY_INBOUND_REQUIRE_MENTION` |
147
148
 
148
149
  ## 在飞书里直接批准(`interactive`)
@@ -250,6 +251,7 @@ content:
250
251
  | `DSH_NOTIFY_INBOUND_MAX_PER_MINUTE` | `inbound.maxPerMinute` | 数字 |
251
252
  | `DSH_NOTIFY_INBOUND_SESSION_ID` | `inbound.sessionId` | string |
252
253
  | `DSH_NOTIFY_INBOUND_REQUIRE_MENTION` | `inbound.requireMentionInGroups` | bool |
254
+ | `DSH_NOTIFY_INBOUND_STICKY` | `inbound.stickyTarget` | bool |
253
255
  | `DSH_NOTIFY_INTERACTIVE_ENABLED` | `interactive.enabled` | bool |
254
256
  | `DSH_NOTIFY_INTERACTIVE_TIMEOUT_MS` | `interactive.timeoutMs` | 数字 |
255
257
  | `DSH_NOTIFY_INTERACTIVE_ALLOW_USERS` | `interactive.allowFromUsers` | 列表 |
package/lib/config.js CHANGED
@@ -122,6 +122,8 @@ export const DEFAULTS = {
122
122
  commandPrefix: '#dsh',
123
123
  maxPerMinute: 6,
124
124
  sessionId: '',
125
+ // 普通消息发给「上次显式指定的会话」;关掉则退回「最近的根会话」
126
+ stickyTarget: true,
125
127
  // 群里要 @ 机器人才响应(私聊不需要)
126
128
  requireMentionInGroups: true,
127
129
  },
@@ -278,6 +280,7 @@ export function resolveInbound(raw) {
278
280
  sessionId: typeof source.sessionId === 'string' ? source.sessionId.trim() : '',
279
281
  // 群里必须 @ 机器人才响应(私聊永远不需要);默认开,避免在群里抢话
280
282
  requireMentionInGroups: source.requireMentionInGroups !== false,
283
+ stickyTarget: source.stickyTarget !== false,
281
284
  }
282
285
  }
283
286
 
package/lib/env.js CHANGED
@@ -89,6 +89,8 @@ export const ENV_BINDINGS = [
89
89
  { name: 'DSH_NOTIFY_INBOUND_SESSION_ID', inbound: 'sessionId', kind: 'string' },
90
90
  // 群里必须 @ 机器人才响应;私聊不受影响
91
91
  { name: 'DSH_NOTIFY_INBOUND_REQUIRE_MENTION', inbound: 'requireMentionInGroups', kind: 'bool' },
92
+ // 普通消息发给「上次显式指定的会话」;false 则退回「最近的根会话」
93
+ { name: 'DSH_NOTIFY_INBOUND_STICKY', inbound: 'stickyTarget', kind: 'bool' },
92
94
 
93
95
  // 「在飞书里直接批准」:把工具审批发成带按钮的卡片
94
96
  { name: 'DSH_NOTIFY_INTERACTIVE_ENABLED', interactive: 'enabled', kind: 'bool' },
package/lib/index.js CHANGED
@@ -37,7 +37,7 @@ import { startWsInbound } from './inbound-ws.js'
37
37
  export const name = 'feishu-notify'
38
38
 
39
39
  /** 版本号(与 package.json 保持一致,单测会校验)。 */
40
- export const VERSION = '0.27.0'
40
+ export const VERSION = '0.28.0'
41
41
 
42
42
  /** 内存日志上限。 */
43
43
  const LOG_LIMIT = 200
@@ -1508,11 +1508,35 @@ export function apply(ctx, rawConfig) {
1508
1508
  //#endregion
1509
1509
 
1510
1510
  /** 用已配置的 webhook 渠道回一句(可选,失败不影响主流程)。 */
1511
- async function acknowledge(text) {
1511
+ /**
1512
+ * 给用户回一句「收到了」。
1513
+ *
1514
+ * 之前的实现只在 **webhook 模式**下发回执(`webhooks.length === 0` 直接 return),
1515
+ * 于是用应用机器人的场景(绝大多数人)**一条回执都没有**——用户以为消息没被收到。
1516
+ * 现在应用机器人走 `sendRaw` 发文本,webhook 模式保持原样。
1517
+ * @param text - 回执正文。
1518
+ * @param chatId - 发到哪个会话;省略则用第一个推送目标。
1519
+ */
1520
+ async function acknowledge(text, chatId) {
1512
1521
  const channel = config.channels.feishu
1513
- if (channel?.enabled !== true || channel.webhooks.length === 0) return
1514
- const summary = { title: '📥 DSH 收到指令', head: [text], body: '', footer: '', color: 'blue', plain: text }
1522
+ if (channel?.enabled !== true) return
1523
+ const target = typeof chatId === 'string' && chatId !== ''
1524
+ ? chatId
1525
+ : (Array.isArray(channel.receiveChatIds) && channel.receiveChatIds.length > 0 ? channel.receiveChatIds[0] : '')
1515
1526
  try {
1527
+ if (channel.useAppBot === true && channel.appId !== '' && channel.appSecret !== '') {
1528
+ const results = await feishuChannel.sendRaw(
1529
+ channel,
1530
+ { msg_type: 'text', content: { text } },
1531
+ { chatIds: target === '' ? channel.receiveChatIds : [target], timeoutMs: config.timeoutMs },
1532
+ )
1533
+ if (!results.some((item) => item.ok)) {
1534
+ log(`回执发送失败: ${results.map((item) => item.error).filter(Boolean).join('; ')}`)
1535
+ }
1536
+ return
1537
+ }
1538
+ if (!Array.isArray(channel.webhooks) || channel.webhooks.length === 0) return
1539
+ const summary = { title: '📥 DSH 收到指令', head: [text], body: '', footer: '', color: 'blue', plain: text }
1516
1540
  await feishuChannel.deliver(channel, summary, { style: 'text', timeoutMs: config.timeoutMs })
1517
1541
  } catch (error) {
1518
1542
  log(`回执发送失败: ${error?.message ?? error}`)
@@ -1633,7 +1657,14 @@ export function apply(ctx, rawConfig) {
1633
1657
  function resolveTargetAgent() {
1634
1658
  const agents = ctx.get('agents')
1635
1659
  if (agents === undefined) return undefined
1660
+ // ① 粘性:上次显式指定的会话(@标题 / 编号 / 新建)还活着,就用它
1661
+ if (config.inbound.stickyTarget !== false && lastRoutedSessionId !== '') {
1662
+ const sticky = typeof agents.get === 'function' ? agents.get(lastRoutedSessionId) : undefined
1663
+ if (sticky !== undefined && sticky !== null) return sticky
1664
+ }
1665
+ // ② 配置页固定
1636
1666
  if (config.inbound.sessionId !== '') return agents.get(config.inbound.sessionId)
1667
+ // ③ 最近的根会话(原行为)
1637
1668
  const roots = typeof agents.roots === 'function' ? agents.roots() : []
1638
1669
  return roots[roots.length - 1]
1639
1670
  }
@@ -1662,6 +1693,7 @@ export function apply(ctx, rawConfig) {
1662
1693
  lastRoutedSessionId = agent.id
1663
1694
  lastRoutedAt = Date.now()
1664
1695
  log(`${meta.via ?? 'inbound'}:已提交指令到会话 ${agent.id}(来自 ${meta.chatId}/${meta.userId})`)
1696
+ return { id: agent.id, label: sessionLabel(agent) }
1665
1697
  }
1666
1698
 
1667
1699
  /** 机器人自己的身份(群里判断有没有 @ 我),取一次就缓存。 */
@@ -2079,6 +2111,15 @@ export function apply(ctx, rawConfig) {
2079
2111
  * @returns 结构化结果。
2080
2112
  */
2081
2113
  async function applyControl({ control, target, message, via }) {
2114
+ if (control.kind === 'release') {
2115
+ const previous = lastRoutedSessionId
2116
+ lastRoutedSessionId = ''
2117
+ log(`[${via}] 控制:已解除粘性目标(原 ${previous || '无'})`)
2118
+ void replyToChat(message.chatId, previous === ''
2119
+ ? '本来就没有固定目标。'
2120
+ : '🔓 已解除,之后普通消息回到「配置页选定 / 最近的根会话」。')
2121
+ return { ok: true, control: 'release' }
2122
+ }
2082
2123
  if (control.kind === 'status') {
2083
2124
  const text = formatStatus(rosterSessions(), {
2084
2125
  currentId: config.inbound.sessionId !== '' ? config.inbound.sessionId : lastRoutedSessionId,
@@ -2337,8 +2378,9 @@ export function apply(ctx, rawConfig) {
2337
2378
  }
2338
2379
 
2339
2380
  try {
2340
- await enqueue(command, { chatId: message.chatId, userId: message.userId, via })
2341
- void acknowledge(`✅ 已提交:${clip(command, 600)}`, message.chatId)
2381
+ const delivered = await enqueue(command, { chatId: message.chatId, userId: message.userId, via })
2382
+ const targetName = delivered?.label || (delivered?.id ? delivered.id.slice(-6) : '会话')
2383
+ void acknowledge(`✅ 已收到,交给「${targetName}」:${clip(command, 300)}`, message.chatId)
2342
2384
  return { ok: true, accepted: true }
2343
2385
  } catch (error) {
2344
2386
  log(`[${via}] 提交失败 ${error?.message ?? error}`)
@@ -29,6 +29,9 @@ const STOP_COMMANDS = new Set(['停止', '停', '中断', '打住', 'stop', 'can
29
29
  /** 控制命令:看状态。 */
30
30
  const STATUS_COMMANDS = new Set(['状态', '进度', '在干嘛', 'status', 'state'])
31
31
 
32
+ /** 控制命令:解除粘性目标。 */
33
+ const RELEASE_COMMANDS = new Set(['放开', '取消默认', '不粘了', 'release'])
34
+
32
35
  /** 新建会话的命令前缀词。 */
33
36
  const CREATE_WORDS = ['新建', '新会话', '开一个', '开个', 'new', 'create']
34
37
 
@@ -108,6 +111,7 @@ export function parseControl(text) {
108
111
  if (value === '') return undefined
109
112
  if (STOP_COMMANDS.has(value)) return { kind: 'stop' }
110
113
  if (STATUS_COMMANDS.has(value)) return { kind: 'status' }
114
+ if (RELEASE_COMMANDS.has(value)) return { kind: 'release' }
111
115
  return undefined
112
116
  }
113
117
 
@@ -256,6 +260,7 @@ export function formatHelp({ maxPerMinute, prefix } = {}) {
256
260
  '· 发「2 帮我跑测试」→ 只交给第 2 个会话(编号 10 分钟内有效)',
257
261
  '· 发「新建 前端日报」→ 开一个新会话;「新建 标题 内容」→ 建完直接投第一条指令',
258
262
  '· 发「新建@灵畿 标题 内容」→ 建在「灵畿」那个工作区的目录里',
263
+ '· 不带编号 → 继续发给「上次你指定的会话」;发「放开」解除',
259
264
  '· 发「状态」→ 看各会话在跑还是空闲;「停止」→ 中断当前这一轮(也能 @标题 停止)',
260
265
  '',
261
266
  '【审批】',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-feishu-lark-notify",
3
- "version": "0.27.0",
3
+ "version": "0.28.0",
4
4
  "description": "把 DSH 的任务完成 / 需要你处理的通知推到飞书(Lark):在飞书里点审批、答选择题、按标题把指令交给指定会话,也能收发图片和文件。自带可视化配置页。",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -61,6 +61,7 @@ const INBOUND_NOTES = {
61
61
  maxPerMinute: '每个会话独立的 60 秒窗口;`0` = 不限制,最大 600',
62
62
  sessionId: '指令交给哪个会话;空 = 最近一个根会话',
63
63
  requireMentionInGroups: '群里要 @ 机器人才响应(私聊永远不需要);关掉则群里任何消息都处理',
64
+ stickyTarget: '普通消息发给「上次显式指定的会话」(`@标题` / 编号 / 新建);关掉则退回「最近的根会话」',
64
65
  }
65
66
 
66
67
  /** 「在飞书里直接批准」字段的中文说明。 */