Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ComposerCommandMenu.tsx

319 lines10.2 KB

1import { formatProviderSkillDisplayName } from "@t3tools/shared/inlineSkills";
2import type { ComposerPathSearchTarget } from "@t3tools/client-runtime/state/threads";
3import {
4 resolveProviderSkillSourceKind,
5 type ProviderSkillSourceKind,
6} from "@t3tools/client-runtime/providerSkills";
7import {
8 type EnvironmentId,
9 type EnvironmentMachineKind,
10 type ProjectEntry,
11 type ProviderDriverKind,
12 type ProviderInstanceId,
13 type PullRequestContextMetadata,
14 type ScopedThreadRef,
15 type ServerProviderSkill,
16 type ServerProviderSlashCommand,
17} from "@t3tools/contracts";
18import {
19 BlocksIcon,
20 FolderIcon,
21 MessagesSquareIcon,
22 PackageIcon,
23 SettingsIcon,
24 UserRoundIcon,
25 type LucideIcon,
26} from "lucide-react";
27import { memo, useLayoutEffect, useRef } from "react";
28​
29import { type ComposerSlashCommand, type ComposerTriggerKind } from "../../composer-logic";
30import { cn } from "~/lib/utils";
31import { Badge } from "../ui/badge";
32import { Command, CommandGroup, CommandItem, CommandList } from "../ui/command";
33import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
34import { PierreEntryIcon } from "./PierreEntryIcon";
35import { ProviderInstanceIcon } from "./ProviderInstanceIcon";
36import { ComposerBanner } from "./ComposerBanner";
37import { resolvePullRequestState } from "../pullRequest/pullRequestPresentation";
38​
39export type ComposerCommandItem =
40 | {
41 id: string;
42 type: "path";
43 path: string;
44 pathKind: ProjectEntry["kind"];
45 searchTarget: ComposerPathSearchTarget;
46 label: string;
47 description: string;
48 }
49 | {
50 id: string;
51 type: "slash-command";
52 command: ComposerSlashCommand;
53 label: string;
54 description: string;
55 }
56 | {
57 id: string;
58 type: "provider-slash-command";
59 provider: ProviderDriverKind;
60 command: ServerProviderSlashCommand;
61 label: string;
62 description: string;
63 }
64 | {
65 id: string;
66 type: "skill";
67 provider: ProviderDriverKind;
68 skill: ServerProviderSkill;
69 label: string;
70 description: string;
71 }
72 | {
73 id: string;
74 type: "pull-request";
75 pullRequest: PullRequestContextMetadata;
76 label: string;
77 description: string;
78 }
79 | {
80 id: string;
81 type: "thread";
82 thread: ScopedThreadRef;
83 label: string;
84 description: string;
85 }
86 | {
87 id: string;
88 type: "environment";
89 environmentId: EnvironmentId;
90 machine: EnvironmentMachineKind;
91 label: string;
92 description: string;
93 }
94 | {
95 id: string;
96 type: "model";
97 instanceId: ProviderInstanceId;
98 model: string;
99 driverKind: ProviderDriverKind;
100 providerDisplayName: string;
101 acpRegistryAgentId: string | undefined;
102 acpRegistryIconUrl: string | undefined;
103 label: string;
104 description: string;
105 };
106​
107export const ComposerCommandMenu = memo(function ComposerCommandMenu(props: {
108 listId: string;
109 items: ComposerCommandItem[];
110 resolvedTheme: "light" | "dark";
111 isLoading: boolean;
112 triggerKind: ComposerTriggerKind | null;
113 emptyStateText?: string;
114 activeItemId: string | null;
115 onHighlightedItemChange: (itemId: string | null) => void;
116 onSelect: (item: ComposerCommandItem) => void;
117}) {
118 const listRef = useRef<HTMLDivElement>(null);
119​
120 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- scrolls the highlighted menu item's DOM node into view
121 useLayoutEffect(() => {
122 if (!props.activeItemId || !listRef.current) return;
123 const el = listRef.current.querySelector<HTMLElement>(
124 `[data-composer-item-id="${CSS.escape(props.activeItemId)}"]`,
125 );
126 el?.scrollIntoView({ block: "nearest" });
127 }, [props.activeItemId]);
128​
129 return (
130 <Command
131 autoHighlight={false}
132 mode="none"
133 onItemHighlighted={(highlightedValue) => {
134 props.onHighlightedItemChange(
135 typeof highlightedValue === "string" ? highlightedValue : null,
136 );
137 }}
138 >
139 <ComposerBanner.Surface
140 ref={listRef}
141 className="flex min-h-0 w-full flex-col overflow-hidden pb-(--chat-composer-attachment-overlap) **:data-[slot=command-item]:rounded-lg **:data-[slot=scroll-area-scrollbar]:data-[orientation=vertical]:my-4"
142 data-composer-command-drawer="true"
143 >
144 {props.items.length > 0 ? (
145 <CommandList
146 id={props.listId}
147 aria-label={props.triggerKind ? LISTBOX_LABEL_BY_TRIGGER[props.triggerKind] : undefined}
148 className="max-h-72 min-h-0 scroll-pb-6"
149 >
150 <CommandGroup>
151 {props.items.map((item) => (
152 <ComposerCommandMenuItem
153 key={item.id}
154 optionId={composerSuggestionOptionId(props.listId, item.id)}
155 item={item}
156 triggerKind={props.triggerKind}
157 resolvedTheme={props.resolvedTheme}
158 isActive={props.activeItemId === item.id}
159 onHighlight={props.onHighlightedItemChange}
160 onSelect={props.onSelect}
161 />
162 ))}
163 </CommandGroup>
164 </CommandList>
165 ) : (
166 <div className="px-5 pt-3.5 pb-7">
167 <p className="text-secondary-label text-xs">
168 {props.isLoading
169 ? props.triggerKind === "skill" || props.triggerKind === "slash-command"
170 ? "Searching workspace skills..."
171 : props.triggerKind === "pull-request"
172 ? "Finding pull request..."
173 : "Searching workspace files..."
174 : (props.emptyStateText ??
175 (props.triggerKind === "skill"
176 ? "No skills found. Try / to browse provider commands."
177 : props.triggerKind === "path"
178 ? "No matching files or folders."
179 : "No matching command."))}
180 </p>
181 </div>
182 )}
183 </ComposerBanner.Surface>
184 </Command>
185 );
186});
187​
188const ComposerCommandMenuItem = memo(function ComposerCommandMenuItem(props: {
189 optionId: string;
190 item: ComposerCommandItem;
191 triggerKind: ComposerTriggerKind | null;
192 resolvedTheme: "light" | "dark";
193 isActive: boolean;
194 onHighlight: (itemId: string | null) => void;
195 onSelect: (item: ComposerCommandItem) => void;
196}) {
197 const skillSourceKind =
198 props.item.type === "skill" ? resolveProviderSkillSourceKind(props.item.skill) : null;
199 const isSlashSkill =
200 props.triggerKind === "slash-command" && props.item.type === "skill" ? props.item.skill : null;
201 const pullRequestPresentation =
202 props.item.type === "pull-request" ? resolvePullRequestState(props.item.pullRequest) : null;
203​
204 return (
205 <CommandItem
206 render={<div id={props.optionId} />}
207 aria-selected={props.isActive}
208 value={props.item.id}
209 data-composer-item-id={props.item.id}
210 active={props.isActive}
211 onMouseMove={() => {
212 if (!props.isActive) props.onHighlight(props.item.id);
213 }}
214 onMouseDown={(event) => {
215 event.preventDefault();
216 }}
217 onClick={() => {
218 props.onSelect(props.item);
219 }}
220 >
221 {props.item.type === "path" ? (
222 <PierreEntryIcon
223 pathValue={props.item.path}
224 kind={props.item.pathKind}
225 theme={props.resolvedTheme}
226 />
227 ) : null}
228 {props.item.type === "thread" ? (
229 <MessagesSquareIcon aria-hidden="true" className="size-4 shrink-0 text-secondary-label" />
230 ) : null}
231 {props.item.type === "environment" ? (
232 <EnvironmentMachineIcon
233 kind={props.item.machine}
234 aria-hidden="true"
235 className="size-4 shrink-0 text-secondary-label"
236 />
237 ) : null}
238 {props.item.type === "model" ? (
239 <ProviderInstanceIcon
240 driverKind={props.item.driverKind}
241 displayName={props.item.providerDisplayName}
242 acpRegistryAgentId={props.item.acpRegistryAgentId}
243 acpRegistryIconUrl={props.item.acpRegistryIconUrl}
244 className="size-4 shrink-0"
245 iconClassName="size-4"
246 />
247 ) : null}
248 {pullRequestPresentation ? (
249 <pullRequestPresentation.Icon
250 role="img"
251 aria-label={pullRequestPresentation.label}
252 className={cn("size-4 shrink-0", pullRequestPresentation.toneClassName)}
253 />
254 ) : null}
255 <span className="flex min-w-0 flex-1 items-center gap-2">
256 <span className="min-w-0 max-w-[45%] shrink-0 truncate font-sans text-xs font-medium">
257 {isSlashSkill ? (
258 <>
259 <span className="text-secondary-label">/skill:</span>
260 {formatProviderSkillDisplayName(isSlashSkill)}
261 </>
262 ) : (
263 props.item.label
264 )}
265 </span>
266 <span className="min-w-0 flex-1 truncate text-left text-secondary-label text-xs">
267 {props.item.description}
268 </span>
269 {skillSourceKind ? (
270 <SkillSourceBadge
271 kind={skillSourceKind}
272 showSkillSuffix={props.triggerKind === "skill"}
273 />
274 ) : null}
275 </span>
276 </CommandItem>
277 );
278});
279​
280export function composerSuggestionOptionId(listId: string, itemId: string): string {
281 // JSON escapes lone UTF-16 surrogates before URI encoding without losing identity.
282 return `${listId}-${encodeURIComponent(JSON.stringify(itemId))}`;
284​
285const LISTBOX_LABEL_BY_TRIGGER: Record<ComposerTriggerKind, string> = {
286 path: "Files and folders",
287 "pull-request": "Pull requests",
288 "slash-command": "Commands",
289 skill: "Skills",
290};
291​
292const SKILL_SOURCE_ICON_BY_KIND: Record<ProviderSkillSourceKind, LucideIcon> = {
293 app: BlocksIcon,
294 repo: FolderIcon,
295 project: FolderIcon,
296 personal: UserRoundIcon,
297 system: SettingsIcon,
298 other: PackageIcon,
299};
300​
301const SKILL_SOURCE_LABEL_BY_KIND: Record<ProviderSkillSourceKind, string> = {
302 app: "App",
303 repo: "Repo",
304 project: "Project",
305 personal: "Personal",
306 system: "System",
307 other: "Provider",
308};
309​
310function SkillSourceBadge(props: { kind: ProviderSkillSourceKind; showSkillSuffix: boolean }) {
311 const Icon = SKILL_SOURCE_ICON_BY_KIND[props.kind];
312 return (
313 <Badge className="ms-auto" variant="secondary">
314 <Icon aria-hidden="true" className="text-current" />
315 {SKILL_SOURCE_LABEL_BY_KIND[props.kind]}
316 {props.showSkillSuffix ? " Skill" : null}
317 </Badge>
318 );