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(messages): open the main agent's direct Channel from its row (#22624)

Arvin Xud878f83ebaad
diff --git a/src/features/MessageList/AgentConversation.tsx b/src/features/MessageList/AgentConversation.tsx
index 4606a11072e..907d183f7f0 100644
--- a/src/features/MessageList/AgentConversation.tsx
+++ b/src/features/MessageList/AgentConversation.tsx
@@ -1,18 +1,14 @@
'use client';
import { agentDisplayName } from '@lobechat/types';
-import { type DropdownItem } from '@lobehub/ui';
import { memo, useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import AgentAvatar from '@/components/AgentAvatar';
-import { directAvatarState, replyAvatarState } from '@/features/Channels/jobStatus';
-import { useChannelMenu } from '@/features/Channels/useChannelMenu';
import { useAgentDropdownMenu } from '@/features/HomeSidebar/Body/Agent/List/AgentItem/useDropdownMenu';
import { usePreservedAgentUrl } from '@/features/HomeSidebar/Body/Agent/List/usePreservedAgentUrl';
import { useAgentModal } from '@/features/HomeSidebar/Body/Agent/ModalProvider';
import { usePrefetchAgent } from '@/hooks/usePrefetchAgent';
-import { channelSelectors, useChannelStore } from '@/store/channel';
import { useChatStore } from '@/store/chat';
import { operationSelectors } from '@/store/chat/selectors';
@@ -20,22 +16,7 @@ import ConversationRow from './ConversationRow';
import { type Conversation } from './conversations';
import { AVATAR_SIZE } from './style';
import { useAgentConversationLink } from './useAgentConversationLink';
-
-/** The direct Channel's own conversation action, next to the Agent's own menu. */
-const CONVERSATION_ACTION_KEYS = new Set(['unarchive']);
-
-/**
- * A direct Channel is the chat with one Agent, so it cannot be archived or trashed. The row only
- * frees one that was archived earlier. Keys are prefixed so they cannot collide with the Agent's
- * own menu.
- */
-const conversationActions = (menu: DropdownItem[]): DropdownItem[] =>
- menu.flatMap((item) => {
- if (!item || !('key' in item)) return [];
- const key = String(item.key);
- if (!CONVERSATION_ACTION_KEYS.has(key)) return [];
- return [{ ...item, key: `channel-${key}` } as DropdownItem];
- });
+import { useDirectChannelRow } from './useDirectChannelRow';
interface AgentConversationProps {
active: boolean;
@@ -51,18 +32,7 @@ const AgentConversation = memo<AgentConversationProps>(({ active, conversation }
const chatRunning = useChatStore(operationSelectors.isAgentVisiblyRunning(id));
const agentUrl = usePreservedAgentUrl(id);
const { onClick, url } = useAgentConversationLink(id, agentUrl);
- const channelMenu = useChannelMenu();
- const directChannel = conversation.channel;
- // A direct Channel replies through Channel jobs, not chat operations. The open conversation's
- // view is polled while it works, so it is fresher than the list; any other cached view may be
- // stale, so other rows read the jobs the list carries.
- const replyState = useChannelStore((s) => {
- if (!directChannel) return;
- const detail = active
- ? channelSelectors.detail({ channelId: directChannel.id, threadId: null })(s)
- : undefined;
- return detail ? directAvatarState(detail) : replyAvatarState(directChannel.replyJobs ?? []);
- });
+ const { channelActions, replyState } = useDirectChannelRow(conversation.channel, active);
const avatarState = replyState ?? (chatRunning ? 'working' : undefined);
const title = agentDisplayName(item, t('untitledAgent'));
const avatarSrc = typeof avatar === 'string' ? avatar : undefined;
@@ -91,12 +61,11 @@ const AgentConversation = memo<AgentConversationProps>(({ active, conversation }
const menuItems = useMemo(
() => () => {
const agentItems = contextMenuItems();
- if (!directChannel) return agentItems;
- const items = conversationActions(channelMenu(directChannel));
+ const items = channelActions();
if (items.length === 0) return agentItems;
return [...agentItems, { type: 'divider' as const }, ...items];
},
- [channelMenu, contextMenuItems, directChannel],
+ [channelActions, contextMenuItems],
);
return (
diff --git a/src/features/MessageList/Body.tsx b/src/features/MessageList/Body.tsx
index c6d873e7d5b..f45d106aea5 100644
--- a/src/features/MessageList/Body.tsx
+++ b/src/features/MessageList/Body.tsx
@@ -35,7 +35,7 @@ import {
channelActivity,
type Conversation,
type ConversationEntry,
- latestActivity,
+ inboxActivity,
latestTopicOf,
} from './conversations';
import GroupConversation from './GroupConversation';
@@ -151,15 +151,31 @@ const Body = memo(() => {
[recents, inboxAgentId],
);
+ const inboxChannel = inboxAgentId ? directChannels.get(inboxAgentId) : undefined;
+
const entries = useMemo(
() =>
buildConversationEntries(
buildConversationSections(items, recents, directChannels),
- { activeAt: latestActivity(inboxUpdatedAt, inboxTopic), title: inboxTitle },
+ {
+ activeAt: inboxActivity(inboxUpdatedAt, inboxTopic, inboxChannel),
+ channel: inboxChannel,
+ title: inboxTitle,
+ },
query,
channels,
),
- [items, recents, directChannels, inboxUpdatedAt, inboxTitle, inboxTopic, query, channels],
+ [
+ items,
+ recents,
+ directChannels,
+ inboxUpdatedAt,
+ inboxTitle,
+ inboxTopic,
+ inboxChannel,
+ query,
+ channels,
+ ],
);
// Pinned rows lead the list; they render as one block rather than a card each.
@@ -169,7 +185,12 @@ const Body = memo(() => {
const renderEntry = (entry: ConversationEntry) =>
entry.type === 'inbox' ? (
- <InboxConversation activeRouteId={activeRouteId} key={'inbox'} />
+ <InboxConversation
+ activeAt={entry.activeAt}
+ activeRouteId={activeRouteId}
+ channel={entry.channel}
+ key={'inbox'}
+ />
) : entry.type === 'channel' ? (
<ChannelConversation
active={entry.channel.id === activeRouteId}
diff --git a/src/features/MessageList/InboxConversation.tsx b/src/features/MessageList/InboxConversation.tsx
index 1acff8fc7b5..01003122894 100644
--- a/src/features/MessageList/InboxConversation.tsx
+++ b/src/features/MessageList/InboxConversation.tsx
@@ -1,12 +1,12 @@
'use client';
import { DEFAULT_INBOX_AVATAR, DEFAULT_INBOX_TITLE } from '@lobechat/const';
+import type { ChannelListItem } from '@lobechat/types';
import { agentDisplayName } from '@lobechat/types';
-import { Avatar } from '@lobehub/ui';
import { memo } from 'react';
+import AgentAvatar from '@/components/AgentAvatar';
import { resolveInboxAgentRouteId } from '@/features/AgentRoute/useResolvedAgentRouteId';
-import { usePreservedAgentUrl } from '@/features/HomeSidebar/Body/Agent/List/usePreservedAgentUrl';
import { usePrefetchAgent } from '@/hooks/usePrefetchAgent';
import { useAgentStore } from '@/store/agent';
import { agentSelectors, builtinAgentSelectors } from '@/store/agent/selectors';
@@ -14,43 +14,58 @@ import { useChatStore } from '@/store/chat';
import { operationSelectors } from '@/store/chat/selectors';
import ConversationRow from './ConversationRow';
+import { channelPreview } from './conversations';
import { AVATAR_SIZE } from './style';
+import { useInboxConversationLink } from './useAgentConversationLink';
+import { useDirectChannelRow } from './useDirectChannelRow';
/** The built-in Lobe AI conversation, always first in the list. */
-const InboxConversation = memo<{ activeRouteId?: string; preview?: string }>(
- ({ activeRouteId, preview }) => {
- const inboxAgentId = useAgentStore(builtinAgentSelectors.inboxAgentId);
- const routeId = resolveInboxAgentRouteId(inboxAgentId);
- const meta = useAgentStore(agentSelectors.getAgentMetaById(routeId));
- const running = useChatStore(
- inboxAgentId ? operationSelectors.isAgentVisiblyRunning(inboxAgentId) : () => false,
- );
- const prefetchAgent = usePrefetchAgent();
- const url = usePreservedAgentUrl(routeId);
- const title = agentDisplayName(meta, DEFAULT_INBOX_TITLE);
- const active = !!activeRouteId && (activeRouteId === routeId || activeRouteId === inboxAgentId);
+const InboxConversation = memo<{
+ activeAt?: Date;
+ activeRouteId?: string;
+ /** The inbox Agent's direct Channel, when the row opens it. */
+ channel?: ChannelListItem;
+}>(({ activeAt, activeRouteId, channel }) => {
+ const inboxAgentId = useAgentStore(builtinAgentSelectors.inboxAgentId);
+ const routeId = resolveInboxAgentRouteId(inboxAgentId);
+ const meta = useAgentStore(agentSelectors.getAgentMetaById(routeId));
+ const running = useChatStore(
+ inboxAgentId ? operationSelectors.isAgentVisiblyRunning(inboxAgentId) : () => false,
+ );
+ const prefetchAgent = usePrefetchAgent();
+ const { onClick, url } = useInboxConversationLink();
+ const title = agentDisplayName(meta, DEFAULT_INBOX_TITLE);
+ const active = !!activeRouteId && (activeRouteId === routeId || activeRouteId === inboxAgentId);
+ const { channelActions, replyState } = useDirectChannelRow(channel, active);
+ const avatarState = replyState ?? (running ? 'working' : undefined);
- return (
- <ConversationRow
- closeOnReselect
- active={active}
- preview={preview || meta.description || undefined}
- running={running}
- title={title}
- url={url}
- avatar={
- <Avatar
- emojiScaleWithBackground
- avatar={meta.avatar || DEFAULT_INBOX_AVATAR}
- shape={'square'}
- size={AVATAR_SIZE}
- />
- }
- onMouseEnter={() => inboxAgentId && prefetchAgent(inboxAgentId)}
- />
- );
- },
-);
+ return (
+ <ConversationRow
+ closeOnReselect
+ active={active}
+ activeAt={channel ? activeAt : undefined}
+ // An archived direct Channel is the only thing this row can act on.
+ contextMenuItems={channel?.status === 'archived' ? channelActions : undefined}
+ preview={(channel && channelPreview(channel)) || meta.description || undefined}
+ running={running}
+ title={title}
+ unread={channel?.unreadCount ?? undefined}
+ url={url}
+ avatar={
+ <AgentAvatar
+ agentId={routeId}
+ animated={!!avatarState}
+ avatar={meta.avatar || DEFAULT_INBOX_AVATAR}
+ name={title}
+ size={AVATAR_SIZE}
+ state={avatarState}
+ />
+ }
+ onClick={onClick}
+ onMouseEnter={() => inboxAgentId && prefetchAgent(inboxAgentId)}
+ />
+ );
+});
InboxConversation.displayName = 'InboxConversation';
diff --git a/src/features/MessageList/conversations.test.ts b/src/features/MessageList/conversations.test.ts
index 2c68c333f65..3db94c08a19 100644
--- a/src/features/MessageList/conversations.test.ts
+++ b/src/features/MessageList/conversations.test.ts
@@ -5,6 +5,7 @@ import {
buildConversationEntries,
buildConversationSections,
formatConversationTime,
+ inboxActivity,
latestActivity,
latestTopicOf,
} from './conversations';
@@ -235,6 +236,23 @@ describe('latestTopicOf', () => {
});
});
+describe('inboxActivity', () => {
+ const direct = (createdAt: string) =>
+ ({ lastMessage: { content: 'hi', createdAt } }) as unknown as ChannelListItem;
+
+ it('follows the direct Channel instead of the legacy topics', () => {
+ expect(
+ inboxActivity('2026-10-02', topic({ updatedAt: '2026-10-09' }), direct('2026-10-05')),
+ ).toEqual(new Date('2026-10-05'));
+ });
+
+ it('falls back to the latest topic without a direct Channel', () => {
+ expect(inboxActivity('2026-10-02', topic({ updatedAt: '2026-10-09' }))).toEqual(
+ new Date('2026-10-09'),
+ );
+ });
+});
+
describe('latestActivity', () => {
it('uses the later of the own timestamp and the latest topic', () => {
expect(latestActivity('2026-10-02', topic({ updatedAt: '2026-10-05' }))).toEqual(
diff --git a/src/features/MessageList/conversations.ts b/src/features/MessageList/conversations.ts
index e408e0f48ee..0892a1ecb97 100644
--- a/src/features/MessageList/conversations.ts
+++ b/src/features/MessageList/conversations.ts
@@ -79,6 +79,16 @@ export const latestActivity = (
return new Date(Math.max(ownTime, topicTime));
};
+/** The inbox row's activity: once it opens a direct Channel, that replaces its legacy topics. */
+export const inboxActivity = (
+ own: Date | string | undefined,
+ topic: RecentItem | undefined,
+ direct?: ChannelListItem,
+): Date | undefined =>
+ direct
+ ? new Date(Math.max(own ? toTime(own) : 0, channelActivity(direct).getTime()))
+ : latestActivity(own, topic);
+
/**
* Turn the sidebar agent/group list into IM-style conversations: each row
* carries a preview of its latest topic and is ordered by latest activity,
@@ -134,7 +144,7 @@ export const buildConversationSections = (
};
export type ConversationEntry =
- | { activeAt: Date; type: 'inbox' }
+ | { activeAt: Date; channel?: ChannelListItem; type: 'inbox' }
| { activeAt: Date; channel: ChannelListItem; type: 'channel' }
| (Conversation & { type: 'item' });
@@ -145,7 +155,7 @@ export type ConversationEntry =
*/
export const buildConversationEntries = (
{ pinned, recent }: ConversationSections,
- inbox: { activeAt?: Date; title: string } | undefined,
+ inbox: { activeAt?: Date; channel?: ChannelListItem; title: string } | undefined,
query = '',
channels: ChannelListItem[] = [],
): ConversationEntry[] => {
@@ -173,7 +183,11 @@ export const buildConversationEntries = (
if (inbox && matches(inbox.title)) {
const activeAt = inbox.activeAt ?? new Date(0);
const index = rest.findIndex((entry) => entry.activeAt.getTime() < activeAt.getTime());
- rest.splice(index === -1 ? rest.length : index, 0, { activeAt, type: 'inbox' });
+ rest.splice(index === -1 ? rest.length : index, 0, {
+ activeAt,
+ channel: inbox.channel,
+ type: 'inbox',
+ });
}
return [...items(pinned), ...rest];
diff --git a/src/features/MessageList/useAgentConversationLink.test.ts b/src/features/MessageList/useAgentConversationLink.test.ts
index 1eeed1f0698..13f0442db64 100644
--- a/src/features/MessageList/useAgentConversationLink.test.ts
+++ b/src/features/MessageList/useAgentConversationLink.test.ts
@@ -5,11 +5,12 @@ import { act, renderHook } from '@testing-library/react';
import type { MouseEvent } from 'react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
-import { useAgentConversationLink } from './useAgentConversationLink';
+import { useAgentConversationLink, useInboxConversationLink } from './useAgentConversationLink';
const state = vi.hoisted(() => ({
directChannelId: undefined as string | undefined,
enabled: true,
+ inboxAgentId: 'agt_inbox' as string | undefined,
navigate: vi.fn(),
openDirect: vi.fn(),
pathname: '/',
@@ -25,6 +26,18 @@ vi.mock('@/business/client/hooks/useActiveWorkspaceSlug', () => ({
}));
vi.mock('@/libs/replica', () => ({ cacheScope: { get: () => state.scope } }));
+vi.mock('@/features/AgentRoute/useResolvedAgentRouteId', () => ({
+ resolveInboxAgentRouteId: (id?: string) => id ?? 'inbox',
+}));
+vi.mock('@/features/HomeSidebar/Body/Agent/List/usePreservedAgentUrl', () => ({
+ usePreservedAgentUrl: (id: string) => `/agent/${id}`,
+}));
+vi.mock('@/store/agent', () => ({
+ useAgentStore: (selector: (s: object) => unknown) => selector({}),
+}));
+vi.mock('@/store/agent/selectors', () => ({
+ builtinAgentSelectors: { inboxAgentId: () => state.inboxAgentId },
+}));
vi.mock('@/features/Channels/useChannelsEnabled', () => ({
useChannelsEnabled: () => state.enabled,
}));
@@ -50,6 +63,7 @@ describe('useAgentConversationLink', () => {
vi.spyOn(console, 'error').mockImplementation(() => {});
state.directChannelId = undefined;
state.enabled = true;
+ state.inboxAgentId = 'agt_inbox';
state.pathname = '/';
state.scope = 'personal';
state.slug = null;
@@ -60,6 +74,28 @@ describe('useAgentConversationLink', () => {
expect(render().result.current).toEqual({ url: '/channels/chn_000000000001' });
});
+ it('keeps the Agent page while the Agent id is still loading', () => {
+ state.directChannelId = 'chn_000000000001';
+ const { result } = renderHook(() => useAgentConversationLink(undefined, '/agent/inbox'));
+ expect(result.current).toEqual({ onClick: undefined, url: '/agent/inbox' });
+ });
+
+ it('links the inbox Agent row to its direct Channel', () => {
+ state.directChannelId = 'chn_000000000001';
+ const { result } = renderHook(() => useInboxConversationLink());
+ expect(result.current).toEqual({ url: '/channels/chn_000000000001' });
+ });
+
+ it('opens a missing inbox direct Channel on click', async () => {
+ state.openDirect.mockResolvedValue('chn_000000000002');
+ const { result } = renderHook(() => useInboxConversationLink());
+
+ await act(async () => result.current.onClick?.(click()));
+
+ expect(state.openDirect).toHaveBeenCalledWith('agt_inbox');
+ expect(state.navigate).toHaveBeenCalledWith('/channels/chn_000000000002');
+ });
+
it('keeps the Agent page while Channels are off', () => {
state.enabled = false;
expect(render().result.current).toEqual({ onClick: undefined, url: '/agent/agt_nasa' });
diff --git a/src/features/MessageList/useAgentConversationLink.ts b/src/features/MessageList/useAgentConversationLink.ts
index 8e9a2f1cbba..a73a3bfe0f8 100644
--- a/src/features/MessageList/useAgentConversationLink.ts
+++ b/src/features/MessageList/useAgentConversationLink.ts
@@ -2,10 +2,14 @@ import { CHANNEL_URL } from '@lobechat/const';
import { type MouseEvent, useCallback, useEffect, useRef } from 'react';
import { useActiveWorkspaceSlug } from '@/business/client/hooks/useActiveWorkspaceSlug';
+import { resolveInboxAgentRouteId } from '@/features/AgentRoute/useResolvedAgentRouteId';
import { useChannelsEnabled } from '@/features/Channels/useChannelsEnabled';
+import { usePreservedAgentUrl } from '@/features/HomeSidebar/Body/Agent/List/usePreservedAgentUrl';
import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';
import { useActiveLocation } from '@/hooks/useActiveLocation';
import { cacheScope } from '@/libs/replica';
+import { useAgentStore } from '@/store/agent';
+import { builtinAgentSelectors } from '@/store/agent/selectors';
import { channelActions, channelSelectors, useChannelStore } from '@/store/channel';
import { isPlainClick } from './conversations';
@@ -13,12 +17,13 @@ import { isPlainClick } from './conversations';
/**
* With Channels on, an agent row opens the person's direct Channel with the Agent. An Agent made
* before direct Channels has none yet, so the first click opens it; an Agent that cannot answer
- * in a Channel (no reachable device) or a failed open falls back to the Agent's own page.
+ * in a Channel (no reachable device) or a failed open falls back to the Agent's own page. Without
+ * an id yet (the inbox Agent while it loads) the row stays on the Agent's page.
*/
-export const useAgentConversationLink = (agentId: string, agentUrl: string) => {
- const enabled = useChannelsEnabled();
+export const useAgentConversationLink = (agentId: string | undefined, agentUrl: string) => {
+ const enabled = useChannelsEnabled() && !!agentId;
const directChannelId = useChannelStore((s) =>
- enabled ? channelSelectors.directByAgent(agentId)(s)?.id : undefined,
+ enabled ? channelSelectors.directByAgent(agentId!)(s)?.id : undefined,
);
const navigate = useWorkspaceAwareNavigate();
const opening = useRef(false);
@@ -34,7 +39,7 @@ export const useAgentConversationLink = (agentId: string, agentUrl: string) => {
const onClick = useCallback(
(event: MouseEvent<HTMLAnchorElement>) => {
- if (!isPlainClick(event)) return;
+ if (!agentId || !isPlainClick(event)) return;
event.preventDefault();
if (opening.current) return;
opening.current = true;
@@ -62,3 +67,10 @@ export const useAgentConversationLink = (agentId: string, agentUrl: string) => {
if (directChannelId) return { url: CHANNEL_URL(directChannelId) };
return { onClick: enabled ? onClick : undefined, url: agentUrl };
};
+
+/** The inbox Agent's row opens its direct Channel the same way any other Agent's row does. */
+export const useInboxConversationLink = () => {
+ const inboxAgentId = useAgentStore(builtinAgentSelectors.inboxAgentId);
+ const agentUrl = usePreservedAgentUrl(resolveInboxAgentRouteId(inboxAgentId));
+ return useAgentConversationLink(inboxAgentId, agentUrl);
+};
diff --git a/src/features/MessageList/useDirectChannelRow.ts b/src/features/MessageList/useDirectChannelRow.ts
new file mode 100644
index 00000000000..c87d99d7ad9
--- /dev/null
+++ b/src/features/MessageList/useDirectChannelRow.ts
@@ -0,0 +1,44 @@
+import type { ChannelListItem } from '@lobechat/types';
+import { type DropdownItem } from '@lobehub/ui';
+import { useCallback } from 'react';
+
+import { directAvatarState, replyAvatarState } from '@/features/Channels/jobStatus';
+import { useChannelMenu } from '@/features/Channels/useChannelMenu';
+import { channelSelectors, useChannelStore } from '@/store/channel';
+
+/** The direct Channel's own conversation action, next to the Agent's own menu. */
+const CONVERSATION_ACTION_KEYS = new Set(['unarchive']);
+
+/**
+ * A direct Channel is the chat with one Agent, so it cannot be archived or trashed. The row only
+ * frees one that was archived earlier. Keys are prefixed so they cannot collide with the Agent's
+ * own menu.
+ */
+export const conversationActions = (menu: DropdownItem[]): DropdownItem[] =>
+ menu.flatMap((item) => {
+ if (!item || !('key' in item)) return [];
+ const key = String(item.key);
+ if (!CONVERSATION_ACTION_KEYS.has(key)) return [];
+ return [{ ...item, key: `channel-${key}` } as DropdownItem];
+ });
+
+/** What an Agent row takes from the direct Channel it opens. */
+export const useDirectChannelRow = (channel: ChannelListItem | undefined, active: boolean) => {
+ const channelMenu = useChannelMenu();
+ // A direct Channel replies through Channel jobs, not chat operations. The open conversation's
+ // view is polled while it works, so it is fresher than the list; any other cached view may be
+ // stale, so other rows read the jobs the list carries.
+ const replyState = useChannelStore((s) => {
+ if (!channel) return;
+ const detail = active
+ ? channelSelectors.detail({ channelId: channel.id, threadId: null })(s)
+ : undefined;
+ return detail ? directAvatarState(detail) : replyAvatarState(channel.replyJobs ?? []);
+ });
+ const channelActions = useCallback(
+ () => (channel ? conversationActions(channelMenu(channel)) : []),
+ [channel, channelMenu],
+ );
+
+ return { channelActions, replyState };
+};