Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(search): flatten mention chips to the indexed email in search queries (#8370)
gbirmanc530d5828c38
diff --git a/apps/web/src/features/next-soup/soup-view/filters-bar/soup-view-search-bar.tsx b/apps/web/src/features/next-soup/soup-view/filters-bar/soup-view-search-bar.tsxindex 70b607c10f..201ea907b7 100644--- a/apps/web/src/features/next-soup/soup-view/filters-bar/soup-view-search-bar.tsx+++ b/apps/web/src/features/next-soup/soup-view/filters-bar/soup-view-search-bar.tsx@@ -5,7 +5,7 @@ import { useSplitPanelOrThrow } from '@components/app/split-layout/layoutUtils';import { buildConfig } from '@core/component/LexicalMarkdown/builder/MarkdownConfigBuilder';import { MarkdownShell } from '@core/component/LexicalMarkdown/builder/MarkdownShell';import { registerHotkey } from '@core/hotkey/hotkeys';-import { markdownToPlainText } from '@macro-inc/lexical-core/utils/parsers';+import { markdownToSearchText } from '@macro-inc/lexical-core/utils/parsers';import SearchIcon from '@phosphor/magnifying-glass.svg';import XIcon from '@phosphor/x.svg?component-solid';import { cn, Hotkey } from '@ui';@@ -108,10 +108,12 @@ export const SoupSearchbar = (props: SoupSearchbarProps) => {createEffect(() => setSearchPaused(menuIsOpen()));+ // Mention chips flatten to the text the index holds for them (a user's+ // email), not their display name, or a mention-only message never matches.createEffect(on(latestMarkdown, (markdown) => {if (menuIsOpen()) return;- setSearchText(markdownToPlainText(markdown).trim());+ setSearchText(markdownToSearchText(markdown).trim());}));diff --git a/packages/lexical-core/tests/mentions.test.ts b/packages/lexical-core/tests/mentions.test.tsindex 491e0487a9..ecc05cb02b 100644--- a/packages/lexical-core/tests/mentions.test.ts+++ b/packages/lexical-core/tests/mentions.test.ts@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest';import {markdownToEmbeddingText,markdownToPlainText,+ markdownToSearchText,parseContactMentions,parseDateMentions,parseDocumentMentions,@@ -10,6 +11,7 @@ import {parsePullRequestMentions,parseTagMentions,parseUserMentions,+ parseUserMentionsForSearch,} from '../utils/parsers';describe('parseUserMentions', () => {@@ -381,3 +383,57 @@ describe('markdownToEmbeddingText', () => {);});});++// Mirrors the backend indexer (`PlainTextFormatter::format_user`), so a+// query built from mention chips matches what the index holds.+describe('parseUserMentionsForSearch', () => {+ it('flattens a user mention to the email, not the display name', () => {+ const input =+ '<m-user-mention>{"userId":"macro|[email protected]","email":"[email protected]","displayName":"Gabriel"}</m-user-mention>';+ expect(parseUserMentionsForSearch(input)).toBe('[email protected]');+ });++ it('takes the email from the user id when the email field is missing', () => {+ const input =+ '<m-user-mention>{"userId":"macro|[email protected]","displayName":"Gabriel"}</m-user-mention>';+ expect(parseUserMentionsForSearch(input)).toBe('[email protected]');+ });++ it('flattens a bot mention to its display name', () => {+ const input =+ '<m-user-mention>{"userId":"bot|00000000-0000-0000-0000-00000000c5c5","email":"Cursor","displayName":"Cursor"}</m-user-mention>';+ expect(parseUserMentionsForSearch(input)).toBe('Cursor');+ });++ it('names other principals Guest, even with an email', () => {+ expect(+ parseUserMentionsForSearch(+ '<m-user-mention>{"userId":"guest|abc","email":"[email protected]"}</m-user-mention>'+ )+ ).toBe('Guest');+ expect(+ parseUserMentionsForSearch(+ '<m-user-mention>{"userId":"macro|","email":"[email protected]"}</m-user-mention>'+ )+ ).toBe('Guest');+ });++ it('returns empty string for invalid JSON', () => {+ expect(+ parseUserMentionsForSearch('<m-user-mention>not json</m-user-mention>')+ ).toBe('');+ });+});++describe('markdownToSearchText', () => {+ it('differs from markdownToPlainText only in user mentions', () => {+ const input =+ 'ping <m-user-mention>{"userId":"macro|[email protected]","email":"[email protected]","displayName":"Gabriel"}</m-user-mention> re <m-document-mention>{"documentName":"Roadmap"}</m-document-mention>';+ expect(markdownToPlainText(input)).toBe('ping Gabriel re Roadmap');+ expect(markdownToSearchText(input)).toBe('ping [email protected] re Roadmap');+ });++ it('passes plain text through', () => {+ expect(markdownToSearchText('Just plain text.')).toBe('Just plain text.');+ });+});diff --git a/packages/lexical-core/utils/parsers.ts b/packages/lexical-core/utils/parsers.tsindex 40b61896d1..339b1a894b 100644--- a/packages/lexical-core/utils/parsers.ts+++ b/packages/lexical-core/utils/parsers.ts@@ -1,26 +1,60 @@import { isAgentContextData } from '../nodes/AgentContextNode';import { parseDatabaseQueryJson } from '../nodes/DatabaseQueryNode';-export function parseUserMentions(text: string): string {+type UserMentionData = {+ userId?: string;+ displayName?: string;+ email?: string;+};++function replaceUserMentions(+ text: string,+ toText: (data: UserMentionData) => string+): string {return text.replace(/<m-user-mention>(.*?)<\/m-user-mention>/g, (_, json) => {try {- const data = JSON.parse(json);- return userMentionDisplayText(data);+ return toText(JSON.parse(json));} catch {return '';}});}-function userMentionDisplayText(data: {- displayName?: string;- email?: string;-}) {+export function parseUserMentions(text: string): string {+ return replaceUserMentions(text, userMentionDisplayText);+}++/**+ * Flattens user mentions to the text the backend indexes for them, so a+ * search query built from mention chips matches indexed messages.+ */+export function parseUserMentionsForSearch(text: string): string {+ return replaceUserMentions(text, userMentionIndexedText);+}++function userMentionDisplayText(data: UserMentionData) {if (data.displayName) return data.displayName;if (data.email) return data.email.split('@')[0] || data.email;return '';}+/**+ * Mirrors `PlainTextFormatter::format_user` in+ * `crates/mention_utils/src/parse.rs`, which produces the indexed text of a+ * channel message: a Macro user is their email, a bot its display name, and+ * any other principal "Guest". Change both together.+ */+function userMentionIndexedText(data: UserMentionData) {+ const userId = data.userId ?? '';+ if (userId.startsWith('bot|')) {+ return data.displayName || data.email || 'Bot';+ }+ if (userId.startsWith('macro|')) {+ return userId.slice('macro|'.length) || 'Guest';+ }+ return 'Guest';+}+export function parseContactMentions(text: string): string {return text.replace(/<m-contact-mention>(.*?)<\/m-contact-mention>/g,@@ -247,7 +281,7 @@ export function stripAgentContext(text: string): string {/*** Converts markdown text with XML mention tags to plain text.* Extracts the readable text from mention nodes:- * - User mentions: email+ * - User mentions: displayName (fallback to the email local part)* - Contact mentions: name (fallback to emailOrDomain)* - Date mentions: displayFormat* - Document mentions: documentName@@ -260,9 +294,25 @@ export function stripAgentContext(text: string): string {* - Agent activity: nothing; the steps of a reply are not its text*/export function markdownToPlainText(markdown: string): string {+ return flattenMarkdown(markdown, parseUserMentions);+}++/**+ * Like `markdownToPlainText`, but user mentions become the text the search+ * index holds for them (see `parseUserMentionsForSearch`). Use it for text+ * that is matched against indexed content, such as a search query.+ */+export function markdownToSearchText(markdown: string): string {+ return flattenMarkdown(markdown, parseUserMentionsForSearch);+}++function flattenMarkdown(+ markdown: string,+ userMentions: (text: string) => string+): string {const transforms: Array<(text: string) => string> = [parseAgentActivities,- parseUserMentions,+ userMentions,parseContactMentions,parseDateMentions,parseGroupMentions,