Chapter 29 — Image Generation
Part VII — Systems
Headless portrait generation pipeline with pluggable providers.
Package
Section titled “Package”@ai-rpg-engine/image-gen — depends on @ai-rpg-engine/asset-registry for storage.
npm install @ai-rpg-engine/image-genArchitecture
Section titled “Architecture”PortraitRequest → Prompt Builder → Image Provider → Asset Registry ↓ ↓ ↓ genre presets ComfyUI / SVG content-addressed trait flavor placeholder SHA-256 hashThe pipeline converts character metadata into a generation prompt, sends it to a provider, and stores the result in the asset registry. The returned hash becomes the portraitRef on the character build.
Prompt Builder
Section titled “Prompt Builder”buildPortraitPrompt(request) assembles a prompt from:
- Character name and title
- Archetype and discipline class names
- Background origin
- Trait descriptions
- Genre-specific style preset
Example output:
Portrait of Aldric, Grave Warden, Penitent Knight and Occultist,Oath-Breaker origin, known for being Iron Frame and Cursed Blood,dark fantasy oil painting, dramatic lighting, detailed armor and cloth texturesProviders
Section titled “Providers”| Provider | Backend | Dependencies | Use Case |
|---|---|---|---|
PlaceholderProvider | SVG with initials | None | Testing, development |
ComfyUIProvider | ComfyUI HTTP API | ComfyUI server | Local GPU generation |
PlaceholderProvider
Section titled “PlaceholderProvider”Generates deterministic SVG images with character initials on a color-coded background. Always available, zero dependencies.
ComfyUIProvider
Section titled “ComfyUIProvider”Calls ComfyUI’s REST API directly:
- POST workflow to
/prompt - Poll
/history/{id}for completion - Fetch image from
/view
Works with any ComfyUI installation, including comfy-headless.
Custom Provider
Section titled “Custom Provider”Implement the ImageProvider interface:
interface ImageProvider { readonly name: string; generate(prompt: string, opts?: GenerationOptions): Promise<GenerationResult>; isAvailable(): Promise<boolean>;}Genre Style Presets
Section titled “Genre Style Presets”9 built-in presets map genres to art direction:
| Genre | Style Direction |
|---|---|
| fantasy | Dark oil painting, dramatic lighting, medieval textures |
| cyberpunk | Neon lighting, chrome, rain-slicked streets |
| mystery | Victorian noir, gaslight, fog and shadow |
| pirate | Golden age maritime, weathered, dramatic ocean sky |
| horror | Dark illustration, unsettling, muted palette |
| western | Weird west, dusty frontier, supernatural undertones |
Pipeline Functions
Section titled “Pipeline Functions”generatePortrait(request, provider, store)— generate and store a new portraitensurePortrait(request, provider, store)— return existing or generate newbuildPromptPair(request)— get both positive and negative prompts