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