cli_engineer 2.0.0

An autonomous CLI coding agent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use anyhow::{Context, Result, anyhow};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::env;
use std::sync::Arc;
use log::{debug, error};

use crate::llm_manager::LLMProvider;
use crate::event_bus::{Event, EventBus};

/// OpenAI API provider implementation
pub struct OpenAIProvider {
    api_key: String,
    model: String,
    base_url: String,
    temperature: f32,
    event_bus: Option<Arc<EventBus>>,
    cost_per_1m_input_tokens: f32,
    cost_per_1m_output_tokens: f32,
}

#[derive(Debug, Serialize)]
struct OpenAIRequest {
    model: String,
    input: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    reasoning: Option<OpenAIReasoning>,
}

#[derive(Debug, Serialize)]
struct OpenAIReasoning {
    summary: String, // "auto" or "detailed"
}

#[derive(Debug, Deserialize)]
struct OpenAIResponse {
    #[allow(dead_code)]
    id: String,
    #[allow(dead_code)]
    object: String,
    #[allow(dead_code)]
    created_at: u64,
    #[serde(default)]
    #[allow(dead_code)]
    status: Option<String>,
    #[serde(default)]
    #[allow(dead_code)]
    error: Option<serde_json::Value>,
    #[serde(default)]
    #[allow(dead_code)]
    incomplete_details: Option<serde_json::Value>,
    #[serde(default)]
    #[allow(dead_code)]
    instructions: Option<serde_json::Value>,
    #[serde(default)]
    #[allow(dead_code)]
    max_output_tokens: Option<u64>,
    #[serde(default)]
    #[allow(dead_code)]
    model: Option<String>,
    output: Vec<ResponseMessage>,
    #[serde(default)]
    #[allow(dead_code)]
    parallel_tool_calls: Option<bool>,
    #[serde(default)]
    #[allow(dead_code)]
    previous_response_id: Option<String>,
    #[serde(default)]
    reasoning: Option<ResponseReasoning>,
    #[serde(default)]
    #[allow(dead_code)]
    store: Option<bool>,
    #[serde(default)]
    #[allow(dead_code)]
    temperature: Option<f64>,
    #[serde(default)]
    #[allow(dead_code)]
    text: Option<serde_json::Value>,
    #[serde(default)]
    #[allow(dead_code)]
    tool_choice: Option<String>,
    #[serde(default)]
    #[allow(dead_code)]
    tools: Option<Vec<serde_json::Value>>,
    #[serde(default)]
    #[allow(dead_code)]
    top_p: Option<f64>,
    #[serde(default)]
    #[allow(dead_code)]
    truncation: Option<String>,
    #[serde(default)]
    usage: Option<Usage>,
    #[serde(default)]
    #[allow(dead_code)]
    user: Option<serde_json::Value>,
    #[serde(default)]
    #[allow(dead_code)]
    metadata: Option<serde_json::Value>,
}

#[derive(Debug, Deserialize)]
struct ResponseMessage {
    #[serde(rename = "type")]
    #[allow(dead_code)]
    message_type: String,
    #[allow(dead_code)]
    id: String,
    #[serde(default)]
    #[allow(dead_code)]
    status: Option<String>,
    #[serde(default)]
    #[allow(dead_code)]
    role: Option<String>,
    #[serde(default)]
    content: Option<Vec<ContentItem>>,
    #[serde(default)]
    #[allow(dead_code)]
    summary: Option<Vec<serde_json::Value>>,
}

#[derive(Debug, Deserialize)]
struct ContentItem {
    #[serde(rename = "type")]
    content_type: String,
    text: String,
    #[serde(default)]
    #[allow(dead_code)]
    annotations: Vec<serde_json::Value>,
}

#[derive(Debug, Deserialize)]
struct ResponseReasoning {
    #[allow(dead_code)]
    effort: Option<String>,
    summary: Option<String>,
}

#[derive(Debug, Deserialize)]
struct Usage {
    input_tokens: usize,
    #[serde(default)]
    #[allow(dead_code)]
    input_tokens_details: Option<serde_json::Value>,
    output_tokens: usize,
    #[serde(default)]
    #[allow(dead_code)]
    output_tokens_details: Option<serde_json::Value>,
    total_tokens: usize,
}

impl OpenAIProvider {
    /// Create a new OpenAI provider with default settings
    pub fn new(model: Option<String>, temperature: Option<f32>) -> Result<Self> {
        let api_key =
            env::var("OPENAI_API_KEY").context("OPENAI_API_KEY environment variable not set")?;
        Ok(Self {
            api_key,
            model: model.unwrap_or_else(|| "gpt-4.1".to_string()),
            base_url: "https://api.openai.com/v1".to_string(),
            temperature: temperature.unwrap_or(0.2),
            event_bus: None,
            cost_per_1m_input_tokens: 0.0,
            cost_per_1m_output_tokens: 0.0,
        })
    }

    /// Create a new OpenAI provider with custom configuration
    #[allow(dead_code)]
    pub fn with_config(api_key: String, model: String) -> Self {
        Self {
            api_key,
            model,
            base_url: "https://api.openai.com/v1".to_string(),
            temperature: 1.0, // Use default temperature of 1.0 for OpenAI models
            event_bus: None,
            cost_per_1m_input_tokens: 0.0,
            cost_per_1m_output_tokens: 0.0,
        }
    }

    /// Set custom base URL (for API-compatible services)
    #[allow(dead_code)]
    pub fn with_base_url(mut self, base_url: String) -> Self {
        self.base_url = base_url;
        self
    }

    /// Set temperature for response generation
    #[allow(dead_code)]
    pub fn with_temperature(mut self, temperature: f32) -> Self {
        self.temperature = temperature;
        self
    }

    /// Set event bus for event handling
    #[allow(dead_code)]
    pub fn with_event_bus(mut self, event_bus: Arc<EventBus>) -> Self {
        self.event_bus = Some(event_bus);
        self
    }

    /// Set cost per 1 million input tokens
    #[allow(dead_code)]
    pub fn with_cost_per_1m_input_tokens(mut self, cost: f32) -> Self {
        self.cost_per_1m_input_tokens = cost;
        self
    }

    /// Set cost per 1 million output tokens
    #[allow(dead_code)]
    pub fn with_cost_per_1m_output_tokens(mut self, cost: f32) -> Self {
        self.cost_per_1m_output_tokens = cost;
        self
    }

    fn is_reasoning_model(model: &str) -> bool {
        model.starts_with("o1") || model.starts_with("o3") || model.starts_with("o4-mini")
    }

    /// Helper function to emit reasoning summary in chunks for better dashboard display
    async fn emit_reasoning_summary_chunks(&self, summary: &str) {
        if let Some(event_bus) = &self.event_bus {
            // Split by sentences first, then by chunks if sentences are too long
            let sentences: Vec<&str> = summary.split(". ").collect();
            let mut current_chunk = String::new();
            const MAX_CHUNK_SIZE: usize = 200; // Similar to Ollama's approach

            for (i, sentence) in sentences.iter().enumerate() {
                let sentence_with_period = if i < sentences.len() - 1 && !sentence.ends_with('.') {
                    format!("{}. ", sentence)
                } else {
                    sentence.to_string()
                };

                // If adding this sentence would exceed chunk size, emit current chunk
                if !current_chunk.is_empty() && current_chunk.len() + sentence_with_period.len() > MAX_CHUNK_SIZE {
                    let _ = event_bus
                        .emit(Event::ReasoningTrace {
                            message: current_chunk.trim().to_string(),
                        })
                        .await;
                    current_chunk.clear();
                }

                current_chunk.push_str(&sentence_with_period);
            }

            // Emit any remaining content
            if !current_chunk.trim().is_empty() {
                let _ = event_bus
                    .emit(Event::ReasoningTrace {
                        message: current_chunk.trim().to_string(),
                    })
                    .await;
            }
        }
    }
}

#[async_trait]
impl LLMProvider for OpenAIProvider {
    fn name(&self) -> &str {
        "OpenAI"
    }

    fn context_size(&self) -> usize {
        match self.model.as_str() {
            "gpt-4o" | "gpt-4o-mini" => 128_000,
            "gpt-4-turbo" => 128_000,
            "gpt-4" => 8_192,
            "gpt-3.5-turbo" => 16_385,
            _ => 4_096, // Conservative default
        }
    }

    fn model_name(&self) -> &str {
        &self.model
    }

    fn handles_own_metrics(&self) -> bool {
        true
    }

    async fn send_prompt(&self, prompt: &str) -> Result<String> {
        let client = reqwest::Client::new();

        // Check if this is a reasoning model that supports reasoning summaries
        let is_reasoning_model = Self::is_reasoning_model(&self.model);

        let request = OpenAIRequest {
            model: self.model.clone(),
            input: prompt.to_string(),
            reasoning: if is_reasoning_model {
                Some(OpenAIReasoning {
                    summary: "detailed".to_string(),
                })
            } else {
                None
            },
        };

        let response = client
            .post(format!("{}/responses", self.base_url))
            .header("Authorization", format!("Bearer {}", self.api_key))
            .header("Content-Type", "application/json")
            .json(&request)
            .send()
            .await
            .context("Failed to send request to OpenAI API")?;

        if !response.status().is_success() {
            let error_text = response.text().await?;
            return Err(anyhow!("OpenAI API error: {}", error_text));
        }

        let response_text = response.text().await?;
        debug!("Raw OpenAI response: {}", response_text);
        
        // Try to parse as pretty JSON first for better debugging
        if let Ok(pretty_json) = serde_json::from_str::<serde_json::Value>(&response_text) {
            debug!("Raw response as JSON: {}", serde_json::to_string_pretty(&pretty_json).unwrap_or_default());
        }

        let openai_response: OpenAIResponse =
            serde_json::from_str(&response_text).map_err(|e| {
                error!("Failed to parse OpenAI response. Error: {}", e);
                error!("Raw response was: {}", response_text);
                anyhow::anyhow!("Failed to parse OpenAI response: {}", e)
            })?;

        debug!("Parsed OpenAI response: {:?}", openai_response);

        let content = openai_response.output.iter().find_map(|item| {
            if item.message_type == "message" {
                item.content.as_ref().and_then(|content| {
                    content.iter().find_map(|content_item| {
                        if content_item.content_type == "text" || content_item.content_type == "output_text" {
                            Some(content_item.text.clone())
                        } else {
                            None
                        }
                    })
                })
            } else {
                None
            }
        }).unwrap_or_default();

        // Handle reasoning summary for reasoning models
        if let Some(reasoning) = &openai_response.reasoning {
            if let Some(summary) = &reasoning.summary {
                self.emit_reasoning_summary_chunks(summary).await;
            }
        }

        // Also check for reasoning summary in output items (for reasoning models)
        for item in &openai_response.output {
            if item.message_type == "reasoning" {
                if let Some(summary_items) = &item.summary {
                    let summary_text: Vec<String> = summary_items
                        .iter()
                        .filter_map(|item| {
                            item.get("text").and_then(|v| v.as_str()).map(|s| s.to_string())
                        })
                        .collect();
                    
                    if !summary_text.is_empty() {
                        let combined_summary = summary_text.join("\n\n");
                        self.emit_reasoning_summary_chunks(&combined_summary).await;
                    }
                }
            }
        }

        // Log token usage if available
        if let Some(usage) = openai_response.usage {
            // Calculate cost using configured pricing
            let input_cost = (usage.input_tokens as f32 * self.cost_per_1m_input_tokens) / 1_000_000.0;
            let output_cost = (usage.output_tokens as f32 * self.cost_per_1m_output_tokens) / 1_000_000.0;
            let total_cost = input_cost + output_cost;

            // Emit APICallCompleted event with accurate token counts and cost
            if let Some(event_bus) = &self.event_bus {
                let _ = event_bus.emit(Event::APICallCompleted {
                    provider: "openai".to_string(),
                    tokens: usage.total_tokens,
                    cost: total_cost,
                }).await;
            }
        }

        Ok(content)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_context_sizes() {
        let provider =
            OpenAIProvider::with_config("test_key".to_string(), "gpt-4o".to_string());
        assert_eq!(provider.context_size(), 128_000);

        let provider =
            OpenAIProvider::with_config("test_key".to_string(), "gpt-3.5-turbo".to_string());
        assert_eq!(provider.context_size(), 16_385);
    }
}