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): 处理停止听写选区事件与 Windows Metro 路径

DavidShen42d71812da33
diff --git a/apps/mobile/app/sessions/new.tsx b/apps/mobile/app/sessions/new.tsx
index f6bd52bc49..e2b9a401a2 100644
--- a/apps/mobile/app/sessions/new.tsx
+++ b/apps/mobile/app/sessions/new.tsx
@@ -715,6 +715,9 @@ export default function NewRemoteSessionScreen() {
const voiceStartupInFlightRef = useRef(false);
const voiceStopInFlightRef = useRef(false);
const voiceSelectionUserOwnedRef = useRef(false);
+ // The press that stops dictation can emit one native selection event of its
+ // own. Consume that event before allowing a real user move to claim control.
+ const voiceStopGestureSelectionGuardRef = useRef(false);
const voiceStartupSeqRef = useRef(0);
const voiceControllerSessionRef = useRef<MobileVoiceControllerSession | null>(null);
const voiceDictionaryLearningTrackerRef = useRef<MobileVoiceDictionaryLearningTracker | null>(null);
@@ -3210,6 +3213,7 @@ export default function NewRemoteSessionScreen() {
}
const selectionBefore = takeRefinementContextTail(currentDraft.slice(0, initialSelection.start));
const selectionAfter = currentDraft.slice(initialSelection.end, initialSelection.end + 1200);
+ voiceStopGestureSelectionGuardRef.current = false;
voiceSelectionUserOwnedRef.current = false;
const controller = createMobileVoiceControllerSession({
credential,
@@ -5912,6 +5916,10 @@ export default function NewRemoteSessionScreen() {
// ASR/refinement teardown, so native cursor edits remain user-owned.
if (!voiceRecordingActiveRef.current) {
const selection = event.nativeEvent.selection;
+ if (voiceStopGestureSelectionGuardRef.current) {
+ voiceStopGestureSelectionGuardRef.current = false;
+ return;
+ }
const previous = firstMessageSelectionRef.current;
// A matching native echo of a controlled selection is not a user move.
if (voiceStopInFlightRef.current
@@ -5928,7 +5936,13 @@ export default function NewRemoteSessionScreen() {
onPasteImagesLoading={beginPastePlaceholders}
onPasteImagesLoadFailed={failPastePlaceholders}
onPressIn={() => {
- if (voiceIsListening) void finishVoiceRecording();
+ if (voiceIsListening) {
+ voiceStopGestureSelectionGuardRef.current = true;
+ setTimeout(() => {
+ voiceStopGestureSelectionGuardRef.current = false;
+ }, 0);
+ void finishVoiceRecording();
+ }
}}
placeholder={voiceIsListening ? '' : composerPlaceholder}
placeholderTextColor={colors.textTertiary}
diff --git a/apps/mobile/scripts/lib/sim-whoami.mjs b/apps/mobile/scripts/lib/sim-whoami.mjs
index 200e19a87e..2e223a1b3c 100644
--- a/apps/mobile/scripts/lib/sim-whoami.mjs
+++ b/apps/mobile/scripts/lib/sim-whoami.mjs
@@ -102,7 +102,7 @@ export function extractSimTakeoverArgs(args) {
}
/** Decide whether a listener has enough Cindy-specific identity for an explicit handoff. */
-export function classifySimMetroListener({ cwd, source, targetWorktree }) {
+export function classifySimMetroListener({ cwd, source, targetWorktree, platform = process.platform }) {
if (!cwd) return { confirmed: false, worktree: null };
const normalizedCwd = normalize(cwd).replaceAll('\\', '/').replace(/\/+$/, '');
@@ -113,10 +113,13 @@ export function classifySimMetroListener({ cwd, source, targetWorktree }) {
}
const worktree = normalizedCwd.slice(0, -suffix.length);
+ const isTarget = platform === 'win32'
+ ? worktree.toLowerCase() === normalizedTarget.toLowerCase()
+ : worktree === normalizedTarget;
return {
confirmed: true,
worktree,
- isTarget: worktree === normalizedTarget,
+ isTarget,
};
}
diff --git a/apps/mobile/src/__tests__/mobileSimWhoami.test.ts b/apps/mobile/src/__tests__/mobileSimWhoami.test.ts
index 5040565440..ba21a1d2b2 100644
--- a/apps/mobile/src/__tests__/mobileSimWhoami.test.ts
+++ b/apps/mobile/src/__tests__/mobileSimWhoami.test.ts
@@ -78,6 +78,33 @@ describe('mobile:sim takeover and JSON arguments', () => {
targetWorktree: '/repo/',
})).toEqual({ confirmed: true, worktree: '/repo', isTarget: true });
});
+
+ it('compares Windows worktree paths case-insensitively', () => {
+ expect(classifySimMetroListener({
+ cwd: 'C:\\Repo\\apps\\mobile',
+ source: 'branch@commit',
+ targetWorktree: 'c:\\repo',
+ platform: 'win32',
+ })).toEqual({ confirmed: true, worktree: 'C:/Repo', isTarget: true });
+ expect(classifySimMetroListener({
+ cwd: 'C:\\RepoOther\\apps\\mobile',
+ source: 'branch@commit',
+ targetWorktree: 'c:\\repo',
+ platform: 'win32',
+ })).toEqual({ confirmed: true, worktree: 'C:/RepoOther', isTarget: false });
+ expect(classifySimMetroListener({
+ cwd: 'C:\\Repo\\apps\\mobile',
+ source: 'branch@commit',
+ targetWorktree: 'D:\\repo',
+ platform: 'win32',
+ })).toEqual({ confirmed: true, worktree: 'C:/Repo', isTarget: false });
+ expect(classifySimMetroListener({
+ cwd: '/Repo/apps/mobile',
+ source: 'branch@commit',
+ targetWorktree: '/repo',
+ platform: 'linux',
+ })).toEqual({ confirmed: true, worktree: '/Repo', isTarget: false });
+ });
});
describe('mobile:sim Metro handoff', () => {
diff --git a/apps/mobile/src/__tests__/newSessionVoiceInput.test.tsx b/apps/mobile/src/__tests__/newSessionVoiceInput.test.tsx
index f5f0691a01..a4282793b9 100644
--- a/apps/mobile/src/__tests__/newSessionVoiceInput.test.tsx
+++ b/apps/mobile/src/__tests__/newSessionVoiceInput.test.tsx
@@ -76,7 +76,8 @@ function composerExpression(name: string) {
}
const expressions = propNames.map((name) => `${name}: ${composerExpression(name)}`);
const compiled = ts.transpileModule(`function pageProps(bindings) {
- const { Platform, voiceIsListening, draft, finishVoiceRecording, composerPlaceholder, firstMessageSelection } = bindings;
+ const { Platform, voiceIsListening, draft, finishVoiceRecording, composerPlaceholder, firstMessageSelection,
+ voiceStopGestureSelectionGuardRef } = bindings;
const styles = { inputVoiceHidden: ${hiddenStyle.initializer.getText(source)} };
return { ${expressions.join(',\n')} };
}`, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText;
@@ -95,14 +96,16 @@ if (!stopCallback.initializer || !ts.isCallExpression(stopCallback.initializer))
type Selection = { start: number; end: number };
const compiledVoice = ts.transpileModule(`function voiceCallbacks(bindings) {
const { voiceSelectionUserOwnedRef, voiceRecordingActiveRef, voiceStopInFlightRef,
+ voiceStopGestureSelectionGuardRef,
firstMessageRef, firstMessageSelectionRef, setFirstMessageSelection, setFirstMessageDraft,
voiceControllerSessionRef, voiceStartupSeqRef, voiceStartupInFlightRef, voiceState,
setVoiceState, setVoiceError, setAudioModeAsync, requestAnimationFrame,
- firstMessageInputRef, formatRemoteError } = bindings;
+ firstMessageInputRef, formatRemoteError, voiceIsListening, finishVoiceRecording } = bindings;
return {
publish: ${draftCallback.initializer.getText(source)},
select: ${composerExpression('onSelectionChange')},
type: ${composerExpression('onChangeText')},
+ press: ${composerExpression('onPressIn')},
stop: ${stopCallback.initializer.arguments[0].getText(source)},
};
}`, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText;
@@ -111,6 +114,7 @@ const voiceCallbacks = new Function(`${compiledVoice}; return voiceCallbacks;`)(
publish: (text: string, selection?: Selection, replacement?: Selection & { text: string }) => void;
select: (event: { nativeEvent: { selection: Selection } }) => void;
type: (text: string) => void;
+ press: () => void;
stop: () => Promise<string | null>;
};
@@ -119,11 +123,14 @@ function pendingVoiceStop(initialDraft = '前后') {
const firstMessageSelectionRef = { current: { start: 1, end: 1 } };
let controlledSelection = firstMessageSelectionRef.current;
let completeStop!: (draft: string) => void;
+ let finishVoiceRecording!: () => Promise<string | null>;
+ let activeStop: Promise<string | null> | undefined;
const stopGate = new Promise<string>((resolve) => { completeStop = resolve; });
const setNativeProps = vi.fn();
const callbacks = voiceCallbacks({
firstMessageRef, firstMessageSelectionRef,
voiceSelectionUserOwnedRef: { current: false },
+ voiceStopGestureSelectionGuardRef: { current: false },
voiceRecordingActiveRef: { current: true }, voiceStopInFlightRef: { current: false },
voiceStartupSeqRef: { current: 1 }, voiceStartupInFlightRef: { current: false },
voiceControllerSessionRef: { current: { stop: () => stopGate } },
@@ -133,12 +140,17 @@ function pendingVoiceStop(initialDraft = '前后') {
setAudioModeAsync: async () => undefined,
requestAnimationFrame: (callback: () => void) => callback(),
firstMessageInputRef: { current: { setNativeProps } }, formatRemoteError: String,
+ voiceIsListening: true, finishVoiceRecording: () => {
+ activeStop = callbacks.stop();
+ return activeStop;
+ },
});
return {
...callbacks,
selection: () => controlledSelection,
move: (start: number, end = start) => callbacks.select({ nativeEvent: { selection: { start, end } } }),
complete: () => completeStop(firstMessageRef.current), setNativeProps,
+ press: () => { callbacks.press(); return activeStop; },
insert: (text: string) => firstMessageRef.current.slice(0, controlledSelection.start)
+ text + firstMessageRef.current.slice(controlledSelection.end),
};
@@ -182,6 +194,20 @@ describe('new-session selection during dictation stop', () => {
expect(voice.insert('?')).toBe('前词后!?');
});
+ it('ignores the selection event caused by the stop gesture, then accepts a later user move', async () => {
+ const voice = pendingVoiceStop();
+ voice.publish('前识别后', { start: 3, end: 3 });
+ const stop = voice.press();
+ voice.move(5); // native selection change caused by the same stop press
+ voice.publish('前原始转写后', { start: 5, end: 5 });
+ expect(voice.selection()).toEqual({ start: 5, end: 5 });
+ voice.move(0); // a real user move during stop owns the selection
+ voice.publish('前润色后', { start: 3, end: 3 });
+ voice.complete();
+ expect(await stop).toBe('前润色后');
+ expect(voice.selection()).toEqual({ start: 0, end: 0 });
+ });
+
it.each([false, true])('follows final ASR and refinement without a user move (partial=%s)', async (partial) => {
const voice = pendingVoiceStop();
if (partial) voice.publish('前识别后', { start: 3, end: 3 });
@@ -255,7 +281,8 @@ function mountInput() {
value: draft, onChangeText: vi.fn(), onPasteImages: vi.fn(),
...pageProps({ Platform: { OS: native.platform }, voiceIsListening: listening,
draft: { firstMessage: draft }, firstMessageSelection: selection,
- finishVoiceRecording, composerPlaceholder: 'Draft' }),
+ finishVoiceRecording, composerPlaceholder: 'Draft',
+ voiceStopGestureSelectionGuardRef: { current: false } }),
inputOverlay: listening ? createElement('span', { 'data-testid': 'preview' }, draft) : null,
})));
}