adp-openclaw 0.0.63 → 0.0.65

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": "adp-openclaw",
3
- "version": "0.0.63",
3
+ "version": "0.0.65",
4
4
  "description": "ADP-OpenClaw demo channel plugin (Go WebSocket backend)",
5
5
  "type": "module",
6
6
  "dependencies": {
package/src/monitor.ts CHANGED
@@ -5,6 +5,7 @@ import type { PluginLogger, ClawdbotConfig } from "openclaw/plugin-sdk";
5
5
  import { getAdpOpenclawRuntime, setActiveWebSocket } from "./runtime.js";
6
6
  import {
7
7
  getChatHistory,
8
+ getMergedChatHistory,
8
9
  listSessions,
9
10
  resolveSessionKey,
10
11
  type ChatHistoryResponse,
@@ -767,18 +768,33 @@ async function connectAndHandle(params: ConnectParams): Promise<void> {
767
768
  try {
768
769
  const limit = historyPayload.limit ?? 200;
769
770
 
770
- // Resolve session key from various inputs
771
- let sessionKey = historyPayload.sessionKey || "main";
772
- if (!historyPayload.sessionKey && historyPayload.conversationId) {
773
- sessionKey = resolveSessionKey(historyPayload.conversationId);
771
+ let result: ChatHistoryResponse;
772
+
773
+ if (historyPayload.sessionKey) {
774
+ // If sessionKey is provided directly, use it as-is
775
+ log?.info(`[adp-openclaw] Using provided sessionKey: ${historyPayload.sessionKey}`);
776
+ result = await getChatHistory(historyPayload.sessionKey, {
777
+ limit,
778
+ log,
779
+ });
780
+ } else if (historyPayload.conversationId) {
781
+ // If conversationId is provided, merge old and new session histories
782
+ // Old format: agent:main:direct:{conversationId}
783
+ // New format: agent:main:adp-openclaw:direct:{conversationId}
784
+ log?.info(`[adp-openclaw] Merging old and new session histories for conversationId: ${historyPayload.conversationId}`);
785
+ result = await getMergedChatHistory(historyPayload.conversationId, {
786
+ limit,
787
+ log,
788
+ });
789
+ } else {
790
+ // Default to "main" session
791
+ log?.info(`[adp-openclaw] Using default session: main`);
792
+ result = await getChatHistory("main", {
793
+ limit,
794
+ log,
795
+ });
774
796
  }
775
797
 
776
- // Use CLI backend only
777
- const result: ChatHistoryResponse = await getChatHistory(sessionKey, {
778
- limit,
779
- log,
780
- });
781
-
782
798
  log?.info(`[adp-openclaw] Sending conv_response: ${result.messages.length} messages (backend=cli)`);
783
799
 
784
800
  // Send response back to GoServer
@@ -243,8 +243,13 @@ function readSessionTranscript(
243
243
  if (parsed.type === "session") {
244
244
  continue;
245
245
  }
246
- // Extract message if present
246
+ // Extract message if present, skip tool-related roles (toolResult, tool)
247
247
  if (parsed.message && parsed.message.role && parsed.message.content) {
248
+ // Filter out tool-related messages for cleaner display
249
+ const role = parsed.message.role;
250
+ if (role === "toolResult" || role === "tool") {
251
+ continue;
252
+ }
248
253
  const msg: OpenClawMessage = {
249
254
  role: parsed.message.role,
250
255
  content: extractMessageContent(parsed.message.content),
@@ -477,6 +482,85 @@ export async function getChatHistoryByConversationId(
477
482
  return getOpenClawChatHistory(sessionKey, options);
478
483
  }
479
484
 
485
+ /**
486
+ * Get merged chat history from both old and new sessionKey formats.
487
+ * Old format: agent:main:direct:{conversationId}
488
+ * New format: agent:main:adp-openclaw:direct:{conversationId}
489
+ *
490
+ * This function fetches history from both session keys and merges them by timestamp.
491
+ *
492
+ * @param conversationId - The conversation ID
493
+ * @param options - Optional parameters
494
+ * @returns Merged chat history sorted by timestamp
495
+ */
496
+ export async function getMergedChatHistory(
497
+ conversationId: string,
498
+ options?: {
499
+ limit?: number;
500
+ log?: PluginLogger;
501
+ },
502
+ ): Promise<ChatHistoryResponse> {
503
+ const { limit = 200, log } = options ?? {};
504
+
505
+ // Build both old and new session key formats
506
+ const oldSessionKey = `agent:main:direct:${conversationId}`;
507
+ const newSessionKey = `agent:main:adp-openclaw:direct:${conversationId}`;
508
+
509
+ log?.info?.(`[session-history] Fetching merged history for conversationId: ${conversationId}`);
510
+ log?.info?.(`[session-history] Old sessionKey: ${oldSessionKey}`);
511
+ log?.info?.(`[session-history] New sessionKey: ${newSessionKey}`);
512
+
513
+ // Fetch history from both session keys in parallel
514
+ const [oldResult, newResult] = await Promise.all([
515
+ getChatHistory(oldSessionKey, { limit, log }).catch((err) => {
516
+ log?.debug?.(`[session-history] Failed to fetch old session history: ${err}`);
517
+ return { sessionKey: oldSessionKey, messages: [], hasMore: false } as ChatHistoryResponse;
518
+ }),
519
+ getChatHistory(newSessionKey, { limit, log }).catch((err) => {
520
+ log?.debug?.(`[session-history] Failed to fetch new session history: ${err}`);
521
+ return { sessionKey: newSessionKey, messages: [], hasMore: false } as ChatHistoryResponse;
522
+ }),
523
+ ]);
524
+
525
+ log?.info?.(`[session-history] Old session messages: ${oldResult.messages.length}`);
526
+ log?.info?.(`[session-history] New session messages: ${newResult.messages.length}`);
527
+
528
+ // Merge messages from both sessions
529
+ const allMessages = [...oldResult.messages, ...newResult.messages];
530
+
531
+ // Sort by timestamp (ascending, oldest first)
532
+ allMessages.sort((a, b) => {
533
+ const timeA = a.timestamp ?? 0;
534
+ const timeB = b.timestamp ?? 0;
535
+ return timeA - timeB;
536
+ });
537
+
538
+ // Deduplicate by message id (if available) or content+timestamp
539
+ const seen = new Set<string>();
540
+ const dedupedMessages = allMessages.filter((msg) => {
541
+ // Create a unique key for deduplication
542
+ const key = msg.id || `${msg.role}:${msg.timestamp}:${msg.content.slice(0, 100)}`;
543
+ if (seen.has(key)) {
544
+ return false;
545
+ }
546
+ seen.add(key);
547
+ return true;
548
+ });
549
+
550
+ // Apply limit (return last N messages if exceeds limit)
551
+ const limitedMessages = dedupedMessages.length > limit
552
+ ? dedupedMessages.slice(-limit)
553
+ : dedupedMessages;
554
+
555
+ log?.info?.(`[session-history] Merged total: ${limitedMessages.length} messages`);
556
+
557
+ return {
558
+ sessionKey: newSessionKey, // Return new sessionKey as the primary
559
+ messages: limitedMessages,
560
+ hasMore: false,
561
+ };
562
+ }
563
+
480
564
  // ============================================================================
481
565
  // CLI-based Backend
482
566
  // ============================================================================