Skip to content

🤯 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.

🐛 fix(desktop): keep sidebar agent header on the tab's agent after Home (#22825)

YuTengjingc29ef4077687
diff --git a/src/features/Electron/ActiveConversationBridge/coordinate.ts b/src/features/Electron/ActiveConversationBridge/coordinate.ts
index f743fc59afc..ca7df637c66 100644
--- a/src/features/Electron/ActiveConversationBridge/coordinate.ts
+++ b/src/features/Electron/ActiveConversationBridge/coordinate.ts
@@ -1,3 +1,5 @@
+import { isHomePathname } from '@/features/HomeLayout/isHomePathname';
+
export interface ActiveConversationCoordinate {
agentBasePath?: string;
agentId?: string;
@@ -5,6 +7,8 @@ export interface ActiveConversationCoordinate {
groupId?: string;
hash: string;
isConversation: boolean;
+ /** The Home page, which projects the inbox agent as the active agent. */
+ isHome: boolean;
pathname: string;
projectBasePath?: string;
projectId?: string;
@@ -18,12 +22,14 @@ interface ResolveActiveConversationCoordinateOptions {
params: { aid?: string; gid?: string; projectId?: string; topicId?: string };
resolvedAgentId?: string;
url: string;
+ workspaceSlug?: string | null;
}
export const resolveActiveConversationCoordinate = ({
params,
resolvedAgentId,
url,
+ workspaceSlug,
}: ResolveActiveConversationCoordinateOptions): ActiveConversationCoordinate => {
const location = new URL(url, 'https://desktop.local');
const segments = location.pathname.split('/').filter(Boolean);
@@ -75,6 +81,7 @@ export const resolveActiveConversationCoordinate = ({
groupId: groupSegmentIndex >= 0 ? params.gid : undefined,
hash: location.hash,
isConversation,
+ isHome: isHomePathname(location.pathname, workspaceSlug),
pathname: location.pathname,
projectBasePath,
projectId: projectConversation ? params.projectId : undefined,
diff --git a/src/features/Electron/ActiveConversationBridge/index.tsx b/src/features/Electron/ActiveConversationBridge/index.tsx
index 5ffba723345..0d3f40a96de 100644
--- a/src/features/Electron/ActiveConversationBridge/index.tsx
+++ b/src/features/Electron/ActiveConversationBridge/index.tsx
@@ -2,9 +2,12 @@
import { useLayoutEffect, useMemo, useRef } from 'react';
+import { useActiveWorkspaceSlug } from '@/business/client/hooks/useActiveWorkspaceSlug';
import { useResolvedAgentRouteId } from '@/features/AgentRoute/useResolvedAgentRouteId';
import { selectActiveTabUrl } from '@/features/Electron/shell/activeTabUrl';
import { useActiveRouteParams } from '@/hooks/useActiveRouteParams';
+import { useAgentStore } from '@/store/agent';
+import { builtinAgentSelectors } from '@/store/agent/selectors';
import { useElectronStore } from '@/store/electron';
import { resolveActiveConversationCoordinate } from './coordinate';
@@ -20,21 +23,25 @@ const ActiveConversationBridge = () => {
}>();
const url = useElectronStore(selectActiveTabUrl) || '/';
const { agentId } = useResolvedAgentRouteId(params.aid);
+ const workspaceSlug = useActiveWorkspaceSlug();
+ // Re-project once the inbox id hydrates so Home adopts it after a cold boot.
+ const inboxAgentId = useAgentStore(builtinAgentSelectors.inboxAgentId);
const coordinate = useMemo(
() =>
resolveActiveConversationCoordinate({
params,
resolvedAgentId: agentId,
url,
+ workspaceSlug,
}),
- [agentId, params, url],
+ [agentId, params, url, workspaceSlug],
);
const coordinateRef = useRef(coordinate);
coordinateRef.current = coordinate;
useLayoutEffect(() => {
projectActiveConversationCoordinate(coordinate);
- }, [coordinate]);
+ }, [coordinate, inboxAgentId]);
useLayoutEffect(() => {
return subscribeActiveConversationNavigation(() => coordinateRef.current);
diff --git a/src/features/Electron/ActiveConversationBridge/projectCoordinate.test.ts b/src/features/Electron/ActiveConversationBridge/projectCoordinate.test.ts
index dded4c4514b..8bf0b926e43 100644
--- a/src/features/Electron/ActiveConversationBridge/projectCoordinate.test.ts
+++ b/src/features/Electron/ActiveConversationBridge/projectCoordinate.test.ts
@@ -1,3 +1,4 @@
+import { INBOX_SESSION_ID } from '@lobechat/const';
import { type AgentGroupDetail } from '@lobechat/types';
import { beforeEach, describe, expect, it } from 'vitest';
@@ -302,4 +303,40 @@ describe('active conversation projection', () => {
activeTopicId: null,
});
});
+
+ describe('route-less pages', () => {
+ beforeEach(() => {
+ useAgentStore.setState({ builtinAgentIdMap: { [INBOX_SESSION_ID]: 'agent-inbox' } }, false);
+ });
+
+ it.each([
+ ['personal Home', '/', null],
+ ['workspace Home', '/acme', 'acme'],
+ ['workspace Home with a trailing slash', '/acme/', 'acme'],
+ ])('projects the inbox agent on %s', (_label, url, workspaceSlug) => {
+ projectActiveConversationCoordinate(
+ resolveActiveConversationCoordinate({ params: {}, url, workspaceSlug }),
+ );
+
+ expect(useAgentStore.getState().activeAgentId).toBe('agent-inbox');
+ expect(useChatStore.getState()).toMatchObject({
+ activeAgentId: undefined,
+ activeTopicId: undefined,
+ });
+ });
+
+ it('clears the active agent on a route-less page other than Home', () => {
+ useAgentStore.setState({ activeAgentId: 'agent-inbox' }, false);
+
+ projectActiveConversationCoordinate(
+ resolveActiveConversationCoordinate({
+ params: {},
+ url: '/settings',
+ workspaceSlug: 'acme',
+ }),
+ );
+
+ expect(useAgentStore.getState().activeAgentId).toBeUndefined();
+ });
+ });
});
diff --git a/src/features/Electron/ActiveConversationBridge/projectCoordinate.ts b/src/features/Electron/ActiveConversationBridge/projectCoordinate.ts
index 439770d3bef..52f66cf2301 100644
--- a/src/features/Electron/ActiveConversationBridge/projectCoordinate.ts
+++ b/src/features/Electron/ActiveConversationBridge/projectCoordinate.ts
@@ -1,4 +1,5 @@
import { useAgentStore } from '@/store/agent';
+import { builtinAgentSelectors } from '@/store/agent/selectors';
import { useAgentGroupStore } from '@/store/agentGroup';
import { useChatStore } from '@/store/chat';
import { topicSelectors } from '@/store/chat/selectors';
@@ -122,11 +123,17 @@ export const projectActiveConversationCoordinate = (
}
if (!coordinate.routeAgentId) {
- if (useAgentStore.getState().activeAgentId !== undefined) {
+ // Home works against the inbox agent; every other route-less page has none.
+ const routeLessAgentId = coordinate.isHome
+ ? builtinAgentSelectors.inboxAgentId(useAgentStore.getState())
+ : undefined;
+ if (useAgentStore.getState().activeAgentId !== routeLessAgentId) {
useAgentStore.setState(
- { activeAgentId: undefined },
+ { activeAgentId: routeLessAgentId },
false,
- 'ActiveConversationBridge/leaveAgent',
+ coordinate.isHome
+ ? 'ActiveConversationBridge/syncHomeAgent'
+ : 'ActiveConversationBridge/leaveAgent',
);
}
if (
diff --git a/src/features/HomeLayout/HomeAgentIdSync.desktop.tsx b/src/features/HomeLayout/HomeAgentIdSync.desktop.tsx
new file mode 100644
index 00000000000..3187ee319ac
--- /dev/null
+++ b/src/features/HomeLayout/HomeAgentIdSync.desktop.tsx
@@ -0,0 +1,14 @@
+/**
+ * Desktop: `ActiveConversationBridge` in the Electron shell owns `activeAgentId`
+ * and projects the inbox agent onto the Home route itself, the same way the
+ * agent layout's `useAgentIdStoreSync.desktop` defers to it.
+ *
+ * Home lives in a keep-alive tab pane that commits on a deferred lane, after the
+ * shell bridge. Writing from here let a hidden Home pane's cleanup clear the
+ * agent the bridge had just projected for the newly active tab, and the next
+ * agent config fetch (often the inbox) then adopted itself as active, so the
+ * agent sidebar header showed the inbox agent over another agent's topic.
+ */
+const HomeAgentIdSync = () => null;
+
+export default HomeAgentIdSync;
diff --git a/src/features/HomeLayout/index.tsx b/src/features/HomeLayout/index.tsx
index 1a2f69315e0..01472d4b56f 100644
--- a/src/features/HomeLayout/index.tsx
+++ b/src/features/HomeLayout/index.tsx
@@ -6,6 +6,7 @@ import { useActiveWorkspaceSlug } from '@/business/client/hooks/useActiveWorkspa
import { useIsDark } from '@/hooks/useIsDark';
import HomeAgentIdSync from './HomeAgentIdSync';
+import { isHomePathname } from './isHomePathname';
import RecentSync from './RecentSync';
import { styles } from './style';
@@ -18,9 +19,7 @@ const Layout: FC<LayoutProps> = ({ children }) => {
const theme = useTheme(); // Keep for colorBgContainerSecondary (not in cssVar)
const { pathname } = useLocation();
const activeSlug = useActiveWorkspaceSlug();
- const isHomeRoute =
- pathname === '/' ||
- (!!activeSlug && (pathname === `/${activeSlug}` || pathname === `/${activeSlug}/`));
+ const isHomeRoute = isHomePathname(pathname, activeSlug);
const [hasActivated, setHasActivated] = useState(isHomeRoute);
const content = children ?? <Outlet />;
diff --git a/src/features/HomeLayout/isHomePathname.ts b/src/features/HomeLayout/isHomePathname.ts
new file mode 100644
index 00000000000..91cbdf596c0
--- /dev/null
+++ b/src/features/HomeLayout/isHomePathname.ts
@@ -0,0 +1,7 @@
+/**
+ * Whether a pathname is the Home page: `/` in personal scope, or the workspace
+ * root (`/<slug>` / `/<slug>/`) when a workspace is active.
+ */
+export const isHomePathname = (pathname: string, workspaceSlug?: string | null): boolean =>
+ pathname === '/' ||
+ (!!workspaceSlug && (pathname === `/${workspaceSlug}` || pathname === `/${workspaceSlug}/`));