learning_ai_common_plat/packages/llm/src/fallback.ts
Saravana Achu Mac 43bf51a290 feat(llm): add Perplexity, Gemini providers and createFallbackChain
- Add PerplexityProvider (OpenAI-compatible, reads PERPLEXITY_API_KEY)
- Add GeminiProvider (Google Generative Language API adapter, reads GEMINI_API_KEY)
- Add createFallbackChain() — ordered provider chain, skips unconfigured,
  aggregates errors; allows any app to replace custom LLM fallback loops
- Extend LLMProviderType with 'perplexity' | 'gemini'
- Update factory to resolve and instantiate new provider types
- Add PAID_PROVIDERS to llm-router registry (OpenAI, Perplexity) for apps
  using round-robin routing alongside free-tier providers
- 27 tests covering fallback chain, new providers, error/edge cases

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-05 12:50:08 -07:00

37 lines
1.1 KiB
TypeScript

/**
* Fallback chain utility.
*
* Wraps an ordered list of LLMProviders into a single LLMProvider that
* tries each in sequence, skipping unconfigured ones, and moves to the
* next on any error. Throws only when all providers are exhausted.
*/
import type { ChatCompletionRequest, ChatCompletionResponse, LLMProvider } from './types.js';
export function createFallbackChain(providers: LLMProvider[]): LLMProvider {
return {
isConfigured(): boolean {
return providers.some(p => p.isConfigured());
},
async chatCompletion(req: ChatCompletionRequest): Promise<ChatCompletionResponse> {
const errors: string[] = [];
for (const provider of providers) {
if (!provider.isConfigured()) continue;
try {
return await provider.chatCompletion(req);
} catch (err) {
errors.push(err instanceof Error ? err.message : String(err));
}
}
throw new Error(
errors.length > 0
? `All providers failed: ${errors.join(' | ')}`
: 'No providers configured'
);
},
};
}