adp-openclaw 0.0.67 → 0.0.69
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 +1 -1
- package/src/session-history.ts +68 -4
package/package.json
CHANGED
package/src/session-history.ts
CHANGED
|
@@ -498,9 +498,10 @@ export async function getMergedChatHistory(
|
|
|
498
498
|
options?: {
|
|
499
499
|
limit?: number;
|
|
500
500
|
log?: PluginLogger;
|
|
501
|
+
filterAfterNewSession?: boolean; // Default true: filter out messages before last /new
|
|
501
502
|
},
|
|
502
503
|
): Promise<ChatHistoryResponse> {
|
|
503
|
-
const { limit = 200, log } = options ?? {};
|
|
504
|
+
const { limit = 200, log, filterAfterNewSession = true } = options ?? {};
|
|
504
505
|
|
|
505
506
|
// Build both old and new session key formats
|
|
506
507
|
const oldSessionKey = `agent:main:direct:${conversationId}`;
|
|
@@ -511,6 +512,7 @@ export async function getMergedChatHistory(
|
|
|
511
512
|
log?.info?.(`[session-history] New sessionKey: ${newSessionKey}`);
|
|
512
513
|
|
|
513
514
|
// Fetch history from both session keys in parallel
|
|
515
|
+
// Note: getChatHistory already applies filterAfterNewSession for each session individually
|
|
514
516
|
const [oldResult, newResult] = await Promise.all([
|
|
515
517
|
getChatHistory(oldSessionKey, { limit, log }).catch((err) => {
|
|
516
518
|
log?.debug?.(`[session-history] Failed to fetch old session history: ${err}`);
|
|
@@ -537,7 +539,7 @@ export async function getMergedChatHistory(
|
|
|
537
539
|
|
|
538
540
|
// Deduplicate by message id (if available) or content+timestamp
|
|
539
541
|
const seen = new Set<string>();
|
|
540
|
-
|
|
542
|
+
let dedupedMessages = allMessages.filter((msg) => {
|
|
541
543
|
// Create a unique key for deduplication
|
|
542
544
|
const key = msg.id || `${msg.role}:${msg.timestamp}:${msg.content.slice(0, 100)}`;
|
|
543
545
|
if (seen.has(key)) {
|
|
@@ -547,6 +549,13 @@ export async function getMergedChatHistory(
|
|
|
547
549
|
return true;
|
|
548
550
|
});
|
|
549
551
|
|
|
552
|
+
// Filter out messages before the last /new session on the MERGED result
|
|
553
|
+
// This is important because the /new might be in the old sessionKey but we still
|
|
554
|
+
// want to filter the combined history based on the latest /new across both sessions
|
|
555
|
+
if (filterAfterNewSession) {
|
|
556
|
+
dedupedMessages = filterMessagesAfterLastNewSession(dedupedMessages, log);
|
|
557
|
+
}
|
|
558
|
+
|
|
550
559
|
// Apply limit (return last N messages if exceeds limit)
|
|
551
560
|
const limitedMessages = dedupedMessages.length > limit
|
|
552
561
|
? dedupedMessages.slice(-limit)
|
|
@@ -761,6 +770,53 @@ async function executeClawCommand(
|
|
|
761
770
|
});
|
|
762
771
|
}
|
|
763
772
|
|
|
773
|
+
/**
|
|
774
|
+
* Filter messages to only include those after the last /new or session reset.
|
|
775
|
+
* This handles the case where OpenClaw returns old session history after a /new command.
|
|
776
|
+
*
|
|
777
|
+
* We look for messages containing "New session started" which is the indicator
|
|
778
|
+
* that a new session was started via /new or /reset command.
|
|
779
|
+
*/
|
|
780
|
+
function filterMessagesAfterLastNewSession(
|
|
781
|
+
messages: OpenClawMessage[],
|
|
782
|
+
log?: PluginLogger,
|
|
783
|
+
): OpenClawMessage[] {
|
|
784
|
+
if (messages.length === 0) {
|
|
785
|
+
return messages;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
// Find the index of the last "New session started" message
|
|
789
|
+
// This message is typically from the assistant after processing /new
|
|
790
|
+
let lastNewSessionIndex = -1;
|
|
791
|
+
|
|
792
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
793
|
+
const msg = messages[i];
|
|
794
|
+
// Check for the "New session started" indicator from OpenClaw
|
|
795
|
+
// This appears in assistant messages after /new command
|
|
796
|
+
if (
|
|
797
|
+
msg.role === "assistant" &&
|
|
798
|
+
msg.content &&
|
|
799
|
+
(msg.content.includes("New session started") ||
|
|
800
|
+
msg.content.includes("✅ New session started"))
|
|
801
|
+
) {
|
|
802
|
+
lastNewSessionIndex = i;
|
|
803
|
+
break;
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
if (lastNewSessionIndex === -1) {
|
|
808
|
+
// No /new found, return all messages
|
|
809
|
+
log?.debug?.(`[session-history] No "New session started" marker found, returning all ${messages.length} messages`);
|
|
810
|
+
return messages;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
// Return messages from the last "New session started" onwards
|
|
814
|
+
const filteredMessages = messages.slice(lastNewSessionIndex);
|
|
815
|
+
log?.info?.(`[session-history] Filtered messages after last /new: ${messages.length} -> ${filteredMessages.length} messages`);
|
|
816
|
+
|
|
817
|
+
return filteredMessages;
|
|
818
|
+
}
|
|
819
|
+
|
|
764
820
|
/**
|
|
765
821
|
* Get chat history via CLI command
|
|
766
822
|
* Uses: openclaw gateway call chat.history --params '{"sessionKey":"<key>","limit":<n>}' --json
|
|
@@ -771,9 +827,10 @@ export async function getOpenClawChatHistoryViaCli(
|
|
|
771
827
|
limit?: number;
|
|
772
828
|
log?: PluginLogger;
|
|
773
829
|
gatewayUrl?: string;
|
|
830
|
+
filterAfterNewSession?: boolean; // Default true: filter out messages before last /new
|
|
774
831
|
},
|
|
775
832
|
): Promise<ChatHistoryResponse> {
|
|
776
|
-
const { limit = 200, log, gatewayUrl } = options ?? {};
|
|
833
|
+
const { limit = 200, log, gatewayUrl, filterAfterNewSession = true } = options ?? {};
|
|
777
834
|
|
|
778
835
|
log?.info?.(`[session-history] Fetching chat history via CLI for session: ${sessionKey}`);
|
|
779
836
|
|
|
@@ -807,9 +864,10 @@ export async function getOpenClawChatHistoryViaCli(
|
|
|
807
864
|
id?: string;
|
|
808
865
|
timestamp?: string;
|
|
809
866
|
}>;
|
|
867
|
+
sessionId?: string; // OpenClaw may return the current sessionId
|
|
810
868
|
};
|
|
811
869
|
|
|
812
|
-
|
|
870
|
+
let messages: OpenClawMessage[] = (parsed.messages ?? []).map((msg) => ({
|
|
813
871
|
role: msg.role as "user" | "assistant" | "system",
|
|
814
872
|
content: typeof msg.content === "string"
|
|
815
873
|
? msg.content
|
|
@@ -823,6 +881,12 @@ export async function getOpenClawChatHistoryViaCli(
|
|
|
823
881
|
|
|
824
882
|
log?.info?.(`[session-history] Got ${messages.length} messages via CLI`);
|
|
825
883
|
|
|
884
|
+
// Filter out messages before the last /new session if enabled
|
|
885
|
+
// This handles the case where OpenClaw returns old session history
|
|
886
|
+
if (filterAfterNewSession) {
|
|
887
|
+
messages = filterMessagesAfterLastNewSession(messages, log);
|
|
888
|
+
}
|
|
889
|
+
|
|
826
890
|
return {
|
|
827
891
|
sessionKey,
|
|
828
892
|
messages,
|