Open History to see the last commit
1import {
2 resolveProviderSkillSourceKind,
3 type ProviderSkillSourceKind,
4} from "@t3tools/client-runtime/providerSkills";
5import {
6 type ProjectEntry,
7 type ProviderDriverKind,
8 type ServerProviderSkill,
9 type ServerProviderSlashCommand,
10} from "@t3tools/contracts";
11import {
12 BlocksIcon,
13 FolderGit2Icon,
14 FolderIcon,
15 PackageIcon,
16 SettingsIcon,
17 UserRoundIcon,
18 type LucideIcon,
19} from "lucide-react";
20import { memo, useLayoutEffect, useRef } from "react";
21
22import { type ComposerSlashCommand, type ComposerTriggerKind } from "../../composer-logic";
23import { cn } from "~/lib/utils";
24import { Command, CommandGroup, CommandItem, CommandList } from "../ui/command";
25import { PierreEntryIcon } from "./PierreEntryIcon";
26
27export type ComposerCommandItem =
28 | {
29 id: string;
30 type: "path";
31 path: string;
32 pathKind: ProjectEntry["kind"];
33 label: string;
34 description: string;
35 }
36 | {
37 id: string;
38 type: "slash-command";
39 command: ComposerSlashCommand;
40 label: string;
41 description: string;
42 }
43 | {
44 id: string;
45 type: "provider-slash-command";
46 provider: ProviderDriverKind;
47 command: ServerProviderSlashCommand;
48 label: string;
49 description: string;
50 }
51 | {
52 id: string;
53 type: "skill";
54 provider: ProviderDriverKind;
55 skill: ServerProviderSkill;
56 label: string;
57 description: string;
58 };
59
60export const ComposerCommandMenu = memo(function ComposerCommandMenu(props: {
61 items: ComposerCommandItem[];
62 resolvedTheme: "light" | "dark";
63 isLoading: boolean;
64 triggerKind: ComposerTriggerKind | null;
65 emptyStateText?: string;
66 activeItemId: string | null;
67 onHighlightedItemChange: (itemId: string | null) => void;
68 onSelect: (item: ComposerCommandItem) => void;
69}) {
70 const listRef = useRef<HTMLDivElement>(null);
71
72 useLayoutEffect(() => {
73 if (!props.activeItemId || !listRef.current) return;
74 const el = listRef.current.querySelector<HTMLElement>(
75 `[data-composer-item-id="${CSS.escape(props.activeItemId)}"]`,
76 );
77 el?.scrollIntoView({ block: "nearest" });
78 }, [props.activeItemId]);
79
80 return (
81 <Command
82 autoHighlight={false}
83 mode="none"
84 onItemHighlighted={(highlightedValue) => {
85 props.onHighlightedItemChange(
86 typeof highlightedValue === "string" ? highlightedValue : null,
87 );
88 }}
89 >
90 <div
91 ref={listRef}
92 className="chat-composer-drawer-surface chat-composer-drawer-attached relative w-full overflow-hidden **:data-[slot=scroll-area-scrollbar]:data-[orientation=vertical]:my-4"
93 data-composer-command-drawer="true"
94 >
95 {props.items.length > 0 ? (
96 <CommandList className="max-h-72 scroll-pb-6">
97 <CommandGroup>
98 {props.items.map((item) => (
99 <ComposerCommandMenuItem
100 key={item.id}
101 item={item}
102 triggerKind={props.triggerKind}
103 resolvedTheme={props.resolvedTheme}
104 isActive={props.activeItemId === item.id}
105 onHighlight={props.onHighlightedItemChange}
106 onSelect={props.onSelect}
107 />
108 ))}
109 </CommandGroup>
110 </CommandList>
111 ) : (
112 <div className="px-5 pt-3.5 pb-7">
113 <p className="text-secondary-label text-xs">
114 {props.isLoading
115 ? props.triggerKind === "skill"
116 ? "Searching workspace skills..."
117 : "Searching workspace files..."
118 : (props.emptyStateText ??
119 (props.triggerKind === "skill"
120 ? "No skills found. Try / to browse provider commands."
121 : props.triggerKind === "path"
122 ? "No matching files or folders."
123 : "No matching command."))}
124 </p>
125 </div>
126 )}
127 </div>
128 </Command>
129 );
130});
131
132const ComposerCommandMenuItem = memo(function ComposerCommandMenuItem(props: {
133 item: ComposerCommandItem;
134 triggerKind: ComposerTriggerKind | null;
135 resolvedTheme: "light" | "dark";
136 isActive: boolean;
137 onHighlight: (itemId: string | null) => void;
138 onSelect: (item: ComposerCommandItem) => void;
139}) {
140 const skillSourceKind =
141 props.item.type === "skill" ? resolveProviderSkillSourceKind(props.item.skill) : null;
142 const slashSkill =
143 props.triggerKind === "slash-command" && props.item.type === "skill" ? props.item.skill : null;
144
145 return (
146 <CommandItem
147 value={props.item.id}
148 data-composer-item-id={props.item.id}
149 className={cn(
150 "cursor-pointer select-none gap-3 rounded-lg px-3 py-2! hover:bg-transparent hover:text-inherit data-highlighted:bg-transparent data-highlighted:text-inherit",
151 props.isActive && "bg-accent! text-accent-foreground!",
152 )}
153 onMouseMove={() => {
154 if (!props.isActive) props.onHighlight(props.item.id);
155 }}
156 onMouseDown={(event) => {
157 event.preventDefault();
158 }}
159 onClick={() => {
160 props.onSelect(props.item);
161 }}
162 >
163 {props.item.type === "path" ? (
164 <PierreEntryIcon
165 pathValue={props.item.path}
166 kind={props.item.pathKind}
167 theme={props.resolvedTheme}
168 />
169 ) : skillSourceKind && !slashSkill ? (
170 <SkillSourceIcon kind={skillSourceKind} />
171 ) : null}
172 <span className="flex min-w-0 flex-1 items-baseline gap-3">
173 <span className="shrink-0 font-sans text-xs font-medium">
174 {slashSkill ? (
175 <>
176 <span className="text-secondary-label">skill:</span>
177 {slashSkill.name}
178 </>
179 ) : (
180 props.item.label
181 )}
182 </span>
183 <span className="min-w-0 flex-1 truncate text-right text-secondary-label text-xs">
184 {props.item.description}
185 </span>
186 </span>
187 </CommandItem>
188 );
189});
190
191const SKILL_SOURCE_ICON_BY_KIND: Record<ProviderSkillSourceKind, LucideIcon> = {
192 app: BlocksIcon,
193 repo: FolderGit2Icon,
194 project: FolderIcon,
195 personal: UserRoundIcon,
196 system: SettingsIcon,
197 other: PackageIcon,
198};
199
200const SKILL_SOURCE_LABEL_BY_KIND: Record<ProviderSkillSourceKind, string> = {
201 app: "App",
202 repo: "Repo",
203 project: "Project",
204 personal: "Personal",
205 system: "System",
206 other: "Other",
207};
208
209function SkillSourceIcon(props: { kind: ProviderSkillSourceKind }) {
210 const Icon = SKILL_SOURCE_ICON_BY_KIND[props.kind];
211 return (
212 <>
213 <Icon aria-hidden="true" className="size-4 shrink-0 text-icon-muted" />
214 <span className="sr-only">{SKILL_SOURCE_LABEL_BY_KIND[props.kind]} skill</span>
215 </>
216 );
217}