The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { formatProviderSkillDisplayName } from "@t3tools/shared/inlineSkills"; |
| 2 | import type { ComposerPathSearchTarget } from "@t3tools/client-runtime/state/threads"; |
| 3 | import { |
| 4 | resolveProviderSkillSourceKind, |
| 5 | type ProviderSkillSourceKind, |
| 6 | } from "@t3tools/client-runtime/providerSkills"; |
| 7 | import { |
| 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"; |
| 18 | import { |
| 19 | BlocksIcon, |
| 20 | FolderIcon, |
| 21 | MessagesSquareIcon, |
| 22 | PackageIcon, |
| 23 | SettingsIcon, |
| 24 | UserRoundIcon, |
| 25 | type LucideIcon, |
| 26 | } from "lucide-react"; |
| 27 | import { msg, useGT, useMessages } from "gt-react"; |
| 28 | import { memo, useLayoutEffect, useRef, useState } from "react"; |
| 29 | |
| 30 | import { type ComposerSlashCommand, type ComposerTriggerKind } from "../../composer-logic"; |
| 31 | import { cn } from "~/lib/utils"; |
| 32 | import { Badge } from "../ui/badge"; |
| 33 | import { Command, CommandGroup, CommandItem, CommandList } from "../ui/command"; |
| 34 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 35 | import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon"; |
| 36 | import { PierreEntryIcon } from "./PierreEntryIcon"; |
| 37 | import { ProviderInstanceIcon } from "./ProviderInstanceIcon"; |
| 38 | import { ComposerBanner } from "./ComposerBanner"; |
| 39 | import { resolvePullRequestState } from "../pullRequest/pullRequestPresentation"; |
| 40 | |
| 41 | export 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 | |
| 109 | export 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="max-h-72 min-h-0 scroll-pb-6" |
| 155 | > |
| 156 | <CommandGroup> |
| 157 | {props.items.map((item) => ( |
| 158 | <ComposerCommandMenuItem |
| 159 | key={item.id} |
| 160 | optionId={composerSuggestionOptionId(props.listId, item.id)} |
| 161 | item={item} |
| 162 | triggerKind={props.triggerKind} |
| 163 | resolvedTheme={props.resolvedTheme} |
| 164 | isActive={props.activeItemId === item.id} |
| 165 | onHighlight={props.onHighlightedItemChange} |
| 166 | onSelect={props.onSelect} |
| 167 | /> |
| 168 | ))} |
| 169 | </CommandGroup> |
| 170 | </CommandList> |
| 171 | ) : ( |
| 172 | <div className="px-5 pt-3.5 pb-7"> |
| 173 | <p className="text-secondary-label text-xs"> |
| 174 | {props.isLoading |
| 175 | ? props.triggerKind === "skill" || props.triggerKind === "slash-command" |
| 176 | ? gt("Searching workspace skills...") |
| 177 | : props.triggerKind === "pull-request" |
| 178 | ? gt("Finding pull request...") |
| 179 | : gt("Searching workspace files...") |
| 180 | : (props.emptyStateText ?? |
| 181 | (props.triggerKind === "skill" |
| 182 | ? gt("No skills found. Try / to browse provider commands.") |
| 183 | : props.triggerKind === "path" |
| 184 | ? gt("No matching files or folders.") |
| 185 | : gt("No matching command.")))} |
| 186 | </p> |
| 187 | </div> |
| 188 | )} |
| 189 | </ComposerBanner.Surface> |
| 190 | </Command> |
| 191 | ); |
| 192 | }); |
| 193 | |
| 194 | const ComposerCommandMenuItem = memo(function ComposerCommandMenuItem(props: { |
| 195 | optionId: string; |
| 196 | item: ComposerCommandItem; |
| 197 | triggerKind: ComposerTriggerKind | null; |
| 198 | resolvedTheme: "light" | "dark"; |
| 199 | isActive: boolean; |
| 200 | onHighlight: (itemId: string | null) => void; |
| 201 | onSelect: (item: ComposerCommandItem) => void; |
| 202 | }) { |
| 203 | const m = useMessages(); |
| 204 | const skillSourceKind = |
| 205 | props.item.type === "skill" ? resolveProviderSkillSourceKind(props.item.skill) : null; |
| 206 | const isSlashSkill = |
| 207 | props.triggerKind === "slash-command" && props.item.type === "skill" ? props.item.skill : null; |
| 208 | const pullRequestPresentation = |
| 209 | props.item.type === "pull-request" ? resolvePullRequestState(props.item.pullRequest, m) : null; |
| 210 | const descriptionRef = useRef<HTMLSpanElement>(null); |
| 211 | const [descriptionTruncated, setDescriptionTruncated] = useState(false); |
| 212 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- measures the rendered description so only cut-off text gets a tooltip |
| 213 | useLayoutEffect(() => { |
| 214 | const description = descriptionRef.current; |
| 215 | setDescriptionTruncated( |
| 216 | props.isActive && !!description && description.scrollWidth > description.clientWidth, |
| 217 | ); |
| 218 | }, [props.isActive]); |
| 219 | |
| 220 | return ( |
| 221 | <CommandItem |
| 222 | render={<div id={props.optionId} />} |
| 223 | aria-selected={props.isActive} |
| 224 | value={props.item.id} |
| 225 | data-composer-item-id={props.item.id} |
| 226 | active={props.isActive} |
| 227 | onMouseMove={() => { |
| 228 | if (!props.isActive) props.onHighlight(props.item.id); |
| 229 | }} |
| 230 | onMouseDown={(event) => { |
| 231 | event.preventDefault(); |
| 232 | }} |
| 233 | onClick={() => { |
| 234 | props.onSelect(props.item); |
| 235 | }} |
| 236 | > |
| 237 | {props.item.type === "path" ? ( |
| 238 | <PierreEntryIcon |
| 239 | pathValue={props.item.path} |
| 240 | kind={props.item.pathKind} |
| 241 | theme={props.resolvedTheme} |
| 242 | /> |
| 243 | ) : null} |
| 244 | {props.item.type === "thread" ? ( |
| 245 | <MessagesSquareIcon aria-hidden="true" className="size-4 shrink-0 text-secondary-label" /> |
| 246 | ) : null} |
| 247 | {props.item.type === "environment" ? ( |
| 248 | <EnvironmentMachineIcon |
| 249 | kind={props.item.machine} |
| 250 | aria-hidden="true" |
| 251 | className="size-4 shrink-0 text-secondary-label" |
| 252 | /> |
| 253 | ) : null} |
| 254 | {props.item.type === "model" ? ( |
| 255 | <ProviderInstanceIcon |
| 256 | driverKind={props.item.driverKind} |
| 257 | displayName={props.item.providerDisplayName} |
| 258 | acpRegistryAgentId={props.item.acpRegistryAgentId} |
| 259 | acpRegistryIconUrl={props.item.acpRegistryIconUrl} |
| 260 | className="size-4 shrink-0" |
| 261 | iconClassName="size-4" |
| 262 | /> |
| 263 | ) : null} |
| 264 | {pullRequestPresentation ? ( |
| 265 | <pullRequestPresentation.Icon |
| 266 | role="img" |
| 267 | aria-label={pullRequestPresentation.label} |
| 268 | className={cn("size-4 shrink-0", pullRequestPresentation.toneClassName)} |
| 269 | /> |
| 270 | ) : null} |
| 271 | <span className="flex min-w-0 flex-1 items-center gap-2"> |
| 272 | <span className="min-w-0 max-w-[45%] shrink-0 truncate font-sans text-xs font-medium"> |
| 273 | {isSlashSkill ? ( |
| 274 | <> |
| 275 | <span className="text-secondary-label">/skill:</span> |
| 276 | {formatProviderSkillDisplayName(isSlashSkill)} |
| 277 | </> |
| 278 | ) : ( |
| 279 | props.item.label |
| 280 | )} |
| 281 | </span> |
| 282 | <Tooltip open={descriptionTruncated}> |
| 283 | <TooltipTrigger |
| 284 | tabIndex={-1} |
| 285 | render={ |
| 286 | <span |
| 287 | ref={descriptionRef} |
| 288 | className="min-w-0 flex-1 truncate text-left text-secondary-label text-xs" |
| 289 | /> |
| 290 | } |
| 291 | > |
| 292 | {props.item.description} |
| 293 | </TooltipTrigger> |
| 294 | <TooltipPopup>{props.item.description}</TooltipPopup> |
| 295 | </Tooltip> |
| 296 | {skillSourceKind ? ( |
| 297 | <SkillSourceBadge |
| 298 | kind={skillSourceKind} |
| 299 | showSkillSuffix={props.triggerKind === "skill"} |
| 300 | /> |
| 301 | ) : null} |
| 302 | </span> |
| 303 | </CommandItem> |
| 304 | ); |
| 305 | }); |
| 306 | |
| 307 | export function composerSuggestionOptionId(listId: string, itemId: string): string { |
| 308 | // JSON escapes lone UTF-16 surrogates before URI encoding without losing identity. |
| 309 | return `${listId}-${encodeURIComponent(JSON.stringify(itemId))}`; |
| 310 | } |
| 311 | |
| 312 | const LISTBOX_LABEL_BY_TRIGGER: Record<ComposerTriggerKind, string> = { |
| 313 | path: msg("Files and folders"), |
| 314 | "pull-request": msg("Pull requests"), |
| 315 | "slash-command": msg("Commands"), |
| 316 | skill: msg("Skills"), |
| 317 | }; |
| 318 | |
| 319 | const SKILL_SOURCE_ICON_BY_KIND: Record<ProviderSkillSourceKind, LucideIcon> = { |
| 320 | app: BlocksIcon, |
| 321 | repo: FolderIcon, |
| 322 | project: FolderIcon, |
| 323 | personal: UserRoundIcon, |
| 324 | system: SettingsIcon, |
| 325 | other: PackageIcon, |
| 326 | }; |
| 327 | |
| 328 | const SKILL_SOURCE_LABEL_BY_KIND: Record< |
| 329 | ProviderSkillSourceKind, |
| 330 | { readonly label: string; readonly skillLabel: string } |
| 331 | > = { |
| 332 | app: { |
| 333 | label: msg("App", { $context: "skill source badge" }), |
| 334 | skillLabel: msg("App Skill"), |
| 335 | }, |
| 336 | repo: { |
| 337 | label: msg("Repo", { $context: "skill source badge" }), |
| 338 | skillLabel: msg("Repo Skill"), |
| 339 | }, |
| 340 | project: { |
| 341 | label: msg("Project", { $context: "skill source badge" }), |
| 342 | skillLabel: msg("Project Skill"), |
| 343 | }, |
| 344 | personal: { |
| 345 | label: msg("Personal", { $context: "skill source badge" }), |
| 346 | skillLabel: msg("Personal Skill"), |
| 347 | }, |
| 348 | system: { |
| 349 | label: msg("System", { $context: "skill source badge" }), |
| 350 | skillLabel: msg("System Skill"), |
| 351 | }, |
| 352 | other: { |
| 353 | label: msg("Provider", { $context: "skill source badge" }), |
| 354 | skillLabel: msg("Provider Skill"), |
| 355 | }, |
| 356 | }; |
| 357 | |
| 358 | function SkillSourceBadge(props: { kind: ProviderSkillSourceKind; showSkillSuffix: boolean }) { |
| 359 | const m = useMessages(); |
| 360 | const Icon = SKILL_SOURCE_ICON_BY_KIND[props.kind]; |
| 361 | const labels = SKILL_SOURCE_LABEL_BY_KIND[props.kind]; |
| 362 | return ( |
| 363 | <Badge className="ms-auto" variant="secondary"> |
| 364 | <Icon aria-hidden="true" className="text-current" /> |
| 365 | {m(props.showSkillSuffix ? labels.skillLabel : labels.label)} |
| 366 | </Badge> |
| 367 | ); |
| 368 | } |