Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
fix(voice): make local calls realtime (#12)
Leoe2ffced5aae3
diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.tsindex 4bf3808db..3db74f6f4 100644--- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts+++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts@@ -231,7 +231,7 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {VALUES ('thread-1','turn-1',- NULL,+ 'message-request-1','thread-1','plan-1','message-1',@@ -322,6 +322,7 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {startedAt: "2026-02-24T00:00:08.000Z",completedAt: "2026-02-24T00:00:08.000Z",assistantMessageId: asMessageId("message-1"),+ requestMessageId: asMessageId("message-request-1"),respondingBotId: null,sourceProposedPlan: {threadId: ThreadId.make("thread-1"),@@ -453,6 +454,7 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {startedAt: "2026-02-24T00:00:08.000Z",completedAt: "2026-02-24T00:00:08.000Z",assistantMessageId: asMessageId("message-1"),+ requestMessageId: asMessageId("message-request-1"),respondingBotId: null,sourceProposedPlan: {threadId: ThreadId.make("thread-1"),@@ -1305,6 +1307,7 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {if (threadShell._tag === "Some") {assert.equal(threadShell.value.latestTurn?.turnId, asTurnId("turn-running"));assert.equal(threadShell.value.latestTurn?.state, "running");+ assert.equal(threadShell.value.latestTurn?.requestMessageId, asMessageId("message-user-2"));assert.equal(threadShell.value.latestTurn?.startedAt, "2026-04-02T00:00:30.000Z");}@@ -1313,6 +1316,10 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {if (threadDetail._tag === "Some") {assert.equal(threadDetail.value.latestTurn?.turnId, asTurnId("turn-running"));assert.equal(threadDetail.value.latestTurn?.state, "running");+ assert.equal(+ threadDetail.value.latestTurn?.requestMessageId,+ asMessageId("message-user-2"),+ );assert.equal(threadDetail.value.latestTurn?.startedAt, "2026-04-02T00:00:30.000Z");}}),@@ -1460,14 +1467,26 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {const commandReadModel = yield* snapshotQuery.getCommandReadModel();assert.equal(commandReadModel.threads[0]?.latestTurn?.turnId, asTurnId("turn-running"));assert.equal(commandReadModel.threads[0]?.latestTurn?.state, "running");+ assert.equal(+ commandReadModel.threads[0]?.latestTurn?.requestMessageId,+ asMessageId("message-user-2"),+ );const shellSnapshot = yield* snapshotQuery.getShellSnapshot();assert.equal(shellSnapshot.threads[0]?.latestTurn?.turnId, asTurnId("turn-running"));assert.equal(shellSnapshot.threads[0]?.latestTurn?.state, "running");+ assert.equal(+ shellSnapshot.threads[0]?.latestTurn?.requestMessageId,+ asMessageId("message-user-2"),+ );const fullSnapshot = yield* snapshotQuery.getSnapshot();assert.equal(fullSnapshot.threads[0]?.latestTurn?.turnId, asTurnId("turn-running"));assert.equal(fullSnapshot.threads[0]?.latestTurn?.state, "running");+ assert.equal(+ fullSnapshot.threads[0]?.latestTurn?.requestMessageId,+ asMessageId("message-user-2"),+ );}),);diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.tsindex 6d8a086a0..f2a40cd85 100644--- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts+++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts@@ -137,6 +137,7 @@ const ProjectionLatestTurnDbRowSchema = Schema.Struct({startedAt: Schema.NullOr(IsoDateTime),completedAt: Schema.NullOr(IsoDateTime),assistantMessageId: Schema.NullOr(MessageId),+ pendingMessageId: Schema.NullOr(MessageId),respondingBotId: Schema.optional(Schema.NullOr(BotId)),sourceProposedPlanThreadId: Schema.NullOr(ThreadId),sourceProposedPlanId: Schema.NullOr(OrchestrationProposedPlanId),@@ -305,6 +306,7 @@ function mapLatestTurn(startedAt: row.startedAt,completedAt: row.completedAt,assistantMessageId: row.assistantMessageId,+ requestMessageId: row.pendingMessageId,respondingBotId: row.respondingBotId ?? null,...(row.sourceProposedPlanThreadId !== null && row.sourceProposedPlanId !== null? {@@ -796,6 +798,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {turns.started_at AS "startedAt",turns.completed_at AS "completedAt",turns.assistant_message_id AS "assistantMessageId",+ turns.pending_message_id AS "pendingMessageId",turns.responding_bot_id AS "respondingBotId",turns.source_proposed_plan_thread_id AS "sourceProposedPlanThreadId",turns.source_proposed_plan_id AS "sourceProposedPlanId"@@ -821,6 +824,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {turns.started_at AS "startedAt",turns.completed_at AS "completedAt",turns.assistant_message_id AS "assistantMessageId",+ turns.pending_message_id AS "pendingMessageId",turns.responding_bot_id AS "respondingBotId",turns.source_proposed_plan_thread_id AS "sourceProposedPlanThreadId",turns.source_proposed_plan_id AS "sourceProposedPlanId"@@ -848,6 +852,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {turns.started_at AS "startedAt",turns.completed_at AS "completedAt",turns.assistant_message_id AS "assistantMessageId",+ turns.pending_message_id AS "pendingMessageId",turns.responding_bot_id AS "respondingBotId",turns.source_proposed_plan_thread_id AS "sourceProposedPlanThreadId",turns.source_proposed_plan_id AS "sourceProposedPlanId"@@ -1196,6 +1201,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {turns.started_at AS "startedAt",turns.completed_at AS "completedAt",turns.assistant_message_id AS "assistantMessageId",+ turns.pending_message_id AS "pendingMessageId",turns.responding_bot_id AS "respondingBotId",turns.source_proposed_plan_thread_id AS "sourceProposedPlanThreadId",turns.source_proposed_plan_id AS "sourceProposedPlanId"@@ -1775,30 +1781,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {if (latestTurnByThread.has(row.threadId)) {continue;}- latestTurnByThread.set(row.threadId, {- turnId: row.turnId,- state:- row.state === "error"- ? "error"- : row.state === "interrupted"- ? "interrupted"- : row.state === "completed"- ? "completed"- : "running",- requestedAt: row.requestedAt,- startedAt: row.startedAt,- completedAt: row.completedAt,- assistantMessageId: row.assistantMessageId,- respondingBotId: row.respondingBotId ?? null,- ...(row.sourceProposedPlanThreadId !== null && row.sourceProposedPlanId !== null- ? {- sourceProposedPlan: {- threadId: row.sourceProposedPlanThreadId,- planId: row.sourceProposedPlanId,- },- }- : {}),- });+ latestTurnByThread.set(row.threadId, mapLatestTurn(row));}for (const row of sessionRows) {diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.tsindex a90f70573..97acdf5d5 100644--- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts+++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts@@ -720,6 +720,7 @@ describe("ProviderCommandReactor", () => {kind: "provider.turn.start.failed",payload: {detail: "Provider instance 'missing' is not available.",+ requestId: "user-message-missing-bot-engine",},}),);@@ -811,6 +812,14 @@ describe("ProviderCommandReactor", () => {let readModel = yield* Effect.promise(() => harness.readModel());let thread = readModel.threads.find((entry) => entry.id === ThreadId.make("thread-1"));expect(thread?.session?.lastError).toContain("deterministic startup failure");+ expect(+ thread?.activities.find((activity) => activity.kind === "provider.turn.start.failed"),+ ).toMatchObject({+ payload: {+ detail: "deterministic startup failure",+ requestId: "user-message-provider-failure",+ },+ });expect(harness.sendTurn).not.toHaveBeenCalled();failStartup = false;@@ -2029,6 +2038,7 @@ describe("ProviderCommandReactor", () => {detail: expect.stringContaining("cannot switch models after the conversation has started",),+ requestId: "user-message-restricted-2",},});}),@@ -3029,6 +3039,7 @@ describe("ProviderCommandReactor", () => {).toMatchObject({payload: {detail: expect.stringContaining("without a provider instance id"),+ requestId: "user-message-missing-instance",},});});diff --git a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts b/apps/server/src/orchestration/Layers/ProviderCommandReactor.tsindex 4a71e3178..905b76d1e 100644--- a/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts+++ b/apps/server/src/orchestration/Layers/ProviderCommandReactor.ts@@ -1175,6 +1175,7 @@ const make = Effect.gen(function* () {detail: `User message '${event.payload.messageId}' was not found for turn start request.`,turnId: null,createdAt: event.payload.createdAt,+ requestId: event.payload.messageId,});return;}@@ -1230,6 +1231,7 @@ const make = Effect.gen(function* () {detail,turnId: null,createdAt: event.payload.createdAt,+ requestId: event.payload.messageId,}),),Effect.asVoid,diff --git a/apps/server/src/voiceCall/VoiceCallManager.test.ts b/apps/server/src/voiceCall/VoiceCallManager.test.tsindex 3bc475e2c..c567f4189 100644--- a/apps/server/src/voiceCall/VoiceCallManager.test.ts+++ b/apps/server/src/voiceCall/VoiceCallManager.test.ts@@ -23,7 +23,7 @@ it("accepts a Codex CLI auth file without auth_mode", () => {assert.isUndefined(parseCodexCliAuth("not json"));});-it("creates a pinned realtime session through the ChatGPT subscription endpoint", async () => {+it("creates a current realtime session through the ChatGPT subscription endpoint", async () => {const fetchMock = vi.fn(async (url: string | URL | Request, init?: RequestInit) => {assert.equal(String(url), "https://chatgpt.com/backend-api/codex/realtime/calls");assert.isNull(new Headers(init?.headers).get("OpenAI-Alpha"));@@ -31,16 +31,32 @@ it("creates a pinned realtime session through the ChatGPT subscription endpoint"readonly session: {readonly type: string;readonly model: string;+ readonly instructions: string;+ readonly output_modalities: ReadonlyArray<string>;readonly audio: {- readonly input: { readonly turn_detection: { readonly type: string } };+ readonly input: {+ readonly turn_detection: {+ readonly type: string;+ readonly interrupt_response: boolean;+ };+ };readonly output: { readonly voice: string };};+ readonly tool_choice: string;+ readonly tools: ReadonlyArray<{ readonly name: string }>;};};assert.equal(body.session.type, "realtime");- assert.equal(body.session.model, "gpt-realtime-2");+ assert.equal(body.session.model, "gpt-realtime-2.1");assert.equal(body.session.audio.output.voice, "marin");assert.equal(body.session.audio.input.turn_detection.type, "semantic_vad");+ assert.isFalse(body.session.audio.input.turn_detection.interrupt_response);+ assert.equal(body.session.tool_choice, "auto");+ assert.deepEqual(+ body.session.tools.map((tool) => tool.name),+ ["send_to_chat"],+ );+ assert.equal(body.session.instructions, "Be concise.");return new Response("answer-sdp");});vi.stubGlobal("fetch", fetchMock);@@ -159,6 +175,43 @@ it.layer(VoiceDisabledTestLayer)("VoiceCallManager global settings", (it) => {});it.layer(TestLayer)("VoiceCallManager", (it) => {+ it.effect("releases the one-call lock when session construction fails", () =>+ Effect.gen(function* () {+ let constructionAttempts = 0;+ const failingLayer = layer({+ getCredential: async () => ({ accessToken: "access", accountId: "account" }),+ makeSession: () => {+ constructionAttempts += 1;+ if (constructionAttempts === 1) throw new Error("session constructor failed");+ return { negotiate: async () => "answer-sdp" };+ },+ }).pipe(+ Layer.provide(Layer.succeed(ProjectionBotRepository, repository)),+ Layer.provide(ServerSettingsService.layerTest()),+ Layer.provide(+ ServerConfig.layerTest(process.cwd(), {+ prefix: "akeru-voice-call-constructor-test-",+ }).pipe(Layer.provide(NodeServices.layer)),+ ),+ );+ const result = yield* Effect.provide(+ Effect.gen(function* () {+ const manager = yield* VoiceCallManager;+ const failed = yield* Effect.result(+ manager.start({ botId, sdp: "offer-sdp" }, "client-1"),+ );+ assert.equal(failed._tag, "Failure");+ assert.deepEqual(yield* manager.get, { status: "idle" });+ const retry = yield* manager.start({ botId, sdp: "offer-sdp" }, "client-1");+ assert.equal(retry.answerSdp, "answer-sdp");+ yield* manager.hangup(retry.call.callId, "client-1");+ }),+ failingLayer,+ );+ return result;+ }),+ );+it.effect("refuses hangup from a different connection owner", () =>Effect.gen(function* () {const manager = yield* VoiceCallManager;@@ -179,6 +232,30 @@ it.layer(TestLayer)("VoiceCallManager", (it) => {}),);+ it.effect("allows only one concurrent start to claim the call lock", () =>+ Effect.gen(function* () {+ const manager = yield* VoiceCallManager;+ const results = yield* Effect.all(+ [+ manager.start({ botId, sdp: "first-offer" }, "client-1").pipe(Effect.result),+ manager.start({ botId, sdp: "second-offer" }, "client-2").pipe(Effect.result),+ ],+ { concurrency: "unbounded" },+ );+ const successes = results.filter((result) => result._tag === "Success");+ const failures = results.filter((result) => result._tag === "Failure");+ assert.lengthOf(successes, 1);+ assert.lengthOf(failures, 1);+ assert.equal(failures[0]?.failure.reason, "already-active");+ const active = yield* manager.get;+ assert.notEqual(active.status, "idle");+ if (active.status !== "idle") {+ const ownerId = results[0]?._tag === "Success" ? "client-1" : "client-2";+ yield* manager.hangup(active.callId, ownerId);+ }+ }),+ );+it.effect("refuses a second call until hangup and clears the active call", () =>Effect.gen(function* () {const manager = yield* VoiceCallManager;diff --git a/apps/server/src/voiceCall/VoiceCallManager.ts b/apps/server/src/voiceCall/VoiceCallManager.tsindex 06bef44bf..765a6c5b7 100644--- a/apps/server/src/voiceCall/VoiceCallManager.ts+++ b/apps/server/src/voiceCall/VoiceCallManager.ts@@ -115,7 +115,7 @@ export function defaultSession(): ChatGptRealtimeSession {input: {format: { type: "audio/pcm", rate: 24_000 },transcription: { model: "gpt-4o-mini-transcribe" },- turn_detection: { type: "semantic_vad" },+ turn_detection: { type: "semantic_vad", interrupt_response: false },},output: { voice },},@@ -124,13 +124,13 @@ export function defaultSession(): ChatGptRealtimeSession {type: "function",name: "send_to_chat",description:- "Delegate a task to the coding-agent team in the chat thread. The team can read files, run commands, write code, and use tools on the user's machine. Call this for any request that needs execution, then confirm the handoff by voice. Pass the user's request as one clear message.",+ "Delegate work to the bot's existing chat thread. Use this for requests that need files, tools, code, the workspace, permissions, or stored memory. Pass the user's request as one clear message.",parameters: {type: "object",properties: {message: {type: "string",- description: "The user's request, cleaned up into one clear chat message.",+ description: "The user's request as one clear chat message.",},},required: ["message"],@@ -179,12 +179,11 @@ function instructionsForBot(bot: {return [`You are ${bot.name}, the user's ${bot.title}.`,bot.description,- "Talk like a sharp teammate. Answer first. Use short, plain sentences.",- "You are a bot on the user's team. Do not claim to be a person.",- "You lead a team of coding agents that work in the chat thread. They can read files, run commands, write code, and use tools on the user's machine. You delegate; they execute.",- "For any task that needs files, tools, code, or the user's computer, call send_to_chat with the request as one clear message. Then tell the user you have handed it to the team. Never say the task is impossible for you.",- "Only answer directly for conversation, questions you can answer from context, and retelling what the team has already done.",- "Context items labeled 'Team update' describe work the chat team finished during this call. Use them when the user asks about progress.",+ "Speak naturally. Answer first. Keep spoken replies concise.",+ "You are a bot on the user's team. Do not claim to be a person or always available.",+ `If the user asks you to stay silent while they talk to someone else, do not answer overheard speech until they address ${bot.name} or ask for a reply.`,+ "For any request that needs files, tools, code, the workspace, permissions, or stored memory, call send_to_chat with one clear request. Then tell the user that the work continues in the chat.",+ "Answer ordinary conversation directly in this live voice session.",].filter((part): part is string => typeof part === "string" && part.trim().length > 0).join("\n\n");@@ -312,12 +311,19 @@ const make = (options?: VoiceCallManagerOptions) =>});}- const session = (() => {- switch (voiceSettings.provider) {- case "chatgpt":- return (options?.makeSession ?? defaultSession)();- }- })();+ const session = yield* Effect.try({+ try: () => {+ switch (voiceSettings.provider) {+ case "chatgpt":+ return (options?.makeSession ?? defaultSession)();+ }+ },+ catch: (cause) =>+ new VoiceCallError({+ reason: "upstream-failed",+ message: cause instanceof Error ? cause.message : "Could not create the voice session.",+ }),+ }).pipe(Effect.tapError(() => clear(claimed.call.callId)));const answerSdp = yield* Effect.tryPromise({try: () =>session.negotiate({diff --git a/apps/web/src/components/roster/BotThreadLanding.markdown.test.ts b/apps/web/src/components/roster/BotThreadLanding.markdown.test.tsindex ef5cc0dd6..e734616a3 100644--- a/apps/web/src/components/roster/BotThreadLanding.markdown.test.ts+++ b/apps/web/src/components/roster/BotThreadLanding.markdown.test.ts@@ -28,4 +28,16 @@ describe("BotThreadLanding message formatting", () => {expect(botSource).not.toContain("justify-end gap-4");expect(groupSource).not.toContain("justify-end gap-4");});++ it("mounts the voice action in the live bot chat header", () => {+ const source = NodeFS.readFileSync(new URL("./BotThreadLanding.tsx", import.meta.url), "utf8");++ expect(source).toContain(+ 'import { BotVoiceCallButton, useVoiceCall } from "../voice/VoiceCall"',+ );+ expect(source).toContain("data-chat-header-actions");+ expect(source).toContain('runtime.latestTurn?.state === "running"');+ expect(source).toContain("voiceCall.activeCall?.botId === bot.id");+ expect(source).toContain("voiceCall.startingBotId === bot.id");+ });});diff --git a/apps/web/src/components/roster/BotThreadLanding.tsx b/apps/web/src/components/roster/BotThreadLanding.tsxindex 01843a489..236c6aed0 100644--- a/apps/web/src/components/roster/BotThreadLanding.tsx+++ b/apps/web/src/components/roster/BotThreadLanding.tsx@@ -27,7 +27,7 @@ import { BotConversationScrollArea } from "./BotConversationScrollArea";import { visibleBotChatMessages } from "./botConversationPresentation";import { resolveStickyBotEngine } from "./botEngineSelection";import { BotPromptComposer } from "./BotPromptComposer";-import { BotVoiceCallButton } from "../voice/VoiceCall";+import { BotVoiceCallButton, useVoiceCall } from "../voice/VoiceCall";import { useBotPresence } from "./botPresence";import { useRosterStore } from "./rosterStore";import { useBotThreadRuntime } from "./useBotThreadRuntime";@@ -75,6 +75,7 @@ export function BotThreadLanding({ botId }: { readonly botId: string }) {);const effectiveModelSelection = stickyEngine;const runtime = useBotThreadRuntime(botId, effectiveModelSelection);+ const voiceCall = useVoiceCall();const presence = useBotPresence(botId);useEffect(() => {@@ -113,7 +114,10 @@ export function BotThreadLanding({ botId }: { readonly botId: string }) {<span className="truncate text-sm font-medium">{bot.name}</span></div><div data-chat-header-actions className="ml-auto flex items-center">- <BotVoiceCallButton bot={bot} />+ <BotVoiceCallButton+ bot={bot}+ disabled={runtime.sending || runtime.latestTurn?.state === "running"}+ /></div></WorkspacePageHeader><BotConversationScrollArea>@@ -182,6 +186,8 @@ export function BotThreadLanding({ botId }: { readonly botId: string }) {draftKey={bot.id}disabled={runtime.sending ||+ voiceCall.activeCall?.botId === bot.id ||+ voiceCall.startingBotId === bot.id ||modelUpdatePending ||effectiveModelSelection === null ||!runtime.botReady ||diff --git a/apps/web/src/components/roster/botThreadRuntime.logic.test.ts b/apps/web/src/components/roster/botThreadRuntime.logic.test.tsindex 588a3636e..c9ad81e8a 100644--- a/apps/web/src/components/roster/botThreadRuntime.logic.test.ts+++ b/apps/web/src/components/roster/botThreadRuntime.logic.test.ts@@ -1,15 +1,153 @@-import { BotId, GroupId, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";+import {+ BotId,+ GroupId,+ MessageId,+ ProjectId,+ ProviderInstanceId,+ ThreadId,+} from "@t3tools/contracts";import { describe, expect, it } from "vite-plus/test";import {+ acceptBotTurnSubmission,buildBotTurnStartInput,buildGroupTurnStartInput,findLatestBotThreadTarget,findLatestGroupThreadTarget,joinOrStartThreadCreate,+ releaseBotTurnSubmissionAfterObservation,+ reserveBotTurnSubmission,+ reserveBotTurnSubmissionAfterObservation,} from "./botThreadRuntime.logic";describe("bot thread runtime", () => {+ it("shares the turn submission lock across runtime instances", () => {+ const release = reserveBotTurnSubmission("env-a:bot-akeru");+ expect(release).not.toBeNull();+ expect(reserveBotTurnSubmission("env-a:bot-akeru")).toBeNull();+ release?.();+ });++ it("retains a reserved submission until the start command responds", () => {+ const key = "env-a:bot-unaccepted";+ const release = reserveBotTurnSubmission(key);+ expect(release).not.toBeNull();+ expect(+ releaseBotTurnSubmissionAfterObservation(+ key,+ {+ requestMessageId: MessageId.make("message-unrelated"),+ state: "completed",+ },+ [+ {+ kind: "provider.turn.start.failed",+ payload: { requestId: "message-unrelated" },+ },+ ],+ ),+ ).toBe(false);+ expect(reserveBotTurnSubmission(key)).toBeNull();+ release?.();+ });++ it("retains an accepted submission until its exact turn settles", () => {+ const key = "env-a:bot-settlement";+ const requestMessageId = MessageId.make("message-request");+ const release = reserveBotTurnSubmission(key);+ expect(release).not.toBeNull();+ acceptBotTurnSubmission(key, requestMessageId);++ expect(+ releaseBotTurnSubmissionAfterObservation(key, { requestMessageId, state: "running" }, []),+ ).toBe(false);+ expect(reserveBotTurnSubmission(key)).toBeNull();+ expect(+ releaseBotTurnSubmissionAfterObservation(key, { requestMessageId, state: "running" }, [+ {+ kind: "provider.turn.start.failed",+ payload: { requestId: requestMessageId },+ },+ ]),+ ).toBe(false);+ expect(+ releaseBotTurnSubmissionAfterObservation(+ key,+ { requestMessageId: MessageId.make("message-other"), state: "completed" },+ [],+ ),+ ).toBe(false);++ for (const state of ["completed", "interrupted", "error"] as const) {+ expect(releaseBotTurnSubmissionAfterObservation(key, { requestMessageId, state }, [])).toBe(+ true,+ );+ const nextRelease = reserveBotTurnSubmission(key);+ expect(nextRelease).not.toBeNull();+ if (state !== "error") acceptBotTurnSubmission(key, requestMessageId);+ else nextRelease?.();+ }+ });++ it("releases only for the exact provider start failure", () => {+ const key = "env-a:bot-start-failure";+ const requestMessageId = MessageId.make("message-request");+ const release = reserveBotTurnSubmission(key);+ expect(release).not.toBeNull();+ acceptBotTurnSubmission(key, requestMessageId);++ expect(+ releaseBotTurnSubmissionAfterObservation(key, null, [+ {+ kind: "provider.turn.start.failed",+ payload: { requestId: "message-other" },+ },+ { kind: "provider.turn.start.failed", payload: null },+ { kind: "provider.turn.start.failed", payload: { requestId: 42 } },+ ]),+ ).toBe(false);+ expect(reserveBotTurnSubmission(key)).toBeNull();+ expect(+ releaseBotTurnSubmissionAfterObservation(key, null, [+ {+ kind: "provider.turn.start.failed",+ payload: { requestId: "message-request" },+ },+ ]),+ ).toBe(true);+ });++ it("does not let stale cleanup release a newer submission", () => {+ const key = "env-a:bot-replaced";+ const staleRelease = reserveBotTurnSubmission(key);+ expect(staleRelease).not.toBeNull();+ staleRelease?.();++ const nextRelease = reserveBotTurnSubmission(key);+ expect(nextRelease).not.toBeNull();+ staleRelease?.();+ expect(reserveBotTurnSubmission(key)).toBeNull();+ nextRelease?.();+ });++ it("reconciles a terminal turn received while the bot view was unmounted", () => {+ const key = "env-a:bot-navigation";+ const requestMessageId = MessageId.make("message-before-navigation");+ const staleRelease = reserveBotTurnSubmission(key);+ expect(staleRelease).not.toBeNull();+ acceptBotTurnSubmission(key, requestMessageId);++ const nextRelease = reserveBotTurnSubmissionAfterObservation(+ key,+ { requestMessageId, state: "completed" },+ [],+ );+ expect(nextRelease).not.toBeNull();+ staleRelease?.();+ expect(reserveBotTurnSubmission(key)).toBeNull();+ nextRelease?.();+ });+it("shares concurrent initial thread creation", async () => {let retained: { threadId: string } | null = null;const inFlight = { current: null as Promise<{ threadId: string } | null> | null };diff --git a/apps/web/src/components/roster/botThreadRuntime.logic.ts b/apps/web/src/components/roster/botThreadRuntime.logic.tsindex 0fd4312be..d4daad714 100644--- a/apps/web/src/components/roster/botThreadRuntime.logic.ts+++ b/apps/web/src/components/roster/botThreadRuntime.logic.ts@@ -7,8 +7,71 @@ import type {ProviderInteractionMode,RuntimeMode,ThreadId,+ OrchestrationLatestTurn,+ OrchestrationThreadActivity,} from "@t3tools/contracts";+const botTurnSubmissions = new Map<+ string,+ {+ readonly requestMessageId: string | null;+ readonly token: symbol;+ }+>();++export function reserveBotTurnSubmission(key: string): (() => void) | null {+ if (botTurnSubmissions.has(key)) return null;+ const token = Symbol(key);+ botTurnSubmissions.set(key, { requestMessageId: null, token });+ return () => {+ if (botTurnSubmissions.get(key)?.token === token) botTurnSubmissions.delete(key);+ };+}++export function acceptBotTurnSubmission(key: string, requestMessageId: string): void {+ const submission = botTurnSubmissions.get(key);+ if (submission) botTurnSubmissions.set(key, { ...submission, requestMessageId });+}++export function releaseBotTurnSubmissionAfterObservation(+ key: string,+ latestTurn: Pick<OrchestrationLatestTurn, "requestMessageId" | "state"> | null | undefined,+ activities: readonly Pick<OrchestrationThreadActivity, "kind" | "payload">[],+): boolean {+ const submission = botTurnSubmissions.get(key);+ if (!submission?.requestMessageId) return false;++ const matchingTurn = latestTurn?.requestMessageId === submission.requestMessageId;+ if (matchingTurn && latestTurn.state === "running") return false;++ const matchingTurnSettled = matchingTurn;+ const providerStartFailed = activities.some((activity) => {+ if (+ activity.kind !== "provider.turn.start.failed" ||+ typeof activity.payload !== "object" ||+ activity.payload === null ||+ !("requestId" in activity.payload) ||+ typeof activity.payload.requestId !== "string"+ ) {+ return false;+ }+ return activity.payload.requestId === submission.requestMessageId;+ });+ if (!matchingTurnSettled && !providerStartFailed) return false;++ botTurnSubmissions.delete(key);+ return true;+}++export function reserveBotTurnSubmissionAfterObservation(+ key: string,+ latestTurn: Pick<OrchestrationLatestTurn, "requestMessageId" | "state"> | null | undefined,+ activities: readonly Pick<OrchestrationThreadActivity, "kind" | "payload">[],+): (() => void) | null {+ releaseBotTurnSubmissionAfterObservation(key, latestTurn, activities);+ return reserveBotTurnSubmission(key);+}+export async function joinOrStartThreadCreate<T>(input: {getRetained: () => T | null;inFlight: { current: Promise<T | null> | null };diff --git a/apps/web/src/components/roster/useBotThreadRuntime.ts b/apps/web/src/components/roster/useBotThreadRuntime.tsindex 734df4e79..664e46c29 100644--- a/apps/web/src/components/roster/useBotThreadRuntime.ts+++ b/apps/web/src/components/roster/useBotThreadRuntime.ts@@ -9,7 +9,7 @@ import {type ModelSelection,type ScopedThreadRef,} from "@t3tools/contracts";-import { useCallback, useMemo, useRef, useState } from "react";+import { useCallback, useEffect, useMemo, useRef, useState } from "react";import { usePrimarySettings } from "../../hooks/useSettings";import { DEFAULT_INTERACTION_MODE } from "../../types";@@ -18,6 +18,7 @@ import { resolveAppModelSelectionState } from "../../modelSelection";import {useAllEnvironmentShellsBootstrapped,useProjects,+ useThreadActivities,useThreadMessages,useThreadShell,useThreadShells,@@ -29,9 +30,12 @@ import { threadEnvironment } from "../../state/threads";import { useAtomCommand } from "../../state/use-atom-command";import { sortScopedProjectsForSidebar } from "../Sidebar.logic";import {+ acceptBotTurnSubmission,buildBotTurnStartInput,findLatestBotThreadTarget,joinOrStartThreadCreate,+ releaseBotTurnSubmissionAfterObservation,+ reserveBotTurnSubmissionAfterObservation,} from "./botThreadRuntime.logic";import { parseChatPath } from "./roster.logic";import { useRosterStore } from "./rosterStore";@@ -113,6 +117,7 @@ export function useBotThreadRuntime(botId: string, effectiveModelSelection: ModeretainedThreadRef.current.threadRef = linkedThreadRef;}const messages = useThreadMessages(linkedThreadRef);+ const activities = useThreadActivities(linkedThreadRef);const defaultProject = useMemo(() =>bootstrapped && primaryEnvironmentId@@ -144,6 +149,15 @@ export function useBotThreadRuntime(botId: string, effectiveModelSelection: Modeconst sendInFlightRef = useRef(false);const [sending, setSending] = useState(false);const [error, setError] = useState<string | null>(null);+ useEffect(() => {+ if (!primaryEnvironmentId) return;+ const submissionKey = `${primaryEnvironmentId}:${botId}`;+ releaseBotTurnSubmissionAfterObservation(+ submissionKey,+ rememberedThread?.latestTurn,+ activities,+ );+ }, [activities, botId, primaryEnvironmentId, rememberedThread?.latestTurn]);const ensureTranscriptThread = useCallback(async (title = `Call with ${bot?.name ?? "bot"}`): Promise<ScopedThreadRef | null> => {@@ -203,11 +217,25 @@ export function useBotThreadRuntime(botId: string, effectiveModelSelection: ModesetError("Add a project before you message a bot.");return false;}+ if (rememberedThread?.latestTurn?.state === "running") {+ setError("Wait for the current reply to finish.");+ return false;+ }const unsupported = files.find((file) => !file.type.startsWith("image/"));if (unsupported) {setError("Bot attachments must be images.");return false;}+ const submissionKey = `${activeProject.environmentId}:${botId}`;+ const releaseSubmission = reserveBotTurnSubmissionAfterObservation(+ submissionKey,+ rememberedThread?.latestTurn,+ activities,+ );+ if (!releaseSubmission) {+ setError("Wait for the current reply to start.");+ return false;+ }sendInFlightRef.current = true;setSending(true);@@ -217,6 +245,8 @@ export function useBotThreadRuntime(botId: string, effectiveModelSelection: ModeeffectiveModelSelection ?? activeProject.defaultModelSelection ?? appDefaultModelSelection;const runtimeMode = bot?.runtimeMode ?? DEFAULT_RUNTIME_MODE;const title = threadTitle(prompt, files);+ const messageId = newMessageId();+ let accepted = false;try {const attachments = await Promise.all(@@ -242,7 +272,7 @@ export function useBotThreadRuntime(botId: string, effectiveModelSelection: ModeprojectId: activeProject.id,title,message: {- messageId: newMessageId(),+ messageId,role: "user",text: prompt,attachments,@@ -259,6 +289,13 @@ export function useBotThreadRuntime(botId: string, effectiveModelSelection: Modereturn false;}+ accepted = true;+ acceptBotTurnSubmission(submissionKey, messageId);+ releaseBotTurnSubmissionAfterObservation(+ submissionKey,+ rememberedThread?.latestTurn,+ activities,+ );retainedThreadRef.current.threadRef = currentThreadRef;useRosterStore.getState()@@ -272,18 +309,21 @@ export function useBotThreadRuntime(botId: string, effectiveModelSelection: ModesetError(cause instanceof Error ? cause.message : "Could not send the message.");return false;} finally {+ if (!accepted) releaseSubmission();sendInFlightRef.current = false;setSending(false);}},[activeProject,+ activities,appDefaultModelSelection,bot,botId,effectiveModelSelection,botReady,ensureTranscriptThread,+ rememberedThread?.latestTurn,startTurn,],);@@ -315,6 +355,7 @@ export function useBotThreadRuntime(botId: string, effectiveModelSelection: ModedefaultProject: activeProject,error,linkedThreadRef,+ latestTurn: rememberedThread?.latestTurn ?? null,messages,send,sending,diff --git a/apps/web/src/components/voice/VoiceCall.test.tsx b/apps/web/src/components/voice/VoiceCall.test.tsxindex 3c9e66192..03b82d4ad 100644--- a/apps/web/src/components/voice/VoiceCall.test.tsx+++ b/apps/web/src/components/voice/VoiceCall.test.tsx@@ -6,11 +6,16 @@ import type { Bot } from "../roster/types";import {BotVoiceCallButtonView,handleVoiceChannelMessage,+ listenForMicrophoneLoss,reduceVoiceCallUiState,resolveVoiceCallOfferSdp,scheduleVoiceDisconnectTimeout,voiceConnectionStateAction,+ voiceEnvironmentConnectionLost,+ voiceStartErrorDescription,waitForIceGathering,+ VoiceCallBarView,+ VoiceCallStartingBarView,type VoiceCallUiState,} from "./VoiceCall";@@ -35,22 +40,23 @@ const bot: Bot = {};describe("voice channel messages", () => {- const makeHandlers = (delivered = true) => ({+ const makeHandlers = () => ({appendTranscript: vi.fn<(role: "user" | "assistant", text: string) => void>(),- sendGoalMessage: vi.fn<(text: string) => Promise<boolean>>(async () => delivered),+ sendGoalMessage: vi.fn<(text: string) => boolean>(() => true),+ speechStarted: vi.fn<() => void>(),+ speechFinished: vi.fn<() => void>(),+ sessionFailed: vi.fn<(message: string) => void>(),});- const flush = () => new Promise((resolve) => setTimeout(resolve, 0));- it("appends both sides of the conversation to the chat", () => {+ it("writes native user and assistant transcripts to the bot thread", () => {const handlers = makeHandlers();- const reply = vi.fn<(payload: string) => void>();handleVoiceChannelMessage(JSON.stringify({type: "conversation.item.input_audio_transcription.completed",+ item_id: "item-1",transcript: " Ship the fix today. ",}),handlers,- reply,);handleVoiceChannelMessage(JSON.stringify({@@ -58,79 +64,97 @@ describe("voice channel messages", () => {transcript: "On it.",}),handlers,- reply,);expect(handlers.appendTranscript.mock.calls).toEqual([["user", "Ship the fix today."],["assistant", "On it."],]);expect(handlers.sendGoalMessage).not.toHaveBeenCalled();- expect(reply).not.toHaveBeenCalled();+ expect(handlers.speechFinished).not.toHaveBeenCalled();});- it("forwards a send_to_chat call as a goal message and confirms to the model", async () => {+ it("keeps the speech queue locked until WebRTC playback drains", () => {const handlers = makeHandlers();- const reply = vi.fn<(payload: string) => void>();handleVoiceChannelMessage(JSON.stringify({- type: "response.function_call_arguments.done",- name: "send_to_chat",- call_id: "call-abc",- arguments: JSON.stringify({ message: "Add retries to the sync job." }),+ type: "response.done",+ response: { status: "completed" },}),handlers,- reply,);- await flush();- expect(handlers.sendGoalMessage).toHaveBeenCalledWith("Add retries to the sync job.");- expect(handlers.appendTranscript).not.toHaveBeenCalled();- const output = JSON.parse(reply.mock.calls[0]![0]) as { item: { output: string } };- const respond = JSON.parse(reply.mock.calls[1]![0]) as unknown;- expect(output).toMatchObject({- type: "conversation.item.create",- item: { type: "function_call_output", call_id: "call-abc" },- });- expect(JSON.parse(output.item.output)).toEqual({ ok: true, delivered: "chat" });- expect(respond).toMatchObject({ type: "response.create" });- });-- it("reports a failed delivery to the model instead of claiming success", async () => {- const handlers = makeHandlers(false);- const reply = vi.fn<(payload: string) => void>();+ expect(handlers.speechFinished).not.toHaveBeenCalled();+ handleVoiceChannelMessage(JSON.stringify({ type: "output_audio_buffer.stopped" }), handlers);+ expect(handlers.speechFinished).toHaveBeenCalledOnce();+ expect(handlers.sessionFailed).not.toHaveBeenCalled();+ });++ it.each(["cancelled", "incomplete"])(+ "does not treat a %s response as drained playback",+ (status) => {+ const handlers = makeHandlers();+ handleVoiceChannelMessage(+ JSON.stringify({ type: "response.done", response: { status } }),+ handlers,+ );+ expect(handlers.speechFinished).not.toHaveBeenCalled();+ expect(handlers.sessionFailed).not.toHaveBeenCalled();+ },+ );++ it("ends a failed Realtime response instead of releasing the queue", () => {+ const handlers = makeHandlers();handleVoiceChannelMessage(JSON.stringify({- type: "response.function_call_arguments.done",- name: "send_to_chat",- call_id: "call-def",- arguments: JSON.stringify({ message: "Fix the flaky test." }),+ type: "response.done",+ response: { status: "failed" },+ }),+ handlers,+ );+ expect(handlers.speechFinished).not.toHaveBeenCalled();+ expect(handlers.sessionFailed).toHaveBeenCalledWith("The voice response failed.");+ });++ it("reports session errors and ignores malformed channel messages", () => {+ const handlers = makeHandlers();+ handleVoiceChannelMessage("not json", handlers);+ expect(handlers.sessionFailed).not.toHaveBeenCalled();+ handleVoiceChannelMessage(+ JSON.stringify({+ type: "error",+ error: { message: "Session expired." },}),handlers,- reply,);- await flush();- const output = JSON.parse(reply.mock.calls[0]![0]) as { item: { output: string } };- expect(JSON.parse(output.item.output)).toMatchObject({ ok: false });+ expect(handlers.sessionFailed).toHaveBeenCalledWith("Session expired.");});- it("acknowledges malformed tool arguments instead of hanging the call", async () => {+ it("delegates workspace work to the existing chat runtime", async () => {const handlers = makeHandlers();- const reply = vi.fn<(payload: string) => void>();- handleVoiceChannelMessage("not json", handlers, reply);- expect(reply).not.toHaveBeenCalled();+ const replies: string[] = [];handleVoiceChannelMessage(JSON.stringify({type: "response.function_call_arguments.done",name: "send_to_chat",- call_id: "call-broken",- arguments: "broken",+ call_id: "call-1",+ arguments: JSON.stringify({ message: "Run the focused tests" }),}),handlers,- reply,+ (payload) => replies.push(payload),);- await flush();- expect(handlers.sendGoalMessage).not.toHaveBeenCalled();- const output = JSON.parse(reply.mock.calls[0]![0]) as { item: { output: string } };- expect(JSON.parse(output.item.output)).toMatchObject({ ok: false });+ await Promise.resolve();+ await Promise.resolve();+ expect(handlers.sendGoalMessage).toHaveBeenCalledWith("Run the focused tests");+ expect(replies.map((payload) => JSON.parse(payload))).toEqual([+ {+ type: "conversation.item.create",+ item: {+ type: "function_call_output",+ call_id: "call-1",+ output: JSON.stringify({ ok: true, delivered: "chat" }),+ },+ },+ { type: "response.create" },+ ]);});});@@ -201,6 +225,30 @@ describe("voice call UI", () => {}});+ it("turns microphone permission and device errors into actions", () => {+ expect(voiceStartErrorDescription(new DOMException("denied", "NotAllowedError"))).toBe(+ "Allow microphone access, then try again.",+ );+ expect(voiceStartErrorDescription(new DOMException("missing", "NotFoundError"))).toBe(+ "Connect a microphone, then try again.",+ );+ });++ it("listens for microphone loss and removes the listener during cleanup", () => {+ const track = {+ addEventListener: vi.fn(),+ removeEventListener: vi.fn(),+ };+ const onLost = vi.fn();+ const stop = listenForMicrophoneLoss(+ { getAudioTracks: () => [track as unknown as MediaStreamTrack] },+ onLost,+ );+ expect(track.addEventListener).toHaveBeenCalledWith("ended", onLost);+ stop();+ expect(track.removeEventListener).toHaveBeenCalledWith("ended", onLost);+ });+it("keeps the recovery window until a disconnected peer reconnects", () => {expect(voiceConnectionStateAction("disconnected")).toBe("wait-for-recovery");expect(voiceConnectionStateAction("connecting")).toBe("keep-recovery-window");@@ -210,6 +258,33 @@ describe("voice call UI", () => {expect(voiceConnectionStateAction("closed")).toBe("end");});+ it("ends a local call when its environment session disconnects", () => {+ expect(+ voiceEnvironmentConnectionLost({+ desired: true,+ network: "online",+ phase: "connected",+ stage: null,+ attempt: 0,+ generation: 1,+ lastFailure: null,+ retryAt: null,+ }),+ ).toBe(false);+ expect(+ voiceEnvironmentConnectionLost({+ desired: true,+ network: "online",+ phase: "backoff",+ stage: null,+ attempt: 1,+ generation: 1,+ lastFailure: null,+ retryAt: Date.now(),+ }),+ ).toBe(true);+ });+it("ends the recovery window unless the peer reaches connected", () => {vi.useFakeTimers();try {@@ -245,4 +320,35 @@ describe("voice call UI", () => {};expect(reduceVoiceCallUiState(active, { type: "hung-up" })).toBeNull();});++ it("keeps return and hangup controls visible in the active-call bar", () => {+ const startedAt = "2026-08-27T00:00:00.000Z";+ const html = renderToStaticMarkup(+ <VoiceCallBarView+ activeCall={{+ callId: "call-1",+ status: "live",+ botId: BotId.make("bot-akeru"),+ botName: "Akeru",+ startedAt,+ }}+ reconnecting+ now={Date.parse(startedAt) + 65_000}+ onReturn={() => {}}+ onHangup={() => {}}+ />,+ );+ expect(html).toContain('aria-label="Return to call with Akeru"');+ expect(html).toContain('aria-label="Hang up"');+ expect(html).toContain("Reconnecting");+ expect(html).toContain("1:05");+ });++ it("keeps a cancel control visible while a call starts", () => {+ const html = renderToStaticMarkup(+ <VoiceCallStartingBarView botName="Akeru" onCancel={() => {}} />,+ );+ expect(html).toContain("Calling Akeru");+ expect(html).toContain('aria-label="Cancel call to Akeru"');+ });});diff --git a/apps/web/src/components/voice/VoiceCall.tsx b/apps/web/src/components/voice/VoiceCall.tsxindex a099db5c1..3cb2873a3 100644--- a/apps/web/src/components/voice/VoiceCall.tsx+++ b/apps/web/src/components/voice/VoiceCall.tsx@@ -1,4 +1,6 @@import { CallEndIcon, CallIcon } from "@hugeicons/core-free-icons";+import { useAtomValue } from "@effect/atom-react";+import type { SupervisorConnectionState } from "@t3tools/client-runtime/connection";import { BotId, type VoiceCallSnapshot } from "@t3tools/contracts";import { useNavigate } from "@tanstack/react-router";import * as Cause from "effect/Cause";@@ -15,6 +17,7 @@ import {} from "react";import type { Bot } from "../roster/types";+import { resolveStickyBotEngine } from "../roster/botEngineSelection";import { useBotThreadRuntime } from "../roster/useBotThreadRuntime";import { useRosterStore } from "../roster/rosterStore";import { Button } from "../ui/button";@@ -22,8 +25,15 @@ import { AppIcon } from "../ui/app-icon";import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";import { toastManager } from "../ui/toast";import { usePrimarySettings } from "../../hooks/useSettings";-import { serverEnvironment } from "../../state/server";-import { usePrimaryEnvironmentId } from "../../state/environments";+import { resolveAppModelSelectionState } from "../../modelSelection";+import {+ applyProviderInstanceSettings,+ deriveProviderInstanceEntries,+ sortProviderInstanceEntries,+} from "../../providerInstances";+import { primaryServerProvidersAtom, serverEnvironment } from "../../state/server";+import { useProjects } from "../../state/entities";+import { useEnvironmentConnectionState, usePrimaryEnvironmentId } from "../../state/environments";import { useAtomCommand } from "../../state/use-atom-command";interface ActiveBrowserCall {@@ -32,7 +42,22 @@ interface ActiveBrowserCall {readonly peer: RTCPeerConnection;readonly microphone: MediaStream;readonly speaker: HTMLAudioElement;+ readonly events: RTCDataChannel;+ stopListeningForDeviceLoss: () => void;disconnectTimer: ReturnType<typeof setTimeout> | null;+ environmentDisconnectTimer: ReturnType<typeof setTimeout> | null;+}++interface PendingBrowserCall {+ cancelled: boolean;+ readonly abortController: AbortController;+ readonly environmentId: NonNullable<ReturnType<typeof usePrimaryEnvironmentId>>;+ failure: Error | null;+ microphone: MediaStream | null;+ peer: RTCPeerConnection | null;+ speaker: HTMLAudioElement | null;+ events: RTCDataChannel | null;+ stopListeningForDeviceLoss: () => void;}export type VoiceCallUiState = ActiveBrowserCall["call"] | null;@@ -50,6 +75,9 @@ export function reduceVoiceCallUiState(export interface VoiceCallChatHandlers {readonly appendTranscript: (role: "user" | "assistant", text: string) => void | Promise<void>;readonly sendGoalMessage: (text: string) => boolean | Promise<boolean>;+ readonly speechStarted: () => void;+ readonly speechFinished: () => void;+ readonly sessionFailed: (message: string) => void;}function stringField(value: unknown, field: string): string | null {@@ -61,7 +89,7 @@ function stringField(value: unknown, field: string): string | null {export function handleVoiceChannelMessage(raw: string,handlers: VoiceCallChatHandlers,- reply: (payload: string) => void,+ reply: (payload: string) => void = () => {},): void {let event: unknown;try {@@ -72,7 +100,7 @@ export function handleVoiceChannelMessage(const type = stringField(event, "type");const transcript = stringField(event, "transcript")?.trim() ?? "";if (type === "conversation.item.input_audio_transcription.completed" && transcript.length > 0) {- handlers.appendTranscript("user", transcript);+ void handlers.appendTranscript("user", transcript);return;}if (@@ -80,7 +108,26 @@ export function handleVoiceChannelMessage(type === "response.audio_transcript.done") &&transcript.length > 0) {- handlers.appendTranscript("assistant", transcript);+ void handlers.appendTranscript("assistant", transcript);+ return;+ }+ if (type === "output_audio_buffer.started") {+ handlers.speechStarted();+ return;+ }+ if (type === "output_audio_buffer.stopped") {+ handlers.speechFinished();+ return;+ }+ if (type === "response.done") {+ const response =+ typeof event === "object" && event !== null+ ? (event as Record<string, unknown>).response+ : null;+ const status = stringField(response, "status");+ if (status !== "completed" && status !== "cancelled" && status !== "incomplete") {+ handlers.sessionFailed("The voice response failed.");+ }return;}if (@@ -123,25 +170,64 @@ export function handleVoiceChannelMessage(: { ok: false, error: "The chat did not accept the message." },),);+ return;+ }+ if (type === "error") {+ const realtimeError =+ typeof event === "object" && event !== null+ ? stringField((event as Record<string, unknown>).error, "message")+ : null;+ handlers.sessionFailed(realtimeError ?? "The voice session failed.");+ }+}++export function voiceStartErrorDescription(cause: unknown): string {+ if (cause instanceof DOMException) {+ switch (cause.name) {+ case "NotAllowedError":+ case "SecurityError":+ return "Allow microphone access, then try again.";+ case "NotFoundError":+ return "Connect a microphone, then try again.";+ case "NotReadableError":+ case "AbortError":+ return "The microphone is unavailable. Close other audio apps, then try again.";+ }}+ return cause instanceof Error ? cause.message : "Check microphone access, then try again.";+}++export function listenForMicrophoneLoss(+ microphone: Pick<MediaStream, "getAudioTracks">,+ onLost: () => void,+): () => void {+ const tracks = microphone.getAudioTracks();+ tracks.forEach((track) => track.addEventListener("ended", onLost));+ return () => tracks.forEach((track) => track.removeEventListener("ended", onLost));}interface VoiceCallContextValue {readonly activeCall: ActiveBrowserCall["call"] | null;+ readonly reconnecting: boolean;readonly startingBotId: string | null;- readonly startOrReturn: (bot: Bot, chatHandlers?: VoiceCallChatHandlers) => void;+ readonly startOrReturn: (bot: Bot) => void;readonly hangup: () => void;readonly returnToCall: () => void;}const VoiceCallContext = createContext<VoiceCallContextValue | null>(null);-export function waitForIceGathering(peer: RTCPeerConnection, timeoutMs = 5_000): Promise<void> {+export function waitForIceGathering(+ peer: RTCPeerConnection,+ timeoutMs = 5_000,+ signal?: AbortSignal,+): Promise<void> {if (peer.iceGatheringState === "complete") return Promise.resolve();return new Promise((resolve) => {const finish = () => {clearTimeout(timer);peer.removeEventListener("icegatheringstatechange", onChange);+ signal?.removeEventListener("abort", finish);resolve();};const onChange = () => {@@ -149,6 +235,7 @@ export function waitForIceGathering(peer: RTCPeerConnection, timeoutMs = 5_000):};const timer = setTimeout(finish, timeoutMs);peer.addEventListener("icegatheringstatechange", onChange);+ signal?.addEventListener("abort", finish, { once: true });});}@@ -170,6 +257,12 @@ export function voiceConnectionStateAction(return "keep-recovery-window";}+export function voiceEnvironmentConnectionLost(+ connection: SupervisorConnectionState | null,+): boolean {+ return connection !== null && connection.phase !== "connected";+}+export function scheduleVoiceDisconnectTimeout(getConnectionState: () => RTCPeerConnectionState,onTimeout: (recovered: boolean) => void,@@ -179,26 +272,153 @@ export function scheduleVoiceDisconnectTimeout(function stopBrowserCall(active: ActiveBrowserCall): void {if (active.disconnectTimer !== null) clearTimeout(active.disconnectTimer);+ if (active.environmentDisconnectTimer !== null) {+ clearTimeout(active.environmentDisconnectTimer);+ }+ active.stopListeningForDeviceLoss();+ active.events.onmessage = null;+ active.events.onerror = null;+ active.events.onclose = null;active.microphone.getTracks().forEach((track) => track.stop());active.peer.close();active.speaker.pause();active.speaker.srcObject = null;}+function cleanPendingBrowserCall(pending: PendingBrowserCall): void {+ pending.abortController.abort();+ pending.stopListeningForDeviceLoss();+ if (pending.events) {+ pending.events.onmessage = null;+ pending.events.onerror = null;+ pending.events.onclose = null;+ }+ pending.microphone?.getTracks().forEach((track) => track.stop());+ pending.peer?.close();+ pending.speaker?.pause();+ if (pending.speaker) pending.speaker.srcObject = null;+}+export function VoiceCallProvider({ children }: { readonly children: ReactNode }) {const navigate = useNavigate();const environmentId = usePrimaryEnvironmentId();+ const environmentConnection = useEnvironmentConnectionState(environmentId);+ const projects = useProjects();const startVoiceCall = useAtomCommand(serverEnvironment.startVoiceCall, { reportFailure: false });const hangupVoiceCall = useAtomCommand(serverEnvironment.hangupVoiceCall, {reportFailure: false,});const [activeCall, dispatchCall] = useReducer(reduceVoiceCallUiState, null);+ const [reconnecting, setReconnecting] = useState(false);const [startingBotId, setStartingBotId] = useState<string | null>(null);+ const sessionBotId = startingBotId ?? activeCall?.botId ?? null;+ const sessionBot = useRosterStore((state) =>+ sessionBotId === null+ ? null+ : (state.bots.find((candidate) => candidate.id === sessionBotId) ?? null),+ );+ const settings = usePrimarySettings();+ const providers = useAtomValue(primaryServerProvidersAtom);+ const instanceEntries = useMemo(+ () =>+ sortProviderInstanceEntries(+ applyProviderInstanceSettings(deriveProviderInstanceEntries(providers), settings),+ ),+ [providers, settings],+ );+ const defaultSelection = useMemo(+ () => resolveAppModelSelectionState(settings, providers),+ [providers, settings],+ );+ const sessionModelSelection = useMemo(+ () =>+ sessionBot+ ? resolveStickyBotEngine({+ engine: sessionBot.engine,+ instanceEntries,+ settings,+ providers,+ defaultSelection,+ })+ : null,+ [defaultSelection, instanceEntries, providers, sessionBot, settings],+ );+ const runtime = useBotThreadRuntime(sessionBotId ?? "", sessionModelSelection);+ const runtimeRef = useRef(runtime);+ runtimeRef.current = runtime;const activeRef = useRef<ActiveBrowserCall | null>(null);+ const mountedRef = useRef(true);+ const pendingStartRef = useRef<PendingBrowserCall | null>(null);const startingBotRef = useRef<string | null>(null);const hangupCommandRef = useRef(hangupVoiceCall);hangupCommandRef.current = hangupVoiceCall;+ const endBrowserCall = useCallback(+ (+ current: ActiveBrowserCall,+ notice?: {+ readonly type: "warning" | "error";+ readonly title: string;+ readonly description?: string;+ },+ ) => {+ if (activeRef.current !== current) return;+ activeRef.current = null;+ setReconnecting(false);+ stopBrowserCall(current);+ dispatchCall({ type: "hung-up" });+ void hangupCommandRef.current({+ environmentId: current.environmentId,+ input: { callId: current.call.callId },+ });+ if (notice) toastManager.add(notice);+ },+ [],+ );++ useEffect(() => {+ const connection = environmentConnection.data;+ const pending = pendingStartRef.current;+ if (+ pending &&+ (environmentId !== pending.environmentId || voiceEnvironmentConnectionLost(connection))+ ) {+ pending.failure ??= new Error(+ environmentId !== pending.environmentId+ ? "The active environment changed."+ : "The environment connection was lost.",+ );+ cleanPendingBrowserCall(pending);+ }+ const current = activeRef.current;+ if (!current) return;+ if (environmentId !== current.environmentId) {+ endBrowserCall(current, {+ type: "warning",+ title: "Call ended",+ description: "The active environment changed.",+ });+ return;+ }+ if (!voiceEnvironmentConnectionLost(connection)) {+ if (current.environmentDisconnectTimer !== null) {+ clearTimeout(current.environmentDisconnectTimer);+ current.environmentDisconnectTimer = null;+ }+ setReconnecting(false);+ return;+ }+ setReconnecting(true);+ current.environmentDisconnectTimer ??= setTimeout(() => {+ current.environmentDisconnectTimer = null;+ endBrowserCall(current, {+ type: "warning",+ title: "Call connection lost",+ description: "Start a new call after the environment reconnects.",+ });+ }, 5_000);+ }, [endBrowserCall, environmentConnection.data, environmentId]);+const returnToCall = useCallback(() => {const call = activeRef.current?.call;if (!call) return;@@ -207,43 +427,120 @@ export function VoiceCallProvider({ children }: { readonly children: ReactNode }}, [navigate]);const startOrReturn = useCallback(- (bot: Bot, chatHandlers?: VoiceCallChatHandlers) => {+ (bot: Bot) => {const current = activeRef.current;if (current !== null) {if (current.call.botId === bot.id) returnToCall();else toastManager.add({ type: "warning", title: "A call is already active" });return;}- if (startingBotRef.current !== null || environmentId === null) return;+ if (startingBotRef.current !== null) return;+ if (environmentId === null) {+ toastManager.add({ type: "error", title: "Voice is unavailable" });+ return;+ }+ if (!projects.some((project) => project.environmentId === environmentId)) {+ toastManager.add({+ type: "error",+ title: "Voice is unavailable",+ description: "Add a project before you call a bot.",+ });+ return;+ }startingBotRef.current = bot.id;setStartingBotId(bot.id);void (async () => {- let microphone: MediaStream | null = null;- let peer: RTCPeerConnection | null = null;- let speaker: HTMLAudioElement | null = null;+ const pending: PendingBrowserCall = {+ cancelled: false,+ abortController: new AbortController(),+ environmentId,+ failure: null,+ microphone: null,+ peer: null,+ speaker: null,+ events: null,+ stopListeningForDeviceLoss: () => {},+ };+ pendingStartRef.current = pending;let serverCallId: string | null = null;try {- microphone = await navigator.mediaDevices.getUserMedia({ audio: true });- peer = new RTCPeerConnection();- speaker = new Audio();+ if (!navigator.mediaDevices?.getUserMedia) {+ throw new DOMException("No microphone API", "NotFoundError");+ }+ const microphone = await navigator.mediaDevices.getUserMedia({+ audio: {+ echoCancellation: true,+ noiseSuppression: true,+ autoGainControl: true,+ },+ video: false,+ });+ pending.microphone = microphone;+ if (pending.cancelled) {+ microphone.getTracks().forEach((track) => track.stop());+ return;+ }+ pending.stopListeningForDeviceLoss = listenForMicrophoneLoss(microphone, () => {+ pending.failure ??= new Error("The microphone disconnected.");+ pending.peer?.close();+ });+ const peer = new RTCPeerConnection();+ pending.peer = peer;+ const speaker = new Audio();+ pending.speaker = speaker;speaker.autoplay = true;peer.ontrack = (event) => {if (!event.streams[0]) return;- speaker!.srcObject = event.streams[0];- void speaker!.play().catch(() => undefined);+ speaker.srcObject = event.streams[0];+ void speaker.play().catch(() => undefined);};- microphone.getTracks().forEach((track) => peer!.addTrack(track, microphone!));+ microphone.getTracks().forEach((track) => peer.addTrack(track, microphone));const events = peer.createDataChannel("oai-events");- if (chatHandlers) {- events.onmessage = (channelMessage) => {- handleVoiceChannelMessage(String(channelMessage.data), chatHandlers, (payload) => {- if (events.readyState === "open") events.send(payload);+ pending.events = events;+ events.onerror = () => {+ pending.failure ??= new Error("The call connection failed.");+ };+ events.onclose = () => {+ pending.failure ??= new Error("The voice session closed.");+ };+ const chatHandlers: VoiceCallChatHandlers = {+ appendTranscript: (role, text) => runtimeRef.current.appendTranscript(role, text),+ sendGoalMessage: (text) => runtimeRef.current.send(text, []),+ speechStarted: () => {+ microphone.getAudioTracks().forEach((track) => {+ track.enabled = false;});- };- }+ },+ speechFinished: () => {+ microphone.getAudioTracks().forEach((track) => {+ track.enabled = true;+ });+ },+ sessionFailed: (message) => {+ const active = activeRef.current;+ if (active?.events === events) {+ endBrowserCall(active, {+ type: "error",+ title: "Voice session failed",+ description: message,+ });+ return;+ }+ pending.failure ??= new Error(message);+ pending.peer?.close();+ },+ };+ events.onmessage = (channelMessage) =>+ handleVoiceChannelMessage(String(channelMessage.data), chatHandlers, (payload) => {+ if (events.readyState === "open") events.send(payload);+ });const offer = await peer.createOffer();+ if (pending.failure) throw pending.failure;await peer.setLocalDescription(offer);- await waitForIceGathering(peer);+ if (pending.failure) throw pending.failure;+ await waitForIceGathering(peer, 5_000, pending.abortController.signal);+ if (pending.cancelled) return;+ if (pending.failure) throw pending.failure;const sdp = resolveVoiceCallOfferSdp(peer, offer);if (!sdp) throw new Error("The microphone did not produce a call offer.");const result = await startVoiceCall({@@ -257,28 +554,58 @@ export function VoiceCallProvider({ children }: { readonly children: ReactNode });}serverCallId = result.value.call.callId;+ if (pending.cancelled) {+ void hangupCommandRef.current({+ environmentId,+ input: { callId: serverCallId },+ });+ return;+ }+ if (pending.failure) throw pending.failure;await peer.setRemoteDescription({ type: "answer", sdp: result.value.answerSdp });+ if (pending.cancelled) {+ void hangupCommandRef.current({+ environmentId,+ input: { callId: serverCallId },+ });+ return;+ }+ if (pending.failure) throw pending.failure;const browserCall: ActiveBrowserCall = {call: result.value.call,environmentId,peer,microphone,speaker,+ events,+ stopListeningForDeviceLoss: pending.stopListeningForDeviceLoss,disconnectTimer: null,+ environmentDisconnectTimer: null,};activeRef.current = browserCall;dispatchCall({ type: "connected", call: browserCall.call });- const endDisconnectedCall = () => {- if (activeRef.current !== browserCall) return;- activeRef.current = null;- stopBrowserCall(browserCall);- dispatchCall({ type: "hung-up" });- void hangupCommandRef.current({- environmentId: browserCall.environmentId,- input: { callId: browserCall.call.callId },+ setReconnecting(false);+ pending.stopListeningForDeviceLoss = () => {};+ browserCall.stopListeningForDeviceLoss();+ browserCall.stopListeningForDeviceLoss = listenForMicrophoneLoss(microphone, () =>+ endBrowserCall(browserCall, {+ type: "warning",+ title: "Microphone disconnected",+ description: "Connect a microphone, then start a new call.",+ }),+ );+ events.onerror = () =>+ endBrowserCall(browserCall, {+ type: "error",+ title: "Call connection failed",+ description: "Start a new call to continue.",+ });+ events.onclose = () =>+ endBrowserCall(browserCall, {+ type: "warning",+ title: "Call ended",+ description: "The voice session closed.",});- toastManager.add({ type: "warning", title: "Call ended" });- };browserCall.peer.onconnectionstatechange = () => {const action = voiceConnectionStateAction(browserCall.peer.connectionState);if (action === "recovered") {@@ -286,59 +613,80 @@ export function VoiceCallProvider({ children }: { readonly children: ReactNode }clearTimeout(browserCall.disconnectTimer);browserCall.disconnectTimer = null;}+ setReconnecting(false);return;}if (action === "wait-for-recovery") {+ setReconnecting(true);browserCall.disconnectTimer ??= scheduleVoiceDisconnectTimeout(() => browserCall.peer.connectionState,(recovered) => {browserCall.disconnectTimer = null;- if (!recovered) endDisconnectedCall();+ if (!recovered) {+ endBrowserCall(browserCall, {+ type: "warning",+ title: "Call connection lost",+ description: "Start a new call to continue.",+ });+ }},);return;}- if (action === "end") endDisconnectedCall();+ if (action === "end") {+ endBrowserCall(browserCall, {+ type: "error",+ title: "Call connection failed",+ description: "Start a new call to continue.",+ });+ }};} catch (error) {- microphone?.getTracks().forEach((track) => track.stop());- peer?.close();- if (speaker) speaker.srcObject = null;+ cleanPendingBrowserCall(pending);if (serverCallId !== null) {void hangupVoiceCall({ environmentId, input: { callId: serverCallId } });}+ if (pending.cancelled) return;toastManager.add({type: "error",title: "Could not start call",- description: error instanceof Error ? error.message : "Check microphone access.",+ description: voiceStartErrorDescription(error),});} finally {+ if (pendingStartRef.current === pending) pendingStartRef.current = null;if (startingBotRef.current === bot.id) startingBotRef.current = null;- setStartingBotId(null);+ if (mountedRef.current) setStartingBotId(null);}})();},- [environmentId, hangupVoiceCall, returnToCall, startVoiceCall, startingBotId],+ [endBrowserCall, environmentId, hangupVoiceCall, projects, returnToCall, startVoiceCall],);const hangup = useCallback(() => {const current = activeRef.current;- if (!current) return;- activeRef.current = null;- stopBrowserCall(current);- dispatchCall({ type: "hung-up" });- void hangupVoiceCall({- environmentId: current.environmentId,- input: { callId: current.call.callId },- }).then((result) => {- if (result._tag === "Failure") {- toastManager.add({ type: "warning", title: "Call ended locally" });- }- });- }, [hangupVoiceCall]);+ if (current) {+ endBrowserCall(current);+ return;+ }+ const pending = pendingStartRef.current;+ if (!pending) return;+ pending.cancelled = true;+ cleanPendingBrowserCall(pending);+ pendingStartRef.current = null;+ startingBotRef.current = null;+ setStartingBotId(null);+ }, [endBrowserCall]);- useEffect(- () => () => {+ useEffect(() => {+ mountedRef.current = true;+ return () => {+ mountedRef.current = false;+ const pending = pendingStartRef.current;+ if (pending) {+ pending.cancelled = true;+ cleanPendingBrowserCall(pending);+ pendingStartRef.current = null;+ }const current = activeRef.current;if (!current) return;activeRef.current = null;@@ -347,13 +695,12 @@ export function VoiceCallProvider({ children }: { readonly children: ReactNode }environmentId: current.environmentId,input: { callId: current.call.callId },});- },- [],- );+ };+ }, []);const value = useMemo<VoiceCallContextValue>(- () => ({ activeCall, startingBotId, startOrReturn, hangup, returnToCall }),- [activeCall, hangup, returnToCall, startOrReturn, startingBotId],+ () => ({ activeCall, reconnecting, startingBotId, startOrReturn, hangup, returnToCall }),+ [activeCall, hangup, reconnecting, returnToCall, startOrReturn, startingBotId],);return (@@ -364,7 +711,7 @@ export function VoiceCallProvider({ children }: { readonly children: ReactNode });}-function useVoiceCall() {+export function useVoiceCall() {const value = useContext(VoiceCallContext);if (!value) throw new Error("Voice call controls must be inside VoiceCallProvider.");return value;@@ -406,26 +753,22 @@ export function BotVoiceCallButtonView({);}-export function BotVoiceCallButton({ bot }: { readonly bot: Bot }) {+export function BotVoiceCallButton({+ bot,+ disabled = false,+}: {+ readonly bot: Bot;+ readonly disabled?: boolean;+}) {const { activeCall, startingBotId, startOrReturn } = useVoiceCall();const globallyEnabled = usePrimarySettings((settings) => settings.voice.enabled);- const runtime = useBotThreadRuntime(bot.id, null);- const runtimeRef = useRef(runtime);- runtimeRef.current = runtime;- const chatHandlers = useMemo<VoiceCallChatHandlers>(- () => ({- appendTranscript: (role, text) => runtimeRef.current.appendTranscript(role, text),- sendGoalMessage: (text) => runtimeRef.current.send(text, []),- }),- [],- );return (<BotVoiceCallButtonViewbot={bot}active={activeCall?.botId === bot.id}- disabled={startingBotId !== null}+ disabled={disabled || startingBotId !== null}globallyEnabled={globallyEnabled}- onClick={() => startOrReturn(bot, chatHandlers)}+ onClick={() => startOrReturn(bot)}/>);}@@ -440,7 +783,12 @@ export function SelectedBotVoiceCallButton() {}function VoiceCallBar() {- const { activeCall, hangup, returnToCall } = useVoiceCall();+ const { activeCall, hangup, reconnecting, returnToCall, startingBotId } = useVoiceCall();+ const startingBotName = useRosterStore((state) =>+ startingBotId === null+ ? null+ : (state.bots.find((candidate) => candidate.id === startingBotId)?.name ?? "Bot"),+ );const [now, setNow] = useState(() => Date.now());useEffect(() => {@@ -450,7 +798,60 @@ function VoiceCallBar() {return () => window.clearInterval(timer);}, [activeCall]);- if (!activeCall) return null;+ if (!activeCall) {+ return startingBotName ? (+ <VoiceCallStartingBarView botName={startingBotName} onCancel={hangup} />+ ) : null;+ }+ return (+ <VoiceCallBarView+ activeCall={activeCall}+ reconnecting={reconnecting}+ now={now}+ onReturn={returnToCall}+ onHangup={hangup}+ />+ );+}++export function VoiceCallStartingBarView({+ botName,+ onCancel,+}: {+ readonly botName: string;+ readonly onCancel: () => void;+}) {+ return (+ <div className="pointer-events-none fixed inset-x-0 top-2 z-60 flex justify-center px-4">+ <div className="pointer-events-auto flex h-10 items-center rounded-full border border-border bg-background/95 pl-4 pr-1.5 shadow-lg backdrop-blur">+ <span className="pr-3 text-sm font-medium">Calling {botName}</span>+ <Button+ type="button"+ size="icon-sm"+ variant="destructive"+ aria-label={`Cancel call to ${botName}`}+ onClick={onCancel}+ >+ <AppIcon icon={CallEndIcon} />+ </Button>+ </div>+ </div>+ );+}++export function VoiceCallBarView({+ activeCall,+ reconnecting,+ now,+ onReturn,+ onHangup,+}: {+ readonly activeCall: ActiveBrowserCall["call"];+ readonly reconnecting: boolean;+ readonly now: number;+ readonly onReturn: () => void;+ readonly onHangup: () => void;+}) {const elapsedSeconds = Math.max(0, Math.floor((now - Date.parse(activeCall.startedAt)) / 1_000));const minutes = Math.floor(elapsedSeconds / 60);const seconds = String(elapsedSeconds % 60).padStart(2, "0");@@ -462,10 +863,17 @@ function VoiceCallBar() {type="button"aria-label={`Return to call with ${activeCall.botName}`}className="flex items-center gap-2 pr-3"- onClick={returnToCall}+ onClick={onReturn}>- <span className="size-2 rounded-full bg-success" />+ <span+ className={+ reconnecting ? "size-2 rounded-full bg-warning" : "size-2 rounded-full bg-success"+ }+ /><span className="text-sm font-medium">{activeCall.botName}</span>+ {reconnecting ? (+ <span className="text-xs text-muted-foreground">Reconnecting</span>+ ) : null}<span className="text-xs tabular-nums text-muted-foreground">{minutes}:{seconds}</span>@@ -476,7 +884,7 @@ function VoiceCallBar() {variant="destructive"aria-label="Hang up"className="rounded-full"- onClick={hangup}+ onClick={onHangup}><AppIcon icon={CallEndIcon} /></Button>diff --git a/apps/web/src/components/voice/VoiceCallProvider.test.tsx b/apps/web/src/components/voice/VoiceCallProvider.test.tsxnew file mode 100644index 000000000..c201f0838--- /dev/null+++ b/apps/web/src/components/voice/VoiceCallProvider.test.tsx@@ -0,0 +1,267 @@+import { renderToStaticMarkup } from "react-dom/server";+import { beforeEach, describe, expect, it, vi } from "vite-plus/test";++const mocks = vi.hoisted(() => ({+ activeProject: { id: "project-1", environmentId: "env-1", workspaceRoot: "/tmp/project" },+ bot: {+ id: "bot-akeru",+ name: "Akeru",+ title: "Generalist",+ label: null,+ description: null,+ disabledMcpServerIds: [],+ avatar: { kind: "blob", shape: "circle", color: "#5B7FD4" },+ engine: null,+ sandbox: null,+ runtimeMode: "full-access",+ usageCap: null,+ voiceEnabled: true,+ groupId: null,+ pinned: false,+ archivedAt: null,+ createdAt: "2026-08-27T00:00:00.000Z",+ updatedAt: "2026-08-27T00:00:00.000Z",+ },+ hangupVoiceCall: vi.fn(async () => ({ _tag: "Success", value: undefined })),+ navigate: vi.fn(async () => undefined),+ selectBot: vi.fn(),+ send: vi.fn(async () => true),+ appendTranscript: vi.fn(async () => undefined),+ startAtom: {},+ hangupAtom: {},+ startVoiceCall: vi.fn(),+ toast: vi.fn(),+}));++vi.mock("@effect/atom-react", () => ({ useAtomValue: () => [] }));+vi.mock("@tanstack/react-router", () => ({ useNavigate: () => mocks.navigate }));+vi.mock("../../hooks/useSettings", () => ({+ usePrimarySettings: (selector?: (settings: { voice: { enabled: boolean } }) => unknown) => {+ const settings = { voice: { enabled: true } };+ return selector ? selector(settings) : settings;+ },+}));+vi.mock("../../modelSelection", () => ({+ resolveAppModelSelectionState: () => ({ instanceId: "codex", model: "gpt-5.6" }),+}));+vi.mock("../../providerInstances", () => ({+ applyProviderInstanceSettings: (entries: unknown) => entries,+ deriveProviderInstanceEntries: () => [],+ sortProviderInstanceEntries: (entries: unknown) => entries,+}));+vi.mock("../../state/entities", () => ({ useProjects: () => [mocks.activeProject] }));+vi.mock("../../state/environments", () => ({+ useEnvironmentConnectionState: () => ({ data: null }),+ usePrimaryEnvironmentId: () => "env-1",+}));+vi.mock("../../state/server", () => ({+ primaryServerProvidersAtom: {},+ serverEnvironment: {+ startVoiceCall: mocks.startAtom,+ hangupVoiceCall: mocks.hangupAtom,+ },+}));+vi.mock("../../state/use-atom-command", () => ({+ useAtomCommand: (atom: unknown) =>+ atom === mocks.startAtom ? mocks.startVoiceCall : mocks.hangupVoiceCall,+}));+vi.mock("../roster/botEngineSelection", () => ({+ resolveStickyBotEngine: () => ({ instanceId: "codex", model: "gpt-5.6" }),+}));+vi.mock("../roster/useBotThreadRuntime", () => ({+ useBotThreadRuntime: () => ({+ appendTranscript: mocks.appendTranscript,+ bootstrapped: true,+ botReady: true,+ defaultProject: mocks.activeProject,+ error: null,+ latestTurn: null,+ messages: [],+ send: mocks.send,+ sending: false,+ }),+}));+vi.mock("../roster/rosterStore", () => {+ const state = { bots: [mocks.bot], selectedBotId: mocks.bot.id, selectBot: mocks.selectBot };+ return {+ useRosterStore: Object.assign((selector: (value: typeof state) => unknown) => selector(state), {+ getState: () => state,+ }),+ };+});+vi.mock("../ui/toast", () => ({ toastManager: { add: mocks.toast } }));++import { VoiceCallProvider, useVoiceCall } from "./VoiceCall";++type VoiceControls = ReturnType<typeof useVoiceCall>;++let latestPeer: TestPeer | null = null;++class TestPeer {+ iceGatheringState: RTCIceGatheringState = "complete";+ connectionState: RTCPeerConnectionState = "connected";+ localDescription: RTCSessionDescription | null = null;+ ontrack: ((event: RTCTrackEvent) => void) | null = null;+ onconnectionstatechange: (() => void) | null = null;+ readonly events = {+ readyState: "open" as RTCDataChannelState,+ onmessage: null as ((event: MessageEvent) => void) | null,+ onerror: null as (() => void) | null,+ onclose: null as (() => void) | null,+ send: vi.fn(),+ };+ constructor() {+ latestPeer = this;+ }+ addTrack() {}+ addEventListener() {}+ removeEventListener() {}+ close() {}+ createDataChannel() {+ return this.events as unknown as RTCDataChannel;+ }+ async createOffer(): Promise<RTCSessionDescriptionInit> {+ return { type: "offer", sdp: "offer-sdp" };+ }+ async setLocalDescription(description: RTCSessionDescriptionInit) {+ this.localDescription = description as RTCSessionDescription;+ }+ async setRemoteDescription() {}+}++function renderControls(): VoiceControls {+ let controls: VoiceControls | null = null;+ function Probe() {+ controls = useVoiceCall();+ return null;+ }+ renderToStaticMarkup(+ <VoiceCallProvider>+ <Probe />+ </VoiceCallProvider>,+ );+ if (controls === null) throw new Error("Voice controls did not mount.");+ return controls;+}++async function flushVoiceStart() {+ for (let index = 0; index < 12; index += 1) await Promise.resolve();+}++describe("voice call provider", () => {+ const track = {+ enabled: true,+ stop: vi.fn(),+ addEventListener: vi.fn(),+ removeEventListener: vi.fn(),+ };+ const microphone = {+ getTracks: () => [track],+ getAudioTracks: () => [track],+ };++ beforeEach(() => {+ vi.clearAllMocks();+ latestPeer = null;+ vi.stubGlobal("RTCPeerConnection", TestPeer);+ vi.stubGlobal(+ "Audio",+ class {+ autoplay = false;+ srcObject: MediaProvider | null = null;+ pause() {}+ async play() {}+ },+ );+ mocks.startVoiceCall.mockResolvedValue({+ _tag: "Success",+ value: {+ call: {+ callId: "call-1",+ status: "live",+ botId: mocks.bot.id,+ botName: mocks.bot.name,+ startedAt: "2026-08-27T00:00:00.000Z",+ },+ answerSdp: "answer-sdp",+ },+ });+ });++ it("refuses a second start while the first call is pending and lets hangup cancel it", async () => {+ let resolveMicrophone!: (value: typeof microphone) => void;+ const pendingMicrophone = new Promise<typeof microphone>((resolve) => {+ resolveMicrophone = resolve;+ });+ vi.stubGlobal("navigator", {+ mediaDevices: { getUserMedia: vi.fn(() => pendingMicrophone) },+ });+ const controls = renderControls();++ controls.startOrReturn(mocks.bot as never);+ controls.startOrReturn({ ...mocks.bot, id: "bot-other", name: "Other" } as never);+ expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledOnce();++ controls.hangup();+ resolveMicrophone(microphone);+ await flushVoiceStart();+ expect(track.stop).toHaveBeenCalled();+ expect(mocks.startVoiceCall).not.toHaveBeenCalled();+ });++ it("returns to the active bot instead of starting a second call", async () => {+ vi.stubGlobal("navigator", {+ mediaDevices: { getUserMedia: vi.fn(async () => microphone) },+ });+ const controls = renderControls();++ controls.startOrReturn(mocks.bot as never);+ await flushVoiceStart();+ expect(mocks.startVoiceCall).toHaveBeenCalledOnce();++ controls.startOrReturn(mocks.bot as never);+ expect(mocks.startVoiceCall).toHaveBeenCalledOnce();+ expect(mocks.selectBot).toHaveBeenCalledWith(mocks.bot.id);+ expect(mocks.navigate).toHaveBeenCalledWith({+ to: "/bots/$botId",+ params: { botId: mocks.bot.id },+ });+ });++ it("uses speech-safe microphone constraints and resumes input after playback drains", async () => {+ vi.stubGlobal("navigator", {+ mediaDevices: { getUserMedia: vi.fn(async () => microphone) },+ });+ const controls = renderControls();++ controls.startOrReturn(mocks.bot as never);+ await flushVoiceStart();++ expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledWith({+ audio: {+ echoCancellation: true,+ noiseSuppression: true,+ autoGainControl: true,+ },+ video: false,+ });+ latestPeer?.events.onmessage?.(+ new MessageEvent("message", {+ data: JSON.stringify({ type: "output_audio_buffer.started" }),+ }),+ );+ expect(track.enabled).toBe(false);+ latestPeer?.events.onmessage?.(+ new MessageEvent("message", {+ data: JSON.stringify({ type: "response.done", response: { status: "completed" } }),+ }),+ );+ expect(track.enabled).toBe(false);+ latestPeer?.events.onmessage?.(+ new MessageEvent("message", {+ data: JSON.stringify({ type: "output_audio_buffer.stopped" }),+ }),+ );+ expect(track.enabled).toBe(true);+ });+});diff --git a/apps/web/src/state/environments.ts b/apps/web/src/state/environments.tsindex 443e99b84..7d9221a69 100644--- a/apps/web/src/state/environments.ts+++ b/apps/web/src/state/environments.ts@@ -86,6 +86,6 @@ export function useRelayEnvironmentDiscovery(): Discovery.RelayEnvironmentDiscovreturn useAtomValue(relayEnvironmentDiscovery.stateValueAtom);}-export function useEnvironmentConnectionState(environmentId: EnvironmentId) {- return useEnvironmentQuery(environmentCatalog.stateAtom(environmentId));+export function useEnvironmentConnectionState(environmentId: EnvironmentId | null) {+ return useEnvironmentQuery(environmentId ? environmentCatalog.stateAtom(environmentId) : null);}diff --git a/packages/client-runtime/src/state/server.test.ts b/packages/client-runtime/src/state/server.test.tsindex 8ee312f61..852f4f11b 100644--- a/packages/client-runtime/src/state/server.test.ts+++ b/packages/client-runtime/src/state/server.test.ts@@ -41,6 +41,7 @@ import {serverUpdateStateForProgressEvent,serverUpdateStateForServerVersion,validateServerUpdateReadyEvent,+ voiceCallHangupConcurrencyKey,} from "./server.ts";const CONFIG = {@@ -66,6 +67,13 @@ const TARGET = new PrimaryConnectionTarget({wsBaseUrl: "wss://environment.example.test",});+it("uses the call ID in hangup single-flight keys", () => {+ const environmentId = EnvironmentId.make("environment-1");+ expect(voiceCallHangupConcurrencyKey({ environmentId, input: { callId: "call-a" } })).not.toBe(+ voiceCallHangupConcurrencyKey({ environmentId, input: { callId: "call-b" } }),+ );+});+function session(client: WsRpcProtocolClient): RpcSession {return {client,diff --git a/packages/client-runtime/src/state/server.ts b/packages/client-runtime/src/state/server.tsindex 26f1e7a6f..c0270c5f9 100644--- a/packages/client-runtime/src/state/server.ts+++ b/packages/client-runtime/src/state/server.ts@@ -65,6 +65,13 @@ export interface ServerUpdateTarget {readonly input: EnvironmentRpcInput<typeof WS_METHODS.serverUpdateServer>;}+export function voiceCallHangupConcurrencyKey(input: {+ readonly environmentId: EnvironmentId;+ readonly input: EnvironmentRpcInput<typeof WS_METHODS.voiceCallHangup>;+}): string {+ return `${input.environmentId}:${input.input.callId}`;+}+const IDLE_SERVER_UPDATE_STATE: ServerUpdateState = { status: "idle" };const EMPTY_SERVER_UPDATE_STATE_ATOM = Atom.make<ServerUpdateState>(IDLE_SERVER_UPDATE_STATE).pipe(Atom.withLabel("environment-data:server:update-state:empty"),@@ -813,7 +820,7 @@ export function createServerEnvironmentAtoms<R, E>(tag: WS_METHODS.voiceCallHangup,concurrency: {mode: "singleFlight",- key: ({ environmentId }) => environmentId,+ key: voiceCallHangupConcurrencyKey,},}),signalProcess: createEnvironmentRpcCommand(runtime, {diff --git a/packages/contracts/src/orchestration.test.ts b/packages/contracts/src/orchestration.test.tsindex 4306b7f4c..7b393fa94 100644--- a/packages/contracts/src/orchestration.test.ts+++ b/packages/contracts/src/orchestration.test.ts@@ -2,6 +2,7 @@ import { assert, it } from "@effect/vitest";import * as Effect from "effect/Effect";import * as Schema from "effect/Schema";+import { MessageId } from "./baseSchemas.ts";import {BotAvatar,DEFAULT_PROVIDER_INTERACTION_MODE,@@ -929,6 +930,7 @@ it.effect("decodes latest turn source proposed plan metadata when present", () =const parsed = yield* decodeOrchestrationLatestTurn({turnId: "turn-2",state: "running",+ requestMessageId: "msg-2",requestedAt: "2026-01-01T00:00:00.000Z",startedAt: "2026-01-01T00:00:01.000Z",completedAt: null,@@ -942,6 +944,21 @@ it.effect("decodes latest turn source proposed plan metadata when present", () =threadId: "thread-1",planId: "plan-1",});+ assert.strictEqual(parsed.requestMessageId, MessageId.make("msg-2"));+ }),+);++it.effect("decodes historical latest turns without a request message id", () =>+ Effect.gen(function* () {+ const parsed = yield* decodeOrchestrationLatestTurn({+ turnId: "turn-2",+ state: "running",+ requestedAt: "2026-01-01T00:00:00.000Z",+ startedAt: null,+ completedAt: null,+ assistantMessageId: null,+ });+ assert.strictEqual(parsed.requestMessageId, undefined);}),);diff --git a/packages/contracts/src/orchestration.ts b/packages/contracts/src/orchestration.tsindex c857a7d49..8fe0e68a8 100644--- a/packages/contracts/src/orchestration.ts+++ b/packages/contracts/src/orchestration.ts@@ -469,6 +469,7 @@ export const OrchestrationLatestTurn = Schema.Struct({startedAt: Schema.NullOr(IsoDateTime),completedAt: Schema.NullOr(IsoDateTime),assistantMessageId: Schema.NullOr(MessageId),+ requestMessageId: Schema.optional(Schema.NullOr(MessageId)),respondingBotId: Schema.optional(Schema.NullOr(BotId)),sourceProposedPlan: Schema.optional(SourceProposedPlanReference),});diff --git a/packages/contracts/src/voiceCall.ts b/packages/contracts/src/voiceCall.tsindex c4f273f16..0a4eeecc0 100644--- a/packages/contracts/src/voiceCall.ts+++ b/packages/contracts/src/voiceCall.ts@@ -3,7 +3,7 @@ import * as Schema from "effect/Schema";import { BotId, IsoDateTime, TrimmedNonEmptyString } from "./baseSchemas.ts";-export const CHATGPT_REALTIME_VOICE_MODEL = "gpt-realtime-2";+export const CHATGPT_REALTIME_VOICE_MODEL = "gpt-realtime-2.1";export const CHATGPT_REALTIME_VOICES = ["alloy","ash",