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(sidebar): 副窗口不记无效窗口放置、弹出窗口单例页签与浏览器快捷键可用

Dashf189548dcfc1
diff --git a/apps/desktop/src/renderer/features/right-sidebar/RightSidebarShell.tsx b/apps/desktop/src/renderer/features/right-sidebar/RightSidebarShell.tsx
index e41ae3a9e4..62629579b4 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/RightSidebarShell.tsx
+++ b/apps/desktop/src/renderer/features/right-sidebar/RightSidebarShell.tsx
@@ -569,6 +569,15 @@ export function RightSidebarShell({
// 单例 kind(当前仅 review)走 addOrFocusSingletonTab:已存在则切到现有,
// 否则正常 addTab。menu meta 的 singleton 字段是数据来源,目前只有 review。
const isSingleton = plugin?.menu?.singleton === true;
+ // 独立窗口:本窗口已承载该单例页签时直接切过去(窗口的激活位只在本地,走任务桶的
+ // 聚焦路径不会更新它,菜单里的「已打开」就成了无响应)。
+ if (soloMode && isSingleton) {
+ const existing = tabs.find((tab) => tab.kind === kind);
+ if (existing) {
+ setHostedActiveTabId(existing.id);
+ return;
+ }
+ }
// 边组里点「+」:新页签直接落在本组(乐观插入那一帧就带着组归属,不闪过右侧栏)。
// 独立窗口里点「+」:乐观插入那一刻交给宿主收编(登记为本窗口承载)。
const addOpts = soloMode
@@ -596,7 +605,7 @@ export function RightSidebarShell({
log.error('handleAdd failed', { sessionId, kind, err });
});
},
- [groupId, isPrimaryGroup, onTabCreated, sessionId, soloMode, subagentsEnabled, t],
+ [groupId, isPrimaryGroup, onTabCreated, sessionId, soloMode, subagentsEnabled, t, tabs],
);
const handleClose = useCallback(
@@ -705,11 +714,11 @@ export function RightSidebarShell({
);
useAppShortcut('right-tab-prev', () => handleCycleTab('prev'), {
- enabled: Boolean(sessionId) && isPrimaryGroup,
+ enabled: Boolean(sessionId) && (isPrimaryGroup || soloMode),
stopImmediate: true,
});
useAppShortcut('right-tab-next', () => handleCycleTab('next'), {
- enabled: Boolean(sessionId) && isPrimaryGroup,
+ enabled: Boolean(sessionId) && (isPrimaryGroup || soloMode),
stopImmediate: true,
});
@@ -760,7 +769,8 @@ export function RightSidebarShell({
}, [sessionId]);
useEffect(() => {
- if (!sessionId || !isPrimaryGroup) return;
+ // 只排除主窗的边组;独立页签窗口自己就是页签的承载方,浏览器 webview 转发的快捷键要收。
+ if (!sessionId || (!isPrimaryGroup && !soloMode)) return;
const off = window.electronAPI.onRsbBrowserCommand(({ command }) => {
if (command === 'right-tab-prev') {
handleCycleTab('prev');
@@ -769,7 +779,7 @@ export function RightSidebarShell({
}
});
return off;
- }, [handleCycleTab, isPrimaryGroup, sessionId]);
+ }, [handleCycleTab, isPrimaryGroup, sessionId, soloMode]);
// 顶栏「在独立窗口中打开」作用于当前激活页签;没有激活页签时不渲染按钮。
const handleDetachActiveTab = useMemo(
diff --git a/apps/desktop/src/renderer/features/right-sidebar/__tests__/pluginPlacement.test.ts b/apps/desktop/src/renderer/features/right-sidebar/__tests__/pluginPlacement.test.ts
new file mode 100644
index 0000000000..144e6c8aac
--- /dev/null
+++ b/apps/desktop/src/renderer/features/right-sidebar/__tests__/pluginPlacement.test.ts
@@ -0,0 +1,77 @@
+// @vitest-environment jsdom
+/**
+ * 测试价值
+ * - 生产入口与可观察结果:openPluginInWindow;可观察的是只有真的开出窗口才留下「窗口放置」记忆:
+ * 弹出窗口里(main 只接受主窗 sender)不调用 open 也不记忆、open 失败会撤销记忆,
+ * 正常时记忆并发出 open。
+ * - Bug / 规则依据:review「副窗口点『在独立窗口中打开』记下无效的窗口放置,之后点击一直试图开窗」。
+ * - 现有覆盖缺口:导航入口测试把 pluginPlacement 整个 mock 掉,没有覆盖放置记忆的写入时机。
+ * - 最小失败输入:isSidebarTabWindow() 为 true;api.open 返回 rejected promise。
+ * - tier:unit(jsdom localStorage + mock electronAPI)。
+ */
+
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { setDataOwnerGeneration } from '@/contexts/dataOwnerGeneration';
+import type { InstalledGhost } from '../../../../shared/ghost';
+
+const tabWindow = vi.hoisted(() => ({ solo: false }));
+vi.mock('@/lib/sidebarTabWindow', () => ({ isSidebarTabWindow: () => tabWindow.solo }));
+
+import {
+ __resetGlobalTabsForTest,
+ getActiveGlobalTabId,
+ isPluginWindowPlacement,
+} from '../globalTabs';
+import { __resetTabGroupsForTest } from '../tabGroups';
+import { openPluginInWindow } from '../pluginPlacement';
+
+const ghost = {
+ manifest: { id: 'alpha', name: 'Alpha', mainView: { html: 'main.html' } },
+ enabled: true,
+} as unknown as InstalledGhost;
+
+type Win = { electronAPI?: unknown };
+
+beforeEach(() => {
+ window.localStorage.clear();
+ __resetGlobalTabsForTest();
+ __resetTabGroupsForTest();
+ setDataOwnerGeneration('owner-a', 1);
+ tabWindow.solo = false;
+});
+
+afterEach(() => {
+ delete (window as unknown as Win).electronAPI;
+ __resetGlobalTabsForTest();
+ __resetTabGroupsForTest();
+ window.localStorage.clear();
+});
+
+describe('openPluginInWindow', () => {
+ it('only remembers a window placement when the window really opens', async () => {
+ const open = vi.fn(() => Promise.resolve());
+ (window as unknown as Win).electronAPI = { sidebarTabWindow: { open, focus: vi.fn() } };
+
+ // 弹出窗口里:退回侧边栏,不调用 open,也不记窗口放置。
+ tabWindow.solo = true;
+ openPluginInWindow(ghost);
+ expect(open).not.toHaveBeenCalled();
+ expect(isPluginWindowPlacement('alpha')).toBe(false);
+ expect(getActiveGlobalTabId()).toBe('g_alpha');
+
+ // 主窗口:记忆并发出 open。
+ tabWindow.solo = false;
+ openPluginInWindow(ghost);
+ expect(open).toHaveBeenCalledTimes(1);
+ expect(isPluginWindowPlacement('alpha')).toBe(true);
+
+ // open 失败:撤销记忆。
+ const failing = vi.fn(() => Promise.reject(new Error('denied')));
+ (window as unknown as Win).electronAPI = {
+ sidebarTabWindow: { open: failing, focus: vi.fn() },
+ };
+ openPluginInWindow(ghost);
+ await vi.waitFor(() => expect(isPluginWindowPlacement('alpha')).toBe(false));
+ });
+});
diff --git a/apps/desktop/src/renderer/features/right-sidebar/pluginPlacement.ts b/apps/desktop/src/renderer/features/right-sidebar/pluginPlacement.ts
index c1e0527a53..58f58368ad 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/pluginPlacement.ts
+++ b/apps/desktop/src/renderer/features/right-sidebar/pluginPlacement.ts
@@ -17,6 +17,7 @@ import { useNavigate } from 'react-router-dom';
import type { InstalledGhost } from '../../../shared/ghost';
import { installedGhostStoragePart } from '../../../shared/pluginIdentity';
import { createLogger } from '@/lib/logger';
+import { isSidebarTabWindow } from '@/lib/sidebarTabWindow';
import { isTabPoppedOut } from '@/lib/sidebarTabWindowState';
import {
addGlobalPluginTab,
@@ -91,16 +92,17 @@ export function openPluginInWindow(ghost: InstalledGhost): void {
const api = window.electronAPI?.sidebarTabWindow;
const tab = addGlobalPluginTab(instanceId, { ghost, view: 'main' });
if (!tab || !api) return;
+ // 弹出窗口里的 preload 同样暴露 api.open,但 main 只接受主窗 sender:副窗口退回侧边栏,
+ // 且不记「窗口放置」(记了用户看不到窗口,后续点击还会按这份记忆继续尝试开窗)。
+ if (isSidebarTabWindow() || !api.open) {
+ setActiveGlobalTabId(tab.id);
+ return;
+ }
rememberPluginWindowPlacement(instanceId, true);
if (isTabPoppedOut(tab.id)) {
void api.focus(tab.id).catch((err) => log.warn('focus plugin window failed', { err }));
return;
}
- if (!api.open) {
- // 副窗口没有弹出权:退回侧边栏。
- setActiveGlobalTabId(tab.id);
- return;
- }
void api
.open({
tabId: tab.id,
@@ -108,7 +110,12 @@ export function openPluginInWindow(ghost: InstalledGhost): void {
context: { workdir: null, remoteHostId: null },
handoff: null,
})
- .catch((err) => log.warn('pop out plugin view failed', { instanceId, err }));
+ .catch((err) => {
+ // 没开成:撤销窗口放置记忆,页签回到侧边栏,避免留下错误记忆。
+ log.warn('pop out plugin view failed', { instanceId, err });
+ rememberPluginWindowPlacement(instanceId, false);
+ setActiveGlobalTabId(tab.id);
+ });
}
/**