Skip to content

Automatic tracking of file uploads #262

@cmccormick-ghd

Description

@cmccormick-ghd

## Current behaviour
File upload is supported through the usePromptInputAttachments but tracking the uploaded files themselves has to be done manually. We've had to implement our own solution for tracking which files are uploading and what their status is (uploaded vs uploading vs failed).

## Proposed solution
It would be great if there was a way of handling this automatically, currently we've implemented a tracker for uploading files so it would be great to have this as part of the usePromptInputAttachments hook (https://github.com/vercel/ai-elements/blob/main/packages/elements/src/prompt-input.tsx) and have it automatically update based on the files' status. For example:

export const iuu = () => {
  ...
  return { context, uploading, uploaded, uploadFailures };
};

Potential code change (context will also need to be adjusted to work with this):

export const usePromptInputAttachments = () => {
  // Dual-mode: prefer provider if present, otherwise use local
  const provider = useOptionalProviderAttachments();
  const local = useContext(LocalAttachmentsContext);
  const context = provider ?? local;
  if (!context) {
    throw new Error(
      "usePromptInputAttachments must be used within a PromptInput or PromptInputProvider"
    );
  }

  + const uploading = context.files.filter((file) => file.type === "uploading");
  + const uploads = context.files.filter((file) => file.type === "uploaded");
  + const uploadFailures = context.files.filter((file) => file.type === "upload_error");

  - return context;
  + return { context, uploading, uploads, uploadFailures };
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions