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): @-mention autocomplete with badges in the chat composer
raullopez-sandbox4e1a2ff7d357
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 2af578453..5bb62b17e 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@@ -1,13 +1,24 @@import { ChevronDown, Minimize2, Plus, RotateCcw, RotateCw, X } from 'lucide-react';import { observer } from 'mobx-react-lite';-import { useEffect, useRef, useState } from 'react';+import { type KeyboardEvent as ReactKeyboardEvent, useEffect, useRef, useState } from 'react';import { reportBuddyMessage } from '@renderer/features/switch-buddy/buddy-telemetry';+import { AgentAvatar } from '@renderer/lib/components/agent-avatar';import { isEditableTarget } from '@renderer/lib/hooks/use-arrow-key-navigation';import { modalStore } from '@renderer/lib/modal/modal-store';import { Button } from '@renderer/lib/ui/button';import { Popover, PopoverContent, PopoverTitle, PopoverTrigger } from '@renderer/lib/ui/popover';+import { cn } from '@renderer/utils/utils';import { type ChatAgent, type ChatMessage, chatAgentLabel } from '@shared/core/chats/chats';import { snippetOf } from '../chat-threads';+import {+ filterMentionAgents,+ MENTION_BADGE,+ mentionAgentIds,+ mentionQuery,+ mentionSegments,+ mentionTokens,+ tokenToRemove,+} from '../mentions';import { isInterruptAck } from '../stop-rows';import type { AgentActivity, ControlRequest } from '../stores/chat-activity-store';import type { ChatFile, ChatTimeline } from '../stores/chat-timeline-store';@@ -161,6 +172,17 @@ export const ChatComposer = observer(function ChatComposer({const [contextOpen, setContextOpen] = useState(false);const picker = useRef<HTMLInputElement>(null);const input = useRef<HTMLTextAreaElement>(null);+ const backdrop = useRef<HTMLDivElement>(null);+ const [caret, setCaret] = useState(0);+ const [mentionIndex, setMentionIndex] = useState(0);+ /** Where the `@` of a suggestion list Esc closed sits, so it stays closed for that `@`. */+ const [dismissedAt, setDismissedAt] = useState<number | null>(null);+ const tokens = mentionTokens(draft, agents);+ const query = agents.length ? mentionQuery(draft, caret) : null;+ const suggestions = query ? filterMentionAgents(agents, query.query) : [];+ const suggesting = query !== null && query.start !== dismissedAt && suggestions.length > 0;+ const activeIndex = Math.min(mentionIndex, suggestions.length - 1);+ const listId = `mentions-${timeline.roomId}`;useEffect(() => {const roomId = timeline.roomId;const focus = () => requestAnimationFrame(() => input.current?.focus());@@ -199,6 +221,54 @@ export const ChatComposer = observer(function ChatComposer({input.current?.focus();};+ const pick = (picked: ChatAgent) => {+ const textarea = input.current;+ if (!textarea || !query) return;+ textarea.focus();+ textarea.setSelectionRange(query.start, caret);+ // insertText keeps the native undo stack; a controlled-value write would wipe it.+ if (!document.execCommand('insertText', false, `@${picked.name} `)) {+ const next = `${draft.slice(0, query.start)}@${picked.name} ${draft.slice(caret)}`;+ setDraft(next);+ setCaret(query.start + picked.name.length + 2);+ }+ setMentionIndex(0);+ };++ const onKeyDown = (event: ReactKeyboardEvent<HTMLTextAreaElement>) => {+ if (event.nativeEvent.isComposing || event.keyCode === 229) return;+ if (suggesting) {+ const step = { ArrowDown: 1, ArrowUp: -1 }[event.key];+ if (step) {+ event.preventDefault();+ setMentionIndex((activeIndex + step + suggestions.length) % suggestions.length);+ return;+ }+ if ((event.key === 'Enter' && !event.shiftKey) || event.key === 'Tab') {+ event.preventDefault();+ pick(suggestions[activeIndex]!);+ return;+ }+ if (event.key === 'Escape') {+ event.preventDefault();+ setDismissedAt(query.start);+ return;+ }+ }+ const textarea = event.currentTarget;+ if (+ (event.key === 'Backspace' || event.key === 'Delete') &&+ textarea.selectionStart === textarea.selectionEnd &&+ !event.altKey &&+ !event.metaKey &&+ !event.ctrlKey+ ) {+ const token = tokenToRemove(tokens, textarea.selectionStart, event.key);+ // Select the mention and let the key delete the selection: one native, undoable edit.+ if (token) textarea.setSelectionRange(token.start, token.end);+ }+ };+const send = () => {const body = draft.trim();if (disabled || (!body && !files.length)) return;@@ -210,6 +280,7 @@ export const ChatComposer = observer(function ChatComposer({timeline.send({body,threadRootId: replyTo?.messageId ?? null,+ mentionAgentIds: mentionAgentIds(mentionTokens(body, agents)),files: files.map(({ file }) => chatFile(file)),});void chatTitler.sent(serverId, timeline.roomId, body);@@ -288,17 +359,90 @@ export const ChatComposer = observer(function ChatComposer({event.target.value = '';}}/>- <PromptInputTextarea- ref={input}- className="min-h-0 px-1 py-1.5"- aria-label={`Message ${recipient}`}- placeholder={disabled ?? `Message ${recipient}...`}- value={draft}- disabled={disabled !== null}- onChange={(event) => {- setDraft(event.target.value);- }}- />+ <div className="relative min-w-0 flex-1">+ {suggesting && (+ <div+ id={listId}+ role="listbox"+ aria-label="Mention an agent"+ className="absolute bottom-full left-0 z-50 mb-3 flex max-h-60 w-72 flex-col overflow-y-auto rounded-md border bg-background-quaternary p-1 text-sm shadow-sm"+ >+ {suggestions.map((each, index) => (+ <div+ key={each.id}+ id={`${listId}-${each.id}`}+ role="option"+ aria-selected={index === activeIndex}+ className={cn(+ 'flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5',+ index === activeIndex && 'bg-background-2'+ )}+ onMouseEnter={() => setMentionIndex(index)}+ // Keep focus (and the caret) in the textarea.+ onMouseDown={(event) => event.preventDefault()}+ onClick={() => pick(each)}+ >+ <AgentAvatar+ name={each.name}+ iconUrl={each.iconUrl}+ serverId={serverId}+ size={20}+ />+ <span className="truncate">{chatAgentLabel(each)}</span>+ {each.alias && (+ <span className="truncate text-xs text-foreground-muted">+ @{each.alias}+ </span>+ )}+ </div>+ ))}+ </div>+ )}+ <div+ ref={backdrop}+ aria-hidden+ 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.text}+ </mark>+ ) : (+ <span key={index}>{segment.text}</span>+ )+ )}+ {/* A trailing newline only takes a line once something follows it. */}+ {'\u200b'}+ </div>+ <PromptInputTextarea+ ref={input}+ className="relative min-h-0 px-1 py-1.5"+ aria-label={`Message ${recipient}`}+ placeholder={disabled ?? `Message ${recipient}...`}+ value={draft}+ disabled={disabled !== null}+ role={agents.length ? 'combobox' : undefined}+ aria-autocomplete={agents.length ? 'list' : undefined}+ aria-expanded={agents.length ? suggesting : undefined}+ aria-controls={suggesting ? listId : undefined}+ aria-activedescendant={+ suggesting ? `${listId}-${suggestions[activeIndex]!.id}` : undefined+ }+ onKeyDown={onKeyDown}+ onChange={(event) => {+ setDraft(event.target.value);+ setCaret(event.target.selectionStart);+ setMentionIndex(0);+ if (!mentionQuery(event.target.value, event.target.selectionStart))+ setDismissedAt(null);+ }}+ onSelect={(event) => setCaret(event.currentTarget.selectionStart)}+ onScroll={(event) => {+ if (backdrop.current) backdrop.current.scrollTop = event.currentTarget.scrollTop;+ }}+ />+ </div><PromptInputSubmitstatus={status}className="h-8 min-w-8"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 22a15381e..102710cf2 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,6 +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 { hiddenStopRows } from '../stop-rows';import type { AgentActivity } from '../stores/chat-activity-store';import type { ChatTimeline, PendingSend } from '../stores/chat-timeline-store';@@ -54,6 +55,19 @@ 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[] }) {+ return mentionSegments(body, mentionTokens(body, agents)).map((segment, index) =>+ segment.mention ? (+ <mark key={index} className={cn(MENTION_BADGE, 'text-foreground')}>+ {segment.text}+ </mark>+ ) : (+ <Fragment key={index}>{segment.text}</Fragment>+ )+ );+}+function PersonAvatar({ name }: { name: string }) {return (<span@@ -277,7 +291,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">- {message.body}+ <MentionText body={message.body} agents={agents} /></div><ChatAttachmentsserverId={serverId}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.tsnew file mode 100644index 000000000..57bc2d6e1--- /dev/null+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/mentions.ts@@ -0,0 +1,96 @@+import type { ChatAgent } from '@shared/core/chats/chats';++/** Characters a name may hold; matches the server's `NAME_CHAR`, so a badge is what the server reads as a mention. */+const NAME_CHAR = /[A-Za-z0-9._-]/;++export type MentionAgent = Pick<ChatAgent, 'id' | 'name' | 'alias'>;+export type MentionToken = { agentId: string; start: number; end: number };++/**+ * Every whole `@name` or `@alias` of a chat agent in `text`, in order. Derived+ * from the text alone, so undo, paste and a restored draft keep their badges,+ * and an edit inside one (or the agent leaving the chat) turns it back to text.+ */+export function mentionTokens(text: string, agents: MentionAgent[]): MentionToken[] {+ const spoken = agents+ .flatMap((agent) =>+ [agent.name, agent.alias].flatMap((name) => (name ? [{ agent, name }] : []))+ )+ // Longest first, so `@cc-2` is never read as `@cc` followed by text.+ .sort((a, b) => b.name.length - a.name.length);+ const tokens: MentionToken[] = [];+ for (let at = text.indexOf('@'); at !== -1; at = text.indexOf('@', at + 1)) {+ if (at > 0 && NAME_CHAR.test(text[at - 1]!)) continue;+ const hit = spoken.find(({ name }) => {+ const end = at + 1 + name.length;+ return (+ text.slice(at + 1, end).toLowerCase() === name.toLowerCase() &&+ (end === text.length || !NAME_CHAR.test(text[end]!))+ );+ });+ if (!hit) continue;+ tokens.push({ agentId: hit.agent.id, start: at, end: at + 1 + hit.name.length });+ at += hit.name.length;+ }+ return tokens;+}++/** The agents a message is for, each once. */+export function mentionAgentIds(tokens: MentionToken[]): string[] {+ return [...new Set(tokens.map((token) => token.agentId))];+}++/** The `@query` being typed at the caret, if any: `@` at the start or after whitespace. */+export function mentionQuery(text: string, caret: number): { start: number; query: string } | null {+ const match = /(?:^|\s)@([A-Za-z0-9._-]*)$/.exec(text.slice(0, caret));+ if (!match) return null;+ return { start: caret - match[1]!.length - 1, query: match[1]! };+}++/** Agents whose name or alias contains `query`, those it starts first. */+export function filterMentionAgents<T extends MentionAgent>(agents: T[], query: string): T[] {+ const q = query.toLowerCase();+ const names = (agent: T) => [agent.name, agent.alias ?? ''].map((name) => name.toLowerCase());+ const rank = (agent: T) => (names(agent).some((name) => name.startsWith(q)) ? 0 : 1);+ return agents+ .filter((agent) => names(agent).some((name) => name.includes(q)))+ .sort((a, b) => rank(a) - rank(b));+}++/**+ * The span a Backspace or Delete at a collapsed caret removes whole, because+ * the caret is inside or at the edge of a mention on that side.+ */+export function tokenToRemove(+ tokens: MentionToken[],+ caret: number,+ key: 'Backspace' | 'Delete'+): MentionToken | null {+ return (+ tokens.find((token) =>+ key === 'Backspace'+ ? token.start < caret && caret <= token.end+ : token.start <= caret && caret < token.end+ ) ?? null+ );+}++/** `text` split into plain runs and mention runs, for drawing badges. */+export function mentionSegments(+ text: string,+ tokens: MentionToken[]+): { text: string; mention: boolean }[] {+ const segments: { text: string; mention: boolean }[] = [];+ 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 });+ at = token.end;+ }+ if (at < text.length) segments.push({ text: text.slice(at), mention: false });+ return segments;+}++/** A mention's badge; a ring rather than padding, so it never moves the text it sits behind. */+export const MENTION_BADGE =+ 'rounded-sm bg-background-tertiary-1 ring-2 ring-background-tertiary-1';diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-store.ts b/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-store.tsindex 1c8574196..81f7fd977 100644--- a/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-store.ts+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-store.ts@@ -41,6 +41,7 @@ export type PendingSend = {requestId: string;body: string;threadRootId: string | null;+ mentionAgentIds: string[];files: PendingFile[];state: 'sending' | 'failed' | 'conflict';error: string | null;@@ -53,6 +54,8 @@ export type PendingSend = {export type SendInput = {body: string;threadRootId: string | null;+ /** The agents picked as mentions; the server adds `@name` for any the body does not name. */+ mentionAgentIds: string[];files: ChatFile[];};@@ -199,6 +202,7 @@ export class ChatTimeline {requestId: this.api.newId(),body: input.body,threadRootId: input.threadRootId,+ mentionAgentIds: input.mentionAgentIds,files: input.files.map((file) => ({id: this.api.newId(),uploadId: this.api.newId(),@@ -313,6 +317,8 @@ export class ChatTimeline {requestId,body: send.body,threadRootId: send.threadRootId,+ // A copy: IPC cannot clone a MobX array.+ mentionAgentIds: [...send.mentionAgentIds],uploadIds: send.files.map((file) => file.uploadId),});runInAction(() => {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.tsnew file mode 100644index 000000000..001a97c02--- /dev/null+++ b/console/apps/switch-console-desktop/src/renderer/tests/chat-mentions.test.ts@@ -0,0 +1,91 @@+import { describe, expect, it } from 'vitest';+import {+ filterMentionAgents,+ mentionAgentIds,+ mentionQuery,+ mentionSegments,+ mentionTokens,+ tokenToRemove,+} from '@renderer/features/chats/mentions';++const agents = [+ { id: 'a1', name: 'agent-one', alias: null },+ { id: 'a2', name: 'agent-two', alias: 'reviewer' },+ { id: 'a3', name: 'cc', alias: null },+ { id: 'a4', name: 'cc-2', alias: null },+];++describe('mentionTokens', () => {+ it('finds whole @name and @alias mentions, case-insensitively', () => {+ const text = 'hi @Agent-Two and @reviewer, @agent-one!';+ expect(mentionTokens(text, agents)).toEqual([+ { agentId: 'a2', start: 3, end: 13 },+ { agentId: 'a2', start: 18, end: 27 },+ { agentId: 'a1', start: 29, end: 39 },+ ]);+ });++ it('prefers the longest name and never matches inside a longer word', () => {+ expect(mentionTokens('@cc-2 @cc @cc-3 bob@cc', agents).map((t) => t.agentId)).toEqual([+ 'a4',+ 'a3',+ ]);+ });++ it('drops a mention once an edit lands inside it, or its agent leaves', () => {+ expect(mentionTokens('@agent-tXwo hi', agents)).toEqual([]);+ expect(mentionTokens('@agent-twos hi', agents)).toEqual([]);+ expect(mentionTokens('@agent-two hi', agents.slice(0, 1))).toEqual([]);+ });++ it('addresses an agent once however often it is mentioned', () => {+ expect(mentionAgentIds(mentionTokens('@agent-two @reviewer @agent-two', agents))).toEqual([+ 'a2',+ ]);+ });++ 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 },+ ]);+ });+});++describe('mention editing', () => {+ const text = 'a @agent-two b';+ const tokens = mentionTokens(text, agents);++ it('removes a whole mention on Backspace at its end or inside it', () => {+ expect(tokenToRemove(tokens, 12, 'Backspace')).toEqual(tokens[0]);+ expect(tokenToRemove(tokens, 5, 'Backspace')).toEqual(tokens[0]);+ expect(tokenToRemove(tokens, 2, 'Backspace')).toBeNull();+ expect(tokenToRemove(tokens, 13, 'Backspace')).toBeNull();+ });++ it('removes a whole mention on Delete at its start', () => {+ expect(tokenToRemove(tokens, 2, 'Delete')).toEqual(tokens[0]);+ expect(tokenToRemove(tokens, 12, 'Delete')).toBeNull();+ });+});++describe('mention suggestions', () => {+ it('opens on @ at the start or after whitespace only', () => {+ expect(mentionQuery('@ag', 3)).toEqual({ start: 0, query: 'ag' });+ expect(mentionQuery('hi\n@', 4)).toEqual({ start: 3, query: '' });+ expect(mentionQuery('bob@ag', 6)).toBeNull();+ expect(mentionQuery('@ag x', 5)).toBeNull();+ });++ it('filters by name and alias, prefix matches first', () => {+ expect(filterMentionAgents(agents, 'TWO').map((a) => a.id)).toEqual(['a2']);+ expect(filterMentionAgents(agents, 'rev').map((a) => a.id)).toEqual(['a2']);+ const named = [+ { id: 'x', name: 'bob-ann', alias: null },+ { id: 'y', name: 'ann', alias: null },+ ];+ expect(filterMentionAgents(named, 'ann').map((a) => a.id)).toEqual(['y', 'x']);+ });+});diff --git a/console/apps/switch-console-desktop/src/renderer/tests/chat-timeline-store.test.ts b/console/apps/switch-console-desktop/src/renderer/tests/chat-timeline-store.test.tsindex 36b2e534b..2f990bd06 100644--- a/console/apps/switch-console-desktop/src/renderer/tests/chat-timeline-store.test.ts+++ b/console/apps/switch-console-desktop/src/renderer/tests/chat-timeline-store.test.ts@@ -1,3 +1,4 @@+import { isObservableArray } from 'mobx';import { describe, expect, it, vi } from 'vitest';import {ChatTimeline,@@ -74,6 +75,7 @@ const file = (name: string): ChatFile => ({const input = (files: ChatFile[] = []) => ({body: 'hello',threadRootId: null,+ mentionAgentIds: [],files,});@@ -97,6 +99,28 @@ describe('ChatTimeline', () => {expect(timeline.messages.map((each) => each.clientTxn)).toEqual([`${requestId}:0`]);});+ it('sends the picked mentions with the message, and again on retry', async () => {+ let fail = true;+ const { value, calls } = api();+ const send = value.send;+ value.send = async (room, body) => {+ if (fail) {+ fail = false;+ calls.send.push(body);+ throw networkError();+ }+ return send(room, body);+ };+ const timeline = new ChatTimeline('room-1', value);+ const requestId = timeline.send({ ...input(), body: '@agent-two hi', mentionAgentIds: ['a2'] });+ await vi.waitFor(() => expect(timeline.pending[0]?.state).toBe('failed'));+ timeline.retry(requestId);+ await vi.waitFor(() => expect(timeline.pending).toHaveLength(0));+ expect(calls.send.map((each) => each.mentionAgentIds)).toEqual([['a2'], ['a2']]);+ // What crosses IPC must be structured-cloneable.+ expect(isObservableArray(calls.send[1]!.mentionAgentIds)).toBe(false);+ });+it('reconciles on the feed echo before the send answers', async () => {let answer!: (messages: ChatMessage[]) => void;const { value } = api({ send: () => new Promise((resolve) => (answer = resolve)) });