Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Say a voice note is recorded only when the harness names the saved audio (#4712)
Milind Soni8fe81b20d91b
diff --git a/server/drivers/agents-call.test.ts b/server/drivers/agents-call.test.tsindex 1ce93b3983..bb1e9ae774 100644--- a/server/drivers/agents-call.test.ts+++ b/server/drivers/agents-call.test.ts@@ -44,7 +44,7 @@ describe("send_voice_note", () => {client: {api: async (path, init) => {calls.push({ path, body: JSON.parse(String(init?.body)) });- return {};+ return { attached: true, file: "0f3c.mp3" };},apiResponse: async () => ({ ok: true, status: 200, body: {} }),},@@ -56,6 +56,17 @@ describe("send_voice_note", () => {]);});+ it("never says recorded when the harness answers without naming the saved audio", async () => {+ for (const answer of [{}, { ok: true }, { attached: true }, { attached: false, file: "0f3c.mp3" }, { attached: true, file: "" }]) {+ const result = await callTool("send_voice_note", { text: "Hi there, here is your voice note." }, context({+ client: { api: async () => answer, apiResponse: async () => ({ ok: true, status: 200, body: answer }) },+ }));+ expect(result.isError, JSON.stringify(answer)).toBe(true);+ expect(result.text).toMatch(/^Voice note not sent: /);+ expect(result.text).not.toContain("Voice note recorded");+ }+ });+it("surfaces missing voice setup as a tool error, never a thrown turn", async () => {const result = await callTool("send_voice_note", { text: "Hello" }, context({client: {diff --git a/server/drivers/agents-call.ts b/server/drivers/agents-call.tsindex 1f728c75ec..7db0c2c15c 100644--- a/server/drivers/agents-call.ts+++ b/server/drivers/agents-call.ts@@ -603,10 +603,13 @@ export async function callTool(called: string, input: Json, context: ToolCallConreturn { text: `send_voice_note is limited to 1000 characters; this one is ${text.length}. Shorten the note.`, isError: true };}try {- await api("/api/internal/voice-note", {+ const r = await api("/api/internal/voice-note", {method: "POST",body: JSON.stringify({ fromBotId: BOT_ID, fromThreadId: THREAD_ID, text }),});+ if (r.attached !== true || typeof r.file !== "string" || !r.file) {+ return { text: "Voice note not sent: OpenMausBot did not confirm the audio was saved. Tell the person the note did not go through; do not say it is attached.", isError: true };+ }return { text: "Voice note recorded. It will be attached to this turn's reply when the turn ends; the note text is also the visible caption." };} catch (error) {return { text: `Voice note not sent: ${error instanceof Error ? error.message : String(error)}`, isError: true };diff --git a/server/index.ts b/server/index.tsindex 8114148379..502730c74b 100644--- a/server/index.ts+++ b/server/index.ts@@ -14429,11 +14429,12 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {return json(res, 409, { error: "this turn has not started yet or already ended" });}const saved = saveAudio(Buffer.from(audio.bytes), audio.mime);+ if (!existsSync(saved.path)) throw new Error("the voice note's audio file was not saved");const key = turnAttachmentKey(internalCapability.threadId, turnId);const current = turnAttachmentsByTurn.get(key) ?? [];current.push({ kind: "audio", path: saved.path, mime: saved.mime, text });turnAttachmentsByTurn.set(key, current); console.info(`[voice-note] parked thread=${internalCapability.threadId} turn=${turnId} file=${basename(saved.path)}`);- return json(res, 201, { attached: true });+ return json(res, 201, { attached: true, file: basename(saved.path) });} catch (error) {const failure = voiceNoteFailure(error);if (failure) return json(res, failure.status, failure.body);diff --git a/server/voice-notes-chat.e2e.test.ts b/server/voice-notes-chat.e2e.test.tsindex 17c7122922..4e9c1b1f82 100644--- a/server/voice-notes-chat.e2e.test.ts+++ b/server/voice-notes-chat.e2e.test.ts@@ -130,6 +130,8 @@ async function withChatVoice(test: (f: {}}+/** The note as the bot wrote it in the turn that lost one. */+const NOTE = "Hi there, it is your assistant. Here is your voice note, sent fresh with this message, so you can hear it right away.";const audioOf = (messages: any[]) => messages.flatMap((message) => (message.attachments ?? []).filter((file: any) => file.kind === "audio").map((file: any) => ({ message, file })));/** The thread as the chat reads it: from its newest line back along each line's parent. */const inOrder = (messages: any[]) => {@@ -230,7 +232,7 @@ it("with a voice note on every reply, says by voice how a failed turn ended", asawait expect.poll(async () => audioOf(await f.messages(bot.activeTaskId)).map((note) => note.file.text).at(-1), { timeout: 30_000 }).toBe("All good now.");}), 90_000);-it("attaches the note to the final reply of a long turn: folded first, said in between, two failed calls", async () => withChatVoice(async (f) => {+it("records a note only with its audio on disk in a long turn: folded first, said in between, two failed calls", async () => withChatVoice(async (f) => {const bot = await f.newBot("Long turn voice bot");await f.api("PATCH", `/api/bots/${bot.id}`, { voiceNoteEveryReply: true });await f.api("PATCH", "/api/config", { context: { compactAt: 1 } });@@ -240,13 +242,15 @@ it("attaches the note to the final reply of a long turn: folded first, said in bframe({ content: text, tool_calls: [{ index: 0, id, type: "function", function: { name, arguments: JSON.stringify(args) } }] }, "tool_calls") + "data: [DONE]\n\n";f.script((request) => {const done = toolResults(request).length;- if (done === 0) return step("c1", "Voice note on its way…", "agents_send_voice_note", { text: "Here is the weekly summary." });+ if (done === 0) return step("c1", "Voice note on its way…", "agents_send_voice_note", { text: NOTE });if (done === 1) return step("c2", "Looking at the numbers.", "agents_send_voice_note", { text: "" });if (done === 2) return step("c3", "Trying another way.", "agents_list_bots", { unexpected: true });if (done === 3) return step("c4", "Almost there.", "agents_list_bots", {});return say("Voice note is attached to this reply.");});- await f.settle(bot, "Summarise the week and send it as a voice note.");+ await f.settle(bot, "hi send vouse message");+ const recorded = JSON.parse(toolResults(f.requests.at(-1)!)[0]!);+ expect(recorded).toEqual({ ok: true, result: expect.stringMatching(/^Voice note recorded\. /) });const messages = await f.messages(bot.activeTaskId);expect(messages.filter((message) => message.kind === "compaction").length).toBeGreaterThan(0);const turn = messages.find((message) => message.text === "Voice note is attached to this reply.")?.turnId;@@ -255,5 +259,7 @@ it("attaches the note to the final reply of a long turn: folded first, said in bexpect(lines.length).toBeGreaterThan(1);const failed = messages.filter((message) => message.kind === "activity" && message.turnId === turn && message.tool?.ok === false);expect(failed.length).toBeGreaterThanOrEqual(1);- expect(audioOf(messages).filter((note) => note.message.turnId === turn).map((note) => [note.message.text, note.file.text])).toEqual([["Voice note is attached to this reply.", "Here is the weekly summary."]]);+ const notes = audioOf(messages).filter((note) => note.message.turnId === turn);+ expect(notes.map((note) => [note.message.text, note.file.text])).toEqual([["Voice note is attached to this reply.", NOTE]]);+ expect(f.audioFiles()).toContain(notes[0]!.file.path.split(/[\\/]/).pop());}), 120_000);