Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat: command k search improve (#2412)

gbirmana49617661149
diff --git a/js/app/packages/app/component/command/useCommandItems.ts b/js/app/packages/app/component/command/useCommandItems.ts
index 93f196017e..6bd68b9ea2 100644
--- a/js/app/packages/app/component/command/useCommandItems.ts
+++ b/js/app/packages/app/component/command/useCommandItems.ts
@@ -53,7 +53,7 @@ function createSearchConfig(hasQuery: boolean) {
useViewedAt: true,
dmBoost: hasQuery ? 1.8 : 1.0,
fuzzyWeight: hasQuery ? 0.7 : 0.0,
- timeWeight: hasQuery ? 0.3 : 0.9,
+ timeWeight: hasQuery ? 0.5 : 0.9,
minFuzzyThreshold: hasQuery ? 0.1 : 0,
commaSeparatedChannelMatch: true,
};
diff --git a/js/app/packages/core/util/freshSort.ts b/js/app/packages/core/util/freshSort.ts
index 2c81cb2a93..9a18354b8c 100644
--- a/js/app/packages/core/util/freshSort.ts
+++ b/js/app/packages/core/util/freshSort.ts
@@ -3,8 +3,7 @@
* alongside freshness.
*/
import type { Accessor } from 'solid-js';
-import type { FilterResult } from 'fuzzy';
-import fuzzy from 'fuzzy';
+import uFuzzy from '@leeoniya/ufuzzy';
import { differenceInMilliseconds } from 'date-fns';
import type { DateValue } from './date';
import { fuzzyScoreCommaSpaceSeparated } from './fuzzy';
@@ -13,6 +12,14 @@ import {
parsedDurationToMilliseconds,
} from './dateSearch/dateParser';
+const uf = new uFuzzy({});
+
+interface FuzzyFilterResult<T> {
+ original: T;
+ string: string;
+ score: number;
+}
+
type BoostFn<T> = (item: T) => number;
type NameFn<T> = (item: T) => string;
@@ -46,6 +53,10 @@ export interface FreshSortConfig<T> {
commaSeparatedChannelMatch?: boolean;
/** Function to calculate per-item boost. Returns a boost multiplier (e.g., 0.2 for +20% boost). Default: undefined */
boostFn?: BoostFn<T>;
+ /** How much to penalize matches with gaps between characters (0-1). Higher values penalize spread-out matches more. Default: 1.0 */
+ gapPenaltyWeight?: number;
+ /** How much to penalize matches that start later in the string. Higher values penalize later starts more. Default: 0.05 */
+ startBonusDecay?: number;
}
type FreshSortConfigWithDefaults = Required<FreshSortConfig<unknown>>;
@@ -62,7 +73,6 @@ export interface FreshSortResult<T> {
timeScore: number;
brevityScore: number;
combinedScore: number;
- fuzzyResult?: FilterResult<T>;
}
const DEFAULT_CONFIG = {
@@ -77,6 +87,8 @@ const DEFAULT_CONFIG = {
dmBoost: 1.0,
commaSeparatedChannelMatch: false,
boostFn: undefined,
+ gapPenaltyWeight: 1.0,
+ startBonusDecay: 0.05,
} as const;
function extractTimestamp(
@@ -132,8 +144,51 @@ function calculateBrevityScore(text: string): number {
return Math.exp(-2 * normalizedLength);
}
+function ufuzzyFilter<T>(
+ items: T[],
+ getName: NameFn<T>,
+ query: string,
+ gapPenaltyWeight: number,
+ startBonusDecay: number
+): FuzzyFilterResult<T>[] {
+ const haystack = items.map(getName);
+ const idxs = uf.filter(haystack, query);
+
+ if (!idxs || idxs.length === 0) return [];
+
+ const info = uf.info(idxs, haystack, query);
+ const order = uf.sort(info, haystack, query);
+
+ if (!order || order.length === 0) return [];
+
+ const queryLen = query.length;
+ return order.map((orderIdx) => {
+ const haystackIdx = info.idx[orderIdx];
+ const ranges = info.ranges[orderIdx];
+
+ let matchSpan = queryLen;
+ if (ranges && ranges.length >= 2) {
+ matchSpan = ranges[ranges.length - 1] - ranges[0];
+ }
+
+ const clampedGapWeight = Math.max(0, Math.min(1, gapPenaltyWeight));
+ const clampedStartDecay = Math.max(0, startBonusDecay);
+ const rawGapPenalty = matchSpan > 0 ? queryLen / matchSpan : 1;
+ const gapPenalty = 1 - (1 - rawGapPenalty) * clampedGapWeight;
+ const startBonus =
+ 1 / (1 + (info.start[orderIdx] ?? 0) * clampedStartDecay);
+ const score = gapPenalty * startBonus * 100;
+
+ return {
+ original: items[haystackIdx],
+ string: haystack[haystackIdx],
+ score,
+ };
+ });
+}
+
function freshSort<T>(
- filterResults: FilterResult<T>[],
+ filterResults: FuzzyFilterResult<T>[],
config: FreshSortConfig<T> = {},
isChannelItem: BooleanFn<T>,
isDmItem: BooleanFn<T>,
@@ -199,7 +254,6 @@ function freshSort<T>(
timeScore,
brevityScore,
combinedScore,
- fuzzyResult: result,
};
});
@@ -224,27 +278,41 @@ export function createFreshSearch<T>({
}: CreateFreshSearchArgs<T>) {
return (items: T[], query: string): FreshSortResult<T>[] => {
const finalConfig = { ...DEFAULT_CONFIG, ...config };
+ const trimmedQuery = query.trim();
- const hasComma = query.includes(',');
- const hasSpace = query.includes(' ');
+ const hasComma = trimmedQuery.includes(',');
+ const hasSpace = trimmedQuery.includes(' ');
const useMultiTermChannelMatch =
finalConfig.commaSeparatedChannelMatch && (hasComma || hasSpace);
+ if (!trimmedQuery) {
+ const allResults: FuzzyFilterResult<T>[] = items.map((item) => ({
+ original: item,
+ string: getName(item),
+ score: 1,
+ }));
+ return freshSort(
+ allResults,
+ config,
+ isChannelItem,
+ isDmItem,
+ getTimestamp
+ );
+ }
+
if (useMultiTermChannelMatch) {
- // For comma or space-separated queries, handle channel items specially
- const channelResults: FilterResult<T>[] = [];
+ const channelResults: FuzzyFilterResult<T>[] = [];
const nonChannelItems: T[] = [];
for (const item of items) {
if (isChannelItem(item)) {
const name = getName(item);
- const score = fuzzyScoreCommaSpaceSeparated(query, name);
+ const score = fuzzyScoreCommaSpaceSeparated(trimmedQuery, name);
if (score >= 0) {
channelResults.push({
original: item,
string: name,
- score: score * 100, // Scale to match fuzzy library scoring
- index: 0,
+ score: score * 100,
});
}
} else {
@@ -252,12 +320,13 @@ export function createFreshSearch<T>({
}
}
- // Get fuzzy results for non-channel items using regular matching
- const nonChannelResults = fuzzy.filter(query, nonChannelItems, {
- extract: getName,
- });
-
- // Combine results
+ const nonChannelResults = ufuzzyFilter(
+ nonChannelItems,
+ getName,
+ trimmedQuery,
+ finalConfig.gapPenaltyWeight,
+ finalConfig.startBonusDecay
+ );
const allResults = [...channelResults, ...nonChannelResults];
return freshSort(
allResults,
@@ -268,9 +337,13 @@ export function createFreshSearch<T>({
);
}
- const fuzzyResults = fuzzy.filter(query, items, {
- extract: getName,
- });
+ const fuzzyResults = ufuzzyFilter(
+ items,
+ getName,
+ query,
+ finalConfig.gapPenaltyWeight,
+ finalConfig.startBonusDecay
+ );
return freshSort(
fuzzyResults,
config,
diff --git a/js/app/packages/core/util/fuzzy.ts b/js/app/packages/core/util/fuzzy.ts
index eb4f32492d..20cab30974 100644
--- a/js/app/packages/core/util/fuzzy.ts
+++ b/js/app/packages/core/util/fuzzy.ts
@@ -36,29 +36,27 @@ export function highlightCommaSpaceSeparatedMatches(
const textParts = text.split(',').map((p) => p.trim());
const highlightedParts = textParts.map((textPart) => {
- let highlighted = textPart;
+ const haystack = [textPart];
+
+ const fullIdxs = uf.filter(haystack, query);
+ if (fullIdxs && fullIdxs.length > 0) {
+ const info = uf.info(fullIdxs, haystack, query);
+ if (info.ranges && info.ranges[0]) {
+ return uFuzzy.highlight(textPart, info.ranges[0], mark, '', append);
+ }
+ }
- // 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 uFuzzy.highlight(textPart, info.ranges[0], mark, '', append);
}
}
}
- return highlighted;
+ return textPart;
});
return highlightedParts.join(', ');