@ququ-jiaxk/dsh-groupchat 0.1.12 → 0.1.14

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.
Files changed (2) hide show
  1. package/index.js +63 -7
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -379,12 +379,21 @@ function replyTargetOfRecentMember(state, group, message) {
379
379
  }
380
380
 
381
381
 
382
+ /** 消息在信号/inbox 里的摘要:正文 + 附件名(绝对路径),附件消息不会只剩一个文件名。 */
383
+ function summaryForMessage(message) {
384
+ const attachments = message.attachments || []
385
+ const attachmentText = attachments.length
386
+ ? '[附件] ' + attachments.map(a => `${a.name}(${a.path})`).join('、')
387
+ : ''
388
+ const body = String(message.content || '').trim()
389
+ return [body, attachmentText].filter(Boolean).join('\n') || '(空消息)'
390
+ }
391
+
382
392
  function dispatch(state, group, message) {
383
393
  const targets = new Set()
384
394
  const sAgent = sagentOf(group)
385
395
  // 只有附件、没有正文的消息也算「用户发言」,不能落进寒暄分支被静默丢弃
386
396
  const hasAttachments = !!(message.attachments && message.attachments.length)
387
- const attachmentLabel = hasAttachments ? '[附件] ' + message.attachments.map(a => a.name).join('、') : ''
388
397
  // 解析 @ 目标(用户消息和 agent 消息都要用)
389
398
  const addressed = resolveAddressedTo(group, message.addressedTo)
390
399
  const addressedMembers = addressed.filter(t => memberSessionIds(group).has(t) && t !== sAgent)
@@ -460,7 +469,7 @@ function dispatch(state, group, message) {
460
469
  // 否则 agent 会用不完整的信息决策。仅对极端超长(>4000 字)做折叠,
461
470
  // 并在折叠处明确标注剩余长度。
462
471
  summary: (() => {
463
- const base = String(message.content || '').trim() ? message.content : (attachmentLabel || '(空消息)')
472
+ const base = summaryForMessage(message)
464
473
  return base.length > 4000
465
474
  ? base.slice(0, 4000) + `\n…(后续还有 ${base.length - 4000} 字,请用 group_read_signal 读全文后再执行)`
466
475
  : base
@@ -554,6 +563,36 @@ export function apply(ctx) {
554
563
  */
555
564
  const agentStates = new Map()
556
565
 
566
+ /**
567
+ * 群上下文:sessionId → 过期时间。
568
+ * 只有被「群信号」唤醒的成员才允许向群里写(group_post / group_task_update)。
569
+ * 用户在成员自己的独立会话里直接派活时,没有群上下文——此时成员的进度不应回流到群里,
570
+ * 否则会出现「单独会话派的任务把群里冻结/看板搅乱」的内耗。
571
+ * sAgent 例外:它的职责就是群管理,且心跳/汇报唤醒不经过 wakeTargets。
572
+ */
573
+ const groupContextUntil = new Map()
574
+ const GROUP_CONTEXT_TTL = 2 * 60 * 60 * 1000
575
+
576
+ function markGroupContext(sessionId) {
577
+ if (sessionId) groupContextUntil.set(sessionId, Date.now() + GROUP_CONTEXT_TTL)
578
+ }
579
+
580
+ function hasGroupContext(sessionId) {
581
+ const until = groupContextUntil.get(sessionId)
582
+ if (!until) return false
583
+ if (until < Date.now()) { groupContextUntil.delete(sessionId); return false }
584
+ return true
585
+ }
586
+
587
+ function assertGroupWriteAllowed(group, sessionId) {
588
+ if (!sessionId || sessionId === group.sAgentSessionId) return
589
+ if (hasGroupContext(sessionId)) return
590
+ throw new Error(
591
+ '当前不是群上下文:你是在用户单独会话里被触发的,已阻止向群里发消息/改进度。'
592
+ + '请直接回复当前会话的用户;如果需要参与群协作,等群里发来信号后再动作。'
593
+ )
594
+ }
595
+
557
596
  /**
558
597
  * Agent 工作日志(activity):sessionId → [{ id, ts, kind, title, detail }]
559
598
  * 记录 agent 在做什么:开始思考、写回复、调用哪个工具、工具结果、状态切换。
@@ -687,6 +726,8 @@ export function apply(ctx) {
687
726
  // 只有真正在跑过才记一条「回到空闲」,避免刷屏
688
727
  if (cur && cur.state !== 'idle') pushActivity(agent.id, 'idle', '本轮结束,回到空闲')
689
728
  setAgentState(agent.id, 'idle')
729
+ // 群上下文随这一轮结束失效:之后用户在独立会话里再找它,不算群协作
730
+ groupContextUntil.delete(agent.id)
690
731
  }
691
732
  })
692
733
 
@@ -894,6 +935,8 @@ export function apply(ctx) {
894
935
  }
895
936
  continue
896
937
  }
938
+ // 这条唤醒来自群信号:给这个会话打上群上下文,允许它回群发言/改进度
939
+ markGroupContext(toSessionId)
897
940
  const n = unreadCount(state, toSessionId)
898
941
  // 关键:消息确实派发给了这个会话 —— 标记「已接收」。
899
942
  // 此时 agent 可能还要排队(正在跑上一个 turn),所以不是立刻 thinking。
@@ -945,6 +988,10 @@ export function apply(ctx) {
945
988
  ? '\n【不要发言】这是用户直接 @ 某位成员的消息,成员会自己答复。你不需要汇总、不需要总结、不需要确认,保持沉默即可。'
946
989
  : ''
947
990
  const latestHint = latest ? `\n最新信号:[${latest.kind}] ${latest.fromName} 说「${latest.summary}」` : ''
991
+ // 带附件的消息:摘要里已经有本地绝对路径,明确让 agent 去读,别只看到文件名就回「拿不到内容」
992
+ const attachmentHint = latest && /\[附件\]/.test(String(latest.summary || ''))
993
+ ? '\n【附件】最新消息带附件,括号里是本地绝对路径。先 group_read_signal 读正文,图片用 read_image 打开、文本用 read 打开,看过内容再回复;不要凭空猜测,也不要说「拿不到附件」。'
994
+ : ''
948
995
 
949
996
  // 寒暄场景:用户只是在道谢/确认。
950
997
  // 注意:这里必须用 + 拼接成【一个】字符串再传给 wakeAgent,
@@ -966,7 +1013,7 @@ export function apply(ctx) {
966
1013
  : `\n先调用 group_inbox 看完整未读列表,再决定是否响应;与你无关的话题保持沉默即可。`
967
1014
  wakeAgent(agents, toSessionId,
968
1015
  `[群聊] 群「${group.name}」(群id=${group.id})有 ${n} 条新信号。${latestHint}${fromUserHint}${userMentionedHint}`
969
- + actionHint + `${brief}${orderHint}`, resumer)
1016
+ + actionHint + `${attachmentHint}${brief}${orderHint}`, resumer)
970
1017
  }
971
1018
  }
972
1019
 
@@ -1033,6 +1080,7 @@ export function apply(ctx) {
1033
1080
  if (!group) throw new Error(`群 ${args.groupId} 不存在`)
1034
1081
  // UI 操作(sessionId 为 null)不校验成员身份;agent 操作校验
1035
1082
  if (sessionId && !isMember(group, sessionId)) throw new Error('你不是该群成员')
1083
+ if (sessionId) assertGroupWriteAllowed(group, sessionId)
1036
1084
  group.task = group.task || {}
1037
1085
  group.task.subtasks = Array.isArray(group.task.subtasks) ? group.task.subtasks : []
1038
1086
 
@@ -1232,6 +1280,7 @@ export function apply(ctx) {
1232
1280
  const group = state.groups.find(g => g.id === args.groupId)
1233
1281
  if (!group) throw new Error(`群 ${args.groupId} 不存在`)
1234
1282
  if (!isMember(group, sessionId)) throw new Error('你不是该群的成员,无权发言')
1283
+ assertGroupWriteAllowed(group, sessionId)
1235
1284
  const msg = postMessage(group, {
1236
1285
  authorSessionId: sessionId,
1237
1286
  authorName: nameOf(group, sessionId),
@@ -1280,6 +1329,9 @@ export function apply(ctx) {
1280
1329
  const tag = i === 0 ? '【最新】' : ''
1281
1330
  lines.push(` ${tag}[${s.kind}] ${s.fromName}: ${s.summary}`)
1282
1331
  })
1332
+ if (sorted.some(s => String(s.summary || '').includes('[附件]'))) {
1333
+ lines.push(' ([附件] 括号里是本地绝对路径:图片用 read_image 打开,文本用 read 打开;也可 group_read_signal 看完整说明)')
1334
+ }
1283
1335
  } else {
1284
1336
  lines.push('没有未读信号。')
1285
1337
  }
@@ -1311,10 +1363,14 @@ export function apply(ctx) {
1311
1363
  if (!g) throw new Error(`你不是群 ${args.groupId} 的成员`)
1312
1364
  const signals = state.signals
1313
1365
  .filter(s => s.groupId === g.id && s.toSessionId === sessionId && !s.read)
1314
- .map(s => ({
1315
- id: s.id, fromName: s.fromName, kind: s.kind,
1316
- summary: s.summary, messageId: s.messageId, ts: s.ts,
1317
- }))
1366
+ .map(s => {
1367
+ const message = state.messages.find(m => m.id === s.messageId)
1368
+ return {
1369
+ id: s.id, fromName: s.fromName, kind: s.kind,
1370
+ summary: message ? summaryForMessage(message) : s.summary,
1371
+ messageId: s.messageId, ts: s.ts,
1372
+ }
1373
+ })
1318
1374
  return {
1319
1375
  groups: [{
1320
1376
  id: g.id, name: g.name, myRole: roleOf(g, sessionId), version: typeof g.version === "number" ? g.version : 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ququ-jiaxk/dsh-groupchat",
3
- "version": "0.1.12",
3
+ "version": "0.1.14",
4
4
  "type": "module",
5
5
  "description": "DSH 群聊插件:把已有会话作为固定角色的智能体拉进项目群,由项目经理 agent(sAgent)聚合、推进、排人、汇报。",
6
6
  "exports": {