@openconsole/shadcn 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/README.md +460 -380
  2. package/components/ai-elements/agent.tsx +141 -0
  3. package/components/ai-elements/artifact.tsx +148 -0
  4. package/components/ai-elements/attachments.tsx +426 -0
  5. package/components/ai-elements/audio-player.tsx +231 -0
  6. package/components/ai-elements/canvas.tsx +26 -0
  7. package/components/ai-elements/chain-of-thought.tsx +222 -0
  8. package/components/ai-elements/checkpoint.tsx +71 -0
  9. package/components/ai-elements/code-block.tsx +562 -0
  10. package/components/ai-elements/commit.tsx +458 -0
  11. package/components/ai-elements/confirmation.tsx +174 -0
  12. package/components/ai-elements/connection.tsx +28 -0
  13. package/components/ai-elements/context.tsx +409 -0
  14. package/components/ai-elements/controls.tsx +18 -0
  15. package/components/ai-elements/conversation.tsx +168 -0
  16. package/components/ai-elements/edge.tsx +143 -0
  17. package/components/ai-elements/environment-variables.tsx +324 -0
  18. package/components/ai-elements/file-tree.tsx +304 -0
  19. package/components/ai-elements/image.tsx +24 -0
  20. package/components/ai-elements/index.ts +51 -0
  21. package/components/ai-elements/inline-citation.tsx +296 -0
  22. package/components/ai-elements/jsx-preview.tsx +310 -0
  23. package/components/ai-elements/message.tsx +360 -0
  24. package/components/ai-elements/mic-selector.tsx +375 -0
  25. package/components/ai-elements/model-selector.tsx +213 -0
  26. package/components/ai-elements/node.tsx +71 -0
  27. package/components/ai-elements/open-in-chat.tsx +370 -0
  28. package/components/ai-elements/package-info.tsx +239 -0
  29. package/components/ai-elements/panel.tsx +15 -0
  30. package/components/ai-elements/persona.tsx +306 -0
  31. package/components/ai-elements/plan.tsx +147 -0
  32. package/components/ai-elements/prompt-input.tsx +1463 -0
  33. package/components/ai-elements/queue.tsx +274 -0
  34. package/components/ai-elements/reasoning.tsx +228 -0
  35. package/components/ai-elements/sandbox.tsx +132 -0
  36. package/components/ai-elements/schema-display.tsx +471 -0
  37. package/components/ai-elements/shimmer.tsx +77 -0
  38. package/components/ai-elements/snippet.tsx +145 -0
  39. package/components/ai-elements/sources.tsx +77 -0
  40. package/components/ai-elements/speech-input.tsx +323 -0
  41. package/components/ai-elements/stack-trace.tsx +528 -0
  42. package/components/ai-elements/suggestion.tsx +57 -0
  43. package/components/ai-elements/task.tsx +87 -0
  44. package/components/ai-elements/terminal.tsx +273 -0
  45. package/components/ai-elements/test-results.tsx +496 -0
  46. package/components/ai-elements/tool.tsx +173 -0
  47. package/components/ai-elements/toolbar.tsx +16 -0
  48. package/components/ai-elements/transcription.tsx +125 -0
  49. package/components/ai-elements/voice-selector.tsx +524 -0
  50. package/components/ai-elements/web-preview.tsx +281 -0
  51. package/components/index.ts +3 -0
  52. package/{accordion.tsx → components/ui/accordion.tsx} +66 -66
  53. package/{alert-dialog.tsx → components/ui/alert-dialog.tsx} +196 -196
  54. package/{alert.tsx → components/ui/alert.tsx} +66 -66
  55. package/{aspect-ratio.tsx → components/ui/aspect-ratio.tsx} +11 -11
  56. package/{avatar.tsx → components/ui/avatar.tsx} +53 -53
  57. package/{badge.tsx → components/ui/badge.tsx} +46 -46
  58. package/{breadcrumb.tsx → components/ui/breadcrumb.tsx} +109 -109
  59. package/{button-group.tsx → components/ui/button-group.tsx} +83 -83
  60. package/{button.tsx → components/ui/button.tsx} +60 -60
  61. package/{calendar.tsx → components/ui/calendar.tsx} +219 -219
  62. package/{card.tsx → components/ui/card.tsx} +92 -92
  63. package/{carousel.tsx → components/ui/carousel.tsx} +241 -241
  64. package/{chart.tsx → components/ui/chart.tsx} +374 -374
  65. package/{checkbox.tsx → components/ui/checkbox.tsx} +32 -32
  66. package/{collapsible.tsx → components/ui/collapsible.tsx} +33 -33
  67. package/{command.tsx → components/ui/command.tsx} +184 -184
  68. package/{context-menu.tsx → components/ui/context-menu.tsx} +252 -252
  69. package/{dialog.tsx → components/ui/dialog.tsx} +143 -143
  70. package/{direction.tsx → components/ui/direction.tsx} +22 -22
  71. package/{drawer.tsx → components/ui/drawer.tsx} +135 -135
  72. package/{dropdown-menu.tsx → components/ui/dropdown-menu.tsx} +257 -257
  73. package/{empty.tsx → components/ui/empty.tsx} +104 -104
  74. package/{field.tsx → components/ui/field.tsx} +248 -248
  75. package/{form.tsx → components/ui/form.tsx} +167 -167
  76. package/{hover-card.tsx → components/ui/hover-card.tsx} +44 -44
  77. package/{icon.tsx → components/ui/icon.tsx} +55 -55
  78. package/components/ui/index.ts +59 -0
  79. package/{input-group.tsx → components/ui/input-group.tsx} +170 -170
  80. package/{input-otp.tsx → components/ui/input-otp.tsx} +77 -77
  81. package/{input.tsx → components/ui/input.tsx} +21 -21
  82. package/{item.tsx → components/ui/item.tsx} +193 -193
  83. package/{kbd.tsx → components/ui/kbd.tsx} +28 -28
  84. package/{label.tsx → components/ui/label.tsx} +24 -24
  85. package/{menubar.tsx → components/ui/menubar.tsx} +276 -276
  86. package/{native-select.tsx → components/ui/native-select.tsx} +62 -62
  87. package/{navigation-menu.tsx → components/ui/navigation-menu.tsx} +168 -168
  88. package/{pagination.tsx → components/ui/pagination.tsx} +127 -127
  89. package/{popover.tsx → components/ui/popover.tsx} +89 -89
  90. package/{progress.tsx → components/ui/progress.tsx} +31 -31
  91. package/{radio-group.tsx → components/ui/radio-group.tsx} +45 -45
  92. package/{resizable.tsx → components/ui/resizable.tsx} +53 -53
  93. package/{scroll-area.tsx → components/ui/scroll-area.tsx} +58 -58
  94. package/{select.tsx → components/ui/select.tsx} +187 -187
  95. package/{separator.tsx → components/ui/separator.tsx} +28 -28
  96. package/{sheet.tsx → components/ui/sheet.tsx} +139 -139
  97. package/{sidebar.tsx → components/ui/sidebar.tsx} +724 -724
  98. package/{skeleton.tsx → components/ui/skeleton.tsx} +13 -13
  99. package/{slider.tsx → components/ui/slider.tsx} +63 -63
  100. package/{sonner.tsx → components/ui/sonner.tsx} +40 -40
  101. package/{spinner.tsx → components/ui/spinner.tsx} +16 -16
  102. package/{switch.tsx → components/ui/switch.tsx} +35 -35
  103. package/{table.tsx → components/ui/table.tsx} +116 -116
  104. package/{tabs.tsx → components/ui/tabs.tsx} +66 -66
  105. package/{textarea.tsx → components/ui/textarea.tsx} +18 -18
  106. package/{toggle-group.tsx → components/ui/toggle-group.tsx} +83 -83
  107. package/{toggle.tsx → components/ui/toggle.tsx} +47 -47
  108. package/{tooltip.tsx → components/ui/tooltip.tsx} +61 -61
  109. package/hooks/index.ts +1 -1
  110. package/hooks/use-mobile.ts +19 -19
  111. package/index.ts +3 -59
  112. package/lib/index.ts +1 -1
  113. package/lib/utils.ts +6 -6
  114. package/package.json +79 -1
  115. package/styles.css +124 -124
  116. package/tsconfig.json +0 -12
  117. package/tsconfig.tsbuildinfo +0 -1
@@ -0,0 +1,1463 @@
1
+ "use client";
2
+
3
+ import {
4
+ Command,
5
+ CommandEmpty,
6
+ CommandGroup,
7
+ CommandInput,
8
+ CommandItem,
9
+ CommandList,
10
+ CommandSeparator,
11
+ } from "../ui/command";
12
+ import {
13
+ DropdownMenu,
14
+ DropdownMenuContent,
15
+ DropdownMenuItem,
16
+ DropdownMenuTrigger,
17
+ } from "../ui/dropdown-menu";
18
+ import {
19
+ HoverCard,
20
+ HoverCardContent,
21
+ HoverCardTrigger,
22
+ } from "../ui/hover-card";
23
+ import {
24
+ InputGroup,
25
+ InputGroupAddon,
26
+ InputGroupButton,
27
+ InputGroupTextarea,
28
+ } from "../ui/input-group";
29
+ import {
30
+ Select,
31
+ SelectContent,
32
+ SelectItem,
33
+ SelectTrigger,
34
+ SelectValue,
35
+ } from "../ui/select";
36
+ import { Spinner } from "../ui/spinner";
37
+ import {
38
+ Tooltip,
39
+ TooltipContent,
40
+ TooltipTrigger,
41
+ } from "../ui/tooltip";
42
+ import { cn } from "../../lib/utils";
43
+ import type { ChatStatus, FileUIPart, SourceDocumentUIPart } from "ai";
44
+ import {
45
+ CornerDownLeftIcon,
46
+ ImageIcon,
47
+ Monitor,
48
+ PlusIcon,
49
+ SquareIcon,
50
+ XIcon,
51
+ } from "lucide-react";
52
+ import { nanoid } from "nanoid";
53
+ import type {
54
+ ChangeEvent,
55
+ ChangeEventHandler,
56
+ ClipboardEventHandler,
57
+ ComponentProps,
58
+ FormEvent,
59
+ FormEventHandler,
60
+ HTMLAttributes,
61
+ KeyboardEventHandler,
62
+ PropsWithChildren,
63
+ ReactNode,
64
+ RefObject,
65
+ } from "react";
66
+ import {
67
+ Children,
68
+ createContext,
69
+ useCallback,
70
+ useContext,
71
+ useEffect,
72
+ useMemo,
73
+ useRef,
74
+ useState,
75
+ } from "react";
76
+
77
+ // ============================================================================
78
+ // Helpers
79
+ // ============================================================================
80
+
81
+ const convertBlobUrlToDataUrl = async (url: string): Promise<string | null> => {
82
+ try {
83
+ const response = await fetch(url);
84
+ const blob = await response.blob();
85
+ // FileReader uses callback-based API, wrapping in Promise is necessary
86
+ // oxlint-disable-next-line eslint-plugin-promise(avoid-new)
87
+ return new Promise((resolve) => {
88
+ const reader = new FileReader();
89
+ // oxlint-disable-next-line eslint-plugin-unicorn(prefer-add-event-listener)
90
+ reader.onloadend = () => resolve(reader.result as string);
91
+ // oxlint-disable-next-line eslint-plugin-unicorn(prefer-add-event-listener)
92
+ reader.onerror = () => resolve(null);
93
+ reader.readAsDataURL(blob);
94
+ });
95
+ } catch {
96
+ return null;
97
+ }
98
+ };
99
+
100
+ const captureScreenshot = async (): Promise<File | null> => {
101
+ if (
102
+ typeof navigator === "undefined" ||
103
+ !navigator.mediaDevices?.getDisplayMedia
104
+ ) {
105
+ return null;
106
+ }
107
+
108
+ let stream: MediaStream | null = null;
109
+ const video = document.createElement("video");
110
+ video.muted = true;
111
+ video.playsInline = true;
112
+
113
+ try {
114
+ stream = await navigator.mediaDevices.getDisplayMedia({
115
+ audio: false,
116
+ video: true,
117
+ });
118
+
119
+ video.srcObject = stream;
120
+
121
+ // Video element uses callback-based API, wrapping in Promise is necessary
122
+ // oxlint-disable-next-line eslint-plugin-promise(avoid-new)
123
+ await new Promise<void>((resolve, reject) => {
124
+ // oxlint-disable-next-line eslint-plugin-unicorn(prefer-add-event-listener)
125
+ video.onloadedmetadata = () => resolve();
126
+ // oxlint-disable-next-line eslint-plugin-unicorn(prefer-add-event-listener)
127
+ video.onerror = () => reject(new Error("Failed to load screen stream"));
128
+ });
129
+
130
+ await video.play();
131
+
132
+ const width = video.videoWidth;
133
+ const height = video.videoHeight;
134
+ if (!width || !height) {
135
+ return null;
136
+ }
137
+
138
+ const canvas = document.createElement("canvas");
139
+ canvas.width = width;
140
+ canvas.height = height;
141
+ const context = canvas.getContext("2d");
142
+ if (!context) {
143
+ return null;
144
+ }
145
+
146
+ context.drawImage(video, 0, 0, width, height);
147
+ // canvas.toBlob uses callback-based API, wrapping in Promise is necessary
148
+ // oxlint-disable-next-line eslint-plugin-promise(avoid-new)
149
+ const blob = await new Promise<Blob | null>((resolve) => {
150
+ canvas.toBlob(resolve, "image/png");
151
+ });
152
+ if (!blob) {
153
+ return null;
154
+ }
155
+
156
+ const timestamp = new Date()
157
+ .toISOString()
158
+ .replaceAll(/[:.]/g, "-")
159
+ .replace("T", "_")
160
+ .replace("Z", "");
161
+
162
+ return new File([blob], `screenshot-${timestamp}.png`, {
163
+ lastModified: Date.now(),
164
+ type: "image/png",
165
+ });
166
+ } finally {
167
+ if (stream) {
168
+ for (const track of stream.getTracks()) {
169
+ track.stop();
170
+ }
171
+ }
172
+ video.pause();
173
+ video.srcObject = null;
174
+ }
175
+ };
176
+
177
+ // ============================================================================
178
+ // Provider Context & Types
179
+ // ============================================================================
180
+
181
+ export interface AttachmentsContext {
182
+ files: (FileUIPart & { id: string })[];
183
+ add: (files: File[] | FileList) => void;
184
+ remove: (id: string) => void;
185
+ clear: () => void;
186
+ openFileDialog: () => void;
187
+ fileInputRef: RefObject<HTMLInputElement | null>;
188
+ }
189
+
190
+ export interface TextInputContext {
191
+ value: string;
192
+ setInput: (v: string) => void;
193
+ clear: () => void;
194
+ }
195
+
196
+ export interface PromptInputControllerProps {
197
+ textInput: TextInputContext;
198
+ attachments: AttachmentsContext;
199
+ /** INTERNAL: Allows PromptInput to register its file textInput + "open" callback */
200
+ __registerFileInput: (
201
+ ref: RefObject<HTMLInputElement | null>,
202
+ open: () => void
203
+ ) => void;
204
+ }
205
+
206
+ const PromptInputController = createContext<PromptInputControllerProps | null>(
207
+ null
208
+ );
209
+ const ProviderAttachmentsContext = createContext<AttachmentsContext | null>(
210
+ null
211
+ );
212
+
213
+ export const usePromptInputController = () => {
214
+ const ctx = useContext(PromptInputController);
215
+ if (!ctx) {
216
+ throw new Error(
217
+ "Wrap your component inside <PromptInputProvider> to use usePromptInputController()."
218
+ );
219
+ }
220
+ return ctx;
221
+ };
222
+
223
+ // Optional variants (do NOT throw). Useful for dual-mode components.
224
+ const useOptionalPromptInputController = () =>
225
+ useContext(PromptInputController);
226
+
227
+ export const useProviderAttachments = () => {
228
+ const ctx = useContext(ProviderAttachmentsContext);
229
+ if (!ctx) {
230
+ throw new Error(
231
+ "Wrap your component inside <PromptInputProvider> to use useProviderAttachments()."
232
+ );
233
+ }
234
+ return ctx;
235
+ };
236
+
237
+ const useOptionalProviderAttachments = () =>
238
+ useContext(ProviderAttachmentsContext);
239
+
240
+ export type PromptInputProviderProps = PropsWithChildren<{
241
+ initialInput?: string;
242
+ }>;
243
+
244
+ /**
245
+ * Optional global provider that lifts PromptInput state outside of PromptInput.
246
+ * If you don't use it, PromptInput stays fully self-managed.
247
+ */
248
+ export const PromptInputProvider = ({
249
+ initialInput: initialTextInput = "",
250
+ children,
251
+ }: PromptInputProviderProps) => {
252
+ // ----- textInput state
253
+ const [textInput, setTextInput] = useState(initialTextInput);
254
+ const clearInput = useCallback(() => setTextInput(""), []);
255
+
256
+ // ----- attachments state (global when wrapped)
257
+ const [attachmentFiles, setAttachmentFiles] = useState<
258
+ (FileUIPart & { id: string })[]
259
+ >([]);
260
+ const fileInputRef = useRef<HTMLInputElement | null>(null);
261
+ // oxlint-disable-next-line eslint(no-empty-function)
262
+ const openRef = useRef<() => void>(() => {});
263
+
264
+ const add = useCallback((files: File[] | FileList) => {
265
+ const incoming = [...files];
266
+ if (incoming.length === 0) {
267
+ return;
268
+ }
269
+
270
+ setAttachmentFiles((prev) => [
271
+ ...prev,
272
+ ...incoming.map((file) => ({
273
+ filename: file.name,
274
+ id: nanoid(),
275
+ mediaType: file.type,
276
+ type: "file" as const,
277
+ url: URL.createObjectURL(file),
278
+ })),
279
+ ]);
280
+ }, []);
281
+
282
+ const remove = useCallback((id: string) => {
283
+ setAttachmentFiles((prev) => {
284
+ const found = prev.find((f) => f.id === id);
285
+ if (found?.url) {
286
+ URL.revokeObjectURL(found.url);
287
+ }
288
+ return prev.filter((f) => f.id !== id);
289
+ });
290
+ }, []);
291
+
292
+ const clear = useCallback(() => {
293
+ setAttachmentFiles((prev) => {
294
+ for (const f of prev) {
295
+ if (f.url) {
296
+ URL.revokeObjectURL(f.url);
297
+ }
298
+ }
299
+ return [];
300
+ });
301
+ }, []);
302
+
303
+ // Keep a ref to attachments for cleanup on unmount (avoids stale closure)
304
+ const attachmentsRef = useRef(attachmentFiles);
305
+
306
+ useEffect(() => {
307
+ attachmentsRef.current = attachmentFiles;
308
+ }, [attachmentFiles]);
309
+
310
+ // Cleanup blob URLs on unmount to prevent memory leaks
311
+ useEffect(
312
+ () => () => {
313
+ for (const f of attachmentsRef.current) {
314
+ if (f.url) {
315
+ URL.revokeObjectURL(f.url);
316
+ }
317
+ }
318
+ },
319
+ []
320
+ );
321
+
322
+ const openFileDialog = useCallback(() => {
323
+ openRef.current?.();
324
+ }, []);
325
+
326
+ const attachments = useMemo<AttachmentsContext>(
327
+ () => ({
328
+ add,
329
+ clear,
330
+ fileInputRef,
331
+ files: attachmentFiles,
332
+ openFileDialog,
333
+ remove,
334
+ }),
335
+ [attachmentFiles, add, remove, clear, openFileDialog]
336
+ );
337
+
338
+ const __registerFileInput = useCallback(
339
+ (ref: RefObject<HTMLInputElement | null>, open: () => void) => {
340
+ fileInputRef.current = ref.current;
341
+ openRef.current = open;
342
+ },
343
+ []
344
+ );
345
+
346
+ const controller = useMemo<PromptInputControllerProps>(
347
+ () => ({
348
+ __registerFileInput,
349
+ attachments,
350
+ textInput: {
351
+ clear: clearInput,
352
+ setInput: setTextInput,
353
+ value: textInput,
354
+ },
355
+ }),
356
+ [textInput, clearInput, attachments, __registerFileInput]
357
+ );
358
+
359
+ return (
360
+ <PromptInputController.Provider value={controller}>
361
+ <ProviderAttachmentsContext.Provider value={attachments}>
362
+ {children}
363
+ </ProviderAttachmentsContext.Provider>
364
+ </PromptInputController.Provider>
365
+ );
366
+ };
367
+
368
+ // ============================================================================
369
+ // Component Context & Hooks
370
+ // ============================================================================
371
+
372
+ const LocalAttachmentsContext = createContext<AttachmentsContext | null>(null);
373
+
374
+ export const usePromptInputAttachments = () => {
375
+ // Prefer local context (inside PromptInput) as it has validation, fall back to provider
376
+ const provider = useOptionalProviderAttachments();
377
+ const local = useContext(LocalAttachmentsContext);
378
+ const context = local ?? provider;
379
+ if (!context) {
380
+ throw new Error(
381
+ "usePromptInputAttachments must be used within a PromptInput or PromptInputProvider"
382
+ );
383
+ }
384
+ return context;
385
+ };
386
+
387
+ // ============================================================================
388
+ // Referenced Sources (Local to PromptInput)
389
+ // ============================================================================
390
+
391
+ export interface ReferencedSourcesContext {
392
+ sources: (SourceDocumentUIPart & { id: string })[];
393
+ add: (sources: SourceDocumentUIPart[] | SourceDocumentUIPart) => void;
394
+ remove: (id: string) => void;
395
+ clear: () => void;
396
+ }
397
+
398
+ export const LocalReferencedSourcesContext =
399
+ createContext<ReferencedSourcesContext | null>(null);
400
+
401
+ export const usePromptInputReferencedSources = () => {
402
+ const ctx = useContext(LocalReferencedSourcesContext);
403
+ if (!ctx) {
404
+ throw new Error(
405
+ "usePromptInputReferencedSources must be used within a LocalReferencedSourcesContext.Provider"
406
+ );
407
+ }
408
+ return ctx;
409
+ };
410
+
411
+ export type PromptInputActionAddAttachmentsProps = ComponentProps<
412
+ typeof DropdownMenuItem
413
+ > & {
414
+ label?: string;
415
+ };
416
+
417
+ export const PromptInputActionAddAttachments = ({
418
+ label = "Add photos or files",
419
+ ...props
420
+ }: PromptInputActionAddAttachmentsProps) => {
421
+ const attachments = usePromptInputAttachments();
422
+
423
+ const handleSelect = useCallback(
424
+ (e: Event) => {
425
+ e.preventDefault();
426
+ attachments.openFileDialog();
427
+ },
428
+ [attachments]
429
+ );
430
+
431
+ return (
432
+ <DropdownMenuItem {...props} onSelect={handleSelect}>
433
+ <ImageIcon className="mr-2 size-4" /> {label}
434
+ </DropdownMenuItem>
435
+ );
436
+ };
437
+
438
+ export type PromptInputActionAddScreenshotProps = ComponentProps<
439
+ typeof DropdownMenuItem
440
+ > & {
441
+ label?: string;
442
+ };
443
+
444
+ export const PromptInputActionAddScreenshot = ({
445
+ label = "Take screenshot",
446
+ onSelect,
447
+ ...props
448
+ }: PromptInputActionAddScreenshotProps) => {
449
+ const attachments = usePromptInputAttachments();
450
+
451
+ const handleSelect = useCallback(
452
+ async (event: Event) => {
453
+ onSelect?.(event);
454
+ if (event.defaultPrevented) {
455
+ return;
456
+ }
457
+
458
+ try {
459
+ const screenshot = await captureScreenshot();
460
+ if (screenshot) {
461
+ attachments.add([screenshot]);
462
+ }
463
+ } catch (error) {
464
+ if (
465
+ error instanceof DOMException &&
466
+ (error.name === "NotAllowedError" || error.name === "AbortError")
467
+ ) {
468
+ return;
469
+ }
470
+ throw error;
471
+ }
472
+ },
473
+ [onSelect, attachments]
474
+ );
475
+
476
+ return (
477
+ <DropdownMenuItem {...props} onSelect={handleSelect}>
478
+ <Monitor className="mr-2 size-4" />
479
+ {label}
480
+ </DropdownMenuItem>
481
+ );
482
+ };
483
+
484
+ export interface PromptInputMessage {
485
+ text: string;
486
+ files: FileUIPart[];
487
+ }
488
+
489
+ export type PromptInputProps = Omit<
490
+ HTMLAttributes<HTMLFormElement>,
491
+ "onSubmit" | "onError"
492
+ > & {
493
+ // e.g., "image/*" or leave undefined for any
494
+ accept?: string;
495
+ multiple?: boolean;
496
+ // When true, accepts drops anywhere on document. Default false (opt-in).
497
+ globalDrop?: boolean;
498
+ // Render a hidden input with given name and keep it in sync for native form posts. Default false.
499
+ syncHiddenInput?: boolean;
500
+ // Minimal constraints
501
+ maxFiles?: number;
502
+ // bytes
503
+ maxFileSize?: number;
504
+ onError?: (err: {
505
+ code: "max_files" | "max_file_size" | "accept";
506
+ message: string;
507
+ }) => void;
508
+ onSubmit: (
509
+ message: PromptInputMessage,
510
+ event: FormEvent<HTMLFormElement>
511
+ ) => void | Promise<void>;
512
+ };
513
+
514
+ export const PromptInput = ({
515
+ className,
516
+ accept,
517
+ multiple,
518
+ globalDrop,
519
+ syncHiddenInput,
520
+ maxFiles,
521
+ maxFileSize,
522
+ onError,
523
+ onSubmit,
524
+ children,
525
+ ...props
526
+ }: PromptInputProps) => {
527
+ // Try to use a provider controller if present
528
+ const controller = useOptionalPromptInputController();
529
+ const usingProvider = !!controller;
530
+
531
+ // Refs
532
+ const inputRef = useRef<HTMLInputElement | null>(null);
533
+ const formRef = useRef<HTMLFormElement | null>(null);
534
+
535
+ // ----- Local attachments (only used when no provider)
536
+ const [items, setItems] = useState<(FileUIPart & { id: string })[]>([]);
537
+ const files = usingProvider ? controller.attachments.files : items;
538
+
539
+ // ----- Local referenced sources (always local to PromptInput)
540
+ const [referencedSources, setReferencedSources] = useState<
541
+ (SourceDocumentUIPart & { id: string })[]
542
+ >([]);
543
+
544
+ // Keep a ref to files for cleanup on unmount (avoids stale closure)
545
+ const filesRef = useRef(files);
546
+
547
+ useEffect(() => {
548
+ filesRef.current = files;
549
+ }, [files]);
550
+
551
+ const openFileDialogLocal = useCallback(() => {
552
+ inputRef.current?.click();
553
+ }, []);
554
+
555
+ const matchesAccept = useCallback(
556
+ (f: File) => {
557
+ if (!accept || accept.trim() === "") {
558
+ return true;
559
+ }
560
+
561
+ const patterns = accept
562
+ .split(",")
563
+ .map((s) => s.trim())
564
+ .filter(Boolean);
565
+
566
+ return patterns.some((pattern) => {
567
+ if (pattern.endsWith("/*")) {
568
+ // e.g: image/* -> image/
569
+ const prefix = pattern.slice(0, -1);
570
+ return f.type.startsWith(prefix);
571
+ }
572
+ return f.type === pattern;
573
+ });
574
+ },
575
+ [accept]
576
+ );
577
+
578
+ const addLocal = useCallback(
579
+ (fileList: File[] | FileList) => {
580
+ const incoming = [...fileList];
581
+ const accepted = incoming.filter((f) => matchesAccept(f));
582
+ if (incoming.length && accepted.length === 0) {
583
+ onError?.({
584
+ code: "accept",
585
+ message: "No files match the accepted types.",
586
+ });
587
+ return;
588
+ }
589
+ const withinSize = (f: File) =>
590
+ maxFileSize ? f.size <= maxFileSize : true;
591
+ const sized = accepted.filter(withinSize);
592
+ if (accepted.length > 0 && sized.length === 0) {
593
+ onError?.({
594
+ code: "max_file_size",
595
+ message: "All files exceed the maximum size.",
596
+ });
597
+ return;
598
+ }
599
+
600
+ setItems((prev) => {
601
+ const capacity =
602
+ typeof maxFiles === "number"
603
+ ? Math.max(0, maxFiles - prev.length)
604
+ : undefined;
605
+ const capped =
606
+ typeof capacity === "number" ? sized.slice(0, capacity) : sized;
607
+ if (typeof capacity === "number" && sized.length > capacity) {
608
+ onError?.({
609
+ code: "max_files",
610
+ message: "Too many files. Some were not added.",
611
+ });
612
+ }
613
+ const next: (FileUIPart & { id: string })[] = [];
614
+ for (const file of capped) {
615
+ next.push({
616
+ filename: file.name,
617
+ id: nanoid(),
618
+ mediaType: file.type,
619
+ type: "file",
620
+ url: URL.createObjectURL(file),
621
+ });
622
+ }
623
+ return [...prev, ...next];
624
+ });
625
+ },
626
+ [matchesAccept, maxFiles, maxFileSize, onError]
627
+ );
628
+
629
+ const removeLocal = useCallback(
630
+ (id: string) =>
631
+ setItems((prev) => {
632
+ const found = prev.find((file) => file.id === id);
633
+ if (found?.url) {
634
+ URL.revokeObjectURL(found.url);
635
+ }
636
+ return prev.filter((file) => file.id !== id);
637
+ }),
638
+ []
639
+ );
640
+
641
+ // Wrapper that validates files before calling provider's add
642
+ const addWithProviderValidation = useCallback(
643
+ (fileList: File[] | FileList) => {
644
+ const incoming = [...fileList];
645
+ const accepted = incoming.filter((f) => matchesAccept(f));
646
+ if (incoming.length && accepted.length === 0) {
647
+ onError?.({
648
+ code: "accept",
649
+ message: "No files match the accepted types.",
650
+ });
651
+ return;
652
+ }
653
+ const withinSize = (f: File) =>
654
+ maxFileSize ? f.size <= maxFileSize : true;
655
+ const sized = accepted.filter(withinSize);
656
+ if (accepted.length > 0 && sized.length === 0) {
657
+ onError?.({
658
+ code: "max_file_size",
659
+ message: "All files exceed the maximum size.",
660
+ });
661
+ return;
662
+ }
663
+
664
+ const currentCount = files.length;
665
+ const capacity =
666
+ typeof maxFiles === "number"
667
+ ? Math.max(0, maxFiles - currentCount)
668
+ : undefined;
669
+ const capped =
670
+ typeof capacity === "number" ? sized.slice(0, capacity) : sized;
671
+ if (typeof capacity === "number" && sized.length > capacity) {
672
+ onError?.({
673
+ code: "max_files",
674
+ message: "Too many files. Some were not added.",
675
+ });
676
+ }
677
+
678
+ if (capped.length > 0) {
679
+ controller?.attachments.add(capped);
680
+ }
681
+ },
682
+ [matchesAccept, maxFileSize, maxFiles, onError, files.length, controller]
683
+ );
684
+
685
+ const clearAttachments = useCallback(
686
+ () =>
687
+ usingProvider
688
+ ? controller?.attachments.clear()
689
+ : setItems((prev) => {
690
+ for (const file of prev) {
691
+ if (file.url) {
692
+ URL.revokeObjectURL(file.url);
693
+ }
694
+ }
695
+ return [];
696
+ }),
697
+ [usingProvider, controller]
698
+ );
699
+
700
+ const clearReferencedSources = useCallback(
701
+ () => setReferencedSources([]),
702
+ []
703
+ );
704
+
705
+ const add = usingProvider ? addWithProviderValidation : addLocal;
706
+ const remove = usingProvider ? controller.attachments.remove : removeLocal;
707
+ const openFileDialog = usingProvider
708
+ ? controller.attachments.openFileDialog
709
+ : openFileDialogLocal;
710
+
711
+ const clear = useCallback(() => {
712
+ clearAttachments();
713
+ clearReferencedSources();
714
+ }, [clearAttachments, clearReferencedSources]);
715
+
716
+ // Let provider know about our hidden file input so external menus can call openFileDialog()
717
+ useEffect(() => {
718
+ if (!usingProvider) {
719
+ return;
720
+ }
721
+ controller.__registerFileInput(inputRef, () => inputRef.current?.click());
722
+ }, [usingProvider, controller]);
723
+
724
+ // Note: File input cannot be programmatically set for security reasons
725
+ // The syncHiddenInput prop is no longer functional
726
+ useEffect(() => {
727
+ if (syncHiddenInput && inputRef.current && files.length === 0) {
728
+ inputRef.current.value = "";
729
+ }
730
+ }, [files, syncHiddenInput]);
731
+
732
+ // Attach drop handlers on nearest form and document (opt-in)
733
+ useEffect(() => {
734
+ const form = formRef.current;
735
+ if (!form) {
736
+ return;
737
+ }
738
+ if (globalDrop) {
739
+ // when global drop is on, let the document-level handler own drops
740
+ return;
741
+ }
742
+
743
+ const onDragOver = (e: DragEvent) => {
744
+ if (e.dataTransfer?.types?.includes("Files")) {
745
+ e.preventDefault();
746
+ }
747
+ };
748
+ const onDrop = (e: DragEvent) => {
749
+ if (e.dataTransfer?.types?.includes("Files")) {
750
+ e.preventDefault();
751
+ }
752
+ if (e.dataTransfer?.files && e.dataTransfer.files.length > 0) {
753
+ add(e.dataTransfer.files);
754
+ }
755
+ };
756
+ form.addEventListener("dragover", onDragOver);
757
+ form.addEventListener("drop", onDrop);
758
+ return () => {
759
+ form.removeEventListener("dragover", onDragOver);
760
+ form.removeEventListener("drop", onDrop);
761
+ };
762
+ }, [add, globalDrop]);
763
+
764
+ useEffect(() => {
765
+ if (!globalDrop) {
766
+ return;
767
+ }
768
+
769
+ const onDragOver = (e: DragEvent) => {
770
+ if (e.dataTransfer?.types?.includes("Files")) {
771
+ e.preventDefault();
772
+ }
773
+ };
774
+ const onDrop = (e: DragEvent) => {
775
+ if (e.dataTransfer?.types?.includes("Files")) {
776
+ e.preventDefault();
777
+ }
778
+ if (e.dataTransfer?.files && e.dataTransfer.files.length > 0) {
779
+ add(e.dataTransfer.files);
780
+ }
781
+ };
782
+ document.addEventListener("dragover", onDragOver);
783
+ document.addEventListener("drop", onDrop);
784
+ return () => {
785
+ document.removeEventListener("dragover", onDragOver);
786
+ document.removeEventListener("drop", onDrop);
787
+ };
788
+ }, [add, globalDrop]);
789
+
790
+ useEffect(
791
+ () => () => {
792
+ if (!usingProvider) {
793
+ for (const f of filesRef.current) {
794
+ if (f.url) {
795
+ URL.revokeObjectURL(f.url);
796
+ }
797
+ }
798
+ }
799
+ },
800
+ [usingProvider]
801
+ );
802
+
803
+ const handleChange: ChangeEventHandler<HTMLInputElement> = useCallback(
804
+ (event) => {
805
+ if (event.currentTarget.files) {
806
+ add(event.currentTarget.files);
807
+ }
808
+ // Reset input value to allow selecting files that were previously removed
809
+ event.currentTarget.value = "";
810
+ },
811
+ [add]
812
+ );
813
+
814
+ const attachmentsCtx = useMemo<AttachmentsContext>(
815
+ () => ({
816
+ add,
817
+ clear: clearAttachments,
818
+ fileInputRef: inputRef,
819
+ files: files.map((item) => ({ ...item, id: item.id })),
820
+ openFileDialog,
821
+ remove,
822
+ }),
823
+ [files, add, remove, clearAttachments, openFileDialog]
824
+ );
825
+
826
+ const refsCtx = useMemo<ReferencedSourcesContext>(
827
+ () => ({
828
+ add: (incoming: SourceDocumentUIPart[] | SourceDocumentUIPart) => {
829
+ const array = Array.isArray(incoming) ? incoming : [incoming];
830
+ setReferencedSources((prev) => [
831
+ ...prev,
832
+ ...array.map((s) => ({ ...s, id: nanoid() })),
833
+ ]);
834
+ },
835
+ clear: clearReferencedSources,
836
+ remove: (id: string) => {
837
+ setReferencedSources((prev) => prev.filter((s) => s.id !== id));
838
+ },
839
+ sources: referencedSources,
840
+ }),
841
+ [referencedSources, clearReferencedSources]
842
+ );
843
+
844
+ const handleSubmit: FormEventHandler<HTMLFormElement> = useCallback(
845
+ async (event) => {
846
+ event.preventDefault();
847
+
848
+ const form = event.currentTarget;
849
+ const text = usingProvider
850
+ ? controller.textInput.value
851
+ : (() => {
852
+ const formData = new FormData(form);
853
+ return (formData.get("message") as string) || "";
854
+ })();
855
+
856
+ // Reset form immediately after capturing text to avoid race condition
857
+ // where user input during async blob conversion would be lost
858
+ if (!usingProvider) {
859
+ form.reset();
860
+ }
861
+
862
+ try {
863
+ // Convert blob URLs to data URLs asynchronously
864
+ const convertedFiles: FileUIPart[] = await Promise.all(
865
+ files.map(async ({ id: _id, ...item }) => {
866
+ if (item.url?.startsWith("blob:")) {
867
+ const dataUrl = await convertBlobUrlToDataUrl(item.url);
868
+ // If conversion failed, keep the original blob URL
869
+ return {
870
+ ...item,
871
+ url: dataUrl ?? item.url,
872
+ };
873
+ }
874
+ return item;
875
+ })
876
+ );
877
+
878
+ const result = onSubmit({ files: convertedFiles, text }, event);
879
+
880
+ // Handle both sync and async onSubmit
881
+ if (result instanceof Promise) {
882
+ try {
883
+ await result;
884
+ clear();
885
+ if (usingProvider) {
886
+ controller.textInput.clear();
887
+ }
888
+ } catch {
889
+ // Don't clear on error - user may want to retry
890
+ }
891
+ } else {
892
+ // Sync function completed without throwing, clear inputs
893
+ clear();
894
+ if (usingProvider) {
895
+ controller.textInput.clear();
896
+ }
897
+ }
898
+ } catch {
899
+ // Don't clear on error - user may want to retry
900
+ }
901
+ },
902
+ [usingProvider, controller, files, onSubmit, clear]
903
+ );
904
+
905
+ // Render with or without local provider
906
+ const inner = (
907
+ <>
908
+ <input
909
+ accept={accept}
910
+ aria-label="Upload files"
911
+ className="hidden"
912
+ multiple={multiple}
913
+ onChange={handleChange}
914
+ ref={inputRef}
915
+ title="Upload files"
916
+ type="file"
917
+ />
918
+ <form
919
+ className={cn("w-full", className)}
920
+ onSubmit={handleSubmit}
921
+ ref={formRef}
922
+ {...props}
923
+ >
924
+ <InputGroup className="overflow-hidden">{children}</InputGroup>
925
+ </form>
926
+ </>
927
+ );
928
+
929
+ const withReferencedSources = (
930
+ <LocalReferencedSourcesContext.Provider value={refsCtx}>
931
+ {inner}
932
+ </LocalReferencedSourcesContext.Provider>
933
+ );
934
+
935
+ // Always provide LocalAttachmentsContext so children get validated add function
936
+ return (
937
+ <LocalAttachmentsContext.Provider value={attachmentsCtx}>
938
+ {withReferencedSources}
939
+ </LocalAttachmentsContext.Provider>
940
+ );
941
+ };
942
+
943
+ export type PromptInputBodyProps = HTMLAttributes<HTMLDivElement>;
944
+
945
+ export const PromptInputBody = ({
946
+ className,
947
+ ...props
948
+ }: PromptInputBodyProps) => (
949
+ <div className={cn("contents", className)} {...props} />
950
+ );
951
+
952
+ export type PromptInputTextareaProps = ComponentProps<
953
+ typeof InputGroupTextarea
954
+ >;
955
+
956
+ export const PromptInputTextarea = ({
957
+ onChange,
958
+ onKeyDown,
959
+ className,
960
+ placeholder = "What would you like to know?",
961
+ ...props
962
+ }: PromptInputTextareaProps) => {
963
+ const controller = useOptionalPromptInputController();
964
+ const attachments = usePromptInputAttachments();
965
+ const [isComposing, setIsComposing] = useState(false);
966
+
967
+ const handleKeyDown: KeyboardEventHandler<HTMLTextAreaElement> = useCallback(
968
+ (e) => {
969
+ // Call the external onKeyDown handler first
970
+ onKeyDown?.(e);
971
+
972
+ // If the external handler prevented default, don't run internal logic
973
+ if (e.defaultPrevented) {
974
+ return;
975
+ }
976
+
977
+ if (e.key === "Enter") {
978
+ if (isComposing || e.nativeEvent.isComposing) {
979
+ return;
980
+ }
981
+ if (e.shiftKey) {
982
+ return;
983
+ }
984
+ e.preventDefault();
985
+
986
+ // Check if the submit button is disabled before submitting
987
+ const { form } = e.currentTarget;
988
+ const submitButton = form?.querySelector(
989
+ 'button[type="submit"]'
990
+ ) as HTMLButtonElement | null;
991
+ if (submitButton?.disabled) {
992
+ return;
993
+ }
994
+
995
+ form?.requestSubmit();
996
+ }
997
+
998
+ // Remove last attachment when Backspace is pressed and textarea is empty
999
+ if (
1000
+ e.key === "Backspace" &&
1001
+ e.currentTarget.value === "" &&
1002
+ attachments.files.length > 0
1003
+ ) {
1004
+ e.preventDefault();
1005
+ const lastAttachment = attachments.files.at(-1);
1006
+ if (lastAttachment) {
1007
+ attachments.remove(lastAttachment.id);
1008
+ }
1009
+ }
1010
+ },
1011
+ [onKeyDown, isComposing, attachments]
1012
+ );
1013
+
1014
+ const handlePaste: ClipboardEventHandler<HTMLTextAreaElement> = useCallback(
1015
+ (event) => {
1016
+ const items = event.clipboardData?.items;
1017
+
1018
+ if (!items) {
1019
+ return;
1020
+ }
1021
+
1022
+ const files: File[] = [];
1023
+
1024
+ for (const item of items) {
1025
+ if (item.kind === "file") {
1026
+ const file = item.getAsFile();
1027
+ if (file) {
1028
+ files.push(file);
1029
+ }
1030
+ }
1031
+ }
1032
+
1033
+ if (files.length > 0) {
1034
+ event.preventDefault();
1035
+ attachments.add(files);
1036
+ }
1037
+ },
1038
+ [attachments]
1039
+ );
1040
+
1041
+ const handleCompositionEnd = useCallback(() => setIsComposing(false), []);
1042
+ const handleCompositionStart = useCallback(() => setIsComposing(true), []);
1043
+
1044
+ const controlledProps = controller
1045
+ ? {
1046
+ onChange: (e: ChangeEvent<HTMLTextAreaElement>) => {
1047
+ controller.textInput.setInput(e.currentTarget.value);
1048
+ onChange?.(e);
1049
+ },
1050
+ value: controller.textInput.value,
1051
+ }
1052
+ : {
1053
+ onChange,
1054
+ };
1055
+
1056
+ return (
1057
+ <InputGroupTextarea
1058
+ className={cn("field-sizing-content max-h-48 min-h-16", className)}
1059
+ name="message"
1060
+ onCompositionEnd={handleCompositionEnd}
1061
+ onCompositionStart={handleCompositionStart}
1062
+ onKeyDown={handleKeyDown}
1063
+ onPaste={handlePaste}
1064
+ placeholder={placeholder}
1065
+ {...props}
1066
+ {...controlledProps}
1067
+ />
1068
+ );
1069
+ };
1070
+
1071
+ export type PromptInputHeaderProps = Omit<
1072
+ ComponentProps<typeof InputGroupAddon>,
1073
+ "align"
1074
+ >;
1075
+
1076
+ export const PromptInputHeader = ({
1077
+ className,
1078
+ ...props
1079
+ }: PromptInputHeaderProps) => (
1080
+ <InputGroupAddon
1081
+ align="block-end"
1082
+ className={cn("order-first flex-wrap gap-1", className)}
1083
+ {...props}
1084
+ />
1085
+ );
1086
+
1087
+ export type PromptInputFooterProps = Omit<
1088
+ ComponentProps<typeof InputGroupAddon>,
1089
+ "align"
1090
+ >;
1091
+
1092
+ export const PromptInputFooter = ({
1093
+ className,
1094
+ ...props
1095
+ }: PromptInputFooterProps) => (
1096
+ <InputGroupAddon
1097
+ align="block-end"
1098
+ className={cn("justify-between gap-1", className)}
1099
+ {...props}
1100
+ />
1101
+ );
1102
+
1103
+ export type PromptInputToolsProps = HTMLAttributes<HTMLDivElement>;
1104
+
1105
+ export const PromptInputTools = ({
1106
+ className,
1107
+ ...props
1108
+ }: PromptInputToolsProps) => (
1109
+ <div
1110
+ className={cn("flex min-w-0 items-center gap-1", className)}
1111
+ {...props}
1112
+ />
1113
+ );
1114
+
1115
+ export type PromptInputButtonTooltip =
1116
+ | string
1117
+ | {
1118
+ content: ReactNode;
1119
+ shortcut?: string;
1120
+ side?: ComponentProps<typeof TooltipContent>["side"];
1121
+ };
1122
+
1123
+ export type PromptInputButtonProps = ComponentProps<typeof InputGroupButton> & {
1124
+ tooltip?: PromptInputButtonTooltip;
1125
+ };
1126
+
1127
+ export const PromptInputButton = ({
1128
+ variant = "ghost",
1129
+ className,
1130
+ size,
1131
+ tooltip,
1132
+ ...props
1133
+ }: PromptInputButtonProps) => {
1134
+ const newSize =
1135
+ size ?? (Children.count(props.children) > 1 ? "sm" : "icon-sm");
1136
+
1137
+ const button = (
1138
+ <InputGroupButton
1139
+ className={cn(className)}
1140
+ size={newSize}
1141
+ type="button"
1142
+ variant={variant}
1143
+ {...props}
1144
+ />
1145
+ );
1146
+
1147
+ if (!tooltip) {
1148
+ return button;
1149
+ }
1150
+
1151
+ const tooltipContent =
1152
+ typeof tooltip === "string" ? tooltip : tooltip.content;
1153
+ const shortcut = typeof tooltip === "string" ? undefined : tooltip.shortcut;
1154
+ const side = typeof tooltip === "string" ? "top" : (tooltip.side ?? "top");
1155
+
1156
+ return (
1157
+ <Tooltip>
1158
+ <TooltipTrigger asChild>{button}</TooltipTrigger>
1159
+ <TooltipContent side={side}>
1160
+ {tooltipContent}
1161
+ {shortcut && (
1162
+ <span className="ml-2 text-muted-foreground">{shortcut}</span>
1163
+ )}
1164
+ </TooltipContent>
1165
+ </Tooltip>
1166
+ );
1167
+ };
1168
+
1169
+ export type PromptInputActionMenuProps = ComponentProps<typeof DropdownMenu>;
1170
+ export const PromptInputActionMenu = (props: PromptInputActionMenuProps) => (
1171
+ <DropdownMenu {...props} />
1172
+ );
1173
+
1174
+ export type PromptInputActionMenuTriggerProps = PromptInputButtonProps;
1175
+
1176
+ export const PromptInputActionMenuTrigger = ({
1177
+ className,
1178
+ children,
1179
+ ...props
1180
+ }: PromptInputActionMenuTriggerProps) => (
1181
+ <DropdownMenuTrigger asChild>
1182
+ <PromptInputButton className={className} {...props}>
1183
+ {children ?? <PlusIcon className="size-4" />}
1184
+ </PromptInputButton>
1185
+ </DropdownMenuTrigger>
1186
+ );
1187
+
1188
+ export type PromptInputActionMenuContentProps = ComponentProps<
1189
+ typeof DropdownMenuContent
1190
+ >;
1191
+ export const PromptInputActionMenuContent = ({
1192
+ className,
1193
+ ...props
1194
+ }: PromptInputActionMenuContentProps) => (
1195
+ <DropdownMenuContent align="start" className={cn(className)} {...props} />
1196
+ );
1197
+
1198
+ export type PromptInputActionMenuItemProps = ComponentProps<
1199
+ typeof DropdownMenuItem
1200
+ >;
1201
+ export const PromptInputActionMenuItem = ({
1202
+ className,
1203
+ ...props
1204
+ }: PromptInputActionMenuItemProps) => (
1205
+ <DropdownMenuItem className={cn(className)} {...props} />
1206
+ );
1207
+
1208
+ // Note: Actions that perform side-effects (like opening a file dialog)
1209
+ // are provided in opt-in modules (e.g., prompt-input-attachments).
1210
+
1211
+ export type PromptInputSubmitProps = ComponentProps<typeof InputGroupButton> & {
1212
+ status?: ChatStatus;
1213
+ onStop?: () => void;
1214
+ };
1215
+
1216
+ export const PromptInputSubmit = ({
1217
+ className,
1218
+ variant = "default",
1219
+ size = "icon-sm",
1220
+ status,
1221
+ onStop,
1222
+ onClick,
1223
+ children,
1224
+ ...props
1225
+ }: PromptInputSubmitProps) => {
1226
+ const isGenerating = status === "submitted" || status === "streaming";
1227
+
1228
+ let Icon = <CornerDownLeftIcon className="size-4" />;
1229
+
1230
+ if (status === "submitted") {
1231
+ Icon = <Spinner />;
1232
+ } else if (status === "streaming") {
1233
+ Icon = <SquareIcon className="size-4" />;
1234
+ } else if (status === "error") {
1235
+ Icon = <XIcon className="size-4" />;
1236
+ }
1237
+
1238
+ const handleClick = useCallback(
1239
+ (e: React.MouseEvent<HTMLButtonElement>) => {
1240
+ if (isGenerating && onStop) {
1241
+ e.preventDefault();
1242
+ onStop();
1243
+ return;
1244
+ }
1245
+ onClick?.(e);
1246
+ },
1247
+ [isGenerating, onStop, onClick]
1248
+ );
1249
+
1250
+ return (
1251
+ <InputGroupButton
1252
+ aria-label={isGenerating ? "Stop" : "Submit"}
1253
+ className={cn(className)}
1254
+ onClick={handleClick}
1255
+ size={size}
1256
+ type={isGenerating && onStop ? "button" : "submit"}
1257
+ variant={variant}
1258
+ {...props}
1259
+ >
1260
+ {children ?? Icon}
1261
+ </InputGroupButton>
1262
+ );
1263
+ };
1264
+
1265
+ export type PromptInputSelectProps = ComponentProps<typeof Select>;
1266
+
1267
+ export const PromptInputSelect = (props: PromptInputSelectProps) => (
1268
+ <Select {...props} />
1269
+ );
1270
+
1271
+ export type PromptInputSelectTriggerProps = ComponentProps<
1272
+ typeof SelectTrigger
1273
+ >;
1274
+
1275
+ export const PromptInputSelectTrigger = ({
1276
+ className,
1277
+ ...props
1278
+ }: PromptInputSelectTriggerProps) => (
1279
+ <SelectTrigger
1280
+ className={cn(
1281
+ "border-none bg-transparent font-medium text-muted-foreground shadow-none transition-colors",
1282
+ "hover:bg-accent hover:text-foreground aria-expanded:bg-accent aria-expanded:text-foreground",
1283
+ className
1284
+ )}
1285
+ {...props}
1286
+ />
1287
+ );
1288
+
1289
+ export type PromptInputSelectContentProps = ComponentProps<
1290
+ typeof SelectContent
1291
+ >;
1292
+
1293
+ export const PromptInputSelectContent = ({
1294
+ className,
1295
+ ...props
1296
+ }: PromptInputSelectContentProps) => (
1297
+ <SelectContent className={cn(className)} {...props} />
1298
+ );
1299
+
1300
+ export type PromptInputSelectItemProps = ComponentProps<typeof SelectItem>;
1301
+
1302
+ export const PromptInputSelectItem = ({
1303
+ className,
1304
+ ...props
1305
+ }: PromptInputSelectItemProps) => (
1306
+ <SelectItem className={cn(className)} {...props} />
1307
+ );
1308
+
1309
+ export type PromptInputSelectValueProps = ComponentProps<typeof SelectValue>;
1310
+
1311
+ export const PromptInputSelectValue = ({
1312
+ className,
1313
+ ...props
1314
+ }: PromptInputSelectValueProps) => (
1315
+ <SelectValue className={cn(className)} {...props} />
1316
+ );
1317
+
1318
+ export type PromptInputHoverCardProps = ComponentProps<typeof HoverCard>;
1319
+
1320
+ export const PromptInputHoverCard = ({
1321
+ openDelay = 0,
1322
+ closeDelay = 0,
1323
+ ...props
1324
+ }: PromptInputHoverCardProps) => (
1325
+ <HoverCard closeDelay={closeDelay} openDelay={openDelay} {...props} />
1326
+ );
1327
+
1328
+ export type PromptInputHoverCardTriggerProps = ComponentProps<
1329
+ typeof HoverCardTrigger
1330
+ >;
1331
+
1332
+ export const PromptInputHoverCardTrigger = (
1333
+ props: PromptInputHoverCardTriggerProps
1334
+ ) => <HoverCardTrigger {...props} />;
1335
+
1336
+ export type PromptInputHoverCardContentProps = ComponentProps<
1337
+ typeof HoverCardContent
1338
+ >;
1339
+
1340
+ export const PromptInputHoverCardContent = ({
1341
+ align = "start",
1342
+ ...props
1343
+ }: PromptInputHoverCardContentProps) => (
1344
+ <HoverCardContent align={align} {...props} />
1345
+ );
1346
+
1347
+ export type PromptInputTabsListProps = HTMLAttributes<HTMLDivElement>;
1348
+
1349
+ export const PromptInputTabsList = ({
1350
+ className,
1351
+ ...props
1352
+ }: PromptInputTabsListProps) => <div className={cn(className)} {...props} />;
1353
+
1354
+ export type PromptInputTabProps = HTMLAttributes<HTMLDivElement>;
1355
+
1356
+ export const PromptInputTab = ({
1357
+ className,
1358
+ ...props
1359
+ }: PromptInputTabProps) => <div className={cn(className)} {...props} />;
1360
+
1361
+ export type PromptInputTabLabelProps = HTMLAttributes<HTMLHeadingElement>;
1362
+
1363
+ export const PromptInputTabLabel = ({
1364
+ className,
1365
+ ...props
1366
+ }: PromptInputTabLabelProps) => (
1367
+ // Content provided via children in props
1368
+ // oxlint-disable-next-line eslint-plugin-jsx-a11y(heading-has-content)
1369
+ <h3
1370
+ className={cn(
1371
+ "mb-2 px-3 font-medium text-muted-foreground text-xs",
1372
+ className
1373
+ )}
1374
+ {...props}
1375
+ />
1376
+ );
1377
+
1378
+ export type PromptInputTabBodyProps = HTMLAttributes<HTMLDivElement>;
1379
+
1380
+ export const PromptInputTabBody = ({
1381
+ className,
1382
+ ...props
1383
+ }: PromptInputTabBodyProps) => (
1384
+ <div className={cn("space-y-1", className)} {...props} />
1385
+ );
1386
+
1387
+ export type PromptInputTabItemProps = HTMLAttributes<HTMLDivElement>;
1388
+
1389
+ export const PromptInputTabItem = ({
1390
+ className,
1391
+ ...props
1392
+ }: PromptInputTabItemProps) => (
1393
+ <div
1394
+ className={cn(
1395
+ "flex items-center gap-2 px-3 py-2 text-xs hover:bg-accent",
1396
+ className
1397
+ )}
1398
+ {...props}
1399
+ />
1400
+ );
1401
+
1402
+ export type PromptInputCommandProps = ComponentProps<typeof Command>;
1403
+
1404
+ export const PromptInputCommand = ({
1405
+ className,
1406
+ ...props
1407
+ }: PromptInputCommandProps) => <Command className={cn(className)} {...props} />;
1408
+
1409
+ export type PromptInputCommandInputProps = ComponentProps<typeof CommandInput>;
1410
+
1411
+ export const PromptInputCommandInput = ({
1412
+ className,
1413
+ ...props
1414
+ }: PromptInputCommandInputProps) => (
1415
+ <CommandInput className={cn(className)} {...props} />
1416
+ );
1417
+
1418
+ export type PromptInputCommandListProps = ComponentProps<typeof CommandList>;
1419
+
1420
+ export const PromptInputCommandList = ({
1421
+ className,
1422
+ ...props
1423
+ }: PromptInputCommandListProps) => (
1424
+ <CommandList className={cn(className)} {...props} />
1425
+ );
1426
+
1427
+ export type PromptInputCommandEmptyProps = ComponentProps<typeof CommandEmpty>;
1428
+
1429
+ export const PromptInputCommandEmpty = ({
1430
+ className,
1431
+ ...props
1432
+ }: PromptInputCommandEmptyProps) => (
1433
+ <CommandEmpty className={cn(className)} {...props} />
1434
+ );
1435
+
1436
+ export type PromptInputCommandGroupProps = ComponentProps<typeof CommandGroup>;
1437
+
1438
+ export const PromptInputCommandGroup = ({
1439
+ className,
1440
+ ...props
1441
+ }: PromptInputCommandGroupProps) => (
1442
+ <CommandGroup className={cn(className)} {...props} />
1443
+ );
1444
+
1445
+ export type PromptInputCommandItemProps = ComponentProps<typeof CommandItem>;
1446
+
1447
+ export const PromptInputCommandItem = ({
1448
+ className,
1449
+ ...props
1450
+ }: PromptInputCommandItemProps) => (
1451
+ <CommandItem className={cn(className)} {...props} />
1452
+ );
1453
+
1454
+ export type PromptInputCommandSeparatorProps = ComponentProps<
1455
+ typeof CommandSeparator
1456
+ >;
1457
+
1458
+ export const PromptInputCommandSeparator = ({
1459
+ className,
1460
+ ...props
1461
+ }: PromptInputCommandSeparatorProps) => (
1462
+ <CommandSeparator className={cn(className)} {...props} />
1463
+ );