Open History to see the last commit
1import {
2 insertRankedSearchResult,
3 normalizeSearchQuery,
4 scoreQueryMatch,
5} from "@t3tools/shared/searchRanking";
6
7import type { ComposerCommandItem } from "./ComposerCommandMenu";
8import { scoreProviderSkill } from "../../providerSkillSearch";
9
10type SlashSearchItem = Extract<
11 ComposerCommandItem,
12 { type: "slash-command" | "provider-slash-command" | "skill" }
13>;
14
15/** Provider commands, `/side`, `/btw` and `/clear` work only at the message start. */
16export function slashCommandItemsForPromptPosition(
17 items: ReadonlyArray<SlashSearchItem>,
18 isAtPromptStart: boolean,
19): SlashSearchItem[] {
20 if (isAtPromptStart) {
21 return [...items];
22 }
23 return items.filter(
24 (item) =>
25 item.type === "skill" ||
26 (item.type === "slash-command" &&
27 item.command !== "side" &&
28 item.command !== "btw" &&
29 item.command !== "clear"),
30 );
31}
32
33function scoreSlashCommandItem(item: SlashSearchItem, query: string): number | null {
34 if (item.type === "skill") {
35 if (query === "skill") {
36 return 0;
37 }
38 const skillQuery = query.startsWith("skill:") ? query.slice("skill:".length) : query;
39 const skillScore = skillQuery ? scoreProviderSkill(item.skill, skillQuery) : 0;
40 if (skillScore !== null) {
41 return skillScore;
42 }
43 return "skill".startsWith(query) ? Number.MAX_SAFE_INTEGER : null;
44 }
45
46 const primaryValue =
47 item.type === "slash-command"
48 ? item.command.toLowerCase()
49 : item.command.name.normalize("NFC").toLowerCase();
50 const description = item.description.normalize("NFC").toLowerCase();
51 const commandQuery = query.normalize("NFC");
52
53 const scores = [
54 scoreQueryMatch({
55 value: primaryValue,
56 query: commandQuery,
57 exactBase: 0,
58 prefixBase: 2,
59 boundaryBase: 4,
60 includesBase: 6,
61 fuzzyBase: 100,
62 boundaryMarkers: ["-", "_", "/"],
63 }),
64 scoreQueryMatch({
65 value: description,
66 query: commandQuery,
67 exactBase: 20,
68 prefixBase: 22,
69 boundaryBase: 24,
70 includesBase: 26,
71 }),
72 ].filter((score): score is number => score !== null);
73
74 if (scores.length === 0) {
75 return null;
76 }
77
78 return Math.min(...scores);
79}
80
81export function searchSlashCommandItems(
82 items: ReadonlyArray<SlashSearchItem>,
83 query: string,
84): SlashSearchItem[] {
85 const normalizedQuery = normalizeSearchQuery(query, { trimLeadingPattern: /^\/+/ });
86 if (!normalizedQuery) {
87 return [...items];
88 }
89
90 const ranked: Array<{
91 item: SlashSearchItem;
92 score: number;
93 tieBreaker: string;
94 }> = [];
95
96 for (const item of items) {
97 const score = scoreSlashCommandItem(item, normalizedQuery);
98 if (score === null) {
99 continue;
100 }
101
102 insertRankedSearchResult(
103 ranked,
104 {
105 item,
106 score,
107 tieBreaker:
108 item.type === "slash-command"
109 ? `0\u0000${item.command}`
110 : item.type === "provider-slash-command"
111 ? `1\u0000${item.command.name}\u0000${item.provider}`
112 : `2\u0000${item.skill.name}\u0000${item.provider}`,
113 },
114 Number.POSITIVE_INFINITY,
115 );
116 }
117
118 return ranked.map((entry) => entry.item);
119}