aether_ai/
gemini.rs

1//! Google Gemini provider implementation.
2//!
3//! Supports Gemini Pro and other Google AI models.
4
5use aether_core::{
6    AetherError, AiProvider, ProviderConfig, Result,
7    provider::{GenerationRequest, GenerationResponse},
8    SlotKind,
9};
10use async_trait::async_trait;
11use reqwest::Client;
12use serde::{Deserialize, Serialize};
13use tracing::{debug, instrument};
14use aether_core::provider::StreamResponse;
15use futures::stream::{BoxStream, StreamExt};
16
17const GEMINI_API_BASE: &str = "https://generativelanguage.googleapis.com/v1beta/models";
18
19/// Google Gemini provider for code generation.
20#[derive(Debug, Clone)]
21pub struct GeminiProvider {
22    client: Client,
23    config: ProviderConfig,
24}
25
26// Request structures
27#[derive(Debug, Serialize)]
28struct GeminiRequest {
29    contents: Vec<Content>,
30    #[serde(skip_serializing_if = "Option::is_none")]
31    generation_config: Option<GenerationConfig>,
32}
33
34#[derive(Debug, Serialize)]
35struct Content {
36    parts: Vec<Part>,
37    role: String,
38}
39
40#[derive(Debug, Serialize)]
41struct Part {
42    text: String,
43}
44
45#[derive(Debug, Serialize)]
46#[serde(rename_all = "camelCase")]
47struct GenerationConfig {
48    #[serde(skip_serializing_if = "Option::is_none")]
49    temperature: Option<f32>,
50    #[serde(skip_serializing_if = "Option::is_none")]
51    max_output_tokens: Option<u32>,
52}
53
54// Response structures
55#[derive(Debug, Deserialize)]
56struct GeminiResponse {
57    candidates: Option<Vec<Candidate>>,
58    usage_metadata: Option<UsageMetadata>,
59}
60
61#[derive(Debug, Deserialize)]
62struct Candidate {
63    content: ContentResponse,
64}
65
66#[derive(Debug, Deserialize)]
67struct ContentResponse {
68    parts: Vec<PartResponse>,
69}
70
71#[derive(Debug, Deserialize)]
72struct PartResponse {
73    text: String,
74}
75
76#[derive(Debug, Deserialize)]
77#[serde(rename_all = "camelCase")]
78struct UsageMetadata {
79    total_token_count: u32,
80}
81
82impl GeminiProvider {
83    /// Create a new Gemini provider with the given configuration.
84    pub fn new(config: ProviderConfig) -> Result<Self> {
85        let timeout = config.timeout_seconds.unwrap_or(60);
86        let client = Client::builder()
87            .timeout(std::time::Duration::from_secs(timeout))
88            .build()
89            .map_err(|e| AetherError::NetworkError(e.to_string()))?;
90
91        Ok(Self { client, config })
92    }
93
94    /// Create a provider from environment variables.
95    ///
96    /// Reads `GOOGLE_API_KEY` and optionally `GEMINI_MODEL`.
97    pub fn from_env() -> Result<Self> {
98        let api_key = std::env::var("GOOGLE_API_KEY")
99            .map_err(|_| AetherError::ConfigError("GOOGLE_API_KEY not set".to_string()))?;
100
101        let model = std::env::var("GEMINI_MODEL").unwrap_or_else(|_| "gemini-1.5-pro".to_string());
102        
103        // Google API key is query param, not header like OpenAI
104        // We store it in config.api_key but will use it in URL
105        let config = ProviderConfig::new(api_key, model);
106        Self::new(config)
107    }
108
109    /// Build the specific prompt for Gemini
110    fn build_prompt(&self, kind: &SlotKind, context: Option<&str>, user_prompt: &str) -> String {
111        let base_instructions = match kind {
112            SlotKind::Html => "Generate valid HTML5 markup.",
113            SlotKind::Css => "Generate valid CSS styles.",
114            SlotKind::JavaScript => "Generate valid JavaScript code.",
115            SlotKind::Function => "Generate a complete function definition.",
116            SlotKind::Class => "Generate a complete class/struct definition.",
117            SlotKind::Component => "Generate a complete component with HTML, CSS, and JavaScript as needed.",
118            _ => "Generate code based on the request.",
119        };
120
121        let context_str = context
122            .map(|c| format!("\nContext:\n{}", c))
123            .unwrap_or_default();
124
125        format!(
126            "Role: Code Generator. Task: {}\n{}\nRequest: {}\nOutput only raw code, no markdown.",
127            base_instructions, context_str, user_prompt
128        )
129    }
130}
131
132#[async_trait]
133impl AiProvider for GeminiProvider {
134    fn name(&self) -> &str {
135        "gemini"
136    }
137
138    #[instrument(skip(self, request), fields(slot = %request.slot.name))]
139    async fn generate(&self, request: GenerationRequest) -> Result<GenerationResponse> {
140        debug!("Generating code with Gemini for slot: {}", request.slot.name);
141
142        let api_key = self.config.resolve_api_key().await?;
143
144        // Gemini API is slightly different (no system role in v1beta easily)
145        // so we verify robust prompt engineering in the user message
146        let full_prompt = self.build_prompt(&request.slot.kind, request.context.as_deref(), &request.slot.prompt);
147
148        let contents = vec![Content {
149            role: "user".to_string(),
150            parts: vec![Part { text: full_prompt }],
151        }];
152
153        let temperature = request.slot.temperature.or(self.config.temperature);
154        let api_request = GeminiRequest {
155            contents,
156            generation_config: Some(GenerationConfig {
157                temperature,
158                max_output_tokens: self.config.max_tokens,
159            }),
160        };
161
162        let url = format!(
163            "{}/{}:generateContent?key={}",
164            GEMINI_API_BASE, self.config.model, api_key
165        );
166
167        let response = self
168            .client
169            .post(&url)
170            .header("Content-Type", "application/json")
171            .json(&api_request)
172            .send()
173            .await
174            .map_err(|e| AetherError::NetworkError(e.to_string()))?;
175
176        if !response.status().is_success() {
177            let status = response.status();
178            let body = response.text().await.unwrap_or_default();
179            return Err(AetherError::ProviderError(format!(
180                "API error {}: {}",
181                status, body
182            )));
183        }
184
185        let gemini_response: GeminiResponse = response
186            .json()
187            .await
188            .map_err(|e| AetherError::ProviderError(e.to_string()))?;
189
190        // Extract text from the first candidate
191        let code = gemini_response
192            .candidates
193            .as_ref()
194            .and_then(|c| c.first())
195            .and_then(|c| c.content.parts.first())
196            .map(|p| p.text.clone())
197            .ok_or_else(|| AetherError::ProviderError("No content generated".to_string()))?;
198
199        // Clean up markdown
200        let code = code.trim().trim_start_matches("```").trim_end_matches("```");
201        // Sometimes it includes the language name like ```rust ... ```
202        let code = if let Some(newline_idx) = code.find('\n') {
203            if code[..newline_idx].chars().all(char::is_alphanumeric) {
204                &code[newline_idx + 1..]
205            } else {
206                code
207            }
208        } else {
209            code
210        };
211
212        Ok(GenerationResponse {
213            code: code.to_string(),
214            tokens_used: gemini_response.usage_metadata.map(|u| u.total_token_count),
215            metadata: None,
216        })
217    }
218
219    fn generate_stream(
220        &self,
221        request: GenerationRequest,
222    ) -> BoxStream<'static, Result<StreamResponse>> {
223        let client = self.client.clone();
224        let config = self.config.clone();
225        let full_prompt = self.build_prompt(&request.slot.kind, request.context.as_deref(), &request.slot.prompt);
226        
227        let temperature = request.slot.temperature.or(config.temperature);
228        let api_request = GeminiRequest {
229            contents: vec![Content {
230                role: "user".to_string(),
231                parts: vec![Part { text: full_prompt }],
232            }],
233            generation_config: Some(GenerationConfig {
234                temperature,
235                max_output_tokens: config.max_tokens,
236            }),
237        };
238
239        let stream = async_stream::stream! {
240            let api_key = match config.resolve_api_key().await {
241                Ok(k) => k,
242                Err(e) => {
243                    yield Err(e);
244                    return;
245                }
246            };
247
248            let url = format!(
249                "{}/{}:streamGenerateContent?alt=sse&key={}",
250                GEMINI_API_BASE, config.model, api_key
251            );
252
253            let response = client
254                .post(&url)
255                .header("Content-Type", "application/json")
256                .json(&api_request)
257                .send()
258                .await
259                .map_err(|e| aether_core::AetherError::NetworkError(e.to_string()));
260
261            let response = match response {
262                Ok(r) => r,
263                Err(e) => {
264                    yield Err(e);
265                    return;
266                }
267            };
268
269            if !response.status().is_success() {
270                let status = response.status();
271                let body = response.text().await.unwrap_or_default();
272                yield Err(aether_core::AetherError::ProviderError(format!(
273                    "API error {}: {}",
274                    status, body
275                )));
276                return;
277            }
278
279            let mut stream = response.bytes_stream();
280            
281            while let Some(chunk_result) = stream.next().await {
282                let chunk = match chunk_result {
283                    Ok(c) => c,
284                    Err(e) => {
285                        yield Err(aether_core::AetherError::NetworkError(e.to_string()));
286                        break;
287                    }
288                };
289
290                let text = String::from_utf8_lossy(&chunk);
291                for line in text.lines() {
292                    let line = line.trim();
293                    if line.is_empty() { continue; }
294                    
295                    if let Some(event_data) = line.strip_prefix("data: ") {
296                        if let Ok(gemini_resp) = serde_json::from_str::<GeminiResponse>(event_data) {
297                            if let Some(candidate) = gemini_resp.candidates.as_ref().and_then(|c| c.first()) {
298                                if let Some(part) = candidate.content.parts.first() {
299                                    yield Ok(StreamResponse {
300                                        delta: part.text.clone(),
301                                        metadata: None,
302                                    });
303                                }
304                            }
305                        }
306                    }
307                }
308            }
309        };
310
311        Box::pin(stream)
312    }
313
314    async fn health_check(&self) -> Result<bool> {
315        let api_key = self.config.resolve_api_key().await?;
316        // Minimal check - try to get model info
317         let url = format!(
318            "{}/{}?key={}",
319            GEMINI_API_BASE, self.config.model, api_key
320        );
321
322        let response = self
323            .client
324            .get(&url)
325            .send()
326            .await
327            .map_err(|e| AetherError::NetworkError(e.to_string()))?;
328
329        Ok(response.status().is_success())
330    }
331}