ai-design-system 0.1.0

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 (290) hide show
  1. package/README.md +307 -0
  2. package/components/ai-elements/actions.tsx +65 -0
  3. package/components/ai-elements/artifact.tsx +147 -0
  4. package/components/ai-elements/branch.tsx +212 -0
  5. package/components/ai-elements/canvas.tsx +24 -0
  6. package/components/ai-elements/chain-of-thought.tsx +228 -0
  7. package/components/ai-elements/code-block.tsx +179 -0
  8. package/components/ai-elements/confirmation.tsx +169 -0
  9. package/components/ai-elements/connection.tsx +28 -0
  10. package/components/ai-elements/context.tsx +408 -0
  11. package/components/ai-elements/controls.tsx +18 -0
  12. package/components/ai-elements/conversation.tsx +97 -0
  13. package/components/ai-elements/edge.tsx +140 -0
  14. package/components/ai-elements/image.tsx +24 -0
  15. package/components/ai-elements/inline-citation.tsx +287 -0
  16. package/components/ai-elements/loader.tsx +96 -0
  17. package/components/ai-elements/message.tsx +80 -0
  18. package/components/ai-elements/node.tsx +71 -0
  19. package/components/ai-elements/open-in-chat.tsx +363 -0
  20. package/components/ai-elements/panel.tsx +15 -0
  21. package/components/ai-elements/plan.tsx +142 -0
  22. package/components/ai-elements/prompt-input.tsx +1352 -0
  23. package/components/ai-elements/queue.tsx +274 -0
  24. package/components/ai-elements/reasoning.tsx +178 -0
  25. package/components/ai-elements/response.tsx +22 -0
  26. package/components/ai-elements/shimmer.tsx +64 -0
  27. package/components/ai-elements/sources.tsx +77 -0
  28. package/components/ai-elements/suggestion.tsx +56 -0
  29. package/components/ai-elements/task.tsx +87 -0
  30. package/components/ai-elements/tool.tsx +179 -0
  31. package/components/ai-elements/toolbar.tsx +16 -0
  32. package/components/ai-elements/web-preview.tsx +263 -0
  33. package/components/blocks/AIConversation/AIConversation.stories.tsx +164 -0
  34. package/components/blocks/AIConversation/AIConversation.tsx +186 -0
  35. package/components/blocks/AIConversation/index.ts +8 -0
  36. package/components/blocks/AppSidebar/AppSidebar.stories.tsx +63 -0
  37. package/components/blocks/AppSidebar/AppSidebar.tsx +87 -0
  38. package/components/blocks/AppSidebar/index.ts +2 -0
  39. package/components/blocks/DocumentEditorWithComments/DocumentEditorWithComments.stories.tsx +341 -0
  40. package/components/blocks/DocumentEditorWithComments/DocumentEditorWithComments.tsx +255 -0
  41. package/components/blocks/DocumentEditorWithComments/index.ts +9 -0
  42. package/components/blocks/FileChangeQueue/FileChangeQueue.stories.tsx +207 -0
  43. package/components/blocks/FileChangeQueue/FileChangeQueue.tsx +143 -0
  44. package/components/blocks/FileChangeQueue/index.ts +7 -0
  45. package/components/blocks/LayoutProvider/LayoutProvider.tsx +34 -0
  46. package/components/blocks/LayoutProvider/index.ts +1 -0
  47. package/components/blocks/index.ts +2 -0
  48. package/components/composites/AgentIndicator/AgentIndicator.stories.tsx +154 -0
  49. package/components/composites/AgentIndicator/AgentIndicator.tsx +102 -0
  50. package/components/composites/AgentIndicator/index.ts +8 -0
  51. package/components/composites/AppHeader/AppHeader.stories.tsx +46 -0
  52. package/components/composites/AppHeader/AppHeader.tsx +24 -0
  53. package/components/composites/AppHeader/index.ts +2 -0
  54. package/components/composites/CommentBox/CommentBox.stories.tsx +192 -0
  55. package/components/composites/CommentBox/CommentBox.tsx +364 -0
  56. package/components/composites/CommentBox/index.ts +8 -0
  57. package/components/composites/Confirmation/Confirmation.stories.tsx +151 -0
  58. package/components/composites/Confirmation/Confirmation.tsx +93 -0
  59. package/components/composites/Confirmation/index.ts +7 -0
  60. package/components/composites/DataTable/DataTable.stories.tsx +35 -0
  61. package/components/composites/DataTable/DataTable.tsx +95 -0
  62. package/components/composites/DataTable/index.ts +2 -0
  63. package/components/composites/DocumentEditor/DocumentEditor.css +106 -0
  64. package/components/composites/DocumentEditor/DocumentEditor.stories.tsx +927 -0
  65. package/components/composites/DocumentEditor/DocumentEditor.tsx +279 -0
  66. package/components/composites/DocumentEditor/index.ts +8 -0
  67. package/components/composites/FileQueue/FileQueue.stories.tsx +175 -0
  68. package/components/composites/FileQueue/FileQueue.tsx +161 -0
  69. package/components/composites/FileQueue/FileStatusBadge.tsx +74 -0
  70. package/components/composites/FileQueue/index.ts +24 -0
  71. package/components/composites/InteractiveChart/InteractiveChart.stories.tsx +49 -0
  72. package/components/composites/InteractiveChart/InteractiveChart.tsx +69 -0
  73. package/components/composites/InteractiveChart/index.ts +2 -0
  74. package/components/composites/ModeToggle/ModeToggle.stories.tsx +212 -0
  75. package/components/composites/ModeToggle/ModeToggle.tsx +100 -0
  76. package/components/composites/ModeToggle/index.ts +7 -0
  77. package/components/composites/NavUser/NavUser.stories.tsx +50 -0
  78. package/components/composites/NavUser/NavUser.tsx +60 -0
  79. package/components/composites/NavUser/index.ts +2 -0
  80. package/components/composites/NavigationList/NavigationList.stories.tsx +46 -0
  81. package/components/composites/NavigationList/NavigationList.tsx +46 -0
  82. package/components/composites/NavigationList/index.ts +2 -0
  83. package/components/composites/OrchestratorMessage/OrchestratorMessage.stories.tsx +188 -0
  84. package/components/composites/OrchestratorMessage/OrchestratorMessage.tsx +72 -0
  85. package/components/composites/OrchestratorMessage/index.ts +8 -0
  86. package/components/composites/PageContainer/PageContainer.stories.tsx +41 -0
  87. package/components/composites/PageContainer/PageContainer.tsx +24 -0
  88. package/components/composites/PageContainer/index.ts +2 -0
  89. package/components/composites/PromptInput/PromptInput.stories.tsx +200 -0
  90. package/components/composites/PromptInput/PromptInput.tsx +129 -0
  91. package/components/composites/PromptInput/index.ts +8 -0
  92. package/components/composites/SpecialistMessage/SpecialistMessage.stories.tsx +286 -0
  93. package/components/composites/SpecialistMessage/SpecialistMessage.tsx +107 -0
  94. package/components/composites/SpecialistMessage/index.ts +8 -0
  95. package/components/composites/StatsCard/StatsCard.stories.tsx +76 -0
  96. package/components/composites/StatsCard/StatsCard.tsx +81 -0
  97. package/components/composites/StatsCard/index.ts +2 -0
  98. package/components/composites/TablePagination/TablePagination.stories.tsx +38 -0
  99. package/components/composites/TablePagination/TablePagination.tsx +119 -0
  100. package/components/composites/TablePagination/index.ts +2 -0
  101. package/components/composites/TableToolbar/TableToolbar.stories.tsx +60 -0
  102. package/components/composites/TableToolbar/TableToolbar.tsx +66 -0
  103. package/components/composites/TableToolbar/index.ts +2 -0
  104. package/components/composites/ThemeSelector/ThemeSelector.stories.tsx +48 -0
  105. package/components/composites/ThemeSelector/ThemeSelector.tsx +79 -0
  106. package/components/composites/ThemeSelector/index.ts +2 -0
  107. package/components/composites/ToolCallDisplay/ToolCallDisplay.stories.tsx +49 -0
  108. package/components/composites/ToolCallDisplay/ToolCallDisplay.tsx +108 -0
  109. package/components/composites/ToolCallDisplay/index.ts +8 -0
  110. package/components/composites/UserMessage/UserMessage.stories.tsx +59 -0
  111. package/components/composites/UserMessage/UserMessage.tsx +52 -0
  112. package/components/composites/UserMessage/index.ts +8 -0
  113. package/components/composites/index.ts +90 -0
  114. package/components/features/AIDocEditor/AIDocEditor.behaviors.stories.tsx +451 -0
  115. package/components/features/AIDocEditor/AIDocEditor.mocks.ts +96 -0
  116. package/components/features/AIDocEditor/AIDocEditor.stories.tsx +140 -0
  117. package/components/features/AIDocEditor/AIDocEditor.tsx +130 -0
  118. package/components/features/AIDocEditor/index.ts +8 -0
  119. package/components/features/AIDocEditor/useAIDocEditor.d.ts +97 -0
  120. package/components/features/AIDocEditor/useAIDocEditor.mock.ts +83 -0
  121. package/components/features/PageLayout/PageLayout.behaviors.stories.tsx +119 -0
  122. package/components/features/PageLayout/PageLayout.mocks.ts +27 -0
  123. package/components/features/PageLayout/PageLayout.stories.tsx +142 -0
  124. package/components/features/PageLayout/PageLayout.tsx +94 -0
  125. package/components/features/PageLayout/index.ts +4 -0
  126. package/components/features/PageLayout/usePageLayout.d.ts +24 -0
  127. package/components/features/PageLayout/usePageLayout.mock.ts +19 -0
  128. package/components/features/RefinementPanel/README.md +189 -0
  129. package/components/features/RefinementPanel/RefinementPanel.behaviors.stories.tsx +475 -0
  130. package/components/features/RefinementPanel/RefinementPanel.mocks.ts +131 -0
  131. package/components/features/RefinementPanel/RefinementPanel.stories.tsx +141 -0
  132. package/components/features/RefinementPanel/RefinementPanel.tsx +160 -0
  133. package/components/features/RefinementPanel/index.ts +25 -0
  134. package/components/features/RefinementPanel/useRefinementPanel.d.ts +74 -0
  135. package/components/features/RefinementPanel/useRefinementPanel.mock.ts +121 -0
  136. package/components/features/SpecNavigator/SpecNavigator.behaviors.stories.tsx +379 -0
  137. package/components/features/SpecNavigator/SpecNavigator.mocks.ts +131 -0
  138. package/components/features/SpecNavigator/SpecNavigator.stories.tsx +122 -0
  139. package/components/features/SpecNavigator/SpecNavigator.tsx +43 -0
  140. package/components/features/SpecNavigator/index.ts +2 -0
  141. package/components/features/SpecNavigator/useSpecNavigator.d.ts +122 -0
  142. package/components/features/SpecNavigator/useSpecNavigator.mock.ts +93 -0
  143. package/components/features/index.ts +18 -0
  144. package/components/index.ts +14 -0
  145. package/components/primitives/Accordion/Accordion.stories.tsx +87 -0
  146. package/components/primitives/Accordion/Accordion.tsx +66 -0
  147. package/components/primitives/Accordion/index.ts +13 -0
  148. package/components/primitives/Alert/Alert.stories.tsx +422 -0
  149. package/components/primitives/Alert/Alert.tsx +61 -0
  150. package/components/primitives/Alert/index.ts +8 -0
  151. package/components/primitives/AlertDialog/AlertDialog.stories.tsx +367 -0
  152. package/components/primitives/AlertDialog/AlertDialog.tsx +182 -0
  153. package/components/primitives/AlertDialog/index.ts +25 -0
  154. package/components/primitives/Avatar/Avatar.stories.tsx +321 -0
  155. package/components/primitives/Avatar/Avatar.tsx +63 -0
  156. package/components/primitives/Avatar/index.ts +8 -0
  157. package/components/primitives/Badge/Badge.stories.tsx +74 -0
  158. package/components/primitives/Badge/Badge.tsx +49 -0
  159. package/components/primitives/Badge/index.ts +2 -0
  160. package/components/primitives/Button/Button.stories.tsx +445 -0
  161. package/components/primitives/Button/Button.tsx +89 -0
  162. package/components/primitives/Button/index.ts +7 -0
  163. package/components/primitives/Card/Card.stories.tsx +831 -0
  164. package/components/primitives/Card/Card.tsx +242 -0
  165. package/components/primitives/Card/index.ts +30 -0
  166. package/components/primitives/Carousel/Carousel.stories.tsx +32 -0
  167. package/components/primitives/Carousel/Carousel.tsx +63 -0
  168. package/components/primitives/Carousel/index.ts +13 -0
  169. package/components/primitives/Chart/Chart.stories.tsx +346 -0
  170. package/components/primitives/Chart/Chart.tsx +117 -0
  171. package/components/primitives/Chart/index.ts +20 -0
  172. package/components/primitives/Checkbox/Checkbox.stories.tsx +87 -0
  173. package/components/primitives/Checkbox/Checkbox.tsx +38 -0
  174. package/components/primitives/Checkbox/index.ts +2 -0
  175. package/components/primitives/Collapsible/Collapsible.stories.tsx +38 -0
  176. package/components/primitives/Collapsible/Collapsible.tsx +39 -0
  177. package/components/primitives/Collapsible/index.ts +8 -0
  178. package/components/primitives/Command/Command.stories.tsx +150 -0
  179. package/components/primitives/Command/Command.tsx +147 -0
  180. package/components/primitives/Command/index.ts +20 -0
  181. package/components/primitives/Dialog/Dialog.stories.tsx +390 -0
  182. package/components/primitives/Dialog/Dialog.tsx +140 -0
  183. package/components/primitives/Dialog/index.ts +22 -0
  184. package/components/primitives/Drawer/Drawer.stories.tsx +327 -0
  185. package/components/primitives/Drawer/Drawer.tsx +208 -0
  186. package/components/primitives/Drawer/index.ts +27 -0
  187. package/components/primitives/DropdownMenu/DropdownMenu.stories.tsx +150 -0
  188. package/components/primitives/DropdownMenu/DropdownMenu.tsx +73 -0
  189. package/components/primitives/DropdownMenu/index.ts +1 -0
  190. package/components/primitives/HoverCard/HoverCard.stories.tsx +26 -0
  191. package/components/primitives/HoverCard/HoverCard.tsx +39 -0
  192. package/components/primitives/HoverCard/index.ts +8 -0
  193. package/components/primitives/Icon/Icon.stories.tsx +281 -0
  194. package/components/primitives/Icon/Icon.tsx +87 -0
  195. package/components/primitives/Icon/index.ts +8 -0
  196. package/components/primitives/Input/Input.stories.tsx +370 -0
  197. package/components/primitives/Input/Input.tsx +88 -0
  198. package/components/primitives/Input/index.ts +7 -0
  199. package/components/primitives/InputGroup/InputGroup.stories.tsx +40 -0
  200. package/components/primitives/InputGroup/InputGroup.tsx +72 -0
  201. package/components/primitives/InputGroup/index.ts +14 -0
  202. package/components/primitives/Label/Label.stories.tsx +227 -0
  203. package/components/primitives/Label/Label.tsx +53 -0
  204. package/components/primitives/Label/index.ts +7 -0
  205. package/components/primitives/Popover/Popover.stories.tsx +42 -0
  206. package/components/primitives/Popover/Popover.tsx +107 -0
  207. package/components/primitives/Popover/index.ts +2 -0
  208. package/components/primitives/Progress/Progress.stories.tsx +340 -0
  209. package/components/primitives/Progress/Progress.tsx +31 -0
  210. package/components/primitives/Progress/index.ts +1 -0
  211. package/components/primitives/ScrollArea/ScrollArea.stories.tsx +26 -0
  212. package/components/primitives/ScrollArea/ScrollArea.tsx +28 -0
  213. package/components/primitives/ScrollArea/index.ts +6 -0
  214. package/components/primitives/Select/Select.stories.tsx +288 -0
  215. package/components/primitives/Select/Select.tsx +162 -0
  216. package/components/primitives/Select/index.ts +22 -0
  217. package/components/primitives/Separator/Separator.stories.tsx +264 -0
  218. package/components/primitives/Separator/Separator.tsx +48 -0
  219. package/components/primitives/Separator/index.ts +7 -0
  220. package/components/primitives/Sidebar/Sidebar.stories.tsx +358 -0
  221. package/components/primitives/Sidebar/Sidebar.tsx +317 -0
  222. package/components/primitives/Sidebar/index.ts +41 -0
  223. package/components/primitives/Table/Table.stories.tsx +389 -0
  224. package/components/primitives/Table/Table.tsx +191 -0
  225. package/components/primitives/Table/index.ts +26 -0
  226. package/components/primitives/Tabs/Tabs.stories.tsx +129 -0
  227. package/components/primitives/Tabs/Tabs.tsx +70 -0
  228. package/components/primitives/Tabs/index.ts +13 -0
  229. package/components/primitives/Textarea/Textarea.stories.tsx +358 -0
  230. package/components/primitives/Textarea/Textarea.tsx +91 -0
  231. package/components/primitives/Textarea/index.ts +7 -0
  232. package/components/primitives/ToggleGroup/ToggleGroup.stories.tsx +87 -0
  233. package/components/primitives/ToggleGroup/ToggleGroup.tsx +52 -0
  234. package/components/primitives/ToggleGroup/index.ts +6 -0
  235. package/components/primitives/Tooltip/Tooltip.stories.tsx +336 -0
  236. package/components/primitives/Tooltip/Tooltip.tsx +78 -0
  237. package/components/primitives/Tooltip/index.ts +10 -0
  238. package/components/primitives/index.ts +34 -0
  239. package/components/ui/accordion.tsx +66 -0
  240. package/components/ui/alert-dialog.tsx +157 -0
  241. package/components/ui/alert.tsx +66 -0
  242. package/components/ui/avatar.tsx +53 -0
  243. package/components/ui/badge.tsx +46 -0
  244. package/components/ui/button.tsx +60 -0
  245. package/components/ui/card.tsx +117 -0
  246. package/components/ui/carousel.tsx +241 -0
  247. package/components/ui/chart.tsx +334 -0
  248. package/components/ui/checkbox.tsx +32 -0
  249. package/components/ui/collapsible.tsx +33 -0
  250. package/components/ui/command.tsx +184 -0
  251. package/components/ui/dialog.tsx +143 -0
  252. package/components/ui/drawer.tsx +118 -0
  253. package/components/ui/dropdown-menu.tsx +257 -0
  254. package/components/ui/hover-card.tsx +44 -0
  255. package/components/ui/input-group.tsx +170 -0
  256. package/components/ui/input.tsx +48 -0
  257. package/components/ui/label.tsx +26 -0
  258. package/components/ui/popover.tsx +33 -0
  259. package/components/ui/progress.tsx +31 -0
  260. package/components/ui/scroll-area.tsx +58 -0
  261. package/components/ui/select.tsx +187 -0
  262. package/components/ui/separator.tsx +31 -0
  263. package/components/ui/sidebar.tsx +577 -0
  264. package/components/ui/table.tsx +120 -0
  265. package/components/ui/tabs.tsx +66 -0
  266. package/components/ui/textarea.tsx +46 -0
  267. package/components/ui/toggle-group.tsx +83 -0
  268. package/components/ui/toggle.tsx +47 -0
  269. package/components/ui/tooltip.tsx +61 -0
  270. package/dist/index.cjs +7389 -0
  271. package/dist/index.cjs.map +1 -0
  272. package/dist/index.css +75 -0
  273. package/dist/index.css.map +1 -0
  274. package/dist/index.js +7160 -0
  275. package/dist/index.js.map +1 -0
  276. package/hooks/useAIDocReviewer.d.ts +0 -0
  277. package/lib/utils.ts +6 -0
  278. package/package.json +140 -0
  279. package/tokens/color/base.json +14 -0
  280. package/tokens/color/dark.json +40 -0
  281. package/tokens/color/green.json +21 -0
  282. package/tokens/color/light.json +52 -0
  283. package/tokens/color/neutral.json +20 -0
  284. package/tokens/color/violet.json +21 -0
  285. package/tokens/spacing.json +22 -0
  286. package/utils/ai-editor/format-date.ts +41 -0
  287. package/utils/ai-editor/index.ts +22 -0
  288. package/utils/ai-editor/type-guards.ts +72 -0
  289. package/utils/ai-editor/validation.ts +130 -0
  290. package/utils/editor-annotations.ts +122 -0
@@ -0,0 +1,1352 @@
1
+ "use client";
2
+
3
+ import { Button } from "@/components/ui/button";
4
+ import {
5
+ Command,
6
+ CommandEmpty,
7
+ CommandGroup,
8
+ CommandInput,
9
+ CommandItem,
10
+ CommandList,
11
+ CommandSeparator,
12
+ } from "@/components/ui/command";
13
+ import {
14
+ DropdownMenu,
15
+ DropdownMenuContent,
16
+ DropdownMenuItem,
17
+ DropdownMenuTrigger,
18
+ } from "@/components/ui/dropdown-menu";
19
+ import {
20
+ HoverCard,
21
+ HoverCardContent,
22
+ HoverCardTrigger,
23
+ } from "@/components/ui/hover-card";
24
+ import {
25
+ InputGroup,
26
+ InputGroupAddon,
27
+ InputGroupButton,
28
+ InputGroupTextarea,
29
+ } from "@/components/ui/input-group";
30
+ import {
31
+ Select,
32
+ SelectContent,
33
+ SelectItem,
34
+ SelectTrigger,
35
+ SelectValue,
36
+ } from "@/components/ui/select";
37
+ import { cn } from "@/lib/utils";
38
+ import type { ChatStatus, FileUIPart } from "ai";
39
+ import {
40
+ ImageIcon,
41
+ Loader2Icon,
42
+ MicIcon,
43
+ PaperclipIcon,
44
+ PlusIcon,
45
+ SendIcon,
46
+ SquareIcon,
47
+ XIcon,
48
+ } from "lucide-react";
49
+ import { nanoid } from "nanoid";
50
+ import {
51
+ type ChangeEvent,
52
+ type ChangeEventHandler,
53
+ Children,
54
+ type ClipboardEventHandler,
55
+ type ComponentProps,
56
+ createContext,
57
+ type FormEvent,
58
+ type FormEventHandler,
59
+ Fragment,
60
+ type HTMLAttributes,
61
+ type KeyboardEventHandler,
62
+ type PropsWithChildren,
63
+ type ReactNode,
64
+ type RefObject,
65
+ useCallback,
66
+ useContext,
67
+ useEffect,
68
+ useMemo,
69
+ useRef,
70
+ useState,
71
+ } from "react";
72
+ // ============================================================================
73
+ // Provider Context & Types
74
+ // ============================================================================
75
+
76
+ export type AttachmentsContext = {
77
+ files: (FileUIPart & { id: string })[];
78
+ add: (files: File[] | FileList) => void;
79
+ remove: (id: string) => void;
80
+ clear: () => void;
81
+ openFileDialog: () => void;
82
+ fileInputRef: RefObject<HTMLInputElement | null>;
83
+ };
84
+
85
+ export type TextInputContext = {
86
+ value: string;
87
+ setInput: (v: string) => void;
88
+ clear: () => void;
89
+ };
90
+
91
+ export type PromptInputControllerProps = {
92
+ textInput: TextInputContext;
93
+ attachments: AttachmentsContext;
94
+ /** INTERNAL: Allows PromptInput to register its file textInput + "open" callback */
95
+ __registerFileInput: (
96
+ ref: RefObject<HTMLInputElement | null>,
97
+ open: () => void
98
+ ) => void;
99
+ };
100
+
101
+ const PromptInputController = createContext<PromptInputControllerProps | null>(
102
+ null
103
+ );
104
+ const ProviderAttachmentsContext = createContext<AttachmentsContext | null>(
105
+ null
106
+ );
107
+
108
+ export const usePromptInputController = () => {
109
+ const ctx = useContext(PromptInputController);
110
+ if (!ctx) {
111
+ throw new Error(
112
+ "Wrap your component inside <PromptInputProvider> to use usePromptInputController()."
113
+ );
114
+ }
115
+ return ctx;
116
+ };
117
+
118
+ // Optional variants (do NOT throw). Useful for dual-mode components.
119
+ const useOptionalPromptInputController = () =>
120
+ useContext(PromptInputController);
121
+
122
+ export const useProviderAttachments = () => {
123
+ const ctx = useContext(ProviderAttachmentsContext);
124
+ if (!ctx) {
125
+ throw new Error(
126
+ "Wrap your component inside <PromptInputProvider> to use useProviderAttachments()."
127
+ );
128
+ }
129
+ return ctx;
130
+ };
131
+
132
+ const useOptionalProviderAttachments = () =>
133
+ useContext(ProviderAttachmentsContext);
134
+
135
+ export type PromptInputProviderProps = PropsWithChildren<{
136
+ initialInput?: string;
137
+ }>;
138
+
139
+ /**
140
+ * Optional global provider that lifts PromptInput state outside of PromptInput.
141
+ * If you don't use it, PromptInput stays fully self-managed.
142
+ */
143
+ export function PromptInputProvider({
144
+ initialInput: initialTextInput = "",
145
+ children,
146
+ }: PromptInputProviderProps) {
147
+ // ----- textInput state
148
+ const [textInput, setTextInput] = useState(initialTextInput);
149
+ const clearInput = useCallback(() => setTextInput(""), []);
150
+
151
+ // ----- attachments state (global when wrapped)
152
+ const [attachements, setAttachements] = useState<
153
+ (FileUIPart & { id: string })[]
154
+ >([]);
155
+ const fileInputRef = useRef<HTMLInputElement | null>(null);
156
+ const openRef = useRef<() => void>(() => {});
157
+
158
+ const add = useCallback((files: File[] | FileList) => {
159
+ const incoming = Array.from(files);
160
+ if (incoming.length === 0) return;
161
+
162
+ setAttachements((prev) =>
163
+ prev.concat(
164
+ incoming.map((file) => ({
165
+ id: nanoid(),
166
+ type: "file" as const,
167
+ url: URL.createObjectURL(file),
168
+ mediaType: file.type,
169
+ filename: file.name,
170
+ }))
171
+ )
172
+ );
173
+ }, []);
174
+
175
+ const remove = useCallback((id: string) => {
176
+ setAttachements((prev) => {
177
+ const found = prev.find((f) => f.id === id);
178
+ if (found?.url) URL.revokeObjectURL(found.url);
179
+ return prev.filter((f) => f.id !== id);
180
+ });
181
+ }, []);
182
+
183
+ const clear = useCallback(() => {
184
+ setAttachements((prev) => {
185
+ for (const f of prev) if (f.url) URL.revokeObjectURL(f.url);
186
+ return [];
187
+ });
188
+ }, []);
189
+
190
+ const openFileDialog = useCallback(() => {
191
+ openRef.current?.();
192
+ }, []);
193
+
194
+ const attachments = useMemo<AttachmentsContext>(
195
+ () => ({
196
+ files: attachements,
197
+ add,
198
+ remove,
199
+ clear,
200
+ openFileDialog,
201
+ fileInputRef,
202
+ }),
203
+ [attachements, add, remove, clear, openFileDialog]
204
+ );
205
+
206
+ const __registerFileInput = useCallback(
207
+ (ref: RefObject<HTMLInputElement | null>, open: () => void) => {
208
+ fileInputRef.current = ref.current;
209
+ openRef.current = open;
210
+ },
211
+ []
212
+ );
213
+
214
+ const controller = useMemo<PromptInputControllerProps>(
215
+ () => ({
216
+ textInput: {
217
+ value: textInput,
218
+ setInput: setTextInput,
219
+ clear: clearInput,
220
+ },
221
+ attachments,
222
+ __registerFileInput,
223
+ }),
224
+ [textInput, clearInput, attachments, __registerFileInput]
225
+ );
226
+
227
+ return (
228
+ <PromptInputController.Provider value={controller}>
229
+ <ProviderAttachmentsContext.Provider value={attachments}>
230
+ {children}
231
+ </ProviderAttachmentsContext.Provider>
232
+ </PromptInputController.Provider>
233
+ );
234
+ }
235
+
236
+ // ============================================================================
237
+ // Component Context & Hooks
238
+ // ============================================================================
239
+
240
+ const LocalAttachmentsContext = createContext<AttachmentsContext | null>(null);
241
+
242
+ export const usePromptInputAttachments = () => {
243
+ // Dual-mode: prefer provider if present, otherwise use local
244
+ const provider = useOptionalProviderAttachments();
245
+ const local = useContext(LocalAttachmentsContext);
246
+ const context = provider ?? local;
247
+ if (!context) {
248
+ throw new Error(
249
+ "usePromptInputAttachments must be used within a PromptInput or PromptInputProvider"
250
+ );
251
+ }
252
+ return context;
253
+ };
254
+
255
+ export type PromptInputAttachmentProps = HTMLAttributes<HTMLDivElement> & {
256
+ data: FileUIPart & { id: string };
257
+ className?: string;
258
+ };
259
+
260
+ export function PromptInputAttachment({
261
+ data,
262
+ className,
263
+ ...props
264
+ }: PromptInputAttachmentProps) {
265
+ const attachments = usePromptInputAttachments();
266
+
267
+ const filename = data.filename || "";
268
+
269
+ const mediaType =
270
+ data.mediaType?.startsWith("image/") && data.url ? "image" : "file";
271
+ const isImage = mediaType === "image";
272
+
273
+ const attachmentLabel = filename || (isImage ? "Image" : "Attachment");
274
+
275
+ return (
276
+ <PromptInputHoverCard>
277
+ <HoverCardTrigger asChild>
278
+ <div
279
+ className={cn(
280
+ "group relative flex h-8 cursor-default select-none items-center gap-1.5 rounded-md border border-border px-1.5 font-medium text-sm transition-all hover:bg-accent hover:text-accent-foreground dark:hover:bg-accent/50",
281
+ className
282
+ )}
283
+ key={data.id}
284
+ {...props}
285
+ >
286
+ <div className="relative size-5 shrink-0">
287
+ <div className="absolute inset-0 flex size-5 items-center justify-center overflow-hidden rounded bg-background transition-opacity group-hover:opacity-0">
288
+ {isImage ? (
289
+ <img
290
+ alt={filename || "attachment"}
291
+ className="size-5 object-cover"
292
+ height={20}
293
+ src={data.url}
294
+ width={20}
295
+ />
296
+ ) : (
297
+ <div className="flex size-5 items-center justify-center text-muted-foreground">
298
+ <PaperclipIcon className="size-3" />
299
+ </div>
300
+ )}
301
+ </div>
302
+ <Button
303
+ aria-label="Remove attachment"
304
+ className="absolute inset-0 size-5 cursor-pointer rounded p-0 opacity-0 transition-opacity group-hover:pointer-events-auto group-hover:opacity-100 [&>svg]:size-2.5"
305
+ onClick={(e) => {
306
+ e.stopPropagation();
307
+ attachments.remove(data.id);
308
+ }}
309
+ type="button"
310
+ variant="ghost"
311
+ >
312
+ <XIcon />
313
+ <span className="sr-only">Remove</span>
314
+ </Button>
315
+ </div>
316
+
317
+ <span className="flex-1 truncate">{attachmentLabel}</span>
318
+ </div>
319
+ </HoverCardTrigger>
320
+ <PromptInputHoverCardContent className="w-auto p-2">
321
+ <div className="w-auto space-y-3">
322
+ {isImage && (
323
+ <div className="flex max-h-96 w-96 items-center justify-center overflow-hidden rounded-md border">
324
+ <img
325
+ alt={filename || "attachment preview"}
326
+ className="max-h-full max-w-full object-contain"
327
+ height={384}
328
+ src={data.url}
329
+ width={448}
330
+ />
331
+ </div>
332
+ )}
333
+ <div className="flex items-center gap-2.5">
334
+ <div className="min-w-0 flex-1 space-y-1 px-0.5">
335
+ <h4 className="truncate font-semibold text-sm leading-none">
336
+ {filename || (isImage ? "Image" : "Attachment")}
337
+ </h4>
338
+ {data.mediaType && (
339
+ <p className="truncate font-mono text-muted-foreground text-xs">
340
+ {data.mediaType}
341
+ </p>
342
+ )}
343
+ </div>
344
+ </div>
345
+ </div>
346
+ </PromptInputHoverCardContent>
347
+ </PromptInputHoverCard>
348
+ );
349
+ }
350
+
351
+ export type PromptInputAttachmentsProps = Omit<
352
+ HTMLAttributes<HTMLDivElement>,
353
+ "children"
354
+ > & {
355
+ children: (attachment: FileUIPart & { id: string }) => ReactNode;
356
+ };
357
+
358
+ export function PromptInputAttachments({
359
+ children,
360
+ }: PromptInputAttachmentsProps) {
361
+ const attachments = usePromptInputAttachments();
362
+
363
+ if (!attachments.files.length) {
364
+ return null;
365
+ }
366
+
367
+ return attachments.files.map((file) => (
368
+ <Fragment key={file.id}>{children(file)}</Fragment>
369
+ ));
370
+ }
371
+
372
+ export type PromptInputActionAddAttachmentsProps = ComponentProps<
373
+ typeof DropdownMenuItem
374
+ > & {
375
+ label?: string;
376
+ };
377
+
378
+ export const PromptInputActionAddAttachments = ({
379
+ label = "Add photos or files",
380
+ ...props
381
+ }: PromptInputActionAddAttachmentsProps) => {
382
+ const attachments = usePromptInputAttachments();
383
+
384
+ return (
385
+ <DropdownMenuItem
386
+ {...props}
387
+ onSelect={(e) => {
388
+ e.preventDefault();
389
+ attachments.openFileDialog();
390
+ }}
391
+ >
392
+ <ImageIcon className="mr-2 size-4" /> {label}
393
+ </DropdownMenuItem>
394
+ );
395
+ };
396
+
397
+ export type PromptInputMessage = {
398
+ text?: string;
399
+ files?: FileUIPart[];
400
+ };
401
+
402
+ export type PromptInputProps = Omit<
403
+ HTMLAttributes<HTMLFormElement>,
404
+ "onSubmit" | "onError"
405
+ > & {
406
+ accept?: string; // e.g., "image/*" or leave undefined for any
407
+ multiple?: boolean;
408
+ // When true, accepts drops anywhere on document. Default false (opt-in).
409
+ globalDrop?: boolean;
410
+ // Render a hidden input with given name and keep it in sync for native form posts. Default false.
411
+ syncHiddenInput?: boolean;
412
+ // Minimal constraints
413
+ maxFiles?: number;
414
+ maxFileSize?: number; // bytes
415
+ onError?: (err: {
416
+ code: "max_files" | "max_file_size" | "accept";
417
+ message: string;
418
+ }) => void;
419
+ onSubmit: (
420
+ message: PromptInputMessage,
421
+ event: FormEvent<HTMLFormElement>
422
+ ) => void | Promise<void>;
423
+ };
424
+
425
+ export const PromptInput = ({
426
+ className,
427
+ accept,
428
+ multiple,
429
+ globalDrop,
430
+ syncHiddenInput,
431
+ maxFiles,
432
+ maxFileSize,
433
+ onError,
434
+ onSubmit,
435
+ children,
436
+ ...props
437
+ }: PromptInputProps) => {
438
+ // Try to use a provider controller if present
439
+ const controller = useOptionalPromptInputController();
440
+ const usingProvider = !!controller;
441
+
442
+ // Refs
443
+ const inputRef = useRef<HTMLInputElement | null>(null);
444
+ const anchorRef = useRef<HTMLSpanElement>(null);
445
+ const formRef = useRef<HTMLFormElement | null>(null);
446
+
447
+ // Find nearest form to scope drag & drop
448
+ useEffect(() => {
449
+ const root = anchorRef.current?.closest("form");
450
+ if (root instanceof HTMLFormElement) {
451
+ formRef.current = root;
452
+ }
453
+ }, []);
454
+
455
+ // ----- Local attachments (only used when no provider)
456
+ const [items, setItems] = useState<(FileUIPart & { id: string })[]>([]);
457
+ const files = usingProvider ? controller.attachments.files : items;
458
+
459
+ const openFileDialogLocal = useCallback(() => {
460
+ inputRef.current?.click();
461
+ }, []);
462
+
463
+ const matchesAccept = useCallback(
464
+ (f: File) => {
465
+ if (!accept || accept.trim() === "") {
466
+ return true;
467
+ }
468
+ if (accept.includes("image/*")) {
469
+ return f.type.startsWith("image/");
470
+ }
471
+ // NOTE: keep simple; expand as needed
472
+ return true;
473
+ },
474
+ [accept]
475
+ );
476
+
477
+ const addLocal = useCallback(
478
+ (fileList: File[] | FileList) => {
479
+ const incoming = Array.from(fileList);
480
+ const accepted = incoming.filter((f) => matchesAccept(f));
481
+ if (incoming.length && accepted.length === 0) {
482
+ onError?.({
483
+ code: "accept",
484
+ message: "No files match the accepted types.",
485
+ });
486
+ return;
487
+ }
488
+ const withinSize = (f: File) =>
489
+ maxFileSize ? f.size <= maxFileSize : true;
490
+ const sized = accepted.filter(withinSize);
491
+ if (accepted.length > 0 && sized.length === 0) {
492
+ onError?.({
493
+ code: "max_file_size",
494
+ message: "All files exceed the maximum size.",
495
+ });
496
+ return;
497
+ }
498
+
499
+ setItems((prev) => {
500
+ const capacity =
501
+ typeof maxFiles === "number"
502
+ ? Math.max(0, maxFiles - prev.length)
503
+ : undefined;
504
+ const capped =
505
+ typeof capacity === "number" ? sized.slice(0, capacity) : sized;
506
+ if (typeof capacity === "number" && sized.length > capacity) {
507
+ onError?.({
508
+ code: "max_files",
509
+ message: "Too many files. Some were not added.",
510
+ });
511
+ }
512
+ const next: (FileUIPart & { id: string })[] = [];
513
+ for (const file of capped) {
514
+ next.push({
515
+ id: nanoid(),
516
+ type: "file",
517
+ url: URL.createObjectURL(file),
518
+ mediaType: file.type,
519
+ filename: file.name,
520
+ });
521
+ }
522
+ return prev.concat(next);
523
+ });
524
+ },
525
+ [matchesAccept, maxFiles, maxFileSize, onError]
526
+ );
527
+
528
+ const add = usingProvider
529
+ ? (files: File[] | FileList) => controller.attachments.add(files)
530
+ : addLocal;
531
+
532
+ const remove = usingProvider
533
+ ? (id: string) => controller.attachments.remove(id)
534
+ : (id: string) =>
535
+ setItems((prev) => {
536
+ const found = prev.find((file) => file.id === id);
537
+ if (found?.url) {
538
+ URL.revokeObjectURL(found.url);
539
+ }
540
+ return prev.filter((file) => file.id !== id);
541
+ });
542
+
543
+ const clear = usingProvider
544
+ ? () => controller.attachments.clear()
545
+ : () =>
546
+ setItems((prev) => {
547
+ for (const file of prev) {
548
+ if (file.url) {
549
+ URL.revokeObjectURL(file.url);
550
+ }
551
+ }
552
+ return [];
553
+ });
554
+
555
+ const openFileDialog = usingProvider
556
+ ? () => controller.attachments.openFileDialog()
557
+ : openFileDialogLocal;
558
+
559
+ // Let provider know about our hidden file input so external menus can call openFileDialog()
560
+ useEffect(() => {
561
+ if (!usingProvider) return;
562
+ controller.__registerFileInput(inputRef, () => inputRef.current?.click());
563
+ }, [usingProvider, controller]);
564
+
565
+ // Note: File input cannot be programmatically set for security reasons
566
+ // The syncHiddenInput prop is no longer functional
567
+ useEffect(() => {
568
+ if (syncHiddenInput && inputRef.current && files.length === 0) {
569
+ inputRef.current.value = "";
570
+ }
571
+ }, [files, syncHiddenInput]);
572
+
573
+ // Attach drop handlers on nearest form and document (opt-in)
574
+ useEffect(() => {
575
+ const form = formRef.current;
576
+ if (!form) return;
577
+
578
+ const onDragOver = (e: DragEvent) => {
579
+ if (e.dataTransfer?.types?.includes("Files")) {
580
+ e.preventDefault();
581
+ }
582
+ };
583
+ const onDrop = (e: DragEvent) => {
584
+ if (e.dataTransfer?.types?.includes("Files")) {
585
+ e.preventDefault();
586
+ }
587
+ if (e.dataTransfer?.files && e.dataTransfer.files.length > 0) {
588
+ add(e.dataTransfer.files);
589
+ }
590
+ };
591
+ form.addEventListener("dragover", onDragOver);
592
+ form.addEventListener("drop", onDrop);
593
+ return () => {
594
+ form.removeEventListener("dragover", onDragOver);
595
+ form.removeEventListener("drop", onDrop);
596
+ };
597
+ }, [add]);
598
+
599
+ useEffect(() => {
600
+ if (!globalDrop) return;
601
+
602
+ const onDragOver = (e: DragEvent) => {
603
+ if (e.dataTransfer?.types?.includes("Files")) {
604
+ e.preventDefault();
605
+ }
606
+ };
607
+ const onDrop = (e: DragEvent) => {
608
+ if (e.dataTransfer?.types?.includes("Files")) {
609
+ e.preventDefault();
610
+ }
611
+ if (e.dataTransfer?.files && e.dataTransfer.files.length > 0) {
612
+ add(e.dataTransfer.files);
613
+ }
614
+ };
615
+ document.addEventListener("dragover", onDragOver);
616
+ document.addEventListener("drop", onDrop);
617
+ return () => {
618
+ document.removeEventListener("dragover", onDragOver);
619
+ document.removeEventListener("drop", onDrop);
620
+ };
621
+ }, [add, globalDrop]);
622
+
623
+ useEffect(
624
+ () => () => {
625
+ if (!usingProvider) {
626
+ for (const f of files) {
627
+ if (f.url) URL.revokeObjectURL(f.url);
628
+ }
629
+ }
630
+ },
631
+ [usingProvider, files]
632
+ );
633
+
634
+ const handleChange: ChangeEventHandler<HTMLInputElement> = (event) => {
635
+ if (event.currentTarget.files) {
636
+ add(event.currentTarget.files);
637
+ }
638
+ };
639
+
640
+ const convertBlobUrlToDataUrl = async (url: string): Promise<string> => {
641
+ const response = await fetch(url);
642
+ const blob = await response.blob();
643
+ return new Promise((resolve, reject) => {
644
+ const reader = new FileReader();
645
+ reader.onloadend = () => resolve(reader.result as string);
646
+ reader.onerror = reject;
647
+ reader.readAsDataURL(blob);
648
+ });
649
+ };
650
+
651
+ const ctx = useMemo<AttachmentsContext>(
652
+ () => ({
653
+ files: files.map((item) => ({ ...item, id: item.id })),
654
+ add,
655
+ remove,
656
+ clear,
657
+ openFileDialog,
658
+ fileInputRef: inputRef,
659
+ }),
660
+ [files, add, remove, clear, openFileDialog]
661
+ );
662
+
663
+ const handleSubmit: FormEventHandler<HTMLFormElement> = (event) => {
664
+ event.preventDefault();
665
+
666
+ const form = event.currentTarget;
667
+ const text = usingProvider
668
+ ? controller.textInput.value
669
+ : (() => {
670
+ const formData = new FormData(form);
671
+ return (formData.get("message") as string) || "";
672
+ })();
673
+
674
+ // Reset form immediately after capturing text to avoid race condition
675
+ // where user input during async blob conversion would be lost
676
+ if (!usingProvider) {
677
+ form.reset();
678
+ }
679
+
680
+ // Convert blob URLs to data URLs asynchronously
681
+ Promise.all(
682
+ files.map(async ({ id, ...item }) => {
683
+ if (item.url && item.url.startsWith("blob:")) {
684
+ return {
685
+ ...item,
686
+ url: await convertBlobUrlToDataUrl(item.url),
687
+ };
688
+ }
689
+ return item;
690
+ })
691
+ ).then((convertedFiles: FileUIPart[]) => {
692
+ try {
693
+ const result = onSubmit({ text, files: convertedFiles }, event);
694
+
695
+ // Handle both sync and async onSubmit
696
+ if (result instanceof Promise) {
697
+ result
698
+ .then(() => {
699
+ clear();
700
+ if (usingProvider) {
701
+ controller.textInput.clear();
702
+ }
703
+ })
704
+ .catch(() => {
705
+ // Don't clear on error - user may want to retry
706
+ });
707
+ } else {
708
+ // Sync function completed without throwing, clear attachments
709
+ clear();
710
+ if (usingProvider) {
711
+ controller.textInput.clear();
712
+ }
713
+ }
714
+ } catch (error) {
715
+ // Don't clear on error - user may want to retry
716
+ }
717
+ });
718
+ };
719
+
720
+ // Render with or without local provider
721
+ const inner = (
722
+ <>
723
+ <span aria-hidden="true" className="hidden" ref={anchorRef} />
724
+ <input
725
+ accept={accept}
726
+ aria-label="Upload files"
727
+ className="hidden"
728
+ multiple={multiple}
729
+ onChange={handleChange}
730
+ ref={inputRef}
731
+ title="Upload files"
732
+ type="file"
733
+ />
734
+ <form
735
+ className={cn("w-full", className)}
736
+ onSubmit={handleSubmit}
737
+ {...props}
738
+ >
739
+ <InputGroup>{children}</InputGroup>
740
+ </form>
741
+ </>
742
+ );
743
+
744
+ return usingProvider ? (
745
+ inner
746
+ ) : (
747
+ <LocalAttachmentsContext.Provider value={ctx}>
748
+ {inner}
749
+ </LocalAttachmentsContext.Provider>
750
+ );
751
+ };
752
+
753
+ export type PromptInputBodyProps = HTMLAttributes<HTMLDivElement>;
754
+
755
+ export const PromptInputBody = ({
756
+ className,
757
+ ...props
758
+ }: PromptInputBodyProps) => (
759
+ <div className={cn("contents", className)} {...props} />
760
+ );
761
+
762
+ export type PromptInputTextareaProps = ComponentProps<
763
+ typeof InputGroupTextarea
764
+ >;
765
+
766
+ export const PromptInputTextarea = ({
767
+ onChange,
768
+ className,
769
+ placeholder = "What would you like to know?",
770
+ ...props
771
+ }: PromptInputTextareaProps) => {
772
+ const controller = useOptionalPromptInputController();
773
+ const attachments = usePromptInputAttachments();
774
+ const [isComposing, setIsComposing] = useState(false);
775
+
776
+ const handleKeyDown: KeyboardEventHandler<HTMLTextAreaElement> = (e) => {
777
+ if (e.key === "Enter") {
778
+ if (isComposing || e.nativeEvent.isComposing) {
779
+ return;
780
+ }
781
+ if (e.shiftKey) {
782
+ return;
783
+ }
784
+ e.preventDefault();
785
+ e.currentTarget.form?.requestSubmit();
786
+ }
787
+
788
+ // Remove last attachment when Backspace is pressed and textarea is empty
789
+ if (
790
+ e.key === "Backspace" &&
791
+ e.currentTarget.value === "" &&
792
+ attachments.files.length > 0
793
+ ) {
794
+ e.preventDefault();
795
+ const lastAttachment = attachments.files.at(-1);
796
+ if (lastAttachment) {
797
+ attachments.remove(lastAttachment.id);
798
+ }
799
+ }
800
+ };
801
+
802
+ const handlePaste: ClipboardEventHandler<HTMLTextAreaElement> = (event) => {
803
+ const items = event.clipboardData?.items;
804
+
805
+ if (!items) {
806
+ return;
807
+ }
808
+
809
+ const files: File[] = [];
810
+
811
+ for (const item of items) {
812
+ if (item.kind === "file") {
813
+ const file = item.getAsFile();
814
+ if (file) {
815
+ files.push(file);
816
+ }
817
+ }
818
+ }
819
+
820
+ if (files.length > 0) {
821
+ event.preventDefault();
822
+ attachments.add(files);
823
+ }
824
+ };
825
+
826
+ const controlledProps = controller
827
+ ? {
828
+ value: controller.textInput.value,
829
+ onChange: (e: ChangeEvent<HTMLTextAreaElement>) => {
830
+ controller.textInput.setInput(e.currentTarget.value);
831
+ onChange?.(e);
832
+ },
833
+ }
834
+ : {
835
+ onChange,
836
+ };
837
+
838
+ return (
839
+ <InputGroupTextarea
840
+ className={cn("field-sizing-content max-h-48 min-h-16", className)}
841
+ name="message"
842
+ onCompositionEnd={() => setIsComposing(false)}
843
+ onCompositionStart={() => setIsComposing(true)}
844
+ onKeyDown={handleKeyDown}
845
+ onPaste={handlePaste}
846
+ placeholder={placeholder}
847
+ {...props}
848
+ {...controlledProps}
849
+ />
850
+ );
851
+ };
852
+
853
+ export type PromptInputHeaderProps = Omit<
854
+ ComponentProps<typeof InputGroupAddon>,
855
+ "align"
856
+ >;
857
+
858
+ export const PromptInputHeader = ({
859
+ className,
860
+ ...props
861
+ }: PromptInputHeaderProps) => (
862
+ <InputGroupAddon
863
+ align="block-end"
864
+ className={cn("order-first flex-wrap gap-1", className)}
865
+ {...props}
866
+ />
867
+ );
868
+
869
+ export type PromptInputFooterProps = Omit<
870
+ ComponentProps<typeof InputGroupAddon>,
871
+ "align"
872
+ >;
873
+
874
+ export const PromptInputFooter = ({
875
+ className,
876
+ ...props
877
+ }: PromptInputFooterProps) => (
878
+ <InputGroupAddon
879
+ align="block-end"
880
+ className={cn("justify-between gap-1", className)}
881
+ {...props}
882
+ />
883
+ );
884
+
885
+ export type PromptInputToolsProps = HTMLAttributes<HTMLDivElement>;
886
+
887
+ export const PromptInputTools = ({
888
+ className,
889
+ ...props
890
+ }: PromptInputToolsProps) => (
891
+ <div className={cn("flex items-center gap-1", className)} {...props} />
892
+ );
893
+
894
+ export type PromptInputButtonProps = ComponentProps<typeof InputGroupButton>;
895
+
896
+ export const PromptInputButton = ({
897
+ variant = "ghost",
898
+ className,
899
+ size,
900
+ ...props
901
+ }: PromptInputButtonProps) => {
902
+ const newSize =
903
+ size ?? (Children.count(props.children) > 1 ? "sm" : "icon-sm");
904
+
905
+ return (
906
+ <InputGroupButton
907
+ className={cn(className)}
908
+ size={newSize}
909
+ type="button"
910
+ variant={variant}
911
+ {...props}
912
+ />
913
+ );
914
+ };
915
+
916
+ export type PromptInputActionMenuProps = ComponentProps<typeof DropdownMenu>;
917
+ export const PromptInputActionMenu = (props: PromptInputActionMenuProps) => (
918
+ <DropdownMenu {...props} />
919
+ );
920
+
921
+ export type PromptInputActionMenuTriggerProps = PromptInputButtonProps;
922
+
923
+ export const PromptInputActionMenuTrigger = ({
924
+ className,
925
+ children,
926
+ ...props
927
+ }: PromptInputActionMenuTriggerProps) => (
928
+ <DropdownMenuTrigger asChild>
929
+ <PromptInputButton className={className} {...props}>
930
+ {children ?? <PlusIcon className="size-4" />}
931
+ </PromptInputButton>
932
+ </DropdownMenuTrigger>
933
+ );
934
+
935
+ export type PromptInputActionMenuContentProps = ComponentProps<
936
+ typeof DropdownMenuContent
937
+ >;
938
+ export const PromptInputActionMenuContent = ({
939
+ className,
940
+ ...props
941
+ }: PromptInputActionMenuContentProps) => (
942
+ <DropdownMenuContent align="start" className={cn(className)} {...props} />
943
+ );
944
+
945
+ export type PromptInputActionMenuItemProps = ComponentProps<
946
+ typeof DropdownMenuItem
947
+ >;
948
+ export const PromptInputActionMenuItem = ({
949
+ className,
950
+ ...props
951
+ }: PromptInputActionMenuItemProps) => (
952
+ <DropdownMenuItem className={cn(className)} {...props} />
953
+ );
954
+
955
+ // Note: Actions that perform side-effects (like opening a file dialog)
956
+ // are provided in opt-in modules (e.g., prompt-input-attachments).
957
+
958
+ export type PromptInputSubmitProps = ComponentProps<typeof InputGroupButton> & {
959
+ status?: ChatStatus;
960
+ };
961
+
962
+ export const PromptInputSubmit = ({
963
+ className,
964
+ variant = "default",
965
+ size = "icon-sm",
966
+ status,
967
+ children,
968
+ ...props
969
+ }: PromptInputSubmitProps) => {
970
+ let Icon = <SendIcon className="size-4" />;
971
+
972
+ if (status === "submitted") {
973
+ Icon = <Loader2Icon className="size-4 animate-spin" />;
974
+ } else if (status === "streaming") {
975
+ Icon = <SquareIcon className="size-4" />;
976
+ } else if (status === "error") {
977
+ Icon = <XIcon className="size-4" />;
978
+ }
979
+
980
+ return (
981
+ <InputGroupButton
982
+ aria-label="Submit"
983
+ className={cn(className)}
984
+ size={size}
985
+ type="submit"
986
+ variant={variant}
987
+ {...props}
988
+ >
989
+ {children ?? Icon}
990
+ </InputGroupButton>
991
+ );
992
+ };
993
+
994
+ interface SpeechRecognition extends EventTarget {
995
+ continuous: boolean;
996
+ interimResults: boolean;
997
+ lang: string;
998
+ start(): void;
999
+ stop(): void;
1000
+ onstart: ((this: SpeechRecognition, ev: Event) => any) | null;
1001
+ onend: ((this: SpeechRecognition, ev: Event) => any) | null;
1002
+ onresult:
1003
+ | ((this: SpeechRecognition, ev: SpeechRecognitionEvent) => any)
1004
+ | null;
1005
+ onerror:
1006
+ | ((this: SpeechRecognition, ev: SpeechRecognitionErrorEvent) => any)
1007
+ | null;
1008
+ }
1009
+
1010
+ interface SpeechRecognitionEvent extends Event {
1011
+ results: SpeechRecognitionResultList;
1012
+ }
1013
+
1014
+ type SpeechRecognitionResultList = {
1015
+ readonly length: number;
1016
+ item(index: number): SpeechRecognitionResult;
1017
+ [index: number]: SpeechRecognitionResult;
1018
+ };
1019
+
1020
+ type SpeechRecognitionResult = {
1021
+ readonly length: number;
1022
+ item(index: number): SpeechRecognitionAlternative;
1023
+ [index: number]: SpeechRecognitionAlternative;
1024
+ isFinal: boolean;
1025
+ };
1026
+
1027
+ type SpeechRecognitionAlternative = {
1028
+ transcript: string;
1029
+ confidence: number;
1030
+ };
1031
+
1032
+ interface SpeechRecognitionErrorEvent extends Event {
1033
+ error: string;
1034
+ }
1035
+
1036
+ declare global {
1037
+ interface Window {
1038
+ SpeechRecognition: {
1039
+ new (): SpeechRecognition;
1040
+ };
1041
+ webkitSpeechRecognition: {
1042
+ new (): SpeechRecognition;
1043
+ };
1044
+ }
1045
+ }
1046
+
1047
+ export type PromptInputSpeechButtonProps = ComponentProps<
1048
+ typeof PromptInputButton
1049
+ > & {
1050
+ textareaRef?: RefObject<HTMLTextAreaElement | null>;
1051
+ onTranscriptionChange?: (text: string) => void;
1052
+ };
1053
+
1054
+ export const PromptInputSpeechButton = ({
1055
+ className,
1056
+ textareaRef,
1057
+ onTranscriptionChange,
1058
+ ...props
1059
+ }: PromptInputSpeechButtonProps) => {
1060
+ const [isListening, setIsListening] = useState(false);
1061
+ const [recognition, setRecognition] = useState<SpeechRecognition | null>(
1062
+ null
1063
+ );
1064
+ const recognitionRef = useRef<SpeechRecognition | null>(null);
1065
+
1066
+ useEffect(() => {
1067
+ if (
1068
+ typeof window !== "undefined" &&
1069
+ ("SpeechRecognition" in window || "webkitSpeechRecognition" in window)
1070
+ ) {
1071
+ const SpeechRecognition =
1072
+ window.SpeechRecognition || window.webkitSpeechRecognition;
1073
+ const speechRecognition = new SpeechRecognition();
1074
+
1075
+ speechRecognition.continuous = true;
1076
+ speechRecognition.interimResults = true;
1077
+ speechRecognition.lang = "en-US";
1078
+
1079
+ speechRecognition.onstart = () => {
1080
+ setIsListening(true);
1081
+ };
1082
+
1083
+ speechRecognition.onend = () => {
1084
+ setIsListening(false);
1085
+ };
1086
+
1087
+ speechRecognition.onresult = (event) => {
1088
+ let finalTranscript = "";
1089
+
1090
+ const results = Array.from(event.results);
1091
+
1092
+ for (const result of results) {
1093
+ if (result.isFinal) {
1094
+ finalTranscript += result[0]?.transcript ?? "";
1095
+ }
1096
+ }
1097
+
1098
+ if (finalTranscript && textareaRef?.current) {
1099
+ const textarea = textareaRef.current;
1100
+ const currentValue = textarea.value;
1101
+ const newValue =
1102
+ currentValue + (currentValue ? " " : "") + finalTranscript;
1103
+
1104
+ textarea.value = newValue;
1105
+ textarea.dispatchEvent(new Event("input", { bubbles: true }));
1106
+ onTranscriptionChange?.(newValue);
1107
+ }
1108
+ };
1109
+
1110
+ speechRecognition.onerror = (event) => {
1111
+ console.error("Speech recognition error:", event.error);
1112
+ setIsListening(false);
1113
+ };
1114
+
1115
+ recognitionRef.current = speechRecognition;
1116
+ setRecognition(speechRecognition);
1117
+ }
1118
+
1119
+ return () => {
1120
+ if (recognitionRef.current) {
1121
+ recognitionRef.current.stop();
1122
+ }
1123
+ };
1124
+ }, [textareaRef, onTranscriptionChange]);
1125
+
1126
+ const toggleListening = useCallback(() => {
1127
+ if (!recognition) {
1128
+ return;
1129
+ }
1130
+
1131
+ if (isListening) {
1132
+ recognition.stop();
1133
+ } else {
1134
+ recognition.start();
1135
+ }
1136
+ }, [recognition, isListening]);
1137
+
1138
+ return (
1139
+ <PromptInputButton
1140
+ className={cn(
1141
+ "relative transition-all duration-200",
1142
+ isListening && "animate-pulse bg-accent text-accent-foreground",
1143
+ className
1144
+ )}
1145
+ disabled={!recognition}
1146
+ onClick={toggleListening}
1147
+ {...props}
1148
+ >
1149
+ <MicIcon className="size-4" />
1150
+ </PromptInputButton>
1151
+ );
1152
+ };
1153
+
1154
+ export type PromptInputModelSelectProps = ComponentProps<typeof Select>;
1155
+
1156
+ export const PromptInputModelSelect = (props: PromptInputModelSelectProps) => (
1157
+ <Select {...props} />
1158
+ );
1159
+
1160
+ export type PromptInputModelSelectTriggerProps = ComponentProps<
1161
+ typeof SelectTrigger
1162
+ >;
1163
+
1164
+ export const PromptInputModelSelectTrigger = ({
1165
+ className,
1166
+ ...props
1167
+ }: PromptInputModelSelectTriggerProps) => (
1168
+ <SelectTrigger
1169
+ className={cn(
1170
+ "border-none bg-transparent font-medium text-muted-foreground shadow-none transition-colors",
1171
+ 'hover:bg-accent hover:text-foreground aria-expanded:bg-accent aria-expanded:text-foreground',
1172
+ className
1173
+ )}
1174
+ {...props}
1175
+ />
1176
+ );
1177
+
1178
+ export type PromptInputModelSelectContentProps = ComponentProps<
1179
+ typeof SelectContent
1180
+ >;
1181
+
1182
+ export const PromptInputModelSelectContent = ({
1183
+ className,
1184
+ ...props
1185
+ }: PromptInputModelSelectContentProps) => (
1186
+ <SelectContent className={cn(className)} {...props} />
1187
+ );
1188
+
1189
+ export type PromptInputModelSelectItemProps = ComponentProps<typeof SelectItem>;
1190
+
1191
+ export const PromptInputModelSelectItem = ({
1192
+ className,
1193
+ ...props
1194
+ }: PromptInputModelSelectItemProps) => (
1195
+ <SelectItem className={cn(className)} {...props} />
1196
+ );
1197
+
1198
+ export type PromptInputModelSelectValueProps = ComponentProps<
1199
+ typeof SelectValue
1200
+ >;
1201
+
1202
+ export const PromptInputModelSelectValue = ({
1203
+ className,
1204
+ ...props
1205
+ }: PromptInputModelSelectValueProps) => (
1206
+ <SelectValue className={cn(className)} {...props} />
1207
+ );
1208
+
1209
+ export type PromptInputHoverCardProps = ComponentProps<typeof HoverCard>;
1210
+
1211
+ export const PromptInputHoverCard = ({
1212
+ openDelay = 0,
1213
+ closeDelay = 0,
1214
+ ...props
1215
+ }: PromptInputHoverCardProps) => (
1216
+ <HoverCard closeDelay={closeDelay} openDelay={openDelay} {...props} />
1217
+ );
1218
+
1219
+ export type PromptInputHoverCardTriggerProps = ComponentProps<
1220
+ typeof HoverCardTrigger
1221
+ >;
1222
+
1223
+ export const PromptInputHoverCardTrigger = (
1224
+ props: PromptInputHoverCardTriggerProps
1225
+ ) => <HoverCardTrigger {...props} />;
1226
+
1227
+ export type PromptInputHoverCardContentProps = ComponentProps<
1228
+ typeof HoverCardContent
1229
+ >;
1230
+
1231
+ export const PromptInputHoverCardContent = ({
1232
+ align = "start",
1233
+ ...props
1234
+ }: PromptInputHoverCardContentProps) => (
1235
+ <HoverCardContent align={align} {...props} />
1236
+ );
1237
+
1238
+ export type PromptInputTabsListProps = HTMLAttributes<HTMLDivElement>;
1239
+
1240
+ export const PromptInputTabsList = ({
1241
+ className,
1242
+ ...props
1243
+ }: PromptInputTabsListProps) => <div className={cn(className)} {...props} />;
1244
+
1245
+ export type PromptInputTabProps = HTMLAttributes<HTMLDivElement>;
1246
+
1247
+ export const PromptInputTab = ({
1248
+ className,
1249
+ ...props
1250
+ }: PromptInputTabProps) => <div className={cn(className)} {...props} />;
1251
+
1252
+ export type PromptInputTabLabelProps = HTMLAttributes<HTMLHeadingElement>;
1253
+
1254
+ export const PromptInputTabLabel = ({
1255
+ className,
1256
+ ...props
1257
+ }: PromptInputTabLabelProps) => (
1258
+ <h3
1259
+ className={cn(
1260
+ "mb-2 px-3 font-medium text-muted-foreground text-xs",
1261
+ className
1262
+ )}
1263
+ {...props}
1264
+ />
1265
+ );
1266
+
1267
+ export type PromptInputTabBodyProps = HTMLAttributes<HTMLDivElement>;
1268
+
1269
+ export const PromptInputTabBody = ({
1270
+ className,
1271
+ ...props
1272
+ }: PromptInputTabBodyProps) => (
1273
+ <div className={cn("space-y-1", className)} {...props} />
1274
+ );
1275
+
1276
+ export type PromptInputTabItemProps = HTMLAttributes<HTMLDivElement>;
1277
+
1278
+ export const PromptInputTabItem = ({
1279
+ className,
1280
+ ...props
1281
+ }: PromptInputTabItemProps) => (
1282
+ <div
1283
+ className={cn(
1284
+ "flex items-center gap-2 px-3 py-2 text-xs hover:bg-accent",
1285
+ className
1286
+ )}
1287
+ {...props}
1288
+ />
1289
+ );
1290
+
1291
+ export type PromptInputCommandProps = ComponentProps<typeof Command>;
1292
+
1293
+ export const PromptInputCommand = ({
1294
+ className,
1295
+ ...props
1296
+ }: PromptInputCommandProps) => <Command className={cn(className)} {...props} />;
1297
+
1298
+ export type PromptInputCommandInputProps = ComponentProps<typeof CommandInput>;
1299
+
1300
+ export const PromptInputCommandInput = ({
1301
+ className,
1302
+ ...props
1303
+ }: PromptInputCommandInputProps) => (
1304
+ <CommandInput className={cn(className)} {...props} />
1305
+ );
1306
+
1307
+ export type PromptInputCommandListProps = ComponentProps<typeof CommandList>;
1308
+
1309
+ export const PromptInputCommandList = ({
1310
+ className,
1311
+ ...props
1312
+ }: PromptInputCommandListProps) => (
1313
+ <CommandList className={cn(className)} {...props} />
1314
+ );
1315
+
1316
+ export type PromptInputCommandEmptyProps = ComponentProps<typeof CommandEmpty>;
1317
+
1318
+ export const PromptInputCommandEmpty = ({
1319
+ className,
1320
+ ...props
1321
+ }: PromptInputCommandEmptyProps) => (
1322
+ <CommandEmpty className={cn(className)} {...props} />
1323
+ );
1324
+
1325
+ export type PromptInputCommandGroupProps = ComponentProps<typeof CommandGroup>;
1326
+
1327
+ export const PromptInputCommandGroup = ({
1328
+ className,
1329
+ ...props
1330
+ }: PromptInputCommandGroupProps) => (
1331
+ <CommandGroup className={cn(className)} {...props} />
1332
+ );
1333
+
1334
+ export type PromptInputCommandItemProps = ComponentProps<typeof CommandItem>;
1335
+
1336
+ export const PromptInputCommandItem = ({
1337
+ className,
1338
+ ...props
1339
+ }: PromptInputCommandItemProps) => (
1340
+ <CommandItem className={cn(className)} {...props} />
1341
+ );
1342
+
1343
+ export type PromptInputCommandSeparatorProps = ComponentProps<
1344
+ typeof CommandSeparator
1345
+ >;
1346
+
1347
+ export const PromptInputCommandSeparator = ({
1348
+ className,
1349
+ ...props
1350
+ }: PromptInputCommandSeparatorProps) => (
1351
+ <CommandSeparator className={cn(className)} {...props} />
1352
+ );