Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): helper 稳定后再重置重启预算,耗尽时卸掉占位
Dash919734a7f663
diff --git a/apps/desktop/src/main/voice-input/MacModifierShortcutListener.ts b/apps/desktop/src/main/voice-input/MacModifierShortcutListener.tsindex 50024f5927..b6e3665df9 100644--- a/apps/desktop/src/main/voice-input/MacModifierShortcutListener.ts+++ b/apps/desktop/src/main/voice-input/MacModifierShortcutListener.ts@@ -32,6 +32,7 @@ const LISTENER_START_TIMEOUT_MS = 1_500;const LISTENER_RESTART_MAX_ATTEMPTS = 3;const LISTENER_RESTART_BASE_DELAY_MS = 1_000;const LISTENER_RESTART_MAX_DELAY_MS = 5_000;+const LISTENER_RESTART_STABLE_MS = 10_000;type ListenerTriggerPhase = 'tap' | 'start' | 'end';@@ -67,6 +68,7 @@ type ListenerPayload = {type MacModifierShortcutListenerOptions = {onTrigger: (phase: ListenerTriggerPhase) => void;onKeys?: (keys: string[]) => void;+ onRestartLimitReached?: () => void;};type ListenerProcess = ChildProcessByStdio<null, Readable, Readable>;@@ -110,6 +112,7 @@ export class MacModifierShortcutListener {private pressedKeys = new Set<string>();private startTimer: NodeJS.Timeout | null = null;private restartTimer: NodeJS.Timeout | null = null;+ private stableTimer: NodeJS.Timeout | null = null;private restartAttempts = 0;private triggered = false;private holdThresholdReached = false;@@ -148,6 +151,7 @@ export class MacModifierShortcutListener {}this.shortcut = shortcut;this.clearRestartTimer();+ this.clearStableTimer();this.restartAttempts = 0;this.endActiveTriggerIfNeeded();this.resetState();@@ -160,6 +164,7 @@ export class MacModifierShortcutListener {async startKeyCapture(): Promise<ListenerStartResult> {this.clearRestartTimer();+ this.clearStableTimer();this.restartAttempts = 0;this.resetState();if (this.child) {@@ -218,6 +223,7 @@ export class MacModifierShortcutListener {releaseShortcutKeepingCapture(): void {this.shortcut = null;this.clearRestartTimer();+ this.clearStableTimer();this.restartAttempts = 0;this.endActiveTriggerIfNeeded();this.resetState();@@ -243,6 +249,7 @@ export class MacModifierShortcutListener {this.child = null;this.ready = false;this.clearRestartTimer();+ this.clearStableTimer();this.restartAttempts = 0;if (child && !child.killed) child.kill();void this.startChildProcess({ preserveShortcutOnFailure: true })@@ -273,6 +280,7 @@ export class MacModifierShortcutListener {this.ready = false;this.shortcut = null;this.clearRestartTimer();+ this.clearStableTimer();this.restartAttempts = 0;this.endActiveTriggerIfNeeded();this.resetState();@@ -339,7 +347,10 @@ export class MacModifierShortcutListener {// 录制框重开。就绪状态同理:只认当前 child 报的 ready。const stale = generation !== this.startGeneration || this.child !== child;const outcome = stale ? supersededStart() : result;- if (result.ok && !stale) this.ready = true;+ if (result.ok && !stale) {+ this.ready = true;+ this.armStableTimer();+ }if (!result.ok && this.child === child) {this.child = null;this.ready = false;@@ -392,6 +403,7 @@ export class MacModifierShortcutListener {if (this.child === child) {this.child = null;this.ready = false;+ this.clearStableTimer();this.endActiveTriggerIfNeeded();this.resetState();}@@ -589,6 +601,8 @@ export class MacModifierShortcutListener {signal,shortcut: shortcutLabel,});+ this.shortcut = null;+ this.options.onRestartLimitReached?.();return;}this.restartAttempts += 1;@@ -617,7 +631,6 @@ export class MacModifierShortcutListener {this.scheduleRestart(null, null);return;}- this.restartAttempts = 0;log.info('modifier shortcut listener restarted', {shortcut: this.shortcut ? getShortcutLogLabel(this.shortcut) : null,});@@ -638,6 +651,20 @@ export class MacModifierShortcutListener {clearTimeout(this.restartTimer);this.restartTimer = null;}++ private armStableTimer(): void {+ this.clearStableTimer();+ this.stableTimer = setTimeout(() => {+ this.stableTimer = null;+ this.restartAttempts = 0;+ }, LISTENER_RESTART_STABLE_MS);+ }++ private clearStableTimer(): void {+ if (!this.stableTimer) return;+ clearTimeout(this.stableTimer);+ this.stableTimer = null;+ }}export async function getMacInputMonitoringPermissionSnapshot(): Promise<MacInputMonitoringPermissionSnapshot> {diff --git a/apps/desktop/src/main/voice-input/WindowsFunctionKeyShortcutListener.ts b/apps/desktop/src/main/voice-input/WindowsFunctionKeyShortcutListener.tsindex 3559214792..9de2934917 100644--- a/apps/desktop/src/main/voice-input/WindowsFunctionKeyShortcutListener.ts+++ b/apps/desktop/src/main/voice-input/WindowsFunctionKeyShortcutListener.ts@@ -37,6 +37,7 @@ type ListenerProcess = ChildProcessByStdio<null, Readable, Readable>;export interface WindowsFunctionKeyShortcutListenerOptions {onTrigger: (phase: ShortcutHoldPhase) => void;+ onRestartLimitReached?: () => void;}function supersededStart(): WindowsFunctionKeyListenerStartResult {@@ -58,7 +59,7 @@ export class WindowsFunctionKeyShortcutListener {private stableTimer: NodeJS.Timeout | null = null;private readonly phaseController: ShortcutHoldPhaseController;- constructor(options: WindowsFunctionKeyShortcutListenerOptions) {+ constructor(private readonly options: WindowsFunctionKeyShortcutListenerOptions) {this.phaseController = new ShortcutHoldPhaseController({ onTrigger: options.onTrigger });}@@ -276,6 +277,8 @@ export class WindowsFunctionKeyShortcutListener {if (!this.shortcutCode || this.restartTimer) return;if (this.restartAttempts >= RESTART_MAX_ATTEMPTS) {log.warn('Windows function key listener restart limit reached', { code, exitCode, signal });+ this.shortcutCode = null;+ this.options.onRestartLimitReached?.();return;}this.restartAttempts += 1;diff --git a/apps/desktop/src/main/voice-input/__tests__/WindowsFunctionKeyShortcutListener.test.ts b/apps/desktop/src/main/voice-input/__tests__/WindowsFunctionKeyShortcutListener.test.tsindex 9844e26592..1985348fa4 100644--- a/apps/desktop/src/main/voice-input/__tests__/WindowsFunctionKeyShortcutListener.test.ts+++ b/apps/desktop/src/main/voice-input/__tests__/WindowsFunctionKeyShortcutListener.test.ts@@ -205,7 +205,11 @@ describe('WindowsFunctionKeyShortcutListener', () => {vi.useFakeTimers();const { WindowsFunctionKeyShortcutListener } =await import('../WindowsFunctionKeyShortcutListener.js');- const listener = new WindowsFunctionKeyShortcutListener({ onTrigger: vi.fn() });+ const onRestartLimitReached = vi.fn();+ const listener = new WindowsFunctionKeyShortcutListener({+ onTrigger: vi.fn(),+ onRestartLimitReached,+ });const starting = listener.setShortcut(f2Shortcut);await vi.advanceTimersByTimeAsync(0);mocks.spawnedChildren.at(-1)?.stdoutData('{"type":"ready"}\n');@@ -221,6 +225,7 @@ describe('WindowsFunctionKeyShortcutListener', () => {mocks.spawnedChildren.at(-1)?.handlers.get('exit')?.(1, null);await vi.advanceTimersByTimeAsync(5_000);expect(mocks.spawn).toHaveBeenCalledTimes(4);+ expect(onRestartLimitReached).toHaveBeenCalledTimes(1);listener.stop();});});diff --git a/apps/desktop/src/main/voice-input/__tests__/globalShortcut.test.ts b/apps/desktop/src/main/voice-input/__tests__/globalShortcut.test.tsindex c034cf201a..585b97a9fa 100644--- a/apps/desktop/src/main/voice-input/__tests__/globalShortcut.test.ts+++ b/apps/desktop/src/main/voice-input/__tests__/globalShortcut.test.ts@@ -122,9 +122,11 @@ const mocks = vi.hoisted(() => {const listenerOptions: {onKeys?: (keys: string[]) => void;onTrigger?: (phase: 'tap' | 'start' | 'end') => void;+ onRestartLimitReached?: () => void;} = {};const windowsListenerOptions: {onTrigger?: (phase: 'tap' | 'start' | 'end') => void;+ onRestartLimitReached?: () => void;} = {};const registerShortcut = vi.fn((accelerator: string) => {void accelerator;@@ -241,6 +243,9 @@ vi.mock('../MacModifierShortcutListener.js', () => ({}) => {mocks.listenerOptions.onKeys = options?.onKeys;mocks.listenerOptions.onTrigger = options?.onTrigger;+ mocks.listenerOptions.onRestartLimitReached = (+ options as { onRestartLimitReached?: () => void }+ )?.onRestartLimitReached;return {setShortcut: mocks.modifierSetShortcut,isRunning: mocks.modifierIsRunning,@@ -263,6 +268,9 @@ vi.mock('../WindowsFunctionKeyShortcutListener.js', () => ({.fn().mockImplementation((options: { onTrigger?: (phase: 'tap' | 'start' | 'end') => void }) => {mocks.windowsListenerOptions.onTrigger = options?.onTrigger;+ mocks.windowsListenerOptions.onRestartLimitReached = (+ options as { onRestartLimitReached?: () => void }+ )?.onRestartLimitReached;return {setShortcut: mocks.windowsSetShortcut,isReady: mocks.windowsIsReady,@@ -463,6 +471,26 @@ describe('voice input global shortcut registration', () => {expect(mocks.windowsReleaseActiveTrigger).toHaveBeenCalledTimes(1);});+ it('releases a reserved Windows F-key when the helper restart budget is exhausted', async () => {+ setPlatform('win32');+ const { registerGlobalVoiceInputIpc } = await import('../global.js');+ registerGlobalVoiceInputIpc(mocks.ipcDeps);+ const setShortcut = mocks.handlers.get('voice-input:global-shortcut:set');+ const f16Shortcut: VoiceInputShortcut = {+ trigger: 'keyboard',+ code: 'F16',+ key: 'F16',+ modifiers: { meta: false, ctrl: false, alt: false, shift: false, fn: false },+ };++ mocks.setStoredShortcut(f16Shortcut);+ await setShortcut?.({}, f16Shortcut);+ expect(mocks.registeredShortcuts.has('F16')).toBe(true);++ mocks.windowsListenerOptions.onRestartLimitReached?.();+ expect(mocks.registeredShortcuts.has('F16')).toBe(false);+ });+it('stops both native listeners through the quit disposer, not will-quit', async () => {const { registerGlobalVoiceInputIpc } = await import('../global.js');registerGlobalVoiceInputIpc(mocks.ipcDeps);diff --git a/apps/desktop/src/main/voice-input/global.ts b/apps/desktop/src/main/voice-input/global.tsindex 9272e11c9d..060e04b355 100644--- a/apps/desktop/src/main/voice-input/global.ts+++ b/apps/desktop/src/main/voice-input/global.ts@@ -126,8 +126,20 @@ function handleNativeGlobalShortcutPhase(phase: GlobalVoiceInputShortcutPhase):}}+function abandonNativeShortcutAfterRestartLimit(): void {+ if (registeredAccelerator) {+ globalShortcut.unregister(registeredAccelerator);+ registeredAccelerator = null;+ }+ registeredShortcut = null;+ registeredNativeShortcutLabel = null;+ registeredNativeShortcutKey = null;+ notifyPendingShortcutRecoveryFailed();+}+const macModifierShortcutListener = new MacModifierShortcutListener({onTrigger: handleNativeGlobalShortcutPhase,+ onRestartLimitReached: abandonNativeShortcutAfterRestartLimit,onKeys: (keys) => {for (const webContentsId of Array.from(modifierShortcutRecordingWebContentsIds)) {const window = BrowserWindow.getAllWindows()@@ -143,6 +155,7 @@ const macModifierShortcutListener = new MacModifierShortcutListener({const windowsFunctionKeyShortcutListener = new WindowsFunctionKeyShortcutListener({onTrigger: handleNativeGlobalShortcutPhase,+ onRestartLimitReached: abandonNativeShortcutAfterRestartLimit,});export function releaseActiveGlobalVoiceInputShortcut(): void {