---
phase: 02-gemini-integration-asset-pipeline
plan: 03
subsystem: assets
tags: [gemini, image-generation, indexeddb, tsx, character-sheets, avatars]

# Dependency graph
requires:
  - phase: 02-01
    provides: "Gemini API wrapper (generateImage, generateText) and config loader"
provides:
  - "6 IndexedDB accessor methods for companionAssets, styleReference, forestElements stores"
  - "Asset generation build script (scripts/generate-assets.ts)"
  - "4 character sheets, 4 avatars, 1 style reference — 9 PNG files total"
affects: [02-04, 02-05, 03-companion-integration]

# Tech tracking
tech-stack:
  added: [tsx]
  patterns: [build-script-asset-generation, blob-to-buffer-conversion]

key-files:
  created:
    - scripts/generate-assets.ts
    - 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
  modified:
    - src/storage/db.ts
    - package.json

key-decisions:
  - "Fixed imageModel name from gemini-3.1-flash-preview-image to gemini-3.1-flash-image-preview"
  - "Script skips already-generated assets for re-runnability"
  - "2-second delay between API calls to avoid rate limiting"

patterns-established:
  - "Build scripts use tsx for Node.js TypeScript execution"
  - "Asset generation reads config from public/config.json via fs.readFileSync"
  - "Character avatars use character sheets as reference images for visual consistency"

requirements-completed: [ASST-01, ASST-02]

# Metrics
duration: 6min
completed: 2026-03-29
---

# Phase 02 Plan 03: Asset Generation Summary

**IndexedDB store accessors + Gemini-powered build script generating 4 character sheets, 4 avatars, and 1 style reference image**

## Performance

- **Duration:** 6 min
- **Started:** 2026-03-29T10:04:23Z
- **Completed:** 2026-03-29T10:10:30Z
- **Tasks:** 2
- **Files modified:** 12

## Accomplishments
- Added 6 IndexedDB accessor methods (save/get for companionAssets, styleReference, forestElements)
- Created build script that calls Gemini API to generate character sheets with 3 views (front, side, face) and matching avatars
- Generated all 9 PNG assets: 4 character sheets, 4 avatars, 1 style reference (initial forest scene)
- Script is re-runnable (skips existing assets) with graceful error handling

## Task Commits

Each task was committed atomically:

1. **Task 1: Add IndexedDB accessor methods for remaining stores** - `a242ce1` (feat)
2. **Task 2: Build script for asset generation + run it** - `1fa7d52` (feat)

## Files Created/Modified
- `src/storage/db.ts` - Added saveCompanionAssets, getCompanionAssets, saveStyleReference, getStyleReference, saveForestElement, getForestElements
- `scripts/generate-assets.ts` - Build script reading config.json, calling Gemini API, saving 9 PNGs
- `package.json` - Added tsx devDependency and generate-assets npm script
- `src/assets/companions/*/character-sheet.png` - Character sheets (3-view: front, side, face) for all 4 companions
- `src/assets/companions/*/avatar.png` - Portrait avatars for all 4 companions
- `src/assets/style-reference/initial-forest-scene.png` - Watercolor forest clearing style reference

## Decisions Made
- Fixed imageModel name in config.json (gemini-3.1-flash-preview-image was 404, corrected to gemini-3.1-flash-image-preview)
- Script skips already-generated assets (checks file existence + size > 1KB) for idempotent re-runs
- Added 2-second delay between API calls to respect rate limits
- Avatar generation uses character sheet as reference image for visual consistency

## Deviations from Plan

### Auto-fixed Issues

**1. [Rule 1 - Bug] Fixed incorrect image model name in config.json**
- **Found during:** Task 2 (running generate-assets script)
- **Issue:** Model name `gemini-3.1-flash-preview-image` returns 404; correct name is `gemini-3.1-flash-image-preview`
- **Fix:** Updated public/config.json imageModel field
- **Files modified:** public/config.json (gitignored)
- **Verification:** API returns 200 and generates images successfully
- **Committed in:** Not committed (file is gitignored), but documented for user awareness

---

**Total deviations:** 1 auto-fixed (1 bug)
**Impact on plan:** Essential fix for API connectivity. No scope creep.

## Issues Encountered
- Gemini API returned 404 for the configured imageModel name. Resolved by listing available models and correcting the name.

## User Setup Required
None - Gemini API key already configured in public/config.json from Phase 1 setup.

## Next Phase Readiness
- All character assets generated and committed to git
- IndexedDB store accessors ready for runtime use
- Style reference image available for consistent art direction in future forest element generation
- Ready for Plan 04 (companion integration) and Plan 05 (forest reward pipeline)

## Self-Check: PASSED

All 10 created files verified present. Both task commits (a242ce1, 1fa7d52) verified in git log.

---
*Phase: 02-gemini-integration-asset-pipeline*
*Completed: 2026-03-29*
