ai-fallback
Advanced tools
Comparing version
import { LanguageModelV1, LanguageModelV1CallOptions, LanguageModelV1CallWarning, LanguageModelV1FinishReason, LanguageModelV1FunctionToolCall, LanguageModelV1LogProbs, LanguageModelV1ProviderMetadata, LanguageModelV1StreamPart } from '@ai-sdk/provider'; | ||
type Logger = { | ||
log: (...args: any[]) => void; | ||
error: (...args: any[]) => void; | ||
}; | ||
interface Settings { | ||
models: Array<LanguageModelV1>; | ||
modelResetInterval?: number; | ||
logger?: Logger; | ||
shouldRetryThisError?: (error: Error) => boolean; | ||
@@ -22,3 +17,2 @@ onError?: (error: Error, modelId: string) => void | Promise<void>; | ||
readonly settings: Settings; | ||
private logger; | ||
currentModelIndex: number; | ||
@@ -25,0 +19,0 @@ private lastModelReset; |
@@ -38,3 +38,2 @@ export function createFallback(settings) { | ||
this.settings = settings; | ||
this.logger = settings.logger || console; | ||
this.modelResetInterval = (_a = settings.modelResetInterval) !== null && _a !== void 0 ? _a : 3 * 60 * 1000; // Default 3 minutes in ms | ||
@@ -85,3 +84,2 @@ // Use globalThis.modelId if defined to find initial model | ||
lastError = error; | ||
this.logger.error('Error with model', this.modelId, error); | ||
// Only retry if it's a server/capacity error | ||
@@ -127,3 +125,2 @@ const shouldRetry = this.settings.shouldRetryThisError || | ||
catch (error) { | ||
self.logger.error('Error in stream', self.modelId, error); | ||
if (self.settings.onError) { | ||
@@ -130,0 +127,0 @@ await self.settings.onError(error, self.modelId); |
{ | ||
"name": "ai-fallback", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Automatically switch AI SDK model provider when one of the providers has downtime", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -35,2 +35,6 @@ ### AI Fallback | ||
resetAfterMs: 60000, // Reset after 1 minute | ||
onError: (error, modelId) => { | ||
console.error(`Error with model ${modelId}:`, error) | ||
}, | ||
modelResetInterval: 60000, // Reset to first model after 1 minute of the first error | ||
}) | ||
@@ -37,0 +41,0 @@ ``` |
@@ -12,11 +12,5 @@ import { | ||
type Logger = { | ||
log: (...args: any[]) => void | ||
error: (...args: any[]) => void | ||
} | ||
interface Settings { | ||
models: Array<LanguageModelV1> | ||
modelResetInterval?: number | ||
logger?: Logger | ||
shouldRetryThisError?: (error: Error) => boolean | ||
@@ -70,3 +64,2 @@ onError?: (error: Error, modelId: string) => void | Promise<void> | ||
readonly settings: Settings | ||
private logger: Logger | ||
@@ -79,5 +72,2 @@ currentModelIndex: number = 0 | ||
this.settings = settings | ||
this.logger = settings.logger || console | ||
this.modelResetInterval = settings.modelResetInterval ?? 3 * 60 * 1000 // Default 3 minutes in ms | ||
@@ -139,3 +129,2 @@ | ||
lastError = error as Error | ||
this.logger.error('Error with model', this.modelId, error) | ||
@@ -209,7 +198,2 @@ // Only retry if it's a server/capacity error | ||
} catch (error) { | ||
self.logger.error( | ||
'Error in stream', | ||
self.modelId, | ||
error, | ||
) | ||
if (self.settings.onError) { | ||
@@ -216,0 +200,0 @@ await self.settings.onError( |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
121
3.42%37154
-2.76%647
-3.29%