@privateclaw/privateclaw 0.1.31 → 0.1.33
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/README.md +14 -9
- package/dist/audio-transcriber.js +98 -20
- package/dist/audio-transcriber.js.map +1 -1
- package/dist/bridges/openclaw-agent-bridge.d.ts +7 -0
- package/dist/bridges/openclaw-agent-bridge.js +71 -25
- package/dist/bridges/openclaw-agent-bridge.js.map +1 -1
- package/dist/channel.d.ts +14 -0
- package/dist/channel.js +103 -0
- package/dist/channel.js.map +1 -0
- package/dist/cli.js +16 -2
- package/dist/cli.js.map +1 -1
- package/dist/compat/openclaw.d.ts +231 -2
- package/dist/compat/openclaw.js +1 -0
- package/dist/compat/openclaw.js.map +1 -1
- package/dist/openclaw-plugin.js +199 -4
- package/dist/openclaw-plugin.js.map +1 -1
- package/dist/pair-session.d.ts +32 -0
- package/dist/pair-session.js +115 -41
- package/dist/pair-session.js.map +1 -1
- package/dist/provider.d.ts +4 -1
- package/dist/provider.js +109 -4
- package/dist/provider.js.map +1 -1
- package/dist/session-control.d.ts +15 -1
- package/dist/session-control.js +184 -5
- package/dist/session-control.js.map +1 -1
- package/dist/types.d.ts +20 -1
- package/openclaw.plugin.json +3 -2
- package/package.json +1 -1
- package/src/audio-transcriber.test.ts +152 -0
- package/src/audio-transcriber.ts +116 -23
- package/src/bridges/openclaw-agent-bridge.test.ts +154 -0
- package/src/bridges/openclaw-agent-bridge.ts +97 -27
- package/src/channel.ts +130 -0
- package/src/cli.ts +17 -1
- package/src/compat/openclaw.ts +282 -2
- package/src/openclaw-plugin.test.ts +392 -3
- package/src/openclaw-plugin.ts +262 -3
- package/src/pair-session.test.ts +119 -0
- package/src/pair-session.ts +144 -47
- package/src/provider.test.ts +266 -0
- package/src/provider.ts +133 -4
- package/src/session-control.test.ts +87 -0
- package/src/session-control.ts +252 -5
- package/src/types.ts +23 -0
package/src/openclaw-plugin.ts
CHANGED
|
@@ -26,9 +26,18 @@ import {
|
|
|
26
26
|
} from "./pair-daemon.js";
|
|
27
27
|
import { WebhookBridge } from "./bridges/webhook-bridge.js";
|
|
28
28
|
import {
|
|
29
|
+
createPrivateClawChannelPlugin,
|
|
30
|
+
PRIVATECLAW_OPENCLAW_ACCOUNT_ID,
|
|
31
|
+
PRIVATECLAW_OPENCLAW_CHANNEL_ID,
|
|
32
|
+
} from "./channel.js";
|
|
33
|
+
import {
|
|
34
|
+
type OpenClawConfigCompat,
|
|
35
|
+
type OpenClawChannelPluginCompat,
|
|
29
36
|
type OpenClawExtensionPluginCompat,
|
|
37
|
+
type OpenClawMsgContextCompat,
|
|
30
38
|
type OpenClawPluginApiCompat,
|
|
31
39
|
type OpenClawPluginCommandContextCompat,
|
|
40
|
+
type OpenClawPluginRuntimeCompat,
|
|
32
41
|
type ReplyPayloadCompat,
|
|
33
42
|
privateClawConfigSchema,
|
|
34
43
|
} from "./compat/openclaw.js";
|
|
@@ -41,6 +50,8 @@ import {
|
|
|
41
50
|
buildManagedSessionsReportLines,
|
|
42
51
|
closeManagedSessionsFromStateDir,
|
|
43
52
|
closeManagedSessionFromStateDir,
|
|
53
|
+
deliverManagedSessionOutboundFromStateDir,
|
|
54
|
+
dispatchRoutedAppMessageToPluginServiceFromStateDir,
|
|
44
55
|
getManagedSessionQrBundleFromStateDir,
|
|
45
56
|
isManagedSessionQrLegacyResult,
|
|
46
57
|
kickManagedParticipantFromStateDir,
|
|
@@ -80,7 +91,9 @@ import type {
|
|
|
80
91
|
PrivateClawAgentBridge,
|
|
81
92
|
PrivateClawInviteBundle,
|
|
82
93
|
PrivateClawManagedSession,
|
|
94
|
+
PrivateClawOpenClawOutboundMessage,
|
|
83
95
|
PrivateClawProviderOptions,
|
|
96
|
+
PrivateClawRoutedAppMessage,
|
|
84
97
|
PrivateClawVerboseController,
|
|
85
98
|
} from "./types.js";
|
|
86
99
|
|
|
@@ -151,6 +164,7 @@ interface PrivateClawSessionsQrCliOptions {
|
|
|
151
164
|
|
|
152
165
|
const DEFAULT_PROVIDER_LABEL = "PrivateClaw";
|
|
153
166
|
const DEFAULT_BRIDGE_MODE: PrivateClawBridgeMode = "openclaw-agent";
|
|
167
|
+
const DEFAULT_OPENCLAW_AGENT_CHANNEL = "privateclaw";
|
|
154
168
|
const QQ_INLINE_IMAGE_REPLY_CHANNELS = new Set(["qqbot", "qq", "qqguild", "qq-guild"]);
|
|
155
169
|
const ALLOWED_THINKING_LEVELS = new Set<OpenClawThinkingLevel>([
|
|
156
170
|
"off",
|
|
@@ -293,7 +307,8 @@ function resolvePluginConfig(
|
|
|
293
307
|
readString(process.env.PRIVATECLAW_OPENCLAW_AGENT_ID);
|
|
294
308
|
const openclawAgentChannel =
|
|
295
309
|
readString(pluginConfig?.openclawAgentChannel) ??
|
|
296
|
-
readString(process.env.PRIVATECLAW_OPENCLAW_AGENT_CHANNEL)
|
|
310
|
+
readString(process.env.PRIVATECLAW_OPENCLAW_AGENT_CHANNEL) ??
|
|
311
|
+
DEFAULT_OPENCLAW_AGENT_CHANNEL;
|
|
297
312
|
const openclawAgentLocal =
|
|
298
313
|
readBoolean(pluginConfig?.openclawAgentLocal) ??
|
|
299
314
|
readBoolean(process.env.PRIVATECLAW_OPENCLAW_AGENT_LOCAL);
|
|
@@ -654,6 +669,84 @@ function buildDaemonEnvFromPluginConfig(
|
|
|
654
669
|
};
|
|
655
670
|
}
|
|
656
671
|
|
|
672
|
+
function isOpenClawPluginRuntimeCompatible(
|
|
673
|
+
runtime: unknown,
|
|
674
|
+
): runtime is OpenClawPluginRuntimeCompat {
|
|
675
|
+
if (!runtime || typeof runtime !== "object") {
|
|
676
|
+
return false;
|
|
677
|
+
}
|
|
678
|
+
const channel = (runtime as { channel?: Record<string, unknown> }).channel;
|
|
679
|
+
const reply =
|
|
680
|
+
channel && typeof channel === "object"
|
|
681
|
+
? (channel.reply as Record<string, unknown> | undefined)
|
|
682
|
+
: undefined;
|
|
683
|
+
const routing =
|
|
684
|
+
channel && typeof channel === "object"
|
|
685
|
+
? (channel.routing as Record<string, unknown> | undefined)
|
|
686
|
+
: undefined;
|
|
687
|
+
const session =
|
|
688
|
+
channel && typeof channel === "object"
|
|
689
|
+
? (channel.session as Record<string, unknown> | undefined)
|
|
690
|
+
: undefined;
|
|
691
|
+
return (
|
|
692
|
+
typeof reply?.dispatchReplyWithBufferedBlockDispatcher === "function" &&
|
|
693
|
+
typeof reply.finalizeInboundContext === "function" &&
|
|
694
|
+
typeof routing?.resolveAgentRoute === "function" &&
|
|
695
|
+
typeof session?.resolveStorePath === "function" &&
|
|
696
|
+
typeof session.recordInboundSession === "function"
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
function buildBasePrivateClawInboundContext(
|
|
701
|
+
message: PrivateClawRoutedAppMessage,
|
|
702
|
+
): OpenClawMsgContextCompat {
|
|
703
|
+
const rawText = readString(message.payload.text) ?? "";
|
|
704
|
+
const senderId =
|
|
705
|
+
readString(message.participant?.appId) ??
|
|
706
|
+
readString(message.payload.appId) ??
|
|
707
|
+
message.sessionId;
|
|
708
|
+
const senderName =
|
|
709
|
+
readString(message.participant?.displayName) ??
|
|
710
|
+
readString(message.payload.displayName) ??
|
|
711
|
+
"PrivateClaw user";
|
|
712
|
+
return {
|
|
713
|
+
Body: rawText,
|
|
714
|
+
BodyForAgent:
|
|
715
|
+
message.groupMode && message.participant
|
|
716
|
+
? `${message.participant.displayName}: ${rawText}`
|
|
717
|
+
: rawText,
|
|
718
|
+
RawBody: rawText,
|
|
719
|
+
CommandBody: rawText,
|
|
720
|
+
BodyForCommands: rawText,
|
|
721
|
+
From: `${PRIVATECLAW_OPENCLAW_CHANNEL_ID}:${senderId}`,
|
|
722
|
+
To: message.sessionId,
|
|
723
|
+
AccountId: PRIVATECLAW_OPENCLAW_ACCOUNT_ID,
|
|
724
|
+
MessageSid: message.payload.clientMessageId,
|
|
725
|
+
MessageSidFull: message.payload.clientMessageId,
|
|
726
|
+
Timestamp: Number.isNaN(Date.parse(message.payload.sentAt))
|
|
727
|
+
? Date.now()
|
|
728
|
+
: Date.parse(message.payload.sentAt),
|
|
729
|
+
ChatType: message.groupMode ? "group" : "direct",
|
|
730
|
+
ConversationLabel: readString(message.sessionLabel) ?? "PrivateClaw",
|
|
731
|
+
SenderId: senderId,
|
|
732
|
+
SenderName: senderName,
|
|
733
|
+
Provider: PRIVATECLAW_OPENCLAW_CHANNEL_ID,
|
|
734
|
+
Surface: PRIVATECLAW_OPENCLAW_CHANNEL_ID,
|
|
735
|
+
CommandAuthorized: true,
|
|
736
|
+
NativeChannelId: message.sessionId,
|
|
737
|
+
OriginatingChannel: PRIVATECLAW_OPENCLAW_CHANNEL_ID,
|
|
738
|
+
OriginatingTo: message.sessionId,
|
|
739
|
+
ExplicitDeliverRoute: true,
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
function readConfiguredSessionStorePath(
|
|
744
|
+
cfg: OpenClawConfigCompat,
|
|
745
|
+
): string | undefined {
|
|
746
|
+
const rawStore = (cfg as { session?: { store?: unknown } }).session?.store;
|
|
747
|
+
return readString(rawStore);
|
|
748
|
+
}
|
|
749
|
+
|
|
657
750
|
class PrivateClawPluginRuntime {
|
|
658
751
|
private readonly providerEntries = new Map<
|
|
659
752
|
string,
|
|
@@ -667,6 +760,8 @@ class PrivateClawPluginRuntime {
|
|
|
667
760
|
private controlServer: PrivateClawSessionControlServer | undefined;
|
|
668
761
|
private readonly defaultRelayBaseUrl: string;
|
|
669
762
|
private readonly verboseController: PrivateClawVerboseController | undefined;
|
|
763
|
+
private openClawRuntime: OpenClawPluginRuntimeCompat | undefined;
|
|
764
|
+
private readonly channelPlugin: OpenClawChannelPluginCompat;
|
|
670
765
|
|
|
671
766
|
constructor(
|
|
672
767
|
private readonly providerOptions: PrivateClawProviderOptions,
|
|
@@ -679,6 +774,11 @@ class PrivateClawPluginRuntime {
|
|
|
679
774
|
readString(defaultRelayBaseUrl) ??
|
|
680
775
|
inferRelayBaseUrlFromAppWsUrl(providerOptions.appWsUrl);
|
|
681
776
|
this.verboseController = verboseController ?? providerOptions.verboseController;
|
|
777
|
+
this.channelPlugin = createPrivateClawChannelPlugin({
|
|
778
|
+
deliverOutboundMessage: async (sessionId, payload) => {
|
|
779
|
+
await this.deliverOpenClawOutboundMessage(sessionId, payload);
|
|
780
|
+
},
|
|
781
|
+
});
|
|
682
782
|
}
|
|
683
783
|
|
|
684
784
|
private async withVerboseLogging<T>(
|
|
@@ -705,6 +805,16 @@ class PrivateClawPluginRuntime {
|
|
|
705
805
|
this.rootConfig = config;
|
|
706
806
|
}
|
|
707
807
|
|
|
808
|
+
setOpenClawRuntime(runtime: unknown): void {
|
|
809
|
+
this.openClawRuntime = isOpenClawPluginRuntimeCompatible(runtime)
|
|
810
|
+
? runtime
|
|
811
|
+
: undefined;
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
getChannelPlugin(): OpenClawChannelPluginCompat {
|
|
815
|
+
return this.channelPlugin;
|
|
816
|
+
}
|
|
817
|
+
|
|
708
818
|
private ensureStateDir(): string {
|
|
709
819
|
this.stateDir = resolvePrivateClawStateDir(this.stateDir);
|
|
710
820
|
return this.stateDir;
|
|
@@ -739,6 +849,8 @@ class PrivateClawPluginRuntime {
|
|
|
739
849
|
...this.providerOptions,
|
|
740
850
|
providerWsUrl: relay.providerWsUrl,
|
|
741
851
|
appWsUrl: relay.appWsUrl,
|
|
852
|
+
appMessageRouter: async (message) =>
|
|
853
|
+
this.routeOrForwardAppMessage(message),
|
|
742
854
|
...(audioTranscriber ? { audioTranscriber } : {}),
|
|
743
855
|
},
|
|
744
856
|
};
|
|
@@ -756,6 +868,139 @@ class PrivateClawPluginRuntime {
|
|
|
756
868
|
return entry.provider;
|
|
757
869
|
}
|
|
758
870
|
|
|
871
|
+
private findLocalProviderBySession(
|
|
872
|
+
sessionId: string,
|
|
873
|
+
): PrivateClawProvider | undefined {
|
|
874
|
+
for (const entry of this.providerEntries.values()) {
|
|
875
|
+
if (entry.provider?.hasManagedSession(sessionId)) {
|
|
876
|
+
return entry.provider;
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
return undefined;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
private async deliverOpenClawOutboundMessage(
|
|
883
|
+
sessionId: string,
|
|
884
|
+
payload: PrivateClawOpenClawOutboundMessage,
|
|
885
|
+
): Promise<void> {
|
|
886
|
+
const provider = this.findLocalProviderBySession(sessionId);
|
|
887
|
+
if (provider) {
|
|
888
|
+
await provider.deliverOpenClawOutboundMessage(sessionId, payload);
|
|
889
|
+
return;
|
|
890
|
+
}
|
|
891
|
+
await deliverManagedSessionOutboundFromStateDir({
|
|
892
|
+
stateDir: this.ensureStateDir(),
|
|
893
|
+
sessionId,
|
|
894
|
+
payload,
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
private async routeAppMessageToOpenClaw(
|
|
899
|
+
message: PrivateClawRoutedAppMessage,
|
|
900
|
+
): Promise<void> {
|
|
901
|
+
const runtime = this.openClawRuntime?.channel;
|
|
902
|
+
if (!runtime) {
|
|
903
|
+
throw new Error(
|
|
904
|
+
"The active PrivateClaw host does not expose OpenClaw channel runtime helpers.",
|
|
905
|
+
);
|
|
906
|
+
}
|
|
907
|
+
const cfg = (this.rootConfig ?? {}) as OpenClawConfigCompat;
|
|
908
|
+
const baseCtx = buildBasePrivateClawInboundContext(message);
|
|
909
|
+
const route = runtime.routing.resolveAgentRoute({
|
|
910
|
+
cfg,
|
|
911
|
+
ctx: baseCtx,
|
|
912
|
+
});
|
|
913
|
+
const finalizedCtx = runtime.reply.finalizeInboundContext(
|
|
914
|
+
{
|
|
915
|
+
...baseCtx,
|
|
916
|
+
SessionKey: route.sessionKey,
|
|
917
|
+
...(route.mainSessionKey !== route.sessionKey
|
|
918
|
+
? { ParentSessionKey: route.mainSessionKey }
|
|
919
|
+
: {}),
|
|
920
|
+
},
|
|
921
|
+
{
|
|
922
|
+
forceBodyForAgent: true,
|
|
923
|
+
forceBodyForCommands: true,
|
|
924
|
+
forceChatType: true,
|
|
925
|
+
forceConversationLabel: true,
|
|
926
|
+
},
|
|
927
|
+
);
|
|
928
|
+
const storePath = runtime.session.resolveStorePath(
|
|
929
|
+
readConfiguredSessionStorePath(cfg),
|
|
930
|
+
{
|
|
931
|
+
agentId: route.agentId,
|
|
932
|
+
},
|
|
933
|
+
);
|
|
934
|
+
await runtime.session.recordInboundSession({
|
|
935
|
+
storePath,
|
|
936
|
+
sessionKey: route.sessionKey,
|
|
937
|
+
ctx: finalizedCtx,
|
|
938
|
+
createIfMissing: true,
|
|
939
|
+
updateLastRoute: {
|
|
940
|
+
sessionKey:
|
|
941
|
+
route.lastRoutePolicy === "main"
|
|
942
|
+
? route.mainSessionKey
|
|
943
|
+
: route.sessionKey,
|
|
944
|
+
channel: PRIVATECLAW_OPENCLAW_CHANNEL_ID,
|
|
945
|
+
to: message.sessionId,
|
|
946
|
+
accountId: PRIVATECLAW_OPENCLAW_ACCOUNT_ID,
|
|
947
|
+
},
|
|
948
|
+
onRecordError: (error) => {
|
|
949
|
+
this.providerOptions.onLog?.(
|
|
950
|
+
`[privateclaw] runtime_session_record_error session=${message.sessionId} message=${formatCommandError(error)}`,
|
|
951
|
+
);
|
|
952
|
+
},
|
|
953
|
+
});
|
|
954
|
+
await runtime.reply.dispatchReplyWithBufferedBlockDispatcher({
|
|
955
|
+
ctx: finalizedCtx,
|
|
956
|
+
cfg,
|
|
957
|
+
dispatcherOptions: {
|
|
958
|
+
deliver: async (payload) => {
|
|
959
|
+
const replyToId =
|
|
960
|
+
typeof payload.replyToId === "string" && payload.replyToId.trim() !== ""
|
|
961
|
+
? payload.replyToId
|
|
962
|
+
: message.payload.clientMessageId;
|
|
963
|
+
await this.deliverOpenClawOutboundMessage(message.sessionId, {
|
|
964
|
+
...(payload.text?.trim() ? { text: payload.text.trim() } : {}),
|
|
965
|
+
...(payload.mediaUrl?.trim()
|
|
966
|
+
? { mediaUrl: payload.mediaUrl.trim() }
|
|
967
|
+
: {}),
|
|
968
|
+
...(payload.mediaUrls?.length
|
|
969
|
+
? { mediaUrls: payload.mediaUrls }
|
|
970
|
+
: {}),
|
|
971
|
+
replyToId,
|
|
972
|
+
});
|
|
973
|
+
},
|
|
974
|
+
onError: (error, info) => {
|
|
975
|
+
this.providerOptions.onLog?.(
|
|
976
|
+
`[privateclaw] runtime_dispatch_error kind=${info.kind} message=${formatCommandError(error)}`,
|
|
977
|
+
);
|
|
978
|
+
},
|
|
979
|
+
},
|
|
980
|
+
});
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
private async routeOrForwardAppMessage(
|
|
984
|
+
message: PrivateClawRoutedAppMessage,
|
|
985
|
+
): Promise<boolean> {
|
|
986
|
+
if (this.openClawRuntime?.channel) {
|
|
987
|
+
await this.routeAppMessageToOpenClaw(message);
|
|
988
|
+
return true;
|
|
989
|
+
}
|
|
990
|
+
try {
|
|
991
|
+
await dispatchRoutedAppMessageToPluginServiceFromStateDir({
|
|
992
|
+
stateDir: this.ensureStateDir(),
|
|
993
|
+
message,
|
|
994
|
+
});
|
|
995
|
+
return true;
|
|
996
|
+
} catch (error) {
|
|
997
|
+
this.providerOptions.onLog?.(
|
|
998
|
+
`[privateclaw] runtime routing unavailable, falling back to bridge for ${message.sessionId}: ${formatCommandError(error)}`,
|
|
999
|
+
);
|
|
1000
|
+
return false;
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
|
|
759
1004
|
private listLocalManagedSessions(): PrivateClawManagedSession[] {
|
|
760
1005
|
return [...this.providerEntries.values()].flatMap((entry) =>
|
|
761
1006
|
entry.provider?.listManagedSessions() ?? [],
|
|
@@ -838,6 +1083,10 @@ class PrivateClawPluginRuntime {
|
|
|
838
1083
|
this.closeLocalManagedSession(sessionId, reason),
|
|
839
1084
|
kickGroupParticipant: (sessionId, appId, reason) =>
|
|
840
1085
|
this.kickLocalManagedParticipant(sessionId, appId, reason),
|
|
1086
|
+
deliverOpenClawOutboundMessage: (sessionId, payload) =>
|
|
1087
|
+
this.deliverOpenClawOutboundMessage(sessionId, payload),
|
|
1088
|
+
routeAppMessageToOpenClaw: (message) =>
|
|
1089
|
+
this.routeAppMessageToOpenClaw(message),
|
|
841
1090
|
},
|
|
842
1091
|
stateDir: this.ensureStateDir(),
|
|
843
1092
|
kind,
|
|
@@ -848,13 +1097,17 @@ class PrivateClawPluginRuntime {
|
|
|
848
1097
|
await this.controlServer.start();
|
|
849
1098
|
}
|
|
850
1099
|
|
|
1100
|
+
async ensurePluginServiceControl(): Promise<void> {
|
|
1101
|
+
await this.ensureControlServer("plugin-service");
|
|
1102
|
+
}
|
|
1103
|
+
|
|
851
1104
|
async createInviteBundle(params?: {
|
|
852
1105
|
ttlMs?: number;
|
|
853
1106
|
label?: string;
|
|
854
1107
|
groupMode?: boolean;
|
|
855
1108
|
relayBaseUrl?: string;
|
|
856
1109
|
}): Promise<PrivateClawInviteBundle> {
|
|
857
|
-
await this.
|
|
1110
|
+
await this.ensurePluginServiceControl();
|
|
858
1111
|
return this.getProvider(params?.relayBaseUrl).createInviteBundle(
|
|
859
1112
|
{
|
|
860
1113
|
...(typeof params?.ttlMs === "number"
|
|
@@ -1119,18 +1372,24 @@ function createPluginDefinition(
|
|
|
1119
1372
|
configSchema: privateClawConfigSchema,
|
|
1120
1373
|
register(api: OpenClawPluginApiCompat) {
|
|
1121
1374
|
const runtime = runtimeFactory(api);
|
|
1375
|
+
runtime.setOpenClawRuntime(api.runtime);
|
|
1122
1376
|
|
|
1123
1377
|
api.registerService?.({
|
|
1124
1378
|
id: "privateclaw-provider",
|
|
1125
|
-
start: ({ stateDir, config }) => {
|
|
1379
|
+
start: async ({ stateDir, config }) => {
|
|
1126
1380
|
runtime.setStateDir(stateDir);
|
|
1127
1381
|
runtime.setRootConfig(config);
|
|
1382
|
+
await runtime.ensurePluginServiceControl();
|
|
1128
1383
|
},
|
|
1129
1384
|
stop: async () => {
|
|
1130
1385
|
await runtime.dispose();
|
|
1131
1386
|
},
|
|
1132
1387
|
});
|
|
1133
1388
|
|
|
1389
|
+
api.registerChannel?.({
|
|
1390
|
+
plugin: runtime.getChannelPlugin(),
|
|
1391
|
+
});
|
|
1392
|
+
|
|
1134
1393
|
api.registerCommand({
|
|
1135
1394
|
name: "privateclaw",
|
|
1136
1395
|
description: PRIVATECLAW_COMMAND_DESCRIPTION,
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { EventEmitter } from "node:events";
|
|
3
|
+
import test from "node:test";
|
|
4
|
+
import {
|
|
5
|
+
createSessionsDrainWaiter,
|
|
6
|
+
createShutdownSignalWaiter,
|
|
7
|
+
createStdinEofWaiter,
|
|
8
|
+
waitForForegroundPairOutcome,
|
|
9
|
+
} from "./pair-session.js";
|
|
10
|
+
|
|
11
|
+
class FakeTtyInput extends EventEmitter {
|
|
12
|
+
isTTY = true;
|
|
13
|
+
destroyed = false;
|
|
14
|
+
#paused = true;
|
|
15
|
+
|
|
16
|
+
isPaused(): boolean {
|
|
17
|
+
return this.#paused;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
pause(): this {
|
|
21
|
+
this.#paused = true;
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
resume(): this {
|
|
26
|
+
this.#paused = false;
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
test("waitForForegroundPairOutcome cancels losing waiters after a signal", async () => {
|
|
32
|
+
const cancelled: string[] = [];
|
|
33
|
+
const outcome = await waitForForegroundPairOutcome({
|
|
34
|
+
provider: {
|
|
35
|
+
listActiveSessions: () => [{ sessionId: "still-active" }] as never[],
|
|
36
|
+
},
|
|
37
|
+
handoffToBackground: async () => undefined,
|
|
38
|
+
createSignalWaiter: () => ({
|
|
39
|
+
promise: Promise.resolve({ kind: "signal", signal: "SIGINT" as const }),
|
|
40
|
+
cancel: () => {
|
|
41
|
+
cancelled.push("signal");
|
|
42
|
+
},
|
|
43
|
+
}),
|
|
44
|
+
createDrainWaiter: () => ({
|
|
45
|
+
promise: new Promise(() => undefined),
|
|
46
|
+
cancel: () => {
|
|
47
|
+
cancelled.push("drain");
|
|
48
|
+
},
|
|
49
|
+
}),
|
|
50
|
+
createInputWaiter: () => ({
|
|
51
|
+
promise: new Promise(() => undefined),
|
|
52
|
+
cancel: () => {
|
|
53
|
+
cancelled.push("stdin");
|
|
54
|
+
},
|
|
55
|
+
}),
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
assert.deepEqual(outcome, {
|
|
59
|
+
kind: "signal",
|
|
60
|
+
signal: "SIGINT",
|
|
61
|
+
});
|
|
62
|
+
assert.deepEqual(cancelled.sort(), ["drain", "signal", "stdin"]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("createShutdownSignalWaiter removes signal listeners when cancelled", () => {
|
|
66
|
+
const signalSource = new EventEmitter();
|
|
67
|
+
const waiter = createShutdownSignalWaiter(
|
|
68
|
+
signalSource as unknown as Pick<NodeJS.Process, "once" | "off">,
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
assert.equal(signalSource.listenerCount("SIGINT"), 1);
|
|
72
|
+
assert.equal(signalSource.listenerCount("SIGTERM"), 1);
|
|
73
|
+
|
|
74
|
+
waiter.cancel();
|
|
75
|
+
|
|
76
|
+
assert.equal(signalSource.listenerCount("SIGINT"), 0);
|
|
77
|
+
assert.equal(signalSource.listenerCount("SIGTERM"), 0);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("createStdinEofWaiter removes stdin listeners when cancelled", () => {
|
|
81
|
+
const input = new FakeTtyInput();
|
|
82
|
+
const waiter = createStdinEofWaiter(
|
|
83
|
+
input as unknown as Pick<
|
|
84
|
+
NodeJS.ReadStream,
|
|
85
|
+
"isTTY" | "destroyed" | "isPaused" | "pause" | "resume" | "once" | "off"
|
|
86
|
+
>,
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
assert.equal(input.isPaused(), false);
|
|
90
|
+
assert.equal(input.listenerCount("end"), 1);
|
|
91
|
+
assert.equal(input.listenerCount("close"), 1);
|
|
92
|
+
|
|
93
|
+
waiter.cancel();
|
|
94
|
+
|
|
95
|
+
assert.equal(input.isPaused(), true);
|
|
96
|
+
assert.equal(input.listenerCount("end"), 0);
|
|
97
|
+
assert.equal(input.listenerCount("close"), 0);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("createSessionsDrainWaiter stops polling after cancellation", async () => {
|
|
101
|
+
let pollCount = 0;
|
|
102
|
+
const waiter = createSessionsDrainWaiter(
|
|
103
|
+
{
|
|
104
|
+
listActiveSessions: () => {
|
|
105
|
+
pollCount += 1;
|
|
106
|
+
return [{ sessionId: "still-active" }] as never[];
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
5,
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
await new Promise((resolve) => setTimeout(resolve, 25));
|
|
113
|
+
const countBeforeCancel = pollCount;
|
|
114
|
+
waiter.cancel();
|
|
115
|
+
await new Promise((resolve) => setTimeout(resolve, 25));
|
|
116
|
+
|
|
117
|
+
assert.ok(countBeforeCancel >= 2);
|
|
118
|
+
assert.equal(pollCount, countBeforeCancel);
|
|
119
|
+
});
|