Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

api.generate.image.artifact.ts

34 lines1.3 KB

1import { createFileRoute } from '@tanstack/react-router'
2import { retrieveArtifact, retrieveBlob } from '@tanstack/ai-persistence'
3import { generationServerPersistence } from '../lib/generation-server-persistence'
4​
5/**
6 * Serves a persisted generation artifact's bytes by id, straight from the
7 * `retrieveArtifact` / `retrieveBlob` helpers. `withGenerationPersistence`'s
8 * `artifactUrl` stamps `?id=<artifactId>` onto each stored image, so a restored
9 * run renders its media from this route (our own origin) rather than the
10 * provider's expiring URL.
11 */
12export const Route = createFileRoute('/api/generate/image/artifact')({
13 server: {
14 handlers: {
15 GET: async ({ request }) => {
16 const id = new URL(request.url).searchParams.get('id')
17 if (!id) return new Response('missing id', { status: 400 })
18​
19 const artifact = await retrieveArtifact(generationServerPersistence, id)
20 if (!artifact) return new Response('not found', { status: 404 })
21​
22 const blob = await retrieveBlob(generationServerPersistence, artifact)
23 if (!blob) return new Response('not found', { status: 404 })
24​
25 return new Response(blob.body ?? (await blob.arrayBuffer()), {
26 headers: {
27 'content-type': artifact.mimeType,
28 'content-length': String(artifact.size),
29 },
30 })
31 },
32 },
33 },
34})