adp-openclaw 0.0.66 → 0.0.68

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.66",
3
+ "version": "0.0.68",
4
4
  "description": "ADP-OpenClaw demo channel plugin (Go WebSocket backend)",
5
5
  "type": "module",
6
6
  "dependencies": {
package/src/monitor.ts CHANGED
@@ -390,7 +390,7 @@ async function connectAndHandle(params: ConnectParams): Promise<void> {
390
390
 
391
391
  // Build delivery target for cron jobs
392
392
  // Format: adp-openclaw:{userId} for direct delivery via this channel
393
- const cronDeliveryTarget = `adp-openclaw:${userIdentifier}`;
393
+ const cronDeliveryTarget = `${userIdentifier}`;
394
394
  const nowMs = Date.now();
395
395
 
396
396
  // Build BodyForAgent with context info for cron jobs (like QQBot does)
@@ -791,8 +791,23 @@ async function connectAndHandle(params: ConnectParams): Promise<void> {
791
791
 
792
792
  let result: ChatHistoryResponse;
793
793
 
794
- if (historyPayload.sessionKey) {
795
- // If sessionKey is provided directly, use it as-is
794
+ // Check if sessionKey is in old format: agent:main:direct:{id}
795
+ // We need to extract the id and merge old + new format histories
796
+ const oldFormatMatch = historyPayload.sessionKey?.match(/^agent:main:direct:(.+)$/);
797
+
798
+ if (oldFormatMatch) {
799
+ // Old format sessionKey detected, need to merge with new format
800
+ // Old: agent:main:direct:{conversationId}
801
+ // New: agent:main:adp-openclaw:direct:{conversationId}
802
+ const extractedId = oldFormatMatch[1];
803
+ log?.info(`[adp-openclaw] Old format sessionKey detected, extracting id: ${extractedId}`);
804
+ log?.info(`[adp-openclaw] Merging old and new session histories for id: ${extractedId}`);
805
+ result = await getMergedChatHistory(extractedId, {
806
+ limit,
807
+ log,
808
+ });
809
+ } else if (historyPayload.sessionKey) {
810
+ // Non-old-format sessionKey, use directly
796
811
  log?.info(`[adp-openclaw] Using provided sessionKey: ${historyPayload.sessionKey}`);
797
812
  result = await getChatHistory(historyPayload.sessionKey, {
798
813
  limit,
@@ -761,6 +761,53 @@ async function executeClawCommand(
761
761
  });
762
762
  }
763
763
 
764
+ /**
765
+ * Filter messages to only include those after the last /new or session reset.
766
+ * This handles the case where OpenClaw returns old session history after a /new command.
767
+ *
768
+ * We look for messages containing "New session started" which is the indicator
769
+ * that a new session was started via /new or /reset command.
770
+ */
771
+ function filterMessagesAfterLastNewSession(
772
+ messages: OpenClawMessage[],
773
+ log?: PluginLogger,
774
+ ): OpenClawMessage[] {
775
+ if (messages.length === 0) {
776
+ return messages;
777
+ }
778
+
779
+ // Find the index of the last "New session started" message
780
+ // This message is typically from the assistant after processing /new
781
+ let lastNewSessionIndex = -1;
782
+
783
+ for (let i = messages.length - 1; i >= 0; i--) {
784
+ const msg = messages[i];
785
+ // Check for the "New session started" indicator from OpenClaw
786
+ // This appears in assistant messages after /new command
787
+ if (
788
+ msg.role === "assistant" &&
789
+ msg.content &&
790
+ (msg.content.includes("New session started") ||
791
+ msg.content.includes("✅ New session started"))
792
+ ) {
793
+ lastNewSessionIndex = i;
794
+ break;
795
+ }
796
+ }
797
+
798
+ if (lastNewSessionIndex === -1) {
799
+ // No /new found, return all messages
800
+ log?.debug?.(`[session-history] No "New session started" marker found, returning all ${messages.length} messages`);
801
+ return messages;
802
+ }
803
+
804
+ // Return messages from the last "New session started" onwards
805
+ const filteredMessages = messages.slice(lastNewSessionIndex);
806
+ log?.info?.(`[session-history] Filtered messages after last /new: ${messages.length} -> ${filteredMessages.length} messages`);
807
+
808
+ return filteredMessages;
809
+ }
810
+
764
811
  /**
765
812
  * Get chat history via CLI command
766
813
  * Uses: openclaw gateway call chat.history --params '{"sessionKey":"<key>","limit":<n>}' --json
@@ -771,9 +818,10 @@ export async function getOpenClawChatHistoryViaCli(
771
818
  limit?: number;
772
819
  log?: PluginLogger;
773
820
  gatewayUrl?: string;
821
+ filterAfterNewSession?: boolean; // Default true: filter out messages before last /new
774
822
  },
775
823
  ): Promise<ChatHistoryResponse> {
776
- const { limit = 200, log, gatewayUrl } = options ?? {};
824
+ const { limit = 200, log, gatewayUrl, filterAfterNewSession = true } = options ?? {};
777
825
 
778
826
  log?.info?.(`[session-history] Fetching chat history via CLI for session: ${sessionKey}`);
779
827
 
@@ -807,9 +855,10 @@ export async function getOpenClawChatHistoryViaCli(
807
855
  id?: string;
808
856
  timestamp?: string;
809
857
  }>;
858
+ sessionId?: string; // OpenClaw may return the current sessionId
810
859
  };
811
860
 
812
- const messages: OpenClawMessage[] = (parsed.messages ?? []).map((msg) => ({
861
+ let messages: OpenClawMessage[] = (parsed.messages ?? []).map((msg) => ({
813
862
  role: msg.role as "user" | "assistant" | "system",
814
863
  content: typeof msg.content === "string"
815
864
  ? msg.content
@@ -823,6 +872,12 @@ export async function getOpenClawChatHistoryViaCli(
823
872
 
824
873
  log?.info?.(`[session-history] Got ${messages.length} messages via CLI`);
825
874
 
875
+ // Filter out messages before the last /new session if enabled
876
+ // This handles the case where OpenClaw returns old session history
877
+ if (filterAfterNewSession) {
878
+ messages = filterMessagesAfterLastNewSession(messages, log);
879
+ }
880
+
826
881
  return {
827
882
  sessionKey,
828
883
  messages,