Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
feat(console): tint each mention badge with its agent's avatar colour
raullopez-sandbox3f69385d4f82
diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/components/chat-composer.tsx b/console/apps/switch-console-desktop/src/renderer/features/chats/components/chat-composer.tsxindex 4fc820f30..bbceea88b 100644--- a/console/apps/switch-console-desktop/src/renderer/features/chats/components/chat-composer.tsx+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/components/chat-composer.tsx@@ -12,7 +12,6 @@ import { type ChatAgent, type ChatMessage, chatAgentLabel } from '@shared/core/cimport { snippetOf } from '../chat-threads';import {filterMentionAgents,- MENTION_BADGE,mentionAgentIds,mentionQuery,mentionSegments,@@ -30,6 +29,7 @@ import {PromptInputSubmit,PromptInputTextarea,} from '../ui/prompt-input';+import { MentionMark } from './mention-mark';async function toBase64(file: File): Promise<string> {const bytes = new Uint8Array(await file.arrayBuffer());@@ -404,10 +404,15 @@ export const ChatComposer = observer(function ChatComposer({className="pointer-events-none absolute inset-0 overflow-y-auto px-1 py-1.5 text-sm break-words whitespace-pre-wrap text-transparent">{mentionSegments(draft, tokens).map((segment, index) =>- segment.mention ? (- <mark key={index} className={cn(MENTION_BADGE, 'text-transparent')}>+ segment.agentId ? (+ <MentionMark+ key={index}+ agent={agents.find((each) => each.id === segment.agentId) ?? null}+ serverId={serverId}+ className="text-transparent"+ >{segment.text}- </mark>+ </MentionMark>) : (<span key={index}>{segment.text}</span>)diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/components/chat-timeline.tsx b/console/apps/switch-console-desktop/src/renderer/features/chats/components/chat-timeline.tsxindex 102710cf2..45c403209 100644--- a/console/apps/switch-console-desktop/src/renderer/features/chats/components/chat-timeline.tsx+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/components/chat-timeline.tsx@@ -15,7 +15,7 @@ import {} from '@shared/core/chats/chats';import { foldTurnEndNotices, placeTurns, type TurnActivity } from '../activity-join';import { messageAnchorId, threadChip } from '../chat-threads';-import { MENTION_BADGE, mentionSegments, mentionTokens } from '../mentions';+import { mentionSegments, mentionTokens } from '../mentions';import { hiddenStopRows } from '../stop-rows';import type { AgentActivity } from '../stores/chat-activity-store';import type { ChatTimeline, PendingSend } from '../stores/chat-timeline-store';@@ -29,6 +29,7 @@ import {turnShown,} from './chat-activity-block';import { ChatAttachments } from './chat-attachments';+import { MentionMark } from './mention-mark';/** Messages more than this far apart start a new dated group. */const GROUP_GAP_MS = 30 * 60 * 1000;@@ -56,12 +57,25 @@ function isBridged(message: ChatMessage): boolean {/** A person's initials disc: people have no picture in Switch. *//** `body` with each `@name` or `@alias` of a chat agent drawn as a badge. */-function MentionText({ body, agents }: { body: string; agents: ChatAgent[] }) {+function MentionText({+ body,+ agents,+ serverId,+}: {+ body: string;+ agents: ChatAgent[];+ serverId: string;+}) {return mentionSegments(body, mentionTokens(body, agents)).map((segment, index) =>- segment.mention ? (- <mark key={index} className={cn(MENTION_BADGE, 'text-foreground')}>+ segment.agentId ? (+ <MentionMark+ key={index}+ agent={agents.find((agent) => agent.id === segment.agentId) ?? null}+ serverId={serverId}+ className="text-foreground"+ >{segment.text}- </mark>+ </MentionMark>) : (<Fragment key={index}>{segment.text}</Fragment>)@@ -291,7 +305,7 @@ export const ChatTimelineView = observer(function ChatTimelineView({)}</span><div className="min-w-0 rounded-2xl bg-background-1 px-4 py-2.5 text-sm leading-relaxed break-words whitespace-pre-wrap">- <MentionText body={message.body} agents={agents} />+ <MentionText body={message.body} agents={agents} serverId={serverId} /></div><ChatAttachmentsserverId={serverId}diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/components/mention-mark.tsx b/console/apps/switch-console-desktop/src/renderer/features/chats/components/mention-mark.tsxnew file mode 100644index 000000000..56096a0da--- /dev/null+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/components/mention-mark.tsx@@ -0,0 +1,33 @@+import type { ReactNode } from 'react';+import { useAgentAccent } from '@renderer/lib/components/agent-accent';+import { cn } from '@renderer/utils/utils';+import type { ChatAgent } from '@shared/core/chats/chats';+import { MENTION_BADGE } from '../mentions';++/**+ * A mention badge tinted with its agent's avatar colour, neutral until that is+ * known or when there is none. Background and an outer ring only: it sits+ * behind the composer's text, so it must not change the text's width.+ */+export function MentionMark({+ agent,+ serverId,+ className,+ children,+}: {+ agent: ChatAgent | null;+ serverId: string;+ className: string;+ children: ReactNode;+}) {+ const accent = useAgentAccent(agent, serverId);+ const tint = accent ? `color-mix(in srgb, ${accent} 18%, transparent)` : null;+ return (+ <mark+ className={cn(tint ? 'rounded-sm' : MENTION_BADGE, className)}+ style={tint ? { backgroundColor: tint, boxShadow: `0 0 0 2px ${tint}` } : undefined}+ >+ {children}+ </mark>+ );+}diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/mentions.ts b/console/apps/switch-console-desktop/src/renderer/features/chats/mentions.tsindex 57bc2d6e1..7312e9a21 100644--- a/console/apps/switch-console-desktop/src/renderer/features/chats/mentions.ts+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/mentions.ts@@ -75,19 +75,19 @@ export function tokenToRemove();}-/** `text` split into plain runs and mention runs, for drawing badges. */+/** `text` split into plain runs and mention runs (with their agent), for drawing badges. */export function mentionSegments(text: string,tokens: MentionToken[]-): { text: string; mention: boolean }[] {- const segments: { text: string; mention: boolean }[] = [];+): { text: string; agentId: string | null }[] {+ const segments: { text: string; agentId: string | null }[] = [];let at = 0;for (const token of tokens) {- if (token.start > at) segments.push({ text: text.slice(at, token.start), mention: false });- segments.push({ text: text.slice(token.start, token.end), mention: true });+ if (token.start > at) segments.push({ text: text.slice(at, token.start), agentId: null });+ segments.push({ text: text.slice(token.start, token.end), agentId: token.agentId });at = token.end;}- if (at < text.length) segments.push({ text: text.slice(at), mention: false });+ if (at < text.length) segments.push({ text: text.slice(at), agentId: null });return segments;}diff --git a/console/apps/switch-console-desktop/src/renderer/lib/components/agent-accent.ts b/console/apps/switch-console-desktop/src/renderer/lib/components/agent-accent.tsnew file mode 100644index 000000000..bc4f2e4ad--- /dev/null+++ b/console/apps/switch-console-desktop/src/renderer/lib/components/agent-accent.ts@@ -0,0 +1,62 @@+import { useEffect, useState } from 'react';+import { useThirdPartyAvatarsEnabled } from '@renderer/lib/stores/use-avatar-settings';+import { pickAccent } from '@renderer/utils/pick-accent';+import { agentAvatarSrc } from './agent-avatar';++const SAMPLE_PX = 24;++/** By image source, shared while pending so many badges for one agent load it once; failures cached too. */+const accents = new Map<string, Promise<string | null>>();+const settled = new Map<string, string | null>();++function sampleAccent(src: string): Promise<string | null> {+ let pending = accents.get(src);+ if (!pending) {+ pending = new Promise<string | null>((resolve) => {+ const image = new Image();+ image.crossOrigin = 'anonymous';+ image.onload = () => {+ try {+ const canvas = document.createElement('canvas');+ canvas.width = canvas.height = SAMPLE_PX;+ const context = canvas.getContext('2d')!;+ context.drawImage(image, 0, 0, SAMPLE_PX, SAMPLE_PX);+ const rgb = pickAccent(context.getImageData(0, 0, SAMPLE_PX, SAMPLE_PX).data);+ resolve(rgb ? `rgb(${rgb.join(' ')})` : null);+ } catch {+ // A host without CORS taints the canvas: the badge keeps its neutral colour.+ resolve(null);+ }+ };+ image.onerror = () => resolve(null);+ image.src = src;+ }).then((accent) => {+ settled.set(src, accent);+ return accent;+ });+ accents.set(src, pending);+ }+ return pending;+}++/** The main colour of an agent's avatar, from the same source the avatar shows; null for none or not yet known. */+export function useAgentAccent(+ agent: { name: string; iconUrl: string | null } | null,+ serverId: string | null+): string | null {+ const thirdParty = useThirdPartyAvatarsEnabled(serverId);+ const src = agent ? agentAvatarSrc(agent.iconUrl, agent.name, thirdParty) : null;+ const [loaded, setLoaded] = useState<{ src: string; accent: string | null } | null>(null);+ useEffect(() => {+ if (!src || settled.has(src)) return;+ let live = true;+ void sampleAccent(src).then((accent) => {+ if (live) setLoaded({ src, accent });+ });+ return () => {+ live = false;+ };+ }, [src]);+ if (!src) return null;+ return settled.get(src) ?? (loaded?.src === src ? loaded.accent : null);+}diff --git a/console/apps/switch-console-desktop/src/renderer/lib/components/agent-avatar.tsx b/console/apps/switch-console-desktop/src/renderer/lib/components/agent-avatar.tsxindex f54d720e7..345972a59 100644--- a/console/apps/switch-console-desktop/src/renderer/lib/components/agent-avatar.tsx+++ b/console/apps/switch-console-desktop/src/renderer/lib/components/agent-avatar.tsx@@ -5,6 +5,18 @@ import { cn } from '@renderer/utils/utils';import { agentInitials, resolveAgentAvatarSrc } from '@shared/core/agents/agent-avatar';import { BUDDY_ICON_URL } from '@shared/core/agents/switch-buddy';+/** The image an agent's avatar shows, or null for its initials. */+export function agentAvatarSrc(+ iconUrl: string | null,+ name: string,+ thirdPartyAvatarsEnabled: boolean | null+): string | null {+ // switch-buddy's picture ships with the app: shown from here, it needs no+ // network and no avatar setting.+ if (iconUrl === BUDDY_ICON_URL) return buddyAvatar;+ return resolveAgentAvatarSrc(iconUrl, name, thirdPartyAvatarsEnabled);+}+/*** An agent's own picture, at whatever size the surface asks for (CHOO-2171).*@@ -52,12 +64,7 @@ export function AgentAvatar({}) {const avatarSettings = useAvatarSettings(serverId);const [failedSrc, setFailedSrc] = useState<string | null>(null);- // switch-buddy's picture ships with the app: shown from here, it needs no- // network and no avatar setting.- const src =- iconUrl === BUDDY_ICON_URL- ? buddyAvatar- : resolveAgentAvatarSrc(iconUrl, name, avatarSettings.thirdPartyAvatarsEnabled);+ const src = agentAvatarSrc(iconUrl, name, avatarSettings.thirdPartyAvatarsEnabled);const [attempt, setAttempt] = useState({ src, count: 0 });const tries = attempt.src === src ? attempt.count : 0;diff --git a/console/apps/switch-console-desktop/src/renderer/tests/browser/chat-composer-mentions.test.tsx b/console/apps/switch-console-desktop/src/renderer/tests/browser/chat-composer-mentions.test.tsxindex 769f1c01e..b5d8261a6 100644--- a/console/apps/switch-console-desktop/src/renderer/tests/browser/chat-composer-mentions.test.tsx+++ b/console/apps/switch-console-desktop/src/renderer/tests/browser/chat-composer-mentions.test.tsx@@ -3,6 +3,7 @@* they only line up with the text if that layer scrolls exactly like the* textarea. And Enter that an IME is handling must never send.*/+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';import { act } from 'react';import { createRoot, type Root } from 'react-dom/client';import { afterEach, describe, expect, it, vi } from 'vitest';@@ -30,7 +31,9 @@ async function render(node: React.ReactNode): Promise<HTMLTextAreaElement> {container.style.width = '600px';document.body.appendChild(container);root = createRoot(container);- await act(async () => root!.render(node));+ await act(async () =>+ root!.render(<QueryClientProvider client={new QueryClient()}>{node}</QueryClientProvider>)+ );return container.querySelector('textarea')!;}diff --git a/console/apps/switch-console-desktop/src/renderer/tests/chat-mentions.test.ts b/console/apps/switch-console-desktop/src/renderer/tests/chat-mentions.test.tsindex 001a97c02..433e2f22d 100644--- a/console/apps/switch-console-desktop/src/renderer/tests/chat-mentions.test.ts+++ b/console/apps/switch-console-desktop/src/renderer/tests/chat-mentions.test.ts@@ -47,9 +47,9 @@ describe('mentionTokens', () => {it('splits text into plain and mention runs', () => {const text = 'x @cc y';expect(mentionSegments(text, mentionTokens(text, agents))).toEqual([- { text: 'x ', mention: false },- { text: '@cc', mention: true },- { text: ' y', mention: false },+ { text: 'x ', agentId: null },+ { text: '@cc', agentId: 'a3' },+ { text: ' y', agentId: null },]);});});diff --git a/console/apps/switch-console-desktop/src/renderer/tests/pick-accent.test.ts b/console/apps/switch-console-desktop/src/renderer/tests/pick-accent.test.tsnew file mode 100644index 000000000..6dfd48761--- /dev/null+++ b/console/apps/switch-console-desktop/src/renderer/tests/pick-accent.test.ts@@ -0,0 +1,31 @@+import { describe, expect, it } from 'vitest';+import { pickAccent } from '@renderer/utils/pick-accent';++const pixels = (...rgba: number[][]) => new Uint8ClampedArray(rgba.flat());+const many = (count: number, rgba: number[]) => Array.from({ length: count }, () => rgba);++describe('pickAccent', () => {+ it('picks the dominant vivid colour, averaged within its bucket', () => {+ const data = pixels(+ ...many(5, [40, 180, 60, 255]),+ [42, 182, 62, 255],+ ...many(3, [60, 90, 220, 255])+ );+ expect(pickAccent(data)).toEqual([40, 180, 60]);+ });++ it('ignores transparent, grey, near-white and near-black pixels', () => {+ const data = pixels(+ ...many(10, [255, 0, 0, 0]),+ ...many(10, [128, 128, 130, 255]),+ ...many(10, [250, 248, 240, 255]),+ ...many(10, [10, 20, 30, 255]),+ [60, 90, 220, 255]+ );+ expect(pickAccent(data)).toEqual([60, 90, 220]);+ });++ it('has no accent for an image without colour', () => {+ expect(pickAccent(pixels(...many(4, [200, 200, 200, 255])))).toBeNull();+ });+});diff --git a/console/apps/switch-console-desktop/src/renderer/utils/pick-accent.ts b/console/apps/switch-console-desktop/src/renderer/utils/pick-accent.tsnew file mode 100644index 000000000..dd52cf9ff--- /dev/null+++ b/console/apps/switch-console-desktop/src/renderer/utils/pick-accent.ts@@ -0,0 +1,32 @@+/**+ * The most common vivid colour in RGBA pixels, averaged within its bucket;+ * null when there is none. Transparent, grey, near-white and near-black pixels+ * are skipped, so an avatar's background and outlines never win.+ */+export function pickAccent(pixels: Uint8ClampedArray): [number, number, number] | null {+ const buckets = new Map<number, { count: number; r: number; g: number; b: number }>();+ for (let at = 0; at + 3 < pixels.length; at += 4) {+ const r = pixels[at]!;+ const g = pixels[at + 1]!;+ const b = pixels[at + 2]!;+ if (pixels[at + 3]! < 128) continue;+ const max = Math.max(r, g, b);+ const min = Math.min(r, g, b);+ if (max < 40 || min > 225 || (max - min) / max < 0.25) continue;+ const key = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4);+ const bucket = buckets.get(key) ?? { count: 0, r: 0, g: 0, b: 0 };+ bucket.count += 1;+ bucket.r += r;+ bucket.g += g;+ bucket.b += b;+ buckets.set(key, bucket);+ }+ let best: { count: number; r: number; g: number; b: number } | null = null;+ for (const bucket of buckets.values()) if (!best || bucket.count > best.count) best = bucket;+ if (!best) return null;+ return [best.r, best.g, best.b].map((sum) => Math.round(sum / best.count)) as [+ number,+ number,+ number,+ ];+}