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.

πŸ’„ style(channel): show the agent avatar on a direct channel's desktop tab (#22901)

Arvin Xua256031051ad
diff --git a/src/features/Channels/ChannelDynamicMeta.test.ts b/src/features/Channels/ChannelDynamicMeta.test.ts
new file mode 100644
index 00000000000..9fafbcbdf0b
--- /dev/null
+++ b/src/features/Channels/ChannelDynamicMeta.test.ts
@@ -0,0 +1,97 @@
+import type { ChannelListItem } from '@lobechat/types';
+import { renderHook } from '@testing-library/react';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { useAgentStore } from '@/store/agent';
+import { useChannelStore } from '@/store/channel';
+import { channelViewKey } from '@/store/channel/projection';
+
+import ChannelDynamicMeta from './ChannelDynamicMeta';
+
+vi.mock('@/features/RouteMeta/workspaceScope', () => ({
+ matchesRouteWorkspace: () => true,
+ useRouteWorkspaceId: () => null,
+}));
+
+const listItem = (patch: Partial<ChannelListItem>) =>
+ ({
+ agentId: null,
+ id: 'chn_000000000001',
+ projectId: null,
+ title: 'Design',
+ workspaceId: null,
+ ...patch,
+ }) as ChannelListItem;
+
+const render = () => {
+ const onResolve = vi.fn();
+ renderHook(() => ChannelDynamicMeta({ onResolve, params: { channelId: 'chn_000000000001' } }));
+ return onResolve;
+};
+
+describe('ChannelDynamicMeta', () => {
+ beforeEach(() => {
+ useAgentStore.setState({ agentMap: {} });
+ useChannelStore.setState({ detailMap: {}, listMap: {} });
+ });
+
+ it('keeps the channel icon for a group channel', () => {
+ useChannelStore.setState({ listMap: { all: [listItem({})] } });
+
+ expect(render()).toHaveBeenLastCalledWith(
+ expect.objectContaining({ avatarKind: undefined, title: 'Design' }),
+ );
+ });
+
+ it("wears the agent's avatar for a direct channel", () => {
+ useChannelStore.setState({
+ listMap: { all: [listItem({ agentId: 'agt_c', title: '小C' })] },
+ });
+ useAgentStore.setState({
+ agentMap: { agt_c: { avatar: '🌟', backgroundColor: '#f60' } as any },
+ });
+
+ expect(render()).toHaveBeenLastCalledWith(
+ expect.objectContaining({
+ avatar: '🌟',
+ avatarKind: 'agent',
+ avatarSeed: 'agt_c',
+ backgroundColor: '#f60',
+ title: '小C',
+ }),
+ );
+ });
+
+ it('clears the avatar once a loaded agent has none, even with a stale member avatar', () => {
+ const channel = listItem({ agentId: 'agt_c', title: '小C' });
+ useChannelStore.setState({
+ detailMap: {
+ [channelViewKey({ channelId: channel.id, threadId: null })]: {
+ channel,
+ members: [{ agentId: 'agt_c', avatar: '🌟' }],
+ } as any,
+ },
+ });
+ useAgentStore.setState({ agentMap: { agt_c: { avatar: null } as any } });
+
+ expect(render()).toHaveBeenLastCalledWith(
+ expect.objectContaining({ avatar: null, avatarKind: 'agent', avatarSeed: 'agt_c' }),
+ );
+ });
+
+ it('falls back to the member avatar before the agent has loaded', () => {
+ const channel = listItem({ agentId: 'agt_c', title: '小C' });
+ useChannelStore.setState({
+ detailMap: {
+ [channelViewKey({ channelId: channel.id, threadId: null })]: {
+ channel,
+ members: [{ agentId: 'agt_c', avatar: '🌟' }],
+ } as any,
+ },
+ });
+
+ expect(render()).toHaveBeenLastCalledWith(
+ expect.objectContaining({ avatar: '🌟', avatarKind: 'agent', avatarSeed: 'agt_c' }),
+ );
+ });
+});
diff --git a/src/features/Channels/ChannelDynamicMeta.tsx b/src/features/Channels/ChannelDynamicMeta.tsx
index 5bc22debf54..4a8bf3b62f0 100644
--- a/src/features/Channels/ChannelDynamicMeta.tsx
+++ b/src/features/Channels/ChannelDynamicMeta.tsx
@@ -1,21 +1,42 @@
import { usePublishDynamicRouteMeta } from '@/features/RouteMeta/usePublishDynamicRouteMeta';
import { matchesRouteWorkspace, useRouteWorkspaceId } from '@/features/RouteMeta/workspaceScope';
import type { DynamicRouteMetaProps } from '@/spa/router/routeMeta';
+import { useAgentStore } from '@/store/agent';
import { channelSelectors, useChannelStore } from '@/store/channel';
-/** Names the tab after the open Channel, from data the sidebar and page already loaded. */
+/**
+ * Names the tab after the open Channel, from data the sidebar and page already loaded. A direct
+ * conversation stands for its Agent, so its tab wears that Agent's avatar instead of `#`.
+ */
const ChannelDynamicMeta = ({ onResolve, params }: DynamicRouteMetaProps) => {
const routeWorkspaceId = useRouteWorkspaceId(params);
const channelId = params.channelId ?? '';
- const title = useChannelStore((state) => {
- const channel =
- channelSelectors.list(state)?.find((item) => item.id === channelId) ??
- channelSelectors.detail({ channelId, threadId: null })(state)?.channel;
- return channel && matchesRouteWorkspace(channel.workspaceId, routeWorkspaceId)
- ? channel.title
- : undefined;
- });
- usePublishDynamicRouteMeta({ title }, onResolve);
+ const detail = useChannelStore(channelSelectors.detail({ channelId, threadId: null }));
+ const channel = useChannelStore(
+ (state) =>
+ channelSelectors.list(state)?.find((item) => item.id === channelId) ?? detail?.channel,
+ );
+ const visible = !!channel && matchesRouteWorkspace(channel.workspaceId, routeWorkspaceId);
+ const agentId = visible ? (channel.agentId ?? undefined) : undefined;
+ const agent = useAgentStore((state) => (agentId ? state.agentMap[agentId] : undefined));
+ // The page loads members before the Agent itself, so the member row covers the first paint.
+ const memberAvatar = agentId
+ ? detail?.members.find((member) => member.agentId === agentId)?.avatar
+ : undefined;
+
+ usePublishDynamicRouteMeta(
+ agentId
+ ? {
+ // A loaded Agent without an avatar publishes `null`, so the tab drops a cached one.
+ avatar: agent ? agent.avatar || null : memberAvatar || undefined,
+ avatarKind: 'agent',
+ avatarSeed: agentId,
+ backgroundColor: agent?.backgroundColor || undefined,
+ title: channel?.title,
+ }
+ : { title: visible ? channel.title : undefined },
+ onResolve,
+ );
return null;
};