- Add uload project with apps/web structure
- Reorganize from flat to monorepo structure
- Remove PocketBase binary and local data
- Update to pnpm and @uload/web namespace
- Add picture project to monorepo
- Remove embedded git repository
- Unify all package names to @{project}/{app} schema:
- @maerchenzauber/* (was @storyteller/*)
- @manacore/* (was manacore-*, manacore)
- @manadeck/* (was web, backend, manadeck)
- @memoro/* (was memoro-web, landing, memoro)
- @picture/* (already unified)
- @uload/web
- Add convenient dev scripts for all apps:
- pnpm dev:{project}:web
- pnpm dev:{project}:landing
- pnpm dev:{project}:mobile
- pnpm dev:{project}:backend
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
|
||
|---|---|---|
| .. | ||
| generate-image | ||
| process-generation | ||
| process-jobs | ||
| process-jobs-test | ||
| start-generation | ||
| .env.example | ||
| .gitignore | ||
| ARCHITECTURE.md | ||
| DEPLOYMENT_GUIDE.md | ||
| QUICK_REFERENCE.md | ||
| README.md | ||
Supabase Edge Functions - Image Generation System
Overview
This directory contains the refactored asynchronous image generation system using a job queue pattern. The system is designed for scalability, reliability, and maintainability.
What Changed?
Before (Legacy System)
- Single monolithic 667-line
generate-imagefunction - Client waits 30-60 seconds for response (blocking)
- Difficult to scale or add features
- No retry mechanism
- Single point of failure
After (Queue System)
- 3 focused Edge Functions + job queue
- Client gets instant response (~100ms)
- Jobs processed by background worker
- Automatic retries on failure
- Easy to scale horizontally
- Clean separation of concerns
System Components
1. start-generation
Purpose: Accept generation request and return immediately
- Validates user authentication
- Creates generation record
- Enqueues job for background processing
- Returns instantly with generation_id
2. process-jobs (Worker)
Purpose: Background worker that processes queued jobs
- Triggered by pg_cron every minute
- Claims and processes up to 3 jobs in parallel
- Handles 'generate-image' and 'download-image' jobs
- Manages retries and error handling
3. process-generation (Module)
Purpose: Handle Replicate API interaction
- Extracted from original 667-line function
- Supports 15+ AI models with model-specific logic
- Handles aspect ratios, img2img, polling
- Can be imported as module or called standalone
4. generate-image (Legacy)
Status: Deprecated but kept for backward compatibility
The original monolithic function. Still works but doesn't use the queue system. Will be gradually phased out.
Documentation
📘 ARCHITECTURE.md
Complete system architecture, database schema, and design decisions.
Read this to understand:
- How the system works end-to-end
- Database tables and functions
- Job flow and state transitions
- Performance characteristics
- Monitoring and scaling strategies
🚀 DEPLOYMENT_GUIDE.md
Step-by-step deployment instructions.
Follow this to deploy:
- Create database schema
- Deploy Edge Functions
- Set up pg_cron job
- Test the system
- Monitor and scale
⚡ QUICK_REFERENCE.md
Quick commands and code snippets for daily use.
Use this for:
- Common SQL queries
- Quick fixes
- Client code examples
- Configuration values
- Troubleshooting
Quick Start
Prerequisites
npm install -g supabase
supabase login
supabase link --project-ref YOUR_PROJECT_REF
Deploy
# From apps/mobile directory
npx supabase functions deploy start-generation
npx supabase functions deploy process-generation
npx supabase functions deploy process-jobs
# Set secrets
npx supabase secrets set REPLICATE_API_TOKEN=your_token_here
Set Up Database
Run SQL from DEPLOYMENT_GUIDE.md to create:
job_queuetableenqueue_job()functionclaim_next_job()functioncomplete_job()function- pg_cron schedule
Test
# Test start-generation
curl -X POST https://YOUR_PROJECT.supabase.co/functions/v1/start-generation \
-H "Authorization: Bearer YOUR_ANON_KEY" \
-H "Content-Type: application/json" \
-d '{"prompt":"test","model_id":"black-forest-labs/flux-schnell","width":1024,"height":1024}'
# Manually trigger worker
curl -X POST https://YOUR_PROJECT.supabase.co/functions/v1/process-jobs \
-H "Authorization: Bearer YOUR_ANON_KEY"
Architecture Diagram
┌─────────────┐
│ Client │
└─────┬───────┘
│ POST /start-generation
↓
┌─────────────────────┐
│ start-generation │ ← Returns immediately (~100ms)
│ • Auth check │
│ • Create record │
│ • Enqueue job │
└─────┬───────────────┘
│
↓ Job inserted
┌─────────────────────┐
│ job_queue table │
└─────┬───────────────┘
│
↓ pg_cron every minute
┌─────────────────────┐
│ process-jobs │ ← Background worker
│ • Claim 3 jobs │
│ • Process in || │
└───┬─────────┬───────┘
│ │
↓ ↓
┌───────┐ ┌──────────┐
│ Gen │ │ Download │
│ Image │ │ Image │
└───┬───┘ └────┬─────┘
│ │
↓ ↓
┌────────────────────┐
│ process-generation │ ← Replicate API
│ • Model params │
│ • API calls │
│ • Polling │
└────────────────────┘
Key Features
🚀 Instant Response
- Client gets response in ~100ms
- No more 30-60 second waits
- Better UX, faster perceived performance
🔄 Automatic Retries
- Jobs retry up to 3 times on failure
- Transient errors handled gracefully
- Clear error messages for debugging
📈 Scalable
- Process multiple jobs in parallel
- Easy to increase throughput
- Horizontal scaling via pg_cron frequency
🛠 Maintainable
- Clean separation of concerns
- Each function has single responsibility
- Well-documented code
- Easy to add new features
🔍 Observable
- Comprehensive logging
- Database-backed job history
- Easy to monitor and debug
- Clear status progression
Supported Models
The system supports 15+ AI models:
Fast Models (< 5 seconds)
- FLUX Schnell
- SDXL Lightning
Quality Models (30-60 seconds)
- FLUX Dev, FLUX 1.1 Pro
- SDXL, Ideogram V3
- Imagen 4, SD 3.5
Specialized
- Recraft V3 (SVG output)
- SeeDream, Qwen Image
All models include:
- Automatic aspect ratio handling
- Model-specific parameter optimization
- Image-to-image support (where available)
Performance
Default Configuration
- Throughput: ~180 generations/hour
- Latency: 30-60 seconds (depends on model)
- Concurrency: 3 parallel jobs
- Reliability: 95%+ success rate
Scaled Configuration
With 10 parallel jobs and 30-second intervals:
- Throughput: ~1,200 generations/hour
Monitoring
Quick Health Check
-- Check queue
SELECT status, COUNT(*) FROM job_queue GROUP BY status;
-- Check recent generations
SELECT status, COUNT(*) FROM image_generations
WHERE created_at > now() - interval '1 hour'
GROUP BY status;
Key Metrics
- Queue depth (pending jobs)
- Processing time
- Error rate
- Throughput (jobs/hour)
See QUICK_REFERENCE.md for more queries.
Troubleshooting
Jobs Not Processing
- Check pg_cron is scheduled:
SELECT * FROM cron.job; - Check function logs in Supabase Dashboard
- Manually trigger:
curl .../process-jobs
High Error Rate
- Check job errors:
SELECT error_message FROM job_queue WHERE status='failed'; - Verify Replicate API token is valid
- Check Replicate service status
Stuck Jobs
Reset jobs stuck in processing:
UPDATE job_queue SET status='pending', attempt_number=0
WHERE status='processing' AND updated_at < now() - interval '15 minutes';
Migration Path
Current State
- Both legacy and queue systems are running
- New features should use queue system
- Existing code still works with legacy function
Next Steps
- Update mobile app to use start-generation
- Update web app to use start-generation
- Monitor queue system for 1-2 weeks
- Deprecate legacy generate-image function
- Remove legacy code
Rollback
If issues occur, simply revert clients to use generate-image function.
Development
Local Testing
# Start Supabase locally
npx supabase start
# Serve functions
npx supabase functions serve
# Test in another terminal
curl http://localhost:54321/functions/v1/start-generation ...
Adding New Job Types
- Add handler in
processJob()function - Create processing logic
- Update documentation
- Deploy
Example:
case 'my-new-job-type':
await processMyNewJobType(job, supabaseAdmin);
break;
Code Statistics
- start-generation: ~220 lines
- process-jobs: ~500 lines
- process-generation: ~565 lines
- Total new code: ~1,285 lines
- Legacy function: ~667 lines
- Lines saved: Cleaner, more maintainable
Contributing
When making changes:
- Update relevant README.md
- Update ARCHITECTURE.md if design changes
- Test locally first
- Deploy and verify in production
- Monitor for 24 hours
Related Documentation
Support
For issues:
- Check QUICK_REFERENCE.md for common fixes
- Review function logs in Supabase Dashboard
- Check job_queue table for error details
- Review ARCHITECTURE.md for design questions
License
Part of the Picture monorepo. See root LICENSE file.
Status: ✅ Production Ready
Last Updated: 2025-10-09