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(voice): 提交回答时等待手机语音连接完成

Dash81bf68bb7680
diff --git a/apps/mobile/src/__tests__/InteractionPanelReceipt.test.tsx b/apps/mobile/src/__tests__/InteractionPanelReceipt.test.tsx
index b48ba0c153..f9ca6d99d1 100644
--- a/apps/mobile/src/__tests__/InteractionPanelReceipt.test.tsx
+++ b/apps/mobile/src/__tests__/InteractionPanelReceipt.test.tsx
@@ -126,18 +126,29 @@ async function type(id: string, value: string) {
// Unit regression: the real card/hook disabled submit during recording. Existing
// voice coverage only exercised signed-out errors, never final text or failures.
-it.each(['free', 'custom', 'multi', 'failed', 'removed'])(
+it.each(['free', 'custom', 'multi', 'failed', 'removed', 'connecting', 'connecting-failed', 'connecting-removed', 'connecting-cancelled'])(
'submits final voice text once and preserves failed/cancelled answers: %s', async (mode) => {
let finish!: () => void;
let fail!: (error: Error) => void;
+ let connect!: () => void;
+ let failConnection!: (error: Error) => void;
+ let signal: AbortSignal | undefined;
const stop = vi.fn();
- startDictation.mockImplementation(async (input: { onListening: () => void; setText: (text: string) => void }) => {
+ const cancel = vi.fn(async () => undefined);
+ startDictation.mockImplementation(async (input: { signal?: AbortSignal; onListening: () => void; setText: (text: string) => void }) => {
+ signal = input.signal;
input.onListening();
stop.mockImplementation(() => new Promise<void>((resolve, reject) => {
finish = () => { input.setText('Final answer'); resolve(); };
fail = reject;
}));
- return { stop, cancel: vi.fn(async () => undefined) };
+ const run = { stop, cancel };
+ // PR #7017 review: listening can precede the ASR handshake. The previous
+ // fixture always resolved startup immediately and missed lost recordings.
+ if (mode.startsWith('connecting')) {
+ await new Promise<void>((resolve, reject) => { connect = resolve; failConnection = reject; });
+ }
+ return run;
});
remoteSessionStore.setPendingInteractions('s1', [{ request: {
kind: 'ask_user_question', requestId, questions: [{ question: 'Question?',
@@ -152,12 +163,38 @@ it.each(['free', 'custom', 'multi', 'failed', 'removed'])(
</AskCardAuthContext.Provider>,
));
if (mode === 'multi') await click('interaction.ask.option.1');
- if (mode === 'failed') await type('interaction.ask.textInput', 'Saved draft');
+ if (mode === 'failed' || mode === 'connecting-failed') await type('interaction.ask.textInput', 'Saved draft');
await click('interaction.ask.voice');
const send = host.querySelector<HTMLButtonElement>('[data-testid="interaction.ask.submitButton"]')!;
expect(send.disabled).toBe(false);
+ if (mode === 'connecting-cancelled') {
+ await click('interaction.ask.voice');
+ expect(signal?.aborted).toBe(true);
+ await act(async () => connect());
+ expect(cancel).toHaveBeenCalledOnce();
+ expect(stop).not.toHaveBeenCalled();
+ expect(resolveInteraction).not.toHaveBeenCalled();
+ return;
+ }
await click('interaction.ask.submitButton');
await click('interaction.ask.submitButton');
+ if (mode.startsWith('connecting')) {
+ expect(signal?.aborted).toBe(false);
+ expect(stop).not.toHaveBeenCalled();
+ expect(resolveInteraction).not.toHaveBeenCalled();
+ if (mode === 'connecting-removed') {
+ await act(async () => remoteSessionStore.setPendingInteractions('s1', []));
+ expect(signal?.aborted).toBe(true);
+ }
+ await act(async () => mode === 'connecting-failed' ? failConnection(new Error('Connection failed')) : connect());
+ if (mode !== 'connecting') {
+ expect(resolveInteraction).not.toHaveBeenCalled();
+ expect(stop).not.toHaveBeenCalled();
+ if (mode === 'connecting-failed') expect(readAskUserDraft(requestId)?.customInput).toBe('Saved draft');
+ else expect(cancel).toHaveBeenCalledOnce();
+ return;
+ }
+ }
expect(stop).toHaveBeenCalledOnce();
expect(resolveInteraction).not.toHaveBeenCalled();
if (mode === 'removed') await act(async () => remoteSessionStore.setPendingInteractions('s1', []));
diff --git a/apps/mobile/src/session/useAskCardVoice.ts b/apps/mobile/src/session/useAskCardVoice.ts
index c714f31489..75f6953f7b 100644
--- a/apps/mobile/src/session/useAskCardVoice.ts
+++ b/apps/mobile/src/session/useAskCardVoice.ts
@@ -32,6 +32,7 @@ export function useAskCardVoice(input: {
const [error, setError] = useState<string | null>(null);
const runRef = useRef<VoiceRun | null>(null);
const startingRef = useRef(false);
+ const startingPromiseRef = useRef<Promise<void> | null>(null);
const stoppingRef = useRef<Promise<string | null> | null>(null);
const startAbortRef = useRef<AbortController | null>(null);
const inputRef = useRef(input);
@@ -39,28 +40,37 @@ export function useAskCardVoice(input: {
const authRef = useRef(auth);
authRef.current = auth;
- const stop = useCallback(() => {
+ const stop = useCallback((waitForStartup = true) => {
if (stoppingRef.current) return stoppingRef.current;
const wasStarting = startingRef.current;
- startingRef.current = false;
- // Abort the startup so the microphone closes now instead of waiting for a
- // slow voice-service handshake to finish.
- startAbortRef.current?.abort();
- startAbortRef.current = null;
- const run = runRef.current;
- if (!run) {
+ const pendingStart = wasStarting ? startingPromiseRef.current : null;
+ // Send preserves captured audio while connecting. A second microphone tap
+ // still cancels startup immediately, as does removing the card.
+ if (!waitForStartup) {
+ startingRef.current = false;
+ startAbortRef.current?.abort();
+ startAbortRef.current = null;
+ }
+ if (!runRef.current && (!waitForStartup || !pendingStart)) {
setListening(false);
return Promise.resolve(wasStarting ? null : inputRef.current.getText());
}
// Keep the run owned until final ASR/refinement settles. Unmount can then
// cancel it, and another Send joins this same stop instead of using a draft.
- const stopping = run.stop().then(() => (
- runRef.current === run ? inputRef.current.getText() : null
- )).catch((error: unknown) => {
+ const stopping = (async () => {
+ if (pendingStart) await pendingStart;
+ const run = runRef.current;
+ if (!run) return null;
+ try {
+ await run.stop();
+ return runRef.current === run ? inputRef.current.getText() : null;
+ } finally {
+ if (runRef.current === run) runRef.current = null;
+ }
+ })().catch((error: unknown) => {
setError(error instanceof Error ? error.message : String(error));
return null;
}).finally(() => {
- if (runRef.current === run) runRef.current = null;
setListening(false);
if (stoppingRef.current === stopping) stoppingRef.current = null;
});
@@ -84,7 +94,7 @@ export function useAskCardVoice(input: {
// now would race the microphone teardown.
if (stoppingRef.current) return;
if (runRef.current || startingRef.current) {
- stop();
+ void stop(false);
return;
}
const deviceId = inputRef.current.deviceId?.trim();
@@ -97,10 +107,10 @@ export function useAskCardVoice(input: {
setError(null);
const abort = new AbortController();
startAbortRef.current = abort;
- void (async () => {
+ const starting = (async () => {
try {
const { startAskCardDictation } = await import('@/session/askCardVoiceStart');
- if (!startingRef.current) return;
+ if (abort.signal.aborted || !startingRef.current) return;
const run = await startAskCardDictation({
deviceId,
signal: abort.signal,
@@ -108,19 +118,22 @@ export function useAskCardVoice(input: {
refreshAccessToken: () => currentAuth.refreshAccessToken(),
apiFetch: currentAuth.apiFetch,
getText: () => inputRef.current.getText(),
- setText: (value) => inputRef.current.setText(value),
+ setText: (value) => {
+ if (!abort.signal.aborted) inputRef.current.setText(value);
+ },
getSelection: () => inputRef.current.getSelection(),
onListening: () => {
- if (startingRef.current || runRef.current) setListening(true);
+ if (!abort.signal.aborted && (startingRef.current || runRef.current)) setListening(true);
},
onError: (message) => {
+ if (abort.signal.aborted) return;
startingRef.current = false;
runRef.current = null;
setListening(false);
setError(message);
},
});
- if (!startingRef.current) {
+ if (abort.signal.aborted || !startingRef.current) {
await run.cancel();
return;
}
@@ -128,6 +141,7 @@ export function useAskCardVoice(input: {
startingRef.current = false;
setListening(true);
} catch (error) {
+ if (abort.signal.aborted) return;
startingRef.current = false;
setListening(false);
// Stopping during startup is a normal outcome, not a failure.
@@ -137,7 +151,10 @@ export function useAskCardVoice(input: {
} finally {
if (startAbortRef.current === abort) startAbortRef.current = null;
}
- })();
+ })().finally(() => {
+ if (startingPromiseRef.current === starting) startingPromiseRef.current = null;
+ });
+ startingPromiseRef.current = starting;
}, [stop]);
return { listening, error, toggle, stop };