🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { getWorkingDirEffectivePath } from '@lobechat/types';
import { nanoid } from '@lobechat/utils';
import { ActionIcon, DropdownMenu, Flexbox, Skeleton } from '@lobehub/ui';
import { SkillsIcon } from '@lobehub/ui/icons';
import { cssVar } from '@lobehub/ui/stylex/cssVar.stylex';
import * as stylex from '@stylexjs/stylex';
import {
BoxesIcon,
ClipboardListIcon,
FilesIcon,
FileTextIcon,
Globe2Icon,
GlobeIcon,
MessageCircleIcon,
PanelRightCloseIcon,
PlusIcon,
SearchIcon,
Settings2Icon,
WrenchIcon,
} from 'lucide-react';
import { AnimatePresence } from 'motion/react';
import * as m from 'motion/react-m';
import {
Activity,
lazy,
memo,
Suspense,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { useTranslation } from 'react-i18next';
import { useActiveWorkspaceId } from '@/business/client/hooks/useActiveWorkspaceId';
import { isDesktop } from '@/const/version';
import {
getPullRequestState,
PR_STATE_VISUAL,
} from '@/features/AgentSidebar/Topic/List/Item/metaCardData';
import { useRepoType } from '@/features/ChatInput/ControlBar/useRepoType';
import { getPortalViewWidth } from '@/features/Portal/portalWidth';
import TopicCommentsSidebar from '@/features/Portal/TopicComments/Sidebar';
import RightPanel from '@/features/RightPanel';
import TopicPullRequestProbes from '@/features/TopicPullRequests/TopicPullRequestProbes';
import { resolveTargetDeviceId } from '@/helpers/agentWorkingDirectory';
import { resolveExecutionTarget } from '@/helpers/executionTarget';
import { useIsGatewayModeEnabled } from '@/helpers/gatewayMode';
import { getWorkingDirectoryPathString } from '@/helpers/workingDirectoryPath';
import { useDeferredMount } from '@/hooks/useDeferredMount';
import { useEffectiveAgencyConfig } from '@/hooks/useEffectiveAgencyConfig';
import { useEffectiveWorkingDirectory } from '@/hooks/useEffectiveWorkingDirectory';
import { useFindInTopicHotkey } from '@/hooks/useHotkeys';
import { useLocalStorageState } from '@/hooks/useLocalStorageState';
import { useAgentStore } from '@/store/agent';
import { agentSelectors, chatConfigByIdSelectors } from '@/store/agent/selectors';
import { useChatStore } from '@/store/chat';
import { chatPortalSelectors, portalThreadSelectors, topicSelectors } from '@/store/chat/selectors';
import { PortalViewType } from '@/store/chat/slices/portal/initialState';
import { messageMapKey } from '@/store/chat/utils/messageMapKey';
import { deviceSelectors, useDeviceStore } from '@/store/device';
import { useElectronStore } from '@/store/electron';
import { useGlobalStore } from '@/store/global';
import { systemStatusSelectors } from '@/store/global/selectors';
import {
PanelDensityContext,
useDensityTokens,
useHeaderActionSize,
useInterfaceDensity,
} from '@/styles/density';
import { densityTokens, sidebarRow } from '@/styles/density.stylex';
import { openTrustedExternalUrl } from '@/utils/openTrustedExternalUrl';
import { type ComposerTarget, createComposerTarget, resolveThreadComposerTarget } from '../types';
import DeferredSkeleton from './DeferredSkeleton';
import DynamicTabPanes, { dynamicTabDescriptor } from './DynamicTabPanes';
import {
BROWSER_TAB_KEY,
BROWSER_TAB_PREFIX,
type BrowserTabMetadata,
isBrowserTab,
isDynamicTab,
isMcpAppTab,
isWorkTab,
type SidebarTabDescriptor,
} from './dynamicTabs';
import FilesPane from './Files/FilesPane';
import { useFilesPaneLocation } from './Files/useFilesPaneLocation';
import { sidebarWidthBudget } from './fitsBesidePortal';
import { CANVAS_MIN_WIDTH, MAX_PANEL_WIDTH, MIN_PANEL_WIDTH, OVERVIEW_PANEL_WIDTH } from './layout';
import Overview from './Overview';
import { getStatusDeviceId } from './Overview/overviewData';
import OverviewSlot from './OverviewSlot';
import PullRequestSidebarTab from './PullRequest/SidebarTab';
import { useSidebarPullRequests } from './PullRequest/useSidebarPullRequests';
import ResourcesSection from './ResourcesSection';
import Review from './Review';
import { TopicSearchPane } from './Search';
import { useExtraWorkingSidebarTabs } from './useExtraWorkingSidebarTabs';
import { useMcpAppSidebar } from './useMcpAppSidebar';
import { useOpenMenuItems } from './useOpenMenuItems';
import { useRetainAfterHide } from './useRetainAfterHide';
import { useSandboxFilesDirectory } from './useSandboxFilesDirectory';
import { useTabContextMenuItems } from './useTabContextMenuItems';
import WorkspaceTab from './WorkspaceTab';
import WorksSection from './WorksSection';
const ParamsSection = lazy(() => import('./ParamsSection'));
const ToolsSection = lazy(() => import('./ToolsSection'));
const styles = stylex.create({
body: { overflowY: 'auto', flex: '1', minHeight: '0' },
close: { flexShrink: 0 },
header: { flexShrink: 0, minWidth: '0' },
pane: { overflowY: 'auto', flex: '1', minHeight: '0' },
paneHidden: { display: 'none' },
paramsLoading: { width: '100%', padding: densityTokens['--lobe-density-card-padding'] },
add: { flexShrink: 0 },
overviewBody: { overflowY: 'auto', minHeight: '0' },
overviewPanel: {
overflow: 'hidden',
display: 'flex',
flexDirection: 'column',
flexShrink: 0,
maxHeight: 'calc(100% - 32px)',
margin: '16px',
borderWidth: '1px',
borderStyle: 'solid',
borderColor: cssVar.colorBorderSecondary,
borderRadius: '12px',
backgroundColor: cssVar.colorBgContainer,
backgroundImage: 'none',
boxShadow: cssVar.boxShadowTertiary,
},
overviewSlot: {
overflow: 'hidden',
display: 'flex',
flexShrink: 0,
alignItems: 'flex-start',
height: '100%',
paddingBlockStart: {
'default': null,
'@container agent-chat-layout (min-width: 1200px)': sidebarRow.headerHeight,
},
},
tabs: {
'overflowAnchor': 'none',
'scrollbarWidth': 'none',
'overflowX': 'auto',
'display': 'flex',
'gap': '2px',
'alignItems': 'center',
'minWidth': '0',
'::-webkit-scrollbar': { display: 'none' },
},
tabsArea: { display: 'flex', flex: '1', gap: '4px', alignItems: 'center', minWidth: '0' },
});
const REVIEW_TREE_STORAGE_KEY = 'lobechat-review-tree';
const OPEN_TABS_STORAGE_KEY = 'lobechat-working-sidebar-open-tabs-v1';
const PINNED_TABS_STORAGE_KEY = 'lobechat-working-sidebar-pinned-tabs-v1';
const OVERVIEW_TRANSITION = { duration: 0.25, ease: [0.32, 0.72, 0, 1] } as const;
// Two-pane Review (diff list + file-tree rail) is cramped below this.
const TWO_PANE_MIN_WIDTH = 560;
/** How long a collapsed panel keeps Review mounted for a quick reopen. */
const REVIEW_RETAIN_MS = 30_000;
interface AgentWorkingSidebarProps {
/**
* Measured width of the row this sidebar shares with the conversation and the
* portal. Undefined until the layout has measured it.
*/
availableWidth?: number;
}
const AgentWorkingSidebar = memo<AgentWorkingSidebarProps>(({ availableWidth }) => {
const interfaceDensity = useInterfaceDensity();
const { headerHeight } = useDensityTokens();
const headerActionSize = useHeaderActionSize(true);
const { t } = useTranslation(['chat', 'setting']);
// Keep the panel frame + tabs on the navigation commit; the pane contents
// mount in a deferred follow-up pass behind a skeleton.
const contentReady = useDeferredMount();
const [
storedWidth,
legacyPortalWidth,
portalWidths,
updateSystemStatus,
toggleRightPanel,
openWorkingSidebar,
toggleTerminalPanel,
setWorkingSidebarTab,
showRightPanel,
showWorkingOverview,
storedTab,
tabRequest,
] = useGlobalStore((s) => [
systemStatusSelectors.workingSidebarWidth(s),
systemStatusSelectors.portalWidth(s),
systemStatusSelectors.portalWidths(s),
s.updateSystemStatus,
s.toggleRightPanel,
s.openWorkingSidebar,
s.toggleTerminalPanel,
s.setWorkingSidebarTab,
// Panel open/collapsed state (drives the `<RightPanel>` expand). Used to gate
// the resources pane's document fetch so a collapsed sidebar doesn't pull the
// full agent-document list into the conversation's initial batch.
s.status.showRightPanel,
systemStatusSelectors.showWorkingOverview(s),
s.status.workingSidebarTab,
s.status.workingSidebarTabRequest,
]);
const overviewExitTransition = showRightPanel
? { ...OVERVIEW_TRANSITION, duration: 0.1 }
: OVERVIEW_TRANSITION;
const activeAgentId = useAgentStore((s) => s.activeAgentId);
const workspaceId = useActiveWorkspaceId();
const [
topicId,
currentPortalView,
portalOpen,
openTopicComments,
portalThread,
chatAgentId,
chatGroupId,
chatThreadId,
] = useChatStore((s) => [
s.activeTopicId,
chatPortalSelectors.currentView(s),
chatPortalSelectors.showStandalonePortal(s),
s.openTopicComments,
portalThreadSelectors.portalCurrentThread(s),
s.activeAgentId,
s.activeGroupId,
s.activeThreadId,
]);
const composerTarget = useMemo<ComposerTarget>(() => {
if (!portalOpen || currentPortalView?.type !== PortalViewType.Thread) {
return createComposerTarget(
messageMapKey({
agentId: chatAgentId,
groupId: chatGroupId,
threadId: chatThreadId,
topicId,
}),
);
}
return resolveThreadComposerTarget({
contextKey: messageMapKey({
agentId: chatAgentId,
isNew: !currentPortalView.threadId,
scope: 'thread',
threadId: currentPortalView.threadId,
topicId,
}),
metadataResolved: !currentPortalView.threadId || !!portalThread,
sourceToolCallId: portalThread?.metadata?.sourceToolCallId,
});
}, [
chatAgentId,
chatGroupId,
chatThreadId,
currentPortalView,
portalOpen,
portalThread,
topicId,
]);
const portalWidth = getPortalViewWidth({
legacyWidth: legacyPortalWidth,
viewType: currentPortalView?.type,
widths: portalWidths,
});
const isChatMode = useAgentStore((s) =>
activeAgentId ? chatConfigByIdSelectors.isChatModeById(activeAgentId)(s) : false,
);
const isHetero = useAgentStore(agentSelectors.isCurrentAgentHeterogeneous);
// Match the folder picker: the runtime's implicit desktop/home cwd isn't a selected workspace.
const workingDirectory = useEffectiveWorkingDirectory(activeAgentId, { homeFallback: false });
// Effective target device for git ops — bound device for remote agents, this
// machine otherwise. Resolved the same way WorkingDirectoryPicker / GitStatus do.
const { agencyConfig, workspaceScoped } = useEffectiveAgencyConfig(activeAgentId);
const currentDeviceId = useElectronStore((s) => s.gatewayDeviceInfo?.deviceId);
const targetDeviceId = resolveTargetDeviceId(agencyConfig, currentDeviceId, {
workspaceScoped,
});
const topicWorkingDirectoryConfig = useChatStore(
(s) => topicSelectors.currentTopicMetadata(s)?.workingDirectoryConfig,
);
// A topic on its own worktree matches no device `workingDirs` entry (the
// device keeps one `activeWorktree` per repo), and a remote device can't be
// probed — so fall back to the repoType the topic persisted for that checkout,
// or Review disappears on web.
const deviceRepoType = useRepoType(workingDirectory, targetDeviceId);
const repoType =
deviceRepoType ??
(getWorkingDirEffectivePath(topicWorkingDirectoryConfig) === workingDirectory
? topicWorkingDirectoryConfig?.repoType
: undefined);
// The SOURCE repo, not the checkout — same fallback chain as
// WorkingDirectorySection: a persisted-worktree topic has no matching
// `workingDirs` entry, and committing the worktree path as sourcePath would
// rewrite the topic's repo source (see that component's comment).
const deviceDirs = useDeviceStore(deviceSelectors.getDeviceWorkingDirs(targetDeviceId));
const sourceWorkingDirectory = useMemo(() => {
if (!workingDirectory) return undefined;
const currentEntry = deviceDirs.find(
(entry) =>
(getWorkingDirectoryPathString(entry.git?.activeWorktree) ??
getWorkingDirectoryPathString(entry.path)) === workingDirectory,
);
const persistedConfig =
getWorkingDirEffectivePath(topicWorkingDirectoryConfig) === workingDirectory
? topicWorkingDirectoryConfig
: undefined;
return (
getWorkingDirectoryPathString(currentEntry?.path) ??
getWorkingDirectoryPathString(persistedConfig?.path) ??
workingDirectory
);
}, [deviceDirs, topicWorkingDirectoryConfig, workingDirectory]);
const deviceRoutingAvailable = useIsGatewayModeEnabled(activeAgentId);
const effectiveTarget = resolveExecutionTarget(agencyConfig, {
clientExecutionAvailable: isDesktop,
deviceRoutingAvailable,
isHetero,
workspaceScoped,
});
// Running against a bound device (remote, or this machine as a device): file
// tree + git reads go over RPC, so both Review and Files are reachable even
// when runtimeMode isn't `local`.
const isDeviceMode = effectiveTarget === 'device' && !!agencyConfig?.boundDeviceId;
// `targetDeviceId` also names the local desktop (directory state, Overview status).
// Files/Review route a deviceId only for remote RPC; "This device" keeps Electron IPC.
const remoteDeviceId = isDeviceMode ? agencyConfig.boundDeviceId : undefined;
const isLocalExecution = effectiveTarget === 'local';
// A cloud-sandbox run that keeps files has a tree on the drive to show too.
const {
active: isSandboxExecution,
directory: sandboxDirectory,
directoryKnown: sandboxDirectoryKnown,
instanceId: sandboxInstanceId,
topicId: sandboxTopicId,
} = useSandboxFilesDirectory(activeAgentId ?? '', {
isSandboxTarget: effectiveTarget === 'sandbox',
topicId,
});
const filesystemEnvironmentAvailable = isLocalExecution || isDeviceMode;
const environmentWorkingDirectory = filesystemEnvironmentAvailable ? workingDirectory : undefined;
const environmentRepoType = filesystemEnvironmentAvailable ? repoType : undefined;
// Files is agent-mode only; the hook owns which directory (if any) is showable.
const { deviceId: filesDeviceId, directory: filesDirectory } = useFilesPaneLocation({
activeAgentId,
filesystemEnvironmentAvailable,
isSandboxExecution,
remoteDeviceId,
sandboxDirectory,
sandboxDirectoryKnown,
sandboxInstanceId,
sandboxTopicId,
topicId,
workingDirectory,
});
const filesAvailable = !isChatMode && filesDirectory !== undefined;
const reviewAvailable = (isLocalExecution || isDeviceMode) && !!workingDirectory && !!repoType;
const {
dismissPullRequest,
linkPullRequest,
pickPullRequest,
probes: pullRequestProbes,
pullRequest,
pullRequests,
refresh: refreshPullRequest,
...pullRequestLink // confirmed, confirmError, onRetryConfirm
} = useSidebarPullRequests({
available: filesystemEnvironmentAvailable && !!workingDirectory,
deviceId: remoteDeviceId,
topicId,
workingDirectory: sourceWorkingDirectory ?? workingDirectory,
});
const paramsAvailable = !isHetero;
const toolsAvailable = !isHetero && !isChatMode;
// The in-app browser pages are renderer-retained Electron webviews — desktop only.
const browserAvailable = isDesktop;
const terminalAvailable = isDesktop;
// Must mint the same key the browser tools do (`sessionIdOf` in
// builtin-tool-browser), or the user and the agent would be looking at two
// different pages. A draft topic has no id yet, but the panel is openable
// there (user types a URL before sending anything), so it borrows a per-agent
// key until the topic materializes.
const browserSessionId = topicId
? `topic:${topicId}`
: `draft-agent:${activeAgentId ?? 'default'}`;
const [browserTabMetadata, setBrowserTabMetadata] = useState<Record<string, BrowserTabMetadata>>(
{},
);
const extraTabs = useExtraWorkingSidebarTabs({
activeAgentId,
environmentWorkingDirectory,
remoteDeviceId,
topicId,
});
const commentsAvailable = !!workspaceId && !!topicId;
const currentCommentTopicId =
currentPortalView?.type === PortalViewType.TopicComments ||
currentPortalView?.type === PortalViewType.TopicCommentThread
? currentPortalView.topicId
: undefined;
const isCurrentTopicComments = currentCommentTopicId === topicId;
const searchAvailable = !!topicId;
useFindInTopicHotkey(searchAvailable);
const tabDescriptors = useMemo<SidebarTabDescriptor[]>(
() => [
{ icon: SkillsIcon, key: 'skills', label: t('workingPanel.resources.filter.skills') },
...(toolsAvailable
? [{ icon: WrenchIcon, key: 'tools', label: t('tools.picker.title', { ns: 'setting' }) }]
: []),
// Documents / web resources are agent-document features — heterogeneous
// agents only carry filesystem skills, so hide both tabs there.
...(isHetero
? []
: [
{
icon: FileTextIcon,
key: 'documents',
label: t('workingPanel.resources.filter.documents'),
},
{ icon: GlobeIcon, key: 'web', label: t('workingPanel.resources.filter.web') },
]),
{ icon: BoxesIcon, key: 'works', label: t('workingPanel.works.title') },
...(searchAvailable
? [{ icon: SearchIcon, key: 'search', label: t('workingPanel.search.title') }]
: []),
...(commentsAvailable
? [{ icon: MessageCircleIcon, key: 'comments', label: t('topicComment.title') }]
: []),
...(reviewAvailable
? [{ icon: ClipboardListIcon, key: 'review', label: t('workingPanel.review.title') }]
: []),
...(pullRequest
? [
{
icon: PR_STATE_VISUAL[getPullRequestState(pullRequest)].icon,
key: 'pr',
label: `#${pullRequest.number}`,
},
]
: []),
...(filesAvailable
? [{ icon: FilesIcon, key: 'files', label: t('workingPanel.files.title') }]
: []),
...(browserAvailable
? [{ icon: Globe2Icon, key: 'browser', label: t('workingPanel.browser.title') }]
: []),
...(paramsAvailable
? [
{
icon: Settings2Icon,
key: 'params',
label: t('settingModel.params.panel.tab', { ns: 'setting' }),
},
]
: []),
...extraTabs,
],
[
browserAvailable,
extraTabs,
commentsAvailable,
filesAvailable,
isHetero,
paramsAvailable,
pullRequest,
reviewAvailable,
searchAvailable,
t,
toolsAvailable,
],
);
const { enabled: mcpAppsEnabled } = useMcpAppSidebar(composerTarget);
const availableTabs = useMemo(
() => new Map(tabDescriptors.map((tab) => [tab.key, tab])),
[tabDescriptors],
);
const availableTabsSignature = JSON.stringify(tabDescriptors.map((tab) => tab.key));
const isAvailableTab = useCallback(
(tab: string) =>
availableTabs.has(tab) ||
(browserAvailable && isBrowserTab(tab)) ||
isWorkTab(tab) ||
(mcpAppsEnabled && isMcpAppTab(tab)),
[availableTabs, browserAvailable, mcpAppsEnabled],
);
const openTabsContextKey = topicId
? `topic:${topicId}`
: `draft:${activeAgentId ?? 'default'}:${workingDirectory ?? 'none'}`;
const pinnedTabsAgentKey = activeAgentId ?? 'default';
const defaultOpenedTabs = useMemo(
() =>
[filesAvailable && 'files', 'skills', isHetero ? 'claudeMemory' : 'documents'].filter(
(tab): tab is string => !!tab && isAvailableTab(tab),
),
[filesAvailable, isAvailableTab, isHetero],
);
const [openTabsByContext, setOpenTabsByContext] = useLocalStorageState<Record<string, string[]>>(
OPEN_TABS_STORAGE_KEY,
{},
);
const [pinnedTabsByAgent, setPinnedTabsByAgent] = useLocalStorageState<Record<string, string[]>>(
PINNED_TABS_STORAGE_KEY,
{},
);
const lastTabRequestRef = useRef(tabRequest?.nonce);
const requestedTab =
tabRequest &&
lastTabRequestRef.current !== tabRequest.nonce &&
tabRequest.tab !== 'overview' &&
isAvailableTab(tabRequest.tab)
? tabRequest.tab
: undefined;
const pinnedTabs = useMemo(
() =>
(pinnedTabsByAgent[pinnedTabsAgentKey] ?? []).filter(
(tab) => !isDynamicTab(tab) && isAvailableTab(tab),
),
[isAvailableTab, pinnedTabsAgentKey, pinnedTabsByAgent],
);
const pinnedTabsSet = useMemo(() => new Set(pinnedTabs), [pinnedTabs]);
const openedTabs = Array.from(
new Set([
...pinnedTabs,
...(openTabsByContext[openTabsContextKey] ?? defaultOpenedTabs).filter(isAvailableTab),
...(requestedTab ? [requestedTab] : []),
]),
);
const openedTabsSignature = openedTabs.join('\0');
const [optimisticSelection, setOptimisticSelection] = useState<{
contextKey: string;
tab: string;
}>();
const previousStoredTabRef = useRef(storedTab);
const resolvedActiveTab: string =
storedTab && openedTabs.includes(storedTab) ? storedTab : (openedTabs[0] ?? 'overview');
const activeTab =
optimisticSelection?.contextKey === openTabsContextKey
? optimisticSelection.tab
: resolvedActiveTab;
const updateOpenedTabs = useCallback(
(updater: (tabs: string[]) => string[]) => {
setOpenTabsByContext((current) => ({
...current,
[openTabsContextKey]: updater(current[openTabsContextKey] ?? defaultOpenedTabs),
}));
},
[defaultOpenedTabs, openTabsContextKey, setOpenTabsByContext],
);
const updatePinnedTabs = useCallback(
(updater: (tabs: string[]) => string[]) => {
setPinnedTabsByAgent((current) => ({
...current,
[pinnedTabsAgentKey]: updater(current[pinnedTabsAgentKey] ?? []),
}));
},
[pinnedTabsAgentKey, setPinnedTabsByAgent],
);
const openTab = useCallback(
(tab: string) => {
if (tab !== 'overview' && !isAvailableTab(tab)) return;
if (tab !== activeTab) setOptimisticSelection({ contextKey: openTabsContextKey, tab });
if (tab !== 'overview') {
updateOpenedTabs((current) => (current.includes(tab) ? current : [...current, tab]));
openWorkingSidebar(tab);
}
if (tab === 'comments' && topicId && !isCurrentTopicComments) {
openTopicComments(topicId);
return;
}
if (tab === 'overview') setWorkingSidebarTab(tab);
},
[
isAvailableTab,
isCurrentTopicComments,
openTopicComments,
setWorkingSidebarTab,
topicId,
activeTab,
openWorkingSidebar,
openTabsContextKey,
updateOpenedTabs,
],
);
const openBrowserTab = useCallback(() => {
if (!browserAvailable) return undefined;
const currentBrowserTabs = (openTabsByContext[openTabsContextKey] ?? []).filter(isBrowserTab);
const tab =
currentBrowserTabs.length === 0 ? BROWSER_TAB_KEY : `${BROWSER_TAB_PREFIX}${nanoid(8)}`;
updateOpenedTabs((current) => [...current, tab]);
setOptimisticSelection({ contextKey: openTabsContextKey, tab });
openWorkingSidebar(tab);
return tab;
}, [
browserAvailable,
openTabsByContext,
openTabsContextKey,
openWorkingSidebar,
updateOpenedTabs,
]);
useEffect(() => {
if (previousStoredTabRef.current === storedTab) return;
previousStoredTabRef.current = storedTab;
setOptimisticSelection(undefined);
}, [storedTab]);
useEffect(() => {
setOptimisticSelection(undefined);
}, [availableTabsSignature, openTabsContextKey]);
useEffect(() => {
if (
activeTab !== 'comments' ||
!topicId ||
(!currentCommentTopicId && currentPortalView) ||
isCurrentTopicComments
)
return;
openTopicComments(topicId);
}, [
activeTab,
currentCommentTopicId,
currentPortalView,
isCurrentTopicComments,
openTopicComments,
topicId,
]);
useEffect(() => {
if (!tabRequest || lastTabRequestRef.current === tabRequest.nonce) return;
if (tabRequest.tab !== 'overview' && !isAvailableTab(tabRequest.tab)) return;
lastTabRequestRef.current = tabRequest.nonce;
if (tabRequest.tab !== 'overview') {
updateOpenedTabs((current) =>
current.includes(tabRequest.tab) ? current : [...current, tabRequest.tab],
);
}
}, [availableTabsSignature, isAvailableTab, tabRequest, updateOpenedTabs]);
const pinTab = useCallback(
(tab: string) => {
if (!availableTabs.has(tab) || isDynamicTab(tab)) return;
updateOpenedTabs((current) => (current.includes(tab) ? current : [...current, tab]));
updatePinnedTabs((current) => (current.includes(tab) ? current : [...current, tab]));
},
[availableTabs, updateOpenedTabs, updatePinnedTabs],
);
const unpinTab = useCallback(
(tab: string) => {
// A tab restored only through its agent pin should remain open in the
// current topic after unpinning instead of disappearing immediately.
updateOpenedTabs((current) => (current.includes(tab) ? current : [...current, tab]));
updatePinnedTabs((current) => current.filter((item) => item !== tab));
},
[updateOpenedTabs, updatePinnedTabs],
);
const closeTabs = useCallback(
(tabs: string[], fallbackTab = 'overview') => {
const removableTabs = new Set(tabs.filter((tab) => !pinnedTabsSet.has(tab)));
if (removableTabs.size === 0) return;
const remainingTabs = openedTabs.filter((tab) => !removableTabs.has(tab));
updateOpenedTabs(() => remainingTabs);
if (removableTabs.has(activeTab)) {
const nextTab = remainingTabs.includes(fallbackTab)
? fallbackTab
: (remainingTabs[0] ?? 'overview');
setOptimisticSelection({ contextKey: openTabsContextKey, tab: nextTab });
setWorkingSidebarTab(nextTab);
}
if (remainingTabs.length === 0) {
toggleRightPanel(false);
}
},
[
activeTab,
openedTabs,
openTabsContextKey,
pinnedTabsSet,
setWorkingSidebarTab,
toggleRightPanel,
updateOpenedTabs,
],
);
const closeTab = useCallback((tab: string) => closeTabs([tab]), [closeTabs]);
const displayedTabs = openedTabs
.map(
(tab) =>
dynamicTabDescriptor(
tab,
browserTabMetadata[`${openTabsContextKey}:${tab}`],
t('workingPanel.browser.title'),
activeAgentId,
) ?? availableTabs.get(tab),
)
.filter((tab): tab is SidebarTabDescriptor => Boolean(tab));
const createTabContextMenuItems = useTabContextMenuItems({
closeTab,
closeTabs,
openedTabs,
pinTab,
pinnedTabsSet,
unpinTab,
});
const tabsRef = useRef<HTMLDivElement>(null);
const pendingTabFocusRef = useRef<string | undefined>(undefined);
const pendingTabFocusTimeoutRef = useRef<number | undefined>(undefined);
const scrollActiveTabIntoView = useCallback(() => {
const tabs = tabsRef.current;
const activeTabButton = tabs?.querySelector<HTMLButtonElement>('button[aria-pressed="true"]');
const activeTabItem = activeTabButton?.parentElement;
if (!tabs || !activeTabItem) return;
const tabsRect = tabs.getBoundingClientRect();
const activeTabRect = activeTabItem.getBoundingClientRect();
if (activeTabRect.left < tabsRect.left) {
tabs.scrollLeft += activeTabRect.left - tabsRect.left;
} else if (activeTabRect.right > tabsRect.right) {
tabs.scrollLeft += activeTabRect.right - tabsRect.right;
}
}, []);
const focusPendingTab = useCallback(() => {
const pendingTab = pendingTabFocusRef.current;
if (!pendingTab) return;
const tabButton = Array.from(
tabsRef.current?.querySelectorAll<HTMLButtonElement>('button[data-tab-key]') ?? [],
).find((button) => button.dataset.tabKey === pendingTab);
if (!tabButton) return;
if (pendingTabFocusTimeoutRef.current !== undefined) {
window.clearTimeout(pendingTabFocusTimeoutRef.current);
pendingTabFocusTimeoutRef.current = undefined;
}
tabButton.focus();
pendingTabFocusRef.current = undefined;
}, []);
const requestTabFocus = useCallback(
(tab: string) => {
pendingTabFocusRef.current = tab;
if (pendingTabFocusTimeoutRef.current !== undefined) {
window.clearTimeout(pendingTabFocusTimeoutRef.current);
}
// Base UI normally resolves this through onOpenChangeComplete. A short
// fallback also covers background Electron windows where transition
// completion is frame-throttled indefinitely.
pendingTabFocusTimeoutRef.current = window.setTimeout(() => {
if (pendingTabFocusRef.current === tab) focusPendingTab();
}, 300);
},
[focusPendingTab],
);
useEffect(() => {
scrollActiveTabIntoView();
}, [
activeTab,
availableTabsSignature,
openedTabsSignature,
scrollActiveTabIntoView,
storedWidth,
tabRequest?.nonce,
]);
useEffect(
() => () => {
if (pendingTabFocusTimeoutRef.current !== undefined) {
window.clearTimeout(pendingTabFocusTimeoutRef.current);
}
},
[],
);
// Review's tree-nav rail lives here (not inside Review) so the panel can widen
// when the two-pane layout is on. Hidden by default — the panel shows only the
// diff list until the user opens the tree from the toolbar. Persisted so it
// survives reloads.
const [showReviewTree, setShowReviewTree] = useLocalStorageState<boolean>(
REVIEW_TREE_STORAGE_KEY,
false,
);
const reviewTwoPane = activeTab === 'review' && reviewAvailable && showReviewTree;
const preferredMinWidth = reviewTwoPane
? TWO_PANE_MIN_WIDTH
: isWorkTab(activeTab)
? CANVAS_MIN_WIDTH
: 0;
const displayWidth = Math.max(storedWidth, preferredMinWidth);
// Yield the row to conversation + portal when the three no longer fit. A
// stored width that merely outgrew the current row (the user dragged the
// panel out, or resized the window down) renders clamped instead of
// unmounting the whole panel — the sidebar disappears only when even its
// minimum width leaves no room for the conversation. Either way this only
// overrides the rendered state — `showRightPanel` keeps the user's own
// choice, so the sidebar comes back by itself once there is room again.
const minDisplayWidth = reviewTwoPane ? TWO_PANE_MIN_WIDTH : MIN_PANEL_WIDTH;
const widthBudget = sidebarWidthBudget({
availableWidth,
portalWidth: portalOpen ? portalWidth : 0,
});
const fits = widthBudget >= minDisplayWidth;
const panelVisible = !!showRightPanel && fits;
const retainReview = useRetainAfterHide(panelVisible, REVIEW_RETAIN_MS);
const overviewFits = widthBudget >= MIN_PANEL_WIDTH;
const renderWidth = Math.min(displayWidth, Math.max(widthBudget, minDisplayWidth));
// Also cap the drag range so releasing a drag can never persist a width that
// immediately fails the fit check and hides the panel.
const maxPanelWidth = Math.min(MAX_PANEL_WIDTH, Math.max(widthBudget, minDisplayWidth));
const openMenuItems = useOpenMenuItems({
agentId: activeAgentId,
mcpAppsAvailable: mcpAppsEnabled && composerTarget.writable,
availableTabs,
extraTabs,
openBrowserTab,
openTab,
openedTabs,
requestTabFocus,
scope: composerTarget.writable ? composerTarget.contextKey : undefined,
terminalAvailable,
toggleTerminalPanel,
topicId: topicId ?? undefined,
});
// The floating card keeps a 16px margin on each side.
const overviewGutter = 32;
const overviewWidth = Math.min(OVERVIEW_PANEL_WIDTH, widthBudget - overviewGutter);
const overviewPanel = (
<OverviewSlot>
<AnimatePresence initial={false}>
{showWorkingOverview && overviewFits && (
<m.div
animate={{ width: overviewWidth + overviewGutter }}
{...stylex.props(styles.overviewSlot)}
exit={{ transition: overviewExitTransition, width: 0 }}
initial={{ width: 0 }}
transition={OVERVIEW_TRANSITION}
>
<m.div
animate={{ opacity: 1, x: 0 }}
className={stylex.props(styles.overviewPanel).className}
exit={{ opacity: 0, transition: overviewExitTransition, x: 12 }}
initial={{ opacity: 0, x: 12 }}
role={'complementary'}
style={{ width: overviewWidth }}
transition={OVERVIEW_TRANSITION}
>
<Flexbox className={stylex.props(styles.overviewBody).className}>
<Overview
active
agentId={activeAgentId}
deviceId={remoteDeviceId}
environmentAvailable={filesystemEnvironmentAvailable}
pullRequest={pullRequest}
pullRequests={pullRequests}
repoType={environmentRepoType}
sourcePath={sourceWorkingDirectory}
statusDeviceId={getStatusDeviceId(effectiveTarget, agencyConfig?.boundDeviceId)}
workingDirectory={environmentWorkingDirectory}
onDismissPullRequest={dismissPullRequest}
onLinkPullRequest={linkPullRequest}
onOpenTab={openTab}
onRefreshPullRequest={refreshPullRequest}
onOpenPullRequest={
pickPullRequest &&
((view) =>
pickPullRequest(view) ? openTab('pr') : openTrustedExternalUrl(view.url))
}
/>
</Flexbox>
</m.div>
</m.div>
)}
</AnimatePresence>
</OverviewSlot>
);
return (
<PanelDensityContext value={interfaceDensity}>
{overviewPanel}
<TopicPullRequestProbes
deviceId={remoteDeviceId}
path={sourceWorkingDirectory ?? environmentWorkingDirectory}
probes={pullRequestProbes}
/>
<RightPanel
collapseThreshold={320}
defaultWidth={renderWidth}
expand={Boolean(showRightPanel) && fits}
maxWidth={maxPanelWidth}
minWidth={Math.min(Math.max(MIN_PANEL_WIDTH, preferredMinWidth), maxPanelWidth)}
style={!showRightPanel ? { visibility: 'hidden' } : undefined}
width={renderWidth}
onSizeChange={(size) => {
if (!size?.width) return;
// The size type allows a string on either axis, so narrow before storing.
const w = typeof size.width === 'string' ? Number.parseInt(size.width) : size.width;
if (!Number.isFinite(w) || w === storedWidth) return;
updateSystemStatus({ workingSidebarWidth: w });
}}
>
<Flexbox height={'100%'} width={'100%'}>
<Flexbox
horizontal
align={'center'}
className={stylex.props(styles.header).className}
gap={4}
height={headerHeight}
justify={'space-between'}
paddingInline={6}
>
<div {...stylex.props(styles.tabsArea)}>
<div {...stylex.props(styles.tabs)} ref={tabsRef}>
{displayedTabs.map((tab, index) => (
<WorkspaceTab
active={activeTab === tab.key}
closeLabel={t('workingPanel.tabs.close')}
contextMenuItems={createTabContextMenuItems(tab.key, index)}
icon={tab.icon}
iconNode={tab.iconNode}
key={tab.key}
label={tab.label}
pinned={pinnedTabsSet.has(tab.key)}
pinnedLabel={t('workingPanel.tabs.pinned')}
tabKey={tab.key}
onClose={pinnedTabsSet.has(tab.key) ? undefined : () => closeTab(tab.key)}
onSelect={() => openTab(tab.key)}
/>
))}
</div>
<DropdownMenu
items={openMenuItems}
placement={'bottomRight'}
onOpenChangeComplete={(open) => {
if (open) return;
if (pendingTabFocusRef.current) focusPendingTab();
else scrollActiveTabIntoView();
}}
>
<ActionIcon
className={stylex.props(styles.add).className}
icon={PlusIcon}
size={headerActionSize}
title={t('workingPanel.openMenu.title')}
/>
</DropdownMenu>
</div>
<ActionIcon
className={stylex.props(styles.close).className}
icon={PanelRightCloseIcon}
size={headerActionSize}
onClick={() => toggleRightPanel(false)}
/>
</Flexbox>
<Flexbox className={stylex.props(styles.body).className} width={'100%'}>
{!contentReady && storedTab !== 'files' && activeTab !== 'files' && (
<DeferredSkeleton />
)}
<FilesPane
active={!!showRightPanel && activeTab === 'files'}
available={filesAvailable}
className={stylex.props(styles.pane).className}
contentReady={contentReady || (activeTab === 'files' && filesAvailable)}
deviceId={filesDeviceId}
directory={filesDirectory}
prefetch={activeTab === 'files' && (!showRightPanel || !contentReady)}
sandboxInstanceId={isSandboxExecution ? sandboxInstanceId : undefined}
sandboxTopicId={sandboxTopicId}
/>
{contentReady && (
<>
{commentsAvailable && (
<Flexbox
style={{ overflow: 'hidden' }}
className={
stylex.props(activeTab === 'comments' ? styles.pane : styles.paneHidden)
.className
}
>
<TopicCommentsSidebar />
</Flexbox>
)}
{!!topicId && (
<Activity mode={showRightPanel && activeTab === 'search' ? 'visible' : 'hidden'}>
<Flexbox
className={stylex.props(styles.pane).className}
style={{ overflow: 'hidden' }}
>
<TopicSearchPane
active={!!showRightPanel && activeTab === 'search'}
key={topicId}
topicId={topicId}
/>
</Flexbox>
</Activity>
)}
{paramsAvailable && activeTab === 'params' && (
<Flexbox className={stylex.props(styles.pane).className}>
<Suspense
fallback={
<Skeleton.Text
className={stylex.props(styles.paramsLoading).className}
rows={6}
/>
}
>
<ParamsSection />
</Suspense>
</Flexbox>
)}
{toolsAvailable && activeTab === 'tools' && (
<Flexbox
className={stylex.props(styles.pane).className}
style={{ overflow: 'hidden' }}
>
<Suspense
fallback={
<Skeleton.Text
className={stylex.props(styles.paramsLoading).className}
rows={6}
/>
}
>
<ToolsSection />
</Suspense>
</Flexbox>
)}
{/* Switching tabs unmounts Review to release its diff DOM (#18984).
Collapsing the panel hides it for REVIEW_RETAIN_MS first: remounting
all diffs and the tree on a quick reopen stalls the open animation
for ~200ms. */}
{reviewAvailable && activeTab === 'review' && retainReview && (
<Activity mode={panelVisible ? 'visible' : 'hidden'}>
<Flexbox className={stylex.props(styles.pane).className}>
<Review
active={panelVisible}
composerTarget={composerTarget}
deviceId={remoteDeviceId}
showTree={showReviewTree}
workingDirectory={workingDirectory}
onToggleTree={() => setShowReviewTree((v) => !v)}
/>
</Flexbox>
</Activity>
)}
{pullRequest && workingDirectory && (
// The PR tab renders at MIN_PANEL_WIDTH: it fits exactly when Overview does.
<PullRequestSidebarTab
{...pullRequestLink}
active={!!showRightPanel && activeTab === 'pr'}
canShow={overviewFits}
className={stylex.props(styles.pane).className}
deviceId={remoteDeviceId}
pullRequest={pullRequest}
repositoryPath={sourceWorkingDirectory}
workingDirectory={workingDirectory}
onOpenTab={openTab}
/>
)}
<DynamicTabPanes
activeTab={activeTab}
agentId={activeAgentId}
browserAvailable={browserAvailable}
browserSessionId={browserSessionId}
composerTarget={composerTarget}
openedTabs={openedTabs}
topicId={topicId ?? undefined}
visible={!!showRightPanel}
onCloseTab={closeTab}
onBrowserMetadataChange={(tab, metadata) => {
const metadataKey = `${openTabsContextKey}:${tab}`;
setBrowserTabMetadata((current) =>
current[metadataKey]?.faviconUrl === metadata.faviconUrl &&
current[metadataKey]?.title === metadata.title &&
current[metadataKey]?.url === metadata.url
? current
: { ...current, [metadataKey]: metadata },
);
}}
/>
{extraTabs.map((tab) => (
<Flexbox
key={tab.key}
className={
stylex.props(activeTab === tab.key ? styles.pane : styles.paneHidden)
.className
}
>
{tab.pane}
</Flexbox>
))}
{/* Resource/works panes stay mounted to keep their state, but hidden ones
go through Activity so their updates render at background priority
instead of blocking visible commits (BrowserPane must NOT move here —
hiding it would unmount the effects keeping its session alive). */}
{['skills', ...(isHetero ? [] : ['documents', 'web'])].map((resourceTab) => (
<Activity
key={resourceTab}
mode={showRightPanel && activeTab === resourceTab ? 'visible' : 'hidden'}
>
<Flexbox className={stylex.props(styles.pane).className} width={'100%'}>
<ResourcesSection
deviceId={remoteDeviceId}
enabled={showRightPanel && activeTab === resourceTab}
filter={resourceTab as 'skills' | 'documents' | 'web'}
/>
</Flexbox>
</Activity>
))}
<Activity mode={showRightPanel && activeTab === 'works' ? 'visible' : 'hidden'}>
<Flexbox className={stylex.props(styles.pane).className}>
<WorksSection active={showRightPanel && activeTab === 'works'} />
</Flexbox>
</Activity>
</>
)}
</Flexbox>
</Flexbox>
</RightPanel>
</PanelDensityContext>
);
});
export default AgentWorkingSidebar;