Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 回复任务时语音结束不再自动弹出键盘 (#5790)

Dashf9274e609bac
diff --git a/apps/mobile/app/sessions/[sessionId].tsx b/apps/mobile/app/sessions/[sessionId].tsx
index 64cefc52ad..ed93e311bc 100644
--- a/apps/mobile/app/sessions/[sessionId].tsx
+++ b/apps/mobile/app/sessions/[sessionId].tsx
@@ -2035,7 +2035,7 @@ export default function SessionScreen() {
const voiceLongPressActiveRef = useRef(false);
const voiceSuppressNextPressRef = useRef(false);
const voiceStopAfterStartRef = useRef(false);
- const finishVoiceRecordingRef = useRef<(() => void) | null>(null);
+ const finishVoiceRecordingRef = useRef<((options?: { focusInput?: boolean }) => void) | null>(null);
const composerInputRef = useRef<ComposerRichInputHandle | null>(null);
const voiceControllerSessionRef = useRef<MobileVoiceControllerSession | null>(null);
const voiceDictionaryLearningTrackerRef = useRef<MobileVoiceDictionaryLearningTracker | null>(null);
@@ -3060,7 +3060,8 @@ export default function SessionScreen() {
}, [deviceId, deviceName, guardedPush, queueDrawerNavigation]);
const handleComposerInputPressIn = useCallback(() => {
if (voiceRecordingActiveRef.current || voiceState === 'listening') {
- finishVoiceRecordingRef.current?.();
+ // Tapping the input to stop dictation means the user wants to type.
+ finishVoiceRecordingRef.current?.({ focusInput: true });
}
}, [voiceState]);
const openSessionMenu = useCallback((view: SessionMenuView = 'menu') => {
@@ -5600,7 +5601,7 @@ export default function SessionScreen() {
};
}, [sessionId]);
- const finishVoiceRecording = useCallback(async (options: { sendAfterTranscribe?: boolean } = {}) => {
+ const finishVoiceRecording = useCallback(async (options: { sendAfterTranscribe?: boolean; focusInput?: boolean } = {}) => {
if (voiceStopInFlightRef.current) return;
const controller = voiceControllerSessionRef.current;
if (!controller) return;
@@ -5627,11 +5628,13 @@ export default function SessionScreen() {
const latestDocument = latestDraft.trim()
? reconcileComposerProjectedText(composerDocumentRef.current, latestDraft)
: documentBeforeStop;
- // Focus only after dictation ends, with the caret after the inserted text.
+ // Keep the caret after the inserted text. The keyboard opens only when the
+ // user stopped dictation by tapping the input (same as the new-task page).
const insertionEnd = composerInputRef.current?.getSelection(latestDraft).end ?? latestDraft.length;
requestAnimationFrame(() => {
if (inputBeforeStop && composerInputRef.current === inputBeforeStop && draftRef.current === latestDraft) {
- inputBeforeStop.applyDocumentAndFocusSelection(composerDocumentRef.current, insertionEnd);
+ if (options.focusInput) inputBeforeStop.applyDocumentAndFocusSelection(composerDocumentRef.current, insertionEnd);
+ else inputBeforeStop.applyDocumentAndSetSelection(composerDocumentRef.current, insertionEnd);
}
});
if (options.sendAfterTranscribe && (composerDocumentHasContent(latestDocument) || attachments.length > 0)) {
@@ -5656,8 +5659,8 @@ export default function SessionScreen() {
}, []);
useEffect(() => {
- finishVoiceRecordingRef.current = () => {
- void finishVoiceRecording();
+ finishVoiceRecordingRef.current = (options) => {
+ void finishVoiceRecording(options);
};
}, [finishVoiceRecording]);
diff --git a/apps/mobile/src/__tests__/composerNativeVoiceSelection.test.tsx b/apps/mobile/src/__tests__/composerNativeVoiceSelection.test.tsx
index ff7d806525..6bfa77978d 100644
--- a/apps/mobile/src/__tests__/composerNativeVoiceSelection.test.tsx
+++ b/apps/mobile/src/__tests__/composerNativeVoiceSelection.test.tsx
@@ -66,11 +66,12 @@ it('allows the stop callback to restore the caret after the final transcript ren
inputBeforeStop.rememberSelection(final, { start: final.length, end: final.length });
const document = render(final);
const end = input.current!.getSelection(final).end;
- // This is the screen's unmount/replacement guard before restoring focus.
+ // This is the screen's unmount/replacement guard before restoring the caret.
if (input.current === inputBeforeStop) {
- inputBeforeStop.applyDocumentAndFocusSelection(document, end);
+ inputBeforeStop.applyDocumentAndSetSelection(document, end);
}
- expect(bridge.setDocument).toHaveBeenCalledWith(expect.anything(), final.length, true);
+ // Dictation ending must not raise the keyboard on its own.
+ expect(bridge.setDocument).toHaveBeenCalledWith(expect.anything(), final.length, false);
});
it('restores the insertion end before an existing suffix and maps quote attachments', () => {
diff --git a/apps/mobile/src/__tests__/composerRichInputHtml.test.ts b/apps/mobile/src/__tests__/composerRichInputHtml.test.ts
index deff5694bd..4ae755d9ca 100644
--- a/apps/mobile/src/__tests__/composerRichInputHtml.test.ts
+++ b/apps/mobile/src/__tests__/composerRichInputHtml.test.ts
@@ -548,6 +548,22 @@ describe('mobile composer rich input HTML', () => {
}, true, { nodeIndex: 1, offset: 8 });
expect(selection.anchorNode).toBe(children[2]);
expect(selection.anchorOffset).toBe(8);
+ // Without focusAfter the caret is restored only while the editor already has focus.
+ let focusCalls = 0;
+ root.focus = () => { focusCalls += 1; };
+ const dictated = {
+ version: 1,
+ nodes: [{ type: 'text', text: 'dictated text' }],
+ };
+ // Replacing the DOM drops the live selection, as a real contenteditable does.
+ selection.removeAllRanges();
+ windowStub.cindyComposer?.applyDocument(dictated, false, { nodeIndex: 0, offset: 4 });
+ expect(selection.anchorNode).toBeNull();
+ Object.assign(documentStub, { activeElement: root });
+ windowStub.cindyComposer?.applyDocument(dictated, false, { nodeIndex: 0, offset: 4 });
+ expect(selection.anchorNode).toBe(children[0]);
+ expect(selection.anchorOffset).toBe(4);
+ expect(focusCalls).toBe(0);
expect(legacyHtml).not.toContain('.flatMap(');
});
diff --git a/apps/mobile/src/__tests__/composerRichInputLifecycle.test.tsx b/apps/mobile/src/__tests__/composerRichInputLifecycle.test.tsx
index ec32240db7..b59c54ead1 100644
--- a/apps/mobile/src/__tests__/composerRichInputLifecycle.test.tsx
+++ b/apps/mobile/src/__tests__/composerRichInputLifecycle.test.tsx
@@ -281,6 +281,17 @@ it('keeps pending caret intent on first ready but does not replay it on reload',
expect(page.applyDocument.mock.lastCall?.slice(1, 3)).toEqual([false, null]);
});
+it('applies the final dictation document without focusing and keeps the remembered caret', () => {
+ const { ref, page, send } = mount();
+ send({ type: 'ready' });
+ act(() => ref.current?.rememberSelection('voice suffix', { start: 5, end: 5 }));
+ act(() => ref.current?.applyDocumentAndSetSelection(textComposerDocument('voice suffix'), 5));
+ expect(page.document).toEqual(textComposerDocument('voice suffix'));
+ expect(page.applyDocument.mock.lastCall?.slice(1, 3)).toEqual([false, { nodeIndex: 0, offset: 5 }]);
+ expect(page.focus).not.toHaveBeenCalled();
+ expect(ref.current?.getSelection('voice suffix')).toEqual({ start: 5, end: 5 });
+});
+
it('restores a hidden dictation draft without focusing or losing its saved insertion end', () => {
const { ref, page, send } = mount(true);
send({ type: 'ready' });
diff --git a/apps/mobile/src/__tests__/sessionComposerDesktopFirst.test.ts b/apps/mobile/src/__tests__/sessionComposerDesktopFirst.test.ts
index 6827b1a2cb..58f1886153 100644
--- a/apps/mobile/src/__tests__/sessionComposerDesktopFirst.test.ts
+++ b/apps/mobile/src/__tests__/sessionComposerDesktopFirst.test.ts
@@ -68,7 +68,7 @@ describe('mobile session composer desktop-first surface', () => {
const voiceStart = source.indexOf('const startVoiceRecording = useCallback(async () => {');
const voiceEnd = source.indexOf('const removeRemoteFileAttachment = useCallback', voiceStart);
const voiceSource = source.slice(voiceStart, voiceEnd);
- const finishVoiceStart = source.indexOf('const finishVoiceRecording = useCallback(async (options: { sendAfterTranscribe?: boolean } = {}) => {');
+ const finishVoiceStart = source.indexOf('const finishVoiceRecording = useCallback(async (options: { sendAfterTranscribe?: boolean; focusInput?: boolean } = {}) => {');
const finishVoiceEnd = source.indexOf('const cancelVoiceRecording = useCallback', finishVoiceStart);
const finishVoiceSource = source.slice(finishVoiceStart, finishVoiceEnd);
const sharedSource = readTextLf(resolve(process.cwd(), 'src/session/MobileComposerInputRow.tsx'), 'utf8');
@@ -529,7 +529,6 @@ describe('mobile session composer desktop-first surface', () => {
expect(source).toContain('placeholderTextColor={colors.textPlaceholder}');
expect(source).toContain('voiceDraftListeningText: {\n color: colors.textPlaceholder,');
expect(source).not.toContain('voiceDraftListeningText: {\n color: colors.statusReady,');
- expect(source).toContain('finishVoiceRecordingRef.current?.();');
expect(source).toContain('const voiceStopInFlightRef = useRef(false);');
expect(voiceSource).toContain('|| voiceStopInFlightRef.current');
expect(finishVoiceSource).toContain('if (voiceStopInFlightRef.current) return;');
@@ -540,6 +539,11 @@ describe('mobile session composer desktop-first surface', () => {
finishVoiceSource.indexOf('const latestDraft = await controller.stop();'),
);
expect(finishVoiceSource).toContain('voiceStopInFlightRef.current = false;');
+ // Only a tap on the input that stops dictation raises the keyboard; mic-button,
+ // release-to-send and automatic stops keep it closed.
+ expect(finishVoiceSource).toContain('if (options.focusInput) inputBeforeStop.applyDocumentAndFocusSelection(composerDocumentRef.current, insertionEnd);');
+ expect(finishVoiceSource).toContain('else inputBeforeStop.applyDocumentAndSetSelection(composerDocumentRef.current, insertionEnd);');
+ expect(source).toContain('finishVoiceRecordingRef.current?.({ focusInput: true });');
expect(composerInputSource).toContain('floatingVoiceButton={voiceUiAvailable ? controls.voiceButton : undefined}');
expect(composerInputSource).not.toContain('floatingVoiceButtonStyle=');
expect(composerInputSource).toContain('voicePlacement={composerVoicePlacement}');
diff --git a/apps/mobile/src/session/ComposerNativeInput.ios.tsx b/apps/mobile/src/session/ComposerNativeInput.ios.tsx
index cc49011620..b34ab41c2c 100644
--- a/apps/mobile/src/session/ComposerNativeInput.ios.tsx
+++ b/apps/mobile/src/session/ComposerNativeInput.ios.tsx
@@ -150,6 +150,14 @@ export const ComposerNativeInput = forwardRef<
true,
);
},
+ applyDocumentAndSetSelection(document, offset) {
+ rememberedSelection.current = null;
+ void native.current?.setDocument(
+ payload(document),
+ composerOffsetToNative(document, offset),
+ false,
+ );
+ },
insertNode(node: ComposerNode) {
void native.current?.insertNodes(
nativeComposerNodes({ version: 1, nodes: [node] }),
diff --git a/apps/mobile/src/session/ComposerRichInput.tsx b/apps/mobile/src/session/ComposerRichInput.tsx
index c0995ee6e7..d772322048 100644
--- a/apps/mobile/src/session/ComposerRichInput.tsx
+++ b/apps/mobile/src/session/ComposerRichInput.tsx
@@ -33,6 +33,8 @@ export interface ComposerRichInputHandle {
rememberSelection(draft: string, selection: { start: number; end: number }): void;
applyDocumentAndSetSelectionToEnd(document: ComposerDocument): void;
applyDocumentAndFocusSelection(document: ComposerDocument, offset: number): void;
+ /** Apply a document and place the caret where possible without opening the keyboard. */
+ applyDocumentAndSetSelection(document: ComposerDocument, offset: number): void;
blur(): void;
focus(): void;
insertNode(node: ComposerNode): void;
@@ -221,6 +223,12 @@ const ComposerWebInput = forwardRef<ComposerRichInputHandle, ComposerRichInputPr
webSignatureRef.current = JSON.stringify(value);
applyDocument(value, true, composerCaretPosition(value, offset));
},
+ // Restores the caret only if the editor is already focused; otherwise
+ // getSelection keeps the remembered insertion point until the user taps it.
+ applyDocumentAndSetSelection: (value, offset) => {
+ webSignatureRef.current = JSON.stringify(value);
+ applyDocument(value, false, composerCaretPosition(value, offset));
+ },
blur: () => {
pendingFocusRef.current = false;
if (pendingDocumentRef.current?.focusAfter) {
diff --git a/apps/mobile/src/session/composerRichInputHtml.ts b/apps/mobile/src/session/composerRichInputHtml.ts
index bb809bc33e..5309fe13f3 100644
--- a/apps/mobile/src/session/composerRichInputHtml.ts
+++ b/apps/mobile/src/session/composerRichInputHtml.ts
@@ -582,8 +582,11 @@ export function buildComposerRichInputHtml(config: ComposerRichInputConfig): str
applyDocument(value, focusAfter, caret, nextDocumentId) {
if (Number.isSafeInteger(nextDocumentId)) documentId = nextDocumentId;
render(value, focusAfter === true && !caret);
- if (!focusAfter || !caret) return;
- root.focus();
+ if (!caret) return;
+ // Without focusAfter, restore the caret only for an editor that already has
+ // focus (keyboard open); never focus it here, which would raise the keyboard.
+ if (focusAfter) root.focus();
+ else if (document.activeElement !== root) return;
const nodes = Array.from(root.childNodes).filter((node) => !isEmptyCaretAnchor(node));
const node = nodes[caret.nodeIndex];
const selection = window.getSelection();