Open History to see the last commit
1import {
2 type ProjectEntry,
3 type ProviderDriverKind,
4 type ServerProviderSkill,
5 type ServerProviderSlashCommand,
6} from "@t3tools/contracts";
7import { BotIcon } from "lucide-react";
8import { memo, useLayoutEffect, useMemo, useRef } from "react";
9
10import { type ComposerSlashCommand, type ComposerTriggerKind } from "../../composer-logic";
11import { formatProviderSkillInstallSource } from "~/providerSkillPresentation";
12import { cn } from "~/lib/utils";
13import {
14 Command,
15 CommandGroup,
16 CommandGroupLabel,
17 CommandItem,
18 CommandList,
19 CommandSeparator,
20} from "../ui/command";
21import { PierreEntryIcon } from "./PierreEntryIcon";
22
23export type ComposerCommandItem =
24 | {
25 id: string;
26 type: "path";
27 path: string;
28 pathKind: ProjectEntry["kind"];
29 label: string;
30 description: string;
31 }
32 | {
33 id: string;
34 type: "slash-command";
35 command: ComposerSlashCommand;
36 label: string;
37 description: string;
38 }
39 | {
40 id: string;
41 type: "provider-slash-command";
42 provider: ProviderDriverKind;
43 command: ServerProviderSlashCommand;
44 label: string;
45 description: string;
46 }
47 | {
48 id: string;
49 type: "skill";
50 provider: ProviderDriverKind;
51 skill: ServerProviderSkill;
52 label: string;
53 description: string;
54 };
55
56type ComposerCommandGroup = {
57 id: string;
58 label: string | null;
59 items: ComposerCommandItem[];
60};
61
62function SkillGlyph(props: { className?: string }) {
63 return (
64 <svg
65 viewBox="0 0 24 24"
66 fill="none"
67 stroke="currentColor"
68 strokeWidth="1.85"
69 strokeLinecap="round"
70 strokeLinejoin="round"
71 className={props.className}
72 aria-hidden="true"
73 >
74 <path d="M21 8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16Z" />
75 <path d="m3.3 7 8.7 5 8.7-5" />
76 <path d="M12 22V12" />
77 </svg>
78 );
79}
80
81function groupCommandItems(
82 items: ComposerCommandItem[],
83 triggerKind: ComposerTriggerKind | null,
84 groupSlashCommandSections: boolean,
85): ComposerCommandGroup[] {
86 if (triggerKind === "skill") {
87 return items.length > 0 ? [{ id: "skills", label: "Skills", items }] : [];
88 }
89 if (triggerKind !== "slash-command" || !groupSlashCommandSections) {
90 return [{ id: "default", label: null, items }];
91 }
92
93 const builtInItems = items.filter((item) => item.type === "slash-command");
94 const providerItems = items.filter((item) => item.type === "provider-slash-command");
95
96 const groups: ComposerCommandGroup[] = [];
97 if (builtInItems.length > 0) {
98 groups.push({ id: "built-in", label: "Built-in", items: builtInItems });
99 }
100 if (providerItems.length > 0) {
101 groups.push({ id: "provider", label: "Provider", items: providerItems });
102 }
103 return groups;
104}
105
106export const ComposerCommandMenu = memo(function ComposerCommandMenu(props: {
107 items: ComposerCommandItem[];
108 resolvedTheme: "light" | "dark";
109 isLoading: boolean;
110 triggerKind: ComposerTriggerKind | null;
111 groupSlashCommandSections?: boolean;
112 emptyStateText?: string;
113 activeItemId: string | null;
114 onHighlightedItemChange: (itemId: string | null) => void;
115 onSelect: (item: ComposerCommandItem) => void;
116}) {
117 const listRef = useRef<HTMLDivElement>(null);
118 const groups = useMemo(
119 () =>
120 groupCommandItems(props.items, props.triggerKind, props.groupSlashCommandSections ?? true),
121 [props.groupSlashCommandSections, props.items, props.triggerKind],
122 );
123
124 useLayoutEffect(() => {
125 if (!props.activeItemId || !listRef.current) return;
126 const el = listRef.current.querySelector<HTMLElement>(
127 `[data-composer-item-id="${CSS.escape(props.activeItemId)}"]`,
128 );
129 el?.scrollIntoView({ block: "nearest" });
130 }, [props.activeItemId]);
131
132 return (
133 <Command
134 autoHighlight={false}
135 mode="none"
136 onItemHighlighted={(highlightedValue) => {
137 props.onHighlightedItemChange(
138 typeof highlightedValue === "string" ? highlightedValue : null,
139 );
140 }}
141 >
142 <div ref={listRef} className="dropdown-glass relative w-full overflow-hidden rounded-[20px]">
143 {props.items.length > 0 ? (
144 <CommandList className="max-h-72">
145 {groups.map((group, groupIndex) => (
146 <div key={group.id}>
147 {groupIndex > 0 ? <CommandSeparator className="my-0.5" /> : null}
148 <CommandGroup>
149 {group.label ? (
150 <CommandGroupLabel className="px-3 pt-2 pb-1 text-[10px] font-semibold uppercase tracking-[0.08em] text-muted-foreground/55">
151 {group.label}
152 </CommandGroupLabel>
153 ) : null}
154 {group.items.map((item) => (
155 <ComposerCommandMenuItem
156 key={item.id}
157 item={item}
158 resolvedTheme={props.resolvedTheme}
159 isActive={props.activeItemId === item.id}
160 onHighlight={props.onHighlightedItemChange}
161 onSelect={props.onSelect}
162 />
163 ))}
164 </CommandGroup>
165 </div>
166 ))}
167 </CommandList>
168 ) : (
169 <div className="px-5 py-3.5">
170 {props.triggerKind === "skill" ? (
171 <CommandGroup>
172 <CommandGroupLabel className="px-0 pt-0 pb-1 text-[10px] font-semibold uppercase tracking-[0.08em] text-muted-foreground/55">
173 Skills
174 </CommandGroupLabel>
175 <p className="text-muted-foreground/70 text-xs">
176 {props.isLoading
177 ? "Searching workspace skills..."
178 : (props.emptyStateText ??
179 "No skills found. Try / to browse provider commands.")}
180 </p>
181 </CommandGroup>
182 ) : (
183 <p className="text-muted-foreground/70 text-xs">
184 {props.isLoading
185 ? "Searching workspace files..."
186 : (props.emptyStateText ??
187 (props.triggerKind === "path"
188 ? "No matching files or folders."
189 : "No matching command."))}
190 </p>
191 )}
192 </div>
193 )}
194 </div>
195 </Command>
196 );
197});
198
199const ComposerCommandMenuItem = memo(function ComposerCommandMenuItem(props: {
200 item: ComposerCommandItem;
201 resolvedTheme: "light" | "dark";
202 isActive: boolean;
203 onHighlight: (itemId: string | null) => void;
204 onSelect: (item: ComposerCommandItem) => void;
205}) {
206 const skillSourceLabel =
207 props.item.type === "skill" ? formatProviderSkillInstallSource(props.item.skill) : null;
208
209 return (
210 <CommandItem
211 value={props.item.id}
212 data-composer-item-id={props.item.id}
213 className={cn(
214 "cursor-pointer select-none gap-2 hover:bg-transparent hover:text-inherit data-highlighted:bg-transparent data-highlighted:text-inherit",
215 props.isActive && "bg-accent! text-accent-foreground!",
216 )}
217 onMouseMove={() => {
218 if (!props.isActive) props.onHighlight(props.item.id);
219 }}
220 onMouseDown={(event) => {
221 event.preventDefault();
222 }}
223 onClick={() => {
224 props.onSelect(props.item);
225 }}
226 >
227 {props.item.type === "path" ? (
228 <PierreEntryIcon
229 pathValue={props.item.path}
230 kind={props.item.pathKind}
231 theme={props.resolvedTheme}
232 />
233 ) : null}
234 {props.item.type === "slash-command" ? (
235 <BotIcon className="size-4 shrink-0 text-muted-foreground/80" />
236 ) : null}
237 {props.item.type === "provider-slash-command" ? (
238 <span className="inline-flex size-4 shrink-0 items-center justify-center text-muted-foreground/80">
239 <SkillGlyph className="size-3.5" />
240 </span>
241 ) : null}
242 {props.item.type === "skill" ? (
243 <span className="inline-flex size-4 shrink-0 items-center justify-center text-muted-foreground/80">
244 <SkillGlyph className="size-3.5" />
245 </span>
246 ) : null}
247 <span className="flex min-w-0 flex-1 items-center gap-2">
248 <span className="shrink-0">{props.item.label}</span>
249 <span className="min-w-0 flex-1 truncate text-muted-foreground/70 text-xs">
250 {props.item.description}
251 </span>
252 </span>
253 {skillSourceLabel ? (
254 <span className="shrink-0 pl-2 text-muted-foreground/70 text-xs">{skillSourceLabel}</span>
255 ) : null}
256 </CommandItem>
257 );
258});