@dcrays/dcgchat-test 0.3.32 → 0.3.34

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.32",
3
+ "version": "0.3.34",
4
4
  "type": "module",
5
5
  "description": "OpenClaw channel plugin for 书灵墨宝 (WebSocket)",
6
6
  "main": "index.ts",
package/src/channel.ts CHANGED
@@ -2,7 +2,7 @@ import type { ChannelPlugin, OpenClawConfig } from 'openclaw/plugin-sdk'
2
2
  import { DEFAULT_ACCOUNT_ID } from 'openclaw/plugin-sdk'
3
3
  import type { ResolvedDcgchatAccount, DcgchatConfig } from './types.js'
4
4
  import { ossUpload } from './request/oss.js'
5
- import { addSentMediaKey, getCronMessageId, getOpenClawConfig, hasSentMediaKey } from './utils/global.js'
5
+ import { addSentMediaKey, getCronMessageId, getInfoBySessionKey, getOpenClawConfig, hasSentMediaKey } from './utils/global.js'
6
6
  import { isWsOpen, mergeDefaultParams, mergeSessionParams, sendFinal, wsSendRaw } from './transport.js'
7
7
  import { dcgLogger, setLogger } from './utils/log.js'
8
8
  import { getOutboundMsgParams, getParamsMessage } from './utils/params.js'
@@ -147,7 +147,7 @@ export const dcgchatPlugin: ChannelPlugin<ResolvedDcgchatAccount> = {
147
147
  })
148
148
  },
149
149
  messaging: {
150
- normalizeTarget: (raw) => normalizeSessionTarget(raw ?? '') || undefined,
150
+ normalizeTarget: (raw) => raw || undefined,
151
151
  targetResolver: {
152
152
  looksLikeId: (raw) => Boolean(raw?.trim()),
153
153
  hint: 'effectiveSessionKey(与 SessionKey 一致;勿填配置里的 WS userId)'
@@ -162,33 +162,25 @@ export const dcgchatPlugin: ChannelPlugin<ResolvedDcgchatAccount> = {
162
162
  outbound: {
163
163
  deliveryMode: 'direct',
164
164
  resolveTarget: ({ to }) => {
165
- const normalized = normalizeSessionTarget(to ?? '')
166
- if (!normalized) {
165
+ if (!to) {
167
166
  return { ok: false, error: new Error('target is empty') }
168
167
  }
169
- return { ok: true, to: normalized }
168
+ return { ok: true, to: to }
170
169
  },
171
170
  textChunkLimit: 4000,
172
171
  sendText: async (ctx) => {
173
- const isCron = ctx.to.indexOf('dcg-cron:') >= 0
174
- const to = normalizeSessionTarget(ctx.to)
175
172
  dcgLogger(`channel sendText to ${ctx.to} `)
176
- const outboundCtx = getOutboundMsgParams(to)
177
- const cronMsgId = getCronMessageId(to)
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
- }
173
+ let messageId = ''
174
+ const to = normalizeSessionTarget(ctx.to)
184
175
  if (isWsOpen()) {
185
- if (outboundCtx?.sessionId) {
186
- const newCtx = { ...outboundCtx, messageId }
187
- wsSendRaw(newCtx, content)
188
- } else {
189
- const sessionInfo = to.split(':')
190
- const sessionId = sessionInfo.at(-1) ?? ''
191
- const agentId = sessionInfo.at(-2) ?? ''
176
+ const isCron = ctx.to.indexOf('dcg-cron:') >= 0
177
+ const outboundCtx = getOutboundMsgParams(to)
178
+ const content: Record<string, unknown> = { response: ctx.text }
179
+ if (isCron) {
180
+ messageId = getCronMessageId(to) || `${Date.now()}`
181
+ const { sessionId, agentId } = getInfoBySessionKey(to)
182
+ content.is_finish = -1
183
+ content.message_tags = { source: 'cron' }
192
184
  const merged = mergeDefaultParams({
193
185
  agentId: agentId,
194
186
  sessionId: `${sessionId}`,
@@ -196,6 +188,14 @@ export const dcgchatPlugin: ChannelPlugin<ResolvedDcgchatAccount> = {
196
188
  real_mobook: !sessionId ? 1 : ''
197
189
  })
198
190
  wsSendRaw(merged, content)
191
+ } else {
192
+ if (outboundCtx?.sessionId) {
193
+ messageId = outboundCtx?.messageId || `${Date.now()}`
194
+ const newCtx = { ...outboundCtx, messageId }
195
+ wsSendRaw(newCtx, content)
196
+ } else {
197
+ dcgLogger(`channel sendText to ${ctx.to} -> sessionId not found`, 'error')
198
+ }
199
199
  }
200
200
  }
201
201
  return {
package/src/cron.ts CHANGED
@@ -45,10 +45,8 @@ function msgParamsToCtx(p: IMsgParams): IMsgParams | null {
45
45
  return p
46
46
  }
47
47
 
48
- const CRON_UPLOAD_DEBOUNCE_MS = 6600
48
+ const CRON_UPLOAD_DEBOUNCE_MS = 2400
49
49
 
50
- /** 待合并的上传上下文(短时间内多次调用只保留最后一次) */
51
- let pendingCronUploadCtx: IMsgParams | null = null
52
50
  let cronUploadFlushTimer: ReturnType<typeof setTimeout> | null = null
53
51
 
54
52
  async function runCronJobsUpload(sessionKey: string): Promise<void> {
@@ -57,12 +55,8 @@ async function runCronJobsUpload(sessionKey: string): Promise<void> {
57
55
  if (fs.existsSync(jobPath)) {
58
56
  try {
59
57
  const url = await ossUpload(jobPath, botToken ?? '', 0)
60
- dcgLogger(`定时任务创建成功: ${url}`)
61
- if (!sessionKey) {
62
- dcgLogger(`runCronJobsUpload: missing sessionKey on msgCtx`, 'error')
63
- return
64
- }
65
- const sessionInfo = sessionKey.split(':')
58
+ dcgLogger(`定时任务更新成功: ${url}`)
59
+ const sessionInfo = sessionKey?.split(':') || []
66
60
  const sessionId = sessionInfo.at(-1) ?? ''
67
61
  const agentId = sessionInfo.at(-2) ?? ''
68
62
  const params = {
@@ -87,10 +81,6 @@ async function runCronJobsUpload(sessionKey: string): Promise<void> {
87
81
  export function sendDcgchatCron(jobId: string): void {
88
82
  const jobPath = getCronJobsPath()
89
83
  const { sessionKey } = readCronJob(jobPath, jobId) || {}
90
- if (!sessionKey) {
91
- dcgLogger('sendDcgchatCron: no message context (missing token / params)', 'error')
92
- return
93
- }
94
84
  dcgLogger(`sessionKey: ${sessionKey}, jobId: ${jobId}`)
95
85
  if (cronUploadFlushTimer !== null) {
96
86
  clearTimeout(cronUploadFlushTimer)
@@ -368,6 +368,9 @@ export class GatewayConnection {
368
368
  if (msg.payload?.action === 'updated') {
369
369
  sendDcgchatCron(msg.payload?.jobId as string)
370
370
  }
371
+ if (msg.payload?.action === 'removed') {
372
+ sendDcgchatCron(msg.payload?.jobId as string)
373
+ }
371
374
  if (msg.payload?.action === 'finished') {
372
375
  finishedDcgchatCron(msg.payload?.jobId as string)
373
376
  }
@@ -143,3 +143,8 @@ export function getCronMessageId(sk: string): string {
143
143
  export function removeCronMessageId(sk: string) {
144
144
  cronMessageIdMap.delete(sk)
145
145
  }
146
+
147
+ export function getInfoBySessionKey(sk: string): { sessionId: string; agentId: string } {
148
+ const sessionInfo = sk.split(':')
149
+ return { sessionId: sessionInfo.at(-1) ?? '', agentId: sessionInfo.at(-2) ?? '' }
150
+ }