Skip to content

Commit 2c959d0

Browse files
committed
add a fallback model when api failed
1 parent fbca06b commit 2c959d0

File tree

1 file changed

+31
-8
lines changed
  • crates/chat-cli/src/cli/chat/cli

1 file changed

+31
-8
lines changed

crates/chat-cli/src/cli/chat/cli/model.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,24 @@ pub async fn get_available_models(os: &Os) -> Result<(Vec<ModelInfo>, ModelInfo)
224224
let endpoint = Endpoint::configured_value(&os.database);
225225
let region = endpoint.region().as_ref();
226226

227-
let (api_models, api_default) = os
228-
.client
229-
.get_available_models(region)
230-
.await
231-
.map_err(|e| ChatError::Custom(format!("Failed to fetch available models: {}", e).into()))?;
227+
match os.client.get_available_models(region).await {
228+
Ok((api_models, api_default)) => {
229+
let models: Vec<ModelInfo> = api_models.iter().map(ModelInfo::from_api_model).collect();
230+
let default_model = ModelInfo::from_api_model(&api_default);
232231

233-
let models: Vec<ModelInfo> = api_models.iter().map(ModelInfo::from_api_model).collect();
234-
let default_model = ModelInfo::from_api_model(&api_default);
232+
tracing::debug!("Successfully fetched {} models from API", models.len());
233+
Ok((models, default_model))
234+
},
235+
// In case of API throttling or other errors, fall back to hardcoded models
236+
Err(e) => {
237+
tracing::error!("Failed to fetch models from API: {}, using fallback list", e);
238+
239+
let models = get_fallback_models();
240+
let default_model = models[0].clone();
235241

236-
Ok((models, default_model))
242+
Ok((models, default_model))
243+
},
244+
}
237245
}
238246

239247
/// Returns the context window length in tokens for the given model_id.
@@ -245,3 +253,18 @@ pub fn context_window_tokens(model_info: Option<&ModelInfo>) -> usize {
245253
fn default_context_window() -> usize {
246254
200_000
247255
}
256+
257+
fn get_fallback_models() -> Vec<ModelInfo> {
258+
vec![
259+
ModelInfo {
260+
model_name: Some("claude-3.7-sonnet".to_string()),
261+
model_id: "claude-3.7-sonnet".to_string(),
262+
context_window_tokens: 200_000,
263+
},
264+
ModelInfo {
265+
model_name: Some("claude-4-sonnet".to_string()),
266+
model_id: "claude-4-sonnet".to_string(),
267+
context_window_tokens: 200_000,
268+
},
269+
]
270+
}

0 commit comments

Comments
 (0)