@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.
- package/README.md +55 -15
- package/dist/cli.js +369 -62
- package/dist/cli.js.map +1 -1
- package/dist/compat/openclaw.d.ts +1 -0
- package/dist/openclaw-plugin.js +300 -23
- package/dist/openclaw-plugin.js.map +1 -1
- package/dist/pair-daemon.d.ts +16 -0
- package/dist/pair-daemon.js +126 -0
- package/dist/pair-daemon.js.map +1 -0
- package/dist/pair-session.d.ts +4 -1
- package/dist/pair-session.js +80 -15
- package/dist/pair-session.js.map +1 -1
- package/dist/provider.d.ts +11 -3
- package/dist/provider.js +160 -8
- package/dist/provider.js.map +1 -1
- package/dist/relay-provider-client.d.ts +6 -0
- package/dist/relay-provider-client.js +30 -3
- package/dist/relay-provider-client.js.map +1 -1
- package/dist/session-control.d.ts +61 -0
- package/dist/session-control.js +292 -0
- package/dist/session-control.js.map +1 -0
- package/dist/text.d.ts +7 -0
- package/dist/text.js +10 -1
- package/dist/text.js.map +1 -1
- package/dist/types.d.ts +32 -0
- package/openclaw.plugin.json +1 -1
- package/package.json +3 -3
- package/src/cli.ts +471 -73
- package/src/compat/openclaw.ts +1 -0
- package/src/openclaw-plugin.test.ts +408 -1
- package/src/openclaw-plugin.ts +445 -42
- package/src/pair-daemon.ts +181 -0
- package/src/pair-session.ts +105 -12
- package/src/provider.test.ts +280 -0
- package/src/provider.ts +209 -9
- package/src/relay-provider-client.ts +41 -4
- package/src/session-control.ts +473 -0
- package/src/text.ts +41 -2
- package/src/types.ts +35 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
async function resolveCliInvocation(cliModuleUrl) {
|
|
8
|
+
const cliPath = fileURLToPath(cliModuleUrl);
|
|
9
|
+
return path.extname(cliPath) === ".ts"
|
|
10
|
+
? {
|
|
11
|
+
command: process.execPath,
|
|
12
|
+
args: ["--import", "tsx", cliPath],
|
|
13
|
+
}
|
|
14
|
+
: {
|
|
15
|
+
command: process.execPath,
|
|
16
|
+
args: [cliPath],
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
async function waitForResultFile(resultPath, timeoutMs) {
|
|
20
|
+
const startedAt = Date.now();
|
|
21
|
+
for (;;) {
|
|
22
|
+
try {
|
|
23
|
+
return JSON.parse(await readFile(resultPath, "utf8"));
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
const code = error.code;
|
|
27
|
+
if (error instanceof SyntaxError) {
|
|
28
|
+
// The daemon may still be writing the result file.
|
|
29
|
+
}
|
|
30
|
+
else if (code !== "ENOENT") {
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
if (Date.now() - startedAt > timeoutMs) {
|
|
34
|
+
throw new Error(`Timed out waiting for background pair session result: ${resultPath}`);
|
|
35
|
+
}
|
|
36
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export async function spawnBackgroundPairDaemon(params) {
|
|
41
|
+
const tempDir = await mkdtemp(path.join(os.tmpdir(), "privateclaw-pair-daemon-"));
|
|
42
|
+
const resultPath = path.join(tempDir, `${randomUUID()}.json`);
|
|
43
|
+
try {
|
|
44
|
+
const invocation = await resolveCliInvocation(params.cliModuleUrl);
|
|
45
|
+
const args = [
|
|
46
|
+
...invocation.args,
|
|
47
|
+
"pair",
|
|
48
|
+
"--daemon-child",
|
|
49
|
+
"--result-file",
|
|
50
|
+
resultPath,
|
|
51
|
+
...(typeof params.ttlMs === "number"
|
|
52
|
+
? ["--ttl-ms", String(params.ttlMs)]
|
|
53
|
+
: []),
|
|
54
|
+
...(params.label ? ["--label", params.label] : []),
|
|
55
|
+
...(params.groupMode ? ["--group"] : []),
|
|
56
|
+
...(params.openInBrowser ? ["--open"] : []),
|
|
57
|
+
];
|
|
58
|
+
await new Promise((resolve, reject) => {
|
|
59
|
+
const child = spawn(invocation.command, args, {
|
|
60
|
+
detached: true,
|
|
61
|
+
stdio: "ignore",
|
|
62
|
+
env: {
|
|
63
|
+
...process.env,
|
|
64
|
+
...params.env,
|
|
65
|
+
OPENCLAW_STATE_DIR: params.stateDir,
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
child.once("error", reject);
|
|
69
|
+
child.once("spawn", () => {
|
|
70
|
+
child.unref();
|
|
71
|
+
resolve();
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
const result = await waitForResultFile(resultPath, 30_000);
|
|
75
|
+
if (!result.ok) {
|
|
76
|
+
throw new Error(result.error);
|
|
77
|
+
}
|
|
78
|
+
return result.bundle;
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export async function handoffForegroundPairToBackground(params) {
|
|
85
|
+
const tempDir = await mkdtemp(path.join(os.tmpdir(), "privateclaw-pair-handoff-"));
|
|
86
|
+
const resultPath = path.join(tempDir, `${randomUUID()}.json`);
|
|
87
|
+
const snapshotPath = path.join(tempDir, `${randomUUID()}.snapshot.json`);
|
|
88
|
+
try {
|
|
89
|
+
await writeFile(snapshotPath, JSON.stringify(params.handoffState), "utf8");
|
|
90
|
+
const invocation = await resolveCliInvocation(params.cliModuleUrl);
|
|
91
|
+
const args = [
|
|
92
|
+
...invocation.args,
|
|
93
|
+
"pair",
|
|
94
|
+
"--daemon-child",
|
|
95
|
+
"--result-file",
|
|
96
|
+
resultPath,
|
|
97
|
+
"--resume-snapshot-file",
|
|
98
|
+
snapshotPath,
|
|
99
|
+
];
|
|
100
|
+
await new Promise((resolve, reject) => {
|
|
101
|
+
const child = spawn(invocation.command, args, {
|
|
102
|
+
detached: true,
|
|
103
|
+
stdio: "ignore",
|
|
104
|
+
env: {
|
|
105
|
+
...process.env,
|
|
106
|
+
...params.env,
|
|
107
|
+
OPENCLAW_STATE_DIR: params.stateDir,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
child.once("error", reject);
|
|
111
|
+
child.once("spawn", () => {
|
|
112
|
+
child.unref();
|
|
113
|
+
resolve();
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
const result = await waitForResultFile(resultPath, 30_000);
|
|
117
|
+
if (!result.ok) {
|
|
118
|
+
throw new Error(result.error);
|
|
119
|
+
}
|
|
120
|
+
return result.resumedSessionCount;
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=pair-daemon.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pair-daemon.js","sourceRoot":"","sources":["../src/pair-daemon.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACpE,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAyBzC,KAAK,UAAU,oBAAoB,CACjC,YAAoB;IAEpB,MAAM,OAAO,GAAG,aAAa,CAAC,YAAY,CAAC,CAAC;IAC5C,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,KAAK;QACpC,CAAC,CAAC;YACE,OAAO,EAAE,OAAO,CAAC,QAAQ;YACzB,IAAI,EAAE,CAAC,U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AAI;gBACd,KAAK,EAAE,QAAQ;gBACf,GAAG,EAAE;oBACH,GAAG,OAAO,CAAC,GAAG;oBACd,GAAG,MAAM,CAAC,GAAG;oBACb,kBAAkB,EAAE,MAAM,CAAC,QAAQ;iBACpC;aACF,CAAC,CAAC;YACH,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;YAC5B,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE;gBACvB,KAAK,CAAC,KAAK,EAAE,CAAC;gBACd,OAAO,EAAE,CAAC;YACZ,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,MAAM,MAAM,GAAG,MAAM,iBAAiB,CACpC,UAAU,EACV,MAAM,CACP,CAAC;QACF,IAAI,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAChC,CAAC;QACD,OAAO,MAAM,CAAC,mBAAmB,CAAC;IACpC,CAAC;YAAS,CAAC;QACT,MAAM,EAAE,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IACtD,CAAC;AACH,CAAC"}
|
package/dist/pair-session.d.ts
CHANGED
|
@@ -6,9 +6,12 @@ export interface PairSessionOptions {
|
|
|
6
6
|
label?: string;
|
|
7
7
|
groupMode?: boolean;
|
|
8
8
|
printOnly?: boolean;
|
|
9
|
+
foreground?: boolean;
|
|
9
10
|
openInBrowser?: boolean;
|
|
10
11
|
qrMediaDir?: string;
|
|
11
12
|
writeLine?: (line: string) => void;
|
|
13
|
+
handoffToBackground?: () => Promise<string | void>;
|
|
12
14
|
}
|
|
13
15
|
export declare function parsePositiveIntegerFlag(value: string | undefined, label: string): number | undefined;
|
|
14
|
-
export declare function
|
|
16
|
+
export declare function printPairInviteBundle(bundle: PrivateClawInviteBundle, writeLine: (line: string) => void): void;
|
|
17
|
+
export declare function runPairSession({ provider, ttlMs, label, groupMode, printOnly, foreground, openInBrowser, qrMediaDir, writeLine, handoffToBackground, }: PairSessionOptions): Promise<PrivateClawInviteBundle>;
|
package/dist/pair-session.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
2
|
import { resolvePrivateClawMediaDir, writeInviteQrPng, writeInviteQrPreviewHtml, } from "./invite-qr-files.js";
|
|
3
|
-
import { buildPrivateClawShutdownMessage, formatBilingualInline, PRIVATECLAW_INVITE_URI_LABEL, PRIVATECLAW_QR_PNG_PATH_LABEL, PRIVATECLAW_WAITING_FOR_APP_MESSAGE, } from "./text.js";
|
|
3
|
+
import { buildPrivateClawBackgroundHandoffFailureMessage, buildPrivateClawShutdownMessage, formatBilingualInline, PRIVATECLAW_INVITE_URI_LABEL, PRIVATECLAW_QR_PNG_PATH_LABEL, PRIVATECLAW_SESSION_ENDED_MESSAGE, PRIVATECLAW_WAITING_FOR_APP_MESSAGE, PRIVATECLAW_WAITING_FOR_APP_WITH_BACKGROUND_MESSAGE, } from "./text.js";
|
|
4
4
|
export function parsePositiveIntegerFlag(value, label) {
|
|
5
5
|
if (!value || value.trim() === "") {
|
|
6
6
|
return undefined;
|
|
@@ -29,9 +29,50 @@ async function waitForShutdownSignal() {
|
|
|
29
29
|
process.once("SIGTERM", handleSigterm);
|
|
30
30
|
});
|
|
31
31
|
}
|
|
32
|
-
|
|
32
|
+
async function waitForStdinEof() {
|
|
33
|
+
if (!process.stdin.isTTY || process.stdin.destroyed) {
|
|
34
|
+
return new Promise(() => undefined);
|
|
35
|
+
}
|
|
36
|
+
return new Promise((resolve) => {
|
|
37
|
+
const wasPaused = process.stdin.isPaused();
|
|
38
|
+
const handleEnd = () => {
|
|
39
|
+
cleanup();
|
|
40
|
+
resolve();
|
|
41
|
+
};
|
|
42
|
+
const handleClose = () => {
|
|
43
|
+
cleanup();
|
|
44
|
+
resolve();
|
|
45
|
+
};
|
|
46
|
+
const cleanup = () => {
|
|
47
|
+
process.stdin.off("end", handleEnd);
|
|
48
|
+
process.stdin.off("close", handleClose);
|
|
49
|
+
if (wasPaused && !process.stdin.destroyed) {
|
|
50
|
+
process.stdin.pause();
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
process.stdin.once("end", handleEnd);
|
|
54
|
+
process.stdin.once("close", handleClose);
|
|
55
|
+
if (wasPaused) {
|
|
56
|
+
process.stdin.resume();
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
async function waitForSessionsToDrain(provider, pollMs = 250) {
|
|
61
|
+
while (provider.listActiveSessions().length > 0) {
|
|
62
|
+
await new Promise((resolve) => setTimeout(resolve, pollMs));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export function printPairInviteBundle(bundle, writeLine) {
|
|
66
|
+
writeLine(bundle.announcementText);
|
|
67
|
+
writeLine(`${PRIVATECLAW_INVITE_URI_LABEL}: ${bundle.inviteUri}`);
|
|
68
|
+
if (bundle.qrPngPath) {
|
|
69
|
+
writeLine(`${PRIVATECLAW_QR_PNG_PATH_LABEL}: ${bundle.qrPngPath}`);
|
|
70
|
+
}
|
|
71
|
+
writeLine(bundle.qrTerminal);
|
|
72
|
+
}
|
|
73
|
+
export async function runPairSession({ provider, ttlMs, label, groupMode = false, printOnly = false, foreground = true, openInBrowser = false, qrMediaDir, writeLine = (line) => {
|
|
33
74
|
console.log(line);
|
|
34
|
-
}, }) {
|
|
75
|
+
}, handoffToBackground, }) {
|
|
35
76
|
try {
|
|
36
77
|
const inviteBundle = await provider.createInviteBundle({
|
|
37
78
|
...(typeof ttlMs === "number" ? { ttlMs } : {}),
|
|
@@ -43,10 +84,7 @@ export async function runPairSession({ provider, ttlMs, label, groupMode = false
|
|
|
43
84
|
...inviteBundle,
|
|
44
85
|
qrPngPath: qrPng.pngPath,
|
|
45
86
|
};
|
|
46
|
-
|
|
47
|
-
writeLine(`${PRIVATECLAW_INVITE_URI_LABEL}: ${bundle.inviteUri}`);
|
|
48
|
-
writeLine(`${PRIVATECLAW_QR_PNG_PATH_LABEL}: ${qrPng.pngPath}`);
|
|
49
|
-
writeLine(bundle.qrTerminal);
|
|
87
|
+
printPairInviteBundle(bundle, writeLine);
|
|
50
88
|
if (openInBrowser) {
|
|
51
89
|
const preview = await writeInviteQrPreviewHtml(bundle, qrMediaDir ?? resolvePrivateClawMediaDir(), qrPng.pngPath);
|
|
52
90
|
await openInBrowserPreview(preview.previewFileUrl);
|
|
@@ -55,17 +93,44 @@ export async function runPairSession({ provider, ttlMs, label, groupMode = false
|
|
|
55
93
|
await provider.dispose();
|
|
56
94
|
return bundle;
|
|
57
95
|
}
|
|
58
|
-
|
|
59
|
-
process.stdin.resume();
|
|
60
|
-
try {
|
|
61
|
-
const signal = await waitForShutdownSignal();
|
|
62
|
-
writeLine(buildPrivateClawShutdownMessage(signal));
|
|
63
|
-
await provider.dispose();
|
|
96
|
+
if (!foreground) {
|
|
64
97
|
return bundle;
|
|
65
98
|
}
|
|
66
|
-
|
|
67
|
-
|
|
99
|
+
writeLine(handoffToBackground
|
|
100
|
+
? PRIVATECLAW_WAITING_FOR_APP_WITH_BACKGROUND_MESSAGE
|
|
101
|
+
: PRIVATECLAW_WAITING_FOR_APP_MESSAGE);
|
|
102
|
+
for (;;) {
|
|
103
|
+
const outcome = await Promise.race([
|
|
104
|
+
waitForShutdownSignal().then((signal) => ({ kind: "signal", signal })),
|
|
105
|
+
waitForSessionsToDrain(provider).then(() => ({ kind: "session-ended" })),
|
|
106
|
+
...(handoffToBackground
|
|
107
|
+
? [waitForStdinEof().then(() => ({ kind: "stdin-eof" }))]
|
|
108
|
+
: []),
|
|
109
|
+
]);
|
|
110
|
+
if (outcome.kind === "stdin-eof") {
|
|
111
|
+
try {
|
|
112
|
+
const message = await handoffToBackground?.();
|
|
113
|
+
if (message) {
|
|
114
|
+
writeLine(message);
|
|
115
|
+
}
|
|
116
|
+
await provider.dispose({ closeSessions: false });
|
|
117
|
+
return bundle;
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
writeLine(buildPrivateClawBackgroundHandoffFailureMessage(error instanceof Error ? error.message : String(error)));
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (outcome.kind === "signal") {
|
|
125
|
+
writeLine(buildPrivateClawShutdownMessage(outcome.signal));
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
writeLine(PRIVATECLAW_SESSION_ENDED_MESSAGE);
|
|
129
|
+
}
|
|
130
|
+
break;
|
|
68
131
|
}
|
|
132
|
+
await provider.dispose();
|
|
133
|
+
return bundle;
|
|
69
134
|
}
|
|
70
135
|
catch (error) {
|
|
71
136
|
await provider.dispose();
|
package/dist/pair-session.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pair-session.js","sourceRoot":"","sources":["../src/pair-session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAE3C,OAAO,EACL,0BAA0B,EAC1B,gBAAgB,EAChB,wBAAwB,GACzB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACL,+BAA+B,EAC/B,qBAAqB,EACrB,4BAA4B,EAC5B,6BAA6B,EAC7B,mCAAmC,
|
|
1
|
+
{"version":3,"file":"pair-session.js","sourceRoot":"","sources":["../src/pair-session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAE3C,OAAO,EACL,0BAA0B,EAC1B,gBAAgB,EAChB,wBAAwB,GACzB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACL,+CAA+C,EAC/C,+BAA+B,EAC/B,qBAAqB,EACrB,4BAA4B,EAC5B,6BAA6B,EAC7B,iCAAiC,EACjC,mCAAmC,EACnC,mDAAmD,GACpD,MAAM,WAAW,CAAC;AAgBnB,MAAM,UAAU,wBAAwB,CACtC,KAAyB,EACzB,KAAa;IAEb,IAAI,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YAClC,CAAC,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC;IAE7C,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC1C,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE;YAC9C,KAAK,EAAE,QAAQ;YACf,GAAG,CAAC,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SAC/D,CAAC,CAAC;QAEH,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAC5B,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE;YAC3B,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC;gBACf,OAAO,EAAE,CAAC;gBACV,OAAO;YACT,CAAC;YACD,MAAM,CACJ,IAAI,KAAK,CACP,qBAAqB,CACnB,kBAAkB,IAAI,IAAI,SAAS,IAAI,EACvC,iDAAiD,IAAI,IAAI,SAAS,IAAI,CACvE,CACF,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/dist/provider.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type PrivateClawAttachment, type PrivateClawInvite } from "@privateclaw/protocol";
|
|
2
|
-
import type { PrivateClawInviteBundle, PrivateClawProviderOptions } from "./types.js";
|
|
1
|
+
import { type PrivateClawAttachment, type PrivateClawInvite, type PrivateClawParticipant } from "@privateclaw/protocol";
|
|
2
|
+
import type { PrivateClawInviteBundle, PrivateClawProviderHandoffState, PrivateClawManagedSession, PrivateClawProviderOptions } from "./types.js";
|
|
3
3
|
export declare const DEFAULT_SESSION_TTL_MS: number;
|
|
4
4
|
export declare class PrivateClawProvider {
|
|
5
5
|
private readonly options;
|
|
@@ -7,8 +7,16 @@ export declare class PrivateClawProvider {
|
|
|
7
7
|
private readonly sessions;
|
|
8
8
|
constructor(options: PrivateClawProviderOptions);
|
|
9
9
|
connect(): Promise<void>;
|
|
10
|
-
dispose(
|
|
10
|
+
dispose(params?: {
|
|
11
|
+
closeSessions?: boolean;
|
|
12
|
+
}): Promise<void>;
|
|
11
13
|
listActiveSessions(): PrivateClawInvite[];
|
|
14
|
+
listManagedSessions(): PrivateClawManagedSession[];
|
|
15
|
+
exportHandoffState(): PrivateClawProviderHandoffState;
|
|
16
|
+
importHandoffState(state: PrivateClawProviderHandoffState): void;
|
|
17
|
+
suppressReconnectsForHandoff(): void;
|
|
18
|
+
resumeReconnectsAfterHandoffFailure(): void;
|
|
19
|
+
kickGroupParticipant(sessionId: string, appId: string, reason?: string): Promise<PrivateClawParticipant>;
|
|
12
20
|
private buildSessionAppUrl;
|
|
13
21
|
private requireSession;
|
|
14
22
|
private deleteSession;
|
package/dist/provider.js
CHANGED
|
@@ -97,6 +97,72 @@ function buildGeneratedParticipantLabel(sessionId, appId, usedLabels) {
|
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
|
+
function cloneConversationTurn(turn) {
|
|
101
|
+
return {
|
|
102
|
+
...turn,
|
|
103
|
+
...(turn.attachments
|
|
104
|
+
? {
|
|
105
|
+
attachments: turn.attachments.map((attachment) => ({
|
|
106
|
+
...attachment,
|
|
107
|
+
})),
|
|
108
|
+
}
|
|
109
|
+
: {}),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function buildRelayLabel(appWsUrl) {
|
|
113
|
+
try {
|
|
114
|
+
const url = new URL(appWsUrl);
|
|
115
|
+
if (!url.hostname) {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
const port = url.port;
|
|
119
|
+
const isDefaultPort = port === "" ||
|
|
120
|
+
(url.protocol === "wss:" && port === "443") ||
|
|
121
|
+
(url.protocol === "ws:" && port === "80");
|
|
122
|
+
return isDefaultPort ? url.hostname : `${url.hostname}:${port}`;
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function toProviderSessionHandoff(session) {
|
|
129
|
+
return {
|
|
130
|
+
invite: { ...session.invite },
|
|
131
|
+
...(session.label ? { label: session.label } : {}),
|
|
132
|
+
history: session.history.map(cloneConversationTurn),
|
|
133
|
+
groupMode: session.groupMode,
|
|
134
|
+
botMuted: session.botMuted,
|
|
135
|
+
participants: [...session.participants.values()].map((participant) => ({
|
|
136
|
+
...participant,
|
|
137
|
+
})),
|
|
138
|
+
removedParticipantAppIds: [...session.removedParticipantAppIds.values()],
|
|
139
|
+
state: session.state,
|
|
140
|
+
...(session.pendingRenewal
|
|
141
|
+
? { pendingRenewal: { ...session.pendingRenewal } }
|
|
142
|
+
: {}),
|
|
143
|
+
...(session.renewalReminderSentAt
|
|
144
|
+
? { renewalReminderSentAt: session.renewalReminderSentAt }
|
|
145
|
+
: {}),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function fromProviderSessionHandoff(snapshot) {
|
|
149
|
+
return {
|
|
150
|
+
invite: { ...snapshot.invite },
|
|
151
|
+
...(snapshot.label ? { label: snapshot.label } : {}),
|
|
152
|
+
history: snapshot.history.map(cloneConversationTurn),
|
|
153
|
+
groupMode: snapshot.groupMode,
|
|
154
|
+
botMuted: snapshot.botMuted,
|
|
155
|
+
participants: new Map(snapshot.participants.map((participant) => [participant.appId, { ...participant }])),
|
|
156
|
+
removedParticipantAppIds: new Set(snapshot.removedParticipantAppIds),
|
|
157
|
+
state: snapshot.state,
|
|
158
|
+
...(snapshot.pendingRenewal
|
|
159
|
+
? { pendingRenewal: { ...snapshot.pendingRenewal } }
|
|
160
|
+
: {}),
|
|
161
|
+
...(snapshot.renewalReminderSentAt
|
|
162
|
+
? { renewalReminderSentAt: snapshot.renewalReminderSentAt }
|
|
163
|
+
: {}),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
100
166
|
function buildRenewalReminderMessage(session) {
|
|
101
167
|
const command = "/renew-session";
|
|
102
168
|
if (session.groupMode) {
|
|
@@ -130,6 +196,23 @@ function toParticipantSnapshot(participant) {
|
|
|
130
196
|
joinedAt: participant.joinedAt,
|
|
131
197
|
};
|
|
132
198
|
}
|
|
199
|
+
function toManagedSessionSnapshot(session) {
|
|
200
|
+
const participants = [...session.participants.values()]
|
|
201
|
+
.sort((left, right) => left.joinedAt.localeCompare(right.joinedAt))
|
|
202
|
+
.map(toParticipantSnapshot);
|
|
203
|
+
return {
|
|
204
|
+
sessionId: session.invite.sessionId,
|
|
205
|
+
expiresAt: session.invite.expiresAt,
|
|
206
|
+
...(session.invite.providerLabel
|
|
207
|
+
? { providerLabel: session.invite.providerLabel }
|
|
208
|
+
: {}),
|
|
209
|
+
...(session.label ? { label: session.label } : {}),
|
|
210
|
+
groupMode: session.groupMode,
|
|
211
|
+
participantCount: participants.length,
|
|
212
|
+
participants,
|
|
213
|
+
state: session.state,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
133
216
|
function buildWelcomeMessage() {
|
|
134
217
|
return formatBilingualText("PrivateClaw 已连接。从现在起,消息都会通过这个一次性的端到端加密会话进行保护。", "PrivateClaw connected. Messages from now on are protected by this one-time end-to-end encrypted session.");
|
|
135
218
|
}
|
|
@@ -160,6 +243,9 @@ function buildParticipantJoinedMessage(participantLabel) {
|
|
|
160
243
|
function buildParticipantLeftMessage(participantLabel) {
|
|
161
244
|
return formatBilingualInline(`${participantLabel} 离开了群聊。`, `${participantLabel} left the group chat.`);
|
|
162
245
|
}
|
|
246
|
+
function buildParticipantRemovedByOperatorMessage(participantLabel) {
|
|
247
|
+
return formatBilingualInline(`${participantLabel} 已被移出群聊。`, `${participantLabel} was removed from the group chat.`);
|
|
248
|
+
}
|
|
163
249
|
function buildHandshakeIncompleteMessage() {
|
|
164
250
|
return formatBilingualInline("PrivateClaw 握手尚未完成。请重新扫码或重试连接。", "The PrivateClaw handshake is not complete yet. Scan the QR code again or retry the connection.");
|
|
165
251
|
}
|
|
@@ -205,6 +291,7 @@ export class PrivateClawProvider {
|
|
|
205
291
|
this.options = options;
|
|
206
292
|
this.relayClient = new RelayProviderClient({
|
|
207
293
|
providerWsUrl: options.providerWsUrl,
|
|
294
|
+
...(options.providerId ? { providerId: options.providerId } : {}),
|
|
208
295
|
onFrame: async (sessionId, envelope) => {
|
|
209
296
|
await this.handleRelayFrame(sessionId, envelope);
|
|
210
297
|
},
|
|
@@ -220,16 +307,22 @@ export class PrivateClawProvider {
|
|
|
220
307
|
async connect() {
|
|
221
308
|
await this.relayClient.connect();
|
|
222
309
|
}
|
|
223
|
-
async dispose() {
|
|
310
|
+
async dispose(params) {
|
|
311
|
+
const closeSessions = params?.closeSessions !== false;
|
|
224
312
|
const activeSessionIds = [...this.sessions.keys()];
|
|
225
313
|
for (const sessionId of activeSessionIds) {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
314
|
+
if (closeSessions) {
|
|
315
|
+
try {
|
|
316
|
+
await this.relayClient.closeSession(sessionId, "provider_shutdown");
|
|
317
|
+
}
|
|
318
|
+
catch (error) {
|
|
319
|
+
this.options.onLog?.(`[provider] failed to close session ${sessionId} during shutdown: ${error instanceof Error ? error.message : String(error)}`);
|
|
320
|
+
}
|
|
321
|
+
finally {
|
|
322
|
+
this.deleteSession(sessionId);
|
|
323
|
+
}
|
|
231
324
|
}
|
|
232
|
-
|
|
325
|
+
else {
|
|
233
326
|
this.deleteSession(sessionId);
|
|
234
327
|
}
|
|
235
328
|
}
|
|
@@ -238,6 +331,56 @@ export class PrivateClawProvider {
|
|
|
238
331
|
listActiveSessions() {
|
|
239
332
|
return [...this.sessions.values()].map((session) => session.invite);
|
|
240
333
|
}
|
|
334
|
+
listManagedSessions() {
|
|
335
|
+
return [...this.sessions.values()].map(toManagedSessionSnapshot);
|
|
336
|
+
}
|
|
337
|
+
exportHandoffState() {
|
|
338
|
+
return {
|
|
339
|
+
providerId: this.relayClient.getProviderId(),
|
|
340
|
+
sessions: [...this.sessions.values()].map(toProviderSessionHandoff),
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
importHandoffState(state) {
|
|
344
|
+
if (state.providerId !== this.relayClient.getProviderId()) {
|
|
345
|
+
throw new Error("Handoff providerId does not match this provider instance.");
|
|
346
|
+
}
|
|
347
|
+
for (const sessionId of [...this.sessions.keys()]) {
|
|
348
|
+
this.deleteSession(sessionId);
|
|
349
|
+
}
|
|
350
|
+
for (const snapshot of state.sessions) {
|
|
351
|
+
const session = fromProviderSessionHandoff(snapshot);
|
|
352
|
+
this.sessions.set(session.invite.sessionId, session);
|
|
353
|
+
this.scheduleRenewalReminder(session.invite.sessionId);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
suppressReconnectsForHandoff() {
|
|
357
|
+
this.relayClient.suppressReconnects();
|
|
358
|
+
}
|
|
359
|
+
resumeReconnectsAfterHandoffFailure() {
|
|
360
|
+
this.relayClient.resumeReconnects();
|
|
361
|
+
}
|
|
362
|
+
async kickGroupParticipant(sessionId, appId, reason = "participant_removed") {
|
|
363
|
+
const session = this.requireSession(sessionId);
|
|
364
|
+
if (!session.groupMode) {
|
|
365
|
+
throw new Error("Participant removal is only available for group sessions.");
|
|
366
|
+
}
|
|
367
|
+
const normalizedAppId = appId.trim();
|
|
368
|
+
if (normalizedAppId === "") {
|
|
369
|
+
throw new Error("Participant appId is required.");
|
|
370
|
+
}
|
|
371
|
+
const participant = session.participants.get(normalizedAppId);
|
|
372
|
+
if (!participant) {
|
|
373
|
+
throw new Error(`Participant ${normalizedAppId} is not part of session ${sessionId}.`);
|
|
374
|
+
}
|
|
375
|
+
session.participants.delete(normalizedAppId);
|
|
376
|
+
session.removedParticipantAppIds.add(normalizedAppId);
|
|
377
|
+
await this.relayClient.closeApp(sessionId, normalizedAppId, reason);
|
|
378
|
+
if (session.participants.size > 0) {
|
|
379
|
+
await this.sendSystemMessageToGroupParticipants(sessionId, buildParticipantRemovedByOperatorMessage(participant.displayName));
|
|
380
|
+
await this.sendCapabilities(sessionId);
|
|
381
|
+
}
|
|
382
|
+
return toParticipantSnapshot(participant);
|
|
383
|
+
}
|
|
241
384
|
buildSessionAppUrl(sessionId) {
|
|
242
385
|
const url = new URL(this.options.appWsUrl);
|
|
243
386
|
url.searchParams.set("sessionId", sessionId);
|
|
@@ -588,9 +731,14 @@ export class PrivateClawProvider {
|
|
|
588
731
|
});
|
|
589
732
|
switch (payload.kind) {
|
|
590
733
|
case "client_hello": {
|
|
734
|
+
const normalizedAppId = payload.appId?.trim() || "legacy-app";
|
|
735
|
+
if (session.removedParticipantAppIds.has(normalizedAppId)) {
|
|
736
|
+
await this.relayClient.closeApp(sessionId, normalizedAppId, "participant_removed");
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
591
739
|
const participantState = await this.upsertParticipant(sessionId, {
|
|
592
740
|
sentAt: payload.sentAt,
|
|
593
|
-
...(payload.appId ? { appId:
|
|
741
|
+
...(payload.appId ? { appId: normalizedAppId } : {}),
|
|
594
742
|
...(payload.deviceLabel
|
|
595
743
|
? { deviceLabel: payload.deviceLabel }
|
|
596
744
|
: {}),
|
|
@@ -866,6 +1014,7 @@ export class PrivateClawProvider {
|
|
|
866
1014
|
async createInviteBundle(params) {
|
|
867
1015
|
await this.connect();
|
|
868
1016
|
const groupMode = params?.groupMode === true;
|
|
1017
|
+
const relayLabel = buildRelayLabel(this.options.appWsUrl);
|
|
869
1018
|
const { sessionId, expiresAt } = await this.relayClient.createSession(params?.ttlMs ?? this.options.defaultTtlMs, params?.label, groupMode);
|
|
870
1019
|
const invite = {
|
|
871
1020
|
version: 1,
|
|
@@ -875,13 +1024,16 @@ export class PrivateClawProvider {
|
|
|
875
1024
|
expiresAt,
|
|
876
1025
|
...(groupMode ? { groupMode: true } : {}),
|
|
877
1026
|
...(this.options.providerLabel ? { providerLabel: this.options.providerLabel } : {}),
|
|
1027
|
+
...(relayLabel ? { relayLabel } : {}),
|
|
878
1028
|
};
|
|
879
1029
|
this.sessions.set(sessionId, {
|
|
880
1030
|
invite,
|
|
1031
|
+
...(params?.label ? { label: params.label } : {}),
|
|
881
1032
|
history: [],
|
|
882
1033
|
groupMode,
|
|
883
1034
|
botMuted: false,
|
|
884
1035
|
participants: new Map(),
|
|
1036
|
+
removedParticipantAppIds: new Set(),
|
|
885
1037
|
state: "awaiting_hello",
|
|
886
1038
|
});
|
|
887
1039
|
this.scheduleRenewalReminder(sessionId);
|