use crate::agent::backend::BackendConfig;
use crate::error::{IronError, Result};
use serde::{Deserialize, Serialize};
#[allow(unused_imports)]
use std::time::Instant;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatMessage {
pub role: String,
pub content: String,
}
#[derive(Debug, Serialize)]
#[allow(dead_code)]
struct OllamaChatRequest {
model: String,
messages: Vec<ChatMessage>,
stream: bool,
#[serde(skip_serializing_if = "Option::is_none")]
options: Option<OllamaOptions>,
}
#[derive(Debug, Serialize)]
#[allow(dead_code)]
struct OllamaOptions {
temperature: f32,
num_predict: i32,
}
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct OllamaChatResponse {
message: ChatMessage,
#[allow(dead_code)]
done: bool,
}
#[derive(Debug, Serialize)]
#[allow(dead_code)]
struct ChatCompletionRequest {
model: String,
messages: Vec<ChatMessage>,
#[serde(skip_serializing_if = "Option::is_none")]
temperature: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
max_tokens: Option<usize>,
}
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct ChatCompletionResponse {
choices: Vec<ChatChoice>,
#[allow(dead_code)]
usage: Option<Usage>,
}
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct ChatChoice {
message: ChatMessage,
#[allow(dead_code)]
finish_reason: Option<String>,
}
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct Usage {
prompt_tokens: usize,
completion_tokens: usize,
total_tokens: usize,
}
pub struct RemoteClient {
#[allow(dead_code)]
pub(super) config: BackendConfig,
#[cfg(feature = "remote-backends")]
pub(super) http_client: reqwest::blocking::Client,
}
impl RemoteClient {
pub fn new(config: BackendConfig) -> Result<Self> {
config.validate()?;
#[cfg(feature = "remote-backends")]
{
let mut builder = reqwest::blocking::Client::builder()
.timeout(config.timeout)
.pool_max_idle_per_host(0);
if matches!(
config.backend_type,
crate::agent::backend::BackendType::IronWorks
| crate::agent::backend::BackendType::RemoteOllama
| crate::agent::backend::BackendType::RemoteLMStudio
| crate::agent::backend::BackendType::RemoteMlx
| crate::agent::backend::BackendType::RemoteVllm
| crate::agent::backend::BackendType::RemoteTensorRT
) {
builder = builder.timeout(std::time::Duration::from_secs(120)); }
let http_client = builder
.build()
.map_err(|e| IronError::Network(format!("Failed to create HTTP client: {}", e)))?;
Ok(Self {
config,
http_client,
})
}
#[cfg(not(feature = "remote-backends"))]
{
Err(IronError::NotImplemented(
"Remote backends require 'remote-backends' feature".into(),
))
}
}
pub fn query(&self, system_prompt: &str, user_query: &str) -> Result<(String, u64)> {
#[cfg(feature = "remote-backends")]
{
let start = Instant::now();
if let crate::agent::backend::BackendType::Cli(provider) = self.config.backend_type {
let client = crate::agent::local::CliClient::detect(provider).ok_or_else(|| {
IronError::Configuration(format!(
"{} is configured but its executable is no longer on PATH",
provider.display_name()
))
})?;
return client.with_timeout(self.config.timeout).query_blocking(
system_prompt,
user_query,
Some(&self.config.model_id),
);
}
let messages = vec![
ChatMessage {
role: "system".to_string(),
content: system_prompt.to_string(),
},
ChatMessage {
role: "user".to_string(),
content: user_query.to_string(),
},
];
let endpoint = self
.config
.endpoint
.as_ref()
.ok_or_else(|| IronError::Configuration("No endpoint configured".into()))?;
if matches!(
self.config.backend_type,
crate::agent::backend::BackendType::RemoteOllama
) {
let request = OllamaChatRequest {
model: self.config.model_id.clone(),
messages,
stream: false,
options: Some(OllamaOptions {
temperature: self.config.temperature,
num_predict: self.config.max_tokens as i32,
}),
};
let url = format!("{}/api/chat", endpoint);
let response = self
.http_client
.post(&url)
.json(&request)
.send()
.map_err(|e| IronError::Network(format!("Request failed: {}", e)))?;
if !response.status().is_success() {
let status = response.status();
let error_text = response
.text()
.unwrap_or_else(|_| "Unknown error".to_string());
return Err(IronError::Network(format!(
"API error {}: {}",
status, error_text
)));
}
let ollama_response: OllamaChatResponse = response
.json()
.map_err(|e| IronError::Parse(format!("Failed to parse response: {}", e)))?;
let elapsed = start.elapsed().as_millis() as u64;
return Ok((ollama_response.message.content, elapsed));
}
let request = ChatCompletionRequest {
model: self.config.model_id.clone(),
messages,
temperature: Some(self.config.temperature),
max_tokens: Some(self.config.max_tokens),
};
let url = format!("{}/chat/completions", endpoint);
let mut req = self.http_client.post(&url).json(&request);
if let Some(ref api_key) = self.config.api_key {
req = match self.config.backend_type {
crate::agent::backend::BackendType::RemoteAnthropic => req
.header("x-api-key", api_key)
.header("anthropic-version", "2023-06-01"),
_ => req.header("Authorization", format!("Bearer {}", api_key)),
};
}
let response = req
.send()
.map_err(|e| IronError::Network(format!("Request failed: {}", e)))?;
if !response.status().is_success() {
let status = response.status();
let error_text = response
.text()
.unwrap_or_else(|_| "Unknown error".to_string());
return Err(IronError::Network(format!(
"API error {}: {}",
status, error_text
)));
}
let completion: ChatCompletionResponse = response
.json()
.map_err(|e| IronError::Parse(format!("Failed to parse response: {}", e)))?;
let response_text = completion
.choices
.first()
.ok_or_else(|| IronError::Parse("No choices in response".into()))?
.message
.content
.clone();
let elapsed = start.elapsed().as_millis() as u64;
Ok((response_text, elapsed))
}
#[cfg(not(feature = "remote-backends"))]
{
let _ = (system_prompt, user_query);
Err(IronError::NotImplemented(
"Remote backends require 'remote-backends' feature".into(),
))
}
}
pub fn is_available(&self) -> bool {
#[cfg(feature = "remote-backends")]
{
if let Some(ref endpoint) = self.config.endpoint {
let path = if matches!(
self.config.backend_type,
crate::agent::backend::BackendType::RemoteOllama
) {
"/api/tags"
} else {
"/models"
};
let url = format!("{}{}", endpoint, path);
self.http_client
.get(&url)
.timeout(std::time::Duration::from_secs(2))
.send()
.map(|response| response.status().is_success())
.unwrap_or(false)
} else {
false
}
}
#[cfg(not(feature = "remote-backends"))]
{
false
}
}
pub fn list_models(&self) -> Result<Vec<String>> {
#[cfg(feature = "remote-backends")]
{
let endpoint = self
.config
.endpoint
.as_ref()
.ok_or_else(|| IronError::Configuration("No endpoint configured".into()))?;
let url = format!("{}/models", endpoint);
let response = self
.http_client
.get(&url)
.send()
.map_err(|e| IronError::Network(format!("Failed to list models: {}", e)))?;
if !response.status().is_success() {
return Err(IronError::Network(format!(
"Failed to list models: {}",
response.status()
)));
}
let json: serde_json::Value = response
.json()
.map_err(|e| IronError::Parse(format!("Failed to parse models list: {}", e)))?;
if let Some(data) = json.get("data").and_then(|d| d.as_array()) {
let models = data
.iter()
.filter_map(|m| m.get("id").and_then(|id| id.as_str()))
.map(|s| s.to_string())
.collect();
return Ok(models);
}
if let Some(models) = json.get("models").and_then(|m| m.as_array()) {
let model_names = models
.iter()
.filter_map(|m| m.get("name").and_then(|n| n.as_str()))
.map(|s| s.to_string())
.collect();
return Ok(model_names);
}
Err(IronError::Parse(
"model listing has neither a `data` nor a `models` array".into(),
))
}
#[cfg(not(feature = "remote-backends"))]
{
Err(IronError::NotImplemented(
"Remote backends require 'remote-backends' feature".into(),
))
}
}
}
pub struct RemoteClientBuilder {
config: BackendConfig,
}
impl RemoteClientBuilder {
pub fn openai(model: &str) -> Self {
Self {
config: BackendConfig::openai(model, None),
}
}
pub fn anthropic(model: &str) -> Self {
Self {
config: BackendConfig::anthropic(model, None),
}
}
pub fn ollama(model: &str) -> Self {
Self {
config: BackendConfig::ollama(model),
}
}
pub fn mlx(model: &str) -> Self {
Self {
config: BackendConfig::mlx(model),
}
}
pub fn lm_studio(model: &str) -> Self {
Self {
config: BackendConfig::lm_studio(model),
}
}
pub fn api_key(mut self, key: String) -> Self {
self.config.api_key = Some(key);
self
}
pub fn endpoint(mut self, endpoint: String) -> Self {
self.config.endpoint = Some(endpoint);
self
}
pub fn temperature(mut self, temp: f32) -> Self {
self.config.temperature = temp;
self
}
pub fn max_tokens(mut self, tokens: usize) -> Self {
self.config.max_tokens = tokens;
self
}
pub fn build(self) -> Result<RemoteClient> {
RemoteClient::new(self.config)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_remote_client_builder() {
let builder = RemoteClientBuilder::ollama("llama3")
.temperature(0.5)
.max_tokens(512);
assert_eq!(builder.config.model_id, "llama3");
assert_eq!(builder.config.temperature, 0.5);
assert_eq!(builder.config.max_tokens, 512);
}
}