@privateclaw/privateclaw 0.1.11 → 0.1.13

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.
@@ -8,7 +8,15 @@ import {
8
8
  writeInviteQrPng,
9
9
  } from "./invite-qr-files.js";
10
10
  import { loadAvailableOpenClawCommands } from "./openclaw-command-discovery.js";
11
- import { parsePositiveIntegerFlag, runPairSession } from "./pair-session.js";
11
+ import {
12
+ parsePositiveIntegerFlag,
13
+ printPairInviteBundle,
14
+ runPairSession,
15
+ } from "./pair-session.js";
16
+ import {
17
+ handoffForegroundPairToBackground,
18
+ spawnBackgroundPairDaemon,
19
+ } from "./pair-daemon.js";
12
20
  import { WebhookBridge } from "./bridges/webhook-bridge.js";
13
21
  import {
14
22
  type OpenClawExtensionPluginCompat,
@@ -20,14 +28,27 @@ import {
20
28
  import { DEFAULT_SESSION_TTL_MS, PrivateClawProvider } from "./provider.js";
21
29
  import { DEFAULT_RELAY_BASE_URL } from "./relay-defaults.js";
22
30
  import { resolveRelayEndpoints } from "./relay-endpoints.js";
31
+ import {
32
+ buildManagedSessionsReportLines,
33
+ kickManagedParticipantFromStateDir,
34
+ listManagedSessionsFromStateDir,
35
+ PrivateClawSessionControlServer,
36
+ resolvePrivateClawStateDir,
37
+ type PrivateClawControlHostKind,
38
+ } from "./session-control.js";
23
39
  import {
24
40
  buildPrivateClawCommandErrorMessage,
41
+ formatBilingualInline,
42
+ PRIVATECLAW_CLI_FOREGROUND_OPTION_DESCRIPTION,
25
43
  PRIVATECLAW_CLI_GROUP_OPTION_DESCRIPTION,
44
+ PRIVATECLAW_CLI_KICK_DESCRIPTION,
26
45
  PRIVATECLAW_CLI_LABEL_OPTION_DESCRIPTION,
27
46
  PRIVATECLAW_CLI_OPEN_OPTION_DESCRIPTION,
28
47
  PRIVATECLAW_CLI_PAIR_DESCRIPTION,
29
48
  PRIVATECLAW_CLI_PRINT_ONLY_OPTION_DESCRIPTION,
49
+ PRIVATECLAW_CLI_RELAY_OPTION_DESCRIPTION,
30
50
  PRIVATECLAW_CLI_ROOT_DESCRIPTION,
51
+ PRIVATECLAW_CLI_SESSIONS_DESCRIPTION,
31
52
  PRIVATECLAW_CLI_TTL_OPTION_DESCRIPTION,
32
53
  PRIVATECLAW_COMMAND_DESCRIPTION,
33
54
  PRIVATECLAW_INVITE_URI_LABEL,
@@ -36,6 +57,7 @@ import {
36
57
  import type {
37
58
  PrivateClawAgentBridge,
38
59
  PrivateClawInviteBundle,
60
+ PrivateClawManagedSession,
39
61
  PrivateClawProviderOptions,
40
62
  } from "./types.js";
41
63
 
@@ -85,9 +107,11 @@ interface ResolvedPrivateClawPluginConfig {
85
107
  interface PrivateClawPairCliOptions {
86
108
  ttlMs?: string;
87
109
  label?: string;
110
+ relay?: string;
88
111
  printOnly?: boolean;
89
112
  group?: boolean;
90
113
  open?: boolean;
114
+ foreground?: boolean;
91
115
  }
92
116
 
93
117
  const DEFAULT_PROVIDER_LABEL = "PrivateClaw";
@@ -316,27 +340,110 @@ function normalizePairCliOptions(raw: unknown): PrivateClawPairCliOptions {
316
340
  const options = raw as Record<string, unknown>;
317
341
  const ttlMs = readString(options.ttlMs);
318
342
  const label = readString(options.label);
343
+ const relay = readString(options.relay);
319
344
  return {
320
345
  ...(ttlMs ? { ttlMs } : {}),
321
346
  ...(label ? { label } : {}),
347
+ ...(relay ? { relay } : {}),
322
348
  ...(typeof options.printOnly === "boolean" ? { printOnly: options.printOnly } : {}),
323
349
  ...(typeof options.group === "boolean" ? { group: options.group } : {}),
324
350
  ...(typeof options.open === "boolean" ? { open: options.open } : {}),
351
+ ...(typeof options.foreground === "boolean"
352
+ ? { foreground: options.foreground }
353
+ : {}),
325
354
  };
326
355
  }
327
356
 
328
357
  function parsePrivateClawCommandArgs(raw: string | undefined): {
329
358
  groupMode?: boolean;
359
+ relayBaseUrl?: string;
330
360
  } {
331
361
  const normalizedArgs = (raw ?? "")
332
362
  .split(/\s+/)
333
- .map((token) => token.trim().toLowerCase())
363
+ .map((token) => token.trim())
334
364
  .filter((token) => token !== "");
335
- return normalizedArgs.some(
336
- (token) => token === "group" || token === "--group" || token === "-g",
337
- )
338
- ? { groupMode: true }
339
- : {};
365
+ let groupMode = false;
366
+ let relayBaseUrl: string | undefined;
367
+
368
+ for (let index = 0; index < normalizedArgs.length; index += 1) {
369
+ const token = normalizedArgs[index];
370
+ if (!token) {
371
+ continue;
372
+ }
373
+ const normalized = token.toLowerCase();
374
+ if (normalized === "group" || normalized === "--group" || normalized === "-g") {
375
+ groupMode = true;
376
+ continue;
377
+ }
378
+
379
+ if (normalized === "--relay" || normalized === "-r") {
380
+ const nextToken = normalizedArgs[index + 1]?.trim();
381
+ if (!nextToken) {
382
+ throw new Error(
383
+ formatBilingualInline(
384
+ "`/privateclaw --relay` 后面需要提供 relay URL。",
385
+ "`/privateclaw --relay` requires a relay URL.",
386
+ ),
387
+ );
388
+ }
389
+ relayBaseUrl = nextToken;
390
+ index += 1;
391
+ continue;
392
+ }
393
+
394
+ const equalsIndex = token.indexOf("=");
395
+ if (equalsIndex <= 0) {
396
+ continue;
397
+ }
398
+ const key = token.slice(0, equalsIndex).toLowerCase();
399
+ if (key !== "relay" && key !== "--relay" && key !== "-r") {
400
+ continue;
401
+ }
402
+ const value = token.slice(equalsIndex + 1).trim();
403
+ if (!value) {
404
+ throw new Error(
405
+ formatBilingualInline(
406
+ "`relay=` 后面需要提供 relay URL。",
407
+ "`relay=` requires a relay URL.",
408
+ ),
409
+ );
410
+ }
411
+ relayBaseUrl = value;
412
+ }
413
+
414
+ return {
415
+ ...(groupMode ? { groupMode: true } : {}),
416
+ ...(relayBaseUrl ? { relayBaseUrl } : {}),
417
+ };
418
+ }
419
+
420
+ function inferRelayBaseUrlFromAppWsUrl(appWsUrl: string): string {
421
+ try {
422
+ const url = new URL(appWsUrl);
423
+ url.pathname = "/";
424
+ url.search = "";
425
+ url.hash = "";
426
+ return url.origin;
427
+ } catch {
428
+ return DEFAULT_RELAY_BASE_URL;
429
+ }
430
+ }
431
+
432
+ function mergeDaemonEnvForRelay(
433
+ baseEnv: NodeJS.ProcessEnv | undefined,
434
+ relayBaseUrl?: string,
435
+ ): NodeJS.ProcessEnv | undefined {
436
+ if (!baseEnv) {
437
+ return undefined;
438
+ }
439
+ const normalizedRelayBaseUrl = readString(relayBaseUrl);
440
+ if (!normalizedRelayBaseUrl) {
441
+ return baseEnv;
442
+ }
443
+ return {
444
+ ...baseEnv,
445
+ PRIVATECLAW_RELAY_BASE_URL: normalizedRelayBaseUrl,
446
+ };
340
447
  }
341
448
 
342
449
  function buildProviderOptions(
@@ -370,37 +477,182 @@ function buildProviderOptions(
370
477
  };
371
478
  }
372
479
 
480
+ function buildDaemonEnvFromPluginConfig(
481
+ pluginConfig: ResolvedPrivateClawPluginConfig,
482
+ ): NodeJS.ProcessEnv {
483
+ return {
484
+ PRIVATECLAW_RELAY_BASE_URL: pluginConfig.relayBaseUrl,
485
+ PRIVATECLAW_BRIDGE_MODE: pluginConfig.bridgeMode,
486
+ PRIVATECLAW_PROVIDER_LABEL: pluginConfig.providerLabel,
487
+ ...(typeof pluginConfig.sessionTtlMs === "number"
488
+ ? { PRIVATECLAW_SESSION_TTL_MS: String(pluginConfig.sessionTtlMs) }
489
+ : {}),
490
+ ...(pluginConfig.welcomeMessage
491
+ ? { PRIVATECLAW_WELCOME_MESSAGE: pluginConfig.welcomeMessage }
492
+ : {}),
493
+ ...(pluginConfig.webhookUrl
494
+ ? { PRIVATECLAW_WEBHOOK_URL: pluginConfig.webhookUrl }
495
+ : {}),
496
+ ...(pluginConfig.webhookToken
497
+ ? { PRIVATECLAW_WEBHOOK_TOKEN: pluginConfig.webhookToken }
498
+ : {}),
499
+ ...(pluginConfig.echoPrefix
500
+ ? { PRIVATECLAW_ECHO_PREFIX: pluginConfig.echoPrefix }
501
+ : {}),
502
+ ...(pluginConfig.openclawAgentExecutable
503
+ ? { PRIVATECLAW_OPENCLAW_AGENT_BIN: pluginConfig.openclawAgentExecutable }
504
+ : {}),
505
+ ...(pluginConfig.openclawAgentId
506
+ ? { PRIVATECLAW_OPENCLAW_AGENT_ID: pluginConfig.openclawAgentId }
507
+ : {}),
508
+ ...(pluginConfig.openclawAgentChannel
509
+ ? { PRIVATECLAW_OPENCLAW_AGENT_CHANNEL: pluginConfig.openclawAgentChannel }
510
+ : {}),
511
+ ...(typeof pluginConfig.openclawAgentLocal === "boolean"
512
+ ? {
513
+ PRIVATECLAW_OPENCLAW_AGENT_LOCAL: pluginConfig.openclawAgentLocal
514
+ ? "true"
515
+ : "false",
516
+ }
517
+ : {}),
518
+ ...(pluginConfig.openclawAgentThinking
519
+ ? { PRIVATECLAW_OPENCLAW_AGENT_THINKING: pluginConfig.openclawAgentThinking }
520
+ : {}),
521
+ ...(typeof pluginConfig.openclawAgentTimeoutSeconds === "number"
522
+ ? {
523
+ PRIVATECLAW_OPENCLAW_AGENT_TIMEOUT_SECONDS: String(
524
+ pluginConfig.openclawAgentTimeoutSeconds,
525
+ ),
526
+ }
527
+ : {}),
528
+ };
529
+ }
530
+
373
531
  class PrivateClawPluginRuntime {
374
- private provider: PrivateClawProvider | undefined;
532
+ private readonly providerEntries = new Map<
533
+ string,
534
+ {
535
+ provider?: PrivateClawProvider;
536
+ providerOptions: PrivateClawProviderOptions;
537
+ }
538
+ >();
375
539
  private stateDir: string | undefined;
540
+ private controlServer: PrivateClawSessionControlServer | undefined;
541
+ private readonly defaultRelayBaseUrl: string;
376
542
 
377
543
  constructor(
378
544
  private readonly providerOptions: PrivateClawProviderOptions,
379
545
  private readonly defaultTtlMs: number = DEFAULT_SESSION_TTL_MS,
380
- ) {}
546
+ private readonly daemonEnv?: NodeJS.ProcessEnv,
547
+ defaultRelayBaseUrl?: string,
548
+ ) {
549
+ this.defaultRelayBaseUrl =
550
+ readString(defaultRelayBaseUrl) ??
551
+ inferRelayBaseUrlFromAppWsUrl(providerOptions.appWsUrl);
552
+ }
381
553
 
382
554
  setStateDir(stateDir: string): void {
383
555
  this.stateDir = stateDir;
384
556
  }
385
557
 
558
+ private ensureStateDir(): string {
559
+ this.stateDir = resolvePrivateClawStateDir(this.stateDir);
560
+ return this.stateDir;
561
+ }
562
+
386
563
  private getMediaDir(): string {
387
- return resolvePrivateClawMediaDir(this.stateDir);
564
+ return resolvePrivateClawMediaDir(this.ensureStateDir());
565
+ }
566
+
567
+ private resolveRelayBaseUrl(relayBaseUrl?: string): string {
568
+ return readString(relayBaseUrl) ?? this.defaultRelayBaseUrl;
388
569
  }
389
570
 
390
- private getProvider(): PrivateClawProvider {
391
- if (!this.provider) {
392
- this.provider = new PrivateClawProvider(this.providerOptions);
571
+ private getProviderEntry(relayBaseUrl?: string): {
572
+ provider?: PrivateClawProvider;
573
+ providerOptions: PrivateClawProviderOptions;
574
+ } {
575
+ const resolvedRelayBaseUrl = this.resolveRelayBaseUrl(relayBaseUrl);
576
+ let entry = this.providerEntries.get(resolvedRelayBaseUrl);
577
+ if (!entry) {
578
+ const relay = resolveRelayEndpoints(resolvedRelayBaseUrl);
579
+ entry = {
580
+ providerOptions: {
581
+ ...this.providerOptions,
582
+ providerWsUrl: relay.providerWsUrl,
583
+ appWsUrl: relay.appWsUrl,
584
+ },
585
+ };
586
+ this.providerEntries.set(resolvedRelayBaseUrl, entry);
587
+ }
588
+ return entry;
589
+ }
590
+
591
+ private getProvider(relayBaseUrl?: string): PrivateClawProvider {
592
+ const entry = this.getProviderEntry(relayBaseUrl);
593
+ if (!entry.provider) {
594
+ entry.provider = new PrivateClawProvider(entry.providerOptions);
595
+ }
596
+
597
+ return entry.provider;
598
+ }
599
+
600
+ private listLocalManagedSessions(): PrivateClawManagedSession[] {
601
+ return [...this.providerEntries.values()].flatMap((entry) =>
602
+ entry.provider?.listManagedSessions() ?? [],
603
+ );
604
+ }
605
+
606
+ private async kickLocalManagedParticipant(
607
+ sessionId: string,
608
+ appId: string,
609
+ reason = "participant_removed",
610
+ ) {
611
+ for (const entry of this.providerEntries.values()) {
612
+ const provider = entry.provider;
613
+ if (!provider) {
614
+ continue;
615
+ }
616
+ if (
617
+ provider
618
+ .listManagedSessions()
619
+ .some((session) => session.sessionId === sessionId)
620
+ ) {
621
+ return provider.kickGroupParticipant(sessionId, appId, reason);
622
+ }
393
623
  }
624
+ throw new Error(`Unknown PrivateClaw session: ${sessionId}`);
625
+ }
394
626
 
395
- return this.provider;
627
+ private async ensureControlServer(
628
+ kind: PrivateClawControlHostKind,
629
+ ): Promise<void> {
630
+ if (this.controlServer) {
631
+ return;
632
+ }
633
+ this.controlServer = new PrivateClawSessionControlServer({
634
+ provider: {
635
+ listManagedSessions: () => this.listLocalManagedSessions(),
636
+ kickGroupParticipant: (sessionId, appId, reason) =>
637
+ this.kickLocalManagedParticipant(sessionId, appId, reason),
638
+ },
639
+ stateDir: this.ensureStateDir(),
640
+ kind,
641
+ ...(this.providerOptions.onLog
642
+ ? { onLog: this.providerOptions.onLog }
643
+ : {}),
644
+ });
645
+ await this.controlServer.start();
396
646
  }
397
647
 
398
648
  async createInviteBundle(params?: {
399
649
  ttlMs?: number;
400
650
  label?: string;
401
651
  groupMode?: boolean;
652
+ relayBaseUrl?: string;
402
653
  }): Promise<PrivateClawInviteBundle> {
403
- return this.getProvider().createInviteBundle(
654
+ await this.ensureControlServer("plugin-service");
655
+ return this.getProvider(params?.relayBaseUrl).createInviteBundle(
404
656
  {
405
657
  ...(typeof params?.ttlMs === "number"
406
658
  ? { ttlMs: params.ttlMs }
@@ -434,37 +686,130 @@ class PrivateClawPluginRuntime {
434
686
  };
435
687
  }
436
688
 
689
+ async runBackgroundPairSession(params?: {
690
+ ttlMs?: number;
691
+ label?: string;
692
+ groupMode?: boolean;
693
+ relayBaseUrl?: string;
694
+ openInBrowser?: boolean;
695
+ writeLine?: (line: string) => void;
696
+ }): Promise<PrivateClawInviteBundle> {
697
+ if (!this.daemonEnv) {
698
+ throw new Error(
699
+ formatBilingualInline(
700
+ "当前运行时无法安全地转入后台,请改用 --foreground。",
701
+ "This runtime cannot safely move into the background. Use --foreground instead.",
702
+ ),
703
+ );
704
+ }
705
+ const daemonEnv = mergeDaemonEnvForRelay(
706
+ this.daemonEnv,
707
+ params?.relayBaseUrl,
708
+ );
709
+
710
+ const bundle = await spawnBackgroundPairDaemon({
711
+ cliModuleUrl: new URL(
712
+ import.meta.url.endsWith(".ts") ? "./cli.ts" : "./cli.js",
713
+ import.meta.url,
714
+ ).href,
715
+ stateDir: this.ensureStateDir(),
716
+ ...(daemonEnv ? { env: daemonEnv } : {}),
717
+ ...(typeof params?.ttlMs === "number" ? { ttlMs: params.ttlMs } : {}),
718
+ ...(params?.label ? { label: params.label } : {}),
719
+ ...(params?.groupMode === true ? { groupMode: true } : {}),
720
+ ...(params?.openInBrowser === true ? { openInBrowser: true } : {}),
721
+ });
722
+ printPairInviteBundle(bundle, params?.writeLine ?? ((line) => console.log(line)));
723
+ return bundle;
724
+ }
725
+
437
726
  async runPairSession(params?: {
438
727
  ttlMs?: number;
439
728
  label?: string;
440
729
  printOnly?: boolean;
441
730
  groupMode?: boolean;
731
+ relayBaseUrl?: string;
732
+ foreground?: boolean;
442
733
  openInBrowser?: boolean;
443
734
  writeLine?: (line: string) => void;
444
735
  }): Promise<PrivateClawInviteBundle> {
736
+ if (!params?.printOnly) {
737
+ await this.ensureControlServer("pair-foreground");
738
+ }
739
+ const provider = this.getProvider(params?.relayBaseUrl);
740
+ const handoffEnv = mergeDaemonEnvForRelay(
741
+ this.daemonEnv,
742
+ params?.relayBaseUrl,
743
+ );
445
744
  return runPairSession({
446
- provider: this.getProvider(),
745
+ provider,
447
746
  ...(typeof params?.ttlMs === "number"
448
747
  ? { ttlMs: params.ttlMs }
449
748
  : { ttlMs: this.defaultTtlMs }),
450
749
  ...(params?.label ? { label: params.label } : {}),
451
750
  ...(typeof params?.printOnly === "boolean" ? { printOnly: params.printOnly } : {}),
452
751
  ...(params?.groupMode === true ? { groupMode: true } : {}),
752
+ ...(typeof params?.foreground === "boolean"
753
+ ? { foreground: params.foreground }
754
+ : {}),
453
755
  ...(typeof params?.openInBrowser === "boolean"
454
756
  ? { openInBrowser: params.openInBrowser }
455
757
  : {}),
456
758
  qrMediaDir: this.getMediaDir(),
759
+ ...(this.daemonEnv && !params?.printOnly
760
+ ? {
761
+ handoffToBackground: async () => {
762
+ provider.suppressReconnectsForHandoff();
763
+ try {
764
+ await handoffForegroundPairToBackground({
765
+ cliModuleUrl: new URL(
766
+ import.meta.url.endsWith(".ts") ? "./cli.ts" : "./cli.js",
767
+ import.meta.url,
768
+ ).href,
769
+ stateDir: this.ensureStateDir(),
770
+ ...(handoffEnv ? { env: handoffEnv } : {}),
771
+ handoffState: provider.exportHandoffState(),
772
+ });
773
+ return formatBilingualInline(
774
+ "当前 PrivateClaw 会话已转入后台。可使用 `openclaw privateclaw sessions` 查看。",
775
+ "The current PrivateClaw session is now running in the background. Use `openclaw privateclaw sessions` to inspect it.",
776
+ );
777
+ } catch (error) {
778
+ provider.resumeReconnectsAfterHandoffFailure();
779
+ throw error;
780
+ }
781
+ },
782
+ }
783
+ : {}),
457
784
  ...(params?.writeLine ? { writeLine: params.writeLine } : {}),
458
785
  });
459
786
  }
460
787
 
788
+ async listManagedSessions() {
789
+ return listManagedSessionsFromStateDir(this.ensureStateDir());
790
+ }
791
+
792
+ async kickManagedParticipant(sessionId: string, appId: string) {
793
+ return kickManagedParticipantFromStateDir({
794
+ stateDir: this.ensureStateDir(),
795
+ sessionId,
796
+ appId,
797
+ reason: "participant_removed",
798
+ });
799
+ }
800
+
461
801
  async dispose(): Promise<void> {
462
- if (!this.provider) {
463
- return;
802
+ if (this.controlServer) {
803
+ await this.controlServer.stop();
804
+ this.controlServer = undefined;
805
+ }
806
+ const providers = [...this.providerEntries.values()]
807
+ .map((entry) => entry.provider)
808
+ .filter((provider): provider is PrivateClawProvider => provider != null);
809
+ this.providerEntries.clear();
810
+ for (const provider of providers) {
811
+ await provider.dispose();
464
812
  }
465
-
466
- await this.provider.dispose();
467
- this.provider = undefined;
468
813
  }
469
814
  }
470
815
 
@@ -535,29 +880,76 @@ function createPluginDefinition(
535
880
  privateclaw
536
881
  .command("pair")
537
882
  .description(PRIVATECLAW_CLI_PAIR_DESCRIPTION)
538
- .option("--ttl-ms <ms>", PRIVATECLAW_CLI_TTL_OPTION_DESCRIPTION)
539
- .option("--label <label>", PRIVATECLAW_CLI_LABEL_OPTION_DESCRIPTION)
540
- .option("--group", PRIVATECLAW_CLI_GROUP_OPTION_DESCRIPTION)
541
- .option("--print-only", PRIVATECLAW_CLI_PRINT_ONLY_OPTION_DESCRIPTION)
542
- .option("--open", PRIVATECLAW_CLI_OPEN_OPTION_DESCRIPTION)
543
- .action(async (rawOptions: unknown) => {
544
- const options = normalizePairCliOptions(rawOptions);
545
- const ttlMs = parsePositiveIntegerFlag(options.ttlMs, "--ttl-ms");
546
- const label = readString(options.label);
547
- await runtime.runPairSession({
548
- ...(typeof ttlMs === "number" ? { ttlMs } : {}),
549
- ...(label ? { label } : {}),
550
- ...(options.group ? { groupMode: true } : {}),
551
- ...(typeof options.printOnly === "boolean"
552
- ? { printOnly: options.printOnly }
553
- : {}),
554
- ...(typeof options.open === "boolean"
883
+ .option("--ttl-ms <ms>", PRIVATECLAW_CLI_TTL_OPTION_DESCRIPTION)
884
+ .option("--label <label>", PRIVATECLAW_CLI_LABEL_OPTION_DESCRIPTION)
885
+ .option("-r, --relay <url>", PRIVATECLAW_CLI_RELAY_OPTION_DESCRIPTION)
886
+ .option("--group", PRIVATECLAW_CLI_GROUP_OPTION_DESCRIPTION)
887
+ .option("--print-only", PRIVATECLAW_CLI_PRINT_ONLY_OPTION_DESCRIPTION)
888
+ .option("--open", PRIVATECLAW_CLI_OPEN_OPTION_DESCRIPTION)
889
+ .option("--foreground", PRIVATECLAW_CLI_FOREGROUND_OPTION_DESCRIPTION)
890
+ .action(async (rawOptions: unknown) => {
891
+ const options = normalizePairCliOptions(rawOptions);
892
+ const ttlMs = parsePositiveIntegerFlag(options.ttlMs, "--ttl-ms");
893
+ const label = readString(options.label);
894
+ const relayBaseUrl = readString(options.relay);
895
+ const pairParams = {
896
+ ...(typeof ttlMs === "number" ? { ttlMs } : {}),
897
+ ...(label ? { label } : {}),
898
+ ...(relayBaseUrl ? { relayBaseUrl } : {}),
899
+ ...(options.group ? { groupMode: true } : {}),
900
+ ...(typeof options.printOnly === "boolean"
901
+ ? { printOnly: options.printOnly }
902
+ : {}),
903
+ ...(typeof options.open === "boolean"
555
904
  ? { openInBrowser: options.open }
556
905
  : {}),
557
- writeLine: (line) => {
906
+ writeLine: (line: string) => {
558
907
  console.log(line);
559
908
  },
560
- });
909
+ };
910
+ if (options.printOnly || options.foreground) {
911
+ await runtime.runPairSession({
912
+ ...pairParams,
913
+ foreground: true,
914
+ });
915
+ return;
916
+ }
917
+ await runtime.runBackgroundPairSession(pairParams);
918
+ });
919
+
920
+ privateclaw
921
+ .command("sessions")
922
+ .description(PRIVATECLAW_CLI_SESSIONS_DESCRIPTION)
923
+ .action(async () => {
924
+ const listings = await runtime.listManagedSessions();
925
+ for (const line of buildManagedSessionsReportLines(listings)) {
926
+ console.log(line);
927
+ }
928
+ });
929
+
930
+ privateclaw
931
+ .command("kick")
932
+ .description(PRIVATECLAW_CLI_KICK_DESCRIPTION)
933
+ .argument("<sessionId>")
934
+ .argument("<appId>")
935
+ .action(async (sessionIdRaw: unknown, appIdRaw: unknown) => {
936
+ const sessionId = readString(sessionIdRaw);
937
+ const appId = readString(appIdRaw);
938
+ if (!sessionId || !appId) {
939
+ throw new Error(
940
+ formatBilingualInline(
941
+ "kick 需要提供 sessionId 和 appId。",
942
+ "kick requires both a sessionId and appId.",
943
+ ),
944
+ );
945
+ }
946
+ const result = await runtime.kickManagedParticipant(sessionId, appId);
947
+ console.log(
948
+ formatBilingualInline(
949
+ `已从会话 ${result.session.sessionId} 中移除 ${result.participant.displayName} (${result.participant.appId})。`,
950
+ `Removed ${result.participant.displayName} (${result.participant.appId}) from session ${result.session.sessionId}.`,
951
+ ),
952
+ );
561
953
  });
562
954
  },
563
955
  { commands: ["privateclaw"] },
@@ -570,7 +962,13 @@ export function createOpenClawCompatiblePlugin(
570
962
  options: PrivateClawProviderOptions & { defaultTtlMs?: number },
571
963
  ): OpenClawExtensionPluginCompat {
572
964
  return createPluginDefinition(
573
- () => new PrivateClawPluginRuntime(options, options.defaultTtlMs),
965
+ () =>
966
+ new PrivateClawPluginRuntime(
967
+ options,
968
+ options.defaultTtlMs,
969
+ undefined,
970
+ inferRelayBaseUrlFromAppWsUrl(options.appWsUrl),
971
+ ),
574
972
  );
575
973
  }
576
974
 
@@ -578,7 +976,12 @@ export function createPrivateClawPlugin(): OpenClawExtensionPluginCompat {
578
976
  return createPluginDefinition((api) => {
579
977
  const pluginConfig = resolvePluginConfig(api.pluginConfig);
580
978
  const resolved = buildProviderOptions(pluginConfig, api);
581
- return new PrivateClawPluginRuntime(resolved.options, resolved.defaultTtlMs);
979
+ return new PrivateClawPluginRuntime(
980
+ resolved.options,
981
+ resolved.defaultTtlMs,
982
+ buildDaemonEnvFromPluginConfig(pluginConfig),
983
+ pluginConfig.relayBaseUrl,
984
+ );
582
985
  });
583
986
  }
584
987