use anyhow::{Context, Result, anyhow};
use serde::Deserialize;
use super::types::BackendChoice;
#[derive(Debug, Clone, Deserialize)]
pub struct RouterPlan {
pub choices: Vec<BackendChoice>,
}
pub fn parse_router_response(raw: &str) -> Result<RouterPlan> {
let trimmed = raw.trim();
if let Ok(plan) = serde_json::from_str::<RouterPlan>(trimmed) {
return Ok(plan);
}
let (start, end) = (
trimmed.find('{').ok_or_else(|| anyhow!("no '{{' found"))?,
trimmed.rfind('}').ok_or_else(|| anyhow!("no '}}' found"))?,
);
serde_json::from_str(&trimmed[start..=end]).context("router response is not valid RouterPlan")
}