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。

feat(plugins): 伙伴对话也算插件的当前任务,任务页可只查询落点

Dash3f3acb095138
diff --git a/apps/desktop/resources/system-skills/cindy-plugin-creator/references/plugin-task-view.md b/apps/desktop/resources/system-skills/cindy-plugin-creator/references/plugin-task-view.md
index 7161a36123..7c64dc3506 100644
--- a/apps/desktop/resources/system-skills/cindy-plugin-creator/references/plugin-task-view.md
+++ b/apps/desktop/resources/system-skills/cindy-plugin-creator/references/plugin-task-view.md
@@ -32,6 +32,10 @@
包内页面、不接收网址,所以没有 preview 那样的白名单与限速。错误码:
`PERMISSION_DENIED`(未声明 taskView 或未启用)/ `INVALID_REQUEST` /
`HOST_NOT_READY`(当前没有任务,或没有宿主窗口);
+- `cindy.taskView({ peek: true })` 只问"现在打开会落到哪个任务",不开页签:有台前任务
+ 回 `{ ok: true, sessionId }`,没有则 `HOST_NOT_READY`。用它决定自己的界面(如 mainView
+ 管理页)要不要出现"在当前任务中打开"一类入口——整页主视图里通常没有台前任务;
+ 台前任务会变,需要时重新问;
- 旧版 Cindy 不认识 `taskView`,装入时会忽略该字段,`cindy.taskView` 也不存在。需要
兼容旧版时先判断 `typeof cindy.taskView === 'function'`,不支持就退回 preview(§4.15);
- 任务页与 panel / mainView 共用同一 Ghost WebView 沙箱:零 Node、零通用 preload、每插件
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/taskViewSlot.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/taskViewSlot.test.ts
index 9db27e0bab..4e0b455c01 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/taskViewSlot.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/taskViewSlot.test.ts
@@ -1,6 +1,7 @@
/** taskViewSlot.test — 任务页开页槽(taskView)的假 deps 单测。 */
// - 生产入口与可观察结果:GhostTaskViewSlot.handleRequest 的回执与广播载荷。
-// - Bug / 规则依据:插件请求在任务右侧栏打开自己的任务页;落点任务要回给插件绑定。
+// - Bug / 规则依据:插件请求在任务右侧栏打开自己的任务页;落点任务要回给插件绑定;
+// peek 只回答落点、不开页签(整页管理页没有当前任务时不显示「在当前任务中打开」)。
// - 现有覆盖缺口:新槽。最小失败输入:未声明 taskView、sessionId 形状非法、无任务可落。
// - tier:unit。
import { describe, expect, it, vi } from 'vitest';
@@ -56,6 +57,15 @@ describe('taskViewSlot', () => {
expect(pushes).toEqual([{ instanceId: '_ns__acme__sim', sessionId: 'focused-session' }]);
});
+ it('peek reports where a task page would open without opening one', () => {
+ const { slot, pushes } = makeSlot();
+ expect(slot.handleRequest('sim', { peek: true })).toEqual({ ok: true, sessionId: 'focused-session' });
+ expect(makeSlot({ focusedSessionId: () => null }).slot.handleRequest('sim', { peek: true })).toMatchObject({ ok: false, errorCode: 'HOST_NOT_READY' });
+ expect(slot.handleRequest('sim', { peek: 'yes' })).toMatchObject({ ok: false, errorCode: 'INVALID_REQUEST' });
+ expect(makeSlot({ getGhost: () => taskGhost({ taskView: false }) }).slot.handleRequest('sim', { peek: true })).toMatchObject({ ok: false, errorCode: 'PERMISSION_DENIED' });
+ expect(pushes).toEqual([]);
+ });
+
it('refuses plugins without a task page, malformed task ids, and having nowhere to land', () => {
expect(makeSlot({ getGhost: () => taskGhost({ taskView: false }) }).slot.handleRequest('sim', {})).toMatchObject({ ok: false, errorCode: 'PERMISSION_DENIED' });
expect(makeSlot({ getGhost: () => taskGhost({ enabled: false }) }).slot.handleRequest('sim', {})).toMatchObject({ ok: false, errorCode: 'PERMISSION_DENIED' });
diff --git a/apps/desktop/src/main/cindy-brain/taskViewSlot.ts b/apps/desktop/src/main/cindy-brain/taskViewSlot.ts
index a584a36fa0..2cff0162fc 100644
--- a/apps/desktop/src/main/cindy-brain/taskViewSlot.ts
+++ b/apps/desktop/src/main/cindy-brain/taskViewSlot.ts
@@ -8,7 +8,9 @@
* - 资格审:已装、启用、声明了 taskView;
* - 落点:显式 sessionId 优先(通常来自 session-context 注入),缺省台前任务,
* 两者皆无如实拒;sessionId 只做形状校验(右侧栏是本机用户自己的 UI);
- * - 成功回执带回实际落点任务,插件据此绑定自己的业务状态。
+ * - 成功回执带回实际落点任务,插件据此绑定自己的业务状态;
+ * - `peek: true` 只回答"现在打开会落到哪个任务"(没有任务则 HOST_NOT_READY),
+ * 不开页签;供插件决定界面里是否出现"在当前任务中打开"一类入口。
*
* 纯逻辑 + 依赖注入:广播在 cindy-brain/index.ts 组装时注入。
*/
@@ -57,9 +59,13 @@ export class GhostTaskViewSlot {
return fail('INVALID_REQUEST', 'sessionId 形状不合法');
}
}
+ if (request.peek !== undefined && request.peek !== true) {
+ return fail('INVALID_REQUEST', 'peek 只接受 true');
+ }
const sessionId =
typeof request.sessionId === 'string' ? request.sessionId : this.deps.focusedSessionId();
if (!sessionId) return fail('HOST_NOT_READY', '当前没有任务可承载任务页');
+ if (request.peek === true) return { ok: true, sessionId };
const delivered = this.deps.broadcast({ instanceId: installedGhostStoragePart(ghost), sessionId });
if (!delivered) return fail('HOST_NOT_READY', '当前没有可用的宿主窗口');
this.deps.log?.info('ghost task view open requested', { ghostId, sessionId });
diff --git a/apps/desktop/src/preload/ghostPreload.ts b/apps/desktop/src/preload/ghostPreload.ts
index 42a33fb3da..d9e4e9bfd8 100644
--- a/apps/desktop/src/preload/ghostPreload.ts
+++ b/apps/desktop/src/preload/ghostPreload.ts
@@ -34,7 +34,8 @@ import { contextBridge, ipcRenderer } from 'electron';
* - preview(req):preview 槽的便捷口——send({type:'preview-request', …req})
* 的语法糖;URL 白名单守门在主机侧 previewSlot。
* - taskView(req):taskView 槽的便捷口——send({type:'task-view-request', …req});
- * 在任务右侧栏打开 / 聚焦本插件的任务页,回执 {ok, sessionId} 带回落点任务。
+ * 在任务右侧栏打开 / 聚焦本插件的任务页,回执 {ok, sessionId} 带回落点任务;
+ * {peek:true} 只回答落点、不开页签。
* - workspace(req):workspace 槽的便捷口——send({type:'workspace-request',
* …req}) 的语法糖;目录授权与会话创建守门全在主机侧 workspaceSlot。
* - confirm(req):confirm 槽的便捷口——send({type:'confirm-request', …req}) 的
diff --git a/apps/desktop/src/renderer/components/layout/MainLayout.tsx b/apps/desktop/src/renderer/components/layout/MainLayout.tsx
index 3cca8a8c00..d762ec435f 100644
--- a/apps/desktop/src/renderer/components/layout/MainLayout.tsx
+++ b/apps/desktop/src/renderer/components/layout/MainLayout.tsx
@@ -503,7 +503,9 @@ export function MainLayout() {
// 会话切换上报(订阅槽① did-session-switched 数据源):路由变化即报当前
// 台前会话,平台无关、无 agent-island 门控(它是 mac-only + 焦点门,借用会
// 让 Windows / 后台窗口漏报)。去重与投递资格全在 main,这里只做纯上报。
+ // 伙伴会话路由由校验过归属的伙伴页面自己上报(useBotPluginFocusedSession)。
useEffect(() => {
+ if (isAgentIslandVisibleSessionOwnedByBotRoute(location.pathname)) return;
window.electronAPI?.ghosts?.noteSessionFocused?.(
resolveAgentIslandVisibleSessionIdFromPath(location.pathname),
);
diff --git a/apps/desktop/src/renderer/features/bots/BotSessionView.tsx b/apps/desktop/src/renderer/features/bots/BotSessionView.tsx
index c0dc3cbbd4..d6edbe07f0 100644
--- a/apps/desktop/src/renderer/features/bots/BotSessionView.tsx
+++ b/apps/desktop/src/renderer/features/bots/BotSessionView.tsx
@@ -16,6 +16,7 @@ import type { BotChatIdentity } from './BotSessionContentHeader';
import type { BotChatBinding } from './botChatPresentation';
import { useBotIslandVisibleSession } from './useBotIslandVisibleSession';
import { BotDmThreadHost } from './BotDmThreadHost';
+import { useBotPluginFocusedSession } from './useBotPluginFocusedSession';
type BotSessionGate =
| { kind: 'loading' }
@@ -87,7 +88,9 @@ function BotSessionGateView() {
const [gate, setGate] = useState<BotSessionGate>({ kind: 'loading' });
// The companion now runs on its main computer: this copy's main task is read only.
const shadowed = useCompanionCloud().demotedBotIds.includes(botId ?? '');
- useBotIslandVisibleSession(gate.kind === 'ready' ? sessionId ?? null : null);
+ const ownedSessionId = gate.kind === 'ready' ? sessionId ?? null : null;
+ useBotIslandVisibleSession(ownedSessionId);
+ useBotPluginFocusedSession(ownedSessionId);
// The gate proves ownership once; name and avatar edited in settings must
// still reach the open chat's header and composer.
const liveProfile = useBotProfiles().find((profile) => profile.id === botId);
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/botIslandVisibility.test.tsx b/apps/desktop/src/renderer/features/bots/__tests__/botIslandVisibility.test.tsx
index 24c833547d..8e0fab100e 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/botIslandVisibility.test.tsx
+++ b/apps/desktop/src/renderer/features/bots/__tests__/botIslandVisibility.test.tsx
@@ -28,6 +28,7 @@ import { BotSessionView } from '../BotSessionView';
import { BotHistorySessionView } from '../BotHistorySessionView';
const report = vi.fn();
+const focus = vi.fn();
const consumeEntryAlert = vi.fn(async () => ({ ok: true as const, consumed: true }));
const get = vi.fn();
const history = vi.fn();
@@ -46,6 +47,7 @@ beforeEach(() => {
history.mockResolvedValue([{ id: 'chat-a' }]);
Object.defineProperty(window, 'electronAPI', { configurable: true, value: {
agentIsland: { setVisibleSession: report, consumeEntryAlert },
+ ghosts: { noteSessionFocused: focus },
localDb: { bots: { get, history, list: vi.fn(async () => []) } },
} });
});
@@ -199,3 +201,20 @@ describe('history ownership gate disposes the clicked island card', () => {
expect(consumeEntryAlert).not.toHaveBeenCalled();
});
});
+
+describe('chat as the task in front for plugins', () => {
+ // A companion chat is where someone uses the simulator; plugins were told "no task"
+ // there, so "open in the current task" failed. Report it once ownership is proven.
+ it('reports the validated chat, never an unowned route, and clears it on leave', async () => {
+ mocks.supported = false;
+ const view = render(<BotSessionView />);
+ await waitFor(() => expect(focus).toHaveBeenLastCalledWith('chat-a'));
+ mocks.params = { botId: 'bot-b', sessionId: 'not-owned' };
+ view.rerender(<BotSessionView />);
+ await act(async () => {});
+ expect(focus).not.toHaveBeenCalledWith('not-owned');
+ expect(focus).toHaveBeenLastCalledWith(null);
+ view.unmount();
+ expect(focus).toHaveBeenLastCalledWith(null);
+ });
+});
diff --git a/apps/desktop/src/renderer/features/bots/useBotPluginFocusedSession.ts b/apps/desktop/src/renderer/features/bots/useBotPluginFocusedSession.ts
new file mode 100644
index 0000000000..0a0e5f0a49
--- /dev/null
+++ b/apps/desktop/src/renderer/features/bots/useBotPluginFocusedSession.ts
@@ -0,0 +1,13 @@
+import { useEffect } from 'react';
+
+/**
+ * 伙伴会话通过归属校验后,报给插件系统作台前任务(插件的「当前任务」,如在任务页
+ * 打开模拟器)。MainLayout 只按 /cc-agent 路由上报,对伙伴会话路由让位给这里。
+ * Called by the route's ownership gate, never by URL-only navigation.
+ */
+export function useBotPluginFocusedSession(sessionId: string | null) {
+ useEffect(() => {
+ window.electronAPI?.ghosts?.noteSessionFocused?.(sessionId);
+ return () => window.electronAPI?.ghosts?.noteSessionFocused?.(null);
+ }, [sessionId]);
+}