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。

插件视图缺省单实例:整页与侧栏页签 / 独立窗口互斥

Dash44da8a0dde45
diff --git a/apps/desktop/src/main/cindy-brain/forge.ts b/apps/desktop/src/main/cindy-brain/forge.ts
index c5ea193ed6..1438bc029a 100644
--- a/apps/desktop/src/main/cindy-brain/forge.ts
+++ b/apps/desktop/src/main/cindy-brain/forge.ts
@@ -4669,6 +4669,10 @@ if (r.ok && r.confirmed) {
\`bottom\`,缺省 right;其它 open 值下声明 side 拒装);\`window\` = 弹成独立 OS 窗口。
这只是**默认**:用户把页签拖到别的边、弹出或合并回来之后,主机记住用户的放置,
左侧栏入口此后按用户的位置打开,直到用户在插件详情页「恢复默认位置」;
+- **同一视图缺省单实例**:主机保证你的主视图同一时刻只活在一个地方——用户进整页时
+ 侧栏 / 独立窗口里的页签会被收掉,页签出现时整页让位。两份同一界面各自保存整份数据
+ 会互相覆盖,这条兜底让你不必自己做多视图防覆盖。只有自己已处理好多视图一致性(共享
+ 写入方 / 保存带版本号 / BroadcastChannel 同步)的插件才声明 \`"multiple": true\` 放开;
- \`mainView\` 与 \`panel\` 是两种独立 UI 贡献项。可以让两者指向同一 HTML,但必须分别
声明;主视图不接受 position、宽度或 systemButtons;
- 插件批准并启用后,入口默认显示。用户可以在插件详情关闭“显示在侧边栏”;这只隐藏
@@ -4921,7 +4925,7 @@ Cindy 统一归类、随机选择与排序,同批每个场景和每个插件
或用 $command 点名;AI 工具调用不受影响(见 §2 说明)
- \`tools\` 为空 · panel 详单缺少实际形态(既没有 html,也不是有效的 tab/停靠配置)
- mainView.html 文件缺失/路径不安全、icon 不在系统图标白名单、open 不是 page / sidebar /
- window、side 不是四条边之一或在 open 非 sidebar 时声明
+ window、side 不是四条边之一或在 open 非 sidebar 时声明、multiple 不是字面量 true
- settingsHtml 路径不合法/文件不在包里 · settingsHeight 越界(160–800)或没配 settingsHtml 单独声明
- panel.systemButtons 格式错(不是对象、已知键的值非布尔,或 position:"tab" 时声明——插件页内面板没有标准头)
- keywords(已废弃字段,旧包兼容保留,新意识别写)有单字词 · kind 写了但不是 "chip"(可省略) · schemaVersion 不是 3 · 缺 minCindyVersion
diff --git a/apps/desktop/src/renderer/features/plugin/GhostMainViewHost.tsx b/apps/desktop/src/renderer/features/plugin/GhostMainViewHost.tsx
index 244d32b810..b0688b8c8a 100644
--- a/apps/desktop/src/renderer/features/plugin/GhostMainViewHost.tsx
+++ b/apps/desktop/src/renderer/features/plugin/GhostMainViewHost.tsx
@@ -9,6 +9,7 @@ import {
useIsContentHeaderHidden,
} from '@/components/layout/ContentHeader';
import { WINDOW_DRAG_STYLE } from '@/components/layout/windowDrag';
+import { useSingleInstanceViewGuard } from '@/features/right-sidebar/pluginPlacement';
import { useMacFullscreen } from '@/hooks/useMacFullscreen';
import { installedGhostStoragePart } from '../../../shared/pluginIdentity';
@@ -23,6 +24,8 @@ export function GhostMainViewHost() {
);
const isContentHeaderHidden = useIsContentHeaderHidden();
const { isFullscreen } = useMacFullscreen();
+ // 单实例视图:整页与侧栏页签 / 独立窗口互斥(见 pluginPlacement)。
+ useSingleInstanceViewGuard(item?.installedGhost ?? null);
useEffect(() => {
if (!item) navigate('/plugins', { replace: true });
diff --git a/apps/desktop/src/renderer/features/plugin/__tests__/GhostMainViewHost.test.tsx b/apps/desktop/src/renderer/features/plugin/__tests__/GhostMainViewHost.test.tsx
index 174db10018..f1b718167c 100644
--- a/apps/desktop/src/renderer/features/plugin/__tests__/GhostMainViewHost.test.tsx
+++ b/apps/desktop/src/renderer/features/plugin/__tests__/GhostMainViewHost.test.tsx
@@ -349,3 +349,57 @@ describe('GhostMainViewHost', () => {
expect(screen.getByTestId('webview')).not.toBe(ownerAWebview);
});
});
+
+// 测试价值
+// - 生产入口与可观察结果:GhostMainViewHost 的单实例守卫;可观察的是进整页时该插件的侧栏
+// 页签被收掉、整页开着时页签出现则导航回主入口、声明 mainView.multiple 的插件两者并存。
+// - Bug / 规则依据:同一视图两份界面各自保存整份数据会互相覆盖(Mivo Canvas PR #1109 审查 P1);
+// 插件缺省单实例,自己处理好一致性的才声明 multiple。
+// - 现有覆盖缺口:整页与全局页签此前互不知晓。
+// - 最小失败输入:已有 g_workspace 页签时挂载整页。
+// - tier:unit(jsdom,localStorage 全局页签)。
+describe('GhostMainViewHost · 单实例视图守卫', () => {
+ const ghostOf = () => mocks.routeCapable[0].installedGhost as InstalledGhost;
+
+ beforeEach(async () => {
+ const { setDataOwnerGeneration } = await import('@/contexts/dataOwnerGeneration');
+ const { __resetGlobalTabsForTest } = await import('@/features/right-sidebar/globalTabs');
+ const { __resetTabGroupsForTest } = await import('@/features/right-sidebar/tabGroups');
+ window.localStorage.clear();
+ __resetGlobalTabsForTest();
+ __resetTabGroupsForTest();
+ setDataOwnerGeneration('owner-a', 1);
+ });
+
+ it('entering the page removes an existing tab; a tab appearing later makes the page yield', async () => {
+ const { addGlobalPluginTab, getGlobalTabs } = await import('@/features/right-sidebar/globalTabs');
+ const { act } = await import('@testing-library/react');
+ addGlobalPluginTab('workspace', { ghost: ghostOf() });
+ expect(getGlobalTabs()).toHaveLength(1);
+ renderInHeaderSlot();
+ expect(getGlobalTabs()).toHaveLength(0);
+ expect(mocks.navigate).not.toHaveBeenCalled();
+
+ act(() => {
+ addGlobalPluginTab('workspace', { ghost: ghostOf() });
+ });
+ expect(mocks.navigate).toHaveBeenCalledWith('/', { replace: true });
+ expect(getGlobalTabs()).toHaveLength(1);
+ });
+
+ it('leaves both alive when the plugin declares mainView.multiple', async () => {
+ const { addGlobalPluginTab, getGlobalTabs } = await import('@/features/right-sidebar/globalTabs');
+ const { act } = await import('@testing-library/react');
+ const multi = { ...manifest, mainView: { html: 'main-view.html', multiple: true } };
+ mocks.routeCapable = [
+ { ...mocks.routeCapable[0], manifest: multi, installedGhost: { ...ghostOf(), manifest: multi } },
+ ];
+ addGlobalPluginTab('workspace', { ghost: ghostOf() });
+ renderInHeaderSlot();
+ expect(getGlobalTabs()).toHaveLength(1);
+ act(() => {
+ addGlobalPluginTab('workspace', { ghost: ghostOf() });
+ });
+ expect(mocks.navigate).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/desktop/src/renderer/features/right-sidebar/pluginPlacement.ts b/apps/desktop/src/renderer/features/right-sidebar/pluginPlacement.ts
index 63251d9698..4f8b59ac4b 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/pluginPlacement.ts
+++ b/apps/desktop/src/renderer/features/right-sidebar/pluginPlacement.ts
@@ -11,6 +11,9 @@
* panel.position,最后右侧组(见 globalTabs.defaultGroupForGhost)。
*/
+import { useEffect, useRef } from 'react';
+import { useNavigate } from 'react-router-dom';
+
import type { InstalledGhost } from '../../../shared/ghost';
import { installedGhostStoragePart } from '../../../shared/pluginIdentity';
import { createLogger } from '@/lib/logger';
@@ -20,7 +23,9 @@ import {
findGlobalTabByInstance,
isPluginWindowPlacement,
rememberPluginWindowPlacement,
+ removeGlobalTab,
setActiveGlobalTabId,
+ useGlobalTabs,
} from './globalTabs';
import { requestRightSidebarVisibility } from './lib/sidebarCommands';
import { getTabGroup } from './tabGroups';
@@ -89,3 +94,42 @@ export function focusExistingPluginTab(ghost: InstalledGhost): boolean {
if (getTabGroup(existing.id) === 'right') requestRightSidebarVisibility('open');
return true;
}
+
+/** 单实例视图(缺省):同一插件视图同一时刻只活在一个地方。插件声明 mainView.multiple 才放开。 */
+export function isSingleInstanceView(ghost: InstalledGhost): boolean {
+ return ghost.manifest.mainView?.multiple !== true;
+}
+
+/**
+ * 整页(/apps/<id>)的单实例守卫:
+ * - 进整页时该实例的页签还在侧栏 / 独立窗口里 → 收掉页签(用户明确选了整页,整页赢;
+ * 弹出窗口随之关闭);
+ * - 整页开着时页签又出现(用户点了「在侧边栏中打开」/ 弹出)→ 整页让位,回主入口。
+ * 两份同一界面各自保存整份数据会互相覆盖,主机在这里兜底;声明了 multiple 的插件不管。
+ */
+export function useSingleInstanceViewGuard(ghost: InstalledGhost | null): void {
+ const navigate = useNavigate();
+ const globalTabs = useGlobalTabs();
+ const instanceId = ghost ? installedGhostStoragePart(ghost) : null;
+ const single = ghost ? isSingleInstanceView(ghost) : false;
+ const settledRef = useRef<string | null>(null);
+ useEffect(() => {
+ if (!instanceId || !single) {
+ settledRef.current = null;
+ return;
+ }
+ const existing = globalTabs.find(
+ (tab) => (tab.state as { instanceId?: string } | null)?.instanceId === instanceId,
+ );
+ if (settledRef.current !== instanceId) {
+ // 刚进整页:页签在别处就收掉。
+ settledRef.current = instanceId;
+ if (existing) removeGlobalTab(existing.id);
+ return;
+ }
+ if (existing) {
+ // 整页开着时页签出现:整页让位。
+ navigate('/', { replace: true });
+ }
+ }, [globalTabs, instanceId, navigate, single]);
+}
diff --git a/apps/desktop/src/shared/__tests__/ghost.test.ts b/apps/desktop/src/shared/__tests__/ghost.test.ts
index c268417b82..e57e404825 100644
--- a/apps/desktop/src/shared/__tests__/ghost.test.ts
+++ b/apps/desktop/src/shared/__tests__/ghost.test.ts
@@ -2352,6 +2352,13 @@ describe('ghost · panel.position 校验', () => {
expect(validateGhostManifest(withView({ open: 'page', side: 'left' })).ok).toBe(false);
expect(validateGhostManifest(withView({ side: 'left' })).ok).toBe(false);
expect(validateGhostManifest(withView({ open: 'sidebar', side: 'middle' })).ok).toBe(false);
+ // multiple:只接受字面量 true(缺省单实例,主机互斥整页与页签)。
+ expect(validateGhostManifest(withView({ multiple: true }))).toMatchObject({
+ ok: true,
+ manifest: { mainView: { multiple: true } },
+ });
+ expect(validateGhostManifest(withView({ multiple: false })).ok).toBe(false);
+ expect(validateGhostManifest(withView({ multiple: 'yes' })).ok).toBe(false);
});
it("'tab'(右侧栏页签)通过;tab 时 minWidth/defaultFraction 收词明确拒绝", () => {
diff --git a/apps/desktop/src/shared/ghost.ts b/apps/desktop/src/shared/ghost.ts
index 70cf5fac4b..b09eaf2294 100644
--- a/apps/desktop/src/shared/ghost.ts
+++ b/apps/desktop/src/shared/ghost.ts
@@ -501,6 +501,13 @@ export interface GhostMainViewDecl {
open?: GhostMainViewOpenMode;
/** open:'sidebar' 时的默认边;其它 open 值下声明本字段拒装。 */
side?: GhostTabSide;
+ /**
+ * 允许同一个视图同时开在多处(整页 + 侧栏页签 / 独立窗口各一份,各自独立的 webview)。
+ * 缺省单实例:主机保证同一插件视图同一时刻只活在一个地方——进整页就收掉页签,页签
+ * 出现时整页让位——避免两份界面各自保存整份数据互相覆盖。只有自己处理好多视图
+ * 一致性(共享写入方 / 版本号 / BroadcastChannel 同步)的插件才声明 true。
+ */
+ multiple?: true;
}
/**
@@ -4057,6 +4064,9 @@ function validateGhostManifestInput(value: unknown, preserveHistoricalTasks: boo
reason: `mainView.open 必须是 ${GHOST_MAIN_VIEW_OPEN_MODES.join(' / ')}`,
};
}
+ if (raw.mainView.multiple !== undefined && raw.mainView.multiple !== true) {
+ return { ok: false, reason: 'mainView.multiple 只接受字面量 true(不需要时省略)' };
+ }
if (raw.mainView.side !== undefined) {
if (raw.mainView.open !== 'sidebar') {
return { ok: false, reason: "mainView.side 只在 open:'sidebar' 时有效,其它打开方式请移除" };
@@ -4066,13 +4076,14 @@ function validateGhostManifestInput(value: unknown, preserveHistoricalTasks: boo
}
}
mainView = {
- ...unknownDeclarationFields(raw.mainView, ['html', 'title', 'icon', 'open', 'side']),
+ ...unknownDeclarationFields(raw.mainView, ['html', 'title', 'icon', 'open', 'side', 'multiple']),
...(raw.mainView.title !== undefined ? { title: raw.mainView.title } : {}),
...(raw.mainView.icon !== undefined ? { icon: raw.mainView.icon as GhostMainViewIcon } : {}),
...(raw.mainView.open !== undefined
? { open: raw.mainView.open as GhostMainViewOpenMode }
: {}),
...(raw.mainView.side !== undefined ? { side: raw.mainView.side as GhostTabSide } : {}),
+ ...(raw.mainView.multiple === true ? { multiple: true as const } : {}),
html: raw.mainView.html,
};
}