gitdataai/libs/agent/sync.rs
ZhenYi 96b92fe487
Some checks are pending
CI / Rust Lint & Check (push) Waiting to run
CI / Rust Tests (push) Waiting to run
CI / Frontend Lint & Type Check (push) Waiting to run
CI / Frontend Build (push) Blocked by required conditions
feat(workspace): initialize Rust workspace with core services and dependencies
2026-05-01 00:40:29 +08:00

41 lines
1.1 KiB
Rust

//! Model sync from OpenRouter — syncs AI model metadata into the local database.
use crate::error::AgentError;
/// Response from `GET /v1/models`.
#[derive(Debug, serde::Deserialize)]
pub struct ModelsListResponse {
pub data: Vec<ModelEntry>,
}
#[derive(Debug, serde::Deserialize)]
pub struct ModelEntry {
pub id: String,
}
pub async fn list_accessible_models(
client: &reqwest::Client,
base_url: &str,
api_key: &str,
) -> Result<std::collections::HashSet<String>, AgentError> {
let base = base_url.trim_end_matches('/');
let url = if base.ends_with("/v1") {
format!("{}/models", base)
} else {
format!("{}/v1/models", base)
};
let resp = client
.get(&url)
.header("Authorization", format!("Bearer {}", api_key))
.send()
.await
.map_err(|e| AgentError::Internal(format!("failed to list models: {}", e)))?;
let body: ModelsListResponse = resp
.json()
.await
.map_err(|e| AgentError::Internal(format!("failed to parse models response: {}", e)))?;
Ok(body.data.into_iter().map(|m| m.id).collect())
}