Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 修复伙伴控制提示重复显示及折叠后消失
ficowang9821a86e047a
diff --git a/apps/desktop/src/renderer/__tests__/controlledBannerPlacement.test.ts b/apps/desktop/src/renderer/__tests__/controlledBannerPlacement.test.tsindex ea6decf3d6..41149187ea 100644--- a/apps/desktop/src/renderer/__tests__/controlledBannerPlacement.test.ts+++ b/apps/desktop/src/renderer/__tests__/controlledBannerPlacement.test.ts@@ -20,7 +20,7 @@ describe('controlled banner placement', () => {expect(sessionViewSource).toContain('showControlledBanner?: boolean;');expect(sessionViewSource).toContain('showControlledBanner = false');expect(sessionViewSource).toContain(- 'const showComposerControlledBanner = ownsRoute || showControlledBanner;',+ 'const showComposerControlledBanner = viewVisible && (ownsRoute || showControlledBanner);',);expect(sessionViewSource).toContain('const hasControlledBanner = showComposerControlledBanner && controlledBy.length > 0;',@@ -29,7 +29,7 @@ describe('controlled banner placement', () => {'const controlledBannerCollapsed = useComposerCollapsed(sessionId ?? null);',);expect(sessionViewSource).toContain(- 'const showExpandedControlledBanner = hasControlledBanner && !controlledBannerCollapsed;',+ 'hasControlledBanner && (!controlledBannerCollapsed || Boolean(botChatIdentity));',);expect(sessionViewSource).toContain('placement="composer"');expect(sessionViewSource).toContain('sessionId={sessionId ?? null}');@@ -42,7 +42,7 @@ describe('controlled banner placement', () => {expect(sessionViewSource).toContain('const isHidden = suppressContent || (!showContent && !visible);',);- expect(sessionViewSource).toContain('{showExpandedControlledBanner && (');+ expect(sessionViewSource).toContain('{showCenteredControlledBanner && (');expect(sessionViewSource).toContain('rightLeadingSlot?: ReactNode;');expect(sessionViewSource).toContain('{rightLeadingSlot}');expect(sessionViewSource).toContain('data-running-status-meta="true"');@@ -128,7 +128,7 @@ describe('controlled banner placement', () => {it('opts in only route-owned chat views, not Worker panes or embedded doc rails', () => {expect(sessionViewSource).toContain(- 'const showComposerControlledBanner = ownsRoute || showControlledBanner;',+ 'const showComposerControlledBanner = viewVisible && (ownsRoute || showControlledBanner);',);expect(routeSource).not.toContain('<CCAgentSessionView');expect(routeSource).not.toContain('showControlledBanner');@@ -139,13 +139,8 @@ describe('controlled banner placement', () => {expect(workerPanelSource).not.toContain('showControlledBanner');});- it('suppresses the global floating fallback on legacy Orca redirect pages', () => {- expect(mainLayoutSource).toContain(- 'function hasInlineControlledBannerPath(pathname: string): boolean',- );- expect(mainLayoutSource).toContain(- "return parts.length === 3 && parts[1] === 'orca' && parts[2] !== 'new';",- );- expect(mainLayoutSource).toContain('{!hasInlineControlledBanner && <ControlledBanner />}');+ it('always mounts the global fallback so loading and unavailable routes retain a control notice', () => {+ expect(mainLayoutSource).toContain('<ControlledBanner />');+ expect(mainLayoutSource).not.toContain('hasInlineControlledBannerPath');});});diff --git a/apps/desktop/src/renderer/components/layout/MainLayout.tsx b/apps/desktop/src/renderer/components/layout/MainLayout.tsxindex e4fb5ec4da..b79fd57e2f 100644--- a/apps/desktop/src/renderer/components/layout/MainLayout.tsx+++ b/apps/desktop/src/renderer/components/layout/MainLayout.tsx@@ -141,8 +141,6 @@ interface RightSidebarSessionDeclarationOptions {const applicationMenuLog = createLogger('ApplicationMenu');const log = createLogger('MainLayout');-// Keep in sync with single-segment static routes under /cc-agent in router.tsx.-const CC_AGENT_STATIC_SEGMENTS = ['boot', 'files', 'new', 'new-dialogue', 'orca', 'scheduled'];function getInitialCollapsed(): boolean {// 「在新窗口打开」的副窗口默认折叠侧栏 —— 多开盯多个会话时,每个窗口都铺一条@@ -191,13 +189,6 @@ function isZoomShortcutBlockedTarget(target: EventTarget | null): boolean {);}-function hasInlineControlledBannerPath(pathname: string): boolean {- const parts = pathname.split('/').filter(Boolean);- if (parts[0] !== 'cc-agent') return false;- if (parts.length === 2) return !CC_AGENT_STATIC_SEGMENTS.includes(parts[1]);- return parts.length === 3 && parts[1] === 'orca' && parts[2] !== 'new';-}-/*** SidebarPinSpacer —— peek 抽屉固定展开(pinning)期的流内占位。* 抽屉在 pinning 期保持 fixed 冻结不动,本组件在流内跑 0→width 的宽度动画@@ -398,7 +389,6 @@ export function MainLayout() {}, []);const isSettingsRoute = location.pathname === '/settings';- const hasInlineControlledBanner = hasInlineControlledBannerPath(location.pathname);// 完全隐藏态 hover 临时浮出(peek)状态机 —— hover 折叠按钮抽屉滑出预览,// 点击/⌘B 固定展开(pin)。rail 态(isCollapsed=false)不触发。见 useSidebarPeek。@@ -1637,8 +1627,8 @@ export function MainLayout() {单例 + vanilla DOM,这里只挂一个空 React 节点,Phase 6 maximize 时会在这里加 layout 控制。 */}<BrowserWebviewPool />- {/* 被控端可见性:主聊天页在输入区内联渲染;其它页面保留全局兜底。 */}- {!hasInlineControlledBanner && <ControlledBanner />}+ {/* 实际存在内联提示(含伙伴 / 折叠态)时组件自行退让,其余页面保留兜底。 */}+ <ControlledBanner /></FeatureSidebarSlotProvider>);}diff --git a/apps/desktop/src/renderer/features/cc-agent/CCAgentSessionView.tsx b/apps/desktop/src/renderer/features/cc-agent/CCAgentSessionView.tsxindex bc3e841fba..083e3a406f 100644--- a/apps/desktop/src/renderer/features/cc-agent/CCAgentSessionView.tsx+++ b/apps/desktop/src/renderer/features/cc-agent/CCAgentSessionView.tsx@@ -856,11 +856,10 @@ export function CCAgentSessionView({viewVisible &&navigationMode !== 'sidebar-embedded' &&navigationMode !== 'split-pane');- const showComposerControlledBanner = ownsRoute || showControlledBanner;+ const showComposerControlledBanner = viewVisible && (ownsRoute || showControlledBanner);const controlledBy = useControlledBy();const hasControlledBanner = showComposerControlledBanner && controlledBy.length > 0;const controlledBannerCollapsed = useComposerCollapsed(sessionId ?? null);- const showExpandedControlledBanner = hasControlledBanner && !controlledBannerCollapsed;const isMac = window.electronAPI?.platform === 'darwin';// messageWidth:消息流容器宽度(视觉边距 50px / compact 20px)// inputWidth:ChatInput / 状态栏 / workingDir 行的宽度(视觉边距 40px / compact 10px)@@ -936,6 +935,9 @@ export function CCAgentSessionView({// 只有 URL 说了不算 —— 那是导航投影,不是身份。const botChatIdentity: BotChatIdentity | null =botIdentity && session?.source === 'bot' ? botIdentity : null;+ // 伙伴没有 RunningStatusBar,折叠呼吸灯继续留在输入框上方,不能随状态行一起消失。+ const showCenteredControlledBanner =+ hasControlledBanner && (!controlledBannerCollapsed || Boolean(botChatIdentity));// assistant 气泡左侧的伙伴头像。节点在整场对话里是同一个,memo 住让 MessageItem// 的 memo 比较仍然成立(否则每帧新节点 = 全流重渲染)。const botAssistantAvatar = useMemo(@@ -4783,7 +4785,7 @@ export function CCAgentSessionView({}className="mb-0"/>- {showExpandedControlledBanner && (+ {showCenteredControlledBanner && (<ControlledBannerplacement="composer"maxWidth={controlledBannerMaxWidth}diff --git a/apps/desktop/src/renderer/features/remote-device/ControlledBanner.tsx b/apps/desktop/src/renderer/features/remote-device/ControlledBanner.tsxindex 4b752773f6..f99159943c 100644--- a/apps/desktop/src/renderer/features/remote-device/ControlledBanner.tsx+++ b/apps/desktop/src/renderer/features/remote-device/ControlledBanner.tsx@@ -14,7 +14,14 @@* 完整设备名 + 远程控制逻辑说明。*/-import { useCallback, useEffect, useState, useSyncExternalStore, type CSSProperties } from 'react';+import {+ useCallback,+ useEffect,+ useLayoutEffect,+ useState,+ useSyncExternalStore,+ type CSSProperties,+} from 'react';import { useTranslation } from 'react-i18next';import { useNavigate } from 'react-router-dom';import { Eye, MonitorOff, X } from 'lucide-react';@@ -43,6 +50,37 @@ let cachedControllers: Controller[] = [];let pushSubscribed = false;const controllerListeners = new Set<(c: Controller[]) => void>();+// 按实际挂载的内联提示让全局浮条退让,不猜路由(伙伴也复用聊天视图)。+// 多个 pane / 路由过渡可短暂共存,必须等最后一个内联提示卸载才恢复兜底。+const inlineBannerOwners = new Set<symbol>();+const inlineBannerListeners = new Set<() => void>();+const getInlineBannerSnapshot = () => inlineBannerOwners.size > 0;+function subscribeInlineBanners(listener: () => void) {+ inlineBannerListeners.add(listener);+ return () => {+ inlineBannerListeners.delete(listener);+ };+}++function useInlineBannerPresence(inline: boolean): boolean {+ const hasInlineBanner = useSyncExternalStore(+ subscribeInlineBanners,+ getInlineBannerSnapshot,+ getInlineBannerSnapshot,+ );+ useLayoutEffect(() => {+ if (!inline) return;+ const owner = Symbol();+ inlineBannerOwners.add(owner);+ for (const listener of inlineBannerListeners) listener();+ return () => {+ inlineBannerOwners.delete(owner);+ for (const listener of inlineBannerListeners) listener();+ };+ }, [inline]);+ return hasInlineBanner;+}+// composer 被控提示的折叠状态只在当前 renderer 生命周期内保存,并严格按 sessionId// 分桶。它不是全局偏好,不写 localStorage / DB / 服务端;切换任务时直接读取对应桶,// 因此不会把 A 任务的折叠态闪到 B 任务首帧。@@ -75,6 +113,8 @@ export function __resetControlledBannerForTests(): void {controllerListeners.clear();collapsedComposerSessionIds.clear();collapsedComposerListeners.clear();+ inlineBannerOwners.clear();+ inlineBannerListeners.clear();}function emitControllers(next: Controller[]) {@@ -138,8 +178,11 @@ export function ControlledBanner({const controllers = useControlledBy();const composerSessionId = placement === 'composer' ? sessionId : null;const composerCollapsed = useComposerCollapsed(composerSessionId);+ const hasInlineBanner = useInlineBannerPresence(+ placement !== 'floating' && controllers.length > 0,+ );- if (controllers.length === 0) return null;+ if (controllers.length === 0 || (placement === 'floating' && hasInlineBanner)) return null;const label =controllers.length === 1diff --git a/apps/desktop/src/renderer/features/remote-device/__tests__/ControlledBanner.test.tsx b/apps/desktop/src/renderer/features/remote-device/__tests__/ControlledBanner.test.tsxindex df19bba4a4..e42d99dede 100644--- a/apps/desktop/src/renderer/features/remote-device/__tests__/ControlledBanner.test.tsx+++ b/apps/desktop/src/renderer/features/remote-device/__tests__/ControlledBanner.test.tsx@@ -1,7 +1,7 @@// @vitest-environment jsdom-import type { ReactElement } from 'react';-import { cleanup, fireEvent, render, screen } from '@testing-library/react';+import { StrictMode, type ReactElement } from 'react';+import { act, cleanup, fireEvent, render, screen } from '@testing-library/react';import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { ControlledBanner, __resetControlledBannerForTests } from '../ControlledBanner';@@ -104,3 +104,66 @@ describe('ControlledBanner composer collapse state', () => {).toBeTruthy();});});++describe('ControlledBanner floating fallback', () => {+ it('yields to a mounted composer, including its collapsed indicator, and returns after unmount', async () => {+ const fallback = render(<ControlledBanner />);+ await screen.findByText('Controlled by iPhone');++ const composer = render(<ControlledBanner placement="composer" sessionId="bot-session" />);+ expect(screen.getAllByText('Controlled by iPhone')).toHaveLength(1);+ expect(fallback.container.childElementCount).toBe(0);++ fireEvent.click(screen.getByRole('button', { name: 'remoteDevice.collapseControlledNotice' }));+ expect(screen.queryByText('Controlled by iPhone')).toBeNull();+ expect(screen.getByRole('button', { name: 'Expand: Controlled by iPhone' })).toBeTruthy();+ expect(fallback.container.childElementCount).toBe(0);++ composer.unmount();+ expect(screen.getAllByText('Controlled by iPhone')).toHaveLength(1);+ expect(fallback.container.childElementCount).toBe(1);+ });++ it('waits for the last inline owner and survives StrictMode effect replay', async () => {+ const fallback = render(<ControlledBanner />);+ await screen.findByText('Controlled by iPhone');+ const first = render(+ <StrictMode>+ <ControlledBanner placement="composer" sessionId="first" />+ </StrictMode>,+ );+ const second = render(<ControlledBanner placement="inline" />);+ expect(fallback.container.childElementCount).toBe(0);++ first.unmount();+ expect(fallback.container.childElementCount).toBe(0);+ second.unmount();+ expect(fallback.container.childElementCount).toBe(1);+ });++ it('handles placement changes without retaining a stale owner', async () => {+ const view = render(<ControlledBanner placement="composer" sessionId="session" />);+ await screen.findByText('Controlled by iPhone');+ view.rerender(<ControlledBanner />);+ expect(screen.getAllByText('Controlled by iPhone')).toHaveLength(1);+ view.rerender(<ControlledBanner placement="inline" />);+ expect(screen.getAllByText('Controlled by iPhone')).toHaveLength(1);+ view.unmount();+ render(<ControlledBanner />);+ expect(screen.getAllByText('Controlled by iPhone')).toHaveLength(1);+ });++ it('shows neither surface after disconnect and restores only the composer on reconnect', async () => {+ const fallback = render(<ControlledBanner />);+ render(<ControlledBanner placement="composer" sessionId="session" />);+ await screen.findByText('Controlled by iPhone');+ const onControlledState = vi.mocked(window.electronAPI.deviceLink.onControlledState);+ expect(onControlledState).toHaveBeenCalledTimes(1);+ const push = onControlledState.mock.calls[0][0];+ act(() => push({ controllers: [] }));+ expect(document.querySelector('[data-controlled-banner-chip]')).toBeNull();+ act(() => push({ controllers: [{ deviceId: 'iphone', name: 'iPhone' }] }));+ expect(screen.getAllByText('Controlled by iPhone')).toHaveLength(1);+ expect(fallback.container.childElementCount).toBe(0);+ });+});