File size: 24,872 Bytes
e18726e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
import { spawn, spawnSync, type ChildProcess } from "node:child_process";
import { randomUUID } from "node:crypto";
import { asPositiveFiniteNumber } from "@openclaw/normalization-core/number-coercion";
import { asOptionalRecord } from "@openclaw/normalization-core/record-coerce";
import { readNonEmptyStringPreservingWhitespace as readNonEmptyString } from "@openclaw/normalization-core/string-coerce";
import { formatErrorMessage } from "../infra/errors.js";
import type { MeetingAudioBackendSelection, MeetingAudioRuntime } from "./audio-backend.js";
import { decodeMeetingAudioBase64 } from "./audio-base64.js";
import { terminateMeetingBridgeProcess } from "./bridge-process.js";
import { splitCommandArgv } from "./command-argv.js";
import {
  prepareMeetingNodeAudio,
  readMeetingNodeCommand,
  type MeetingNodeAudioConfig,
} from "./node-audio-config.js";
import { MeetingNodeAudioPullWaiters } from "./node-audio-pull-waiters.js";
import type { MeetingRealtimeAudioFormat } from "./realtime-audio-format.js";

export type { MeetingNodeAudioConfig } from "./node-audio-config.js";

const NODE_BRIDGE_TERMINATION_GRACE_MS = 2_000;
const NODE_BRIDGE_INPUT_DRAIN_MS = NODE_BRIDGE_TERMINATION_GRACE_MS + 1_000;
const NODE_BRIDGE_TERMINAL_RETENTION_MS = 5_000;
const NODE_BRIDGE_MAX_QUEUED_INPUT_CHUNKS = 200;
const NODE_BRIDGE_MAX_QUEUED_INPUT_BYTES = 1024 * 1024;

type NodeOutputWriteWaiter = {
  output: ChildProcess;
  release: () => void;
};

type NodeBridgeSession = {
  id: string;
  url?: string;
  mode?: string;
  outputCommand: { command: string; args: string[] };
  input?: ChildProcess;
  output?: ChildProcess;
  chunks: Buffer[];
  queuedInputBytes: number;
  waiters: MeetingNodeAudioPullWaiters;
  closed: boolean;
  createdAt: string;
  lastInputAt?: string;
  lastOutputAt?: string;
  lastClearAt?: string;
  lastInputBytes: number;
  lastOutputBytes: number;
  clearCount: number;
  outputGeneration: number;
  outputWriteWaiters: Set<NodeOutputWriteWaiter>;
  stopPromise?: Promise<void>;
  retiredOutputStops: Set<Promise<void>>;
  stopping: boolean;
  discardQueuedAudioOnStop: boolean;
  terminalEvictionTimer?: ReturnType<typeof setTimeout>;
};

export type MeetingNodeHostOptions = {
  commandName: string;
  displayName: string;
  browserLabel: string;
  bridgeIdPrefix: string;
  defaultAudioInputCommand: readonly string[];
  defaultAudioOutputCommand: readonly string[];
  defaultAudio?: {
    backend?: MeetingAudioBackendSelection;
    bufferBytes: number;
    format: MeetingRealtimeAudioFormat;
  };
  talkBackModes: ReadonlySet<string>;
  agentMode: string;
  normalizeUrl(input: unknown): string;
  normalizeMeetingKey(url?: string): string | undefined;
  assertAudioAvailable(timeoutMs: number): void | Promise<void>;
  prepareAudio?(config: MeetingNodeAudioConfig, timeoutMs: number): Promise<MeetingAudioRuntime>;
  browser: {
    application: string;
    buildProfileArgs(profile: string): string[];
    openedStatus: string;
    openedNotes: string[];
  };
};

function readPositiveNumberOr(value: unknown, fallback: number): number {
  return asPositiveFiniteNumber(value) ?? fallback;
}

function readOutputGeneration(value: unknown): number | undefined {
  if (value === undefined) {
    return undefined;
  }
  if (typeof value === "number" && Number.isSafeInteger(value) && value >= 0) {
    return value;
  }
  throw new Error("outputGeneration must be a non-negative integer");
}

function runCommandWithTimeout(argv: string[], timeoutMs: number) {
  const { command, args } = splitCommandArgv(argv, "command");
  const result = spawnSync(command, args, { encoding: "utf8", timeout: timeoutMs });
  const errorMessage = result.error ? formatErrorMessage(result.error) : "";
  const stderr =
    errorMessage && result.stderr
      ? `${errorMessage}: ${result.stderr}`
      : errorMessage || result.stderr || (result.signal ? `terminated by ${result.signal}` : "");
  return {
    code: typeof result.status === "number" ? result.status : 1,
    stdout: result.stdout ?? "",
    stderr,
  };
}

function waitForInputDrain(
  stream: ChildProcess["stdout"] | undefined,
  timeoutMs: number,
): Promise<void> {
  if (!stream || stream.readableEnded || stream.destroyed) {
    return Promise.resolve();
  }
  return new Promise<void>((resolve) => {
    let settled = false;
    const finish = () => {
      if (settled) {
        return;
      }
      settled = true;
      clearTimeout(timeout);
      stream.off("end", finish);
      stream.off("close", finish);
      resolve();
    };
    const timeout = setTimeout(finish, timeoutMs);
    timeout.unref?.();
    stream.once("end", finish);
    stream.once("close", finish);
    if (stream.readableEnded || stream.destroyed) {
      finish();
    }
  });
}

export function createMeetingNodeHost(options: MeetingNodeHostOptions): {
  handleCommand(paramsJSON?: string | null): Promise<string>;
} {
  const sessions = new Map<string, NodeBridgeSession>();

  const wake = (session: NodeBridgeSession) => {
    session.waiters.wake();
  };

  const releaseOutputWriteWaiters = (session: NodeBridgeSession, output?: ChildProcess): void => {
    for (const waiter of session.outputWriteWaiters) {
      if (!output || waiter.output === output) {
        waiter.release();
      }
    }
  };

  const retireOutputProcess = (session: NodeBridgeSession, outputProcess?: ChildProcess) => {
    const stopPromise = terminateMeetingBridgeProcess(outputProcess, {
      graceMs: NODE_BRIDGE_TERMINATION_GRACE_MS,
    });
    session.retiredOutputStops.add(stopPromise);
    void stopPromise.finally(() => {
      session.retiredOutputStops.delete(stopPromise);
    });
  };

  const deleteSession = (session: NodeBridgeSession): void => {
    if (session.terminalEvictionTimer) {
      clearTimeout(session.terminalEvictionTimer);
      session.terminalEvictionTimer = undefined;
    }
    session.chunks.length = 0;
    session.queuedInputBytes = 0;
    if (sessions.get(session.id) === session) {
      sessions.delete(session.id);
    }
  };

  const armTerminalEviction = (session: NodeBridgeSession): void => {
    if (session.terminalEvictionTimer) {
      clearTimeout(session.terminalEvictionTimer);
    }
    session.terminalEvictionTimer = setTimeout(() => {
      deleteSession(session);
    }, NODE_BRIDGE_TERMINAL_RETENTION_MS);
    session.terminalEvictionTimer.unref?.();
  };

  const stopSession = (
    session: NodeBridgeSession,
    stopOptions: { discardQueuedAudio?: boolean } = {},
  ): Promise<void> => {
    session.stopping = true;
    if (stopOptions.discardQueuedAudio) {
      session.discardQueuedAudioOnStop = true;
      if (session.terminalEvictionTimer) {
        clearTimeout(session.terminalEvictionTimer);
        session.terminalEvictionTimer = undefined;
      }
      session.chunks.length = 0;
      session.queuedInputBytes = 0;
      if (!session.closed) {
        session.closed = true;
      }
      wake(session);
    }
    releaseOutputWriteWaiters(session);
    // Process and stream errors can arrive together during teardown. Close once
    // so every caller shares one bounded process-termination promise.
    if (session.stopPromise) {
      return session.stopPromise;
    }
    const terminalReady = session.discardQueuedAudioOnStop
      ? Promise.resolve()
      : waitForInputDrain(session.input?.stdout, NODE_BRIDGE_INPUT_DRAIN_MS).then(() => {
          if (session.discardQueuedAudioOnStop || session.closed) {
            return;
          }
          session.closed = true;
          wake(session);
        });
    session.stopPromise = Promise.all([
      terminateMeetingBridgeProcess(session.input, {
        graceMs: NODE_BRIDGE_TERMINATION_GRACE_MS,
      }),
      terminateMeetingBridgeProcess(session.output, {
        graceMs: NODE_BRIDGE_TERMINATION_GRACE_MS,
      }),
      ...session.retiredOutputStops,
      terminalReady,
    ]).then(() => {
      if (session.discardQueuedAudioOnStop) {
        deleteSession(session);
        return;
      }
      // A terminal pull normally deletes the session first. This deadline
      // releases bounded retention when no consumer returns to drain it.
      armTerminalEviction(session);
    });
    return session.stopPromise;
  };

  const attachOutputProcessHandlers = (session: NodeBridgeSession, outputProcess: ChildProcess) => {
    const stopIfCurrent = () => {
      if (session.output === outputProcess) {
        void stopSession(session);
      }
    };
    outputProcess.on("exit", stopIfCurrent);
    outputProcess.on("error", stopIfCurrent);
    outputProcess.stdin?.on("error", stopIfCurrent);
    outputProcess.stderr?.on("error", stopIfCurrent);
  };

  const startOutputProcess = (command: { command: string; args: string[] }) =>
    spawn(command.command, command.args, { stdio: ["pipe", "ignore", "pipe"] });

  const startCommandPair = (params: {
    inputCommand: string[];
    outputCommand: string[];
    url?: string;
    mode?: string;
  }): NodeBridgeSession => {
    const input = splitCommandArgv(params.inputCommand, "audio command");
    const output = splitCommandArgv(params.outputCommand, "audio command");
    const session: NodeBridgeSession = {
      id: `${options.bridgeIdPrefix}${randomUUID()}`,
      url: params.url,
      mode: params.mode,
      outputCommand: output,
      chunks: [],
      queuedInputBytes: 0,
      waiters: new MeetingNodeAudioPullWaiters(),
      closed: false,
      createdAt: new Date().toISOString(),
      lastInputBytes: 0,
      lastOutputBytes: 0,
      clearCount: 0,
      outputGeneration: 0,
      outputWriteWaiters: new Set(),
      retiredOutputStops: new Set(),
      stopping: false,
      discardQueuedAudioOnStop: false,
    };
    const outputProcess = startOutputProcess(output);
    let inputProcess: ChildProcess;
    try {
      inputProcess = spawn(input.command, input.args, {
        stdio: ["ignore", "pipe", "pipe"],
      });
    } catch (error) {
      void terminateMeetingBridgeProcess(outputProcess, {
        graceMs: NODE_BRIDGE_TERMINATION_GRACE_MS,
      });
      throw error;
    }
    session.input = inputProcess;
    session.output = outputProcess;
    inputProcess.stdout?.on("data", (chunk) => {
      if (session.discardQueuedAudioOnStop || session.closed) {
        return;
      }
      const audio = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
      session.lastInputAt = new Date().toISOString();
      session.lastInputBytes += audio.byteLength;
      // Keep the existing normal 200-chunk window while bounding custom capture
      // commands whose stdout chunks are much larger than the default 4 KiB.
      const retainedAudio = Buffer.from(
        audio.subarray(Math.max(0, audio.byteLength - NODE_BRIDGE_MAX_QUEUED_INPUT_BYTES)),
      );
      session.chunks.push(retainedAudio);
      session.queuedInputBytes += retainedAudio.byteLength;
      while (
        session.chunks.length > NODE_BRIDGE_MAX_QUEUED_INPUT_CHUNKS ||
        session.queuedInputBytes > NODE_BRIDGE_MAX_QUEUED_INPUT_BYTES
      ) {
        const evicted = session.chunks.shift();
        if (evicted) {
          session.queuedInputBytes -= evicted.byteLength;
        }
      }
      if (!session.stopPromise) {
        wake(session);
      }
    });
    const stop = () => {
      void stopSession(session);
    };
    inputProcess.on("exit", stop);
    inputProcess.on("error", stop);
    inputProcess.stdout?.on("error", stop);
    inputProcess.stderr?.on("error", stop);
    attachOutputProcessHandlers(session, outputProcess);
    sessions.set(session.id, session);
    return session;
  };

  const pullAudio = async (params: Record<string, unknown>) => {
    const bridgeId = readNonEmptyString(params.bridgeId);
    if (!bridgeId) {
      throw new Error("bridgeId required");
    }
    const session = sessions.get(bridgeId);
    if (!session) {
      throw new Error(`unknown bridgeId: ${bridgeId}`);
    }
    const timeoutMs = Math.min(readPositiveNumberOr(params.timeoutMs, 250), 2_000);
    if (session.chunks.length === 0 && !session.closed) {
      await session.waiters.wait(timeoutMs);
    }
    const chunk = session.chunks.shift();
    if (chunk) {
      session.queuedInputBytes -= chunk.byteLength;
    }
    const closed = session.closed && session.chunks.length === 0;
    if (closed) {
      deleteSession(session);
    } else if (chunk && session.closed && session.terminalEvictionTimer) {
      // Terminal audio can span many node RPCs. Retain only while the consumer
      // keeps making progress, then evict after one full idle window.
      armTerminalEviction(session);
    }
    return {
      bridgeId,
      closed,
      base64: chunk ? chunk.toString("base64") : undefined,
    };
  };

  const staleOutputResult = (session: NodeBridgeSession) => ({
    bridgeId: session.id,
    ok: true,
    stale: true,
  });

  const writeOutputChunk = (
    session: NodeBridgeSession,
    output: ChildProcess,
    audio: Buffer,
  ): Promise<void> =>
    new Promise<void>((resolve, reject) => {
      const stdin = output.stdin;
      if (!stdin) {
        reject(new Error("audio output stream is closed"));
        return;
      }
      let settled = false;
      const finish = (error?: Error) => {
        if (settled) {
          return;
        }
        settled = true;
        session.outputWriteWaiters.delete(waiter);
        if (error) {
          reject(error);
        } else {
          resolve();
        }
      };
      const waiter: NodeOutputWriteWaiter = { output, release: () => finish() };
      session.outputWriteWaiters.add(waiter);
      try {
        stdin.write(audio, (error) => finish(error ?? undefined));
      } catch (error) {
        finish(error instanceof Error ? error : new Error(formatErrorMessage(error)));
        return;
      }
      if (stdin.destroyed || stdin.writableEnded) {
        finish(new Error("audio output stream is closed"));
      }
    });

  const pushAudio = async (params: Record<string, unknown>) => {
    const bridgeId = readNonEmptyString(params.bridgeId);
    const base64 = readNonEmptyString(params.base64);
    if (!bridgeId || !base64) {
      throw new Error("bridgeId and base64 required");
    }
    const session = sessions.get(bridgeId);
    if (!session || session.stopping || session.closed) {
      throw new Error(`bridge is not open: ${bridgeId}`);
    }
    const requestedGeneration = readOutputGeneration(params.outputGeneration);
    if (requestedGeneration !== undefined && requestedGeneration !== session.outputGeneration) {
      return staleOutputResult(session);
    }
    const output = session.output;
    if (!output?.stdin) {
      throw new Error(`bridge is not open: ${bridgeId}`);
    }
    const audio = decodeMeetingAudioBase64(base64, "pushAudio");
    try {
      await writeOutputChunk(session, output, audio);
    } catch {
      if (
        session.output !== output ||
        (requestedGeneration !== undefined && requestedGeneration !== session.outputGeneration)
      ) {
        return staleOutputResult(session);
      }
      void stopSession(session);
      throw new Error(`bridge is not open: ${bridgeId}`);
    }
    if (
      session.stopping ||
      session.closed ||
      session.output !== output ||
      (requestedGeneration !== undefined && requestedGeneration !== session.outputGeneration)
    ) {
      return staleOutputResult(session);
    }
    session.lastOutputAt = new Date().toISOString();
    session.lastOutputBytes += audio.byteLength;
    return { bridgeId, ok: true };
  };

  const clearAudio = (params: Record<string, unknown>) => {
    const bridgeId = readNonEmptyString(params.bridgeId);
    if (!bridgeId) {
      throw new Error("bridgeId required");
    }
    const session = sessions.get(bridgeId);
    if (!session || session.stopping || session.closed) {
      throw new Error(`bridge is not open: ${bridgeId}`);
    }
    const requestedGeneration = readOutputGeneration(params.outputGeneration);
    if (requestedGeneration !== undefined && requestedGeneration <= session.outputGeneration) {
      return staleOutputResult(session);
    }
    if (requestedGeneration === undefined && session.outputGeneration >= Number.MAX_SAFE_INTEGER) {
      throw new Error("outputGeneration exhausted");
    }
    const nextGeneration = requestedGeneration ?? session.outputGeneration + 1;
    const previousOutput = session.output;
    const outputProcess = startOutputProcess(session.outputCommand);
    session.output = outputProcess;
    session.outputGeneration = nextGeneration;
    attachOutputProcessHandlers(session, outputProcess);
    releaseOutputWriteWaiters(session, previousOutput);
    session.clearCount += 1;
    session.lastClearAt = new Date().toISOString();
    retireOutputProcess(session, previousOutput);
    return { bridgeId, ok: true, clearCount: session.clearCount };
  };

  const startBrowser = async (params: Record<string, unknown>) => {
    const url = options.normalizeUrl(params.url);
    const timeoutMs = readPositiveNumberOr(params.joinTimeoutMs, 30_000);
    const mode = readNonEmptyString(params.mode);
    let audioRuntime: MeetingAudioRuntime | undefined;
    let bridgeId: string | undefined;
    let audioBridge:
      | { type: "external-command" }
      | { type: "node-command-pair"; outputGeneration: true }
      | undefined;
    if (mode && options.talkBackModes.has(mode)) {
      audioRuntime = await prepareMeetingNodeAudio(params, Math.min(timeoutMs, 10_000), options);
      const healthCommand = readMeetingNodeCommand(params.audioBridgeHealthCommand);
      if (healthCommand) {
        const health = runCommandWithTimeout(healthCommand, timeoutMs);
        if (health.code !== 0) {
          throw new Error(
            `Chrome audio bridge health check failed: ${health.stderr || health.stdout || health.code}`,
          );
        }
      }
      const bridgeCommand = readMeetingNodeCommand(params.audioBridgeCommand);
      if (bridgeCommand) {
        if (mode === options.agentMode) {
          throw new Error(
            "Chrome agent mode requires audioInputCommand and audioOutputCommand so OpenClaw can run STT and regular TTS directly.",
          );
        }
        const bridge = runCommandWithTimeout(bridgeCommand, timeoutMs);
        if (bridge.code !== 0) {
          throw new Error(
            `failed to start Chrome audio bridge: ${bridge.stderr || bridge.stdout || bridge.code}`,
          );
        }
        audioBridge = { type: "external-command" };
      } else {
        const session = startCommandPair({
          inputCommand: audioRuntime.inputCommand,
          outputCommand: audioRuntime.outputCommand,
          url,
          mode,
        });
        bridgeId = session.id;
        audioBridge = { type: "node-command-pair", outputGeneration: true };
      }
    }

    if (params.launch !== false) {
      const argv = ["open", "-a", options.browser.application, url];
      const browserProfile = readNonEmptyString(params.browserProfile);
      if (browserProfile) {
        argv.push(...options.browser.buildProfileArgs(browserProfile));
      }
      try {
        const result = runCommandWithTimeout(argv, timeoutMs);
        if (result.code !== 0) {
          throw new Error(
            `failed to launch Chrome for ${options.browserLabel}: ${result.stderr || result.stdout || result.code}`,
          );
        }
      } catch (error) {
        if (bridgeId) {
          const session = sessions.get(bridgeId);
          if (session) {
            void stopSession(session, { discardQueuedAudio: true });
          }
        }
        throw error;
      }
    }
    return {
      launched: params.launch !== false,
      audioBackend: audioRuntime?.backend,
      audioDeviceLabel: audioRuntime?.deviceLabel,
      bridgeId,
      audioBridge,
      browser:
        params.launch !== false
          ? {
              status: options.browser.openedStatus,
              browserUrl: url,
              notes: options.browser.openedNotes,
            }
          : undefined,
    };
  };

  const bridgeStatus = (params: Record<string, unknown>) => {
    const bridgeId = readNonEmptyString(params.bridgeId);
    const session = bridgeId ? sessions.get(bridgeId) : undefined;
    return {
      bridge: session
        ? {
            bridgeId,
            closed: session.stopping || session.closed,
            createdAt: session.createdAt,
            lastInputAt: session.lastInputAt,
            lastOutputAt: session.lastOutputAt,
            lastClearAt: session.lastClearAt,
            lastInputBytes: session.lastInputBytes,
            lastOutputBytes: session.lastOutputBytes,
            clearCount: session.clearCount,
            queuedInputChunks: session.chunks.length,
          }
        : bridgeId
          ? { bridgeId, closed: true }
          : undefined,
    };
  };

  const summarizeSession = (session: NodeBridgeSession) => ({
    bridgeId: session.id,
    url: session.url,
    mode: session.mode,
    closed: session.closed,
    createdAt: session.createdAt,
    lastInputAt: session.lastInputAt,
    lastOutputAt: session.lastOutputAt,
    lastInputBytes: session.lastInputBytes,
    lastOutputBytes: session.lastOutputBytes,
  });

  const listSessions = (params: Record<string, unknown>) => {
    const urlKey = options.normalizeMeetingKey(readNonEmptyString(params.url));
    const mode = readNonEmptyString(params.mode);
    const bridges = [...sessions.values()]
      .filter((session) => !session.stopping && !session.closed)
      .filter((session) => !urlKey || options.normalizeMeetingKey(session.url) === urlKey)
      .filter((session) => !mode || session.mode === mode)
      .map(summarizeSession);
    return { bridges };
  };

  const stopSessionsByUrl = async (params: Record<string, unknown>) => {
    const urlKey = options.normalizeMeetingKey(readNonEmptyString(params.url));
    if (!urlKey) {
      throw new Error("url required");
    }
    const mode = readNonEmptyString(params.mode);
    const exceptBridgeId = readNonEmptyString(params.exceptBridgeId);
    let stopped = 0;
    const stopping: Promise<void>[] = [];
    for (const [bridgeId, session] of sessions) {
      if (exceptBridgeId && bridgeId === exceptBridgeId) {
        continue;
      }
      if (options.normalizeMeetingKey(session.url) !== urlKey) {
        continue;
      }
      if (mode && session.mode !== mode) {
        continue;
      }
      const wasClosed = session.stopping || session.closed;
      stopping.push(stopSession(session, { discardQueuedAudio: true }));
      if (!wasClosed) {
        stopped += 1;
      }
    }
    await Promise.all(stopping);
    return { ok: true, stopped };
  };

  const stopBrowser = async (params: Record<string, unknown>) => {
    const bridgeId = readNonEmptyString(params.bridgeId);
    if (!bridgeId) {
      return { ok: true, stopped: false };
    }
    const session = sessions.get(bridgeId);
    if (!session) {
      return { ok: true, stopped: false };
    }
    const wasStopped = session.stopping || session.closed;
    await stopSession(session, { discardQueuedAudio: true });
    return { ok: true, stopped: !wasStopped };
  };

  return {
    async handleCommand(paramsJSON?: string | null): Promise<string> {
      let raw: unknown = {};
      if (paramsJSON) {
        try {
          raw = JSON.parse(paramsJSON) as unknown;
        } catch {
          throw new Error(`${options.displayName} node host received malformed params JSON.`);
        }
      }
      const params = asOptionalRecord(raw) ?? {};
      const action = readNonEmptyString(params.action);
      let result: unknown;
      switch (action) {
        case "setup":
          {
            const audioRuntime = await prepareMeetingNodeAudio(params, 10_000, options);
            result = {
              ok: true,
              audioBackend: audioRuntime.backend,
              audioDeviceLabel: audioRuntime.deviceLabel,
            };
          }
          break;
        case "start":
          result = await startBrowser(params);
          break;
        case "status":
          result = bridgeStatus(params);
          break;
        case "list":
          result = listSessions(params);
          break;
        case "stopByUrl":
          result = await stopSessionsByUrl(params);
          break;
        case "pullAudio":
          result = await pullAudio(params);
          break;
        case "pushAudio":
          result = await pushAudio(params);
          break;
        case "clearAudio":
          result = clearAudio(params);
          break;
        case "stop":
          result = await stopBrowser(params);
          break;
        default:
          throw new Error(`unsupported ${options.commandName} action`);
      }
      return JSON.stringify(result);
    },
  };
}