adp-openclaw 0.0.27 → 0.0.29
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 +96 -26
package/package.json
CHANGED
package/src/session-history.ts
CHANGED
|
@@ -519,18 +519,21 @@ function findOpenClawCli(config: SessionFileConfig): string | null {
|
|
|
519
519
|
}
|
|
520
520
|
|
|
521
521
|
/**
|
|
522
|
-
* Execute an openclaw CLI command and return the result
|
|
522
|
+
* Execute an openclaw CLI command and return the result.
|
|
523
|
+
* @param subcommands - Array of subcommands, e.g. ["gateway", "call", "chat.history"] or ["sessions"]
|
|
524
|
+
* @param args - Additional CLI flags, e.g. ["--json", "--params", '{"sessionKey":"main"}']
|
|
523
525
|
*/
|
|
524
526
|
async function executeClawCommand(
|
|
525
|
-
|
|
527
|
+
subcommands: string[],
|
|
526
528
|
args: string[],
|
|
527
529
|
options?: {
|
|
528
530
|
log?: PluginLogger;
|
|
529
531
|
timeout?: number;
|
|
530
532
|
gatewayUrl?: string;
|
|
533
|
+
gatewayToken?: string;
|
|
531
534
|
},
|
|
532
535
|
): Promise<string> {
|
|
533
|
-
const { log, timeout = 30000, gatewayUrl } = options ?? {};
|
|
536
|
+
const { log, timeout = 30000, gatewayUrl, gatewayToken } = options ?? {};
|
|
534
537
|
|
|
535
538
|
const cliPath = findOpenClawCli(sessionFileConfig);
|
|
536
539
|
if (!cliPath) {
|
|
@@ -540,22 +543,34 @@ async function executeClawCommand(
|
|
|
540
543
|
// Build command arguments
|
|
541
544
|
const fullArgs = [...args];
|
|
542
545
|
if (gatewayUrl) {
|
|
543
|
-
fullArgs.
|
|
546
|
+
fullArgs.push("--url", gatewayUrl);
|
|
547
|
+
}
|
|
548
|
+
if (gatewayToken) {
|
|
549
|
+
fullArgs.push("--token", gatewayToken);
|
|
544
550
|
}
|
|
545
551
|
|
|
546
|
-
|
|
552
|
+
const cmdStr = `${cliPath} ${subcommands.join(" ")} ${fullArgs.join(" ")}`;
|
|
553
|
+
log?.debug?.(`[session-history] Executing: ${cmdStr}`);
|
|
547
554
|
|
|
548
555
|
return new Promise((resolve, reject) => {
|
|
549
556
|
const parts = cliPath.split(" ");
|
|
550
557
|
const binary = parts[0];
|
|
551
558
|
const preArgs = parts.slice(1);
|
|
552
559
|
|
|
560
|
+
// Ensure node's bin directory is in PATH (fixes nvm/pnpm environments
|
|
561
|
+
// where the spawned shell may not have node in its PATH)
|
|
562
|
+
const nodeDir = path.dirname(process.execPath);
|
|
563
|
+
const currentPath = process.env.PATH || "";
|
|
564
|
+
const envPath = currentPath.includes(nodeDir)
|
|
565
|
+
? currentPath
|
|
566
|
+
: `${nodeDir}:${currentPath}`;
|
|
567
|
+
|
|
553
568
|
const spawnOpts: SpawnOptions = {
|
|
554
569
|
timeout,
|
|
555
|
-
env: { ...process.env },
|
|
570
|
+
env: { ...process.env, PATH: envPath },
|
|
556
571
|
};
|
|
557
572
|
|
|
558
|
-
const child = spawn(binary, [...preArgs,
|
|
573
|
+
const child = spawn(binary, [...preArgs, ...subcommands, ...fullArgs], spawnOpts);
|
|
559
574
|
|
|
560
575
|
let stdout = "";
|
|
561
576
|
let stderr = "";
|
|
@@ -586,7 +601,7 @@ async function executeClawCommand(
|
|
|
586
601
|
|
|
587
602
|
/**
|
|
588
603
|
* Get chat history via CLI command
|
|
589
|
-
* Uses: openclaw
|
|
604
|
+
* Uses: openclaw gateway call chat.history --params '{"sessionKey":"<key>","limit":<n>}' --json
|
|
590
605
|
*/
|
|
591
606
|
export async function getOpenClawChatHistoryViaCli(
|
|
592
607
|
sessionKey: string = "main",
|
|
@@ -601,19 +616,26 @@ export async function getOpenClawChatHistoryViaCli(
|
|
|
601
616
|
log?.info?.(`[session-history] Fetching chat history via CLI for session: ${sessionKey}`);
|
|
602
617
|
|
|
603
618
|
try {
|
|
604
|
-
|
|
619
|
+
// Build RPC params object
|
|
620
|
+
const params: Record<string, unknown> = {};
|
|
605
621
|
if (sessionKey) {
|
|
606
|
-
|
|
622
|
+
params.sessionKey = sessionKey;
|
|
607
623
|
}
|
|
608
624
|
if (limit) {
|
|
609
|
-
|
|
625
|
+
params.limit = limit;
|
|
610
626
|
}
|
|
611
627
|
|
|
612
|
-
//
|
|
613
|
-
const
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
628
|
+
// Use: openclaw gateway call chat.history --params '{...}' --json
|
|
629
|
+
const args = ["--json", "--params", JSON.stringify(params)];
|
|
630
|
+
|
|
631
|
+
const result = await executeClawCommand(
|
|
632
|
+
["gateway", "call", "chat.history"],
|
|
633
|
+
args,
|
|
634
|
+
{
|
|
635
|
+
log,
|
|
636
|
+
gatewayUrl: gatewayUrl || sessionFileConfig.gatewayUrl,
|
|
637
|
+
},
|
|
638
|
+
);
|
|
617
639
|
|
|
618
640
|
// Parse JSON result
|
|
619
641
|
const parsed = JSON.parse(result) as {
|
|
@@ -652,7 +674,8 @@ export async function getOpenClawChatHistoryViaCli(
|
|
|
652
674
|
|
|
653
675
|
/**
|
|
654
676
|
* List sessions via CLI command
|
|
655
|
-
* Uses: openclaw
|
|
677
|
+
* Uses: openclaw sessions --json (local, reads session store directly)
|
|
678
|
+
* Falls back to: openclaw gateway call sessions.list --params '{...}' --json (via Gateway RPC)
|
|
656
679
|
*/
|
|
657
680
|
export async function listOpenClawSessionsViaCli(
|
|
658
681
|
options?: {
|
|
@@ -667,22 +690,69 @@ export async function listOpenClawSessionsViaCli(
|
|
|
667
690
|
|
|
668
691
|
log?.info?.(`[session-history] Listing sessions via CLI`);
|
|
669
692
|
|
|
693
|
+
// Try local "openclaw sessions --json" first (no Gateway needed)
|
|
694
|
+
try {
|
|
695
|
+
const localArgs = ["--json"];
|
|
696
|
+
const result = await executeClawCommand(["sessions"], localArgs, { log });
|
|
697
|
+
|
|
698
|
+
const parsed = JSON.parse(result) as {
|
|
699
|
+
sessions?: Array<{
|
|
700
|
+
key: string;
|
|
701
|
+
sessionId?: string;
|
|
702
|
+
sessionFile?: string;
|
|
703
|
+
updatedAt?: number;
|
|
704
|
+
label?: string;
|
|
705
|
+
displayName?: string;
|
|
706
|
+
}>;
|
|
707
|
+
count?: number;
|
|
708
|
+
};
|
|
709
|
+
|
|
710
|
+
let sessions: OpenClawSession[] = (parsed.sessions ?? []).map((s) => ({
|
|
711
|
+
key: s.key,
|
|
712
|
+
sessionId: s.sessionId,
|
|
713
|
+
sessionFile: s.sessionFile,
|
|
714
|
+
updatedAt: s.updatedAt,
|
|
715
|
+
title: s.label || s.displayName,
|
|
716
|
+
}));
|
|
717
|
+
|
|
718
|
+
// Apply limit
|
|
719
|
+
if (limit && sessions.length > limit) {
|
|
720
|
+
sessions = sessions.slice(0, limit);
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
log?.info?.(`[session-history] Got ${sessions.length} sessions via CLI (local)`);
|
|
724
|
+
|
|
725
|
+
return {
|
|
726
|
+
sessions,
|
|
727
|
+
total: parsed.count ?? sessions.length,
|
|
728
|
+
};
|
|
729
|
+
} catch (localErr) {
|
|
730
|
+
log?.debug?.(`[session-history] Local sessions command failed, trying gateway RPC: ${localErr}`);
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// Fall back to Gateway RPC: openclaw gateway call sessions.list --params '{...}' --json
|
|
670
734
|
try {
|
|
671
|
-
const
|
|
735
|
+
const params: Record<string, unknown> = {};
|
|
672
736
|
if (limit) {
|
|
673
|
-
|
|
737
|
+
params.limit = limit;
|
|
674
738
|
}
|
|
675
739
|
if (includeGlobal) {
|
|
676
|
-
|
|
740
|
+
params.includeGlobal = true;
|
|
677
741
|
}
|
|
678
742
|
if (includeUnknown) {
|
|
679
|
-
|
|
743
|
+
params.includeUnknown = true;
|
|
680
744
|
}
|
|
681
745
|
|
|
682
|
-
const
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
746
|
+
const args = ["--json", "--params", JSON.stringify(params)];
|
|
747
|
+
|
|
748
|
+
const result = await executeClawCommand(
|
|
749
|
+
["gateway", "call", "sessions.list"],
|
|
750
|
+
args,
|
|
751
|
+
{
|
|
752
|
+
log,
|
|
753
|
+
gatewayUrl: gatewayUrl || sessionFileConfig.gatewayUrl,
|
|
754
|
+
},
|
|
755
|
+
);
|
|
686
756
|
|
|
687
757
|
const parsed = JSON.parse(result) as {
|
|
688
758
|
sessions?: Array<{
|
|
@@ -704,7 +774,7 @@ export async function listOpenClawSessionsViaCli(
|
|
|
704
774
|
title: s.label || s.displayName,
|
|
705
775
|
}));
|
|
706
776
|
|
|
707
|
-
log?.info?.(`[session-history] Got ${sessions.length} sessions via CLI`);
|
|
777
|
+
log?.info?.(`[session-history] Got ${sessions.length} sessions via CLI (gateway RPC)`);
|
|
708
778
|
|
|
709
779
|
return {
|
|
710
780
|
sessions,
|