Skip to content

Commit 6afb1ac

Browse files
committed
refactor(api): migrate to channel-based architecture
1 parent bb5a1ab commit 6afb1ac

54 files changed

Lines changed: 4306 additions & 2099 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

API_REFACTOR_DESIGN.md

Lines changed: 1218 additions & 0 deletions
Large diffs are not rendered by default.

CLAUDE.md

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,42 @@ Contains code shared between frontend and API:
5656
- `src/constants/` - Provider configs, model configs, aspect ratios
5757
- `src/utils/` - Validation utilities (prompt, dimensions, steps)
5858

59-
### API Provider System
59+
### API Channel System
6060

61-
The API uses a provider abstraction pattern:
61+
The API is organized by provider "channels" (single folder per provider) plus shared core infra:
6262

63-
- `apps/api/src/providers/types.ts` - Provider interface definition
64-
- `apps/api/src/providers/gitee.ts` - Gitee AI implementation
65-
- `apps/api/src/providers/huggingface.ts` - HuggingFace implementation
66-
- `apps/api/src/providers/registry.ts` - Provider registration and lookup
63+
- `apps/api/src/core/types.ts` - Unified channel/capability types
64+
- `apps/api/src/core/channel-registry.ts` - Channel registry (in-memory)
65+
- `apps/api/src/core/token-manager.ts` - Token parsing + rotation (server-side)
66+
- `apps/api/src/core/openai-compat.ts` - OpenAI-compatible capability helpers
67+
- `apps/api/src/channels/index.ts` - Auto-register built-in channels (+ best-effort custom init)
68+
- `apps/api/src/channels/huggingface/*` - HF image (Gradio Spaces) + LLM (HF token optional, falls back to Pollinations)
69+
- `apps/api/src/channels/gitee/*` - Gitee image + LLM + video
70+
- `apps/api/src/channels/modelscope/*` - ModelScope image (async task mode) + LLM
71+
- `apps/api/src/channels/custom/*` - User-defined OpenAI-compatible channels (env-driven)
6772

6873
### Key Endpoints
6974

70-
- `POST /api/generate` - Unified image generation endpoint
71-
- Supports `provider` parameter: `"gitee"` | `"huggingface"`
72-
- Requires `X-API-Key` header for Gitee AI
73-
- Optional `X-HF-Token` header for HuggingFace
74-
- `POST /api/generate-hf` - Legacy HuggingFace endpoint (backward compatible)
75-
- `POST /api/upscale` - RealESRGAN 4x upscaling
75+
- `GET /` - Health check
76+
- `GET /v1/models` - OpenAI-compatible model list
77+
- `POST /v1/images/generations` - OpenAI-compatible image generation
78+
- `POST /v1/chat/completions` - OpenAI-compatible chat completions
79+
80+
Authentication (OpenAI-style):
81+
82+
- Header: `Authorization: Bearer <token>`
83+
- Optional provider hint prefix (kept for backward compatibility): `gitee:` / `ms:` / `hf:` / `deepseek:`
84+
- Multiple tokens supported: `Authorization: Bearer gitee:tok1,tok2,tok3` (server rotates on 429/quota errors)
85+
86+
Custom channels:
87+
88+
- Configure via env (`CUSTOM_CHANNELS_JSON` or `CUSTOM_CHANNEL_1_*`, etc.)
89+
- Use model id `custom/<channelId>/<model>` for both image + chat (routes resolve the channelId from the model string)
7690

7791
### Frontend Structure
7892

7993
- `src/pages/ImageGenerator.tsx` - Main page with single image generation
80-
- `src/pages/FlowPage.tsx` - Visual canvas for batch generation using React Flow
94+
- `src/pages/FlowPageV2.tsx` - Visual canvas for batch generation using React Flow
8195
- `src/hooks/useImageGenerator.ts` - Core state management and API calls
8296
- `src/components/ui/` - shadcn/ui components
8397
- `src/components/feature/` - Feature-specific components (PromptCard, ImageResultCard, etc.)
@@ -93,6 +107,25 @@ The API uses a provider abstraction pattern:
93107
- Flow mode persists nodes/edges/images to IndexedDB
94108
- API URL configured via `VITE_API_URL` env var (defaults to relative path for same-origin deployment)
95109

110+
### Token Rotation System
111+
112+
The app supports multiple API tokens per provider for automatic rotation on rate limits:
113+
114+
- Frontend: `apps/web/src/lib/tokenRotation.ts` can rotate per-request (Flow mode)
115+
- Backend: `apps/api/src/core/token-manager.ts` rotates when multiple tokens are supplied in `Authorization`
116+
117+
### Middleware Stack (API)
118+
119+
Applied in order:
120+
121+
1. `requestId` - Generates unique request ID for tracing
122+
2. `cors` - CORS headers with configurable origins
123+
3. `securityHeaders` - Security headers (CSP, X-Frame-Options, etc.)
124+
4. `requestLogger` - Logs requests with timing
125+
5. `timeout` - Request timeout (varies by endpoint: 30s-120s)
126+
6. `bodyLimit` - Request body size limit (20KB-50KB)
127+
7. `rateLimitPresets` - Rate limiting (5-60 req/min depending on endpoint)
128+
96129
### Tooling
97130

98131
- **Biome** - Linting and formatting (replaces ESLint + Prettier)

0 commit comments

Comments
 (0)