config file, bugfixes

This commit is contained in:
leach
2025-08-15 15:41:32 -04:00
parent b171a6b2b2
commit 7d237f692c
7 changed files with 364 additions and 29 deletions

View File

@@ -3,7 +3,9 @@ use reqwest::Client;
use serde::Deserialize;
use serde_json::{json, Value};
use std::env;
use std::time::Duration;
use crate::config::Config;
use super::{provider::Provider, session::Message};
#[derive(Debug)]
@@ -128,7 +130,8 @@ struct SearchAction {
#[allow(dead_code)]
#[serde(rename = "type")]
action_type: String,
query: String,
#[serde(default)]
query: Option<String>,
}
#[derive(Deserialize)]
@@ -164,19 +167,19 @@ struct AnthropicContent {
}
impl OpenAIClient {
pub fn new() -> Result<Self> {
pub fn new(config: &Config) -> Result<Self> {
let api_key = env::var("OPENAI_API_KEY")
.context("OPENAI_API_KEY environment variable is required")?;
let base_url = env::var("OPENAI_BASE_URL")
.unwrap_or_else(|_| "https://api.openai.com/v1".to_string());
let client = Client::new();
let client = Client::builder()
.timeout(Duration::from_secs(config.api.request_timeout_seconds))
.build()
.context("Failed to create HTTP client")?;
Ok(Self {
client,
api_key,
base_url,
base_url: config.api.openai_base_url.clone(),
})
}
@@ -368,7 +371,11 @@ impl OpenAIClient {
if item.status.as_deref() == Some("completed") {
search_count += 1;
if let Some(action) = &item.action {
final_content.push_str(&format!("🔍 Search {}: \"{}\"\n", search_count, action.query));
if let Some(query) = &action.query {
final_content.push_str(&format!("🔍 Search {}: \"{}\"\n", search_count, query));
} else {
final_content.push_str(&format!("🔍 Search {}: [no query specified]\n", search_count));
}
}
}
}
@@ -439,17 +446,19 @@ impl OpenAIClient {
}
impl AnthropicClient {
pub fn new() -> Result<Self> {
pub fn new(config: &Config) -> Result<Self> {
let api_key = env::var("ANTHROPIC_API_KEY")
.context("ANTHROPIC_API_KEY environment variable is required")?;
let base_url = "https://api.anthropic.com/v1".to_string();
let client = Client::new();
let client = Client::builder()
.timeout(Duration::from_secs(config.api.request_timeout_seconds))
.build()
.context("Failed to create HTTP client")?;
Ok(Self {
client,
api_key,
base_url,
base_url: config.api.anthropic_base_url.clone(),
})
}
@@ -489,9 +498,10 @@ impl AnthropicClient {
let (system_prompt, user_messages) = Self::convert_messages(messages);
let config = crate::config::Config::load().unwrap_or_default();
let mut payload = json!({
"model": model,
"max_tokens": 4096,
"max_tokens": config.limits.max_tokens_anthropic,
"messages": user_messages
});
@@ -504,7 +514,7 @@ impl AnthropicClient {
.post(&url)
.header("x-api-key", &self.api_key)
.header("Content-Type", "application/json")
.header("anthropic-version", "2023-06-01")
.header("anthropic-version", &config.api.anthropic_version)
.json(&payload)
.send()
.await
@@ -544,16 +554,16 @@ impl AnthropicClient {
}
}
pub fn create_client(model: &str) -> Result<ChatClient> {
pub fn create_client(model: &str, config: &Config) -> Result<ChatClient> {
let provider = super::provider::get_provider_for_model(model);
match provider {
Provider::OpenAI => {
let client = OpenAIClient::new()?;
let client = OpenAIClient::new(config)?;
Ok(ChatClient::OpenAI(client))
}
Provider::Anthropic => {
let client = AnthropicClient::new()?;
let client = AnthropicClient::new(config)?;
Ok(ChatClient::Anthropic(client))
}
}