Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
Merge upstream/main into ask-card-stop-submit
zcinta0514ff6fbed09806
diff --cc apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsxindex 2227c0b45b,45988006bd..54054a7e7f--- a/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx+++ b/apps/desktop/src/renderer/__tests__/askUserQuestionVoice.test.tsx@@@ -238,86 -259,201 +266,271 @@@ describe('AskUserQuestion voice entry'await waitFor(() => expect(start).toHaveBeenCalledOnce());// Switching questions mid-dictation would write the rest into another answer.- for (const label of ['跳过', '下一题', '提交']) {- const button = view.queryByText(label);- if (button?.closest('button')) {- expect((button.closest('button') as HTMLButtonElement).disabled).toBe(true);- }- }expect((view.getByText('跳过').closest('button') as HTMLButtonElement).disabled).toBe(true);});++ // #6785: the old card disabled submit until the user stopped the mic.+ // Unit: real card actions must wait for the final transcript, submit once,+ // and leave failures editable. Existing cases only tested the voice entry.+ it.each(['click', 'enter'] as const)('stops and submits the final answer once via %s', async (action) => {+ let finish!: (ok: boolean) => void;+ stop.mockImplementationOnce(() => new Promise<boolean>((resolve) => { finish = resolve; }));+ const { view, onAnswer } = renderAsk([{ question: '补充一句' }]);+ fireEvent.click(view.getByTestId('ask-user-voice'));+ const submit = view.getByText('提交').closest('button')!;+ expect(submit.disabled).toBe(false);+ if (action === 'click') fireEvent.click(submit);+ else fireEvent.keyDown(view.getByPlaceholderText('请输入回答…'), { key: 'Enter' });+ fireEvent.click(submit);+ expect(stop).toHaveBeenCalledOnce();+ expect(onAnswer).not.toHaveBeenCalled();+ await act(async () => finish(true));+ expect(onAnswer).toHaveBeenCalledExactlyOnceWith('req-voice', { '补充一句': '完整回答' });+ });++ it('keeps navigation locked after done until stop finishes restoring audio', async () => {+ idleBeforeStop = true;+ let finish!: (ok: boolean) => void;+ stop.mockImplementationOnce(() => new Promise<boolean>((resolve) => { finish = resolve; }));+ const { view, onAnswer } = renderAsk([{ question: '选哪些?', multiSelect: true, options: [{ label: '甲' }, { label: '乙' }] }]);+ fireEvent.click(view.getByTestId('ask-user-voice'));+ fireEvent.click(view.getByText('提交'));+ const submit = view.getByRole('button', { name: '提交' });+ expect(submit.getAttribute('aria-busy')).toBe('true');+ expect((view.getByText('跳过').closest('button') as HTMLButtonElement).disabled).toBe(true);+ fireEvent.click(view.getByText('乙'));+ fireEvent.keyDown(window, { key: 'Escape' });+ fireEvent.keyDown(window, { key: '2' });+ expect(onAnswer).not.toHaveBeenCalled();+ await act(async () => finish(true));+ expect(onAnswer).toHaveBeenCalledExactlyOnceWith('req-voice', { '选哪些?': JSON.stringify(['完整回答']) });+ });++ it('keeps the answer editable instead of submitting after a failed stop', async () => {+ stop.mockResolvedValueOnce(false);+ const { view, onAnswer } = renderAsk([{ question: '补充一句' }]);+ fireEvent.change(view.getByPlaceholderText('请输入回答…'), { target: { value: '保留文字' } });+ fireEvent.click(view.getByTestId('ask-user-voice'));+ await act(async () => fireEvent.click(view.getByText('提交')));+ expect(onAnswer).not.toHaveBeenCalled();+ expect((view.getByPlaceholderText('请输入回答…') as HTMLTextAreaElement).value).toBe('保留文字');+ });++ it('does not submit an empty finalized transcript', async () => {+ finalTranscript = '';+ const { view, onAnswer } = renderAsk([{ question: '补充一句' }]);+ fireEvent.click(view.getByTestId('ask-user-voice'));+ await act(async () => fireEvent.click(view.getByText('提交')));+ expect(stop).toHaveBeenCalledOnce();+ expect(onAnswer).not.toHaveBeenCalled();+ expect(view.getByText('提交').closest('button')!.disabled).toBe(true);+ });++ it('does not submit a late result after leaving the card', async () => {+ let finish!: (ok: boolean) => void;+ stop.mockImplementationOnce(() => new Promise<boolean>((resolve) => { finish = resolve; }));+ const { view, onAnswer } = renderAsk([{ question: '补充一句' }]);+ fireEvent.click(view.getByTestId('ask-user-voice'));+ fireEvent.click(view.getByText('提交'));+ view.unmount();+ await act(async () => finish(true));+ expect(onAnswer).not.toHaveBeenCalled();+ });++ it('encodes checked options together with the final voice answer', async () => {+ const { view, onAnswer } = renderAsk([{ question: '选哪些?', multiSelect: true, options: [{ label: '甲' }, { label: '乙' }] }]);+ fireEvent.click(view.getByText('甲'));+ fireEvent.click(view.getByTestId('ask-user-voice'));+ await act(async () => fireEvent.click(view.getByText('提交')));+ expect(onAnswer).toHaveBeenCalledExactlyOnceWith('req-voice', { '选哪些?': JSON.stringify(['甲', '完整回答']) });+ });});+ describe('voice shortcut ownership across cards', () => {+ type ShortcutPayload = { id?: string; phase?: 'start' | 'tap' | 'end' };+ let previousApi: typeof window.electronAPI;+ let listeners: Set<(payload?: ShortcutPayload) => void>;+ let claimed: string[];++ beforeEach(() => {+ previousApi = window.electronAPI;+ listeners = new Set();+ claimed = [];+ window.electronAPI = {+ voiceInput: {+ onGlobalShortcutTrigger: (callback: (payload?: ShortcutPayload) => void) => {+ listeners.add(callback);+ return () => { listeners.delete(callback); };+ },+ claimGlobalShortcutTrigger: (id: string) => { claimed.push(id); },+ getDataSnapshot: () => ({ settings: { shortcut: null } }),+ },+ } as unknown as typeof window.electronAPI;+ });++ afterEach(() => {+ window.electronAPI = previousApi;+ });++ // The preload fans one trigger out to every subscriber in the window.+ const press = (payload: ShortcutPayload) => act(async () => {+ for (const listener of [...listeners]) listener(payload);+ });++ function cardProps(index: number, viewerState: 'expanded' | 'minimized' = 'expanded', preferred = false, hostVisible = true) {+ return {+ voiceShortcutPreferred: preferred,+ hostVisible,+ sessionId: `pane-${index}`,+ pending: { requestId: `req-${index}`, questions: [{ question: `第 ${index} 题?`, options: [{ label: '甲' }, { label: '乙' }] }] },+ onAnswer: vi.fn(),+ viewerState,+ onViewerStateChange: () => {},+ draft: null,+ onDraftChange: () => {},+ };+ }++ function renderPanes(+ states: Array<'expanded' | 'minimized'> = ['expanded', 'expanded'],+ preferredIndex?: number,+ hiddenIndex?: number,+ ) {+ const tree = (current: typeof states) => createElement('div', null,+ createElement('input', { 'data-testid': 'sidebar-search' }),+ createElement('button', { 'data-testid': 'sidebar-tab', type: 'button' }, 'tab'),+ ...current.map((state, index) => createElement('section', { key: index, 'data-testid': `pane-${index}` },+ createElement(AskUserQuestionPrompt, cardProps(index, state, index === preferredIndex, index !== hiddenIndex)))),+ );+ const view = render(tree(states));+ const voiceIn = (index: number) => within(view.getByTestId(`pane-${index}`)).getByTestId('ask-user-voice');+ return { view, voiceIn, rerender: (next: typeof states) => view.rerender(tree(next)) };+ }++ it('gives the press to the most recently shown card when focus is in neither', async () => {+ const { voiceIn } = renderPanes();+ await press({ id: 'p1', phase: 'start' });++ await waitFor(() => expect(start).toHaveBeenCalledOnce());+ expect(voiceIn(1).getAttribute('aria-pressed')).toBe('true');+ expect(voiceIn(0).getAttribute('aria-pressed')).toBe('false');+ expect(claimed).toEqual(['p1']);+ });++ it('gives an unfocused press to the Orca lead card over a newer worker card', async () => {+ const { voiceIn } = renderPanes(['expanded', 'expanded'], 0);+ await press({ id: 'p1', phase: 'start' });++ await waitFor(() => expect(start).toHaveBeenCalledOnce());+ expect(voiceIn(0).getAttribute('aria-pressed')).toBe('true');+ expect(voiceIn(1).getAttribute('aria-pressed')).toBe('false');+ });++ it('still gives the press to a focused worker card over the Orca lead card', async () => {+ const { view, voiceIn } = renderPanes(['expanded', 'expanded'], 0);+ within(view.getByTestId('pane-1')).getByRole('button', { name: /输入其他回答…/ }).focus();+ await press({ id: 'p1', phase: 'start' });++ await waitFor(() => expect(start).toHaveBeenCalledOnce());+ expect(voiceIn(1).getAttribute('aria-pressed')).toBe('true');+ expect(voiceIn(0).getAttribute('aria-pressed')).toBe('false');+ });++ it('follows keyboard focus to an older card', async () => {+ const { view, voiceIn } = renderPanes();+ within(view.getByTestId('pane-0')).getByRole('button', { name: /输入其他回答…/ }).focus();+ await press({ id: 'p1', phase: 'start' });++ await waitFor(() => expect(start).toHaveBeenCalledOnce());+ expect(voiceIn(0).getAttribute('aria-pressed')).toBe('true');+ expect(voiceIn(1).getAttribute('aria-pressed')).toBe('false');+ });++ it('still acts on a local key press that a sibling composer already prevented', async () => {+ const previous = window.electronAPI;+ window.electronAPI = {+ ...previous,+ voiceInput: { ...previous.voiceInput, getDataSnapshot: () => ({ settings: { shortcut: { code: 'F9', key: 'F9', modifiers: { meta: false, ctrl: false, alt: false, shift: false, fn: false } } } }) },+ } as unknown as typeof window.electronAPI;+ // Registered before the cards mount, like a composer in an earlier pane:+ // it runs first and prevents the default for every matching press.+ const siblingComposer = (event: Event) => event.preventDefault();+ window.addEventListener('keydown', siblingComposer, true);+ try {+ const { view, voiceIn } = renderPanes();+ const focused = within(view.getByTestId('pane-0')).getByRole('button', { name: /输入其他回答…/ });+ focused.focus();+ await act(async () => {+ focused.dispatchEvent(new KeyboardEvent('keydown', { key: 'F9', code: 'F9', bubbles: true, cancelable: true }));+ });+ expect(voiceIn(0).getAttribute('aria-pressed')).toBe('true');+ } finally {+ window.removeEventListener('keydown', siblingComposer, true);+ window.electronAPI = previous;+ }+ });++ it('leaves the press unclaimed when another text field has focus', async () => {+ const { view } = renderPanes();+ view.getByTestId('sidebar-search').focus();+ await press({ id: 'p1', phase: 'start' });++ expect(start).not.toHaveBeenCalled();+ // Unclaimed, the main process falls back to the global voice overlay.+ expect(claimed).toEqual([]);+ });++ it('leaves the press unclaimed when a button outside every card has focus', async () => {+ const { view } = renderPanes();+ view.getByTestId('sidebar-tab').focus();+ await press({ id: 'p1', phase: 'start' });++ expect(start).not.toHaveBeenCalled();+ expect(claimed).toEqual([]);+ });++ it('never gives the press to a card whose host view is hidden', async () => {+ // Pane 1 is newest but sits in a collapsed panel; its box still exists.+ const { voiceIn } = renderPanes(['expanded', 'expanded'], undefined, 1);+ await press({ id: 'p1', phase: 'start' });++ await waitFor(() => expect(start).toHaveBeenCalledOnce());+ expect(voiceIn(0).getAttribute('aria-pressed')).toBe('true');+ expect(voiceIn(1).getAttribute('aria-pressed')).toBe('false');+ });++ it('lets the card that is recording take the next press, wherever focus is', async () => {+ const { view, voiceIn } = renderPanes();+ fireEvent.click(voiceIn(0));+ await waitFor(() => expect(start).toHaveBeenCalledOnce());+ view.getByTestId('sidebar-search').focus();++ await press({ id: 'p2', phase: 'start' });+ expect(stop).toHaveBeenCalledOnce();+ expect(start).toHaveBeenCalledOnce();+ expect(voiceIn(1).getAttribute('aria-pressed')).toBe('false');+ });++ it('stops a minimized card that is recording, and never starts one', async () => {+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });+ const { voiceIn, rerender } = renderPanes(['expanded']);+ // Fake performance starts at zero; pass the initial 250ms shortcut guard.+ await act(async () => { await vi.advanceTimersByTimeAsync(300); });+ fireEvent.click(voiceIn(0));+ expect(start).toHaveBeenCalledOnce();++ rerender(['minimized']);+ await press({ id: 'p1', phase: 'start' });+ expect(stop).toHaveBeenCalledOnce();+ expect(claimed).toEqual(['p1']);++ await act(async () => { await vi.advanceTimersByTimeAsync(300); });+ await press({ id: 'p2', phase: 'start' });+ expect(start).toHaveBeenCalledOnce();+ expect(claimed).toEqual(['p1']);+ });+ });+describe('companion voice mode integration', () => {beforeEach(async () => { await i18n.changeLanguage('en'); });function setup(question: Partial<AskUserQuestionItem> = {}) {diff --cc apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsxindex bf5b482ed2,af28454234..bad0373a9e--- a/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx+++ b/apps/desktop/src/renderer/components/new-chat/AskUserQuestionPrompt.tsx@@@ -948,25 -940,32 +975,34 @@@ function AskUserQuestionForm(<buttontype="button"className={cn(- 'flex min-w-0 flex-1 items-center justify-between py-[6px] text-left',+ 'flex min-w-0 flex-1 items-center gap-3 py-[6px] text-left','transition-colors hover:bg-[var(--ask-option-hover)]',)}+ disabled={voiceLocked}onClick={handleCustomOptionClick}>- <div className="flex items-center gap-3">- {isMultiSelect && (- <div className="h-[18px] w-[18px] shrink-0 rounded-[4px] border-[1.5px] border-[var(--ask-checkbox-border)]" />- )}- <span className="text-14 italic text-[var(--ask-option-custom)]">- {t('chat.askUserQuestion.customAnswer')}- </span>- </div>- <div className="ml-3 flex h-[28px] w-[28px] shrink-0 items-center justify-center rounded-[8px] bg-[var(--ask-badge-bg)] text-13 font-medium text-[var(--ask-badge-text)]">- {options.length + 1}- </div>+ {isMultiSelect && (+ <div className="h-[18px] w-[18px] shrink-0 rounded-[4px] border-[1.5px] border-[var(--ask-checkbox-border)]" />+ )}+ <span className="text-14 italic text-[var(--ask-option-custom)]">+ {t('chat.askUserQuestion.customAnswer')}+ </span>+ {/* The visible badge moved out of the button; keep its number in the+ button for screen readers, as the option rows do. */}+ <span className="sr-only">{options.length + 1}</span></button>- {!voiceBridge && (<AskUserVoiceButton label={voiceButtonLabel} busy={dictation.isBusy} onClick={beginVoiceInput} />)}+ {!voiceBridge && (<AskUserVoiceButton label={voiceButtonLabel} busy={dictation.isBusy} disabled={voiceSubmitting} onClick={beginVoiceInput} />)}+ {/* Pointer shortcut only: the row button above already carries the+ action and its number for keyboard and screen-reader users. */}+ <div+ aria-hidden="true"+ data-testid="ask-user-custom-badge"+ onClick={handleCustomOptionClick}+ className="flex h-[28px] w-[28px] shrink-0 cursor-pointer items-center justify-center rounded-[8px] bg-[var(--ask-badge-bg)] text-13 font-medium text-[var(--ask-badge-text)]"+ >+ {options.length + 1}+ </div>++</div>)}</div>