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: channel name in search should appear regardless of order (#1087)
gbirman736428766077
diff --git a/js/app/packages/core/util/freshSort.test.ts b/js/app/packages/core/util/freshSort.test.tsindex 1cf8d83075..441ba49bc0 100644--- a/js/app/packages/core/util/freshSort.test.ts+++ b/js/app/packages/core/util/freshSort.test.ts@@ -1,6 +1,5 @@import { describe, expect, it } from 'vitest';import { createFreshSearch } from './freshSort';-import { fuzzyScoreCommaSeparated, fuzzyTestCommaSeparated } from './fuzzy';interface MockItem {id: string;@@ -70,64 +69,6 @@ describe('freshSort ordering', () => {});});-describe('fuzzyTestCommaSeparated', () => {- it('matches when all query parts match text parts', () => {- expect(fuzzyTestCommaSeparated('nick,hutch', 'Nick Noble,teo,hutch')).toBe(- true- );- });-- it('matches regardless of order', () => {- expect(fuzzyTestCommaSeparated('teo,nick', 'Nick Noble,teo,hutch')).toBe(- true- );- });-- it('matches single query term against multi-part name', () => {- expect(fuzzyTestCommaSeparated('teo', 'Nick Noble,teo,hutch')).toBe(true);- });-- it('does not match when a query part is missing', () => {- expect(fuzzyTestCommaSeparated('nick,alice', 'Nick Noble,teo,hutch')).toBe(- false- );- });-- it('handles fuzzy matching within parts', () => {- expect(fuzzyTestCommaSeparated('nob,teo', 'Nick Noble,teo,hutch')).toBe(- true- );- });-- it('handles whitespace around commas', () => {- expect(- fuzzyTestCommaSeparated('nick , hutch', 'Nick Noble , teo , hutch')- ).toBe(true);- });-- it('returns true for empty query', () => {- expect(fuzzyTestCommaSeparated('', 'Nick Noble,teo,hutch')).toBe(true);- });-});--describe('fuzzyScoreCommaSeparated', () => {- it('returns score between 0 and 1', () => {- const score = fuzzyScoreCommaSeparated('nick,teo', 'Nick Noble,teo,hutch');- expect(score).toBeGreaterThan(0);- expect(score).toBeLessThanOrEqual(1);- });-- it('returns -1 when no match', () => {- const score = fuzzyScoreCommaSeparated('alice,bob', 'Nick Noble,teo,hutch');- expect(score).toBe(-1);- });-- it('returns 1 for empty query', () => {- const score = fuzzyScoreCommaSeparated('', 'Nick Noble,teo,hutch');- expect(score).toBe(1);- });-});-describe('createFreshSearch with comma-separated channel matching', () => {function createCommaSeparatedSearch() {return createFreshSearch<MockItem>(@@ -199,4 +140,27 @@ describe('createFreshSearch with comma-separated channel matching', () => {// Both should match - channel via comma-separated, item via regular fuzzyexpect(results.length).toBe(2);});++ it('matches channel with space-separated query in any order', () => {+ const now = Date.now();+ const items: MockItem[] = [+ {+ id: '1',+ name: 'jackson kustec, gabriel birman, jacob, eric hayes',+ type: 'channel',+ viewedAt: now,+ },+ { id: '2', name: 'Other Channel', type: 'channel', viewedAt: now },+ ];++ const search = createCommaSeparatedSearch();++ const results1 = search(items, 'jackson jacob');+ expect(results1.length).toBe(1);+ expect(results1[0].item.id).toBe('1');++ const results2 = search(items, 'jacob jackson');+ expect(results2.length).toBe(1);+ expect(results2[0].item.id).toBe('1');+ });});diff --git a/js/app/packages/core/util/freshSort.ts b/js/app/packages/core/util/freshSort.tsindex 1be2e119f4..e03a917ce2 100644--- a/js/app/packages/core/util/freshSort.ts+++ b/js/app/packages/core/util/freshSort.ts@@ -4,7 +4,7 @@*/import type { FilterResult } from 'fuzzy';import fuzzy from 'fuzzy';-import { fuzzyScoreCommaSeparated } from './fuzzy';+import { fuzzyScoreCommaSpaceSeparated } from './fuzzy';export interface FreshSortConfig {/** Weight for fuzzy match (0-1). Higher values prioritize search relevance. Default: 0.7 */@@ -198,18 +198,20 @@ export function createFreshSearch<T extends TimestampedItem>() {return (items: T[], query: string): FreshSortResult<T>[] => {const finalConfig = { ...DEFAULT_CONFIG, ...config };- const useCommaSeparated =- finalConfig.commaSeparatedChannelMatch && query.includes(',');+ const hasComma = query.includes(',');+ const hasSpace = query.includes(' ');+ const useMultiTermChannelMatch =+ finalConfig.commaSeparatedChannelMatch && (hasComma || hasSpace);- if (useCommaSeparated) {- // For comma-separated queries, handle channel items specially+ if (useMultiTermChannelMatch) {+ // For comma or space-separated queries, handle channel items speciallyconst channelResults: FilterResult<T>[] = [];const nonChannelItems: T[] = [];for (const item of items) {if (isChannelItem(item)) {const name = extractor(item);- const score = fuzzyScoreCommaSeparated(query, name);+ const score = fuzzyScoreCommaSpaceSeparated(query, name);if (score >= 0) {channelResults.push({original: item,diff --git a/js/app/packages/core/util/fuzzy.test.ts b/js/app/packages/core/util/fuzzy.test.tsnew file mode 100644index 0000000000..48aec13bf6--- /dev/null+++ b/js/app/packages/core/util/fuzzy.test.ts@@ -0,0 +1,294 @@+import { describe, expect, it } from 'vitest';+import {+ fuzzyMatch,+ fuzzyScoreCommaSpaceSeparated,+ fuzzyTestCommaSpaceSeparated,+ highlightCommaSpaceSeparatedMatches,+} from './fuzzy';++interface TestItem {+ id: string;+ name: string;+ type?: string;+}++describe('fuzzyTestCommaSpaceSeparated', () => {+ it('matches comma-separated query terms', () => {+ expect(+ fuzzyTestCommaSpaceSeparated('nick,hutch', 'Nick Noble,teo,hutch')+ ).toBe(true);+ });++ it('matches space-separated query terms', () => {+ expect(+ fuzzyTestCommaSpaceSeparated(+ 'jackson jacob',+ 'jacob, jackson kustec, gabriel'+ )+ ).toBe(true);+ });++ it('matches regardless of order', () => {+ expect(+ fuzzyTestCommaSpaceSeparated('teo,nick', 'Nick Noble,teo,hutch')+ ).toBe(true);+ expect(+ fuzzyTestCommaSpaceSeparated(+ 'jacob jackson',+ 'jackson kustec, jacob, gabriel'+ )+ ).toBe(true);+ });++ it('matches single query term against multi-part name', () => {+ expect(fuzzyTestCommaSpaceSeparated('teo', 'Nick Noble,teo,hutch')).toBe(+ true+ );+ });++ it('does not match when a query part is missing', () => {+ expect(+ fuzzyTestCommaSpaceSeparated('nick,alice', 'Nick Noble,teo,hutch')+ ).toBe(false);+ });++ it('handles fuzzy matching within parts', () => {+ expect(+ fuzzyTestCommaSpaceSeparated('nob,teo', 'Nick Noble,teo,hutch')+ ).toBe(true);+ });++ it('handles whitespace around commas', () => {+ expect(+ fuzzyTestCommaSpaceSeparated('nick , hutch', 'Nick Noble , teo , hutch')+ ).toBe(true);+ });++ it('returns true for empty query', () => {+ expect(fuzzyTestCommaSpaceSeparated('', 'Nick Noble,teo,hutch')).toBe(true);+ });+});++describe('fuzzyScoreCommaSpaceSeparated', () => {+ it('returns score between 0 and 1 with comma-separated query', () => {+ const score = fuzzyScoreCommaSpaceSeparated(+ 'nick,teo',+ 'Nick Noble,teo,hutch'+ );+ expect(score).toBeGreaterThan(0);+ expect(score).toBeLessThanOrEqual(1);+ });++ it('matches space-separated query terms in any order', () => {+ const score = fuzzyScoreCommaSpaceSeparated(+ 'jackson jacob',+ 'jackson kustec, gabriel birman, jacob, eric hayes'+ );+ expect(score).toBeGreaterThan(0);+ expect(score).toBeLessThanOrEqual(1);+ });++ it('matches when query terms are reversed', () => {+ const score = fuzzyScoreCommaSpaceSeparated(+ 'jacob jackson',+ 'jackson kustec, gabriel birman, jacob, eric hayes'+ );+ expect(score).toBeGreaterThan(0);+ expect(score).toBeLessThanOrEqual(1);+ });++ it('matches mixed space and comma query', () => {+ const score = fuzzyScoreCommaSpaceSeparated(+ 'jackson, jacob',+ 'jackson kustec, gabriel birman, jacob, eric hayes'+ );+ expect(score).toBeGreaterThan(0);+ });++ it('returns -1 when no match', () => {+ const score = fuzzyScoreCommaSpaceSeparated(+ 'alice,bob',+ 'Nick Noble,teo,hutch'+ );+ expect(score).toBe(-1);+ });++ it('returns -1 when a term does not match (space-separated)', () => {+ const score = fuzzyScoreCommaSpaceSeparated(+ 'jackson alice',+ 'jackson kustec, gabriel birman, jacob, eric hayes'+ );+ expect(score).toBe(-1);+ });++ it('returns 1 for empty query', () => {+ const score = fuzzyScoreCommaSpaceSeparated('', 'Nick Noble,teo,hutch');+ expect(score).toBe(1);+ });+});++describe('highlightCommaSpaceSeparatedMatches', () => {+ it('highlights comma-separated query terms', () => {+ const result = highlightCommaSpaceSeparatedMatches(+ 'hutch,gab',+ 'hutch, gab, eric'+ );+ expect(result).toContain('<macro_em>hutch</macro_em>');+ expect(result).toContain('<macro_em>gab</macro_em>');+ });++ it('highlights space-separated query terms', () => {+ const result = highlightCommaSpaceSeparatedMatches(+ 'hutch gab',+ 'hutch, gab, eric'+ );+ expect(result).toContain('<macro_em>hutch</macro_em>');+ expect(result).toContain('<macro_em>gab</macro_em>');+ });++ it('highlights terms in any order', () => {+ const result = highlightCommaSpaceSeparatedMatches(+ 'jackson jacob',+ 'jacob, jackson kustec, gabriel'+ );+ expect(result).toContain('<macro_em>jacob</macro_em>');+ expect(result).toContain('<macro_em>jackson</macro_em>');+ });++ it('highlights partial fuzzy matches', () => {+ const result = highlightCommaSpaceSeparatedMatches(+ 'jac gab',+ 'jacob, jackson kustec, gabriel'+ );+ expect(result).toContain('<macro_em>jac</macro_em>');+ expect(result).toContain('<macro_em>gab</macro_em>');+ });++ it('preserves comma separators in output', () => {+ const result = highlightCommaSpaceSeparatedMatches(+ 'hutch gab',+ 'hutch, gab, eric'+ );+ expect(result).toMatch(/,\s/);+ });++ it('returns original text for empty query', () => {+ const result = highlightCommaSpaceSeparatedMatches('', 'hutch, gab, eric');+ expect(result).toBe('hutch, gab, eric');+ });++ it('does not highlight unmatched terms', () => {+ const result = highlightCommaSpaceSeparatedMatches(+ 'hutch',+ 'hutch, gab, eric'+ );+ expect(result).toContain('<macro_em>hutch</macro_em>');+ expect(result).not.toContain('<macro_em>gab</macro_em>');+ expect(result).not.toContain('<macro_em>eric</macro_em>');+ });+});++describe('fuzzyMatch with delimiter-separated channel matching', () => {+ it('matches channels with space-separated query in any order', () => {+ const items: TestItem[] = [+ {+ id: '1',+ name: 'jackson kustec, gabriel birman, jacob, eric hayes',+ type: 'channel',+ },+ { id: '2', name: 'Other Channel', type: 'channel' },+ ];++ const results1 = fuzzyMatch('jackson jacob', items, (item) => item.name);+ expect(results1.length).toBe(1);+ expect(results1[0].item.id).toBe('1');+ expect(results1[0].nameHighlight).toContain('<macro_em>jackson</macro_em>');+ expect(results1[0].nameHighlight).toContain('<macro_em>jacob</macro_em>');++ const results2 = fuzzyMatch('jacob jackson', items, (item) => item.name);+ expect(results2.length).toBe(1);+ expect(results2[0].item.id).toBe('1');+ });++ it('uses regular fuzzy search for non-channel items', () => {+ const items: TestItem[] = [+ { id: '1', name: 'jackson document', type: 'document' },+ { id: '2', name: 'jacob notes', type: 'document' },+ ];++ const results = fuzzyMatch('jackson', items, (item) => item.name);+ expect(results.length).toBe(1);+ expect(results[0].item.id).toBe('1');+ });++ it('uses regular fuzzy search when query has no spaces or commas', () => {+ const items: TestItem[] = [+ { id: '1', name: 'jackson kustec, jacob', type: 'channel' },+ { id: '2', name: 'james', type: 'channel' },+ ];++ const results = fuzzyMatch('jack', items, (item) => item.name);+ expect(results.length).toBe(1);+ expect(results[0].item.id).toBe('1');+ });++ it('returns all items with no highlights when query is empty', () => {+ const items: TestItem[] = [+ { id: '1', name: 'Channel 1', type: 'channel' },+ { id: '2', name: 'Channel 2', type: 'channel' },+ ];++ const results = fuzzyMatch('', items, (item) => item.name);+ expect(results.length).toBe(2);+ expect(results[0].nameHighlight).toBe('Channel 1');+ expect(results[1].nameHighlight).toBe('Channel 2');+ });++ it('correctly orders non-channel items by match quality', () => {+ const items: TestItem[] = [+ { id: '1', name: 'test document', type: 'document' },+ { id: '2', name: 'test', type: 'document' },+ { id: '3', name: 'testing something else', type: 'document' },+ ];++ const results = fuzzyMatch('test', items, (item) => item.name);++ expect(results.length).toBe(3);+ expect(results[0].item.name).toBe('test');+ expect(results[1].item.name).toBe('test document');+ expect(results[2].item.name).toBe('testing something else');+ });++ it('correctly orders channels and non-channels by match quality with single-term query', () => {+ const items: TestItem[] = [+ { id: '1', name: 'design document', type: 'document' },+ { id: '2', name: 'design, alice, bob', type: 'channel' },+ { id: '3', name: 'design', type: 'document' },+ { id: '4', name: 'design team, charlie', type: 'channel' },+ ];++ const results = fuzzyMatch('design', items, (item) => item.name);++ expect(results.length).toBe(4);+ expect(results[0].item.name).toBe('design');+ expect(results[1].item.name).toBe('design document');+ expect(results[2].item.name).toBe('design team, charlie');+ expect(results[3].item.name).toBe('design, alice, bob');+ });++ it('correctly interleaves channels and non-channels by match quality with multi-term query', () => {+ const items: TestItem[] = [+ { id: '1', name: 'alice, bob', type: 'channel' },+ { id: '2', name: 'alice bob', type: 'document' },+ { id: '3', name: 'bob notes', type: 'document' },+ { id: '4', name: 'alice, charlie, bob', type: 'channel' },+ ];++ // Use space-separated query to trigger channel-specific matching+ const results = fuzzyMatch('alice bob', items, (item) => item.name);++ expect(results.length).toBe(3);+ expect(results[0].item.name).toBe('alice, bob');+ expect(results[1].item.name).toBe('alice, charlie, bob');+ });+});diff --git a/js/app/packages/core/util/fuzzy.ts b/js/app/packages/core/util/fuzzy.tsindex 72b5c961bd..5d617ce63e 100644--- a/js/app/packages/core/util/fuzzy.ts+++ b/js/app/packages/core/util/fuzzy.ts@@ -18,8 +18,56 @@ const mark = (part: string, matched: boolean) =>const append = (accum: string, part: string) => accum + part;+/**+ * Highlights matched query terms in a comma-separated text.+ * Query can be space or comma-separated. Each query term is highlighted+ * in the text parts where it matches.+ */+export function highlightCommaSpaceSeparatedMatches(+ query: string,+ text: string+): string {+ if (!query) return text;++ const queryParts = query+ .split(/[\s,]+/)+ .map((p) => p.trim())+ .filter((p) => p.length > 0);+ const textParts = text.split(',').map((p) => p.trim());++ const highlightedParts = textParts.map((textPart) => {+ let highlighted = textPart;++ // Try to highlight each query part in this text part+ for (const queryPart of queryParts) {+ const haystack = [textPart];+ const idxs = uf.filter(haystack, queryPart);++ if (idxs && idxs.length > 0) {+ const info = uf.info(idxs, haystack, queryPart);+ if (info.ranges && info.ranges[0]) {+ highlighted = uFuzzy.highlight(+ textPart,+ info.ranges[0],+ mark,+ '',+ append+ );+ break; // Only highlight once per text part+ }+ }+ }++ return highlighted;+ });++ return highlightedParts.join(', ');+}+/*** Fuzzy matches items against a query, returning filtered and ranked results with highlighted matches.+ * For channel items (type === 'channel'), supports delimiter-separated matching where query terms+ * separated by spaces or commas can match in any order.* Returns all items with no highlights if query is empty.*/export function fuzzyMatch<T>(@@ -34,30 +82,71 @@ export function fuzzyMatch<T>(score: 0,}));- const haystack = items.map(extract);- const idxs = uf.filter(haystack, query);-- if (!idxs || idxs.length === 0) return [];+ const hasMultipleTerms = /[\s,]/.test(query);+ const channelItems: T[] = [];+ const nonChannelItems: T[] = [];++ // Separate channels from other items if query has multiple terms+ if (hasMultipleTerms) {+ for (const item of items) {+ if ((item as any).type === 'channel') {+ channelItems.push(item);+ } else {+ nonChannelItems.push(item);+ }+ }+ }- const info = uf.info(idxs, haystack, query);- const order = uf.sort(info, haystack, query);+ const results: FuzzyNameMatchResultWithItem<T>[] = [];++ // Handle channel items with delimiter-separated matching+ if (hasMultipleTerms && channelItems.length > 0) {+ for (const item of channelItems) {+ const name = extract(item);+ const score = fuzzyScoreCommaSpaceSeparated(query, name);+ if (score >= 0) {+ results.push({+ item,+ nameHighlight: highlightCommaSpaceSeparatedMatches(query, name),+ score: score * 100,+ });+ }+ }+ }- if (!order || order.length === 0) return [];+ // Handle non-channel items (or all items if no multiple terms)+ const itemsToSearch = hasMultipleTerms ? nonChannelItems : items;+ const haystack = itemsToSearch.map(extract);+ const idxs = uf.filter(haystack, query);- return order.map((orderIdx) => {- const infoIdx = info.idx[orderIdx];- const ranges = info.ranges[orderIdx];+ if (idxs && idxs.length > 0) {+ const info = uf.info(idxs, haystack, query);+ const order = uf.sort(info, haystack, query);++ if (order && order.length > 0) {+ order.forEach((orderIdx, position) => {+ const infoIdx = info.idx[orderIdx];+ const ranges = info.ranges[orderIdx];++ const nameHighlight = ranges+ ? uFuzzy.highlight(haystack[infoIdx], ranges, mark, '', append)+ : haystack[infoIdx];++ results.push({+ item: itemsToSearch[infoIdx],+ nameHighlight,+ // Normalize score: position 0 (best match) should get highest score+ // Use 100 - position to put on same scale as channel scores (0-100)+ score: 100 - position,+ });+ });+ }+ }- const nameHighlight = ranges- ? uFuzzy.highlight(haystack[infoIdx], ranges, mark, '', append)- : haystack[infoIdx];+ // Sort by score (higher is better for both channels and non-channels)+ results.sort((a, b) => b.score - a.score);- return {- item: items[infoIdx],- nameHighlight,- score: orderIdx,- };- });+ return results;}/**@@ -98,15 +187,20 @@ export function fuzzyTest(query: string, text: string): boolean {}/**- * Tests if a comma-separated query matches against a comma-separated text.- * Each query part must fuzzy-match at least one text part.- * e.g., query "nick,hutch" matches text "Nick Noble,teo,hutch"+ * Tests if a delimiter-separated query matches against a comma-separated text.+ * Query can be separated by commas or spaces. Text is always comma-separated.+ * Each query part must fuzzy-match at least one text part in any order.+ * e.g., query "nick hutch" or "nick,hutch" matches text "Nick Noble,teo,hutch"+ * e.g., query "jackson jacob" matches text "jacob, jackson kustec, gabriel"*/-export function fuzzyTestCommaSeparated(query: string, text: string): boolean {+export function fuzzyTestCommaSpaceSeparated(+ query: string,+ text: string+): boolean {if (!query) return true;const queryParts = query- .split(',')+ .split(/[\s,]+/).map((p) => p.trim()).filter((p) => p.length > 0);const textParts = text@@ -133,15 +227,21 @@ export function fuzzyTestCommaSeparated(query: string, text: string): boolean {}/**- * Calculates a score for comma-separated fuzzy matching.+ * Calculates a score for delimiter-separated fuzzy matching.+ * Query can be separated by commas or spaces. Text is always comma-separated.* Returns the average of best match scores for each query part.- * Returns -1 if any query part fails to match (can be used instead of fuzzyTestCommaSeparated).+ * Returns -1 if any query part fails to match.+ * e.g., query "nick hutch" or "nick,hutch" matches text "Nick Noble,teo,hutch"+ * e.g., query "jackson jacob" matches text "jacob, jackson kustec, gabriel"*/-export function fuzzyScoreCommaSeparated(query: string, text: string): number {+export function fuzzyScoreCommaSpaceSeparated(+ query: string,+ text: string+): number {if (!query) return 1;const queryParts = query- .split(',')+ .split(/[\s,]+/).map((p) => p.trim()).filter((p) => p.length > 0);const textParts = text