banatie-service/apps/api-service
Oleg Proskurin ca112886e5 feat: implement Phase 5 live generation with prompt caching
Implement cached generation endpoint that streams image bytes directly with
intelligent caching based on prompt hashing for optimal performance.

**Core Service:**
- **PromptCacheService**: Prompt-based cache management
  - SHA-256 hashing of prompts for cache lookup
  - Cache hit/miss tracking with statistics
  - Support for cache entry creation and retrieval
  - Hit count and last accessed timestamp tracking
  - Cache statistics per project

**v1 API Routes:**
- `GET /api/v1/live/generate` - Generate with caching, stream image bytes

**Endpoint Features:**
- Prompt-based caching with SHA-256 hashing
- Cache HIT: Streams existing image with X-Cache-Status: HIT header
- Cache MISS: Generates new image, caches it, streams with X-Cache-Status: MISS
- Direct image byte streaming (not JSON response)
- Cache-Control headers for browser caching (1 year max-age)
- Hit count tracking for cache analytics
- Integration with promptUrlCache database table

**Caching Logic:**
- Compute SHA-256 hash of prompt for cache key
- Check cache by promptHash and projectId
- On HIT: Fetch image from database, download from storage, stream bytes
- On MISS: Generate new image, create cache entry, stream bytes
- Record cache hits with incremented hit count

**Response Headers:**
- Content-Type: image/jpeg (or appropriate MIME type)
- Content-Length: Actual byte length
- Cache-Control: public, max-age=31536000 (1 year)
- X-Cache-Status: HIT | MISS
- X-Cache-Hit-Count: Number of cache hits (on HIT)
- X-Generation-Id: UUID (on MISS)
- X-Image-Id: UUID (always)

**Technical Notes:**
- Uses ImageService to fetch cached images by ID
- Uses StorageFactory to download image buffers from MinIO
- Parses storage keys to extract org/project/category/filename
- Validates storage key format before download
- All Phase 5 code is fully type-safe with zero TypeScript errors

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-09 23:00:10 +07:00
..
_tests chore: update docs 2025-10-12 22:19:10 +07:00
src feat: implement Phase 5 live generation with prompt caching 2025-11-09 23:00:10 +07:00
.env feat: update setup 2025-10-12 21:12:58 +07:00
.prettier.config.js chore: prettier 2025-10-09 23:16:42 +07:00
Dockerfile feat: update setup 2025-10-12 21:12:58 +07:00
NETWORK_ERROR_DETECTION.md feat: detect network issues 2025-10-10 00:17:29 +07:00
docker-compose.yml feat: update config 2025-10-12 21:49:42 +07:00
eslint.config.js chore: prettier 2025-10-09 23:16:42 +07:00
package.json feat: add Phase 1 foundation for API v2.0 2025-11-09 21:53:50 +07:00
secrets.env.example feat: update setup 2025-10-12 21:12:58 +07:00
test-network-error-detector.ts feat: detect network issues 2025-10-10 00:17:29 +07:00
tsconfig.json chore: prettier 2025-10-09 23:16:42 +07:00