AI Attachment - React Component | Kokonut UI
AI Attachment
A file dropped into the chat uploads, then the assistant reads it page by page. Page citations in the answer light up the page they point at.
Install with
npx shadcn@latest add https://kokonutui.com/r/ai-attachment.json
Usage
With no props it plays the demo. Pass status to drive it from your own upload and parser: uploading with uploaded from 0 to 1, reading with pagesRead, then done or error. The answer shows once it's done and can stream in a part at a time; a part is text or { page }, a citation that lights its page when pointed at. The file chip always shows, since it is the upload; the question bubble only shows when you pass prompt.
"use client";
import AIAttachment, { type AnswerPart } from "@/components/kokonutui/ai-attachment";
export function UploadedFile({
file,
upload,
parse,
answer,
}: {
file: { name: string; size: number; pages: number };
/** From your upload, e.g. XMLHttpRequest progress events. */
upload: { loaded: number; total: number; done: boolean };
/** From your parser, as it works through the pages. */
parse: { pagesRead: number; done: boolean; error?: string };
answer: AnswerPart[];
}) {
let status: "uploading" | "reading" | "done" | "error" = "uploading";
if (parse.error) status = "error";
else if (parse.done) status = "done";
else if (upload.done) status = "reading";
return (
<AIAttachment
answer={answer}
error={parse.error}
file={file}
pagesRead={parse.pagesRead}
status={status}
uploaded={upload.loaded / upload.total}
/>
);
}
Here it's fed an upload, a page-by-page read and a streamed answer. Play with an error stops on a page with no text.
Installation
AI Attachment is a free, open-source React component in the Kokonut UI AI collection. Add it with the shadcn CLI, which copies the source into components/kokonutui/ai-attachment.tsx, so the code is yours to change.
npx shadcn@latest add @kokonutui/ai-attachment
Or add it by hand: install its packages, then copy the source from above into components/kokonutui/ai-attachment.tsx.
npm install motion
Props
fileAttachedFileDefaultdemo data
promptstring- The user's message under the file. Only the demo draws one by default.
answerAnswerPart[]- The answer, with page citations. Shown once the file is read; it can stream.
statusAttachmentStatus
uploadednumber
pagesReadnumber
durationnumber
errorstring
uploadMsnumber- Default
1400
pageMsnumber- Default
260
classNamestring
AttachedFile
namerequired -stringsizerequired -numberpagesrequired -number
AnswerPart
A run of text, or a citation to a page (1-based).
type AnswerPart = string | { page: number };
AttachmentStatus
Where the file is: on its way up, being read, read, or failed.
type AttachmentStatus = "uploading" | "reading" | "done" | "error";
Dependencies
| Needs | Version | Comes from |
|---|---|---|
motion |
^12.23.25 |
npm, installed by the CLI |
cn from lib/utils.ts |
shadcn init | |
| React | 19 |
Your app |
| Tailwind CSS | 4 |
Your app |
No provider, font or global CSS to set up: the file works as soon as it lands.