Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(meetings): retry a whisper install killed while leaving
Sebastien Tardiff5ab1083a811
diff --git a/server/meetings/meetbot.test.ts b/server/meetings/meetbot.test.tsindex 8165e3d0ec..1585772d33 100644--- a/server/meetings/meetbot.test.ts+++ b/server/meetings/meetbot.test.ts@@ -1,4 +1,4 @@-import { mkdtempSync, rmSync } from "node:fs";+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { describe, expect, it, vi } from "vitest";@@ -156,19 +156,46 @@ describe("the meeting bot on Google Meet", () => {});describe("local transcription install", () => {+ function scriptedSpawn(plan: Array<number | "hang">) {+ const calls: Array<{ file: string; args: string[]; detached?: boolean }> = [];+ const killed: string[] = [];+ const groups: number[] = [];+ let n = 0;+ const spawn = ((file: string, args: readonly string[] = [], options?: { detached?: boolean }) => {+ const behavior = plan[n++] ?? "hang";+ calls.push({ file, args: [...args], detached: options?.detached });+ const listeners: Array<(code: number | null) => void> = [];+ const child = {+ pid: 424242,+ kill(signal?: string) {+ killed.push(signal ?? "SIGTERM");+ for (const listener of listeners) listener(null);+ },+ on(event: string, cb: (code: number | null) => void) {+ if (event === "exit") {+ listeners.push(cb);+ if (typeof behavior === "number") queueMicrotask(() => cb(behavior));+ }+ return child;+ },+ stdout: null,+ stdin: null,+ };+ return child;+ }) as unknown as typeof import("node:child_process").spawn;+ return { spawn, calls, killed, groups };+ }+it("leaves while the first install is still running", async () => {const home = mkdtempSync(join(tmpdir(), "omb-whisper-"));- const killed: string[] = [];const events: Array<{ type: string; data?: Record<string, unknown> }> = [];+ const scripted = scriptedSpawn(["hang"]);const stream = whisperStream((type, data) => { events.push({ type, data }); }, () => {}, () => {}, {home,stopWaitMs: 50,- spawn: (() => ({- kill(signal?: string) { killed.push(signal ?? "SIGTERM"); },- on() { return this; },- stdout: null,- stdin: null,- })) as unknown as typeof import("node:child_process").spawn,+ groupProcesses: true,+ killGroup(pid: number) { scripted.groups.push(pid); },+ spawn: scripted.spawn,});const result = await Promise.race([stream.stop().then(() => "stopped"),@@ -176,8 +203,31 @@ describe("local transcription install", () => {]);rmSync(home, { recursive: true, force: true });expect(result).toBe("stopped");- expect(killed).toContain("SIGKILL");+ expect(scripted.killed).toContain("SIGKILL");+ expect(scripted.groups).toEqual([424242]);+ expect(scripted.calls[0]?.detached).toBe(true);+ expect(events.some((event) => event.type === "stt" && event.data?.status === "installing")).toBe(true);+ expect(events.some((event) => event.type === "stt" && event.data?.status === "failed")).toBe(false);+ });++ it("installs again when the virtual environment cannot import", async () => {+ const home = mkdtempSync(join(tmpdir(), "omb-whisper-"));+ mkdirSync(join(home, "venv", "bin"), { recursive: true });+ writeFileSync(join(home, "venv", "bin", "python"), "");+ const events: Array<{ type: string; data?: Record<string, unknown> }> = [];+ const scripted = scriptedSpawn([1, "hang"]);+ const stream = whisperStream((type, data) => { events.push({ type, data }); }, () => {}, () => {}, {+ home,+ stopWaitMs: 50,+ spawn: scripted.spawn,+ });+ for (let i = 0; i < 50 && scripted.calls.length < 2; i++) await new Promise((resolve) => setTimeout(resolve, 10));+ await stream.stop();+ rmSync(home, { recursive: true, force: true });+ expect(scripted.calls[0]?.args).toEqual(["-c", "import faster_whisper, numpy"]);+ expect(scripted.calls[1]?.file).toBe("bash");expect(events.some((event) => event.type === "stt" && event.data?.status === "installing")).toBe(true);+ expect(events.some((event) => event.type === "stt" && event.data?.status === "failed")).toBe(false);});});diff --git a/server/meetings/meetbot.ts b/server/meetings/meetbot.tsindex a97ebf3dd6..92fc22b37a 100644--- a/server/meetings/meetbot.ts+++ b/server/meetings/meetbot.ts@@ -45,10 +45,11 @@ const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve,/** A pip install on a dead network, or ffmpeg on a stuck pipe, never exits. Leave must not wait for that. */const WHISPER_INSTALL_MS = 10 * 60_000;+const WHISPER_PROBE_MS = 15_000;const STOP_INSTALL_WAIT_MS = 1_000;const FFMPEG_ENCODE_MS = 20_000;-function boundChild(child: ChildProcess, timeoutMs: number): Promise<void> {+function boundChild(child: ChildProcess, timeoutMs: number, end: () => void): Promise<void> {return new Promise((resolve, reject) => {let settled = false;const finish = (error?: Error) => {@@ -59,7 +60,7 @@ function boundChild(child: ChildProcess, timeoutMs: number): Promise<void> {else resolve();};const timer = setTimeout(() => {- child.kill("SIGKILL");+ end();finish(new Error(`timed out after ${timeoutMs} ms`));}, timeoutMs);child.on("error", (error) => finish(error));@@ -814,25 +815,59 @@ for chunk in iter(lambda: sys.stdin.buffer.read(FRAME * 4), b""):/** Speech-to-text on the computer itself, for a person with no speech key: faster-whisper, installed on first use. */export function whisperStream(emit: SessionParts["emit"], onWords: (words: Word[]) => void, log: (line: string) => void,- options: { home?: string; spawn?: typeof spawn; installMs?: number; stopWaitMs?: number } = {}) {+ options: { home?: string; spawn?: typeof spawn; installMs?: number; stopWaitMs?: number; groupProcesses?: boolean; killGroup?: (pid: number) => void } = {}) {const home = options.home ?? join(process.env.HOME ?? "/tmp", ".mausbot-meet");const run = options.spawn ?? spawn;const python = join(home, "venv", "bin", "python");+ // `python3 -m venv` stays a child of Bash. A Linux process group lets leave kill both.+ const groupProcesses = options.groupProcesses ?? process.platform !== "win32";+ const killGroup = options.killGroup ?? ((pid: number) => { process.kill(-pid, "SIGKILL"); });+ const endChild = (child: ChildProcess, group: boolean) => {+ if (group && typeof child.pid === "number" && child.pid > 0) {+ try { killGroup(child.pid); } catch { /* already gone */ }+ }+ try { child.kill("SIGKILL"); } catch { /* already gone */ }+ };mkdirSync(home, { recursive: true });writeFileSync(join(home, "whisper_worker.py"), WHISPER_WORKER);let worker: ChildProcess | null = null;let install: ChildProcess | null = null;+ let endInstall: (() => void) | null = null;let stopped = false;let startedAt = 0;const queue: Buffer[] = [];+ const watchInstall = async (child: ChildProcess, timeoutMs: number, group: boolean) => {+ const end = () => endChild(child, group);+ install = child;+ endInstall = end;+ try {+ await boundChild(child, timeoutMs, end);+ } finally {+ if (install === child) {+ install = null;+ endInstall = null;+ }+ }+ };const ready = (async () => {- if (!existsSync(python)) {+ let depsReady = false;+ if (existsSync(python)) {+ try {+ await watchInstall(run(python, ["-c", "import faster_whisper, numpy"], { stdio: "ignore" }), WHISPER_PROBE_MS, false);+ depsReady = !stopped;+ } catch {+ depsReady = false;+ }+ }+ if (stopped) return;+ if (!depsReady) {emit("stt", { status: "installing" });- install = run("bash", ["-c", `python3 -m venv ${JSON.stringify(join(home, "venv"))} && ${JSON.stringify(join(home, "venv", "bin", "pip"))} install -q faster-whisper numpy`], { stdio: "ignore" });+ const script = `python3 -m venv ${JSON.stringify(join(home, "venv"))} && ${JSON.stringify(join(home, "venv", "bin", "pip"))} install -q faster-whisper numpy`;try {- await boundChild(install, options.installMs ?? WHISPER_INSTALL_MS);- } finally {- install = null;+ await watchInstall(run("bash", ["-c", script], { stdio: "ignore", detached: groupProcesses }), options.installMs ?? WHISPER_INSTALL_MS, groupProcesses);+ } catch (error) {+ if (stopped) return;+ throw error;}}if (stopped) return;@@ -852,7 +887,10 @@ export function whisperStream(emit: SessionParts["emit"], onWords: (words: Word[});emit("stt", { status: "connected" });for (const chunk of queue.splice(0)) worker.stdin!.write(chunk);- })().catch((error: Error) => emit("stt", { status: "failed", detail: error.message }));+ })().catch((error: Error) => {+ if (stopped) return;+ emit("stt", { status: "failed", detail: error.message });+ });return {write(chunk: Buffer) {if (!startedAt) startedAt = Date.now() - chunk.length / 32;@@ -861,7 +899,7 @@ export function whisperStream(emit: SessionParts["emit"], onWords: (words: Word[},async stop() {stopped = true;- install?.kill("SIGKILL");+ endInstall?.();await new Promise<void>((resolve) => {let settled = false;const finish = () => { if (settled) return; settled = true; clearTimeout(timer); resolve(); };