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(agents): map the avatar palette's new colors and stop breaking on the next ones (#23621)

Arvin Xu718c46e7cc98
diff --git a/src/features/AgentViewAll/coverColor.test.ts b/src/features/AgentViewAll/coverColor.test.ts
index 4d26b1e99c1..ab11e44ddf0 100644
--- a/src/features/AgentViewAll/coverColor.test.ts
+++ b/src/features/AgentViewAll/coverColor.test.ts
@@ -1,7 +1,8 @@
import { DEFAULT_AVATAR } from '@lobechat/const';
import type { SidebarAgentItem } from '@lobechat/types';
-import { cssVar } from '@lobehub/ui';
-import { describe, expect, it } from 'vitest';
+import type * as UI from '@lobehub/ui';
+import { AGENT_AVATAR_COLORS, cssVar } from '@lobehub/ui';
+import { describe, expect, it, vi } from 'vitest';
import {
AGENT_COVER_PATTERNS,
@@ -11,6 +12,19 @@ import {
seedCoverColor,
} from './coverColor';
+const traits = vi.hoisted(() => ({ color: undefined as string | undefined }));
+vi.mock('@lobehub/ui', async (original) => {
+ const ui = await original<typeof UI>();
+ return {
+ ...ui,
+ // A test can pin the generated character's color; otherwise the real traits apply.
+ getAgentAvatarTraits: (id: string) => {
+ const real = ui.getAgentAvatarTraits(id);
+ return traits.color ? { ...real, color: traits.color } : real;
+ },
+ };
+});
+
const agent = (overrides: Partial<SidebarAgentItem> = {}): SidebarAgentItem => ({
id: 'agt_1',
pinned: false,
@@ -81,6 +95,23 @@ describe('seedCoverColor', () => {
it('is stable per agent id', () => {
expect(seedCoverColor(agent())).toBe(seedCoverColor(agent({ title: 'Renamed' })));
});
+
+ it('maps every color the installed avatar palette has, including red, green and cyan', () => {
+ for (const color of [...AGENT_AVATAR_COLORS, 'red', 'green', 'cyan']) {
+ traits.color = color;
+ expect(seedCoverColor(agent()), color).toMatch(/\S/);
+ }
+ traits.color = 'red';
+ expect(seedCoverColor(agent())).toBe(cssVar.red);
+ traits.color = undefined;
+ });
+
+ // `@lobehub/ui` grows the palette in minor releases; a color not listed yet must not break.
+ it('falls back to gray for a color it does not know yet', () => {
+ traits.color = 'lime';
+ expect(seedCoverColor(agent())).toBe(cssVar.gray);
+ traits.color = undefined;
+ });
});
describe('coverPattern', () => {
diff --git a/src/features/AgentViewAll/coverColor.ts b/src/features/AgentViewAll/coverColor.ts
index ecf68afe359..8b15832077a 100644
--- a/src/features/AgentViewAll/coverColor.ts
+++ b/src/features/AgentViewAll/coverColor.ts
@@ -1,5 +1,5 @@
import type { SidebarAgentItem } from '@lobechat/types';
-import { type AgentAvatarColor, cssVar, getAgentAvatarTraits, parseAgentAvatar } from '@lobehub/ui';
+import { cssVar, getAgentAvatarTraits, parseAgentAvatar } from '@lobehub/ui';
import { isDefaultAgentAvatar } from '@/components/AgentAvatar';
import { remoteAvatarSrc } from '@/components/Avatar/fallback';
@@ -8,8 +8,13 @@ import { remoteAvatarSrc } from '@/components/Avatar/fallback';
* The generated character's palette as theme colors. `ink` / `gray` share the
* gray scale: the cover is mixed toward the container color, which keeps both
* readable in either theme.
+ *
+ * Keyed by string rather than `AgentAvatarColor`: `@lobehub/ui` grows that
+ * palette in minor releases, and an exhaustive record breaks the type check of
+ * every build that resolves a newer one. A color it adds before it is listed
+ * here falls back to gray.
*/
-const SEED_COLORS: Record<AgentAvatarColor, string> = {
+const SEED_COLORS: Record<string, string> = {
cyan: cssVar.cyan,
geekblue: cssVar.geekblue,
gray: cssVar.gray,
@@ -30,7 +35,7 @@ const SEED_COLORS: Record<AgentAvatarColor, string> = {
export const seedCoverColor = (item: SidebarAgentItem): string => {
const picked = typeof item.avatar === 'string' ? parseAgentAvatar(item.avatar) : undefined;
- return SEED_COLORS[picked?.color ?? getAgentAvatarTraits(item.id).color];
+ return SEED_COLORS[picked?.color ?? getAgentAvatarTraits(item.id).color] ?? cssVar.gray;
};
export const AGENT_COVER_PATTERNS = [