use std::collections::{HashMap, HashSet};
use std::sync::{Arc, LazyLock, Mutex, PoisonError};
use std::time::{Duration, Instant};
use bytes::Bytes;
use cpa_common::gostr::{GoStr, trim_space};
use cpa_common::json::{self as gj, Kind};
use cpa_core::config::Config;
use cpa_core::credential::Credential;
use futures_util::StreamExt;
use futures_util::stream::FuturesUnordered;
use sha2::{Digest, Sha256};
use crate::proxy::{self, GoClients, GoHeaders, Proxy};
const DAILY_BASE_URL: &str = "https://daily-cloudcode-pa.googleapis.com";
const MODELS_PATH: &str = "/v1internal:fetchAvailableModels";
const PROBE_TIMEOUT: Duration = Duration::from_secs(5);
const CACHE_TTL: Duration = Duration::from_secs(300);
const FAILURE_TTL: Duration = Duration::from_secs(60);
type Hints = HashSet<String>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Status {
Success,
AuthError,
Transient,
}
type Clock = Arc<dyn Fn() -> Instant + Send + Sync>;
type Shared = (Hints, Status, String);
pub(crate) struct Capabilities {
clock: Clock,
timeout: Duration,
cache: Mutex<HashMap<String, (Hints, Instant)>>,
failures: Mutex<HashMap<String, Instant>>,
flights: Mutex<HashMap<String, Arc<tokio::sync::OnceCell<Shared>>>>,
in_flight: std::sync::atomic::AtomicUsize,
}
impl Capabilities {
pub(crate) fn new(clock: Clock) -> Self {
Self {
clock,
timeout: PROBE_TIMEOUT,
cache: Mutex::default(),
failures: Mutex::default(),
flights: Mutex::default(),
in_flight: std::sync::atomic::AtomicUsize::new(0),
}
}
fn now(&self) -> Instant {
(self.clock)()
}
fn purge(&self, now: Instant) {
lock(&self.cache).retain(|_, (_, expires)| now < *expires);
lock(&self.failures).retain(|_, expires| now < *expires);
}
fn cached(&self, key: &str, now: Instant) -> Option<Hints> {
lock(&self.cache)
.get(key)
.filter(|(_, expires)| now < *expires)
.map(|(hints, _)| hints.clone())
}
pub(crate) async fn fetch(&self, client: &wreq::Client, credential: &Credential, proxy_url: &str) -> Hints {
let token = credential.str("access_token").map(str::trim).unwrap_or_default();
if token.is_empty() {
return Hints::new();
}
let urls = base_urls(credential);
let key = format!("{}#{proxy_url}", urls.join("|"));
let failure_key = format!(
"{}#{}",
credential.id,
Sha256::digest(token.as_bytes())
.iter()
.map(|b| format!("{b:02x}"))
.collect::<String>()
);
loop {
let now = self.now();
if lock(&self.failures)
.get(&failure_key)
.is_some_and(|expires| now < *expires)
{
return Hints::new();
}
if let Some(hints) = self.cached(&key, now) {
return hints;
}
self.in_flight.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
let flight = lock(&self.flights).entry(key.clone()).or_default().clone();
let (hints, status, probed) = flight
.get_or_init(|| async {
let result = self.lead(client, &urls, &key, token).await;
let mut flights = lock(&self.flights);
if flights.get(&key).is_some_and(|current| Arc::ptr_eq(current, &flight)) {
flights.remove(&key);
}
result
})
.await
.clone();
self.in_flight.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
if status != Status::AuthError {
return hints;
}
if probed != token {
continue;
}
self.purge(now);
lock(&self.failures).insert(failure_key, self.now() + FAILURE_TTL);
return Hints::new();
}
}
async fn lead(&self, client: &wreq::Client, urls: &[String], key: &str, token: &str) -> Shared {
let inside = self.now();
if let Some(hints) = self.cached(key, inside) {
return (hints, Status::Success, token.to_owned());
}
let (hints, status) = probe(client, urls, token, self.timeout).await;
if status != Status::AuthError {
let ttl = if status == Status::Success {
CACHE_TTL
} else {
FAILURE_TTL
};
self.purge(inside);
lock(&self.cache).insert(key.to_owned(), (hints.clone(), self.now() + ttl));
}
(hints, status, token.to_owned())
}
}
fn lock<T>(m: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
m.lock().unwrap_or_else(PoisonError::into_inner)
}
pub(crate) fn shared() -> &'static Capabilities {
static SHARED: LazyLock<Capabilities> = LazyLock::new(|| Capabilities::new(Arc::new(Instant::now)));
&SHARED
}
fn base_urls(credential: &Credential) -> Vec<String> {
let trim = |s: &str| s.trim().trim_end_matches('/').to_owned();
if let Some(raw) = credential.attributes.get("base_urls").filter(|r| !r.trim().is_empty()) {
let urls: Vec<String> = raw.split(',').map(trim).filter(|u| !u.is_empty()).collect();
if !urls.is_empty() {
return urls;
}
}
let own = credential
.attributes
.get("base_url")
.map(|v| trim(v))
.filter(|v| !v.is_empty())
.or_else(|| credential.str("base_url").map(trim).filter(|v| !v.is_empty()));
vec![own.unwrap_or_else(|| DAILY_BASE_URL.to_owned())]
}
async fn probe(client: &wreq::Client, urls: &[String], token: &str, timeout: Duration) -> (Hints, Status) {
let all = async {
if let [url] = urls {
return fetch_one(client, url, token, timeout).await;
}
let mut pending: FuturesUnordered<_> = urls.iter().map(|url| fetch_one(client, url, token, timeout)).collect();
let (mut first, mut status) = (None, Status::Transient);
while let Some((hints, outcome)) = pending.next().await {
match outcome {
Status::Success if !hints.is_empty() => return (hints, Status::Success),
Status::Success => {
first.get_or_insert(hints);
}
Status::AuthError => status = Status::AuthError,
Status::Transient => {}
}
}
match first {
Some(hints) => (hints, Status::Success),
None => (Hints::new(), status),
}
};
tokio::time::timeout(timeout, all)
.await
.unwrap_or((Hints::new(), Status::Transient))
}
async fn fetch_one(client: &wreq::Client, base: &str, token: &str, timeout: Duration) -> (Hints, Status) {
let mut headers = GoHeaders::new();
headers.set("Content-Type", "application/json");
headers.set("Authorization", format!("Bearer {token}"));
headers.set("User-Agent", crate::antigravity_version::user_agent());
headers.set("Connection", "close");
let url = format!("{}{MODELS_PATH}", base.trim_end_matches('/'));
let body = Some(Bytes::from_static(b"{}"));
let Ok(answer) = proxy::request(client, wreq::Method::POST, &url, headers, body, Some(timeout)).await else {
return (Hints::new(), Status::Transient);
};
if matches!(answer.status, 401 | 403) {
return (Hints::new(), Status::AuthError);
}
let Ok(body) = proxy::read_all(answer.body, usize::MAX, false).await else {
return (Hints::new(), Status::Transient);
};
if !(200..300).contains(&answer.status) {
return (Hints::new(), Status::Transient);
}
match parse(&body) {
Some(hints) => (hints, Status::Success),
None => (Hints::new(), Status::Transient),
}
}
fn parse(body: &[u8]) -> Option<Hints> {
if !gj::std_valid(body) {
return None;
}
let root = gj::parse(body);
match root.kind {
Kind::Null => return Some(Hints::new()),
Kind::Json if root.is_object() => {}
_ => return None,
}
let mut ids: Vec<Vec<u8>> = Vec::new();
let mut ok = true;
root.each(|key, value| {
if !key.str().go_eq_fold("webSearchModelIds") {
return true;
}
match value.kind {
Kind::Null => ids.clear(),
Kind::Json if value.is_array() => {
ids.clear();
for item in value.array() {
match item.kind {
Kind::String => ids.push(item.bytes().into_owned()),
Kind::Null => ids.push(Vec::new()),
_ => ok = false,
}
}
}
_ => ok = false,
}
true
});
ok.then(|| {
ids.iter()
.map(|id| String::from_utf8_lossy(trim_space(id)).go_lower())
.filter(|id| !id.is_empty())
.collect()
})
}
#[derive(Default)]
pub(crate) struct Probes {
seen: Mutex<HashMap<String, (u64, u64)>>,
next: std::sync::atomic::AtomicU64,
}
impl Probes {
pub(crate) fn registered(self: &Arc<Self>, clients: &GoClients, credentials: &[Arc<Credential>], cfg: &Config) {
let Ok(runtime) = tokio::runtime::Handle::try_current() else {
return;
};
let present: HashSet<&str> = credentials
.iter()
.filter(|c| c.provider == crate::antigravity::PROVIDER)
.map(|c| c.id.as_str())
.collect();
let mut seen = lock(&self.seen);
seen.retain(|id, _| {
let keep = present.contains(id.as_str());
if !keep {
cpa_core::registry::dynamic::set_web_search_hints(id, Hints::new());
}
keep
});
for credential in credentials {
if credential.provider != crate::antigravity::PROVIDER || credential.disabled {
continue;
}
if seen
.get(&credential.id)
.is_some_and(|(revision, _)| *revision == credential.revision)
{
continue;
}
let ticket = self.next.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
seen.insert(credential.id.clone(), (credential.revision, ticket));
let proxy_url = Proxy::effective_url(credential, cfg);
let client = clients.get(&Proxy::parse(&proxy_url));
let (probes, credential) = (self.clone(), credential.clone());
runtime.spawn(async move {
let hints = shared().fetch(&client, &credential, &proxy_url).await;
let seen = lock(&probes.seen);
if seen.get(&credential.id).map(|(_, t)| *t) == Some(ticket) {
cpa_core::registry::dynamic::set_web_search_hints(&credential.id, hints);
}
});
}
}
}
#[cfg(test)]
#[path = "antigravity_models_tests.rs"]
mod tests;