@dcrays/dcgchat 0.4.29 → 0.5.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.
@@ -1,224 +0,0 @@
1
- import fs from 'node:fs'
2
- import os from 'node:os'
3
- import path from 'node:path'
4
- import type { AnyAgentTool } from 'openclaw/plugin-sdk'
5
- import { jsonResult } from 'openclaw/plugin-sdk'
6
- import { sendDcgchatMedia } from '../channel.js'
7
- import { getOutboundMsgParams } from '../utils/params.js'
8
- import { sendText } from '../transport.js'
9
-
10
- /** 与 `registerTool` 工厂入参一致(主包未导出 `OpenClawPluginToolContext` 时仅用所需字段)。 */
11
- export type DcgchatMessageToolContext = {
12
- sessionKey?: string
13
- workspaceDir?: string
14
- }
15
-
16
- /** 统一为 POSIX 风格斜杠,便于跨平台判断(不改变语义,仅用于匹配)。 */
17
- function toPosixPath(p: string): string {
18
- return path.normalize(p.trim()).replace(/\\/g, '/')
19
- }
20
-
21
- /** `filepath` 解析后在 `rootDir` 内或等于 `rootDir`(防 `..` 逃逸)。 */
22
- function isPathInsideDir(filepath: string, rootDir: string): boolean {
23
- const root = path.resolve(rootDir)
24
- const resolved = path.resolve(filepath)
25
- const rel = path.relative(root, resolved)
26
- if (rel.startsWith('..') || path.isAbsolute(rel)) return false
27
- return true
28
- }
29
-
30
- /**
31
- * 允许发送的路径:
32
- * - 当前 Agent 工作区根及其子路径(`workspaceDir`,如 ~/.openclaw/workspace-xxx/output/...);
33
- * - 兼容旧挂载:Unix `/workspace`、`/mobook`;Windows 盘符下 `workspace`、`mobook`。
34
- */
35
- function isSafePath(filepath: string, workspaceDir?: string): boolean {
36
- const ws = workspaceDir?.trim()
37
- if (ws && isPathInsideDir(filepath, ws)) return true
38
- const p = toPosixPath(filepath)
39
- if (p.startsWith('/workspace/') || p === '/workspace') return true
40
- if (p.startsWith('/mobook/') || p === '/mobook') return true
41
- return /^[A-Za-z]:\/(workspace|mobook)(\/|$)/.test(p)
42
- }
43
-
44
- /** 同一路径在 Windows 上可能大小写不同,用于 Set 去重。 */
45
- function pathKey(filepath: string): string {
46
- const n = path.normalize(filepath.trim())
47
- return os.platform() === 'win32' ? n.toLowerCase() : n
48
- }
49
-
50
- const fileType1 = ['.webp', '.gif', '.bmp', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.pdf', '.txt', '.rtf', '.odt', '.json']
51
- const fileType2 = ['.xml', '.csv', '.yaml', '.yml', '.html', '.htm', '.md', '.markdown', '.css', '.js', '.ts', '.png', '.jpg', '.jpeg']
52
- const fileType3 = ['.zip', '.rar', '.7z', '.tar', '.gz', '.bz2', '.xz', '.exe', '.dmg', '.pkg', '.apk', '.ipa', '.log', '.dat', '.bin']
53
- const fileType4 = ['.svg', '.ico', '.mp3', '.wav', '.ogg', '.aac', '.m4a', '.mp4', '.avi', '.mov', '.wmv', '.flv', '.mkv', '.webm']
54
- const SAFE_EXTENSIONS = new Set([...fileType1, ...fileType2, ...fileType3, ...fileType4])
55
-
56
- const messageToolParameters = {
57
- type: 'object',
58
- additionalProperties: false,
59
- properties: {
60
- target: {
61
- type: 'string',
62
- description: '目标会话键(sessionKey),必须与当前会话 SessionKey 一致,禁止填写 userId。'
63
- },
64
- content: {
65
- type: 'string',
66
- description: '发送文本内容'
67
- },
68
- media: {
69
- type: 'array',
70
- description: '发送附件',
71
- items: {
72
- type: 'object',
73
- additionalProperties: false,
74
- properties: {
75
- file: {
76
- type: 'string',
77
- description:
78
- '文件绝对路径:须在「当前 Agent 工作区」目录下(如 /root/.openclaw/workspace-xxx/output/28337/slices_result.json),或为兼容环境的 /workspace/、/mobook/(Windows 盘符下 workspace、mobook)'
79
- }
80
- },
81
- required: ['file']
82
- }
83
- }
84
- },
85
- anyOf: [{ required: ['content'] }, { required: ['media'] }]
86
- }
87
-
88
- /** 从正文提取可发送的文件路径(固定挂载 + 当前工作区前缀)。 */
89
- function extractPaths(text: string | undefined, workspaceDir?: string): string[] {
90
- if (!text) return []
91
- const unix = text.match(/\/workspace\/[^\s]+|\/mobook\/[^\s]+/g) ?? []
92
- const win = text.match(/[A-Za-z]:[/\\](?:workspace|mobook)[/\\][^\s]+/g) ?? []
93
- const underWs: string[] = []
94
- const ws = workspaceDir?.trim()
95
- if (ws) {
96
- const variants = new Set<string>()
97
- variants.add(ws)
98
- variants.add(toPosixPath(ws))
99
- if (path.sep === '\\') variants.add(ws.replace(/\//g, '\\'))
100
- for (const prefix of variants) {
101
- if (!prefix) continue
102
- let from = 0
103
- while (from < text.length) {
104
- const i = text.indexOf(prefix, from)
105
- if (i === -1) break
106
- let end = i + prefix.length
107
- while (end < text.length && !/\s/.test(text[end])) end++
108
- underWs.push(text.slice(i, end))
109
- from = i + 1
110
- }
111
- }
112
- }
113
- return [...new Set([...unix, ...win, ...underWs])]
114
- }
115
-
116
- function isSafeFile(filepath: string) {
117
- if (!fs.existsSync(filepath)) return false
118
- const stat = fs.statSync(filepath)
119
- if (!stat.isFile()) return false
120
- if (stat.size === 0) return false
121
- const ext = path.extname(filepath).toLowerCase()
122
- return SAFE_EXTENSIONS.has(ext)
123
- }
124
-
125
- /**
126
- * 书灵墨宝出站消息工具:须符合 OpenClaw `AgentTool`(execute 返回 `AgentToolResult`)。
127
- * 工具名使用 `dcgchat_message`,避免与核心内置 `message` 冲突。
128
- * 通过注册时的 `OpenClawPluginToolContext.sessionKey` 出站,不再使用非标准的 `execute(args, ctx)`。
129
- */
130
- export function createDcgchatMessageTool(pluginCtx: DcgchatMessageToolContext): AnyAgentTool {
131
- return {
132
- name: 'dcgchat_message',
133
- label: 'dcgchat_message',
134
- description: `
135
- 向用户发送消息。
136
- 若传 target,target 必须是 sessionKey,不能是 userId。
137
- 如果发送附件:必须使用 media 字段
138
- 文件路径须在当前 Agent 工作区目录下(随部署变化,如 ~/.openclaw/workspace-xxx/...),或为兼容环境的 /workspace/、/mobook/(Windows 盘符下 workspace、mobook)。
139
- 禁止在正文中直接输出可访问路径(应通过 media 发送)
140
- `,
141
- parameters: messageToolParameters,
142
- execute: async (_toolCallId, args, signal) => {
143
- if (signal?.aborted) {
144
- const err = new Error('Message send aborted')
145
- err.name = 'AbortError'
146
- throw err
147
- }
148
-
149
- const sessionKey = pluginCtx.sessionKey?.trim()
150
- if (!sessionKey) {
151
- return jsonResult({ error: '缺少 sessionKey,无法向当前会话发送消息' })
152
- }
153
-
154
- try {
155
- const sentFiles = new Set<string>()
156
- const sentKeys = new Set<string>()
157
- const workspaceDir = pluginCtx.workspaceDir
158
-
159
- if (args.media?.length) {
160
- for (const media of args.media) {
161
- const filepath = media.file
162
- if (!filepath) continue
163
- if (!isSafePath(filepath, workspaceDir)) continue
164
- if (!isSafeFile(filepath)) continue
165
- const key = pathKey(filepath)
166
- if (sentKeys.has(key)) continue
167
-
168
- await sendDcgchatMedia({ sessionKey, mediaUrl: filepath })
169
- sentFiles.add(filepath)
170
- sentKeys.add(key)
171
- }
172
- }
173
-
174
- const fallbackPaths = extractPaths(args.content, workspaceDir)
175
- for (const filepath of fallbackPaths) {
176
- if (!isSafePath(filepath, workspaceDir)) continue
177
- if (!isSafeFile(filepath)) continue
178
- const key = pathKey(filepath)
179
- if (sentKeys.has(key)) continue
180
-
181
- await sendDcgchatMedia({ sessionKey, mediaUrl: filepath })
182
- sentFiles.add(filepath)
183
- sentKeys.add(key)
184
- }
185
-
186
- if (args.media?.length && sentFiles.size === 0) {
187
- return jsonResult({
188
- success: false,
189
- error:
190
- '未能发送任何附件:路径须位于当前 Agent 工作区,或为 /workspace/、/mobook/ 下的真实文件(非空、扩展名在白名单内)。',
191
- sentMediaCount: 0
192
- })
193
- }
194
-
195
- let content = args.content ?? ''
196
- for (const filepath of sentFiles) {
197
- const posix = toPosixPath(filepath)
198
- const variants = posix === filepath ? [filepath] : [filepath, posix]
199
- const seen = new Set<string>()
200
- for (const v of variants) {
201
- if (!v || seen.has(v)) continue
202
- seen.add(v)
203
- content = content.split(v).join('')
204
- }
205
- }
206
- content = content.trim()
207
-
208
- if (content.length > 0) {
209
- const msgCtx = getOutboundMsgParams(sessionKey)
210
- sendText(content, msgCtx)
211
- }
212
-
213
- return jsonResult({
214
- success: true,
215
- sentMediaCount: sentFiles.size
216
- })
217
- } catch (err) {
218
- return jsonResult({
219
- error: err instanceof Error ? err.message : String(err)
220
- })
221
- }
222
- }
223
- }
224
- }
package/src/transport.ts DELETED
@@ -1,203 +0,0 @@
1
- import { clearSentMediaKeys, getWsConnection } from './utils/global.js'
2
- import { dcgLogger } from './utils/log.js'
3
- import type { IMsgParams } from './types.js'
4
- import { getEffectiveMsgParams, getParamsDefaults } from './utils/params.js'
5
-
6
- /** 用 sessionKey 从 map 取参,再合并 overrides(channel 出站、媒体等) */
7
- export function mergeSessionParams(sessionKey: string, overrides?: Partial<IMsgParams>): IMsgParams {
8
- const base = getEffectiveMsgParams(sessionKey)
9
- if (!overrides) return base
10
- return { ...base, ...overrides }
11
- }
12
- export function mergeDefaultParams(overrides?: Partial<IMsgParams>): IMsgParams {
13
- const base = getParamsDefaults()
14
- if (!overrides) return base
15
- return { ...base, ...overrides }
16
- }
17
-
18
- export type InboundMsgForContext = {
19
- _userId: number | string
20
- content: {
21
- bot_token: string
22
- domain_id?: string
23
- app_id?: string
24
- bot_id?: string
25
- agent_id?: string
26
- session_id: string
27
- message_id: string
28
- }
29
- }
30
-
31
- export type OpenclawBotChatEnvelope = {
32
- messageType: 'openclaw_bot_chat'
33
- _userId: number | undefined
34
- source: 'client'
35
- content: Record<string, unknown>
36
- }
37
-
38
- function isInboundWire(arg: unknown): arg is InboundMsgForContext {
39
- return Boolean(arg && typeof arg === 'object' && '_userId' in arg && 'content' in arg)
40
- }
41
-
42
- /** 下行 WebSocket 帧 → 内部上下文(字段缺省用 channel 配置补) */
43
- function inboundToCtx(msg: InboundMsgForContext, d: IMsgParams): IMsgParams {
44
- const c = msg.content
45
- return {
46
- userId: Number(msg._userId ?? d.userId),
47
- botToken: c.bot_token ?? d.botToken,
48
- domainId: String(c.domain_id ?? d.domainId),
49
- appId: String(c.app_id ?? d.appId),
50
- botId: c.bot_id,
51
- agentId: c.agent_id,
52
- sessionId: c.session_id,
53
- messageId: c.message_id
54
- }
55
- }
56
-
57
- /** 上行:与配置合并缺省后再 `...ctx` 覆盖(原 wsSendRaw) */
58
- function mergeOutboundWithDefaults(ctx: IMsgParams, d: IMsgParams): IMsgParams {
59
- return {
60
- userId: Number(ctx.userId ?? d.userId),
61
- botToken: ctx.botToken ?? d.botToken,
62
- domainId: String(ctx.domainId ?? d.domainId),
63
- appId: String(ctx.appId ?? d.appId),
64
- ...ctx
65
- }
66
- }
67
-
68
- /**
69
- * 组装完整 wire `content` 对象:先写会话/机器人基础字段(回落到 d),再合并调用方传入的 payload。
70
- * `content` 在使用处构造(如 response、state、files),同名键可覆盖基础字段。
71
- */
72
- export function buildWireContent(base: IMsgParams, d: IMsgParams, content: Record<string, unknown>): Record<string, unknown> {
73
- const resolvedBotToken = base.botToken ?? d.botToken
74
- const domain = base.domainId ?? d.domainId
75
- const app = base.appId ?? d.appId
76
- return {
77
- bot_token: base.botToken || resolvedBotToken,
78
- domain_id: base.domainId || domain,
79
- app_id: base.appId || app,
80
- bot_id: base.botId,
81
- agent_id: base.agentId,
82
- session_id: base.sessionId,
83
- message_id: base.messageId || Date.now().toString(),
84
- ...content
85
- }
86
- }
87
-
88
- /** 上行:在已合并的 ctx 上套 openclaw_bot_chat 信封(messageType / _userId / source + content) */
89
- function buildOutboundOpenclawBotChatEnvelope(
90
- ctx: IMsgParams,
91
- content: Record<string, unknown>,
92
- opts?: { mergeChannelDefaults?: boolean }
93
- ): OpenclawBotChatEnvelope {
94
- const d = getParamsDefaults()
95
- const base = opts?.mergeChannelDefaults ? mergeOutboundWithDefaults(ctx, d) : ctx
96
- return {
97
- messageType: 'openclaw_bot_chat',
98
- _userId: base.userId,
99
- source: 'client',
100
- content: buildWireContent(base, d, content)
101
- }
102
- }
103
-
104
- /**
105
- * 下行解析为 DcgchatMsgContext,或上行组装 openclaw_bot_chat 信封。
106
- * 上行时 `content` 由调用方传入;基础参数来自 `ctx` 与 `getParamsDefaults()`(可选 mergeChannelDefaults,同原 wsSendRaw)。
107
- */
108
- export function buildOpenclawBotChat(msg: InboundMsgForContext): IMsgParams
109
- export function buildOpenclawBotChat(
110
- ctx: IMsgParams,
111
- content: Record<string, unknown>,
112
- opts?: { mergeChannelDefaults?: boolean }
113
- ): OpenclawBotChatEnvelope
114
- export function buildOpenclawBotChat(
115
- arg1: InboundMsgForContext | IMsgParams,
116
- arg2?: Record<string, unknown>,
117
- opts?: { mergeChannelDefaults?: boolean }
118
- ): IMsgParams | OpenclawBotChatEnvelope {
119
- const d = getParamsDefaults()
120
-
121
- if (arg2 === undefined && isInboundWire(arg1)) {
122
- return inboundToCtx(arg1, d)
123
- }
124
-
125
- const ctx = arg1 as IMsgParams
126
- return buildOutboundOpenclawBotChatEnvelope(ctx, arg2 ?? {}, opts)
127
- }
128
-
129
- export function isWsOpen(): boolean {
130
- const isOpen = getWsConnection()?.readyState === WebSocket.OPEN
131
- if (!isOpen) {
132
- dcgLogger(`server socket not ready ${getWsConnection()?.readyState}`, 'error')
133
- }
134
- return isOpen
135
- }
136
-
137
- /**
138
- * 聊天流路径:content 单独 JSON.stringify(双重编码),符合 dcgchat 协议。
139
- * `ctx` 须由调用方用 getEffectiveMsgParams(sessionKey) 等解析好;`content` 为完整业务 payload。
140
- */
141
- export function wsSend(ctx: IMsgParams, content: Record<string, unknown>): boolean {
142
- const ws = getWsConnection()
143
- if (ws?.readyState !== WebSocket.OPEN) return false
144
- const envelope = buildOpenclawBotChat(ctx, content)
145
- ws.send(JSON.stringify({ ...envelope, content: JSON.stringify(envelope.content) }))
146
- return true
147
- }
148
-
149
- /**
150
- * 媒体 / channel 出站:content 保持嵌套对象(单次编码)。
151
- * `ctx` 须由调用方解析(如需合并覆盖可先 mergeSessionParams)。
152
- */
153
- export function wsSendRaw(ctx: IMsgParams, content: Record<string, unknown>, isLog = true): boolean {
154
- const ws = getWsConnection()
155
- if (ws?.readyState !== WebSocket.OPEN) {
156
- dcgLogger(`server socket not ready ${ws?.readyState}`, 'error')
157
- return false
158
- }
159
- const envelope = buildOpenclawBotChat(ctx, content, { mergeChannelDefaults: true })
160
- ws.send(JSON.stringify(envelope))
161
- if (isLog) {
162
- dcgLogger('已发送:' + JSON.stringify(envelope))
163
- }
164
- return true
165
- }
166
-
167
- export function sendChunk(text: string, ctx: IMsgParams, chunkIdx: number): boolean {
168
- return wsSend(ctx, { response: text, state: 'chunk', chunk_idx: chunkIdx })
169
- }
170
-
171
- export function sendFinal(ctx: IMsgParams, tag: string): boolean {
172
- dcgLogger(` message handling complete state: to=${ctx.sessionId} final tag:${tag}`)
173
- clearSentMediaKeys(ctx.sessionId)
174
- return wsSend(ctx, { response: '', state: 'final' })
175
- }
176
-
177
- export function sendText(text: string, ctx: IMsgParams, event?: Record<string, unknown>): boolean {
178
- return wsSend(ctx, { response: text, ...event })
179
- }
180
-
181
- export function sendError(errorMsg: string, ctx: IMsgParams): boolean {
182
- return wsSend(ctx, { response: `[错误] ${errorMsg}`, state: 'final' })
183
- }
184
-
185
- export function sendEventMessage(params: Record<string, string> = {}) {
186
- const ctx = getParamsDefaults()
187
- const ws = getWsConnection()
188
- if (isWsOpen()) {
189
- ws?.send(
190
- JSON.stringify({
191
- messageType: 'openclaw_bot_event',
192
- source: 'client',
193
- content: {
194
- bot_token: ctx.botToken,
195
- domain_id: ctx.domainId,
196
- app_id: ctx.appId,
197
- bot_id: ctx.botId,
198
- ...params
199
- }
200
- })
201
- )
202
- }
203
- }
package/src/types.ts DELETED
@@ -1,139 +0,0 @@
1
- /**
2
- * 插件配置(channels.dcgchat 下的字段)
3
- */
4
- export type DcgchatConfig = {
5
- enabled?: boolean
6
- /** 后端 WebSocket 地址,例如 ws://localhost:8080/openclaw/ws */
7
- wsUrl?: string
8
- /** 连接认证 token */
9
- botToken?: string
10
- /** 用户标识 */
11
- userId?: string
12
- domainId?: string
13
- appId?: string
14
- /**
15
- * 内置 `message` 工具走 OpenClaw 目标解析:`true`(默认)时仅将符合 sessionKey 形态的字符串视为合法 target,
16
- * 纯数字(WS userId 等)会解析失败;设为 `false` 恢复旧版宽松行为(不推荐)。
17
- */
18
- strictMessageToolTarget?: boolean
19
- }
20
-
21
- export type ResolvedDcgchatAccount = {
22
- accountId: string
23
- enabled: boolean
24
- configured: boolean
25
- wsUrl: string
26
- botToken: string
27
- userId: string
28
- domainId?: string
29
- appId?: string
30
- }
31
-
32
- /**
33
- * 下行消息:后端 → OpenClaw(用户发的消息)
34
- */
35
- // export type InboundMessage = {
36
- // type: "message";
37
- // userId: string;
38
- // text: string;
39
- // };
40
- export type InboundMessage = {
41
- messageType: string // "openclaw_bot_chat",
42
- _userId: number
43
- source: string // 'server',
44
- // content: string;
45
- content: {
46
- skills_scope: Record<string, any>[]
47
- bot_token: string
48
- agent_clone_code?: string
49
- domain_id?: string
50
- app_id?: string
51
- bot_id?: string
52
- agent_id?: string
53
- session_id: string
54
- real_mobook: string | number
55
- message_id: string
56
- text: string
57
- files?: {
58
- url: string
59
- name: string
60
- }[]
61
- }
62
- }
63
-
64
- // {"_userId":40,"content":"{\"bot_token\":\"sk_b7f8a3e1c5d24e6f8a1b3c4d5e6f7a8b\",\"session_id\":\"1\",\"message_id\":\"1\",\"text\":\"你好\"}","messageType":"openclaw_bot_chat","msgId":398599,"source":"server","title":"OPENCLAW机器人对话"}
65
-
66
- /**
67
- * 上行消息:OpenClaw → 后端(Agent 回复)
68
- */
69
- // export type OutboundReply = {
70
- // type: "reply";
71
- // userId: string;
72
- // text: string;
73
- // };
74
-
75
- export type OutboundReply = {
76
- messageType: string // "openclaw_bot_chat",
77
- _userId: number // 100
78
- source: string // 'client',
79
- // content: string;
80
- content: {
81
- bot_token: string // ""
82
- session_id: string // ""
83
- message_id: string // ""
84
- response: string // ""
85
- state: string // final, chunk
86
- domain_id?: string
87
- app_id?: string
88
- bot_id?: string
89
- agent_id?: string
90
- files?: { url: string; name: string }[]
91
- }
92
- }
93
-
94
- export interface IResponse<T = unknown> {
95
- /** 响应状态码 */
96
- code?: number | string
97
- /** 响应数据 */
98
- data?: T
99
- /** 响应消息 */
100
- message?: string
101
- }
102
-
103
- export interface IStsToken {
104
- bucket: string
105
- endPoint: string
106
- expiration: string
107
- ossFileKey: string
108
- policy: string
109
- region: string
110
- signature: string
111
- sourceFileName: string
112
- stsEndPoint: string
113
- tempAccessKeyId: string
114
- tempAccessKeySecret: string
115
- tempSecurityToken: string
116
- uploadDir: string
117
- protocol: string
118
- }
119
-
120
- export interface IStsTokenReq {
121
- sourceFileName: string
122
- isPrivate: number
123
- }
124
-
125
- export interface IMsgParams {
126
- userId?: number
127
- botToken?: string
128
- sessionId?: string
129
- messageId?: string
130
- domainId?: string
131
- appId?: string
132
- botId?: string
133
- agentId?: string
134
- /** 与 OpenClaw 路由一致,用于 map 与异步链路(工具 / HTTP / cron)对齐当前会话 */
135
- sessionKey?: string
136
- real_mobook?: string | number
137
- is_finish?: number
138
- message_tags?: Record<string, string>
139
- }
@@ -1,7 +0,0 @@
1
- export const ENV: 'production' | 'test' | 'develop' = 'production'
2
-
3
-
4
- export const systemCommand = ['/new', '/status']
5
- export const stopCommand = ['/stop']
6
-
7
- export const ignoreToolCommand = ['/search', '/abort', '/queue interrupt', ...systemCommand, ...stopCommand]
@@ -1,55 +0,0 @@
1
- import type { GatewayEvent } from '../gateway/index.js'
2
- import { finishedDcgchatCron, sendDcgchatCron } from '../cron.js'
3
- import { dcgLogger } from './log.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'
8
-
9
- /**
10
- * 处理网关 event 帧的副作用(agent 流式输出、cron 同步),并构造供上层分发的 GatewayEvent。
11
- */
12
- export function handleGatewayEventMessage(msg: { event?: string; payload?: Record<string, unknown>; seq?: number }): GatewayEvent {
13
- try {
14
- // 子agent消息输出
15
- if (msg.event === 'agent') {
16
- const pl = msg.payload as { runId: string; data?: { delta?: unknown } }
17
- const sessionKey = getSessionKeyBySubAgentRunId(pl.runId)
18
- const outboundCtx = getEffectiveMsgParams(sessionKey)
19
- if (pl.data?.delta) {
20
- if (outboundCtx.sessionId) {
21
- sendChunk(pl.data.delta as string, outboundCtx, 0)
22
- }
23
- }
24
- }
25
- // 定时任务
26
- if (msg.event === 'cron') {
27
- const p = msg.payload
28
- dcgLogger(`[Gateway] 收到定时任务事件: ${JSON.stringify(p)}`)
29
- if (p?.action === 'added') {
30
- sendDcgchatCron(p?.jobId as string)
31
- }
32
- if (p?.action === 'updated') {
33
- sendDcgchatCron(p?.jobId as string)
34
- }
35
- if (p?.action === 'removed') {
36
- sendDcgchatCron(p?.jobId as string)
37
- }
38
- if (p?.action === 'finished' && p?.status === 'ok') {
39
- const hasFileOutput = p.delivered === true && p.deliveryStatus === 'delivered'
40
- let summary = p?.summary as string
41
- if (summary.indexOf('HEARTBEAT_OK') >= 0 && summary !== 'HEARTBEAT_OK') {
42
- summary = summary.replace('HEARTBEAT_OK', '')
43
- }
44
- finishedDcgchatCron(p?.jobId as string, summary, hasFileOutput)
45
- }
46
- }
47
- } catch (error) {
48
- dcgLogger(`[Gateway] 处理事件失败: ${error}`, 'error')
49
- }
50
- return {
51
- type: msg.event as string,
52
- payload: msg.payload,
53
- seq: msg.seq as number | undefined
54
- }
55
- }