prompt-input-cursor.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Derived from vercel/ai-elements (skills/ai-elements/scripts/prompt-input-cursor.tsx).
  3. * Copyright 2023 Vercel, Inc. Licensed under Apache-2.0.
  4. * Modified by ZCode: local integration, formatting and adaptations.
  5. * See THIRD-PARTY-NOTICES.md in the repository root for license and provenance.
  6. */
  7. "use client";
  8. import type { AttachmentData } from "@/components/ai-elements/attachments";
  9. import {
  10. Attachment,
  11. AttachmentInfo,
  12. AttachmentPreview,
  13. AttachmentRemove,
  14. Attachments,
  15. } from "@/components/ai-elements/attachments";
  16. import {
  17. ModelSelector,
  18. ModelSelectorContent,
  19. ModelSelectorEmpty,
  20. ModelSelectorGroup,
  21. ModelSelectorInput,
  22. ModelSelectorItem,
  23. ModelSelectorList,
  24. ModelSelectorLogo,
  25. ModelSelectorLogoGroup,
  26. ModelSelectorName,
  27. ModelSelectorTrigger,
  28. } from "@/components/ai-elements/model-selector";
  29. import type { PromptInputMessage } from "@/components/ai-elements/prompt-input";
  30. import {
  31. PromptInput,
  32. PromptInputBody,
  33. PromptInputButton,
  34. PromptInputCommand,
  35. PromptInputCommandEmpty,
  36. PromptInputCommandGroup,
  37. PromptInputCommandInput,
  38. PromptInputCommandItem,
  39. PromptInputCommandList,
  40. PromptInputCommandSeparator,
  41. PromptInputFooter,
  42. PromptInputHeader,
  43. PromptInputHoverCard,
  44. PromptInputHoverCardContent,
  45. PromptInputHoverCardTrigger,
  46. PromptInputProvider,
  47. PromptInputSubmit,
  48. PromptInputTab,
  49. PromptInputTabBody,
  50. PromptInputTabItem,
  51. PromptInputTabLabel,
  52. PromptInputTextarea,
  53. PromptInputTools,
  54. usePromptInputAttachments,
  55. usePromptInputReferencedSources,
  56. } from "@/components/ai-elements/prompt-input";
  57. import { Button } from "@/components/ui/button";
  58. import type { SourceDocumentUIPart } from "ai";
  59. import { AtSignIcon, CheckIcon, FilesIcon, GlobeIcon, ImageIcon, RulerIcon } from "lucide-react";
  60. import { memo, useCallback, useState } from "react";
  61. const models = [
  62. {
  63. chef: "OpenAI",
  64. chefSlug: "openai",
  65. id: "gpt-4o",
  66. name: "GPT-4o",
  67. providers: ["openai", "azure"],
  68. },
  69. {
  70. chef: "OpenAI",
  71. chefSlug: "openai",
  72. id: "gpt-4o-mini",
  73. name: "GPT-4o Mini",
  74. providers: ["openai", "azure"],
  75. },
  76. {
  77. chef: "Anthropic",
  78. chefSlug: "anthropic",
  79. id: "claude-opus-4-20250514",
  80. name: "Claude 4 Opus",
  81. providers: ["anthropic", "azure", "google", "amazon-bedrock"],
  82. },
  83. {
  84. chef: "Anthropic",
  85. chefSlug: "anthropic",
  86. id: "claude-sonnet-4-20250514",
  87. name: "Claude 4 Sonnet",
  88. providers: ["anthropic", "azure", "google", "amazon-bedrock"],
  89. },
  90. {
  91. chef: "Google",
  92. chefSlug: "google",
  93. id: "gemini-2.0-flash-exp",
  94. name: "Gemini 2.0 Flash",
  95. providers: ["google"],
  96. },
  97. ];
  98. const SUBMITTING_TIMEOUT = 200;
  99. const STREAMING_TIMEOUT = 2000;
  100. interface AttachmentItemProps {
  101. attachment: AttachmentData;
  102. onRemove: (id: string) => void;
  103. }
  104. const AttachmentItem = memo(({ attachment, onRemove }: AttachmentItemProps) => {
  105. const handleRemove = useCallback(() => onRemove(attachment.id), [onRemove, attachment.id]);
  106. return (
  107. <Attachment data={attachment} key={attachment.id} onRemove={handleRemove}>
  108. <AttachmentPreview />
  109. <AttachmentRemove />
  110. </Attachment>
  111. );
  112. });
  113. AttachmentItem.displayName = "AttachmentItem";
  114. interface SourceItemProps {
  115. source: AttachmentData;
  116. onRemove: (id: string) => void;
  117. }
  118. const SourceItem = memo(({ source, onRemove }: SourceItemProps) => {
  119. const handleRemove = useCallback(() => onRemove(source.id), [onRemove, source.id]);
  120. return (
  121. <Attachment data={source} key={source.id} onRemove={handleRemove}>
  122. <AttachmentPreview />
  123. <AttachmentInfo />
  124. <AttachmentRemove />
  125. </Attachment>
  126. );
  127. });
  128. SourceItem.displayName = "SourceItem";
  129. interface ModelItemProps {
  130. m: (typeof models)[0];
  131. selectedModel: string;
  132. onSelect: (id: string) => void;
  133. }
  134. const ModelItem = memo(({ m, selectedModel, onSelect }: ModelItemProps) => {
  135. const handleSelect = useCallback(() => onSelect(m.id), [onSelect, m.id]);
  136. return (
  137. <ModelSelectorItem key={m.id} onSelect={handleSelect} value={m.id}>
  138. <ModelSelectorLogo provider={m.chefSlug} />
  139. <ModelSelectorName>{m.name}</ModelSelectorName>
  140. <ModelSelectorLogoGroup>
  141. {m.providers.map((provider) => (
  142. <ModelSelectorLogo key={provider} provider={provider} />
  143. ))}
  144. </ModelSelectorLogoGroup>
  145. {selectedModel === m.id ? (
  146. <CheckIcon className="ml-auto size-4" />
  147. ) : (
  148. <div className="ml-auto size-4" />
  149. )}
  150. </ModelSelectorItem>
  151. );
  152. });
  153. ModelItem.displayName = "ModelItem";
  154. interface SourceCommandItemProps {
  155. source: SourceDocumentUIPart;
  156. onAdd: (source: SourceDocumentUIPart) => void;
  157. }
  158. const SourceCommandItem = memo(({ source, onAdd }: SourceCommandItemProps) => {
  159. const handleSelect = useCallback(() => onAdd(source), [onAdd, source]);
  160. return (
  161. <PromptInputCommandItem key={`${source.filename}-${source.title}`} onSelect={handleSelect}>
  162. <GlobeIcon className="text-primary" />
  163. <div className="flex flex-col">
  164. <span className="font-medium text-sm">{source.title}</span>
  165. <span className="text-muted-foreground text-xs">{source.filename}</span>
  166. </div>
  167. </PromptInputCommandItem>
  168. );
  169. });
  170. SourceCommandItem.displayName = "SourceCommandItem";
  171. const sampleSources: SourceDocumentUIPart[] = [
  172. {
  173. filename: "packages/elements/src",
  174. mediaType: "text/plain",
  175. sourceId: "1",
  176. title: "prompt-input.tsx",
  177. type: "source-document",
  178. },
  179. {
  180. filename: "apps/test/app/examples",
  181. mediaType: "text/plain",
  182. sourceId: "2",
  183. title: "queue.tsx",
  184. type: "source-document",
  185. },
  186. {
  187. filename: "packages/elements/src",
  188. mediaType: "text/plain",
  189. sourceId: "3",
  190. title: "queue.tsx",
  191. type: "source-document",
  192. },
  193. ];
  194. const sampleTabs = {
  195. active: [{ path: "packages/elements/src/task-queue-panel.tsx" }],
  196. recents: [
  197. { path: "apps/test/app/examples/task-queue-panel.tsx" },
  198. { path: "apps/test/app/page.tsx" },
  199. { path: "packages/elements/src/task.tsx" },
  200. { path: "apps/test/app/examples/prompt-input.tsx" },
  201. { path: "packages/elements/src/queue.tsx" },
  202. { path: "apps/test/app/examples/queue.tsx" },
  203. ],
  204. };
  205. const PromptInputAttachmentsDisplay = () => {
  206. const attachments = usePromptInputAttachments();
  207. const handleRemove = useCallback((id: string) => attachments.remove(id), [attachments]);
  208. if (attachments.files.length === 0) {
  209. return null;
  210. }
  211. return (
  212. <Attachments variant="inline">
  213. {attachments.files.map((attachment) => (
  214. <AttachmentItem attachment={attachment} key={attachment.id} onRemove={handleRemove} />
  215. ))}
  216. </Attachments>
  217. );
  218. };
  219. const PromptInputReferencedSourcesDisplay = () => {
  220. const refs = usePromptInputReferencedSources();
  221. const handleRemove = useCallback((id: string) => refs.remove(id), [refs]);
  222. if (refs.sources.length === 0) {
  223. return null;
  224. }
  225. return (
  226. <Attachments variant="inline">
  227. {refs.sources.map((source) => (
  228. <SourceItem key={source.id} onRemove={handleRemove} source={source as AttachmentData} />
  229. ))}
  230. </Attachments>
  231. );
  232. };
  233. const SampleFilesMenu = () => {
  234. const refs = usePromptInputReferencedSources();
  235. const handleAdd = useCallback((source: SourceDocumentUIPart) => refs.add(source), [refs]);
  236. return (
  237. <PromptInputCommand>
  238. <PromptInputCommandInput
  239. className="border-none focus-visible:ring-0"
  240. placeholder="Add files, folders, docs..."
  241. />
  242. <PromptInputCommandList>
  243. <PromptInputCommandEmpty className="p-3 text-muted-foreground text-sm">
  244. No results found.
  245. </PromptInputCommandEmpty>
  246. <PromptInputCommandGroup heading="Added">
  247. <PromptInputCommandItem>
  248. <GlobeIcon />
  249. <span>Active Tabs</span>
  250. <span className="ml-auto text-muted-foreground">✓</span>
  251. </PromptInputCommandItem>
  252. </PromptInputCommandGroup>
  253. <PromptInputCommandSeparator />
  254. <PromptInputCommandGroup heading="Other Files">
  255. {sampleSources
  256. .filter(
  257. (source) =>
  258. !refs.sources.some(
  259. (s) => s.title === source.title && s.filename === source.filename,
  260. ),
  261. )
  262. .map((source) => (
  263. <SourceCommandItem
  264. key={`${source.filename}-${source.title}`}
  265. onAdd={handleAdd}
  266. source={source}
  267. />
  268. ))}
  269. </PromptInputCommandGroup>
  270. </PromptInputCommandList>
  271. </PromptInputCommand>
  272. );
  273. };
  274. const Example = () => {
  275. const [model, setModel] = useState<string>(models[0].id);
  276. const [modelSelectorOpen, setModelSelectorOpen] = useState(false);
  277. const [status, setStatus] = useState<"submitted" | "streaming" | "ready" | "error">("ready");
  278. const selectedModelData = models.find((m) => m.id === model);
  279. const handleModelSelect = useCallback((id: string) => {
  280. setModel(id);
  281. setModelSelectorOpen(false);
  282. }, []);
  283. const handleSubmit = useCallback((message: PromptInputMessage) => {
  284. const hasText = Boolean(message.text);
  285. const hasAttachments = Boolean(message.files?.length);
  286. if (!(hasText || hasAttachments)) {
  287. return;
  288. }
  289. setStatus("submitted");
  290. setTimeout(() => {
  291. setStatus("streaming");
  292. }, SUBMITTING_TIMEOUT);
  293. setTimeout(() => {
  294. setStatus("ready");
  295. }, STREAMING_TIMEOUT);
  296. }, []);
  297. return (
  298. <div className="flex size-full flex-col justify-end">
  299. <PromptInputProvider>
  300. <PromptInput globalDrop multiple onSubmit={handleSubmit}>
  301. <PromptInputHeader>
  302. <PromptInputHoverCard>
  303. <PromptInputHoverCardTrigger>
  304. <PromptInputButton className="h-8!" size="icon-sm" variant="outline">
  305. <AtSignIcon className="text-muted-foreground" size={12} />
  306. </PromptInputButton>
  307. </PromptInputHoverCardTrigger>
  308. <PromptInputHoverCardContent className="w-[400px] p-0">
  309. <SampleFilesMenu />
  310. </PromptInputHoverCardContent>
  311. </PromptInputHoverCard>
  312. <PromptInputHoverCard>
  313. <PromptInputHoverCardTrigger>
  314. <PromptInputButton size="sm" variant="outline">
  315. <RulerIcon className="text-muted-foreground" size={12} />
  316. <span>1</span>
  317. </PromptInputButton>
  318. </PromptInputHoverCardTrigger>
  319. <PromptInputHoverCardContent className="divide-y overflow-hidden p-0">
  320. <div className="space-y-2 p-3">
  321. <p className="font-medium text-muted-foreground text-sm">
  322. Attached Project Rules
  323. </p>
  324. <p className="ml-4 text-muted-foreground text-sm">Always Apply:</p>
  325. <p className="ml-8 text-sm">ultracite.mdc</p>
  326. </div>
  327. <p className="bg-sidebar px-4 py-3 text-muted-foreground text-sm">
  328. Click to manage
  329. </p>
  330. </PromptInputHoverCardContent>
  331. </PromptInputHoverCard>
  332. <PromptInputHoverCard>
  333. <PromptInputHoverCardTrigger>
  334. <PromptInputButton size="sm" variant="outline">
  335. <FilesIcon className="text-muted-foreground" size={12} />
  336. <span>1 Tab</span>
  337. </PromptInputButton>
  338. </PromptInputHoverCardTrigger>
  339. <PromptInputHoverCardContent className="w-[300px] space-y-4 px-0 py-3">
  340. <PromptInputTab>
  341. <PromptInputTabLabel>Active Tabs</PromptInputTabLabel>
  342. <PromptInputTabBody>
  343. {sampleTabs.active.map((tab) => (
  344. <PromptInputTabItem key={tab.path}>
  345. <GlobeIcon className="text-primary" size={16} />
  346. <span className="truncate" dir="rtl">
  347. {tab.path}
  348. </span>
  349. </PromptInputTabItem>
  350. ))}
  351. </PromptInputTabBody>
  352. </PromptInputTab>
  353. <PromptInputTab>
  354. <PromptInputTabLabel>Recents</PromptInputTabLabel>
  355. <PromptInputTabBody>
  356. {sampleTabs.recents.map((tab) => (
  357. <PromptInputTabItem key={tab.path}>
  358. <GlobeIcon className="text-primary" size={16} />
  359. <span className="truncate" dir="rtl">
  360. {tab.path}
  361. </span>
  362. </PromptInputTabItem>
  363. ))}
  364. </PromptInputTabBody>
  365. </PromptInputTab>
  366. <div className="border-t px-3 pt-2 text-muted-foreground text-xs">
  367. Only file paths are included
  368. </div>
  369. </PromptInputHoverCardContent>
  370. </PromptInputHoverCard>
  371. <PromptInputAttachmentsDisplay />
  372. <PromptInputReferencedSourcesDisplay />
  373. </PromptInputHeader>
  374. <PromptInputBody>
  375. <PromptInputTextarea placeholder="Plan, search, build anything" />
  376. </PromptInputBody>
  377. <PromptInputFooter>
  378. <PromptInputTools>
  379. <ModelSelector onOpenChange={setModelSelectorOpen} open={modelSelectorOpen}>
  380. <ModelSelectorTrigger asChild>
  381. <PromptInputButton>
  382. {selectedModelData?.chefSlug && (
  383. <ModelSelectorLogo provider={selectedModelData.chefSlug} />
  384. )}
  385. {selectedModelData?.name && (
  386. <ModelSelectorName>{selectedModelData.name}</ModelSelectorName>
  387. )}
  388. </PromptInputButton>
  389. </ModelSelectorTrigger>
  390. <ModelSelectorContent>
  391. <ModelSelectorInput placeholder="Search models..." />
  392. <ModelSelectorList>
  393. <ModelSelectorEmpty>No models found.</ModelSelectorEmpty>
  394. {["OpenAI", "Anthropic", "Google"].map((chef) => (
  395. <ModelSelectorGroup heading={chef} key={chef}>
  396. {models
  397. .filter((m) => m.chef === chef)
  398. .map((m) => (
  399. <ModelItem
  400. key={m.id}
  401. m={m}
  402. onSelect={handleModelSelect}
  403. selectedModel={model}
  404. />
  405. ))}
  406. </ModelSelectorGroup>
  407. ))}
  408. </ModelSelectorList>
  409. </ModelSelectorContent>
  410. </ModelSelector>
  411. </PromptInputTools>
  412. <div className="flex items-center gap-2">
  413. <Button size="icon-sm" variant="ghost">
  414. <ImageIcon className="text-muted-foreground" size={16} />
  415. </Button>
  416. <PromptInputSubmit className="!h-8" status={status} />
  417. </div>
  418. </PromptInputFooter>
  419. </PromptInput>
  420. </PromptInputProvider>
  421. </div>
  422. );
  423. };
  424. export default Example;