Skip to content

Commit a5dcca5

Browse files
committed
fix: address open PR review feedback after rebase
1 parent e6fb333 commit a5dcca5

22 files changed

Lines changed: 287 additions & 136 deletions

‎apps/mobile/src/features/threads/ThreadComposer.tsx‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
483483
onUsageLimits:
484484
usageLimitsOffered && draftAttachments.length === 0 ? openUsageLimits : undefined,
485485
});
486-
const voiceTarget = useMemo(
487-
() => ({ environmentId: props.environmentId, threadId: props.selectedThread.id }),
488-
[props.environmentId, props.selectedThread.id],
489-
);
486+
const voiceTarget = { environmentId: props.environmentId, threadId: props.selectedThread.id };
490487
const voicePhase = useVoiceModePhase(voiceTarget);
491488
useVoiceModeThreadLifecycle(voiceTarget);
492489
// Dictation and a voice conversation both need the microphone.
@@ -502,15 +499,18 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
502499
onChangeSelection: composerMenu.onSelectionChange,
503500
});
504501
const offersVoiceConversation =
505-
voiceModeSupported && selectedProviderStatus?.supportsVoice === true;
502+
voiceModeSupported &&
503+
props.serverConfig?.providers.find(
504+
(provider) => provider.instanceId === props.selectedThread.providerInstanceId,
505+
)?.supportsVoice === true;
506506
// The server attaches voice to the thread's provider thread, which the first
507507
// run creates. An optimistic pending thread already has a message timestamp.
508508
const canToggleVoiceConversation =
509509
voiceConversationLive ||
510510
(props.connectionState === "connected" &&
511511
props.selectedThread.activeProviderThreadId !== null &&
512-
!voiceInput.isBusy);
513-
const toggleVoiceConversation = useCallback(() => voiceMode.toggle(voiceTarget), [voiceTarget]);
512+
!voiceInput.isGloballyBusy);
513+
const toggleVoiceConversation = () => voiceMode.toggle(voiceTarget);
514514
const voiceModeButton = offersVoiceConversation ? (
515515
<ComposerVoiceModeButton
516516
phase={voicePhase}

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -620,10 +620,7 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
620620
const activeUserInputRequestId = props.activePendingUserInput?.requestId ?? null;
621621
const composerSlotHidden =
622622
activeUserInputRequestId !== null || props.creationState?.kind === "failed";
623-
const voiceTarget = useMemo(
624-
() => ({ environmentId: props.environmentId, threadId: props.selectedThread.id }),
625-
[props.environmentId, props.selectedThread.id],
626-
);
623+
const voiceTarget = { environmentId: props.environmentId, threadId: props.selectedThread.id };
627624
const voiceLiveHere = useVoiceModePhase(voiceTarget) !== "idle";
628625
// The open /usage-limits panel for this thread, model and turn. Only the open
629626
// moment is stored: the rows read live provider data, so a redeemed reset
@@ -712,30 +709,27 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
712709
// Android lays the screen out below its opaque header and reports a zero
713710
// header height, so the window height overstates the room by the header.
714711
const [screenHeight, setScreenHeight] = useState<number | null>(null);
715-
const handleScreenLayout = useCallback((event: LayoutChangeEvent) => {
712+
const handleScreenLayout = (event: LayoutChangeEvent) => {
716713
setScreenHeight(event.nativeEvent.layout.height);
717-
}, []);
714+
};
718715
const screenAreaHeight = screenHeight ?? windowHeight;
719716
// The usage panel stacks with the composer and any approval card, so it gets
720717
// whatever room they leave below the header. Measured only while it is open
721718
// so typing in the composer does not re-render the screen.
722719
const usageLimitsOpen = usageLimitsReport !== null && activeUserInputRequestId === null;
723720
const [usageLimitsLayout, setUsageLimitsLayout] = useState({ overlay: 0, panel: 0 });
724-
const handleComposerOverlayLayout = useCallback(
725-
(event: LayoutChangeEvent) => {
726-
onComposerLayout(event);
727-
if (!usageLimitsOpen) return;
728-
const overlay = event.nativeEvent.layout.height;
729-
setUsageLimitsLayout((current) =>
730-
current.overlay === overlay ? current : { ...current, overlay },
731-
);
732-
},
733-
[onComposerLayout, usageLimitsOpen],
734-
);
735-
const handleUsageLimitsLayout = useCallback((event: LayoutChangeEvent) => {
721+
const handleComposerOverlayLayout = (event: LayoutChangeEvent) => {
722+
onComposerLayout(event);
723+
if (!usageLimitsOpen) return;
724+
const overlay = event.nativeEvent.layout.height;
725+
setUsageLimitsLayout((current) =>
726+
current.overlay === overlay ? current : { ...current, overlay },
727+
);
728+
};
729+
const handleUsageLimitsLayout = (event: LayoutChangeEvent) => {
736730
const panel = event.nativeEvent.layout.height;
737731
setUsageLimitsLayout((current) => (current.panel === panel ? current : { ...current, panel }));
738-
}, []);
732+
};
739733
const overlayBesideUsageLimits =
740734
usageLimitsLayout.overlay > 0
741735
? usageLimitsLayout.overlay - usageLimitsLayout.panel
@@ -1369,10 +1363,10 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
13691363
) : null}
13701364
</View>
13711365

1372-
{/* The composer below hides for a question card; keep a live
1366+
{/* The composer below hides for a question card; keep a live
13731367
voice conversation's mute and stop controls reachable. */}
1374-
{activeUserInputRequestId !== null && voiceLiveHere ? <ComposerVoiceStrip /> : null}
1375-
{/* Hidden (not unmounted) while a user-input request owns the
1368+
{activeUserInputRequestId !== null && voiceLiveHere ? <ComposerVoiceStrip /> : null}
1369+
{/* Hidden (not unmounted) while a user-input request owns the
13761370
composer slot, so composer drafts and editor state survive.
13771371
A rejected creation has no thread to send to; the failure card
13781372
owns the slot instead. */}

‎apps/mobile/src/features/voice-input/useVoiceInputController.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export function useVoiceInputController(input: {
6969
audioLevels: global.audioLevels,
7070
elapsedSeconds: global.elapsedSeconds,
7171
isBusy,
72+
isGloballyBusy: global.isBusy,
7273
freezesEditor: isBusy,
7374
blocksSubmission: isBusy,
7475
start,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import Animated, {
99
type SharedValue,
1010
} from "react-native-reanimated";
1111

12-
import { AppText as Text } from "../../components/AppText";
13-
import { SymbolView, type AppSymbolName } from "../../components/AppSymbol";
12+
import { AppText as Text } from "../../components/ui/AppText";
13+
import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol";
1414
import { cn } from "../../lib/cn";
1515
import { useVoiceModeState } from "./useVoiceMode";
1616
import { voiceMode } from "./voiceMode";

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
} from "@t3tools/client-runtime/voice-mode";
66
import { useCallback, useEffect, useSyncExternalStore } from "react";
77
import { Alert } from "react-native";
8+
import { useNavigationState } from "@react-navigation/native";
89

910
import { voiceMode } from "./voiceMode";
1011
import { resolveVoiceModeNotice } from "./voiceModePresentation";
@@ -32,6 +33,15 @@ export function useVoiceModePhase(target: VoiceSessionTarget): VoiceModePhase {
3233
*/
3334
export function useVoiceModeThreadLifecycle(target: VoiceSessionTarget): void {
3435
const { environmentId, threadId } = target;
36+
const visibleThreadId = useNavigationState((state) => {
37+
const route = state.routes.findLast((route) => route.name === "Thread");
38+
const params = route?.params;
39+
return params && "threadId" in params ? String(params.threadId) : null;
40+
});
41+
useEffect(() => {
42+
if (visibleThreadId !== threadId && voiceMode.isActiveFor({ environmentId, threadId }))
43+
voiceMode.stop();
44+
}, [environmentId, threadId, visibleThreadId]);
3545

3646
useEffect(
3747
() => () => {

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import type { EnvironmentRegistry } from "@t3tools/client-runtime/connection";
22
import {
33
VoiceModeController,
44
makeVoiceSessionOpener,
5+
makeVoiceSessionValidator,
56
type VoiceModePhase,
67
type VoicePeerConnection,
78
} from "@t3tools/client-runtime/voice-mode";
89
import * as Effect from "effect/Effect";
910
import { AtomRegistry } from "effect/reactivity";
10-
import { setAudioModeAsync, setIsAudioActiveAsync } from "expo-audio";
11+
import { setAudioModeAsync } from "expo-audio";
1112
import { activateKeepAwakeAsync, deactivateKeepAwake } from "expo-keep-awake";
1213
import { AppState, NativeModules } from "react-native";
1314
import type * as WebRTC from "react-native-webrtc";
@@ -51,6 +52,7 @@ function createPeerConnection(): VoicePeerConnection {
5152
}
5253

5354
export const voiceMode = new VoiceModeController({
55+
validateTarget: makeVoiceSessionValidator(runOnConnectionRuntime),
5456
createPeerConnection,
5557
getMicrophone: () => loadWebRTC().mediaDevices.getUserMedia({ audio: true }),
5658
// Native WebRTC plays remote audio tracks through its own audio device; there is nothing to attach.
@@ -77,11 +79,7 @@ async function routeVoiceToSpeaker(): Promise<void> {
7779

7880
/** Mirrors dictation's release so interrupted audio in other apps resumes. */
7981
async function releaseVoiceAudio(): Promise<void> {
80-
try {
81-
await setAudioModeAsync({ allowsRecording: false });
82-
} finally {
83-
await setIsAudioActiveAsync(false);
84-
}
82+
await setAudioModeAsync({ allowsRecording: false });
8583
}
8684

8785
const KEEP_AWAKE_TAG = "voice-mode";

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -398,22 +398,28 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
398398
);
399399
});
400400

401-
it.effect("skips a conditional default that would shadow an unconditional user shortcut", () =>
402-
Effect.gen(function* () {
403-
const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
404-
yield* writeKeybindingsConfig(keybindingsConfigPath, [
405-
{ key: "f8", command: "script.custom-action.run" },
406-
]);
401+
it.effect.each([undefined, "(!terminalFocus)"])(
402+
"preserves a user voice shortcut with condition %s",
403+
(when) =>
404+
Effect.gen(function* () {
405+
const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
406+
yield* writeKeybindingsConfig(keybindingsConfigPath, [
407+
{
408+
key: "f8",
409+
command: "script.custom-action.run",
410+
...(when === undefined ? {} : { when }),
411+
},
412+
]);
407413

408-
yield* Effect.gen(function* () {
409-
const keybindings = yield* Keybindings.Keybindings;
410-
yield* keybindings.syncDefaultKeybindingsOnStartup;
411-
});
414+
yield* Effect.gen(function* () {
415+
const keybindings = yield* Keybindings.Keybindings;
416+
yield* keybindings.syncDefaultKeybindingsOnStartup;
417+
});
412418

413-
const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
414-
assert.isFalse(persisted.some((entry) => entry.command === "voice.toggle"));
415-
assert.isTrue(persisted.some((entry) => entry.command === "navigation.back"));
416-
}).pipe(Effect.provide(layerKeybindings())),
419+
const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);
420+
assert.isFalse(persisted.some((entry) => entry.command === "voice.toggle"));
421+
assert.isTrue(persisted.some((entry) => entry.command === "navigation.back"));
422+
}).pipe(Effect.provide(layerKeybindings())),
417423
);
418424

419425
it.effect("upserts custom keybindings to configured path", () =>

‎apps/server/src/keybindings.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
compileResolvedKeybindingRule,
4242
compileResolvedKeybindingsConfig,
4343
parseKeybindingShortcut,
44+
parseKeybindingWhenExpression,
4445
} from "@t3tools/shared/keybindings";
4546

4647
export {
@@ -127,7 +128,12 @@ function encodedRuleShortcut(rule: KeybindingRule): string | null {
127128
function shadowsShortcut(existing: KeybindingRule, rule: KeybindingRule): boolean {
128129
const existingShortcut = encodedRuleShortcut(existing);
129130
if (!existingShortcut || existingShortcut !== encodedRuleShortcut(rule)) return false;
130-
return existing.when === undefined || rule.when === undefined || existing.when === rule.when;
131+
return (
132+
existing.when === undefined ||
133+
rule.when === undefined ||
134+
JSON.stringify(parseKeybindingWhenExpression(existing.when)) ===
135+
JSON.stringify(parseKeybindingWhenExpression(rule.when))
136+
);
131137
}
132138

133139
function keybindingRuleFromUpsertInput(input: ServerUpsertKeybindingInput): KeybindingRule {

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,10 +1934,15 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
19341934
undefined,
19351935
);
19361936
/** Native turns Codex started for a voice handoff -> the request and its thread. */
1937+
const voiceSession = yield* Ref.make<symbol | undefined>(undefined);
19371938
const voiceResubmits = yield* Ref.make(
19381939
new Map<
19391940
string,
1940-
{ readonly text: string; readonly providerThread: OrchestrationV2ProviderThread }
1941+
{
1942+
readonly text: string;
1943+
readonly providerThread: OrchestrationV2ProviderThread;
1944+
readonly session: symbol | undefined;
1945+
}
19411946
>(),
19421947
);
19431948
const finalAnswerItemIdsByTurn = yield* Ref.make(new Map<string, Set<string>>());
@@ -4733,10 +4738,12 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
47334738
// Codex started this turn itself for a voice handoff. V2 runs
47344739
// are orchestrator-owned, so stop it and resubmit the request
47354740
// as an ordinary run once it settles (see turn/completed).
4741+
const voiceOwner = yield* Ref.get(voiceSession);
47364742
yield* Ref.update(voiceResubmits, (current) =>
47374743
new Map(current).set(payload.turnId, {
47384744
text: handoff,
47394745
providerThread: voiceThread,
4746+
session: voiceOwner,
47404747
}),
47414748
);
47424749
yield* client
@@ -6173,7 +6180,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
61736180
// so running it again would repeat it.
61746181
if (
61756182
(payload.turn.status === "interrupted" || payload.turn.status === "failed") &&
6176-
continuationRequests !== undefined
6183+
continuationRequests !== undefined &&
6184+
voiceResubmit.session !== undefined &&
6185+
voiceResubmit.session === (yield* Ref.get(voiceSession))
61776186
) {
61786187
yield* voice.expectSpokenTurn(voiceResubmit.text);
61796188
yield* continuationRequests.offer({
@@ -7352,12 +7361,14 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
73527361
}),
73537362
),
73547363
),
7355-
),
7356-
startVoiceSession: (voiceInput) =>
7357-
Stream.unwrap(
7364+
),
7365+
startVoiceSession: (voiceInput) => {
7366+
const owner = Symbol("voice-session");
7367+
return Stream.unwrap(
73587368
Effect.gen(function* () {
73597369
const nativeThreadId = yield* getNativeThreadId(voiceInput.providerThread);
73607370
yield* ensureInitialized;
7371+
yield* Ref.set(voiceSession, owner);
73617372
yield* Ref.set(voiceProviderThread, voiceInput.providerThread);
73627373
return voice.start({
73637374
providerThreadId: nativeThreadId,
@@ -7370,9 +7381,17 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
73707381
).pipe(
73717382
// A replaced conversation must not clear its successor's thread.
73727383
Stream.ensuring(
7373-
Ref.update(voiceProviderThread, (current) =>
7374-
current === voiceInput.providerThread ? undefined : current,
7375-
),
7384+
Effect.gen(function* () {
7385+
yield* Ref.update(
7386+
voiceResubmits,
7387+
(current) =>
7388+
new Map([...current].filter(([, entry]) => entry.session !== owner)),
7389+
);
7390+
if ((yield* Ref.get(voiceSession)) === owner) {
7391+
yield* Ref.set(voiceSession, undefined);
7392+
yield* Ref.set(voiceProviderThread, undefined);
7393+
}
7394+
}),
73767395
),
73777396
Stream.mapError((cause) =>
73787397
cause._tag === "ProviderAdapterProtocolError"
@@ -7384,7 +7403,8 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
73847403
payload: cause,
73857404
}),
73867405
),
7387-
),
7406+
);
7407+
},
73887408
uploadFeedback: Effect.fn("CodexAdapterV2.uploadFeedback")(
73897409
function* (feedbackInput) {
73907410
const threadId = yield* getNativeThreadId(feedbackInput.providerThread);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as Stream from "effect/Stream";
12
import { assert, it } from "@effect/vitest";
23
import {
34
CheckpointId,
@@ -107,6 +108,7 @@ function layerExecutorFor(input: {
107108
Layer.succeed(
108109
ProviderSessionManager.ProviderSessionManagerV2,
109110
ProviderSessionManager.ProviderSessionManagerV2.of({
111+
startVoiceSession: () => Stream.empty,
110112
shutdown: Effect.void,
111113
open: () => Effect.die("unused open"),
112114
get: () => Effect.succeed(Option.none()),

0 commit comments

Comments
 (0)