@privateclaw/privateclaw 0.1.15 → 0.1.17

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.
Files changed (40) hide show
  1. package/README.md +44 -1
  2. package/dist/audio-transcriber.d.ts +46 -0
  3. package/dist/audio-transcriber.js +433 -0
  4. package/dist/audio-transcriber.js.map +1 -0
  5. package/dist/bridges/openclaw-agent-bridge.d.ts +4 -1
  6. package/dist/bridges/openclaw-agent-bridge.js +81 -8
  7. package/dist/bridges/openclaw-agent-bridge.js.map +1 -1
  8. package/dist/cli.js +57 -6
  9. package/dist/cli.js.map +1 -1
  10. package/dist/openclaw-plugin.js +150 -51
  11. package/dist/openclaw-plugin.js.map +1 -1
  12. package/dist/pair-daemon.d.ts +6 -0
  13. package/dist/pair-daemon.js +31 -2
  14. package/dist/pair-daemon.js.map +1 -1
  15. package/dist/provider.d.ts +2 -0
  16. package/dist/provider.js +103 -14
  17. package/dist/provider.js.map +1 -1
  18. package/dist/session-control.d.ts +7 -0
  19. package/dist/session-control.js +122 -0
  20. package/dist/session-control.js.map +1 -1
  21. package/dist/text.d.ts +2 -0
  22. package/dist/text.js +3 -1
  23. package/dist/text.js.map +1 -1
  24. package/dist/types.d.ts +8 -0
  25. package/package.json +1 -1
  26. package/src/audio-transcriber.test.ts +299 -0
  27. package/src/audio-transcriber.ts +615 -0
  28. package/src/bridges/openclaw-agent-bridge.test.ts +78 -13
  29. package/src/bridges/openclaw-agent-bridge.ts +109 -7
  30. package/src/cli.ts +73 -5
  31. package/src/openclaw-plugin.test.ts +12 -2
  32. package/src/openclaw-plugin.ts +191 -54
  33. package/src/pair-daemon.test.ts +48 -0
  34. package/src/pair-daemon.ts +41 -2
  35. package/src/provider.test.ts +304 -0
  36. package/src/provider.ts +146 -20
  37. package/src/session-control.test.ts +80 -0
  38. package/src/session-control.ts +151 -0
  39. package/src/text.ts +12 -2
  40. package/src/types.ts +12 -0
@@ -21,6 +21,7 @@ import {
21
21
  buildManagedSessionQrLegacyLines,
22
22
  closeManagedSessionsFromStateDir,
23
23
  closeManagedSessionFromStateDir,
24
+ followManagedSessionLogFromStateDir,
24
25
  getManagedSessionQrBundleFromStateDir,
25
26
  isManagedSessionQrLegacyResult,
26
27
  listManagedSessionsFromStateDir,
@@ -84,6 +85,19 @@ function waitForClose(socket: WebSocket): Promise<void> {
84
85
  });
85
86
  }
86
87
 
88
+ async function waitForCondition(
89
+ predicate: () => boolean,
90
+ timeoutMs = 2_000,
91
+ ): Promise<void> {
92
+ const startedAt = Date.now();
93
+ while (!predicate()) {
94
+ if (Date.now() - startedAt >= timeoutMs) {
95
+ throw new Error("Timed out waiting for test condition.");
96
+ }
97
+ await new Promise((resolve) => setTimeout(resolve, 20));
98
+ }
99
+ }
100
+
87
101
  async function spawnLegacyManagedSessionHost(params: {
88
102
  stateDir: string;
89
103
  sessionId: string;
@@ -328,6 +342,72 @@ test("session control can print and notify a managed session QR", async (t) => {
328
342
  await Promise.all([waitForClose(appOne), waitForClose(appTwo)]);
329
343
  });
330
344
 
345
+ test("session control can follow a managed OpenClaw session log", async (t) => {
346
+ const stateDir = await mkdtemp(path.join(os.tmpdir(), "privateclaw-session-control-follow-"));
347
+ t.after(async () => {
348
+ await rm(stateDir, { recursive: true, force: true });
349
+ });
350
+
351
+ const sessionId = "follow-session";
352
+ const child = await spawnLegacyManagedSessionHost({
353
+ stateDir,
354
+ sessionId,
355
+ });
356
+ t.after(async () => {
357
+ if (!child.killed && child.exitCode == null) {
358
+ child.kill("SIGTERM");
359
+ }
360
+ if (child.exitCode == null) {
361
+ await new Promise<void>((resolve) => {
362
+ child.once("exit", () => resolve());
363
+ });
364
+ }
365
+ });
366
+
367
+ const printed: string[] = [];
368
+ const controller = new AbortController();
369
+ const followPromise = followManagedSessionLogFromStateDir({
370
+ stateDir,
371
+ sessionId,
372
+ pollMs: 10,
373
+ signal: controller.signal,
374
+ writeLine: (line) => {
375
+ printed.push(line);
376
+ },
377
+ });
378
+
379
+ const logPath = path.join(stateDir, "agents", "main", "sessions", `${sessionId}.jsonl`);
380
+ await mkdir(path.dirname(logPath), { recursive: true });
381
+ const firstLine = JSON.stringify({
382
+ type: "message",
383
+ message: {
384
+ role: "assistant",
385
+ content: [{ type: "text", text: "hello from follow test" }],
386
+ },
387
+ });
388
+ await writeFile(logPath, `${firstLine}\n`, "utf8");
389
+ await waitForCondition(() => printed.includes(firstLine));
390
+
391
+ const secondLine = JSON.stringify({
392
+ type: "message",
393
+ message: {
394
+ role: "toolResult",
395
+ content: [{ type: "text", text: "tool output" }],
396
+ },
397
+ });
398
+ await writeFile(logPath, `${firstLine}\n${secondLine}\n`, "utf8");
399
+ await waitForCondition(() => printed.includes(secondLine));
400
+
401
+ controller.abort();
402
+ await followPromise;
403
+
404
+ assert.ok(
405
+ printed.some((line) => line.includes(`Following the OpenClaw session log for session ${sessionId}`)),
406
+ );
407
+ assert.ok(printed.includes(firstLine));
408
+ assert.ok(printed.includes(secondLine));
409
+ });
410
+
331
411
  test("session control can terminate a managed session", async (t) => {
332
412
  const relay = createRelayServer({
333
413
  host: "127.0.0.1",
@@ -254,6 +254,60 @@ export function resolvePrivateClawControlDir(stateDir: string): string {
254
254
  return path.join(resolvePrivateClawStateDir(stateDir), "privateclaw", "control");
255
255
  }
256
256
 
257
+ async function resolveOpenClawSessionLogPath(params: {
258
+ stateDir: string;
259
+ sessionId: string;
260
+ }): Promise<string | undefined> {
261
+ const agentsDir = path.join(resolvePrivateClawStateDir(params.stateDir), "agents");
262
+ try {
263
+ const agentEntries = await readdir(agentsDir, { withFileTypes: true });
264
+ for (const entry of agentEntries) {
265
+ if (!entry.isDirectory()) {
266
+ continue;
267
+ }
268
+ const candidate = path.join(agentsDir, entry.name, "sessions", `${params.sessionId}.jsonl`);
269
+ try {
270
+ await access(candidate);
271
+ return candidate;
272
+ } catch (error) {
273
+ const code = (error as NodeJS.ErrnoException).code;
274
+ if (code === "ENOENT") {
275
+ continue;
276
+ }
277
+ throw error;
278
+ }
279
+ }
280
+ } catch (error) {
281
+ const code = (error as NodeJS.ErrnoException).code;
282
+ if (code === "ENOENT") {
283
+ return undefined;
284
+ }
285
+ throw error;
286
+ }
287
+
288
+ return undefined;
289
+ }
290
+
291
+ async function waitForPollInterval(
292
+ pollMs: number,
293
+ signal?: AbortSignal,
294
+ ): Promise<void> {
295
+ if (signal?.aborted) {
296
+ return;
297
+ }
298
+ await new Promise<void>((resolve) => {
299
+ const timeout = setTimeout(() => {
300
+ signal?.removeEventListener("abort", handleAbort);
301
+ resolve();
302
+ }, pollMs);
303
+ const handleAbort = () => {
304
+ clearTimeout(timeout);
305
+ resolve();
306
+ };
307
+ signal?.addEventListener("abort", handleAbort, { once: true });
308
+ });
309
+ }
310
+
257
311
  export function buildManagedSessionsReportLines(
258
312
  listings: ReadonlyArray<PrivateClawDiscoveredSessionListing>,
259
313
  ): string[] {
@@ -827,3 +881,100 @@ export async function getManagedSessionQrBundleFromStateDir(params: {
827
881
  throw error;
828
882
  }
829
883
  }
884
+
885
+ export async function followManagedSessionLogFromStateDir(params: {
886
+ stateDir: string;
887
+ sessionId: string;
888
+ writeLine?: (line: string) => void;
889
+ pollMs?: number;
890
+ signal?: AbortSignal;
891
+ }): Promise<void> {
892
+ const writeLine = params.writeLine ?? ((line: string) => console.log(line));
893
+ const pollMs = params.pollMs ?? 250;
894
+ let sessionLogPath = await resolveOpenClawSessionLogPath({
895
+ stateDir: params.stateDir,
896
+ sessionId: params.sessionId,
897
+ });
898
+
899
+ try {
900
+ await resolveManagedSessionDescriptor({
901
+ stateDir: params.stateDir,
902
+ sessionId: params.sessionId,
903
+ });
904
+ } catch (error) {
905
+ if (!sessionLogPath) {
906
+ throw error;
907
+ }
908
+ }
909
+
910
+ let announcedWaiting = false;
911
+ let announcedFollow = false;
912
+ let sizeBytes = 0;
913
+ let trailingPartialLine = "";
914
+
915
+ for (;;) {
916
+ if (params.signal?.aborted) {
917
+ return;
918
+ }
919
+
920
+ if (!sessionLogPath) {
921
+ sessionLogPath = await resolveOpenClawSessionLogPath({
922
+ stateDir: params.stateDir,
923
+ sessionId: params.sessionId,
924
+ });
925
+ if (!sessionLogPath) {
926
+ if (!announcedWaiting) {
927
+ writeLine(
928
+ formatBilingualInline(
929
+ `正在等待会话 ${params.sessionId} 的 OpenClaw session 日志文件出现……`,
930
+ `Waiting for the OpenClaw session log for session ${params.sessionId} to appear...`,
931
+ ),
932
+ );
933
+ announcedWaiting = true;
934
+ }
935
+ await waitForPollInterval(pollMs, params.signal);
936
+ continue;
937
+ }
938
+ }
939
+
940
+ if (!announcedFollow) {
941
+ writeLine(
942
+ formatBilingualInline(
943
+ `开始跟随会话 ${params.sessionId} 的 OpenClaw session 日志:${sessionLogPath}`,
944
+ `Following the OpenClaw session log for session ${params.sessionId}: ${sessionLogPath}`,
945
+ ),
946
+ );
947
+ announcedFollow = true;
948
+ }
949
+
950
+ try {
951
+ const sessionLogBuffer = await readFile(sessionLogPath);
952
+ if (sessionLogBuffer.length < sizeBytes) {
953
+ sizeBytes = 0;
954
+ trailingPartialLine = "";
955
+ }
956
+ const deltaBuffer = sessionLogBuffer.subarray(sizeBytes);
957
+ if (deltaBuffer.length > 0) {
958
+ const chunk = trailingPartialLine + deltaBuffer.toString("utf8");
959
+ const lines = chunk.split(/\r?\n/gu);
960
+ trailingPartialLine = lines.pop() ?? "";
961
+ for (const line of lines) {
962
+ if (line.trim() !== "") {
963
+ writeLine(line);
964
+ }
965
+ }
966
+ sizeBytes = sessionLogBuffer.length;
967
+ }
968
+ } catch (error) {
969
+ const code = (error as NodeJS.ErrnoException).code;
970
+ if (code === "ENOENT") {
971
+ sessionLogPath = undefined;
972
+ announcedFollow = false;
973
+ continue;
974
+ }
975
+ throw error;
976
+ }
977
+
978
+ await waitForPollInterval(pollMs, params.signal);
979
+ }
980
+ }
package/src/text.ts CHANGED
@@ -57,6 +57,11 @@ export const PRIVATECLAW_CLI_SESSIONS_QR_DESCRIPTION = formatBilingualInline(
57
57
  "Print the QR code for a selected session, optionally open a browser preview, and optionally notify the current participants.",
58
58
  );
59
59
 
60
+ export const PRIVATECLAW_CLI_SESSIONS_FOLLOW_DESCRIPTION = formatBilingualInline(
61
+ "跟随指定会话的 OpenClaw session JSONL 日志,便于实时排障。",
62
+ "Follow the OpenClaw session JSONL log for a selected session to debug it in real time.",
63
+ );
64
+
60
65
  export const PRIVATECLAW_CLI_SESSIONS_KILL_DESCRIPTION = formatBilingualInline(
61
66
  "终止指定的活动会话;对于旧的后台 daemon,会回退为终止整个 daemon host。",
62
67
  "Terminate a selected active session; for older background daemons, this falls back to terminating the whole daemon host.",
@@ -113,6 +118,11 @@ export const PRIVATECLAW_CLI_FOREGROUND_OPTION_DESCRIPTION = formatBilingualInli
113
118
  "Keep the current command in the foreground until the session ends or you press Ctrl+C; in supported runtimes you can also press Ctrl+D to move it into the background.",
114
119
  );
115
120
 
121
+ export const PRIVATECLAW_CLI_VERBOSE_OPTION_DESCRIPTION = formatBilingualInline(
122
+ "输出更详细的 provider / bridge 调试日志。",
123
+ "Emit more detailed provider / bridge debug logs.",
124
+ );
125
+
116
126
  export const PRIVATECLAW_INVITE_URI_LABEL = formatBilingualInline(
117
127
  "邀请链接",
118
128
  "Invite URI",
@@ -172,8 +182,8 @@ export function buildPrivateClawCommandErrorMessage(details: string): string {
172
182
  }
173
183
 
174
184
  export const PRIVATECLAW_RENEW_SESSION_DESCRIPTION = formatBilingualInline(
175
- "轮换当前 PrivateClaw 会话密钥,并将当前会话延长 8 小时。",
176
- "Rotate the current PrivateClaw session key and extend this session by 8 hours.",
185
+ "轮换当前 PrivateClaw 会话密钥,并将当前会话延长 24 小时。",
186
+ "Rotate the current PrivateClaw session key and extend this session by 24 hours.",
177
187
  );
178
188
 
179
189
  export const PRIVATECLAW_SESSION_QR_DESCRIPTION = formatBilingualInline(
package/src/types.ts CHANGED
@@ -38,6 +38,16 @@ export interface PrivateClawAudioTranscriptionRequest {
38
38
  attachments: ReadonlyArray<PrivateClawAttachment>;
39
39
  }
40
40
 
41
+ export interface PrivateClawAudioTranscriber {
42
+ transcribeAudioAttachments(
43
+ params: PrivateClawAudioTranscriptionRequest,
44
+ ): Promise<string>;
45
+ }
46
+
47
+ export interface PrivateClawVerboseController {
48
+ enabled: boolean;
49
+ }
50
+
41
51
  export interface PrivateClawAgentBridge {
42
52
  handleUserMessage(params: {
43
53
  sessionId: string;
@@ -55,11 +65,13 @@ export interface PrivateClawProviderOptions {
55
65
  providerWsUrl: string;
56
66
  appWsUrl: string;
57
67
  bridge: PrivateClawAgentBridge;
68
+ audioTranscriber?: PrivateClawAudioTranscriber;
58
69
  providerId?: string;
59
70
  defaultTtlMs?: number;
60
71
  providerLabel?: string;
61
72
  welcomeMessage?: string;
62
73
  commandsProvider?: () => Promise<PrivateClawSlashCommand[]>;
74
+ verboseController?: PrivateClawVerboseController;
63
75
  onLog?: (message: string) => void;
64
76
  }
65
77