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): 将 Forge OIDC 确认投到主窗口

fmfsaisai0cea5714acc4
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/forgeOidcInstallConfirmBridge.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/forgeOidcInstallConfirmBridge.test.ts
index 247f66902b..8e9c33c2e0 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/forgeOidcInstallConfirmBridge.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/forgeOidcInstallConfirmBridge.test.ts
@@ -1,7 +1,8 @@
-import { describe, expect, it } from 'vitest';
+import { describe, expect, it, vi } from 'vitest';
import type { GhostManifest } from '../../../shared/ghost';
import {
+ createForgeOidcInstallMainWindowSender,
ForgeOidcInstallConfirmBridge,
forgeOidcInstallConfirmFacts,
type ForgeOidcInstallConfirmPush,
@@ -29,6 +30,47 @@ const OIDC = manifest([
},
]);
+const CONFIRM_PUSH: ForgeOidcInstallConfirmPush = {
+ requestId: 'request-1',
+ ghostId: 'acme-tool',
+ ghostName: 'Acme Tool',
+ hosts: ['api.acme.test'],
+};
+
+describe('createForgeOidcInstallMainWindowSender', () => {
+ it('辅助 sidebar 聚焦时仍只把确认投给主 App 窗口', () => {
+ const mainWindow = { kind: 'main' };
+ const focusedSidebar = { kind: 'sidebar' };
+ const send = vi.fn();
+ const sender = createForgeOidcInstallMainWindowSender({
+ getMainWindow: () => mainWindow,
+ isTrustedMainWindow: () => true,
+ send,
+ });
+
+ expect(sender(CONFIRM_PUSH)).toBe(true);
+ expect(send).toHaveBeenCalledWith(mainWindow, CONFIRM_PUSH);
+ expect(send).not.toHaveBeenCalledWith(focusedSidebar, CONFIRM_PUSH);
+ });
+
+ it('只有受信辅助窗口、没有主 App 窗口时失败关闭', () => {
+ const trustedAuxiliaryWindow = { kind: 'resource-usage' };
+ const isTrustedMainWindow = vi.fn((window: typeof trustedAuxiliaryWindow) =>
+ Boolean(window),
+ );
+ const send = vi.fn();
+ const sender = createForgeOidcInstallMainWindowSender<typeof trustedAuxiliaryWindow>({
+ getMainWindow: () => null,
+ isTrustedMainWindow,
+ send,
+ });
+
+ expect(sender(CONFIRM_PUSH)).toBe(false);
+ expect(isTrustedMainWindow).not.toHaveBeenCalled();
+ expect(send).not.toHaveBeenCalled();
+ });
+});
+
describe('forgeOidcInstallConfirmFacts', () => {
it('requires confirmation only for organization installs that declare oidc-token', () => {
expect(forgeOidcInstallConfirmFacts(OIDC, 'org')).toEqual({
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/forgeOidcInstallEntry.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/forgeOidcInstallEntry.test.ts
index 6a434235df..46331d0f6c 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/forgeOidcInstallEntry.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/forgeOidcInstallEntry.test.ts
@@ -37,4 +37,18 @@ describe('Forge OIDC install entry wiring', () => {
);
expect(body).toContain('forgeOidcInstallConfirmFacts(');
});
+
+ it('wires OIDC confirmation to the registered main App window instead of focused auxiliaries', () => {
+ const start = source.indexOf('function ensureForgeOidcInstallConfirmBridge()');
+ const end = source.indexOf('/**\n * 确认弹窗槽单例', start);
+ expect(start).toBeGreaterThanOrEqual(0);
+ expect(end).toBeGreaterThan(start);
+ const wiring = source.slice(start, end);
+
+ expect(wiring).toContain('createForgeOidcInstallMainWindowSender<BrowserWindow>({');
+ expect(wiring).toContain('getMainWindow: getDeepLinkMainWindow');
+ expect(wiring).not.toContain('BrowserWindow.getFocusedWindow');
+ expect(wiring).not.toContain('BrowserWindow.getAllWindows');
+ expect(source).not.toContain('function pickTrustedAppWindow');
+ });
});
diff --git a/apps/desktop/src/main/cindy-brain/forgeOidcInstallConfirmBridge.ts b/apps/desktop/src/main/cindy-brain/forgeOidcInstallConfirmBridge.ts
index a1dd3cb001..9f4653bb1b 100644
--- a/apps/desktop/src/main/cindy-brain/forgeOidcInstallConfirmBridge.ts
+++ b/apps/desktop/src/main/cindy-brain/forgeOidcInstallConfirmBridge.ts
@@ -33,6 +33,27 @@ export interface ForgeOidcInstallConfirmBridgeDeps {
log?: { warn: (message: string, meta?: Record<string, unknown>) => void };
}
+export interface ForgeOidcInstallMainWindowSenderDeps<TWindow> {
+ getMainWindow(): TWindow | null;
+ isTrustedMainWindow(window: TWindow): boolean;
+ send(window: TWindow, payload: ForgeOidcInstallConfirmPush): void;
+}
+
+/**
+ * 只投挂载 ForgeOidcInstallConfirmHost 的主 App 窗口。辅助窗口即使受信、
+ * 当前聚焦或排在窗口列表最前,也不能替代主窗口接这条确认请求。
+ */
+export function createForgeOidcInstallMainWindowSender<TWindow>(
+ deps: ForgeOidcInstallMainWindowSenderDeps<TWindow>,
+): (payload: ForgeOidcInstallConfirmPush) => boolean {
+ return (payload) => {
+ const mainWindow = deps.getMainWindow();
+ if (!mainWindow || !deps.isTrustedMainWindow(mainWindow)) return false;
+ deps.send(mainWindow, payload);
+ return true;
+ };
+}
+
interface PendingConfirm {
resolve: (confirmed: boolean) => void;
timeoutId: ReturnType<typeof setTimeout>;
diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.ts
index 8ea88b9f00..2965b6e543 100644
--- a/apps/desktop/src/main/cindy-brain/index.ts
+++ b/apps/desktop/src/main/cindy-brain/index.ts
@@ -161,6 +161,7 @@ import { mapGhostOauthConnectError } from './ghostOauthSetupError.js';
import { reclaimLoopbackPort } from './portReclaim.js';
import { GhostConnectionManager } from './ghostConnections.js';
import { getResolvedMainLocale, t } from '../i18n.js';
+import { getDeepLinkMainWindow } from '../deepLink.js';
import { reconcileGhostSkillLinks, removeGhostSkillLinksForRoots } from './skillSlot.js';
import { assertGhostSkillProjectionStableOwner } from '../authBoundaryQuarantine.js';
import { type GhostOwnerScope } from './ghostOwnerScope.js';
@@ -269,6 +270,7 @@ import {
initGhostConfirmDialogBridge,
} from './ghostConfirmDialogBridge.js';
import {
+ createForgeOidcInstallMainWindowSender,
forgeOidcInstallConfirmFacts,
getForgeOidcInstallConfirmBridge,
initForgeOidcInstallConfirmBridge,
@@ -2939,27 +2941,16 @@ let confirmSlotSingleton: GhostConfirmSlot | null = null;
export const GHOST_CONFIRM_CHANNEL = 'ghosts:confirm-request';
export const FORGE_OIDC_INSTALL_CONFIRM_CHANNEL = 'forge-oidc-install:confirm-request';
-function pickTrustedAppWindow(): Electron.BrowserWindow | undefined {
- const focused = BrowserWindow.getFocusedWindow();
- if (focused && !focused.isDestroyed() && isTrustedAppRendererWindow(focused)) return focused;
- return BrowserWindow.getAllWindows().find(
- (candidate) => !candidate.isDestroyed() && isTrustedAppRendererWindow(candidate),
- );
-}
-
-function sendTrustedGhostWindowPush(channel: string, payload: unknown): boolean {
- const win = pickTrustedAppWindow();
- if (!win) return false;
- sendGhostWindowPush(win, channel, payload);
- return true;
-}
-
function ensureForgeOidcInstallConfirmBridge() {
return (
getForgeOidcInstallConfirmBridge() ??
initForgeOidcInstallConfirmBridge({
- sendToWindow: (payload) =>
- sendTrustedGhostWindowPush(FORGE_OIDC_INSTALL_CONFIRM_CHANNEL, payload),
+ sendToWindow: createForgeOidcInstallMainWindowSender<BrowserWindow>({
+ getMainWindow: getDeepLinkMainWindow,
+ isTrustedMainWindow: isTrustedAppRendererWindow,
+ send: (window, payload) =>
+ sendGhostWindowPush(window, FORGE_OIDC_INSTALL_CONFIRM_CHANNEL, payload),
+ }),
log,
})
);