AI files
Attachments and generated files for AI chats in a private bucket, provider file references with expiry, and versioned documents with suggested edits.
The ai-files block stores the files of an AI assistant: what a user
attaches to a message, what a model generates (images, speech), the
reference a provider's file API returned for a file, and documents the
assistant writes beside the chat, with their versions in the
versions block. Messages point
at a file with a supabase-storage:// URL, so a stored chat never holds a
signed URL that expires or a file someone else owns.
better-supabase add ai-files # adds tenant, access and versions as well| Table | Holds |
|---|---|
ai_files | One row per object: owner, tenant, chat, media_type, byte_size, sha256, status, source |
ai_provider_files | A provider's file id or file URI per file, with expires_at |
ai_documents | Documents per owner and chat: kind (text, code, sheet, image), title, the version |
ai_suggestions | Suggested edits on a version, and whether the owner accepted them |
Objects live in the private ai-files bucket at
{organization_id}/{owner_id}/{file_id}/{filename}. The storage policies
accept an upload only to the path of a pending record the caller reserved,
and allow a read to the owner, an ai_files.manage of the tenant and, with
the ai-chat module installed, anyone who can read
the file's chat.
| Permission | Lets a member | Default roles |
|---|---|---|
ai_files.upload | upload files and create documents in the tenant | owner, admin, member |
ai_files.manage | read and delete every file and document in the tenant | owner, admin |
The keys are the ai-chat keys by default; rename them with
blocks["ai-files"].permissions.upload and .manage.
| Option | Default | Sets |
|---|---|---|
bucket | ai-files | The bucket id |
maxSize | 50 MB | The largest file in bytes, in the table and the bucket |
allowedMimeTypes | any | Media types the bucket and reserve_ai_file accept (image/*) |
pendingTtl | 1 day | How long purge keeps an upload that never finished |
Server
Server code calls the ai-files instance on db, in the caller's organization. With gen.hooks set, better-supabase gen writes the hooks:
export const { useList: useAiFiles } = defineDataHooks(
"aiFiles",
(db: AppDb) => db.aiFiles,
);const rows = await db.aiFiles.list({ limit: 50 }).orThrow();"use client";
import { useAiFiles } from "@/lib/supabase/hooks";
const rows = useAiFiles({ limit: 50 });transport and storage act as the user, so the table and bucket
policies apply. service and serviceStorage act as the service role for
what only the server writes: generated files, provider references and the
purge.
| Group | Methods |
|---|---|
files | upload, confirm, get, resolve, list, sign, read, remove, store, purge |
providerFiles | get, upsert, expiring |
documents | create, get, update, rollback, versions, remove, suggest, suggestions, resolve |
Every method returns a Result. A file the caller can't see is
not_found, the same as a file that doesn't exist.
Paging files
files.page pages the tenant's files by keyset (ADR 1077), with facets and count. Sorts are createdAt (newest first), filename and size. chatId, status, source and mediaType filter by a list of values, and q searches the file name. files.list keeps working and is deprecated; read a chat's files with filters: { chatId: [id] }. A cursor belongs to the sort it came from, so start again from the first page when sort changes. A sort, filter, facet or text the entity does not declare returns an invalid_input error. Block lists describes the options every block list shares.
Uploads
const { file, token } = await files.files
.upload(organizationId, {
filename: "plan.pdf",
mediaType: "application/pdf",
size: blob.size,
chatId,
})
.orThrow();
await supabase.storage
.from(file.bucket)
.uploadToSignedUrl(file.path, token, blob);
await files.files.confirm(file.id).orThrow();
const part = { type: "file", mediaType: file.mediaType, url: aiFileUrl(file) };upload reserves the record and returns a signed upload URL for its path.
For files over 6 MB, upload to the same path with TUS
(/storage/v1/upload/resumable) instead; the policies are the same.
confirm checks that the object exists and takes its stored size. In a
writable support session
it first claims the upload for the user the admin acts as; a refused claim
returns forbidden (AI_FILE_FORBIDDEN) or not_found
(AI_FILE_NOT_UPLOADED) and leaves the file pending. Run
purge from a job: it deletes pending uploads older than pendingTtl,
expired files and the files of deleted chats, then removes their objects.
Reading files
resolve(url) looks a supabase-storage:// URL up as the caller and fails
for a file they can't read, so a user can't send a model another user's
file by pasting its URL. read returns the bytes and refuses files over
maxBytes (50 MB by default). sign(messages) replaces the storage URLs of
file parts with signed URLs that expire after downloadTtl seconds (300 by
default), for a browser or a provider that fetches URLs itself.
Documents
const doc = await files.documents
.create(organizationId, { kind: "code", title: "main.ts", content, chatId })
.orThrow();
await files.documents
.update(doc.id, { content: next, expectedVersion: doc.version })
.orThrow();update writes a new version; with expectedVersion it fails with
conflict (AI_DOCUMENT_CONFLICT) when another writer got there first.
An edit with the same content and message as the current version writes no
version. rollback(id, version) writes an old version as the newest one, so
the history stays linear. suggest records an edit on the current version and
resolve accepts or rejects it; accepting doesn't change the document, the
client writes the new version.
Versions
A document's versions are rows of the versions
table with subject_type ai_document and the document id as
subject_id. content holds { text, messageId } and storage_path the
path of an image or a large document. The versions block numbers them and
keeps the newest blocks.versions.options.keep (default 50); older ones are
deleted. Deleting a document deletes its versions.
AI documents are private to their owner, an ai_files.manage of the tenant
and the readers of their chat, so the versions block hides them: its
functions refuse the ai_document subject type and a restrictive policy
keeps the rows out of direct reads. Read them with documents.get and
documents.versions.
AI SDK
better-supabase/ai-sdk/files reads these files in
experimental_download, stores generated files and caches provider file
references.
Your own fields
Columns you add to ai_files with alter table in your own schema files are fields of the block (details):,
keyed by column name with their SQL type. Pass createAiFiles a Standard Schema
for them as fields: files.upload, files.store take them next to the block's own keys and
validate them (a bad value is a validation error), and the file reads return them
parsed. A column the module already uses is refused. See
your own fields.
const client = createAiFiles({
transport,
fields: z.object({ priority: z.number().int().positive().nullable() }),
});Without the client
db.aiFiles is this client. Code that does not go through bsb.db() builds it with a transport and an organization id:
import { createAiFiles, rpcTransport } from "better-supabase/blocks/ai-files";
export const aiFilesFor = (supabase: SupabaseClient, admin: SupabaseClient) =>
createAiFiles({
transport: rpcTransport(supabase),
storage: supabase.storage,
service: rpcTransport(admin),
serviceStorage: admin.storage,
});Last updated on