Open History to see the last commit
1import { sqlitePersistence } from './sqlite-persistence'
2
3let instance: ReturnType<typeof sqlitePersistence> | undefined
4
5/**
6 * Server-side generation persistence for the example app — the counterpart to
7 * `generation-persistence.ts`, which is the client-driven half.
8 *
9 * Backed by the same self-contained `node:sqlite` adapter the persistent-chat
10 * demo uses (`./sqlite-persistence`), in its own database file. It supplies the
11 * three stores generation needs: `generationRuns` (the run record
12 * `reconstructGeneration` reads back) plus `artifacts` + `blobs` (the generated
13 * bytes, so a restored `result` can actually render its image rather than
14 * coming back `null`).
15 *
16 * Durable on purpose. The POST that records a run and the GET that serves its
17 * bytes are separate requests, and an in-memory store would also lose every
18 * artifact whenever Vite re-evaluates this module on HMR — leaving artifact
19 * URLs already on screen to 404 mid-session. On disk, none of that applies:
20 * generated images survive an edit, a restart, and a second worker. `.data/` is
21 * gitignored.
22 *
23 * Lazily opened so importing this module never opens the database in a browser
24 * bundle.
25 */
26export function generationServerPersistence() {
27 return (instance ??= sqlitePersistence({
28 url: './.data/generation.db',
29 migrate: true,
30 }))
31}
32
33/**
34 * Serve URL for a stored artifact — must match `routes/api.artifacts.ts`.
35 *
36 * One route serves every activity's media, so each generation route passes this
37 * as `artifactUrl` and nothing else has to know how bytes are addressed.
38 */
39export function artifactServeUrl(artifactId: string): string {
40 return `/api/artifacts?id=${encodeURIComponent(artifactId)}`
41}