1use std::collections::HashMap;
7use std::env;
8
9use llm::{
10 LLMProvider,
11 builder::{LLMBackend, LLMBuilder},
12 chat::ChatMessage,
13};
14use thiserror::Error;
15
16#[derive(Error, Debug)]
17pub enum VibelandConfigError {
18 #[error("LLM error: {0}")]
19 LlmError(String),
20
21 #[error("Configuration error: {0}")]
22 ConfigError(String),
23
24 #[error("Missing environment variable: {0}")]
25 MissingVariable(String),
26}
27
28pub struct LlmWrapper {
30 provider: Box<dyn LLMProvider>,
31}
32
33impl LlmWrapper {
34 pub fn from_env() -> Result<Self, VibelandConfigError> {
36 let mode = env::var("VIBELAND_LLM_MODE").unwrap_or_else(|_| {
37 #[cfg(feature = "ollama")]
38 return "ollama".to_string();
39 #[cfg(all(feature = "remote", not(feature = "ollama")))]
40 return "remote".to_string();
41 });
42
43 let provider = match mode.as_str() {
44 #[cfg(any(
45 feature = "openai",
46 feature = "anthropic",
47 feature = "google",
48 feature = "deepseek",
49 feature = "xai",
50 feature = "groq"
51 ))]
52 "remote" => {
53 let backend_name = env::var("VIBELAND_REMOTE_PROVIDER").map_err(|_| {
54 VibelandConfigError::MissingVariable("VIBELAND_REMOTE_PROVIDER".to_string())
55 })?;
56
57 let model = env::var("VIBELAND_REMOTE_MODEL").map_err(|_| {
58 VibelandConfigError::MissingVariable("VIBELAND_REMOTE_MODEL".to_string())
59 })?;
60
61 let api_key = env::var("VIBELAND_REMOTE_API_KEY").map_err(|_| {
62 VibelandConfigError::MissingVariable("VIBELAND_REMOTE_API_KEY".to_string())
63 })?;
64
65 let backend = match backend_name.as_str() {
66 #[cfg(feature = "openai")]
67 "openai" => LLMBackend::OpenAI,
68 #[cfg(feature = "anthropic")]
69 "anthropic" => LLMBackend::Anthropic,
70 #[cfg(feature = "google")]
71 "google" => LLMBackend::Google,
72 #[cfg(feature = "deepseek")]
73 "deepseek" => LLMBackend::DeepSeek,
74 #[cfg(feature = "xai")]
75 "xai" => LLMBackend::XAI,
76 #[cfg(feature = "groq")]
77 "groq" => LLMBackend::Groq,
78 _ => {
79 return Err(VibelandConfigError::ConfigError(format!(
80 "Unsupported provider: {} (feature may not be enabled)",
81 backend_name
82 )));
83 }
84 };
85
86 let mut builder = LLMBuilder::new()
87 .backend(backend)
88 .api_key(api_key)
89 .model(model);
90
91 if let Ok(max_tokens) = env::var("VIBELAND_REMOTE_MAX_TOKENS") {
93 if let Ok(tokens) = max_tokens.parse::<u32>() {
94 builder = builder.max_tokens(tokens);
95 }
96 }
97
98 if let Ok(temp) = env::var("VIBELAND_REMOTE_TEMPERATURE") {
99 if let Ok(temperature) = temp.parse::<f32>() {
100 builder = builder.temperature(temperature);
101 }
102 }
103
104 builder
105 .build()
106 .map_err(|e| VibelandConfigError::LlmError(e.to_string()))?
107 }
108 #[cfg(feature = "ollama")]
109 "ollama" => {
110 let model = env::var("VIBELAND_OLLAMA_MODEL").map_err(|_| {
111 VibelandConfigError::MissingVariable("VIBELAND_OLLAMA_MODEL".to_string())
112 })?;
113
114 let base_url = env::var("VIBELAND_OLLAMA_URL")
115 .unwrap_or_else(|_| "http://localhost:11434".to_string());
116
117 let mut builder = LLMBuilder::new()
118 .backend(LLMBackend::Ollama)
119 .base_url(&base_url)
120 .model(model);
121
122 if let Ok(max_tokens) = env::var("VIBELAND_OLLAMA_MAX_TOKENS") {
124 if let Ok(tokens) = max_tokens.parse::<u32>() {
125 builder = builder.max_tokens(tokens);
126 }
127 }
128
129 if let Ok(temp) = env::var("VIBELAND_OLLAMA_TEMPERATURE") {
130 if let Ok(temperature) = temp.parse::<f32>() {
131 builder = builder.temperature(temperature);
132 }
133 }
134
135 builder
136 .build()
137 .map_err(|e| VibelandConfigError::LlmError(e.to_string()))?
138 }
139 _ => {
140 return Err(VibelandConfigError::ConfigError(format!(
141 "Unsupported mode: {}",
142 mode
143 )));
144 }
145 };
146
147 Ok(Self { provider })
148 }
149
150 pub async fn execute_prompt(&self, prompt: &str) -> Result<String, VibelandConfigError> {
152 let message = ChatMessage::user().content(prompt).build();
153 let response = self
154 .provider
155 .chat(&[message])
156 .await
157 .map_err(|e| VibelandConfigError::LlmError(e.to_string()))?;
158
159 response
160 .text()
161 .ok_or_else(|| VibelandConfigError::LlmError("No response text".to_string()))
162 }
163
164 pub async fn execute_prompt_with_vars(
166 &self,
167 template: &str,
168 variables: &HashMap<String, String>,
169 ) -> Result<String, VibelandConfigError> {
170 let mut prompt = template.to_string();
171 for (key, value) in variables {
172 prompt = prompt.replace(&format!("{{{}}}", key), value);
173 }
174 self.execute_prompt(&prompt).await
175 }
176}
177
178pub fn read_client_env() -> Result<LlmWrapper, Box<dyn std::error::Error>> {
180 LlmWrapper::from_env().map_err(|e| e.into())
181}