Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Live: let a phone app that runs Gemini's session itself start a Gemini call (#7493)

Bugfixer 27ccddd315514
diff --git a/server/live-call-controller.test.ts b/server/live-call-controller.test.ts
index 3db5fbf4b9..c1c4c0262d 100644
--- a/server/live-call-controller.test.ts
+++ b/server/live-call-controller.test.ts
@@ -596,6 +596,15 @@ describe("LiveCallController lifecycle", () => {
}
});
+ it("lets a phone app that runs Gemini's session itself start the call", async () => {
+ for (const client of ["ios", "android"] as const) {
+ const t = gemini();
+ const started = await t.controller.start({ auth: owner, ...BOT, client, relays: ["gemini"] });
+ expect(started.gemini?.type).toBe("gemini");
+ expect(t.deps.createSession).toHaveBeenCalledTimes(1);
+ }
+ });
+
it("never calls Gemini with a key saved for another provider", async () => {
const t = gemini();
t.settings.endpoint = { provider: "fish", apiBase: "https://api.fish.audio/v1/agent", model: "agent_1" };
diff --git a/server/live-call-controller.ts b/server/live-call-controller.ts
index 709753b21f..520a06ebae 100644
--- a/server/live-call-controller.ts
+++ b/server/live-call-controller.ts
@@ -50,6 +50,8 @@ export type LiveCallStarted =
| { call: LiveCallState; sdp?: undefined; fish?: undefined; gemini: LiveGeminiTransport; elevenlabs?: undefined }
| { call: LiveCallState; sdp?: undefined; fish?: undefined; gemini?: undefined; elevenlabs: LiveElevenLabsTransport };
export const LIVE_FISH_NEEDS_APP = "Live calls through Fish Audio work in the desktop app or a web browser, not in the phone app yet.";
+/** The relayed sessions a phone app can run itself, as it names them when it starts a call. */
+export type LiveRelayClient = "gemini";
export const LIVE_GEMINI_NEEDS_APP = "Live calls through Gemini work in the desktop app or a web browser, not in the phone app yet.";
export const LIVE_ELEVENLABS_NEEDS_APP = "Live calls through ElevenLabs work in the desktop app or a web browser, not in the phone app yet.";
@@ -184,7 +186,7 @@ export class LiveCallController {
return this.call && this.call.state.status !== "ended" ? { ...this.call.state } : null;
}
- async start(input: { auth: RequestAuth; device?: string; botId: string; botName: string; threadId: string; client: LiveClient; sdp?: string }): Promise<LiveCallStarted> {
+ async start(input: { auth: RequestAuth; device?: string; botId: string; botName: string; threadId: string; client: LiveClient; sdp?: string; relays?: readonly LiveRelayClient[] }): Promise<LiveCallStarted> {
const settings = this.deps.settings();
// First: someone refused here is not told about a running call, or sent to set Azure or a relayed service up.
// The chosen relayed service comes before a key's endpoint, so its refusal and its mismatch check apply.
@@ -203,7 +205,8 @@ export class LiveCallController {
}
}
const relayed = limited === "azure" || limited === "custom" ? null : limited;
- if (relayed && (input.client === "ios" || input.client === "android")) throw new LiveSessionError(relayed === "fish" ? LIVE_FISH_NEEDS_APP : relayed === "gemini" ? LIVE_GEMINI_NEEDS_APP : LIVE_ELEVENLABS_NEEDS_APP, 400);
+ // A phone app runs a relayed session only when it says it can (`relays`); older phones are sent to the desktop app.
+ if (relayed && (input.client === "ios" || input.client === "android") && !(relayed === "gemini" && input.relays?.includes("gemini"))) throw new LiveSessionError(relayed === "fish" ? LIVE_FISH_NEEDS_APP : relayed === "gemini" ? LIVE_GEMINI_NEEDS_APP : LIVE_ELEVENLABS_NEEDS_APP, 400);
if (!relayed && !input.sdp) throw new LiveSessionError("The call request was not valid.", 400);
if (this.call && this.call.state.status !== "ended") throw new LiveCallBusyError({ ...this.call.state });
if (input.device && this.revokedDevices.has(input.device)) throw new LiveCallSignedOutError();
diff --git a/server/routes/live.test.ts b/server/routes/live.test.ts
index 042a10d291..1d1f948b6b 100644
--- a/server/routes/live.test.ts
+++ b/server/routes/live.test.ts
@@ -85,6 +85,13 @@ describe("live routes", () => {
expect((await request(d, "POST", "/api/live/session", { botId: "bot1", sdp: "v=0\r\n", client: "web" })).status).toBe(201);
expect(d.calls.start).toHaveBeenCalledWith(expect.objectContaining({ client: "web" }));
});
+ it("passes on the relayed sessions a phone app runs itself, and nothing else", async () => {
+ const d = deps();
+ expect((await request(d, "POST", "/api/live/session", { botId: "bot1", relays: ["gemini"], client: "android" })).status).toBe(201);
+ expect(d.calls.start).toHaveBeenCalledWith(expect.objectContaining({ client: "android", relays: ["gemini"] }));
+ expect(d.calls.start).not.toHaveBeenCalledWith(expect.objectContaining({ sdp: expect.anything() }));
+ expect((await request(deps(), "POST", "/api/live/session", { botId: "bot1", relays: ["fish"], client: "android" })).status).toBe(400);
+ });
it("keeps the SDP byte for byte", async () => {
const d = deps();
await request(d, "POST", "/api/live/session", { botId: "bot1", sdp: "v=0\r\na=x \r\n", client: "desktop" });
diff --git a/server/routes/live.ts b/server/routes/live.ts
index 5a00df5aac..c538333d7d 100644
--- a/server/routes/live.ts
+++ b/server/routes/live.ts
@@ -26,6 +26,8 @@ const sessionBody = z.object({
// Not trimmed: the SDP offer goes to OpenAI byte for byte. Absent on a relayed call, which has no offer.
sdp: z.string().min(1).refine((sdp) => Buffer.byteLength(sdp) <= MAX_SDP_BYTES).optional(),
client: z.enum(["desktop", "web", "ios", "android"]),
+ // The relayed sessions a phone app runs itself (Gemini Live); a phone that sends none keeps being sent to the desktop app.
+ relays: z.array(z.enum(["gemini"])).max(4).optional(),
}).strict();
const endBody = z.object({ callId: z.string().min(1).max(120) }).strict();
const relayMs = z.number().finite().min(0).max(86_400_000);
@@ -88,7 +90,8 @@ export function createLiveRoutes(deps: LiveRouteDeps): RouteHandler {
if (!target) return json(res, 404, { error: "That bot or chat does not exist." });
try {
const device = companionDevice(req);
- const started = await deps.calls.start({ auth, ...(device ? { device } : {}), ...target, client: parsed.data.client, ...(parsed.data.sdp ? { sdp: parsed.data.sdp } : {}) });
+ const started = await deps.calls.start({ auth, ...(device ? { device } : {}), ...target, client: parsed.data.client, ...(parsed.data.sdp ? { sdp: parsed.data.sdp } : {}),
+ ...(parsed.data.relays ? { relays: parsed.data.relays } : {}) });
return json(res, 201, { call: started.call, transport: started.fish ?? started.gemini ?? started.elevenlabs ?? { type: "webrtc", sdp: started.sdp } });
} catch (error) {
if (error instanceof LiveCallBusyError) return json(res, 409, { error: error.message, activeCall: error.call });