Skip to content
31 linesCodeBlameRaw

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

Chat and workspace agents: channels, DMs and named agents you talk to1/**
2 * `@handle` mentions in a message: people by username, agents by handle.
3 * Pure, so they are tested apart from the service.
4 *
5 * A message keeps the handles it mentions as ` a b ` (spaces around each),
6 * so the sidebar finds a viewer's mentions with `LIKE '% name %'` without
7 * matching `@bobby` for `@bob`.
8 */
9
10/**
11 * `@` after the start or a character that cannot be part of an address
12 * or a word (so `me@example.com` is not a mention), then the handle.
13 */
14const MENTION = /(^|[^a-z0-9_.@-])@([a-z0-9](?:[a-z0-9_-]{0,38}[a-z0-9_])?)/gi;
15
16/** The handles `body` mentions, lowercased, each once, in order. */
17export function mentionedHandles(body: string): string[] {
18 const found = new Set<string>();
19 for (const match of String(body ?? "").matchAll(MENTION)) found.add(match[2].toLowerCase());
20 return [...found];
21}
22
23/** How a message's mentions are kept: `` (none) or ` a b `. */
24export function mentionsColumn(handles: string[]): string {
25 return handles.length ? ` ${handles.join(" ")} ` : "";
26}
27
28/** Whether a kept mentions column names `handle`. */
29export function mentions(column: string | null | undefined, handle: string): boolean {
30 return !!column && column.includes(` ${handle.toLowerCase()} `);
31}