feat(mobile): add offline iPhone voice input #8614
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 8 commits into
main fromrefs/pull/8614/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
github-actions[bot](GitHub profile, opens in a new tab) added labels📱 Native Change
- packages/client-runtime/src/voice-input/controller.ts lines 359 to 360Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +396,21 @@396+ return;397+ }398+ if (this.state.phase === "preparing" || this.state.phase === "transcribing") {399+ this.operationToken += 1;400+ this.setState(IDLE_STATE);401+ }402+ }403+404+ private async finishRecording(405+ alreadyStopped: boolean,406+ completedUri: string | null,407+ ): Promise<void> {408+ if (this.finishing || this.state.phase !== "recording") return;409+ this.finishing = true;410+ const operationToken = this.operationToken;411+ this.setState({ phase: "transcribing", error: null, errorAction: null });412+413+ try {414+ if (!alreadyStopped) await this.dependencies.recorder.stop();415+ await this.releaseAudioSession();416+ this.recordingUri = completedUri ?? this.dependencies.recorder.uri ?? this.recordingUri;@@ -308,13 +278,16 @@308278 ) ?? null309279 );310280 }, [props.serverConfig, props.selectedThread.modelSelection.instanceId]);281+ const composerOwnerKey = scopedThreadKey(props.environmentId, props.selectedThread.id);311282312283 const composerMenu = useComposerCommandMenu({313284 draftMessage: props.draftMessage,285+ ownerKey: composerOwnerKey,314286 environmentId: props.environmentId,315287 projectCwd: props.projectCwd,316288 selectedProviderStatus,317289 hasThread: true,318290 onChangeDraftMessage: props.onChangeDraftMessage,319291 onUpdateInteractionMode: props.onUpdateInteractionMode,320292 });293+ const voiceInput = useVoiceInputController({
- apps/mobile/src/features/threads/ThreadComposer.tsx line 357Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -309,12 +279,21 @@309279 );310280 }, [props.serverConfig, props.selectedThread.modelSelection.instanceId]);281+ const composerOwnerKey = scopedThreadKey(props.environmentId, props.selectedThread.id);311282312283 const composerMenu = useComposerCommandMenu({313284 draftMessage: props.draftMessage,285+ ownerKey: composerOwnerKey,314286 environmentId: props.environmentId,315287 projectCwd: props.projectCwd,316288 selectedProviderStatus,317289 hasThread: true,318290 onChangeDraftMessage: props.onChangeDraftMessage,319291 onUpdateInteractionMode: props.onUpdateInteractionMode,320292 });293+ const voiceInput = useVoiceInputController({294+ ownerKey: composerOwnerKey,295+ draftMessage: props.draftMessage,296+ selection: composerMenu.selection,297+ onChangeDraftMessage: props.onChangeDraftMessage,298+ onChangeSelection: composerMenu.onSelectionChange,299+ });@@ -0,0 +153,21 @@153+ const replacement = transcript.trim();154+ if (replacement.length === 0) {155+ return { kind: "empty" };156+ }157+158+ const isEmptySelection = captured.selection.start === captured.selection.end;159+ const normalizedLocale = locale.replaceAll("_", "-").toLowerCase();160+ const usesEnglishSpacing = normalizedLocale === "en" || normalizedLocale.startsWith("en-");161+ let insertion = replacement;162+ if (isEmptySelection && usesEnglishSpacing) {163+ const left = captured.text[captured.selection.start - 1];164+ const right = captured.text[captured.selection.start];165+ const leftNeedsBoundary =166+ left !== undefined &&167+ /[A-Za-z0-9.!?,:;)\]}'"]/.test(left) &&168+ (right === undefined || /\s/.test(right));169+ const rightNeedsBoundary =170+ right !== undefined &&171+ /[A-Za-z0-9([{'"]/.test(right) &&172+ (left === undefined || /\s/.test(left));173+ insertion = `${leftNeedsBoundary ? " " : ""}${replacement}${rightNeedsBoundary ? " " : ""}`;apps/mobile/src/features/threads/NewTaskDraftScreen.tsx line 268Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -231,3 +239,8 @@231239 onChangeDraftMessage: flow.setPrompt,232240 onUpdateInteractionMode: flow.planModeEnabled ? flow.setInteractionMode : undefined,233241 });242+ const voiceInput = useVoiceInputController({243+ ownerKey: flow.draftKey,244+ draftMessage: flow.prompt,245+ selection: composerMenu.selection,246+ disabled: isIncomingShareTransferPending || isImportingShare || flow.submitting,
- packages/client-runtime/src/voice-input/controller.ts line 292Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +328,21 @@328+ if (this.state.phase !== "recording") return Promise.resolve();329+ return this.finishRecording(false, null);330+ }331+332+ cancel(): void {333+ switch (this.state.phase) {334+ case "idle":335+ return;336+ case "error":337+ this.setState(IDLE_STATE);338+ return;339+ case "preparing":340+ this.operationToken += 1;341+ this.setState(IDLE_STATE);342+ return;343+ case "recording":344+ this.discardRecording(null);345+ return;346+ case "transcribing":347+ this.operationToken += 1;348+ this.setState(IDLE_STATE);
- apps/mobile/src/native/voiceTranscription.ios.ts line 93Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +52,21 @@52+ "Voice transcription does not support this device language.",53+ { cause: error },54+ );55+ }56+57+ throw wrapError(58+ "preparation-failed",59+ "Voice transcription could not prepare this language.",60+ error,61+ );62+ }63+}64+65+export async function transcribeVoiceRecording(uri: string, locale: string): Promise<string> {66+ try {67+ const audio = await new File(uri).arrayBuffer();68+ const result = await AppleTranscription.transcribe(audio, locale);69+ return result.segments70+ .map((segment) => segment.text)71+ .join(" ")72+ .trim();apps/mobile/src/features/threads/use-composer-command-menu.ts line 65Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -54,3 +58,8 @@5458 return { start, end: selectionEnd };5559 });5660 }, [draftMessage.length]);61+ useEffect(() => {62+ if (previousOwnerKeyRef.current === ownerKey) return;63+ previousOwnerKeyRef.current = ownerKey;64+ setSelection(composerSelectionAtEnd(draftMessage));65+ }, [draftMessage, ownerKey]);
- apps/mobile/src/features/threads/ThreadComposer.tsx line 402Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -337,3 +400,3 @@337400 }338401 onEditorFocusChange?.(false);339402 }, [onEditorFocusChange, onExpandedChange, settingsSheetPresentation.isActive]);
- packages/client-runtime/src/voice-input/controller.ts lines 333 to 333OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +313,21 @@313+ return this.interruptRecording();314+ }315+316+ handleRecorderStatus(status: VoiceRecorderStatus): Promise<void> | void {317+ if (this.state.phase !== "recording") return;318+ if (status.hasError) {319+ return this.interruptRecording(320+ status.error ?? "Voice recording was interrupted.",321+ status.url,322+ );323+ }324+ if (status.isFinished) {325+ if (!status.url) {326+ return this.interruptRecording();327+ }328+ return this.finishRecording(true, status.url);329+ }330+ }331+332+ ownerChanged(): void {333+ if (this.state.phase === "idle" || this.state.phase === "error") return;
juliusmarminge merged this352710d
juliusmarminge closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
d020e7f5-6801-48a2-9440-06dce8facce2You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Warning
Your free Security trial is over. An organization admin can activate Security or dismiss this notice.
Comment `@coderabbitai help` to get the list of available commands.