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: keep inbox route valid during initialization (#17759)

YuTengjing7925160e2561
diff --git a/src/features/AgentRoute/useResolvedAgentRouteId.test.ts b/src/features/AgentRoute/useResolvedAgentRouteId.test.ts
new file mode 100644
index 00000000000..dcee024fdd9
--- /dev/null
+++ b/src/features/AgentRoute/useResolvedAgentRouteId.test.ts
@@ -0,0 +1,14 @@
+import { BUILTIN_AGENT_SLUGS } from '@lobechat/builtin-agents';
+import { describe, expect, it } from 'vitest';
+
+import { resolveInboxAgentRouteId } from './useResolvedAgentRouteId';
+
+describe('resolveInboxAgentRouteId', () => {
+ it('should use the stable inbox slug while the persisted agent ID is loading', () => {
+ expect(resolveInboxAgentRouteId()).toBe(BUILTIN_AGENT_SLUGS.inbox);
+ });
+
+ it('should use the persisted agent ID after initialization', () => {
+ expect(resolveInboxAgentRouteId('inbox-agent-id')).toBe('inbox-agent-id');
+ });
+});
diff --git a/src/features/AgentRoute/useResolvedAgentRouteId.ts b/src/features/AgentRoute/useResolvedAgentRouteId.ts
index 27e46224224..6f68441305f 100644
--- a/src/features/AgentRoute/useResolvedAgentRouteId.ts
+++ b/src/features/AgentRoute/useResolvedAgentRouteId.ts
@@ -5,6 +5,13 @@ import { builtinAgentSelectors } from '@/store/agent/selectors';
const builtinAgentSlugs = new Set<string>(Object.values(BUILTIN_AGENT_SLUGS));
+/**
+ * Keep the inbox route valid while its persisted agent ID is still loading.
+ * `useResolvedAgentRouteId` resolves the stable slug once initialization finishes.
+ */
+export const resolveInboxAgentRouteId = (inboxAgentId?: string) =>
+ inboxAgentId ?? BUILTIN_AGENT_SLUGS.inbox;
+
export const useResolvedAgentRouteId = (routeAgentId?: string) => {
const isBuiltinSlug = !!routeAgentId && builtinAgentSlugs.has(routeAgentId);
const resolvedAgentId = useAgentStore(
diff --git a/src/routes/(main)/home/_layout/Body/Agent/List/InboxItem.tsx b/src/routes/(main)/home/_layout/Body/Agent/List/InboxItem.tsx
index fa2751c4a84..372902b604b 100644
--- a/src/routes/(main)/home/_layout/Body/Agent/List/InboxItem.tsx
+++ b/src/routes/(main)/home/_layout/Body/Agent/List/InboxItem.tsx
@@ -7,6 +7,7 @@ import { Loader2 } from 'lucide-react';
import { type CSSProperties } from 'react';
import { memo } from 'react';
+import { resolveInboxAgentRouteId } from '@/features/AgentRoute/useResolvedAgentRouteId';
import NavItem from '@/features/NavPanel/components/NavItem';
import WorkspaceLink from '@/features/Workspace/WorkspaceLink';
import { usePrefetchAgent } from '@/hooks/usePrefetchAgent';
@@ -51,7 +52,8 @@ interface InboxItemProps {
const InboxItem = memo<InboxItemProps>(({ className, style }) => {
const inboxAgentId = useAgentStore(builtinAgentSelectors.inboxAgentId);
- const inboxMeta = useAgentStore(agentSelectors.getAgentMetaById(inboxAgentId!));
+ const inboxRouteAgentId = resolveInboxAgentRouteId(inboxAgentId);
+ const inboxMeta = useAgentStore(agentSelectors.getAgentMetaById(inboxRouteAgentId));
const isLoading = useChatStore(
inboxAgentId ? operationSelectors.isAgentVisiblyRunning(inboxAgentId) : () => false,
@@ -59,10 +61,10 @@ const InboxItem = memo<InboxItemProps>(({ className, style }) => {
const prefetchAgent = usePrefetchAgent();
const inboxAgentTitle = inboxMeta.title || 'Lobe AI';
const inboxAgentAvatar = inboxMeta.avatar || DEFAULT_INBOX_AVATAR;
- const inboxUrl = usePreservedAgentUrl(inboxAgentId!);
+ const inboxUrl = usePreservedAgentUrl(inboxRouteAgentId);
// Prefetch agent layout chunk and data eagerly since Lobe AI is almost always clicked
- prefetchAgent(inboxAgentId!);
+ if (inboxAgentId) prefetchAgent(inboxAgentId);
const avatarNode = (
<Avatar emojiScaleWithBackground avatar={inboxAgentAvatar} shape={'square'} size={24} />
diff --git a/src/routes/(mobile)/(home)/features/SessionListContent/Inbox/index.tsx b/src/routes/(mobile)/(home)/features/SessionListContent/Inbox/index.tsx
index ebcd804c31a..6f8ed90742d 100644
--- a/src/routes/(mobile)/(home)/features/SessionListContent/Inbox/index.tsx
+++ b/src/routes/(mobile)/(home)/features/SessionListContent/Inbox/index.tsx
@@ -3,6 +3,7 @@ import { memo } from 'react';
import { Link } from 'react-router';
import { DEFAULT_INBOX_AVATAR } from '@/const/meta';
+import { resolveInboxAgentRouteId } from '@/features/AgentRoute/useResolvedAgentRouteId';
import { useNavigateToAgent } from '@/hooks/useNavigateToAgent';
import { useAgentStore } from '@/store/agent';
import { builtinAgentSelectors } from '@/store/agent/selectors';
@@ -17,14 +18,15 @@ const Inbox = memo(() => {
const isInboxActive = useSessionStore(sessionSelectors.isInboxSession);
const navigateToAgent = useNavigateToAgent();
const inboxAgentId = useAgentStore(builtinAgentSelectors.inboxAgentId);
+ const inboxRouteAgentId = resolveInboxAgentRouteId(inboxAgentId);
return (
<Link
aria-label={'Lobe AI'}
- to={AGENT_CHAT_URL(inboxAgentId, mobile)}
+ to={AGENT_CHAT_URL(inboxRouteAgentId, mobile)}
onClick={(e) => {
e.preventDefault();
- navigateToAgent(inboxAgentId);
+ navigateToAgent(inboxRouteAgentId);
}}
>
<ListItem