@dcrays/dcgchat-test 0.4.15 → 0.4.17

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dcrays/dcgchat-test",
3
- "version": "0.4.15",
3
+ "version": "0.4.17",
4
4
  "type": "module",
5
5
  "description": "OpenClaw channel plugin for 书灵墨宝 (WebSocket)",
6
6
  "main": "index.ts",
package/src/bot.ts CHANGED
@@ -143,7 +143,6 @@ const typingCallbacks = createTypingCallbacks({
143
143
  * 处理一条用户消息,调用 Agent 并返回回复
144
144
  */
145
145
  export async function handleDcgchatMessage(msg: InboundMessage, accountId: string): Promise<void> {
146
- let completeText = ''
147
146
  const config = getOpenClawConfig()
148
147
  if (!config) {
149
148
  dcgLogger('no config available', 'error')
@@ -251,7 +250,8 @@ export async function handleDcgchatMessage(msg: InboundMessage, accountId: strin
251
250
 
252
251
  const sentMediaKeys = new Set<string>()
253
252
  const getMediaKey = (url: string) => url.split(/[\\/]/).pop() ?? url
254
- let streamedTextLen = 0
253
+ /** Feishu snapshot 模式一致:payload.text 为当前轮助手全文快照,据此算增量,避免工具前后快照变短或非单调时丢字 */
254
+ let lastStreamSnapshot = ''
255
255
 
256
256
  if (msg.content.skills_scope.length > 0 && !msg.content?.agent_clone_code && !ignoreToolCommand.includes(text?.trim())) {
257
257
  const workspaceDir = getWorkspaceDir()
@@ -326,38 +326,19 @@ export async function handleDcgchatMessage(msg: InboundMessage, accountId: strin
326
326
  dcgLogger(`interrupt command: ${text}`)
327
327
  const ctxForAbort = priorOutboundCtx.messageId?.trim() || priorOutboundCtx.sessionId?.trim() ? priorOutboundCtx : outboundCtx
328
328
  sendFinal(ctxForAbort.messageId?.trim() ? ctxForAbort : { ...ctxForAbort, messageId: `${Date.now()}` }, 'abort')
329
- sendText('会话已终止', outboundCtx)
330
329
  sessionStreamSuppressed.add(dcgSessionKey)
331
- const abortOneSession = async (sessionKey: string) => {
332
- try {
333
- await sendGatewayRpc({ method: 'chat.abort', params: { sessionKey } })
334
- } catch (e) {
335
- dcgLogger(`chat.abort ${sessionKey}: ${String(e)}`, 'error')
336
- }
337
- }
338
- const keysToAbort = new Set<string>(getChildSessionKeysTrackedForRequester(dcgSessionKey))
339
330
  try {
340
- const listed = await sendGatewayRpc<{ sessions?: Array<{ key?: string }> }>({
341
- method: 'sessions.list',
342
- params: { spawnedBy: dcgSessionKey, limit: 256 }
343
- })
344
- for (const s of listed?.sessions ?? []) {
345
- const k = typeof s?.key === 'string' ? s.key.trim() : ''
346
- if (k) keysToAbort.add(k)
347
- }
331
+ const runId = activeRunIdBySessionKey.get(dcgSessionKey)
332
+ await sendGatewayRpc({ method: 'chat.abort', params: { sessionKey: dcgSessionKey, runId } })
348
333
  } catch (e) {
349
- dcgLogger(`sessions.list spawnedBy: ${String(e)}`, 'error')
334
+ dcgLogger(`chat.abort ${dcgSessionKey}: ${String(e)}`, 'error')
350
335
  }
351
- for (const sk of keysToAbort) {
352
- await abortOneSession(sk)
353
- }
354
- await abortOneSession(dcgSessionKey)
355
336
  streamChunkIdxBySessionKey.delete(dcgSessionKey)
337
+ clearSentMediaKeys(msg.content.message_id)
356
338
  resetSubagentStateForRequesterSession(dcgSessionKey)
357
339
  setMsgStatus(dcgSessionKey, 'finished')
358
- clearSentMediaKeys(msg.content.message_id)
359
340
  clearParamsMessage(dcgSessionKey)
360
- clearParamsMessage(userId)
341
+ sendText('会话已终止', outboundCtx)
361
342
  sendFinal(outboundCtx, 'stop')
362
343
  return
363
344
  } else {
@@ -394,22 +375,27 @@ export async function handleDcgchatMessage(msg: InboundMessage, accountId: strin
394
375
  onPartialReply: async (payload: ReplyPayload) => {
395
376
  if (sessionStreamSuppressed.has(dcgSessionKey)) return
396
377
 
397
- if (payload.text) {
398
- completeText = payload.text
399
- }
400
378
  // --- Streaming text chunks ---
401
379
  if (payload.text) {
402
- const delta = payload.text.startsWith(completeText.slice(0, streamedTextLen))
403
- ? payload.text.slice(streamedTextLen)
404
- : payload.text
380
+ const t = payload.text
381
+ let delta = ''
382
+ if (t.startsWith(lastStreamSnapshot)) {
383
+ delta = t.slice(lastStreamSnapshot.length)
384
+ lastStreamSnapshot = t
385
+ } else if (lastStreamSnapshot.startsWith(t)) {
386
+ // 快照缩短(模型修订等):不重复下发
387
+ } else {
388
+ // 与上一轮快照不衔接(常见于工具后快照从新的助手片段重新开始):整段下发
389
+ delta = t
390
+ lastStreamSnapshot = t
391
+ }
405
392
  if (delta.trim()) {
406
393
  const prev = streamChunkIdxBySessionKey.get(dcgSessionKey) ?? 0
407
394
  streamChunkIdxBySessionKey.set(dcgSessionKey, prev + 1)
408
395
  sendChunk(delta, outboundCtx, prev)
409
396
  }
410
- streamedTextLen = payload.text.length
411
397
  } else {
412
- dcgLogger(`onPartialReply no text: ${JSON.stringify(payload)}`, 'error')
398
+ dcgLogger(`onPartialReply no text (media/tool metadata): ${JSON.stringify(payload)}`)
413
399
  }
414
400
  // --- Media from payload ---
415
401
  const mediaList = resolveReplyMediaList(payload)
package/src/tool.ts CHANGED
@@ -51,8 +51,30 @@ const eventList = [
51
51
  { event: 'after_tool_call', message: '' }
52
52
  ]
53
53
 
54
+ /** 子 agent 的 sessionKey 往往未写入 params map,回落到主会话 outbound 参数避免 messageId 缺失 */
55
+ function resolveOutboundParamsForSession(sk: string) {
56
+ const k = sk.trim()
57
+ let params = getEffectiveMsgParams(k)
58
+ if (params.messageId?.trim() || params.sessionId?.trim()) return params
59
+ const parent = requesterByChildSessionKey.get(k)
60
+ if (parent) {
61
+ const parentParams = getEffectiveMsgParams(parent)
62
+ if (parentParams.messageId?.trim() || parentParams.sessionId?.trim()) return parentParams
63
+ }
64
+ return params
65
+ }
66
+
67
+ /** 主会话已 running 时,子会话上的工具/事件也应下发(否则子 key 无 running 状态会整段丢消息) */
68
+ function isSessionActiveForTool(sk: string): boolean {
69
+ const k = sk.trim()
70
+ if (!k) return false
71
+ if (getMsgStatus(k) === 'running') return true
72
+ const parent = requesterByChildSessionKey.get(k)
73
+ return parent ? getMsgStatus(parent) === 'running' : false
74
+ }
75
+
54
76
  function sendToolCallMessage(sk: string, text: string, toolCallId: string, isCover: number) {
55
- const params = getEffectiveMsgParams(sk)
77
+ const params = resolveOutboundParamsForSession(sk)
56
78
  const content = { is_finish: -1, tool_call_id: toolCallId, is_cover: isCover, thinking_content: text, response: '' }
57
79
  wsSendRaw(params, content, false)
58
80
  }
@@ -306,14 +328,27 @@ export function monitoringToolMessage(api: OpenClawPluginApi) {
306
328
  trackSubagentLifecycle(item.event, event, args)
307
329
  const sk = resolveHookSessionKey(item.event, args ?? {})
308
330
  if (sk) {
309
- const status = getMsgStatus(sk)
310
- if (status === 'running') {
331
+ if (isSessionActiveForTool(sk)) {
311
332
  if (['after_tool_call', 'before_tool_call'].includes(item.event)) {
312
333
  const { result: _result, ...rest } = event
313
334
  dcgLogger(`工具调用结果: ~ event:${item.event} ~ params:${JSON.stringify(rest)}`)
314
335
 
315
336
  if (item.event === 'before_tool_call') {
316
- return cronToolCall(rest, sk)
337
+ const hookResult = cronToolCall(rest, sk)
338
+ const text = JSON.stringify({
339
+ type: item.event,
340
+ specialIdentification: 'dcgchat_tool_call_special_identification',
341
+ callId: event.toolCallId || event.runId || Date.now().toString(),
342
+ ...rest,
343
+ status: 'running'
344
+ })
345
+ sendToolCallMessage(
346
+ sk,
347
+ text,
348
+ event.toolCallId || event.runId || Date.now().toString(),
349
+ 0
350
+ )
351
+ return hookResult
317
352
  }
318
353
  const text = JSON.stringify({
319
354
  type: item.event,
@@ -329,7 +364,7 @@ export function monitoringToolMessage(api: OpenClawPluginApi) {
329
364
  item.event === 'after_tool_call' ? 1 : 0
330
365
  )
331
366
  } else if (item.event) {
332
- const msgCtx = getEffectiveMsgParams(sk)
367
+ const msgCtx = resolveOutboundParamsForSession(sk)
333
368
  if (item.event === 'llm_output') {
334
369
  if (event.lastAssistant?.errorMessage === '1003-额度不足请充值') {
335
370
  const message = '您的积分已消耗完,您可以通过充值积分来继续使用'
@@ -346,7 +381,7 @@ export function monitoringToolMessage(api: OpenClawPluginApi) {
346
381
  params: item.message
347
382
  })
348
383
  sendToolCallMessage(sk, text, event.runId || Date.now().toString(), 0)
349
- dcgLogger(`工具调用结果: ~ event:${item.event} ${status}`)
384
+ dcgLogger(`工具调用结果: ~ event:${item.event}`)
350
385
  }
351
386
  }
352
387
  } else if (item.event !== 'before_tool_call') {
@@ -1,14 +1,17 @@
1
1
  import type { GatewayEvent } from '../gateway/index.js'
2
2
  import { finishedDcgchatCron, sendDcgchatCron } from '../cron.js'
3
3
  import { dcgLogger } from './log.js'
4
- import { getEffectiveMsgParams, getSessionKeyBySubAgentRunId } from './params.js'
5
- import { sendChunk } from '../transport.js'
4
+ import { clearParamsMessage, getEffectiveMsgParams, getSessionKeyBySubAgentRunId } from './params.js'
5
+ import { sendChunk, sendFinal, sendText } from '../transport.js'
6
+ import { resetSubagentStateForRequesterSession } from '../tool.js'
7
+ import { setMsgStatus } from './global.js'
6
8
 
7
9
  /**
8
10
  * 处理网关 event 帧的副作用(agent 流式输出、cron 同步),并构造供上层分发的 GatewayEvent。
9
11
  */
10
12
  export function handleGatewayEventMessage(msg: { event?: string; payload?: Record<string, unknown>; seq?: number }): GatewayEvent {
11
13
  try {
14
+ // 子agent消息输出
12
15
  if (msg.event === 'agent') {
13
16
  const pl = msg.payload as { runId: string; data?: { delta?: unknown } }
14
17
  const sessionKey = getSessionKeyBySubAgentRunId(pl.runId)
@@ -19,6 +22,7 @@ export function handleGatewayEventMessage(msg: { event?: string; payload?: Recor
19
22
  }
20
23
  }
21
24
  }
25
+ // 定时任务
22
26
  if (msg.event === 'cron') {
23
27
  const p = msg.payload
24
28
  dcgLogger(`[Gateway] 收到定时任务事件: ${JSON.stringify(p)}`)
@@ -111,14 +111,15 @@ export function clearSentMediaKeys(messageId?: string) {
111
111
  /** 每个 sessionKey 下多个定时任务 messageId,入队在尾、出队在头(FIFO) */
112
112
  const cronMessageIdMap = new Map<string, string[]>()
113
113
 
114
- export function setCronMessageId(sk: string, messageId: string) {
115
- if (!sk?.trim() || !messageId?.trim()) return
114
+ export function setCronMessageId(sk: string, messageId: string | number | null | undefined) {
115
+ const mid = messageId != null && messageId !== '' ? String(messageId).trim() : ''
116
+ if (!sk?.trim() || !mid) return
116
117
  let q = cronMessageIdMap.get(sk)
117
118
  if (!q) {
118
119
  q = []
119
120
  cronMessageIdMap.set(sk, q)
120
121
  }
121
- q.push(messageId)
122
+ q.push(mid)
122
123
  }
123
124
 
124
125
  /** 窥视队首 messageId(不移除),供发送中与 finished 配对使用 */