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