@privateclaw/privateclaw 0.1.12 → 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.
@@ -46,6 +46,7 @@ import {
46
46
  PRIVATECLAW_CLI_OPEN_OPTION_DESCRIPTION,
47
47
  PRIVATECLAW_CLI_PAIR_DESCRIPTION,
48
48
  PRIVATECLAW_CLI_PRINT_ONLY_OPTION_DESCRIPTION,
49
+ PRIVATECLAW_CLI_RELAY_OPTION_DESCRIPTION,
49
50
  PRIVATECLAW_CLI_ROOT_DESCRIPTION,
50
51
  PRIVATECLAW_CLI_SESSIONS_DESCRIPTION,
51
52
  PRIVATECLAW_CLI_TTL_OPTION_DESCRIPTION,
@@ -56,6 +57,7 @@ import {
56
57
  import type {
57
58
  PrivateClawAgentBridge,
58
59
  PrivateClawInviteBundle,
60
+ PrivateClawManagedSession,
59
61
  PrivateClawProviderOptions,
60
62
  } from "./types.js";
61
63
 
@@ -105,6 +107,7 @@ interface ResolvedPrivateClawPluginConfig {
105
107
  interface PrivateClawPairCliOptions {
106
108
  ttlMs?: string;
107
109
  label?: string;
110
+ relay?: string;
108
111
  printOnly?: boolean;
109
112
  group?: boolean;
110
113
  open?: boolean;
@@ -337,9 +340,11 @@ function normalizePairCliOptions(raw: unknown): PrivateClawPairCliOptions {
337
340
  const options = raw as Record<string, unknown>;
338
341
  const ttlMs = readString(options.ttlMs);
339
342
  const label = readString(options.label);
343
+ const relay = readString(options.relay);
340
344
  return {
341
345
  ...(ttlMs ? { ttlMs } : {}),
342
346
  ...(label ? { label } : {}),
347
+ ...(relay ? { relay } : {}),
343
348
  ...(typeof options.printOnly === "boolean" ? { printOnly: options.printOnly } : {}),
344
349
  ...(typeof options.group === "boolean" ? { group: options.group } : {}),
345
350
  ...(typeof options.open === "boolean" ? { open: options.open } : {}),
@@ -351,16 +356,94 @@ function normalizePairCliOptions(raw: unknown): PrivateClawPairCliOptions {
351
356
 
352
357
  function parsePrivateClawCommandArgs(raw: string | undefined): {
353
358
  groupMode?: boolean;
359
+ relayBaseUrl?: string;
354
360
  } {
355
361
  const normalizedArgs = (raw ?? "")
356
362
  .split(/\s+/)
357
- .map((token) => token.trim().toLowerCase())
363
+ .map((token) => token.trim())
358
364
  .filter((token) => token !== "");
359
- return normalizedArgs.some(
360
- (token) => token === "group" || token === "--group" || token === "-g",
361
- )
362
- ? { groupMode: true }
363
- : {};
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
+ };
364
447
  }
365
448
 
366
449
  function buildProviderOptions(
@@ -446,15 +529,27 @@ function buildDaemonEnvFromPluginConfig(
446
529
  }
447
530
 
448
531
  class PrivateClawPluginRuntime {
449
- private provider: PrivateClawProvider | undefined;
532
+ private readonly providerEntries = new Map<
533
+ string,
534
+ {
535
+ provider?: PrivateClawProvider;
536
+ providerOptions: PrivateClawProviderOptions;
537
+ }
538
+ >();
450
539
  private stateDir: string | undefined;
451
540
  private controlServer: PrivateClawSessionControlServer | undefined;
541
+ private readonly defaultRelayBaseUrl: string;
452
542
 
453
543
  constructor(
454
544
  private readonly providerOptions: PrivateClawProviderOptions,
455
545
  private readonly defaultTtlMs: number = DEFAULT_SESSION_TTL_MS,
456
546
  private readonly daemonEnv?: NodeJS.ProcessEnv,
457
- ) {}
547
+ defaultRelayBaseUrl?: string,
548
+ ) {
549
+ this.defaultRelayBaseUrl =
550
+ readString(defaultRelayBaseUrl) ??
551
+ inferRelayBaseUrlFromAppWsUrl(providerOptions.appWsUrl);
552
+ }
458
553
 
459
554
  setStateDir(stateDir: string): void {
460
555
  this.stateDir = stateDir;
@@ -469,12 +564,64 @@ class PrivateClawPluginRuntime {
469
564
  return resolvePrivateClawMediaDir(this.ensureStateDir());
470
565
  }
471
566
 
472
- private getProvider(): PrivateClawProvider {
473
- if (!this.provider) {
474
- this.provider = new PrivateClawProvider(this.providerOptions);
567
+ private resolveRelayBaseUrl(relayBaseUrl?: string): string {
568
+ return readString(relayBaseUrl) ?? this.defaultRelayBaseUrl;
569
+ }
570
+
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);
475
595
  }
476
596
 
477
- return this.provider;
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
+ }
623
+ }
624
+ throw new Error(`Unknown PrivateClaw session: ${sessionId}`);
478
625
  }
479
626
 
480
627
  private async ensureControlServer(
@@ -484,7 +631,11 @@ class PrivateClawPluginRuntime {
484
631
  return;
485
632
  }
486
633
  this.controlServer = new PrivateClawSessionControlServer({
487
- provider: this.getProvider(),
634
+ provider: {
635
+ listManagedSessions: () => this.listLocalManagedSessions(),
636
+ kickGroupParticipant: (sessionId, appId, reason) =>
637
+ this.kickLocalManagedParticipant(sessionId, appId, reason),
638
+ },
488
639
  stateDir: this.ensureStateDir(),
489
640
  kind,
490
641
  ...(this.providerOptions.onLog
@@ -498,9 +649,10 @@ class PrivateClawPluginRuntime {
498
649
  ttlMs?: number;
499
650
  label?: string;
500
651
  groupMode?: boolean;
652
+ relayBaseUrl?: string;
501
653
  }): Promise<PrivateClawInviteBundle> {
502
654
  await this.ensureControlServer("plugin-service");
503
- return this.getProvider().createInviteBundle(
655
+ return this.getProvider(params?.relayBaseUrl).createInviteBundle(
504
656
  {
505
657
  ...(typeof params?.ttlMs === "number"
506
658
  ? { ttlMs: params.ttlMs }
@@ -538,6 +690,7 @@ class PrivateClawPluginRuntime {
538
690
  ttlMs?: number;
539
691
  label?: string;
540
692
  groupMode?: boolean;
693
+ relayBaseUrl?: string;
541
694
  openInBrowser?: boolean;
542
695
  writeLine?: (line: string) => void;
543
696
  }): Promise<PrivateClawInviteBundle> {
@@ -549,6 +702,10 @@ class PrivateClawPluginRuntime {
549
702
  ),
550
703
  );
551
704
  }
705
+ const daemonEnv = mergeDaemonEnvForRelay(
706
+ this.daemonEnv,
707
+ params?.relayBaseUrl,
708
+ );
552
709
 
553
710
  const bundle = await spawnBackgroundPairDaemon({
554
711
  cliModuleUrl: new URL(
@@ -556,7 +713,7 @@ class PrivateClawPluginRuntime {
556
713
  import.meta.url,
557
714
  ).href,
558
715
  stateDir: this.ensureStateDir(),
559
- ...(this.daemonEnv ? { env: this.daemonEnv } : {}),
716
+ ...(daemonEnv ? { env: daemonEnv } : {}),
560
717
  ...(typeof params?.ttlMs === "number" ? { ttlMs: params.ttlMs } : {}),
561
718
  ...(params?.label ? { label: params.label } : {}),
562
719
  ...(params?.groupMode === true ? { groupMode: true } : {}),
@@ -571,6 +728,7 @@ class PrivateClawPluginRuntime {
571
728
  label?: string;
572
729
  printOnly?: boolean;
573
730
  groupMode?: boolean;
731
+ relayBaseUrl?: string;
574
732
  foreground?: boolean;
575
733
  openInBrowser?: boolean;
576
734
  writeLine?: (line: string) => void;
@@ -578,7 +736,11 @@ class PrivateClawPluginRuntime {
578
736
  if (!params?.printOnly) {
579
737
  await this.ensureControlServer("pair-foreground");
580
738
  }
581
- const provider = this.getProvider();
739
+ const provider = this.getProvider(params?.relayBaseUrl);
740
+ const handoffEnv = mergeDaemonEnvForRelay(
741
+ this.daemonEnv,
742
+ params?.relayBaseUrl,
743
+ );
582
744
  return runPairSession({
583
745
  provider,
584
746
  ...(typeof params?.ttlMs === "number"
@@ -600,17 +762,17 @@ class PrivateClawPluginRuntime {
600
762
  provider.suppressReconnectsForHandoff();
601
763
  try {
602
764
  await handoffForegroundPairToBackground({
603
- cliModuleUrl: new URL(
604
- import.meta.url.endsWith(".ts") ? "./cli.ts" : "./cli.js",
605
- import.meta.url,
606
- ).href,
607
- stateDir: this.ensureStateDir(),
608
- ...(this.daemonEnv ? { env: this.daemonEnv } : {}),
609
- handoffState: provider.exportHandoffState(),
610
- });
611
- return formatBilingualInline(
612
- "当前 PrivateClaw 会话已转入后台。可使用 `openclaw privateclaw sessions` 查看。",
613
- "The current PrivateClaw session is now running in the background. Use `openclaw privateclaw sessions` to inspect it.",
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.",
614
776
  );
615
777
  } catch (error) {
616
778
  provider.resumeReconnectsAfterHandoffFailure();
@@ -641,12 +803,13 @@ class PrivateClawPluginRuntime {
641
803
  await this.controlServer.stop();
642
804
  this.controlServer = undefined;
643
805
  }
644
- if (!this.provider) {
645
- return;
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();
646
812
  }
647
-
648
- await this.provider.dispose();
649
- this.provider = undefined;
650
813
  }
651
814
  }
652
815
 
@@ -717,24 +880,27 @@ function createPluginDefinition(
717
880
  privateclaw
718
881
  .command("pair")
719
882
  .description(PRIVATECLAW_CLI_PAIR_DESCRIPTION)
720
- .option("--ttl-ms <ms>", PRIVATECLAW_CLI_TTL_OPTION_DESCRIPTION)
721
- .option("--label <label>", PRIVATECLAW_CLI_LABEL_OPTION_DESCRIPTION)
722
- .option("--group", PRIVATECLAW_CLI_GROUP_OPTION_DESCRIPTION)
723
- .option("--print-only", PRIVATECLAW_CLI_PRINT_ONLY_OPTION_DESCRIPTION)
724
- .option("--open", PRIVATECLAW_CLI_OPEN_OPTION_DESCRIPTION)
725
- .option("--foreground", PRIVATECLAW_CLI_FOREGROUND_OPTION_DESCRIPTION)
726
- .action(async (rawOptions: unknown) => {
727
- const options = normalizePairCliOptions(rawOptions);
728
- const ttlMs = parsePositiveIntegerFlag(options.ttlMs, "--ttl-ms");
729
- const label = readString(options.label);
730
- const pairParams = {
731
- ...(typeof ttlMs === "number" ? { ttlMs } : {}),
732
- ...(label ? { label } : {}),
733
- ...(options.group ? { groupMode: true } : {}),
734
- ...(typeof options.printOnly === "boolean"
735
- ? { printOnly: options.printOnly }
736
- : {}),
737
- ...(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"
738
904
  ? { openInBrowser: options.open }
739
905
  : {}),
740
906
  writeLine: (line: string) => {
@@ -796,7 +962,13 @@ export function createOpenClawCompatiblePlugin(
796
962
  options: PrivateClawProviderOptions & { defaultTtlMs?: number },
797
963
  ): OpenClawExtensionPluginCompat {
798
964
  return createPluginDefinition(
799
- () => new PrivateClawPluginRuntime(options, options.defaultTtlMs),
965
+ () =>
966
+ new PrivateClawPluginRuntime(
967
+ options,
968
+ options.defaultTtlMs,
969
+ undefined,
970
+ inferRelayBaseUrlFromAppWsUrl(options.appWsUrl),
971
+ ),
800
972
  );
801
973
  }
802
974
 
@@ -808,6 +980,7 @@ export function createPrivateClawPlugin(): OpenClawExtensionPluginCompat {
808
980
  resolved.options,
809
981
  resolved.defaultTtlMs,
810
982
  buildDaemonEnvFromPluginConfig(pluginConfig),
983
+ pluginConfig.relayBaseUrl,
811
984
  );
812
985
  });
813
986
  }
package/src/text.ts CHANGED
@@ -33,8 +33,8 @@ export const PRIVATECLAW_PLUGIN_DESCRIPTION = formatBilingualInline(
33
33
  );
34
34
 
35
35
  export const PRIVATECLAW_COMMAND_DESCRIPTION = formatBilingualInline(
36
- "创建一次性加密的 PrivateClaw 会话二维码;传入 `group` 可启用群聊模式。",
37
- "Create a one-time encrypted PrivateClaw session QR code; pass `group` to enable group chat.",
36
+ "创建一次性加密的 PrivateClaw 会话二维码;传入 `group` 可启用群聊模式,传入 `relay=<url>` 或 `--relay <url>` 可临时覆盖 relay。",
37
+ "Create a one-time encrypted PrivateClaw session QR code; pass `group` to enable group chat, and `relay=<url>` or `--relay <url>` to override the relay for this invite.",
38
38
  );
39
39
 
40
40
  export const PRIVATECLAW_CLI_ROOT_DESCRIPTION = formatBilingualInline(
@@ -67,6 +67,11 @@ export const PRIVATECLAW_CLI_LABEL_OPTION_DESCRIPTION = formatBilingualInline(
67
67
  "Optional relay session label.",
68
68
  );
69
69
 
70
+ export const PRIVATECLAW_CLI_RELAY_OPTION_DESCRIPTION = formatBilingualInline(
71
+ "临时覆盖本次命令使用的 relay base URL。",
72
+ "Temporarily override the relay base URL for this command.",
73
+ );
74
+
70
75
  export const PRIVATECLAW_CLI_GROUP_OPTION_DESCRIPTION = formatBilingualInline(
71
76
  "允许多个 PrivateClaw 客户端加入同一个会话。",
72
77
  "Allow multiple PrivateClaw app clients to join the same session.",