Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

feat(console): send and draw mentions by their span

raullopez-sandbox66a7aaaf92b6
diff --git a/console/apps/switch-console-desktop/src/main/core/chats/chat-stream.test.ts b/console/apps/switch-console-desktop/src/main/core/chats/chat-stream.test.ts
index e7f416d30..6142004be 100644
--- a/console/apps/switch-console-desktop/src/main/core/chats/chat-stream.test.ts
+++ b/console/apps/switch-console-desktop/src/main/core/chats/chat-stream.test.ts
@@ -38,6 +38,7 @@ function message(roomId: string, seq: number): ChatMessage {
clientTxn: null,
namedAgentIds: [],
namedUserIds: [],
+ mentions: [],
threadReplyCount: 0,
threadLastReplySeq: null,
};
diff --git a/console/apps/switch-console-desktop/src/main/core/chats/controller.ts b/console/apps/switch-console-desktop/src/main/core/chats/controller.ts
index 8e9aee19d..5dd716dac 100644
--- a/console/apps/switch-console-desktop/src/main/core/chats/controller.ts
+++ b/console/apps/switch-console-desktop/src/main/core/chats/controller.ts
@@ -218,6 +218,7 @@ export const chatsController = createRPCController({
uploadIds: input.uploadIds,
mentionAgentIds: input.mentionAgentIds,
mentionUserIds: input.mentionUserIds,
+ mentionSpans: input.mentionSpans,
})
),
threadMessages: (params: {
diff --git a/console/apps/switch-console-desktop/src/main/core/chats/gateway.ts b/console/apps/switch-console-desktop/src/main/core/chats/gateway.ts
index 05335810e..6d235610d 100644
--- a/console/apps/switch-console-desktop/src/main/core/chats/gateway.ts
+++ b/console/apps/switch-console-desktop/src/main/core/chats/gateway.ts
@@ -11,6 +11,7 @@ import {
roomActivitySchema,
} from '@shared/core/chats/activity';
import {
+ type ChatMentionSpan,
type ChatMessage,
type ChatMessagesPage,
chatMessagesPageSchema,
@@ -233,6 +234,7 @@ export async function sendChatMessage(
uploadIds: string[];
mentionAgentIds?: string[];
mentionUserIds?: string[];
+ mentionSpans?: ChatMentionSpan[];
}
): Promise<ChatMessage[]> {
return (
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.tsx
index aa6a06bb2..9607bcac7 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
@@ -21,6 +21,7 @@ import {
type MentionPerson,
mentionQuery,
mentionSegments,
+ mentionSpans,
type MentionToken,
mentionUserIds,
type RememberedMention,
@@ -338,6 +339,7 @@ export const ChatComposer = observer(function ChatComposer({
threadRootId: replyTo?.messageId ?? null,
mentionAgentIds: mentionAgentIds(sent),
mentionUserIds: mentionUserIds(sent),
+ mentionSpans: mentionSpans(sent),
files: files.map(({ file }) => chatFile(file)),
});
void chatTitler.sent(serverId, timeline.roomId, body);
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.tsx
index 777a4e6fa..bf769cb76 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
@@ -21,6 +21,7 @@ import {
mentionSegments,
mentionTokens,
personTargets,
+ tokensFromSpans,
} from '../mentions';
import { hiddenStopRows } from '../stop-rows';
import type { AgentActivity } from '../stores/chat-activity-store';
@@ -74,13 +75,16 @@ function MentionText({
people: MentionPerson[];
serverId: string;
}) {
- // The people the message names come first, so one sharing an agent's name reads as the person.
- const targets = [
- ...personTargets(people.filter((person) => message.namedUserIds.includes(person.userId))),
- ...agentTargets(agents),
- ];
const body = message.body;
- return mentionSegments(body, mentionTokens(body, targets)).map((segment, index) =>
+ // The server's stretches say whom each mention names; older messages are read from the text,
+ // the people the message names first, so one sharing an agent's name reads as the person.
+ const tokens = message.mentions.length
+ ? tokensFromSpans(body, message.mentions)
+ : mentionTokens(body, [
+ ...personTargets(people.filter((person) => message.namedUserIds.includes(person.userId))),
+ ...agentTargets(agents),
+ ]);
+ return mentionSegments(body, tokens).map((segment, index) =>
segment.token ? (
<MentionMark
key={index}
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.ts
index 06462d62e..a3c6f355d 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
@@ -1,4 +1,4 @@
-import type { ChatAgent } from '@shared/core/chats/chats';
+import type { ChatAgent, ChatMentionSpan } 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._-]/;
@@ -218,3 +218,28 @@ export function composeMentions(
);
return [...kept, ...found].sort((a, b) => a.start - b.start);
}
+
+/** The mentions as the server takes them: each stretch of the body (UTF-16 offsets) and whom it names. */
+export function mentionSpans(tokens: MentionToken[]): ChatMentionSpan[] {
+ return tokens.map((token) => ({
+ kind: token.kind === 'person' ? 'user' : 'agent',
+ id: token.id,
+ offset: token.start,
+ length: token.end - token.start,
+ }));
+}
+
+/** A message's mentions from the stretches the server returned, those that lie within the body. */
+export function tokensFromSpans(body: string, spans: ChatMentionSpan[]): MentionToken[] {
+ return spans
+ .filter((span) => span.offset >= 0 && span.offset + span.length <= body.length)
+ .map(
+ (span): MentionToken => ({
+ kind: span.kind === 'user' ? 'person' : 'agent',
+ id: span.id,
+ start: span.offset,
+ end: span.offset + span.length,
+ })
+ )
+ .sort((a, b) => a.start - b.start);
+}
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.ts
index 3b6f816e8..cbcb14cd8 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
@@ -1,5 +1,6 @@
import { makeAutoObservable, runInAction } from 'mobx';
import {
+ type ChatMentionSpan,
type ChatMessage,
type ChatMessagesPage,
type ChatUpload,
@@ -43,6 +44,7 @@ export type PendingSend = {
threadRootId: string | null;
mentionAgentIds: string[];
mentionUserIds: string[];
+ mentionSpans: ChatMentionSpan[];
files: PendingFile[];
state: 'sending' | 'failed' | 'conflict';
error: string | null;
@@ -59,6 +61,8 @@ export type SendInput = {
mentionAgentIds: string[];
/** The people picked as mentions; they are told, and no agent is addressed by them. */
mentionUserIds: string[];
+ /** Each picked or matched mention's stretch of the body and whom it names. */
+ mentionSpans: ChatMentionSpan[];
files: ChatFile[];
};
@@ -83,6 +87,7 @@ export type TimelineApi = {
uploadIds: string[];
mentionAgentIds?: string[];
mentionUserIds?: string[];
+ mentionSpans?: ChatMentionSpan[];
}
) => Promise<ChatMessage[]>;
newId: () => string;
@@ -208,6 +213,7 @@ export class ChatTimeline {
threadRootId: input.threadRootId,
mentionAgentIds: input.mentionAgentIds,
mentionUserIds: input.mentionUserIds,
+ mentionSpans: input.mentionSpans,
files: input.files.map((file) => ({
id: this.api.newId(),
uploadId: this.api.newId(),
@@ -325,6 +331,7 @@ export class ChatTimeline {
// A copy: IPC cannot clone a MobX array.
mentionAgentIds: [...send.mentionAgentIds],
mentionUserIds: [...send.mentionUserIds],
+ mentionSpans: send.mentionSpans.map((span) => ({ ...span })),
uploadIds: send.files.map((file) => file.uploadId),
});
runInAction(() => {
diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-threads.test.ts b/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-threads.test.ts
index 5cd9a2879..b1d1e9010 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-threads.test.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/stores/chat-timeline-threads.test.ts
@@ -22,6 +22,7 @@ const makeMessage = (overrides: Partial<ChatMessage> = {}): ChatMessage => ({
clientTxn: null,
namedAgentIds: [],
namedUserIds: [],
+ mentions: [],
threadReplyCount: 0,
threadLastReplySeq: null,
...overrides,
diff --git a/console/apps/switch-console-desktop/src/renderer/features/chats/stores/empty-chat-finder.test.ts b/console/apps/switch-console-desktop/src/renderer/features/chats/stores/empty-chat-finder.test.ts
index 5151403b4..d85b03295 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/chats/stores/empty-chat-finder.test.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/chats/stores/empty-chat-finder.test.ts
@@ -49,6 +49,7 @@ const makeMessage = (overrides: Partial<ChatMessage> = {}): ChatMessage => ({
clientTxn: null,
namedAgentIds: [],
namedUserIds: [],
+ mentions: [],
threadReplyCount: 0,
threadLastReplySeq: null,
...overrides,
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.ts
index 312e59fe8..a1693ed4c 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
@@ -3,6 +3,8 @@ import {
agentTargets,
composeMentions,
editMentions,
+ mentionSpans,
+ tokensFromSpans,
shiftMentions,
filterMentionAgents,
filterMentionPeople,
@@ -186,3 +188,16 @@ describe('undoing the removal of a picked mention', () => {
expect(composeMentions('hi @Bob Member', undone.mentions, targets)[0]!.kind).toBe('person');
});
});
+
+describe('mention spans', () => {
+ it('counts offsets in UTF-16 code units, so an emoji before a mention moves it by two', () => {
+ const body = '🎉 @Bob Member';
+ const tokens = [{ kind: 'person' as const, id: 'u1', start: 3, end: 14 }];
+ expect(body.slice(3, 14)).toBe('@Bob Member');
+ expect(mentionSpans(tokens)).toEqual([{ kind: 'user', id: 'u1', offset: 3, length: 11 }]);
+ expect(tokensFromSpans(body, mentionSpans(tokens))).toEqual(tokens);
+ expect(tokensFromSpans(body, [{ kind: 'agent', id: 'a1', offset: 10, length: 20 }])).toEqual(
+ []
+ );
+ });
+});
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.ts
index 99c28359a..2619c4462 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
@@ -23,6 +23,7 @@ function message(seq: number, extra: Partial<ChatMessage> = {}): ChatMessage {
clientTxn: null,
namedAgentIds: [],
namedUserIds: [],
+ mentions: [],
threadReplyCount: 0,
threadLastReplySeq: null,
...extra,
@@ -81,6 +82,7 @@ const input = (files: ChatFile[] = []) => ({
threadRootId: null,
mentionAgentIds: [],
mentionUserIds: [],
+ mentionSpans: [],
files,
});
diff --git a/console/apps/switch-console-desktop/src/renderer/tests/chat-timeline-view.test.ts b/console/apps/switch-console-desktop/src/renderer/tests/chat-timeline-view.test.ts
index 575cf86fa..557abaa10 100644
--- a/console/apps/switch-console-desktop/src/renderer/tests/chat-timeline-view.test.ts
+++ b/console/apps/switch-console-desktop/src/renderer/tests/chat-timeline-view.test.ts
@@ -31,6 +31,7 @@ function message(seq: number, userId: string, name: string): ChatMessage {
clientTxn: null,
namedAgentIds: [],
namedUserIds: [],
+ mentions: [],
threadReplyCount: 0,
threadLastReplySeq: null,
};
@@ -150,3 +151,43 @@ describe('ChatTimelineView mentions of people no longer in the chat', () => {
expect(html).toContain('data-mention="person"');
});
});
+
+describe('ChatTimelineView mentions from the server’s spans', () => {
+ it('badges a same-name agent and person each as itself, after an emoji', () => {
+ const body = '🎉 @Bob Member and @Bob Member';
+ const timeline = new ChatTimeline('room-1', api);
+ timeline.apply({
+ ...message(1, 'me', 'Alice'),
+ body,
+ mentions: [
+ { kind: 'agent', id: 'a9', offset: body.indexOf('@'), length: 11 },
+ { kind: 'user', id: 'u1', offset: body.lastIndexOf('@'), length: 11 },
+ ],
+ });
+ const html = renderToStaticMarkup(
+ createElement(ChatTimelineView, {
+ serverId: 's',
+ timeline,
+ agents: [
+ {
+ id: 'a9',
+ name: 'Bob Member',
+ displayName: null,
+ iconUrl: null,
+ provider: null,
+ alias: null,
+ ownerName: null,
+ ownedByViewer: false,
+ },
+ ],
+ people: [{ userId: 'u1', name: 'Bob Member' }],
+ activities: [],
+ onReply: () => {},
+ })
+ );
+ expect([...html.matchAll(/data-mention="(\w+)"[^>]*>@Bob Member</g)].map((m) => m[1])).toEqual([
+ 'agent',
+ 'person',
+ ]);
+ });
+});
diff --git a/console/apps/switch-console-desktop/src/renderer/tests/chats-store.test.ts b/console/apps/switch-console-desktop/src/renderer/tests/chats-store.test.ts
index 3d5cb8f06..fde7db060 100644
--- a/console/apps/switch-console-desktop/src/renderer/tests/chats-store.test.ts
+++ b/console/apps/switch-console-desktop/src/renderer/tests/chats-store.test.ts
@@ -58,6 +58,7 @@ function message(roomId: string, seq: number): ChatMessage {
clientTxn: null,
namedAgentIds: [],
namedUserIds: [],
+ mentions: [],
threadReplyCount: 0,
threadLastReplySeq: null,
};
diff --git a/console/apps/switch-console-desktop/src/shared/core/chats/chats.ts b/console/apps/switch-console-desktop/src/shared/core/chats/chats.ts
index 85620f016..1fba5bf88 100644
--- a/console/apps/switch-console-desktop/src/shared/core/chats/chats.ts
+++ b/console/apps/switch-console-desktop/src/shared/core/chats/chats.ts
@@ -63,6 +63,15 @@ export const chatAttachmentSchema = z.object({
});
export type ChatAttachment = z.infer<typeof chatAttachmentSchema>;
+/** Who one stretch of a message's text names: offset and length in UTF-16 code units, as JS strings index. */
+export const chatMentionSpanSchema = z.object({
+ kind: z.enum(['agent', 'user']),
+ id: z.string(),
+ offset: z.number().int(),
+ length: z.number().int(),
+});
+export type ChatMentionSpan = z.infer<typeof chatMentionSpanSchema>;
+
export const chatMessageSchema = z.object({
messageId: z.string(),
seq: z.number().int(),
@@ -78,6 +87,8 @@ export const chatMessageSchema = z.object({
namedAgentIds: z.array(z.string()),
/** The people the message mentions; empty from a server older than people mentions. */
namedUserIds: z.array(z.string()).default([]),
+ /** Each mention's stretch of the body and whom it names; empty for older messages and servers. */
+ mentions: z.array(chatMentionSpanSchema).default([]),
threadReplyCount: z.number().int(),
threadLastReplySeq: z.number().int().nullable(),
});
@@ -163,6 +174,7 @@ export type ChatSendInput = {
uploadIds: string[];
mentionAgentIds?: string[];
mentionUserIds?: string[];
+ mentionSpans?: ChatMentionSpan[];
};
export type ChatUploadInput = {