fallback changes from PR #35

This commit is contained in:
Badri Narayanan S
2026-01-03 18:01:21 +05:30
parent 9c4a712a9a
commit df6625b531
5 changed files with 86 additions and 7 deletions

View File

@@ -16,6 +16,7 @@ import { logger } from '../utils/logger.js';
import { parseResetTime } from './rate-limit-parser.js';
import { buildCloudCodeRequest, buildHeaders } from './request-builder.js';
import { streamSSEResponse } from './sse-streamer.js';
import { getFallbackModel } from '../fallback-config.js';
/**
@@ -31,7 +32,7 @@ import { streamSSEResponse } from './sse-streamer.js';
* @yields {Object} Anthropic-format SSE events (message_start, content_block_start, content_block_delta, etc.)
* @throws {Error} If max retries exceeded or no accounts available
*/
export async function* sendMessageStream(anthropicRequest, accountManager) {
export async function* sendMessageStream(anthropicRequest, accountManager, fallbackEnabled = false) {
const model = anthropicRequest.model;
// Retry loop with account failover
@@ -74,6 +75,17 @@ export async function* sendMessageStream(anthropicRequest, accountManager) {
}
if (!account) {
// Check if fallback is enabled and available
if (fallbackEnabled) {
const fallbackModel = getFallbackModel(model);
if (fallbackModel) {
logger.warn(`[CloudCode] All accounts exhausted for ${model}. Attempting fallback to ${fallbackModel} (streaming)`);
// Retry with fallback model
const fallbackRequest = { ...anthropicRequest, model: fallbackModel };
yield* sendMessageStream(fallbackRequest, accountManager, false); // Disable fallback for recursive call
return;
}
}
throw new Error('No accounts available');
}
}