claw-subagent-service 0.0.80 → 0.0.83

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": "claw-subagent-service",
3
- "version": "0.0.80",
3
+ "version": "0.0.83",
4
4
  "description": "虾说智能助手",
5
5
  "main": "cli.js",
6
6
  "bin": {
@@ -169,7 +169,12 @@ class RongCloudClient {
169
169
  // 自定义消息 content 可能是对象,提取文本内容并保留 mentionedInfo
170
170
  if (rawContent && typeof rawContent === 'object') {
171
171
  mentionedInfo = mentionedInfo || rawContent.mentionedInfo || null;
172
- rawContent = rawContent.content || rawContent.text || JSON.stringify(rawContent);
172
+ // system_service 等结构化消息保留完整 JSON(上层需要 msg_type 等字段)
173
+ if (message.messageType === 'system_service' || rawContent.msg_type) {
174
+ rawContent = JSON.stringify(rawContent);
175
+ } else {
176
+ rawContent = rawContent.content || rawContent.text || JSON.stringify(rawContent);
177
+ }
173
178
  }
174
179
 
175
180
  const content = rawContent || '';
package/service/worker.js CHANGED
@@ -334,66 +334,75 @@ async function initRongCloud() {
334
334
  const originalHandleMessage = messageHandler.handleMessage.bind(messageHandler);
335
335
 
336
336
  messageHandler.handleMessage = async (msg) => {
337
- // 检查是否是结构化消息
338
- if (msg.content && typeof msg.content === 'string') {
339
- try {
340
- const parsed = JSON.parse(msg.content);
341
-
342
- if (parsed.msg_type) {
343
- // 这是结构化消息,使用 RongyunMessageHandler 处理
344
- log.info(`[WORKER] 收到结构化消息: type=${parsed.msg_type}, from=${parsed.source_im_id || msg.senderUserId}`);
337
+ // 检查是否是结构化消息(支持字符串和对象两种格式)
338
+ // 融云 SDK 对自定义消息可能直接返回对象而非 JSON 字符串
339
+ if (msg.content) {
340
+ let parsed = null;
341
+
342
+ if (typeof msg.content === 'string') {
343
+ // 字符串类型:尝试 JSON 解析
344
+ try {
345
+ parsed = JSON.parse(msg.content);
346
+ } catch {
347
+ // 不是 JSON,是普通消息
348
+ }
349
+ } else if (typeof msg.content === 'object' && msg.content !== null) {
350
+ // 对象类型:直接使用(融云 SDK 已自动解析)
351
+ parsed = msg.content;
352
+ }
345
353
 
346
- // 忽略自己发送的消息
347
- if (parsed.source_im_id === rongcloudConfig.accountId) {
348
- return;
349
- }
354
+ if (parsed && parsed.msg_type) {
355
+ // 这是结构化消息,使用 RongyunMessageHandler 处理
356
+ log.info(`[WORKER] 收到结构化消息: type=${parsed.msg_type}, from=${parsed.source_im_id || msg.senderUserId}`);
350
357
 
351
- // Timestamp 校验(5分钟有效期)
352
- const msgTimestamp = parsed.timestamp;
353
- if (msgTimestamp) {
354
- const currentTime = Math.floor(Date.now() / 1000);
355
- const timeDiff = Math.abs(currentTime - msgTimestamp);
356
- if (timeDiff > 300) { // 5分钟 = 300秒
357
- logTimestampValidation(
358
- `消息时间戳过期: msg_time=${msgTimestamp}, current_time=${currentTime}, ` +
359
- `diff=${timeDiff}s, type=${parsed.msg_type}, source=${parsed.source_im_id}`
360
- );
361
- return;
362
- }
363
- }
358
+ // 忽略自己发送的消息
359
+ if (parsed.source_im_id === rongcloudConfig.accountId) {
360
+ return;
361
+ }
364
362
 
365
- // 解析 content 字段(它本身可能是 JSON 字符串)
366
- let innerContent = parsed.content;
367
- if (typeof innerContent === 'string') {
368
- try {
369
- innerContent = JSON.parse(innerContent);
370
- } catch {
371
- // 保持字符串
372
- }
363
+ // Timestamp 校验(5分钟有效期)
364
+ const msgTimestamp = parsed.timestamp;
365
+ if (msgTimestamp) {
366
+ const currentTime = Math.floor(Date.now() / 1000);
367
+ const timeDiff = Math.abs(currentTime - msgTimestamp);
368
+ if (timeDiff > 300) { // 5分钟 = 300秒
369
+ logTimestampValidation(
370
+ `消息时间戳过期: msg_time=${msgTimestamp}, current_time=${currentTime}, ` +
371
+ `diff=${timeDiff}s, type=${parsed.msg_type}, source=${parsed.source_im_id}`
372
+ );
373
+ return;
373
374
  }
375
+ }
374
376
 
375
- // 构建消息数据
376
- // 注意:后端发送的 command 消息中,command/command_id/request_id 在 content 字段内
377
- // 保留原始 content(用户消息内容),同时展开其他字段
378
- const messageData = {
379
- ...parsed,
380
- ...innerContent, // 展开 content 中的字段(如 command, command_id 等)
381
- content: typeof innerContent === 'object' ? innerContent.content : innerContent,
382
- senderUserId: parsed.source_im_id || msg.senderUserId,
383
- targetId: msg.targetId,
384
- conversationType: msg.conversationType,
385
- };
386
-
387
- // 使用 RongyunMessageHandler 处理
377
+ // 解析 content 字段(它本身可能是 JSON 字符串)
378
+ let innerContent = parsed.content;
379
+ if (typeof innerContent === 'string') {
388
380
  try {
389
- await rongyunMessageHandler.handle(messageData);
390
- } catch (err) {
391
- log.error(`[WORKER] RongyunMessageHandler 处理异常: ${err.message}`);
381
+ innerContent = JSON.parse(innerContent);
382
+ } catch {
383
+ // 保持字符串
392
384
  }
393
- return;
394
385
  }
395
- } catch {
396
- // 不是 JSON,是普通消息,继续传给原始 handler
386
+
387
+ // 构建消息数据
388
+ // 注意:后端发送的 command 消息中,command/command_id/request_id 在 content 字段内
389
+ // 保留原始 content(用户消息内容),同时展开其他字段
390
+ const messageData = {
391
+ ...parsed,
392
+ ...innerContent, // 展开 content 中的字段(如 command, command_id 等)
393
+ content: typeof innerContent === 'object' ? innerContent.content : innerContent,
394
+ senderUserId: parsed.source_im_id || msg.senderUserId,
395
+ targetId: msg.targetId,
396
+ conversationType: msg.conversationType,
397
+ };
398
+
399
+ // 使用 RongyunMessageHandler 处理
400
+ try {
401
+ await rongyunMessageHandler.handle(messageData);
402
+ } catch (err) {
403
+ log.error(`[WORKER] RongyunMessageHandler 处理异常: ${err.message}`);
404
+ }
405
+ return;
397
406
  }
398
407
  }
399
408