Skip to content

Commit 2bf0203

Browse files
committed
fix(voice): preserve microphone consent and loaded provider sessions
1 parent 371a609 commit 2bf0203

11 files changed

Lines changed: 47 additions & 19 deletions

File tree

‎apps/desktop/src/window/MainWindowPermissions.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ describe("decideMainWindowMediaRequest", () => {
3838
expect(decide({ microphoneStatus: () => "not-determined" })).toBe("ask-microphone");
3939
expect(decide({ microphoneStatus: () => "denied" })).toBe("ask-microphone");
4040
expect(decide()).toBe("grant");
41+
expect(
42+
isMainWindowPermissionAllowed("media", APP_URL, APP_URL, {
43+
platform: "darwin",
44+
microphoneStatus: () => "not-determined",
45+
}),
46+
).toBe(false);
47+
expect(
48+
isMainWindowPermissionAllowed("media", APP_URL, APP_URL, {
49+
platform: "darwin",
50+
microphoneStatus: () => "granted",
51+
}),
52+
).toBe(true);
4153
});
4254

4355
it("grants the microphone directly off macOS", () => {

‎apps/desktop/src/window/MainWindowPermissions.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ export function isMainWindowPermissionAllowed(
3232
permission: string,
3333
origin: string | undefined,
3434
appUrl: string,
35+
media?: { readonly platform: NodeJS.Platform; readonly microphoneStatus: () => string },
3536
): boolean {
3637
return permission === "media"
37-
? isAppUrl(origin, appUrl)
38+
? isAppUrl(origin, appUrl) &&
39+
(media?.platform !== "darwin" || media.microphoneStatus() !== "not-determined")
3840
: permission !== "deprecated-sync-clipboard-read";
3941
}
4042

@@ -67,6 +69,9 @@ export function installMainWindowPermissionHandlers(
6769
});
6870
});
6971
session.setPermissionCheckHandler((_webContents, permission, requestingOrigin) =>
70-
isMainWindowPermissionAllowed(permission, requestingOrigin, options.appUrl),
72+
isMainWindowPermissionAllowed(permission, requestingOrigin, options.appUrl, {
73+
platform: options.platform,
74+
microphoneStatus: () => Electron.systemPreferences.getMediaAccessStatus("microphone"),
75+
}),
7176
);
7277
}

‎apps/mobile/src/features/voice-mode/ComposerVoiceMode.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export function ComposerVoiceStrip() {
108108

109109
// Levels arrive at 10Hz; they drive shared values directly, and the
110110
// speaking flag only re-renders when it flips.
111-
// oxlint-disable-next-line t3code/react-effect-budget -- Subscribe native meter animations to the audio level stream.
111+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Subscribe native meter animations to the audio level stream.
112112
useEffect(() => {
113113
const isSpeaking = createSpeakingDetector();
114114
const unsubscribe = voiceMode.subscribeLevels((levels) => {

‎apps/mobile/src/features/voice-mode/useVoiceMode.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ export function useVoiceModeThreadLifecycle(target: VoiceSessionTarget): void {
3939
const params = route?.params;
4040
return params && "threadId" in params ? String(params.threadId) : null;
4141
});
42-
// oxlint-disable-next-line t3code/react-effect-budget -- Synchronize the WebRTC lifetime with the visible native thread.
42+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Synchronize the WebRTC lifetime with the visible native thread.
4343
useEffect(() => {
4444
if (visibleThreadId !== threadId && voiceMode.isActiveFor({ environmentId, threadId }))
4545
voiceMode.stop();
4646
}, [environmentId, threadId, visibleThreadId]);
4747

48-
// oxlint-disable-next-line t3code/react-effect-budget -- Release the native call when its owning thread screen unmounts.
48+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Release the native call when its owning thread screen unmounts.
4949
useEffect(
5050
() => () => {
5151
if (
@@ -57,7 +57,7 @@ export function useVoiceModeThreadLifecycle(target: VoiceSessionTarget): void {
5757
[environmentId, threadId, routeName],
5858
);
5959

60-
// oxlint-disable-next-line t3code/react-effect-budget -- Subscribe to call lifecycle failures and remote endings.
60+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Subscribe to call lifecycle failures and remote endings.
6161
useEffect(() => {
6262
// Reads the live state: dismissing notifies listeners re-entrantly, so a
6363
// listener's argument can already be stale.

‎apps/server/src/keybindings.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
355355
);
356356
});
357357

358-
it.effect.each([undefined, "(!terminalFocus)"])(
358+
it.effect.each([undefined, "(!terminalFocus)", "composerFocus"])(
359359
"preserves a user voice shortcut with condition %s",
360360
(when) =>
361361
Effect.gen(function* () {
@@ -374,7 +374,10 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
374374
});
375375

376376
const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
377-
assert.isFalse(persisted.some((entry) => entry.command === "voice.toggle"));
377+
assert.equal(
378+
persisted.some((entry) => entry.command === "voice.toggle"),
379+
when === "composerFocus",
380+
);
378381
assert.isTrue(persisted.some((entry) => entry.command === "navigation.back"));
379382
}).pipe(Effect.provide(layerKeybindings())),
380383
);

‎apps/server/src/keybindings.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,15 @@ function encodedRuleShortcut(rule: KeybindingRule): string | null {
114114
return parsed ? encodeShortcut(parsed) : null;
115115
}
116116

117-
/**
118-
* Whether appending `rule` after `existing` would shadow it: same shortcut, and
119-
* either the same `when` or one of them applies in every context.
120-
*/
117+
// Persisted custom rules follow defaults, so a conditional override leaves the default available elsewhere.
121118
function shadowsShortcut(existing: KeybindingRule, rule: KeybindingRule): boolean {
122119
const existingShortcut = encodedRuleShortcut(existing);
123120
if (!existingShortcut || existingShortcut !== encodedRuleShortcut(rule)) return false;
121+
if (existing.when === undefined) return true;
122+
if (rule.when === undefined) return false;
124123
return (
125-
existing.when === undefined ||
126-
rule.when === undefined ||
127124
JSON.stringify(parseKeybindingWhenExpression(existing.when)) ===
128-
JSON.stringify(parseKeybindingWhenExpression(rule.when))
125+
JSON.stringify(parseKeybindingWhenExpression(rule.when))
129126
);
130127
}
131128

‎apps/server/src/orchestration-v2/ProviderSessionManager.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3683,6 +3683,7 @@ it.effect(
36833683
.startVoiceSession({ threadId, offerSdp: "offer" })
36843684
.pipe(Stream.runDrain, Effect.forkChild);
36853685
yield* Deferred.await(started);
3686+
assert.equal((yield* Ref.get(state)).resumeCount, 1);
36863687
yield* TestClock.adjust("2 seconds");
36873688
assert.isTrue(Option.isSome(yield* manager.get(providerSessionId)));
36883689
assert.equal((yield* Ref.get(state)).closeCount, 0);

‎apps/server/src/orchestration-v2/ProviderSessionManager.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,15 +2068,25 @@ export const layerWithOptions = (
20682068
() =>
20692069
markIdle(providerSessionId, providerThread.id, voiceOrdinal).pipe(Effect.ignore),
20702070
);
2071-
return runtime.startVoiceSession({ providerThread, offerSdp: input.offerSdp });
2071+
const loadedThread = yield* runtime.resumeThread({
2072+
providerThread,
2073+
threadId: input.threadId,
2074+
});
2075+
return runtime.startVoiceSession({
2076+
providerThread: loadedThread,
2077+
offerSdp: input.offerSdp,
2078+
});
20722079
}),
20732080
).pipe(
20742081
Stream.mapError((cause) =>
20752082
cause._tag === "ProviderVoiceSessionError"
20762083
? cause
20772084
: new ProviderVoiceSessionError({
20782085
threadId: input.threadId,
2079-
detail: cause.message,
2086+
detail:
2087+
cause._tag === "ProviderAdapterProtocolError"
2088+
? cause.detail
2089+
: "The voice conversation could not continue.",
20802090
cause,
20812091
}),
20822092
),

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
11081108
};
11091109
}, []);
11101110

1111+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- binds voice shortcuts to window keyboard events for the mounted composer
11111112
useEffect(() => {
11121113
const handler = (event: globalThis.KeyboardEvent) => {
11131114
const command = resolveShortcutCommand(event, keybindings, {

‎apps/web/src/components/chat/VoiceModeStrip.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export const VoiceModeStrip = memo(function VoiceModeStrip({
8484
const speakerMeterRef = useRef<HTMLSpanElement>(null);
8585
const [speaking, setSpeaking] = useState(false);
8686

87-
// oxlint-disable-next-line t3code/react-effect-budget -- Subscribe visible meters to the audio level stream.
87+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Subscribe visible meters to the audio level stream.
8888
useEffect(() => {
8989
if (!isActive) return;
9090
// Teardown publishes a silent sample, which clears `speaking` for the next conversation.

0 commit comments

Comments
 (0)