Files
ImaGen/docs/architecture.md
mAi e22f286024 mAi: #7 - cloud-sync to Supabase Storage + imagen.images
Every successful imagen generate now (a) uploads the PNG to the private
imagen-generated bucket and (b) inserts a row into imagen.images, the
data plane the flexsiebels owner-mode viewer reads from.

Schema, RLS, indexes, bucket and PostgREST exposure landed via four
applied migrations on msupabase: imagen_schema_init,
imagen_schema_grants, imagen_storage_policies, imagen_pgrst_expose
(authenticator role-level ALTER + reload). Owner UUID for m:
ac6c9501-3757-4a6d-8b97-2cff4288382b — documented in the config sample.

Code: new internal/cloud/ package mirroring the internal/usage/ shape.
PostgREST POST against the imagen schema (Accept-Profile + Content-
Profile headers), Storage upload via PUT with x-upsert, retry on 5xx /
transport but not 4xx, owner_user_id required (the column is NOT NULL
and the read-side RLS policy needs it).

Wiring in cmd/imagen/generate.go: --no-cloud flag, output.cloud_sync
config knob (auto|on|off mirroring --preview), $IMAGEN_CLOUD_SYNC env
override. The hook reads the just-written PNG + sidecar from disk and
calls cloud.Sync; failures emit "imagen: cloud sync: <err>" to stderr
without changing exit code, so a Supabase blip never loses the artefact.
output.Outputs grew Date/Slug/Seed fields so storage_path mirrors the
local filename's prefix exactly (no UTC-vs-local drift).

Config: owner_user_id field added; sample comment points at the
auth.users lookup. imagen config validate warns on stderr when
cloud_sync is on/auto but owner_user_id is empty.

Tests: cloud_test.go covers happy path, retry-on-5xx, no-retry-on-4xx,
missing-owner-uuid, missing-date-or-slug, signed URL, and the partial-
success case where the upload landed but the DB insert failed.
generate_test.go covers the precedence chain for cloud-sync mode
resolution. Build + tests clean across the tree.

Real smoke against mRock: generation through flux-schnell-local writes
the local PNG + sidecar AND uploads to imagen-generated/2026-05-11/...
AND inserts into imagen.images. Signed URL round-trips the same bytes.
--no-cloud verified to skip both Storage and DB.
2026-05-11 01:51:09 +02:00

4.1 KiB

ImaGen architecture

ImaGen is intentionally small. The framework owns plumbing; adapters own the upstream API. Each adapter only ever sees its own slice of imagen.yaml.

Layers

        ┌───────────────────────┐
        │   cmd/imagen          │   CLI dispatch
        │   (or HTTP server)    │
        └──────────┬────────────┘
                   │
        ┌──────────▼────────────┐
        │   internal/prompt     │   style preset → prompt suffix
        │   internal/output     │   filename templating, sidecar
        │   internal/config     │   YAML loader, validation
        │   internal/preview    │   tmux-img window spawner
        │   internal/cloud      │   Supabase Storage + imagen.images
        │   internal/usage      │   mai.imagen_usage cost-tracking
        └──────────┬────────────┘
                   │
        ┌──────────▼────────────┐
        │   internal/backend    │   Backend interface + Registry
        └──────────┬────────────┘
                   │
        ┌──────────▼────────────┐
        │   adapters            │   ComfyUI · Replicate · OpenAI · …
        │   (each one register- │   each registers a `type` name on
        │    s in init())       │   `backend.Default` at init time.
        └───────────────────────┘

The Backend contract

type Request struct {
    Prompt         string
    NegativePrompt string
    Width, Height  int
    Steps          int
    Seed           int64
    Style          string
    BackendOpts    map[string]any
}

type Result struct {
    ImageReader io.ReadCloser
    MimeType    string
    Metadata    map[string]any
}

type Backend interface {
    Name() string
    Generate(ctx context.Context, req Request) (*Result, error)
}

Adapters translate Request into whatever the upstream expects. Fields they can't honour (e.g. NegativePrompt on DALL-E) are silently ignored.

Registry

backend.Default holds the process-wide name → constructor map. Each adapter calls backend.Register("<type>", NewX) from its init(). The CLI imports internal/backend (which transitively triggers the mock's init) and any extra adapter packages.

Config flow

imagen.yaml
  backends:
    flux-schnell-local:
      type: comfyui                  ──┐
      base_url: http://mrock:8188      │  framework keeps `type`,
      model: flux1-schnell.safetensors │  hands the rest to the
      default_steps: 4                 │  comfyui adapter as cfg map[string]any
                                     ──┘

The framework never inspects fields below type. That's the adapter's contract with itself, expressed however the adapter wants (typed struct, map lookups, JSON tags — its call).

Output

output:
  directory: ~/Pictures/imagen
  naming: "{date}-{slug}-{seed}.png"
  write_metadata_json: true

Placeholders: {date}, {time}, {slug} (lowercased prompt, alnum-only, truncated to 40 chars), {seed}, {backend}, {ext}. The sidecar JSON contains the prompt, backend instance name, seed, ISO timestamp, and the Result.Metadata map verbatim.

Where adapters fail fast

  • Missing required field in their config block — return an error from the constructor; the CLI surfaces it as imagen: backend "X": <err>.
  • Unset env-var for credentials — same.
  • Network errors during Generate — wrap and return; no retry policy yet (decide per-adapter, or move to a shared retry helper if a pattern emerges).

Out of scope (today)

  • Image post-processing (cropping, watermarking).
  • Cost-tracking (lands with the Replicate adapter, since only API backends bill).
  • Multi-image n>1 per request — backends that support it can expose it via BackendOpts; the framework doesn't have a first-class field yet.