Skip to content

Commit 3b92e84

Browse files
fix(ui): handle WebKit voice constraint errors (#132904)
Normalize Error-backed WebKit overconstraints at the browser media owner and keep user-facing microphone and camera failures actionable. Co-authored-by: roboclaw-bot <[email protected]> Co-authored-by: Tak Hoffman <[email protected]>
1 parent bcc0b5a commit 3b92e84

3 files changed

Lines changed: 108 additions & 38 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Control UI E2E coverage for legacy WebKit media errors.
2+
import { expect, it } from "vitest";
3+
import { installMockGateway } from "../test-helpers/control-ui-e2e.ts";
4+
import { captureComposerProof } from "./browser-talk-start-stop.fixtures.ts";
5+
import { createControlUiE2eSuite } from "./control-ui-e2e-suite.test-support.ts";
6+
7+
const suite = createControlUiE2eSuite({ name: "Control UI browser Talk WebKit errors" });
8+
9+
suite.define(() => {
10+
it("renders legacy WebKit overconstraints as actionable microphone guidance", async () => {
11+
await suite.withPage(undefined, async ({ page }) => {
12+
const gateway = await installMockGateway(page, {
13+
methodResponses: {
14+
"talk.client.create": {
15+
provider: "openai",
16+
transport: "gateway-relay",
17+
relaySessionId: "relay-webkit-overconstraint-e2e",
18+
audio: {
19+
inputEncoding: "pcm16",
20+
inputSampleRateHz: 16_000,
21+
outputEncoding: "pcm16",
22+
outputSampleRateHz: 24_000,
23+
},
24+
},
25+
"talk.session.close": {},
26+
},
27+
});
28+
await page.addInitScript(() => {
29+
Object.defineProperty(navigator, "mediaDevices", {
30+
configurable: true,
31+
value: {
32+
enumerateDevices: async () => [
33+
{ kind: "audioinput", deviceId: "usb", label: "USB Microphone" },
34+
],
35+
getUserMedia: async () => {
36+
throw Object.assign(new Error("Invalid constraint"), {
37+
name: "OverconstrainedError",
38+
constraint: "",
39+
});
40+
},
41+
},
42+
});
43+
});
44+
45+
await page.setViewportSize({ width: 320, height: 720 });
46+
await page.goto(`${suite.server.baseUrl}settings/appearance`);
47+
await page.locator("[data-settings-microphone]").selectOption("usb");
48+
await page.goto(`${suite.server.baseUrl}chat`);
49+
await page.getByRole("button", { name: "Tap to talk" }).click();
50+
await gateway.waitForRequest("talk.client.create");
51+
52+
await expect
53+
.poll(() => page.getByRole("alert").locator(".agent-chat__talk-status-text").textContent())
54+
.toBe("The selected microphone is unavailable. Choose another input or System default.");
55+
await expect
56+
.poll(() => gateway.getRequests("talk.session.close").then((requests) => requests.length))
57+
.toBe(1);
58+
await captureComposerProof(page, "webkit-selected-microphone-unavailable-composer.png");
59+
await page.getByRole("alert").screenshot({
60+
path: ".artifacts/control-ui-e2e/voice-controls/webkit-selected-microphone-alert.png",
61+
});
62+
});
63+
});
64+
});

‎ui/src/pages/chat/realtime-talk-input.test.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ function mediaDevice(kind: MediaDeviceKind, deviceId: string, label: string): Me
1212
return { kind, deviceId, label, groupId: "", toJSON: () => ({}) } as MediaDeviceInfo;
1313
}
1414

15+
function legacyWebKitOverconstrainedError(): Error & { constraint: string } {
16+
return Object.assign(new Error("Invalid constraint"), {
17+
name: "OverconstrainedError",
18+
constraint: "",
19+
});
20+
}
21+
1522
afterEach(() => {
1623
vi.unstubAllGlobals();
1724
});
@@ -143,9 +150,9 @@ describe("realtime Talk microphone inputs", () => {
143150
);
144151
});
145152

146-
it("does not silently fall back when the selected microphone is unavailable", async () => {
153+
it("explains a legacy WebKit overconstraint without silently falling back", async () => {
147154
const getUserMedia = vi.fn(async () => {
148-
throw new DOMException("missing", "OverconstrainedError");
155+
throw legacyWebKitOverconstrainedError();
149156
});
150157
vi.stubGlobal("navigator", { mediaDevices: { getUserMedia } });
151158

@@ -380,7 +387,7 @@ describe("realtime Talk camera inputs", () => {
380387

381388
it("does not silently fall back when the selected camera is unavailable", async () => {
382389
const getUserMedia = vi.fn(async () => {
383-
throw new DOMException("missing", "OverconstrainedError");
390+
throw legacyWebKitOverconstrainedError();
384391
});
385392
vi.stubGlobal("navigator", { mediaDevices: { getUserMedia } });
386393

‎ui/src/pages/chat/realtime-talk-input.ts‎

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,23 @@ function deviceDetailsHidden(devices: MediaDeviceInfo[], kind: RealtimeTalkDevic
7171
return inputs.length === 0 || inputs.some((device) => !device.deviceId || !device.label);
7272
}
7373

74-
const deviceIssueByDomErrorName: Record<string, RealtimeTalkDeviceIssue> = {
74+
const deviceIssueByMediaErrorName: Record<string, RealtimeTalkDeviceIssue> = {
7575
NotAllowedError: "permission-blocked",
7676
NotFoundError: "none-found",
7777
NotReadableError: "busy",
7878
InvalidStateError: "page-inactive",
7979
};
8080

81+
function mediaDeviceErrorName(error: unknown): string | undefined {
82+
// WebKit shipped OverconstrainedError as Error instead of DOMException.
83+
if (error instanceof DOMException) {
84+
return error.name;
85+
}
86+
return error instanceof Error && error.name === "OverconstrainedError" ? error.name : undefined;
87+
}
88+
8189
function deviceIssueFromError(error: unknown): RealtimeTalkDeviceIssue {
82-
return (
83-
(error instanceof DOMException ? deviceIssueByDomErrorName[error.name] : undefined) ?? "failed"
84-
);
90+
return deviceIssueByMediaErrorName[mediaDeviceErrorName(error) ?? ""] ?? "failed";
8591
}
8692

8793
export function realtimeTalkDeviceIssueMessage(
@@ -233,18 +239,16 @@ export async function openRealtimeTalkInput(
233239
),
234240
};
235241
} catch (error) {
236-
if (
237-
inputDeviceId?.trim() &&
238-
error instanceof DOMException &&
239-
error.name === "OverconstrainedError"
240-
) {
241-
throw new Error(t("chat.composer.selectedMicrophoneUnavailable"), { cause: error });
242-
}
243-
if (error instanceof DOMException && error.name !== "AbortError") {
244-
acquisition = { failure: describeRealtimeTalkInputError(error) };
245-
} else {
242+
const errorName = mediaDeviceErrorName(error);
243+
if (!errorName || errorName === "AbortError") {
246244
throw error;
247245
}
246+
acquisition = {
247+
failure:
248+
inputDeviceId?.trim() && errorName === "OverconstrainedError"
249+
? t("chat.composer.selectedMicrophoneUnavailable")
250+
: describeRealtimeTalkInputError(error),
251+
};
248252
}
249253
if ("failure" in acquisition) {
250254
throw new Error(acquisition.failure);
@@ -266,36 +270,31 @@ export async function openRealtimeTalkCamera(
266270
throw new Error(t("chat.composer.cameraAccessFailed"));
267271
}
268272
const deviceId = videoDeviceId?.trim();
269-
let camera: MediaStream;
273+
let acquisition: { stream: MediaStream } | { failure: string };
270274
try {
271-
camera = await awaitRealtimeTalkMediaRequest(
272-
() =>
273-
devices.getUserMedia({
274-
video: deviceId ? { deviceId: { exact: deviceId } } : true,
275-
}),
275+
const stream = await awaitRealtimeTalkMediaRequest(
276+
() => devices.getUserMedia({ video: deviceId ? { deviceId: { exact: deviceId } } : true }),
276277
options.signal,
277278
);
278279
if (options.signal?.aborted) {
279-
camera.getTracks().forEach((track) => track.stop());
280+
stream.getTracks().forEach((track) => track.stop());
280281
throw realtimeTalkAbortReason(options.signal);
281282
}
282-
return camera;
283+
acquisition = { stream };
283284
} catch (error) {
284285
if (options.signal?.aborted) {
285286
throw realtimeTalkAbortReason(options.signal);
286287
}
287-
if (deviceId && error instanceof DOMException && error.name === "OverconstrainedError") {
288-
throw new Error(t("chat.composer.selectedCameraUnavailable"), { cause: error });
289-
}
290-
if (error instanceof DOMException && error.name === "NotAllowedError") {
291-
throw new Error(t("chat.composer.cameraPermissionBlocked"), { cause: error });
292-
}
293-
if (error instanceof DOMException && error.name === "NotFoundError") {
294-
throw new Error(t("chat.composer.cameraNoneFound"), { cause: error });
295-
}
296-
if (error instanceof DOMException && error.name === "NotReadableError") {
297-
throw new Error(t("chat.composer.cameraBusy"), { cause: error });
298-
}
299-
throw new Error(t("chat.composer.cameraAccessFailed"), { cause: error });
288+
const errorName = mediaDeviceErrorName(error);
289+
acquisition = {
290+
failure:
291+
deviceId && errorName === "OverconstrainedError"
292+
? t("chat.composer.selectedCameraUnavailable")
293+
: realtimeTalkDeviceIssueMessage(deviceIssueFromError(error), "videoinput"),
294+
};
295+
}
296+
if ("failure" in acquisition) {
297+
throw new Error(acquisition.failure);
300298
}
299+
return acquisition.stream;
301300
}

0 commit comments

Comments
 (0)