Files
Netcatty/electron/bridges/windowAttention.cjs
T
b2503ff6ba feat(ai): alert when an External MCP approval is waiting (#3771)
* feat: alert when an external MCP approval is waiting

Show a system notification and request taskbar or Dock attention so confirm-mode approvals are not missed before the existing timeout.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: stop MCP approval attention when a later window focuses

Watch app browser-window-focus while a flash or dock bounce is active so a
window created after the alert starts can clear it.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-20 11:54:44 +08:00

123 lines
3.5 KiB
JavaScript

"use strict";
const DEFAULT_MODE = "unfocused";
function normalizeExternalMcpApprovalAlertMode(value) {
return value === "off" || value === "always" || value === "unfocused" ? value : DEFAULT_MODE;
}
function createWindowAttentionController({ app, getWindows, platform }) {
let bounceId = -1;
let flashing = false;
let flashedWin = null;
let watchingFocus = false;
function onBrowserWindowFocus() {
stop();
}
function startFocusWatch() {
if (watchingFocus || typeof app?.on !== "function") return;
app.on("browser-window-focus", onBrowserWindowFocus);
watchingFocus = true;
}
function stopFocusWatch() {
if (!watchingFocus) return;
watchingFocus = false;
try { app.removeListener("browser-window-focus", onBrowserWindowFocus); } catch { /* ignore */ }
}
function stop() {
stopFocusWatch();
if (flashing && flashedWin && !flashedWin.isDestroyed?.() && typeof flashedWin.flashFrame === "function") {
try { flashedWin.flashFrame(false); } catch { /* ignore */ }
}
flashing = false;
flashedWin = null;
if (bounceId >= 0) {
try { app?.dock?.cancelBounce?.(bounceId); } catch { /* ignore */ }
bounceId = -1;
}
}
function request() {
const windows = (typeof getWindows === "function" ? getWindows() : [])
.filter((win) => win && !win.isDestroyed?.());
if (windows.length === 0) return { requested: false, reason: "no-window" };
if (windows.some((win) => typeof win.isFocused === "function" && win.isFocused())) {
return { requested: false, reason: "focused" };
}
if (platform === "darwin") {
if (bounceId >= 0) return { requested: true, kind: "bounce" };
const id = app?.dock?.bounce?.("informational");
if (typeof id !== "number" || id < 0) return { requested: false, reason: "unavailable" };
bounceId = id;
startFocusWatch();
return { requested: true, kind: "bounce" };
}
const win = windows[0];
if (typeof win.flashFrame !== "function") return { requested: false, reason: "unavailable" };
win.flashFrame(true);
flashing = true;
flashedWin = win;
startFocusWatch();
return { requested: true, kind: "flash" };
}
return { request, stop };
}
let mode = DEFAULT_MODE;
let controller = null;
function installWindowAttention(options) {
controller?.stop?.();
controller = createWindowAttentionController(options);
return controller;
}
function setExternalMcpApprovalAlertMode(value) {
mode = normalizeExternalMcpApprovalAlertMode(value);
if (mode === "off") controller?.stop?.();
return mode;
}
function getExternalMcpApprovalAlertMode() {
return mode;
}
function syncExternalMcpApprovalAttention(pendingCount) {
if (!controller) return { attention: false, reason: "uninstalled" };
if (!(pendingCount > 0) || mode === "off") {
controller.stop();
return { attention: false };
}
return controller.request();
}
function resetWindowAttentionForTests() {
controller?.stop?.();
controller = null;
mode = DEFAULT_MODE;
}
function registerExternalMcpApprovalAlertHandlers(ipcMain) {
ipcMain.handle("netcatty:mcp:approval-alert-mode", (_event, payload) => {
return { mode: setExternalMcpApprovalAlertMode(payload?.mode) };
});
}
module.exports = {
normalizeExternalMcpApprovalAlertMode,
createWindowAttentionController,
installWindowAttention,
setExternalMcpApprovalAlertMode,
getExternalMcpApprovalAlertMode,
syncExternalMcpApprovalAttention,
resetWindowAttentionForTests,
registerExternalMcpApprovalAlertHandlers,
};