|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { asRecord } from "@openclaw/normalization-core/record-coerce"; |
| 3 | +import { createMeetingNodeHost, type MeetingNodeHostOptions } from "./node-host.js"; |
| 4 | + |
| 5 | +type MeetingConfiguredNodeHostOptions = Omit<MeetingNodeHostOptions, "assertAudioAvailable"> & { |
| 6 | + meetingLabel: string; |
| 7 | + outputMentionsAudioDevice(output: string): boolean; |
| 8 | + sharePrerequisiteDeadline: boolean; |
| 9 | + systemProfilerCommand: string; |
| 10 | +}; |
| 11 | + |
| 12 | +function readSetupCommand(params: Record<string, unknown>, name: string): string[] { |
| 13 | + const value = params[name]; |
| 14 | + if ( |
| 15 | + !Array.isArray(value) || |
| 16 | + value.length === 0 || |
| 17 | + value.some((entry) => typeof entry !== "string") |
| 18 | + ) { |
| 19 | + throw new Error(`${name} must be a non-empty string array.`); |
| 20 | + } |
| 21 | + return value as string[]; |
| 22 | +} |
| 23 | + |
| 24 | +function isSpawnSyncTimeout(error: unknown): boolean { |
| 25 | + return error instanceof Error && "code" in error && error.code === "ETIMEDOUT"; |
| 26 | +} |
| 27 | + |
| 28 | +export function createMeetingConfiguredNodeHost(options: MeetingConfiguredNodeHostOptions) { |
| 29 | + const probeCommand = (command: string, timeoutMs: number): "found" | "missing" | "timed-out" => { |
| 30 | + const result = spawnSync("/bin/sh", ["-lc", 'command -v "$1" >/dev/null 2>&1', "sh", command], { |
| 31 | + encoding: "utf8", |
| 32 | + timeout: timeoutMs, |
| 33 | + }); |
| 34 | + if (isSpawnSyncTimeout(result.error)) { |
| 35 | + return "timed-out"; |
| 36 | + } |
| 37 | + return result.status === 0 ? "found" : "missing"; |
| 38 | + }; |
| 39 | + const assertAudioAvailable = ( |
| 40 | + timeoutMs: number, |
| 41 | + commands: readonly (readonly string[])[] = [ |
| 42 | + options.defaultAudioInputCommand, |
| 43 | + options.defaultAudioOutputCommand, |
| 44 | + ], |
| 45 | + ) => { |
| 46 | + if (process.platform !== "darwin") { |
| 47 | + throw new Error(`${options.meetingLabel} talk-back with BlackHole 2ch is macOS-only`); |
| 48 | + } |
| 49 | + const deadline = Date.now() + timeoutMs; |
| 50 | + const commandTimeout = () => |
| 51 | + options.sharePrerequisiteDeadline ? Math.max(1, deadline - Date.now()) : timeoutMs; |
| 52 | + const result = spawnSync(options.systemProfilerCommand, ["SPAudioDataType"], { |
| 53 | + encoding: "utf8", |
| 54 | + timeout: commandTimeout(), |
| 55 | + }); |
| 56 | + if (isSpawnSyncTimeout(result.error)) { |
| 57 | + throw new Error(`${options.meetingLabel} audio prerequisite check timed out on the node.`); |
| 58 | + } |
| 59 | + const stderr = |
| 60 | + result.stderr ?? |
| 61 | + (result.error |
| 62 | + ? result.error instanceof Error |
| 63 | + ? result.error.message |
| 64 | + : String(result.error) |
| 65 | + : ""); |
| 66 | + const output = `${result.stdout ?? ""}\n${stderr}`; |
| 67 | + if ( |
| 68 | + (typeof result.status === "number" ? result.status : result.error ? 1 : 0) !== 0 || |
| 69 | + !options.outputMentionsAudioDevice(output) |
| 70 | + ) { |
| 71 | + throw new Error("BlackHole 2ch audio device not found on the node."); |
| 72 | + } |
| 73 | + const commandNames = new Set<string>(); |
| 74 | + for (const argv of commands) { |
| 75 | + const command = argv[0]; |
| 76 | + if (!command) { |
| 77 | + throw new Error(`Configured audio command not found on the node: ${command || "<empty>"}`); |
| 78 | + } |
| 79 | + commandNames.add(command); |
| 80 | + } |
| 81 | + for (const command of commandNames) { |
| 82 | + if (options.sharePrerequisiteDeadline && Date.now() >= deadline) { |
| 83 | + throw new Error(`${options.meetingLabel} audio prerequisite check timed out on the node.`); |
| 84 | + } |
| 85 | + const probeResult = probeCommand(command, commandTimeout()); |
| 86 | + if (probeResult === "timed-out") { |
| 87 | + throw new Error(`${options.meetingLabel} audio prerequisite check timed out on the node.`); |
| 88 | + } |
| 89 | + if (probeResult === "missing") { |
| 90 | + throw new Error(`Configured audio command not found on the node: ${command}`); |
| 91 | + } |
| 92 | + } |
| 93 | + }; |
| 94 | + const host = createMeetingNodeHost({ ...options, assertAudioAvailable }); |
| 95 | + |
| 96 | + return async (paramsJSON?: string | null): Promise<string> => { |
| 97 | + if (paramsJSON) { |
| 98 | + let raw: unknown; |
| 99 | + try { |
| 100 | + raw = JSON.parse(paramsJSON) as unknown; |
| 101 | + } catch { |
| 102 | + throw new Error(`${options.displayName} node host received malformed params JSON.`); |
| 103 | + } |
| 104 | + const params = asRecord(raw); |
| 105 | + if (params.action === "setup") { |
| 106 | + const commands = [ |
| 107 | + readSetupCommand(params, "audioInputCommand"), |
| 108 | + readSetupCommand(params, "audioOutputCommand"), |
| 109 | + ]; |
| 110 | + if (params.bargeInInputCommand !== undefined) { |
| 111 | + commands.push(readSetupCommand(params, "bargeInInputCommand")); |
| 112 | + } |
| 113 | + assertAudioAvailable(10_000, commands); |
| 114 | + return JSON.stringify({ ok: true }); |
| 115 | + } |
| 116 | + } |
| 117 | + return await host.handleCommand(paramsJSON); |
| 118 | + }; |
| 119 | +} |
0 commit comments