@dcrays/dcgchat-test 0.4.13 → 0.4.15

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.13",
3
+ "version": "0.4.15",
4
4
  "type": "module",
5
5
  "description": "OpenClaw channel plugin for 书灵墨宝 (WebSocket)",
6
6
  "main": "index.ts",
package/src/channel.ts CHANGED
@@ -254,19 +254,19 @@ export const dcgchatPlugin: ChannelPlugin<ResolvedDcgchatAccount> = {
254
254
  const isCron = ctx.to.indexOf('dcg-cron:') >= 0
255
255
  const outboundCtx = getOutboundMsgParams(to)
256
256
  const content: Record<string, unknown> = { response: ctx.text }
257
- if (isCron) {
258
- messageId = getCronMessageId(to) || `${Date.now()}`
259
- const { sessionId, agentId } = getInfoBySessionKey(to)
260
- content.is_finish = -1
261
- content.message_tags = { source: 'cron' }
262
- const merged = mergeDefaultParams({
263
- agentId: agentId,
264
- sessionId: `${sessionId}`,
265
- messageId: messageId,
266
- real_mobook: !sessionId ? 1 : ''
267
- })
268
- wsSendRaw(merged, content)
269
- } else {
257
+ if (!isCron) {
258
+ // messageId = getCronMessageId(to) || `${Date.now()}`
259
+ // const { sessionId, agentId } = getInfoBySessionKey(to)
260
+ // content.is_finish = -1
261
+ // content.message_tags = { source: 'cron' }
262
+ // const merged = mergeDefaultParams({
263
+ // agentId: agentId,
264
+ // sessionId: `${sessionId}`,
265
+ // messageId: messageId,
266
+ // real_mobook: !sessionId ? 1 : ''
267
+ // })
268
+ // wsSendRaw(merged, content)
269
+ // } else {
270
270
  if (outboundCtx?.sessionId) {
271
271
  messageId = outboundCtx?.messageId || `${Date.now()}`
272
272
  const newCtx = { ...outboundCtx, messageId }
package/src/cron.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import path from 'node:path'
2
2
  import fs from 'node:fs'
3
3
  import type { IMsgParams } from './types.js'
4
- import { isWsOpen, mergeDefaultParams, sendEventMessage, sendFinal } from './transport.js'
4
+ import { isWsOpen, mergeDefaultParams, sendEventMessage, sendFinal, wsSendRaw } from './transport.js'
5
5
  import { getCronMessageId, getWorkspaceDir, getWsConnection, removeCronMessageId, setCronMessageId } from './utils/global.js'
6
6
  import { ossUpload } from './request/oss.js'
7
7
  import { dcgLogger } from './utils/log.js'
@@ -135,7 +135,7 @@ export const onRunCronJob = async (jobId: string, messageId: string) => {
135
135
  )
136
136
  sendMessageToGateway(JSON.stringify({ method: 'cron.run', params: { id: jobId, mode: 'force' } }))
137
137
  }
138
- export const finishedDcgchatCron = async (jobId: string) => {
138
+ export const finishedDcgchatCron = async (jobId: string, summary: string) => {
139
139
  const id = jobId?.trim()
140
140
  if (!id) {
141
141
  dcgLogger('finishedDcgchatCron: empty jobId', 'error')
@@ -147,24 +147,23 @@ export const finishedDcgchatCron = async (jobId: string) => {
147
147
  dcgLogger(`finishedDcgchatCron: no sessionKey for job id=${id}`, 'error')
148
148
  return
149
149
  }
150
- const outboundCtx = getEffectiveMsgParams(sessionKey)
151
- const messageId = getCronMessageId(sessionKey)
150
+ const messageId = getCronMessageId(sessionKey) || `${Date.now()}`
152
151
  const sessionInfo = sessionKey.split(':')
153
152
  const sessionId = sessionInfo.at(-1) ?? ''
154
153
  const agentId = sessionInfo.at(-2) ?? ''
155
- if (outboundCtx?.sessionId) {
156
- const newCtx = messageId ? { ...outboundCtx, messageId } : outboundCtx
157
- sendFinal(newCtx, 'cron send')
158
- } else {
159
- const merged = mergeDefaultParams({
160
- agentId: agentId,
161
- sessionId: `${sessionId}`,
162
- messageId: messageId,
163
- is_finish: -1,
164
- real_mobook: !sessionId ? 1 : ''
165
- })
154
+ // if (outboundCtx?.sessionId) {
155
+
156
+ const merged = mergeDefaultParams({
157
+ agentId: agentId,
158
+ sessionId: `${sessionId}`,
159
+ messageId: messageId || `${Date.now()}`,
160
+ real_mobook: !sessionId ? 1 : ''
161
+ })
162
+ wsSendRaw(merged, { response: summary, message_tags: { source: 'cron' }, is_finish: -1 })
163
+ setTimeout(() => {
166
164
  sendFinal(merged, 'cron send')
167
- }
165
+ }, 200)
166
+ // }
168
167
  const ws = getWsConnection()
169
168
  const baseContent = getParamsDefaults()
170
169
  if (isWsOpen()) {
@@ -132,14 +132,13 @@ function injectBestEffort(params: Record<string, unknown>, sk: string): Record<s
132
132
  return newParams
133
133
  }
134
134
 
135
- // job.delivery(须改 job 内对象,勿写 newParams.delivery——顶层可能不存在)
135
+ // job.delivery
136
136
  const job = newParams.job as Record<string, unknown> | undefined
137
137
  if (job?.delivery && typeof job.delivery === 'object') {
138
- const del = job.delivery as CronDelivery
139
- del.bestEffort = true
140
- del.to = `dcg-cron:${sk}`
141
- del.accountId = agentId
142
- del.channel = "dcgchat-test"
138
+ ;(job.delivery as CronDelivery).bestEffort = true
139
+ ;(newParams.delivery as CronDelivery).to = `dcg-cron:${sk}`
140
+ ;(newParams.delivery as CronDelivery).accountId = agentId
141
+ ;(newParams.delivery as CronDelivery).channel = "dcgchat-test"
143
142
  newParams.sessionKey = sk
144
143
  return newParams
145
144
  }
@@ -32,7 +32,7 @@ export function handleGatewayEventMessage(msg: { event?: string; payload?: Recor
32
32
  sendDcgchatCron(p?.jobId as string)
33
33
  }
34
34
  if (p?.action === 'finished') {
35
- finishedDcgchatCron(p?.jobId as string)
35
+ finishedDcgchatCron(p?.jobId as string, p?.summary as string)
36
36
  }
37
37
  }
38
38
  } catch (error) {
@@ -108,18 +108,34 @@ export function clearSentMediaKeys(messageId?: string) {
108
108
  }
109
109
  }
110
110
 
111
- const cronMessageIdMap = new Map<string, string>()
111
+ /** 每个 sessionKey 下多个定时任务 messageId,入队在尾、出队在头(FIFO) */
112
+ const cronMessageIdMap = new Map<string, string[]>()
112
113
 
113
114
  export function setCronMessageId(sk: string, messageId: string) {
114
- cronMessageIdMap.set(sk, messageId)
115
+ if (!sk?.trim() || !messageId?.trim()) return
116
+ let q = cronMessageIdMap.get(sk)
117
+ if (!q) {
118
+ q = []
119
+ cronMessageIdMap.set(sk, q)
120
+ }
121
+ q.push(messageId)
115
122
  }
116
123
 
124
+ /** 窥视队首 messageId(不移除),供发送中与 finished 配对使用 */
117
125
  export function getCronMessageId(sk: string): string {
118
- return cronMessageIdMap.get(sk) ?? ''
126
+ if (!sk?.trim()) return ''
127
+ return cronMessageIdMap.get(sk)?.[0] ?? ''
119
128
  }
120
129
 
130
+ /** 弹出队首一条,与一次 finished 对应;队列为空时移除 key */
121
131
  export function removeCronMessageId(sk: string) {
122
- cronMessageIdMap.delete(sk)
132
+ if (!sk?.trim()) return
133
+ const q = cronMessageIdMap.get(sk)
134
+ if (!q?.length) return
135
+ q.shift()
136
+ if (q.length === 0) {
137
+ cronMessageIdMap.delete(sk)
138
+ }
123
139
  }
124
140
 
125
141
  export const getSessionKey = (content: any, accountId: string) => {