Repository navigation
Fix/prompt input hosted file uploads #523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AbbasFullstack
wants to merge
5
commits into
vercel:main
Choose a base branch
from
AbbasFullstack:fix/prompt-input-hosted-file-uploads
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+176
−32
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2680f1e
feat: support hosted PromptInput attachment uploads
AbbasFullstack a30ae68
fix: handle hosted uploads with PromptInputProvider
AbbasFullstack a395713
fix: preserve provider attachment behavior without upload callback
AbbasFullstack eb634e7
test: cover hosted PromptInput uploads and pending state
AbbasFullstack 20fbd5c
style: format hosted attachment upload handling
AbbasFullstack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -185,6 +185,8 @@ export interface AttachmentsContext { | |
| clear: () => void; | ||
| openFileDialog: () => void; | ||
| fileInputRef: RefObject<HTMLInputElement | null>; | ||
| addUploaded: (files: FileUIPart[]) => void; | ||
| isUploading?: boolean; | ||
| } | ||
|
|
||
| export interface TextInputContext { | ||
|
|
@@ -261,6 +263,13 @@ export const PromptInputProvider = ({ | |
| // oxlint-disable-next-line eslint(no-empty-function) | ||
| const openRef = useRef<() => void>(() => {}); | ||
|
|
||
| const addUploaded = useCallback((files: FileUIPart[]) => { | ||
| setAttachmentFiles((prev) => [ | ||
| ...prev, | ||
| ...files.map((file) => ({ ...file, id: nanoid() })), | ||
| ]); | ||
| }, []); | ||
|
|
||
| const add = useCallback((files: File[] | FileList) => { | ||
| const incoming = [...files]; | ||
| if (incoming.length === 0) { | ||
|
|
@@ -326,13 +335,14 @@ export const PromptInputProvider = ({ | |
| const attachments = useMemo<AttachmentsContext>( | ||
| () => ({ | ||
| add, | ||
| addUploaded, | ||
| clear, | ||
| fileInputRef, | ||
| files: attachmentFiles, | ||
| openFileDialog, | ||
| remove, | ||
| }), | ||
| [attachmentFiles, add, remove, clear, openFileDialog] | ||
| [attachmentFiles, add, addUploaded, remove, clear, openFileDialog] | ||
| ); | ||
|
|
||
| const __registerFileInput = useCallback( | ||
|
|
@@ -501,6 +511,8 @@ export type PromptInputProps = Omit< | |
| maxFiles?: number; | ||
| // bytes | ||
| maxFileSize?: number; | ||
| onFileAdd?: (file: File) => Promise<string>; | ||
| onFileRemove?: (fileUrl: string) => Promise<void>; | ||
| onError?: (err: { | ||
| code: "max_files" | "max_file_size" | "accept"; | ||
| message: string; | ||
|
|
@@ -520,6 +532,8 @@ export const PromptInput = ({ | |
| maxFiles, | ||
| maxFileSize, | ||
| onError, | ||
| onFileAdd, | ||
| onFileRemove, | ||
| onSubmit, | ||
| children, | ||
| ...props | ||
|
|
@@ -534,6 +548,7 @@ export const PromptInput = ({ | |
|
|
||
| // ----- Local attachments (only used when no provider) | ||
| const [items, setItems] = useState<(FileUIPart & { id: string })[]>([]); | ||
| const [isUploading, setIsUploading] = useState(false); | ||
| const files = usingProvider ? controller.attachments.files : items; | ||
|
|
||
| // ----- Local referenced sources (always local to PromptInput) | ||
|
|
@@ -597,33 +612,53 @@ export const PromptInput = ({ | |
| return; | ||
| } | ||
|
|
||
| setItems((prev) => { | ||
| const capacity = | ||
| typeof maxFiles === "number" | ||
| ? Math.max(0, maxFiles - prev.length) | ||
| : undefined; | ||
| const capped = | ||
| typeof capacity === "number" ? sized.slice(0, capacity) : sized; | ||
| if (typeof capacity === "number" && sized.length > capacity) { | ||
| onError?.({ | ||
| code: "max_files", | ||
| message: "Too many files. Some were not added.", | ||
| }); | ||
| } | ||
| const next: (FileUIPart & { id: string })[] = []; | ||
| for (const file of capped) { | ||
| next.push({ | ||
| filename: file.name, | ||
| id: nanoid(), | ||
| mediaType: file.type, | ||
| type: "file", | ||
| url: URL.createObjectURL(file), | ||
| }); | ||
| } | ||
| return [...prev, ...next]; | ||
| }); | ||
| const capacity = | ||
| typeof maxFiles === "number" | ||
| ? Math.max(0, maxFiles - files.length) | ||
| : undefined; | ||
| const capped = | ||
| typeof capacity === "number" ? sized.slice(0, capacity) : sized; | ||
| if (typeof capacity === "number" && sized.length > capacity) { | ||
| onError?.({ | ||
| code: "max_files", | ||
| message: "Too many files. Some were not added.", | ||
| }); | ||
| } | ||
| if (!capped.length) return; | ||
| setIsUploading(!!onFileAdd); | ||
| Promise.all( | ||
| capped.map(async (file) => ({ | ||
| filename: file.name, | ||
| mediaType: file.type, | ||
| type: "file" as const, | ||
| url: onFileAdd ? await onFileAdd(file) : URL.createObjectURL(file), | ||
| })) | ||
| ) | ||
| .then((uploaded) => { | ||
| if (usingProvider) { | ||
| controller?.attachments.addUploaded(uploaded); | ||
| } else { | ||
| setItems((prev) => [ | ||
| ...prev, | ||
| ...uploaded.map((file) => ({ ...file, id: nanoid() })), | ||
| ]); | ||
| } | ||
| }) | ||
| .catch(() => { | ||
| // Keep existing attachments if an upload fails. | ||
|
Comment on lines
+629
to
+648
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }) | ||
| .finally(() => setIsUploading(false)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }, | ||
| [matchesAccept, maxFiles, maxFileSize, onError] | ||
| [ | ||
| matchesAccept, | ||
| maxFiles, | ||
| maxFileSize, | ||
| onError, | ||
| files.length, | ||
| onFileAdd, | ||
| usingProvider, | ||
| controller, | ||
| ] | ||
| ); | ||
|
|
||
| const removeLocal = useCallback( | ||
|
|
@@ -676,10 +711,37 @@ export const PromptInput = ({ | |
| } | ||
|
|
||
| if (capped.length > 0) { | ||
| controller?.attachments.add(capped); | ||
| if (!onFileAdd) { | ||
| controller?.attachments.add(capped); | ||
| return; | ||
| } | ||
| setIsUploading(true); | ||
| Promise.all( | ||
| capped.map(async (file) => ({ | ||
| filename: file.name, | ||
| mediaType: file.type, | ||
| type: "file" as const, | ||
| url: await onFileAdd(file), | ||
| })) | ||
| ) | ||
| .then((uploaded) => { | ||
| controller?.attachments.addUploaded(uploaded); | ||
| }) | ||
| .catch(() => { | ||
| // Keep existing attachments if an upload fails. | ||
| }) | ||
| .finally(() => setIsUploading(false)); | ||
| } | ||
| }, | ||
| [matchesAccept, maxFileSize, maxFiles, onError, files.length, controller] | ||
| [ | ||
| matchesAccept, | ||
| maxFileSize, | ||
| maxFiles, | ||
| onError, | ||
| files.length, | ||
| controller, | ||
| onFileAdd, | ||
| ] | ||
| ); | ||
|
|
||
| const clearAttachments = useCallback( | ||
|
|
@@ -703,7 +765,16 @@ export const PromptInput = ({ | |
| ); | ||
|
|
||
| const add = usingProvider ? addWithProviderValidation : addLocal; | ||
| const remove = usingProvider ? controller.attachments.remove : removeLocal; | ||
| const remove = useCallback((id: string) => { | ||
| const found = files.find((file) => file.id === id); | ||
| if (found?.url && !found.url.startsWith("blob:") && onFileRemove) { | ||
| void onFileRemove(found.url).catch(() => { | ||
| // UI removal should not be blocked by remote cleanup failures. | ||
| }); | ||
| } | ||
| if (usingProvider) controller.attachments.remove(id); | ||
| else removeLocal(id); | ||
| }, [files, onFileRemove, usingProvider, controller, removeLocal]); | ||
| const openFileDialog = usingProvider | ||
| ? controller.attachments.openFileDialog | ||
| : openFileDialogLocal; | ||
|
|
@@ -817,10 +888,15 @@ export const PromptInput = ({ | |
| clear: clearAttachments, | ||
| fileInputRef: inputRef, | ||
| files: files.map((item) => ({ ...item, id: item.id })), | ||
| isUploading, | ||
| addUploaded: (uploaded) => { | ||
| if (usingProvider) controller?.attachments.addUploaded(uploaded); | ||
| else setItems((prev) => [...prev, ...uploaded.map((file) => ({ ...file, id: nanoid() }))]); | ||
| }, | ||
| openFileDialog, | ||
| remove, | ||
| }), | ||
| [files, add, remove, clearAttachments, openFileDialog] | ||
| [files, add, remove, clearAttachments, openFileDialog, isUploading, usingProvider, controller] | ||
| ); | ||
|
|
||
| const refsCtx = useMemo<ReferencedSourcesContext>( | ||
|
|
@@ -844,6 +920,7 @@ export const PromptInput = ({ | |
| const handleSubmit: FormEventHandler<HTMLFormElement> = useCallback( | ||
| async (event) => { | ||
| event.preventDefault(); | ||
| if (isUploading) return; | ||
|
|
||
| const form = event.currentTarget; | ||
| const text = usingProvider | ||
|
|
@@ -899,7 +976,7 @@ export const PromptInput = ({ | |
| // Don't clear on error - user may want to retry | ||
| } | ||
| }, | ||
| [usingProvider, controller, files, onSubmit, clear] | ||
| [usingProvider, controller, files, onSubmit, clear, isUploading] | ||
| ); | ||
|
|
||
| // Render with or without local provider | ||
|
|
@@ -1223,6 +1300,7 @@ export const PromptInputSubmit = ({ | |
| children, | ||
| ...props | ||
| }: PromptInputSubmitProps) => { | ||
| const attachments = usePromptInputAttachments(); | ||
| const isGenerating = status === "submitted" || status === "streaming"; | ||
|
|
||
| let Icon = <CornerDownLeftIcon className="size-4" />; | ||
|
|
@@ -1251,6 +1329,7 @@ export const PromptInputSubmit = ({ | |
| <InputGroupButton | ||
| aria-label={isGenerating ? "Stop" : "Submit"} | ||
| className={cn(className)} | ||
| disabled={attachments.isUploading || props.disabled} | ||
| onClick={handleClick} | ||
| size={size} | ||
| type={isGenerating && onStop ? "button" : "submit"} | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With maxFiles={1}, selecting a second file while the first upload is pending uploads and appends both files, exceeding the limit in both local and provider modes.