@dcrays/dcgchat-test 0.4.16 → 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 +1 -1
- package/src/bot.ts +5 -24
- package/src/utils/gatewayMsgHanlder.ts +6 -2
- package/src/utils/global.ts +4 -3
package/package.json
CHANGED
package/src/bot.ts
CHANGED
|
@@ -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
|
|
341
|
-
|
|
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(`
|
|
350
|
-
}
|
|
351
|
-
for (const sk of keysToAbort) {
|
|
352
|
-
await abortOneSession(sk)
|
|
334
|
+
dcgLogger(`chat.abort ${dcgSessionKey}: ${String(e)}`, 'error')
|
|
353
335
|
}
|
|
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
|
-
|
|
341
|
+
sendText('会话已终止', outboundCtx)
|
|
361
342
|
sendFinal(outboundCtx, 'stop')
|
|
362
343
|
return
|
|
363
344
|
} else {
|
|
@@ -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)}`)
|
package/src/utils/global.ts
CHANGED
|
@@ -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
|
-
|
|
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(
|
|
122
|
+
q.push(mid)
|
|
122
123
|
}
|
|
123
124
|
|
|
124
125
|
/** 窥视队首 messageId(不移除),供发送中与 finished 配对使用 */
|