Skip to content

Commit 378e011

Browse files
committed
fix(ui): keep unpersisted settings in memory and unwedge mic discovery errors
1 parent 91db202 commit 378e011

4 files changed

Lines changed: 71 additions & 6 deletions

File tree

‎ui/src/app/settings.node.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
loadLocalUserIdentity,
77
loadSettings,
88
persistSessionToken,
9+
resetUnpersistedSettingsForTest,
910
resolvePageGatewaySettings,
1011
resolveApplicationStartupSettings,
1112
saveSettings,
@@ -106,6 +107,7 @@ describe("resolveApplicationStartupSettings", () => {
106107

107108
describe("loadSettings default gateway URL derivation", () => {
108109
beforeEach(() => {
110+
resetUnpersistedSettingsForTest();
109111
vi.stubGlobal("localStorage", createStorageMock());
110112
vi.stubGlobal("sessionStorage", createStorageMock());
111113
vi.stubGlobal("navigator", { language: "en-US" } as Navigator);
@@ -115,6 +117,7 @@ describe("loadSettings default gateway URL derivation", () => {
115117
});
116118

117119
afterEach(() => {
120+
resetUnpersistedSettingsForTest();
118121
vi.restoreAllMocks();
119122
setControlUiBasePath(undefined);
120123
vi.unstubAllGlobals();
@@ -430,6 +433,27 @@ describe("loadSettings default gateway URL derivation", () => {
430433
expect(loadSettings().textScale).toBe(125);
431434
});
432435

436+
it("keeps the last written settings in memory when persistence fails", () => {
437+
setTestLocation({
438+
protocol: "https:",
439+
host: "gateway.example:8443",
440+
pathname: "/",
441+
});
442+
443+
const setItem = vi.spyOn(localStorage, "setItem").mockImplementation(() => {
444+
throw new DOMException("blocked", "SecurityError");
445+
});
446+
saveSettings({ ...loadSettings(), realtimeTalkInputDeviceId: "usb-mic" });
447+
448+
// Same-tab reads (e.g. a talk session launched from chat) must observe
449+
// the selection even though localStorage rejected the write.
450+
expect(loadSettings().realtimeTalkInputDeviceId).toBe("usb-mic");
451+
452+
setItem.mockRestore();
453+
saveSettings({ ...loadSettings(), realtimeTalkInputDeviceId: undefined });
454+
expect(loadSettings().realtimeTalkInputDeviceId).toBeUndefined();
455+
});
456+
433457
it("persists only the non-default chat send shortcut", () => {
434458
setTestLocation({
435459
protocol: "https:",

‎ui/src/app/settings.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,22 @@ export function persistSessionToken(gatewayUrl: string, token: string) {
490490
}
491491
}
492492

493+
// Last write that never reached localStorage (private mode, quota, security
494+
// errors). Without it a setting picked on one page silently reverts when
495+
// another page re-reads storage in the same tab.
496+
let unpersistedSettings: UiSettings | null = null;
497+
498+
export function resetUnpersistedSettingsForTest() {
499+
unpersistedSettings = null;
500+
}
501+
493502
export function loadSettings(): UiSettings {
503+
if (unpersistedSettings) {
504+
return {
505+
...unpersistedSettings,
506+
token: loadSessionToken(unpersistedSettings.gatewayUrl),
507+
};
508+
}
494509
const { pageUrl: pageDerivedUrl, effectiveUrl: defaultUrl } = deriveDefaultGatewayUrl();
495510
const storage = getSafeLocalStorage();
496511

@@ -683,6 +698,7 @@ function persistSettings(next: UiSettings, options: { selectGateway?: boolean }
683698
...(next.lobsterPetSounds === true ? { lobsterPetSounds: true } : {}),
684699
};
685700
const serialized = JSON.stringify(persisted);
701+
unpersistedSettings = next;
686702
try {
687703
const { pageUrl } = deriveDefaultGatewayUrl();
688704
const selectionKey = currentGatewaySelectionKeyForPage(pageUrl);
@@ -691,8 +707,12 @@ function persistSettings(next: UiSettings, options: { selectGateway?: boolean }
691707
storage?.setItem(selectionKey, next.gatewayUrl);
692708
}
693709
storage?.removeItem(LEGACY_SETTINGS_KEY);
710+
if (storage) {
711+
unpersistedSettings = null;
712+
}
694713
} catch {
695714
// best-effort — quota exceeded or security restrictions should not
696-
// prevent in-memory settings and visual updates from being applied
715+
// prevent in-memory settings and visual updates from being applied;
716+
// unpersistedSettings keeps this tab consistent until storage recovers
697717
}
698718
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @vitest-environment jsdom
22
import { afterEach, beforeEach, describe, expect, it, vi, type MockInstance } from "vitest";
3-
import { loadSettings, saveSettings } from "../../app/settings.ts";
3+
import { loadSettings, resetUnpersistedSettingsForTest, saveSettings } from "../../app/settings.ts";
44
import {
55
attachChatRealtimeActions,
66
createInitialChatRealtimeState,
@@ -42,13 +42,15 @@ describe("chat realtime actions", () => {
4242
let startSpy: MockInstance<RealtimeTalkSession["start"]>;
4343

4444
beforeEach(() => {
45+
resetUnpersistedSettingsForTest();
4546
vi.stubGlobal("localStorage", window.localStorage);
4647
localStorage.clear();
4748
startSpy = vi.spyOn(RealtimeTalkSession.prototype, "start").mockResolvedValue(undefined);
4849
vi.spyOn(RealtimeTalkSession.prototype, "stop").mockImplementation(() => undefined);
4950
});
5051

5152
afterEach(() => {
53+
resetUnpersistedSettingsForTest();
5254
vi.restoreAllMocks();
5355
vi.unstubAllGlobals();
5456
});
@@ -77,6 +79,18 @@ describe("chat realtime actions", () => {
7779
expect(inspectSession(state).localOptions.inputDeviceId).toBe("usb-mic");
7880
});
7981

82+
it("keeps a microphone picked while storage is blocked for the next launch", async () => {
83+
vi.spyOn(Storage.prototype, "setItem").mockImplementation(() => {
84+
throw new DOMException("blocked", "SecurityError");
85+
});
86+
saveSettings({ ...loadSettings(), realtimeTalkInputDeviceId: "usb-mic" });
87+
const state = createState();
88+
89+
await state.toggleRealtimeTalk();
90+
91+
expect(inspectSession(state).localOptions.inputDeviceId).toBe("usb-mic");
92+
});
93+
8094
it("propagates normalized microphone levels and resets them on error", async () => {
8195
const state = createState();
8296
await state.toggleRealtimeTalk();

‎ui/src/pages/config/config-page.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,17 @@ export class ConfigPage extends OpenClawLightDomElement {
367367
private async refreshMicrophones(requestPermission: boolean) {
368368
this.microphoneLoading = true;
369369
this.microphoneError = null;
370-
const result = await discoverRealtimeTalkInputs(requestPermission);
371-
this.microphoneDevices = result.devices;
372-
this.microphoneError = result.warning;
373-
this.microphoneLoading = false;
370+
try {
371+
const result = await discoverRealtimeTalkInputs(requestPermission);
372+
this.microphoneDevices = result.devices;
373+
this.microphoneError = result.warning;
374+
} catch (error) {
375+
// Discovery is best-effort in blocked/inactive contexts; a rejection
376+
// must not wedge the picker in its loading state.
377+
this.microphoneError = error instanceof Error ? error.message : String(error);
378+
} finally {
379+
this.microphoneLoading = false;
380+
}
374381
}
375382

376383
private syncRouteData() {

0 commit comments

Comments
 (0)