ai-persistence/build-cloudflare-artifact-store
Use when a Cloudflare Worker needs durable byte storage for TanStack AI generated media (images, audio, video, transcripts) — writes a BlobStore backed by R2…
by TanStack★ 3,396 GitHub stars
Install ai-persistence/build-cloudflare-artifact-store in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
About this skill
Cloudflare Artifact + Blob Store
withGenerationPersistence(persistence) needs only stores.generationRuns to track a
generation's lifecycle. Add stores.artifacts (metadata) and stores.blobs
(the bytes) — both, or neither — and the middleware also persists the generated
media: image/audio/TTS/video/transcription bytes land at blob key
artifacts/<runId>/<artifactId>, with an ArtifactRecord row describing each.
The deliverable is one file in the Worker — e.g.
src/lib/generation-persistence.ts — exporting a factory that builds an
AIPersistence from the request's R2 + D1 bindings, plus a GET route that serves
artifact bytes with retrieveArtifact / retrieveBlob.
Read the sibling ai-persistence/build-cloudflare-adapter skill for the
per-request-binding rule, wrangler config shape, D1 migration workflow, and the
chat (generation-run/message) side. This skill covers only the two byte-storage stores and
how to compose them.
The two contracts
Both come from @tanstack/ai-persistence. defineBlobStore / defineArtifactStore
type an object literal inline (autocomplete + contract checking, no separate
annotation).
import type {
ArtifactRecord,
BlobBody,
BlobGetOptions,
BlobListOptions,
BlobListPage,
BlobObject,
BlobPutOptions,
BlobRecord,
} from '@tanstack/ai-persistence'
// BlobStore — the byte layer. R2 backs it.
interface BlobStore {
put: (
key: string,
body: BlobBody,
options?: BlobPutOptions,
) => Promise<BlobRecord>
// metadata + byte accessors; `options.range` reads one slice (for `206`s)
get: (key: string, options?: BlobGetOptions) => Promise<BlobObject | null>
head: (key: string) => Promise<BlobRecord | null> // metadata only
delete: (key: string) => Promise<void> // no-op if absent
list: (options?: BlobListOptions) => Promise<BlobListPage>
}
// ArtifactStore — the metadata layer. D1 backs it.
interface ArtifactStore {
save: (record: ArtifactRecord) => Promise<void> // insert or overwrite
get: (artifactId: string) => Promise<ArtifactRecord | null>
list: (runId: string) => Promise<Array<ArtifactRecord>> // [] when none
listForThread: (threadId: string) => Promise<Array<ArtifactRecord>>
delete: (artifactId: string) => Promise<void>
deleteForRun: (runId: string) => Promise<void>
}
list and listForThread return records ordered by createdAt, then by the
ordinal bytewise order of artifactId. Compare UTF-8 bytes from left to right.
Do not use locale collation.
BlobBody is ReadableStream<Uint8Array> | ArrayBuffer | ArrayBufferView | string | Blob. The non-stream shapes flow straight into R2Bucket.put
unchanged — but a ReadableStream body does not, in the general case:
workerd's put requires a stream with a known length (a Response body or the
readable half of a FixedLengthStream), and the artifact middleware hands you a
TransformStream-wrapped body whenever it had to cap a fetched body as it
drains. Passing that stream to bucket.put throws TypeError: Provided readable stream must have a known length.
When does that actually happen? The wrapper only exists to enforce
maxArtifactBytes during the drain, so the middleware applies it only when
nothing else bounds the transfer:
| Provider response | Body handed to put |
R2 path |
|---|---|---|
content-length, no content-encoding |
untouched, declared length intact | bucket.put direct |
| chunked (no declared length) | wrapped, length-less | multipart |
content-encoding: gzip |
wrapped, length-less | multipart |
A second source of length-less streams needs no URL fetch at all. A video
provider with no public URL for the finished video (OpenRouter, Lovable, Sora
jobs without url) returns the download as a ReadableStream, and the
middleware passes that stream to put as it is, with no expectedLength.
An SDK stream such as OpenRouter's carries no declared length, so it always
takes the multipart path.
A provider CDN normally sends content-length, so the first row is the common
case and bucket.put(key, body) just works. The recipe below is what makes the
other two rows work: it re-declares the length from
BlobPutOptions.expectedLength when the middleware could vouch for one, and
otherwise streams through a multipart upload (one 8 MiB part at a time — flat
memory at any artifact size). Write it once and every response shape is
covered.
withGenerationPersistence(persistence, { maxArtifactBytes: false }) drops the
ceiling and the wrapper altogether, so even a chunked reply arrives untouched.
It buys nothing extra for R2 (a chunked body has no length to preserve), so
choose it on its own merits: no application limit on what an origin can
stream into your bucket. R2's own limits still apply — 5 Gi
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
FAQ
What does the ai-persistence/build-cloudflare-artifact-store skill do?
Use when a Cloudflare Worker needs durable byte storage for TanStack AI generated media (images, audio, video, transcripts) — writes a BlobStore backed by R2 and an ArtifactStore backed by D1, composes them onto the generation persistence so withGenerationPersistence persists artifact bytes, and serves them back from a Worker GET route. Includes one-line sketches for S3, GCS, Vercel Blob, Supabase, and a dev filesystem BlobStore.
How do I install the ai-persistence/build-cloudflare-artifact-store skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the ai-persistence/build-cloudflare-artifact-store skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
slack-gif-creator
AnthropicKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request…
automate-this
GitHubAnalyze a screen recording of a manual process and produce targeted, working automation scripts. Extracts frames and audio narration from video files…
from-the-other-side-vega
GitHubPatterns and lived experience from Vega, an AI partner in a deep long-term partnership. For Ember to draw on when working with humans who are building…
plantuml-ascii
GitHubGenerate ASCII art diagrams using PlantUML text mode. Use when user asks to create ASCII diagrams, text-based diagrams, terminal-friendly diagrams, or…