Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/proud-pots-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ai-elements": minor
---

feat(prompt-input): Add file property to attachment messages for persistent storage
18 changes: 10 additions & 8 deletions packages/elements/src/prompt-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ import {
// ============================================================================

export type AttachmentsContext = {
files: (FileUIPart & { id: string })[];
files: (FileUIPart & { id: string, file?: File })[];
add: (files: File[] | FileList) => void;
remove: (id: string) => void;
clear: () => void;
Expand Down Expand Up @@ -151,7 +151,7 @@ export function PromptInputProvider({

// ----- attachments state (global when wrapped)
const [attachmentFiles, setAttachmentFiles] = useState<
(FileUIPart & { id: string })[]
(FileUIPart & { id: string, file?: File })[]
>([]);
const fileInputRef = useRef<HTMLInputElement | null>(null);
const openRef = useRef<() => void>(() => {});
Expand All @@ -170,6 +170,7 @@ export function PromptInputProvider({
url: URL.createObjectURL(file),
mediaType: file.type,
filename: file.name,
file,
}))
)
);
Expand Down Expand Up @@ -277,7 +278,7 @@ export const usePromptInputAttachments = () => {
};

export type PromptInputAttachmentProps = HTMLAttributes<HTMLDivElement> & {
data: FileUIPart & { id: string };
data: FileUIPart & { id: string; file?: File };
className?: string;
};

Expand Down Expand Up @@ -376,7 +377,7 @@ export type PromptInputAttachmentsProps = Omit<
HTMLAttributes<HTMLDivElement>,
"children"
> & {
children: (attachment: FileUIPart & { id: string }) => ReactNode;
children: (attachment: FileUIPart & { id: string; file?: File }) => ReactNode;
};

export function PromptInputAttachments({
Expand Down Expand Up @@ -429,7 +430,7 @@ export const PromptInputActionAddAttachments = ({

export type PromptInputMessage = {
text: string;
files: FileUIPart[];
files: (FileUIPart & { file?: File })[];
};

export type PromptInputProps = Omit<
Expand Down Expand Up @@ -477,7 +478,7 @@ export const PromptInput = ({
const formRef = useRef<HTMLFormElement | null>(null);

// ----- Local attachments (only used when no provider)
const [items, setItems] = useState<(FileUIPart & { id: string })[]>([]);
const [items, setItems] = useState<(FileUIPart & { id: string; file?: File })[]>([]);
const files = usingProvider ? controller.attachments.files : items;

// Keep a ref to files for cleanup on unmount (avoids stale closure)
Expand Down Expand Up @@ -537,14 +538,15 @@ export const PromptInput = ({
message: "Too many files. Some were not added.",
});
}
const next: (FileUIPart & { id: string })[] = [];
const next: (FileUIPart & { id: string, file?: File })[] = [];
for (const file of capped) {
next.push({
id: nanoid(),
type: "file",
url: URL.createObjectURL(file),
mediaType: file.type,
filename: file.name,
file,
});
}
return prev.concat(next);
Expand Down Expand Up @@ -729,7 +731,7 @@ export const PromptInput = ({
return item;
})
)
.then((convertedFiles: FileUIPart[]) => {
.then((convertedFiles: (FileUIPart & { file?: File })[]) => {
try {
const result = onSubmit({ text, files: convertedFiles }, event);

Expand Down