Skip to main content

codetether_agent/tui/app/smart_switch/
retry.rs

1//! Retry scheduling logic.
2
3use std::collections::HashSet;
4
5use super::candidates::smart_switch_candidates;
6use super::error_detection::{is_retryable_provider_error, smart_switch_model_key};
7
8/// A pending smart switch retry with the prompt and target model.
9#[derive(Debug, Clone)]
10pub struct PendingSmartSwitchRetry {
11    pub prompt: String,
12    pub target_model: String,
13}
14
15/// Schedules a smart switch retry if the error is retryable and candidates exist.
16pub fn maybe_schedule_smart_switch_retry(
17    error_msg: &str,
18    current_model: Option<&str>,
19    current_provider: Option<&str>,
20    available_providers: &[String],
21    prompt: &str,
22    retry_count: u32,
23    attempted_models: &[String],
24) -> Option<PendingSmartSwitchRetry> {
25    if !is_retryable_provider_error(error_msg) {
26        return None;
27    }
28    let max = super::helpers::smart_switch_max_retries() as u32;
29    if retry_count >= max {
30        return None;
31    }
32    let attempted: HashSet<String> = attempted_models
33        .iter()
34        .map(|m| smart_switch_model_key(m))
35        .collect();
36    let candidates = smart_switch_candidates(
37        current_model,
38        current_provider,
39        available_providers,
40        &attempted,
41    );
42    candidates
43        .into_iter()
44        .next()
45        .map(|target_model| PendingSmartSwitchRetry {
46            prompt: prompt.to_string(),
47            target_model,
48        })
49}