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(desktop): Codex Micro 在 Cindy 外单击语音键不再立刻结束 (#3091)

Dash4a5a95a3075a
diff --git a/apps/desktop/src/main/worklouder-codex/__tests__/systemFrontmostInput.test.ts b/apps/desktop/src/main/worklouder-codex/__tests__/systemFrontmostInput.test.ts
index 637af4ec35..83d1590cbd 100644
--- a/apps/desktop/src/main/worklouder-codex/__tests__/systemFrontmostInput.test.ts
+++ b/apps/desktop/src/main/worklouder-codex/__tests__/systemFrontmostInput.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it, vi } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { joystickScrollSpeed } from '../../../shared/workLouderCodexScroll.js';
import {
@@ -10,15 +10,67 @@ import {
} from '../systemFrontmostInput.js';
describe('createWorkLouderCodexSystemFrontmostInput', () => {
- it('maps a held microphone to the global overlay start/end phases', () => {
- const triggerVoice = vi.fn();
- const input = createWorkLouderCodexSystemFrontmostInput({ triggerVoice });
+ describe('microphone key outside Cindy', () => {
+ const HOLD_DELAY_MS = 450;
- expect(input.handle({ type: 'voice', phase: 'press' })).toBe(true);
- expect(input.handle({ type: 'voice', phase: 'release' })).toBe(true);
+ beforeEach(() => {
+ vi.useFakeTimers();
+ });
+
+ afterEach(() => {
+ vi.useRealTimers();
+ });
+
+ function createVoiceInput() {
+ const triggerVoice = vi.fn();
+ return {
+ triggerVoice,
+ input: createWorkLouderCodexSystemFrontmostInput({
+ triggerVoice,
+ holdDelayMs: HOLD_DELAY_MS,
+ createScrollPump: () => ({ setSpeed: vi.fn(), stop: vi.fn() }),
+ }),
+ };
+ }
+
+ it('keeps the overlay recording after a short tap', () => {
+ const { triggerVoice, input } = createVoiceInput();
+
+ expect(input.handle({ type: 'voice', phase: 'press' })).toBe(true);
+ vi.advanceTimersByTime(HOLD_DELAY_MS - 1);
+ expect(input.handle({ type: 'voice', phase: 'release' })).toBe(true);
+
+ expect(triggerVoice.mock.calls.map(([phase]) => phase)).toEqual(['start', 'tap']);
+ });
- expect(triggerVoice).toHaveBeenNthCalledWith(1, 'start');
- expect(triggerVoice).toHaveBeenNthCalledWith(2, 'end');
+ it('submits when the key is held past the hold threshold', () => {
+ const { triggerVoice, input } = createVoiceInput();
+
+ expect(input.handle({ type: 'voice', phase: 'press' })).toBe(true);
+ vi.advanceTimersByTime(HOLD_DELAY_MS);
+ expect(input.handle({ type: 'voice', phase: 'release' })).toBe(true);
+
+ expect(triggerVoice.mock.calls.map(([phase]) => phase)).toEqual(['start', 'end']);
+ });
+
+ it('ignores repeated key-down messages while the key is held', () => {
+ const { triggerVoice, input } = createVoiceInput();
+
+ input.handle({ type: 'voice', phase: 'press' });
+ input.handle({ type: 'voice', phase: 'press' });
+ vi.advanceTimersByTime(HOLD_DELAY_MS);
+ input.handle({ type: 'voice', phase: 'release' });
+
+ expect(triggerVoice.mock.calls.map(([phase]) => phase)).toEqual(['start', 'end']);
+ });
+
+ it('drops a release that has no matching press', () => {
+ const { triggerVoice, input } = createVoiceInput();
+
+ expect(input.handle({ type: 'voice', phase: 'release' })).toBe(true);
+
+ expect(triggerVoice).not.toHaveBeenCalled();
+ });
});
it('sends Return for the send key', () => {
diff --git a/apps/desktop/src/main/worklouder-codex/systemFrontmostInput.ts b/apps/desktop/src/main/worklouder-codex/systemFrontmostInput.ts
index 15a0a2b527..01be591c00 100644
--- a/apps/desktop/src/main/worklouder-codex/systemFrontmostInput.ts
+++ b/apps/desktop/src/main/worklouder-codex/systemFrontmostInput.ts
@@ -4,6 +4,7 @@ import { promisify } from 'node:util';
import type { ChildProcess } from 'node:child_process';
import { createLogger } from '../logger.js';
+import { ShortcutHoldPhaseController } from '../voice-input/ShortcutHoldPhaseController.js';
import {
runMacTextInsertionHelperCommand,
spawnMacTextInsertionHelper,
@@ -279,6 +280,7 @@ export function createWorkLouderCodexSystemFrontmostInput(deps?: {
runner?: SystemFrontmostInputRunner;
triggerVoice?: (phase: 'start' | 'tap' | 'end') => void;
now?: () => number;
+ holdDelayMs?: number;
createScrollPump?: (runner: SystemFrontmostInputRunner) => SystemFrontmostScrollPump;
spawnHoldScroll?: () => Promise<HoldScrollChild>;
}) {
@@ -292,22 +294,23 @@ export function createWorkLouderCodexSystemFrontmostInput(deps?: {
? createMacHoldScrollPump(fallbackPump, deps?.spawnHoldScroll)
: fallbackPump);
let scrolling = false;
- let voicePressed = false;
+ // Outside Cindy the microphone key must feel like the system shortcut: a short
+ // tap leaves the overlay recording (a second tap submits), and only a real
+ // hold submits on release. Sending `end` on every release made the key look
+ // broken unless it was held.
+ //
+ // No separate suspend/wake reset here: unlike the native shortcut listeners,
+ // the keypad owner forces a `release` when the layout stops owning the key or
+ // its task slots are suspended (`releaseHeldHardwareGestures`).
+ const voicePhases = new ShortcutHoldPhaseController({
+ holdDelayMs: deps?.holdDelayMs,
+ onTrigger: triggerVoice,
+ });
return {
handle(action: WorkLouderCodexRendererAction): boolean {
if (action.type === 'voice') {
- if (action.phase === 'press') {
- if (!voicePressed) {
- voicePressed = true;
- triggerVoice('start');
- }
- return true;
- }
- if (voicePressed) {
- voicePressed = false;
- triggerVoice('end');
- }
+ voicePhases.setPressed(action.phase === 'press');
return true;
}
if (action.type === 'command' && action.commandId === 'composer.submit') {