@dcrays/dcgchat-test 0.3.31 → 0.3.32

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.3.31",
3
+ "version": "0.3.32",
4
4
  "type": "module",
5
5
  "description": "OpenClaw channel plugin for 书灵墨宝 (WebSocket)",
6
6
  "main": "index.ts",
package/src/channel.ts CHANGED
@@ -176,10 +176,15 @@ export const dcgchatPlugin: ChannelPlugin<ResolvedDcgchatAccount> = {
176
176
  const outboundCtx = getOutboundMsgParams(to)
177
177
  const cronMsgId = getCronMessageId(to)
178
178
  const messageId = !!cronMsgId ? cronMsgId : isCron ? `${Date.now()}` : outboundCtx?.messageId
179
+ const content: Record<string, unknown> = { response: ctx.text }
180
+ if (isCron || !outboundCtx?.messageId) {
181
+ content.is_finish = -1
182
+ content.message_tags = { source: 'cron' }
183
+ }
179
184
  if (isWsOpen()) {
180
185
  if (outboundCtx?.sessionId) {
181
186
  const newCtx = { ...outboundCtx, messageId }
182
- wsSendRaw(newCtx, { response: ctx.text, is_finish: -1, message_tags: { source: 'channel' } })
187
+ wsSendRaw(newCtx, content)
183
188
  } else {
184
189
  const sessionInfo = to.split(':')
185
190
  const sessionId = sessionInfo.at(-1) ?? ''
@@ -188,10 +193,9 @@ export const dcgchatPlugin: ChannelPlugin<ResolvedDcgchatAccount> = {
188
193
  agentId: agentId,
189
194
  sessionId: `${sessionId}`,
190
195
  messageId: messageId,
191
- is_finish: -1,
192
196
  real_mobook: !sessionId ? 1 : ''
193
197
  })
194
- wsSendRaw(merged, { response: ctx.text, message_tags: { source: 'channel' } })
198
+ wsSendRaw(merged, content)
195
199
  }
196
200
  }
197
201
  return {
package/src/cron.ts CHANGED
@@ -6,7 +6,7 @@ import { getCronMessageId, getWorkspaceDir, getWsConnection, removeCronMessageId
6
6
  import { ossUpload } from './request/oss.js'
7
7
  import { dcgLogger } from './utils/log.js'
8
8
  import { sendMessageToGateway } from './gateway/socket.js'
9
- import { getEffectiveMsgParams } from './utils/params.js'
9
+ import { getEffectiveMsgParams, getParamsDefaults } from './utils/params.js'
10
10
 
11
11
  export function getCronJobsPath(): string {
12
12
  const workspaceDir = getWorkspaceDir()
@@ -51,17 +51,27 @@ const CRON_UPLOAD_DEBOUNCE_MS = 6600
51
51
  let pendingCronUploadCtx: IMsgParams | null = null
52
52
  let cronUploadFlushTimer: ReturnType<typeof setTimeout> | null = null
53
53
 
54
- async function runCronJobsUpload(msgCtx: IMsgParams): Promise<void> {
54
+ async function runCronJobsUpload(sessionKey: string): Promise<void> {
55
55
  const jobPath = getCronJobsPath()
56
+ const botToken = getParamsDefaults().botToken
56
57
  if (fs.existsSync(jobPath)) {
57
58
  try {
58
- const url = await ossUpload(jobPath, msgCtx.botToken ?? '', 0)
59
+ const url = await ossUpload(jobPath, botToken ?? '', 0)
59
60
  dcgLogger(`定时任务创建成功: ${url}`)
60
- if (!msgCtx.sessionKey) {
61
- dcgLogger(`runCronJobsUpload: missing sessionKey ${JSON.stringify(msgCtx)} on msgCtx`, 'error')
61
+ if (!sessionKey) {
62
+ dcgLogger(`runCronJobsUpload: missing sessionKey on msgCtx`, 'error')
62
63
  return
63
64
  }
64
- sendEventMessage(url, msgCtx.sessionKey)
65
+ const sessionInfo = sessionKey.split(':')
66
+ const sessionId = sessionInfo.at(-1) ?? ''
67
+ const agentId = sessionInfo.at(-2) ?? ''
68
+ const params = {
69
+ event_type: 'cron',
70
+ operation_type: 'install',
71
+ session_id: sessionId,
72
+ agent_id: agentId
73
+ }
74
+ sendEventMessage(url, params)
65
75
  } catch (error) {
66
76
  dcgLogger(`${jobPath} upload failed: ${error}`, 'error')
67
77
  }
@@ -70,14 +80,6 @@ async function runCronJobsUpload(msgCtx: IMsgParams): Promise<void> {
70
80
  }
71
81
  }
72
82
 
73
- function flushCronUploadQueue(): void {
74
- cronUploadFlushTimer = null
75
- const ctx = pendingCronUploadCtx
76
- pendingCronUploadCtx = null
77
- if (!ctx) return
78
- void runCronJobsUpload(ctx)
79
- }
80
-
81
83
  /**
82
84
  * 将 jobs.json 同步到 OSS 并推送事件。30s 内多次调用合并为一次上传;定时触发后清空待处理项,避免重复执行。
83
85
  * @param msgCtx 可选;省略时使用当前会话 getEffectiveMsgParams(sessionKey) 快照
@@ -85,17 +87,17 @@ function flushCronUploadQueue(): void {
85
87
  export function sendDcgchatCron(jobId: string): void {
86
88
  const jobPath = getCronJobsPath()
87
89
  const { sessionKey } = readCronJob(jobPath, jobId) || {}
88
- const ctx = msgParamsToCtx(getEffectiveMsgParams(sessionKey))
89
- if (!ctx) {
90
+ if (!sessionKey) {
90
91
  dcgLogger('sendDcgchatCron: no message context (missing token / params)', 'error')
91
92
  return
92
93
  }
93
94
  dcgLogger(`sessionKey: ${sessionKey}, jobId: ${jobId}`)
94
- pendingCronUploadCtx = ctx
95
95
  if (cronUploadFlushTimer !== null) {
96
96
  clearTimeout(cronUploadFlushTimer)
97
97
  }
98
- cronUploadFlushTimer = setTimeout(flushCronUploadQueue, CRON_UPLOAD_DEBOUNCE_MS)
98
+ cronUploadFlushTimer = setTimeout(() => {
99
+ runCronJobsUpload(sessionKey)
100
+ }, CRON_UPLOAD_DEBOUNCE_MS)
99
101
  }
100
102
 
101
103
  /**
@@ -363,10 +363,10 @@ export class GatewayConnection {
363
363
  if (msg.event === 'cron') {
364
364
  dcgLogger(`[Gateway] 收到事件: ${JSON.stringify(msg)}`)
365
365
  if (msg.payload?.action === 'added') {
366
- sendDcgchatCron()
366
+ sendDcgchatCron(msg.payload?.jobId)
367
367
  }
368
368
  if (msg.payload?.action === 'updated') {
369
- sendDcgchatCron()
369
+ sendDcgchatCron(msg.payload?.jobId as string)
370
370
  }
371
371
  if (msg.payload?.action === 'finished') {
372
372
  finishedDcgchatCron(msg.payload?.jobId as string)
@@ -40,7 +40,7 @@ export const queryUserTokenByBotToken = async (botToken: string): Promise<string
40
40
  const response = await post<{ botToken: string }, { token: string }>('/organization/queryUserTokenByBotToken', { botToken })
41
41
 
42
42
  if (!response || !response.data || !response.data.token) {
43
- dcgLogger('获取绑定的用户信息失败: ' + JSON.stringify(response), 'error')
43
+ dcgLogger('获取绑定的用户信息失败: token:' + botToken + '|' + JSON.stringify(response), 'error')
44
44
  return ''
45
45
  }
46
46
 
package/src/transport.ts CHANGED
@@ -178,8 +178,8 @@ export function sendError(errorMsg: string, ctx: IMsgParams): boolean {
178
178
  return wsSend(ctx, { response: `[错误] ${errorMsg}`, state: 'final' })
179
179
  }
180
180
 
181
- export function sendEventMessage(url: string, sessionKey: string) {
182
- const ctx = getEffectiveMsgParams(sessionKey)
181
+ export function sendEventMessage(url: string, params: Record<string, string> = {}) {
182
+ const ctx = getParamsDefaults()
183
183
  const ws = getWsConnection()
184
184
  if (isWsOpen()) {
185
185
  ws?.send(
@@ -187,16 +187,12 @@ export function sendEventMessage(url: string, sessionKey: string) {
187
187
  messageType: 'openclaw_bot_event',
188
188
  source: 'client',
189
189
  content: {
190
- event_type: 'cron',
191
- operation_type: 'install',
192
190
  bot_token: ctx.botToken,
193
191
  domain_id: ctx.domainId,
194
192
  app_id: ctx.appId,
195
193
  oss_url: url,
196
194
  bot_id: ctx.botId,
197
- agent_id: ctx.agentId,
198
- session_id: ctx.sessionId,
199
- message_id: Date.now().toString()
195
+ ...params
200
196
  }
201
197
  })
202
198
  )
@@ -9,7 +9,7 @@ const paramsMessageMap = new Map<string, IMsgParams>()
9
9
 
10
10
  /** 从 OpenClaw 配置读取当前 channel 的基础参数(唯一来源,供 transport / resolve 等复用) */
11
11
  export function getParamsDefaults(): IMsgParams {
12
- const ch = (getOpenClawConfig()?.channels?.['dcgchat'] as DcgchatConfig | undefined) ?? {}
12
+ const ch = (getOpenClawConfig()?.channels?.["dcgchat-test"] as DcgchatConfig | undefined) ?? {}
13
13
  return {
14
14
  userId: Number(ch.userId ?? 0),
15
15
  botToken: ch.botToken ?? '',