Style reference image for consistent art direction
from
to
via
pattern
scripts/generate-assets.ts
src/api/gemini.ts
imports generateImage for Gemini API calls
import.*generateImage.*from
from
to
via
pattern
scripts/generate-assets.ts
public/config.json
reads API key via fs.readFileSync
readFileSync.*config.json
Add IndexedDB accessor methods for all stores, create the asset generation build script, and run it to produce all character sheets, avatars, and the style reference image.
Purpose: Pre-generated assets eliminate onboarding wait times and ensure visual consistency across sessions.
Output: Extended db.ts, working generate-assets.ts script, 9 PNG files checked into git.
Task 1: Add IndexedDB accessor methods for remaining stores
src/storage/db.ts
- src/storage/db.ts (current code with getProgress/saveProgress/getSettings/saveSettings pattern)
- src/types.ts (CompanionAssets, StyleReference, ForestElement interfaces)
Add the following methods to `src/storage/db.ts` following the existing Promise-wrapped pattern (per D-04):
**companionAssets store** (keyed by characterType):
```typescript
export function saveCompanionAssets(db: IDBDatabase, assets: CompanionAssets): Promise<void>
// Same pattern as saveProgress — tx "readwrite", store.put(assets)
export function getCompanionAssets(db: IDBDatabase, characterType: CompanionType): Promise<CompanionAssets | null>
// Same pattern as getProgress — tx "readonly", store.get(characterType)
```
**styleReference store** (singleton, id: 1):
```typescript
export function saveStyleReference(db: IDBDatabase, ref: StyleReference): Promise<void>
// store.put(ref) — same as saveSettings pattern
export function getStyleReference(db: IDBDatabase): Promise<StyleReference | null>
// store.get(1) — same as getSettings pattern
```
**forestElements store** (auto-increment):
```typescript
export function saveForestElement(db: IDBDatabase, element: ForestElement): Promise<number>
// store.add(element), return request.result as number (the auto-incremented id)
export function getForestElements(db: IDBDatabase): Promise<ForestElement[]>
// store.getAll(), return result as ForestElement[]
```
Add required imports at top: `import type { CompanionAssets, CompanionType, ForestElement, StyleReference } from "../types";`
(Keep existing Progress and Settings imports.)
cd /home/dev/workspace/zauberwald && npx tsx -e "import { saveCompanionAssets, getCompanionAssets, saveStyleReference, getStyleReference, saveForestElement, getForestElements } from './src/storage/db'; console.log('All 6 methods exported OK')"
- `grep -c "export function saveCompanionAssets" src/storage/db.ts` returns 1
- `grep -c "export function getCompanionAssets" src/storage/db.ts` returns 1
- `grep -c "export function saveStyleReference" src/storage/db.ts` returns 1
- `grep -c "export function getStyleReference" src/storage/db.ts` returns 1
- `grep -c "export function saveForestElement" src/storage/db.ts` returns 1
- `grep -c "export function getForestElements" src/storage/db.ts` returns 1
- `grep "CompanionAssets\|CompanionType\|ForestElement\|StyleReference" src/storage/db.ts | head -1` shows the types imported
All 6 new IndexedDB accessor methods added to db.ts — saveCompanionAssets, getCompanionAssets, saveStyleReference, getStyleReference, saveForestElement, getForestElements — following existing Promise-wrapped pattern.
Task 2: Build script for asset generation + run it
scripts/generate-assets.ts, package.json
- src/api/gemini.ts (generateImage function signature from Plan 01)
- src/api/config.ts (GeminiConfig interface, loadConfigFromObject)
- src/companion/characters.ts (companion definitions — 4 companions with type, name, personality)
- SPEC.md section 9.2 (Character Sheet prompt, Avatar prompt, visual descriptions per character)
- SPEC.md section 14.2 (asset generation flow)
- public/config.json (actual config file path)
**Step 1: Add tsx devDependency and npm script**
In `package.json`, add to devDependencies: `"tsx": "^4.0.0"`
Add script: `"generate-assets": "tsx scripts/generate-assets.ts"`
Run `npm install` after editing.
**Step 2: Create `scripts/generate-assets.ts`** (per D-01, D-02, ASST-01)
The script runs in Node.js via `npx tsx`. It:
1. Reads `public/config.json` via `fs.readFileSync('./public/config.json', 'utf-8')` and parses as `GeminiConfig`
2. Uses `loadConfigFromObject()` from `src/api/config.ts` to set the config
3. For each of the 4 companions, generates:
a. **Character Sheet** using the prompt from SPEC.md section 9.2:
```
Character sheet for a children's book. The character is {name}, {visualDescription}.
Show the character in 3 views: front view, side view, and a close-up of the face.
All views must show the EXACT SAME character with identical colors, proportions, and features.
Art style: Watercolor children's book illustration, warm pastel colors, soft edges, magical forest theme. White background.
Label each view clearly: "FRONT" "SIDE" "FACE"
The character must be: cute, friendly, approachable, with large expressive eyes. Suitable for a 7-year-old audience.
```
b. **Avatar** using the character sheet as reference image:
```
Create a single portrait of this exact character from the reference sheet.
Show only the face and upper body, looking directly at the viewer with a warm, friendly expression.
Same art style as the reference. Square format. No text. No background (or simple soft gradient).
```
4. Generates **Style Reference** — an initial forest scene:
```
Watercolor children's book illustration of a magical forest clearing.
Warm pastel colors, soft edges, dreamy atmosphere. A gentle meadow surrounded
by friendly trees, dappled sunlight, small flowers, and a hint of magic sparkles.
No text, no letters, no characters. This sets the visual style for all future forest images.
```
5. Saves each file using `fs.writeFileSync`:
- `src/assets/companions/fee-lila/character-sheet.png`
- `src/assets/companions/fee-lila/avatar.png`
- `src/assets/companions/einhorn-stella/character-sheet.png`
- `src/assets/companions/einhorn-stella/avatar.png`
- `src/assets/companions/fuchs-finn/character-sheet.png`
- `src/assets/companions/fuchs-finn/avatar.png`
- `src/assets/companions/eule-elsa/character-sheet.png`
- `src/assets/companions/eule-elsa/avatar.png`
- `src/assets/style-reference/initial-forest-scene.png`
Visual descriptions per companion (from SPEC.md section 9.2):
- fee: "a small fairy with lavender-purple wings, a flowing lilac dress, big warm brown eyes, a tiny flower crown made of forget-me-nots, light skin, and short wavy auburn hair"
- einhorn: "a small unicorn with a white coat, a shimmering golden horn, a flowing mane in soft rainbow pastels (pink, lavender, light blue), big dark eyes with long lashes, and small silver hooves"
- fuchs: "a young red fox with warm orange-brown fur, a white chest and belly, a bushy tail with a white tip, bright curious amber eyes, and slightly oversized pointed ears"
- eule: "a small round owl with soft brown-and-cream feathers, a heart-shaped face, large round golden eyes, tiny ear tufts, and small talons perched on a mossy branch"
The script should:
- Log progress to stdout: "Generating character sheet for {name}..." etc.
- Convert Blob responses to Buffer via `blob.arrayBuffer()` then `Buffer.from()`
- Handle errors gracefully: if one generation fails, log error and continue with others
- Exit with code 1 if any generation failed, 0 if all succeeded
- Add a 2-second delay between API calls to avoid rate limiting
**Step 3: Run the script**
Execute `npm run generate-assets` and verify all 9 PNG files are created.
If any fail, check the error and retry (the script should be re-runnable).
**Important:** The `generateImage` function in `src/api/gemini.ts` uses browser `fetch`. Node.js 18+ has native `fetch`, so this should work. If there are issues with `Blob` in Node.js, use `Buffer` and convert. The script may need to use `node:buffer` Blob or polyfill — handle at implementation time.
cd /home/dev/workspace/zauberwald && ls -la src/assets/companions/*/character-sheet.png src/assets/companions/*/avatar.png src/assets/style-reference/initial-forest-scene.png 2>&1 | grep -c ".png"
- `grep "generate-assets" package.json` shows the npm script
- `grep "tsx" package.json` shows tsx in devDependencies
- `test -f scripts/generate-assets.ts && echo "exists"` returns "exists"
- `ls src/assets/companions/fee-lila/character-sheet.png src/assets/companions/fee-lila/avatar.png` succeeds
- `ls src/assets/companions/einhorn-stella/character-sheet.png src/assets/companions/einhorn-stella/avatar.png` succeeds
- `ls src/assets/companions/fuchs-finn/character-sheet.png src/assets/companions/fuchs-finn/avatar.png` succeeds
- `ls src/assets/companions/eule-elsa/character-sheet.png src/assets/companions/eule-elsa/avatar.png` succeeds
- `ls src/assets/style-reference/initial-forest-scene.png` succeeds
- All 9 PNG files are > 1KB (not empty): `find src/assets -name "*.png" -size +1k | wc -l` returns 9
Build script generates 4 character sheets + 4 avatars + 1 style reference via Gemini API. All 9 PNG files exist in correct directories and are non-empty. `npm run generate-assets` is a working npm script.
- `npm run generate-assets` completes without errors (or re-run successfully)
- `find src/assets -name "*.png" -size +1k | wc -l` returns 9
- All 6 new DB methods compile: `npx tsc --noEmit src/storage/db.ts`
<success_criteria>
IndexedDB has accessors for all 5 stores (progress, settings from Phase 1 + companionAssets, styleReference, forestElements new)