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): 修复分离侧栏自动打开与意识面板

DavidShenc6babddf2351
diff --git a/apps/desktop/src/main/right-sidebar-window/__tests__/controller.test.ts b/apps/desktop/src/main/right-sidebar-window/__tests__/controller.test.ts
index dab598f7c1..572f6f0471 100644
--- a/apps/desktop/src/main/right-sidebar-window/__tests__/controller.test.ts
+++ b/apps/desktop/src/main/right-sidebar-window/__tests__/controller.test.ts
@@ -18,6 +18,7 @@ interface FakeWindow {
destroy: ReturnType<typeof vi.fn>;
hide: ReturnType<typeof vi.fn>;
show: ReturnType<typeof vi.fn>;
+ showInactive: ReturnType<typeof vi.fn>;
focus: ReturnType<typeof vi.fn>;
restore: ReturnType<typeof vi.fn>;
isMinimized: () => boolean;
@@ -64,6 +65,7 @@ function fakeWindow(id = 1): FakeWindow {
}),
hide: vi.fn(() => { visible = false; }),
show: vi.fn(() => { visible = true; }),
+ showInactive: vi.fn(() => { visible = true; }),
focus: vi.fn(),
restore: vi.fn(() => {
minimized = false;
@@ -490,17 +492,65 @@ describe('userInitiated:false', () => {
h.controller.open({ userInitiated: true });
win.show.mockClear();
+ win.showInactive.mockClear();
win.focus.mockClear();
h.controller.open({ userInitiated: false });
expect(win.show).not.toHaveBeenCalled();
+ expect(win.showInactive).not.toHaveBeenCalled();
expect(win.focus).not.toHaveBeenCalled();
});
- it('window not yet built: creates window without showing', () => {
+ it('prewarmed ready window: automated open shows without focusing', () => {
+ const h = makeHarness();
+ h.controller.prewarm();
+ const win = h.windows[0];
+ markReady(h.controller, win);
+
+ h.controller.open({ userInitiated: false });
+
+ expect(win.showInactive).toHaveBeenCalledTimes(1);
+ expect(win.show).not.toHaveBeenCalled();
+ expect(win.focus).not.toHaveBeenCalled();
+ expect(h.controller.getState().open).toBe(true);
+ });
+
+ it('window not yet built: shows without focusing after presentation-ready', () => {
const h = makeHarness();
h.controller.open({ userInitiated: false });
expect(h.windows).toHaveLength(1);
expect(h.createWindowCalls).toHaveLength(1);
+ const win = h.windows[0];
+
+ markReady(h.controller, win);
+
+ expect(win.showInactive).toHaveBeenCalledTimes(1);
+ expect(win.show).not.toHaveBeenCalled();
+ expect(win.focus).not.toHaveBeenCalled();
+ });
+
+ it('not-ready window: automated open fallback shows without focusing', () => {
+ const h = makeHarness();
+ h.controller.open({ userInitiated: false });
+ const win = h.windows[0];
+
+ vi.advanceTimersByTime(5000);
+
+ expect(win.showInactive).toHaveBeenCalledTimes(1);
+ expect(win.show).not.toHaveBeenCalled();
+ expect(win.focus).not.toHaveBeenCalled();
+ });
+
+ it('not-ready window: later user open upgrades pending show to focused', () => {
+ const h = makeHarness();
+ h.controller.open({ userInitiated: false });
+ const win = h.windows[0];
+
+ h.controller.open({ userInitiated: true });
+ markReady(h.controller, win);
+
+ expect(win.show).toHaveBeenCalledTimes(1);
+ expect(win.showInactive).not.toHaveBeenCalled();
+ expect(win.focus).toHaveBeenCalledTimes(1);
});
it('setDetached(true) opens window', () => {
@@ -527,6 +577,8 @@ describe('ensureOpenForAutomation', () => {
const win = h.windows[0];
markReady(h.controller, win);
await expect(h.controller.ensureOpenForAutomation()).resolves.toBeUndefined();
+ expect(win.showInactive).toHaveBeenCalledTimes(1);
+ expect(win.focus).not.toHaveBeenCalled();
});
it('detached + no window: opens and waits for presentation-ready', async () => {
@@ -547,7 +599,7 @@ describe('ensureOpenForAutomation', () => {
const h = makeHarness({ detached: true });
const pending = h.controller.ensureOpenForAutomation();
// ensureOpenForAutomation 内部 readyWaiter 超时为 READY_TIMEOUT_MS(8s)。
- // 5s openFallback 会 showAndFocus 但不影响 readyWaiter。
+ // 5s openFallback 会 showInactive 但不影响 readyWaiter。
const assertion = expect(pending).rejects.toThrow(/ready timeout/);
vi.advanceTimersByTime(8000);
await assertion;
diff --git a/apps/desktop/src/main/right-sidebar-window/__tests__/preload-contract.test.ts b/apps/desktop/src/main/right-sidebar-window/__tests__/preload-contract.test.ts
index 3eda543ff3..6d69de1d49 100644
--- a/apps/desktop/src/main/right-sidebar-window/__tests__/preload-contract.test.ts
+++ b/apps/desktop/src/main/right-sidebar-window/__tests__/preload-contract.test.ts
@@ -119,6 +119,7 @@ const fileBrowserKeys = exposedNestedKeys(source, 'fileBrowser');
const terminalKeys = exposedNestedKeys(source, 'terminal');
const processMonitorKeys = exposedNestedKeys(source, 'processMonitor');
const nativePopupKeys = exposedNestedKeys(source, 'rsbNativePopup');
+const ghostKeys = exposedNestedKeys(source, 'ghosts');
describe('sidebarWindowPreload 椤跺眰濂戠害', () => {
it('鏆撮湶 chrome 鑳藉姏', () => {
@@ -175,7 +176,8 @@ describe('sidebarWindowPreload 椤跺眰濂戠害', () => {
expect(topLevel).toEqual(expect.arrayContaining([
'fileBrowser', 'terminal', 'gitReview', 'processMonitor', 'rsbNativePopup',
'openExternal', 'openFileInBrowser', 'openPath', 'showItemInFolder',
- 'getFilePath', 'cacheImageFromBuffer', 'maker', 'localDb',
+ 'copyMediaToClipboard', 'openMediaWithDefaultApp', 'saveMediaAs',
+ 'getFilePath', 'cacheImageFromBuffer', 'maker', 'localDb', 'ghosts',
]));
expect(fileBrowserKeys).toEqual(expect.arrayContaining([
'listDir', 'listAllFiles', 'readFile', 'writeFile', 'createFile', 'createFolder',
@@ -196,19 +198,30 @@ describe('sidebarWindowPreload 椤跺眰濂戠害', () => {
expect(nativePopupKeys).toEqual(expect.arrayContaining([
'claim', 'setBounds', 'command', 'close', 'onEvent',
]));
+ expect(ghostKeys).toEqual(expect.arrayContaining([
+ 'listSync', 'reload', 'setEnabled', 'resolvePanelMedia', 'runtimeStates',
+ 'onChanged', 'onRuntimeChanged', 'onPreviewMedia',
+ 'unreadSync', 'clearUnread', 'onBadge', 'onUnreadSnapshot',
+ ]));
});
it('涓嶆毚闇?maker / agent / voice / login 鑷不鑳藉姏', () => {
const forbidden = [
'agent', 'voiceInput', 'login', 'settings',
'updater', 'chat', 'session',
- 'resourceUsageWindow', 'ghostPanelWindow', 'ghosts',
+ 'resourceUsageWindow', 'ghostPanelWindow',
'pluginMarket', 'deepLink', 'gitContext',
- 'copyMediaToClipboard', 'cacheMediaForSession',
+ 'cacheMediaForSession',
];
for (const key of forbidden) {
expect(topLevel).not.toEqual(expect.arrayContaining([key]));
}
+ for (const key of [
+ 'install', 'uninstall', 'inspect', 'devRuntime', 'devCall',
+ 'approve', 'revokeApproval', 'setCindyPref', 'listCardsBySession',
+ ]) {
+ expect(ghostKeys).not.toEqual(expect.arrayContaining([key]));
+ }
});
it('RSB browser bridge contract', () => {
diff --git a/apps/desktop/src/main/right-sidebar-window/controller.ts b/apps/desktop/src/main/right-sidebar-window/controller.ts
index b7ab7f097e..821b0e23f5 100644
--- a/apps/desktop/src/main/right-sidebar-window/controller.ts
+++ b/apps/desktop/src/main/right-sidebar-window/controller.ts
@@ -108,6 +108,7 @@ export class RsbWindowController {
private presentationReady = false;
private visible = false;
private pendingOpen = false;
+ private pendingOpenShouldFocus = true;
private disposed = false;
private readyWaiters: Array<{
resolve: () => void;
@@ -173,17 +174,22 @@ export class RsbWindowController {
this.deps.settings.writePatch({ lastOpen: true });
if (this.winRef && !this.winRef.isDestroyed()) {
- if (!userInitiated) return;
if (this.visible && this.winRef.isVisible()) {
- this.winRef.focus();
+ if (userInitiated) {
+ this.pendingOpenShouldFocus = true;
+ this.winRef.focus();
+ }
return;
}
if (this.presentationReady) {
- this.showAndFocus(this.winRef);
+ this.showWindow(this.winRef, userInitiated);
return;
}
// 窗口存在但渲染尚未完成,等待 presentation-ready 或超时
this.winRef.webContents.setBackgroundThrottling(false);
+ this.pendingOpenShouldFocus = this.pendingOpen
+ ? this.pendingOpenShouldFocus || userInitiated
+ : userInitiated;
this.pendingOpen = true;
this.scheduleOpenFallback(this.winRef);
this.broadcast();
@@ -191,13 +197,15 @@ export class RsbWindowController {
}
this.pendingOpen = true;
+ this.pendingOpenShouldFocus = userInitiated;
const win = this.ensureWindow();
if (!win) {
this.pendingOpen = false;
+ this.pendingOpenShouldFocus = true;
return;
}
if (this.presentationReady) {
- this.showAndFocus(win);
+ this.showWindow(win, userInitiated);
return;
}
this.scheduleOpenFallback(win);
@@ -211,6 +219,7 @@ export class RsbWindowController {
} else {
this.deps.settings.writePatch({ lastOpen: false });
this.pendingOpen = false;
+ this.pendingOpenShouldFocus = true;
this.broadcast();
}
}
@@ -260,10 +269,10 @@ export class RsbWindowController {
w.resolve();
}
if (this.pendingOpen) {
- this.showAndFocus(win);
+ this.showWindow(win, this.pendingOpenShouldFocus);
}
// 无论是纯预热还是用户点击打开,presentation-ready 都是 deferred
- // command 可以安全交付的统一边界。不能在 showAndFocus 后提前返回,否则
+ // command 可以安全交付的统一边界。不能在 showWindow 后提前返回,否则
// 点击路径会永久留下此前排队的 passive intent。
this.flushDeferredCommandsToDetachedHost();
return true;
@@ -305,8 +314,6 @@ export class RsbWindowController {
*/
ensureOpenForAutomation(opts: { userInitiated?: boolean } = {}): Promise<void> {
if (!this.deps.settings.read().detached) return Promise.resolve();
- if (this.presentationReady && this.winRef && !this.winRef.isDestroyed())
- return Promise.resolve();
this.open({ userInitiated: opts.userInitiated === true });
if (this.presentationReady) return Promise.resolve();
return new Promise<void>((resolve, reject) => {
@@ -475,14 +482,19 @@ export class RsbWindowController {
return win;
}
- private showAndFocus(win: BrowserWindow): void {
+ private showWindow(win: BrowserWindow, shouldFocus: boolean): void {
this.clearOpenTimeout();
this.clearPrewarmTimeout();
this.pendingOpen = false;
+ this.pendingOpenShouldFocus = shouldFocus;
if (win.isMinimized()) win.restore();
win.webContents.setBackgroundThrottling(true);
- win.show();
- win.focus();
+ if (shouldFocus) {
+ win.show();
+ win.focus();
+ } else {
+ win.showInactive();
+ }
this.visible = true;
// lastOpen 由 open() 在外层写,这里只负责展示
this.deps.sendToWindow(win, RSB_WINDOW_VISIBILITY_CHANGED_CHANNEL, { visible: true });
@@ -493,6 +505,7 @@ export class RsbWindowController {
this.clearOpenTimeout();
this.clearPrewarmTimeout();
this.pendingOpen = false;
+ this.pendingOpenShouldFocus = true;
if (win.isVisible()) win.hide();
this.visible = false;
try {
@@ -533,6 +546,7 @@ export class RsbWindowController {
this.presentationReady = false;
this.visible = false;
this.pendingOpen = false;
+ this.pendingOpenShouldFocus = true;
this.destroyingWindow = false;
const waiters = this.readyWaiters.splice(0);
@@ -560,7 +574,7 @@ export class RsbWindowController {
if (!this.rendererReady && !this.visible) {
this.deps.log.warn('right-sidebar open timed out before renderer-ready');
}
- if (!this.visible) this.showAndFocus(win);
+ if (!this.visible) this.showWindow(win, this.pendingOpenShouldFocus);
}, DEFAULT_OPEN_TIMEOUT_MS);
this.openTimeout.unref?.();
}
@@ -610,7 +624,9 @@ export class RsbWindowController {
private onRendererReloadStarted(win: BrowserWindow): void {
if (win !== this.winRef || win.isDestroyed()) return;
const shouldRestore = this.visible || this.pendingOpen;
- if (shouldRestore) this.pendingOpen = true;
+ if (shouldRestore) {
+ this.pendingOpen = true;
+ }
if (this.visible) win.hide();
win.webContents.setBackgroundThrottling(false);
this.rendererReady = false;
@@ -625,6 +641,7 @@ export class RsbWindowController {
private invalidateWindow(win: BrowserWindow, reason: string): void {
if (win !== this.winRef || win.isDestroyed()) return;
const reopen = this.pendingOpen || this.visible;
+ const reopenShouldFocus = this.pendingOpenShouldFocus;
this.deps.log.warn('right-sidebar cached window invalidated', { reason, reopen });
this.destroyCachedWindow();
if (!reopen || this.disposed) return;
@@ -634,9 +651,11 @@ export class RsbWindowController {
}
this.automaticRecoveryAttempts += 1;
this.pendingOpen = true;
+ this.pendingOpenShouldFocus = reopenShouldFocus;
const replacement = this.ensureWindow();
if (!replacement) {
this.pendingOpen = false;
+ this.pendingOpenShouldFocus = true;
return;
}
this.scheduleOpenFallback(replacement);
diff --git a/apps/desktop/src/preload/sidebarWindowPreload.ts b/apps/desktop/src/preload/sidebarWindowPreload.ts
index 88fd96cf38..c29d4e3fbf 100644
--- a/apps/desktop/src/preload/sidebarWindowPreload.ts
+++ b/apps/desktop/src/preload/sidebarWindowPreload.ts
@@ -246,6 +246,11 @@ contextBridge.exposeInMainWorld('electronAPI', {
openFileInBrowser: (pathOrUrl: string): Promise<unknown> => ipcRenderer.invoke('shell:open-file-in-browser', pathOrUrl),
openPath: (pathOrUrl: string): Promise<unknown> => ipcRenderer.invoke('shell:open-path', pathOrUrl),
showItemInFolder: (params: unknown): Promise<unknown> => ipcRenderer.invoke('shell:show-item-in-folder', params),
+ copyMediaToClipboard: (params: unknown): Promise<unknown> =>
+ ipcRenderer.invoke('media:copy-to-clipboard', params),
+ openMediaWithDefaultApp: (params: unknown): Promise<void> =>
+ ipcRenderer.invoke('media:open-with-default-app', params),
+ saveMediaAs: (params: unknown): Promise<unknown> => ipcRenderer.invoke('media:save-as', params),
getFilePath: (file: File): string => {
try {
return webUtils.getPathForFile(file);
@@ -256,6 +261,39 @@ contextBridge.exposeInMainWorld('electronAPI', {
cacheImageFromBuffer: (params: unknown): Promise<unknown> => ipcRenderer.invoke('image-cache:from-buffer', params),
onRsbBrowserFocusUrlBar: (cb: () => void): (() => void) => onPayload('rsb:browser-focus-url-bar', cb),
+ // 意识面板注册与运行所需的最小 bridge。右侧栏独立窗口会复用
+ // ghostPanels/GhostChipPanelBody,但不暴露安装、卸载、开发运行时或权限管理能力。
+ ghosts: {
+ listSync: (): { ghosts: unknown[] } => ipcRenderer.sendSync('ghosts:list'),
+ reload: (id: string): Promise<{ state: string }> => ipcRenderer.invoke('ghosts:reload', id),
+ setEnabled: (id: string, enabled: boolean): Promise<{ ok: true }> =>
+ ipcRenderer.invoke('ghosts:set-enabled', id, enabled),
+ resolvePanelMedia: (
+ uri: string,
+ purpose?: 'attach' | 'menu',
+ ): Promise<unknown> => ipcRenderer.invoke('ghosts:resolve-panel-media', uri, purpose),
+ runtimeStates: (): Promise<{ states: Record<string, string> }> =>
+ ipcRenderer.invoke('ghosts:runtime-states'),
+ onChanged: (cb: (payload: unknown) => void): (() => void) => onPayload('ghosts:changed', cb),
+ onRuntimeChanged: (cb: (payload: unknown) => void): (() => void) =>
+ onPayload('ghosts:runtime-changed', cb),
+ onPreviewMedia: (cb: (payload: unknown) => void): (() => void) =>
+ onPayload('ghosts:preview-media', cb),
+ unreadSync: (): { entries: unknown[] } => {
+ try {
+ const result = ipcRenderer.sendSync('ghosts:unread') as { entries?: unknown } | null;
+ return { entries: Array.isArray(result?.entries) ? result.entries : [] };
+ } catch {
+ return { entries: [] };
+ }
+ },
+ clearUnread: (id: string, seenAt?: number): Promise<{ ok: boolean }> =>
+ ipcRenderer.invoke('ghosts:clear-unread', id, seenAt),
+ onBadge: (cb: (payload: unknown) => void): (() => void) => onPayload('ghosts:badge', cb),
+ onUnreadSnapshot: (cb: (payload: unknown) => void): (() => void) =>
+ onPayload('ghosts:unread-snapshot', cb),
+ },
+
rightSidebarWindow: {
getState: (): Promise<{ detached: boolean; lastOpen: boolean; open: boolean }> =>
ipcRenderer.invoke('maker:rsb-window:get-state'),
diff --git a/apps/desktop/src/renderer/components/layout/SidebarWindowLayout.tsx b/apps/desktop/src/renderer/components/layout/SidebarWindowLayout.tsx
index 8c7a6ef7a8..afe30f4188 100644
--- a/apps/desktop/src/renderer/components/layout/SidebarWindowLayout.tsx
+++ b/apps/desktop/src/renderer/components/layout/SidebarWindowLayout.tsx
@@ -43,6 +43,7 @@ import { useCloseShortcutShellOwner } from '@/hooks/useCloseWindowShortcut';
import { useLocale } from '@/hooks/useLocale';
import { createLogger } from '@/lib/logger';
import { makerChatStore } from '@/lib/makerChatStore';
+import { GhostMediaLightboxHost } from '@/cindy-brain/GhostMediaLightboxHost';
import {
ensureGhostPanelsRegistered,
useGhostPanelsSync,
@@ -252,6 +253,7 @@ export function SidebarWindowLayout() {
</div>
)}
</div>
+ <GhostMediaLightboxHost />
</div>
);
}