Request Flow
- User sends a message in the ManyAI mobile app.
isImageGenRequest()checks for image-related keywords in the prompt.pickProvider()selects the first available provider fromROUTING_ORDER.callProvider()sends the API request with a 30-second timeout.- On failure: add the provider to
failedProvidersand retry up toMAX_RETRIES = 8with the next provider inROUTING_ORDER. - If all retries fail, display an error message to the user.
Provider Selection (pickProvider)
- Build the available provider set: include all providers with valid API keys; always include Pollinations.
- Filter to
VISION_PROVIDERSif an image is attached. - First pass: iterate
ROUTING_ORDER, preferring providers withbestFormatching the task type. - Second pass: return the first capable provider (fallback to any available if no
bestFormatch). - Final fallback: return Pollinations if no other providers are available.
- Return
nullif all providers are exhausted.
API Shapes (callProvider.ts)
| Provider | Method | Endpoint | Auth | Notes |
|---|---|---|---|---|
| Pollinations | GET | text.pollinations.ai/{encoded_prompt} | None | No key required; always available |
| Gemini | POST | /models/{model}:generateContent | API key in URL | parts array with inline_data for images |
| Anthropic | POST | /messages | x-api-key header | Different body structure from OpenAI |
| Cloudflare | POST | Account ID embedded in URL | Key split on : | Key format: accountId:apiToken |
| All others | POST | /chat/completions | Authorization: Bearer | OpenAI-compatible |
Request timeout: 30 seconds per attempt.
Error Handling
- Failed providers are tracked in
failedProvidersusinguseRef— scoped to the current session only. - Failed providers are skipped on all subsequent attempts within that session.
- Conversation context: the last 10 messages are sent as context with each request; system messages and error messages are excluded.
- On total exhaustion (all providers failed
MAX_RETRIEStimes), a user-visible error is displayed and the session provider list resets.