aicommit 0.1.140

A CLI tool that generates concise and descriptive git commit messages using LLMs
Documentation
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// Model management functions

use crate::types::*;
use crate::{PREFERRED_FREE_MODELS, MAX_CONSECUTIVE_FAILURES, INITIAL_JAIL_HOURS, JAIL_TIME_MULTIPLIER, MAX_JAIL_HOURS, BLACKLIST_AFTER_JAIL_COUNT, BLACKLIST_RETRY_DAYS};
use std::fs;
use chrono;

// From: 035_function_get_available_free_models.rs
pub async fn get_available_free_models(api_key: &str, simulate_offline: bool) -> Result<Vec<String>, String> {
    // If simulate_offline is true, immediately return the fallback list
    if simulate_offline {
        println!("Debug: Simulating offline mode, using fallback model list");
        return fallback_to_preferred_models();
    }
    
    let client = reqwest::Client::builder()
        .timeout(std::time::Duration::from_secs(10)) // Add a reasonable timeout
        .build()
        .unwrap_or_default();
    
    // Try to fetch models from OpenRouter API
    let response = match tokio::time::timeout(
        std::time::Duration::from_secs(15),
        client.get("https://openrouter.ai/api/v1/models")
            .header("Authorization", format!("Bearer {}", api_key))
            .header("HTTP-Referer", "https://suenot.github.io/aicommit/")
            .header("X-Title", "aicommit")
            .send()
    ).await {
        Ok(result) => match result {
            Ok(response) => {
                if !response.status().is_success() {
                    println!("Warning: OpenRouter API returned status code: {}", response.status());
                    return fallback_to_preferred_models();
                }
                response
            },
            Err(e) => {
                println!("Warning: Failed to connect to OpenRouter API: {}", e);
                println!("Using predefined free models as fallback...");
                return fallback_to_preferred_models();
            }
        },
        Err(_) => {
            println!("Warning: Request to OpenRouter API timed out after 15 seconds");
            println!("Using predefined free models as fallback...");
            return fallback_to_preferred_models();
        }
    };
    
    // Try to parse the response
    let models_response: Result<serde_json::Value, _> = response.json().await;
    if let Err(e) = &models_response {
        println!("Warning: Failed to parse OpenRouter API response: {}", e);
        println!("Using predefined free models as fallback...");
        return fallback_to_preferred_models();
    }
    
    let models_response = models_response.unwrap();
    let mut free_models = Vec::new();
    
    if let Some(data) = models_response["data"].as_array() {
        // First pass: check all models that are explicitly marked as free
        for model in data {
            if let Some(id) = model["id"].as_str() {
                // Multiple ways to detect if a model is free:
                
                // 1. Check if the model ID contains ":free"
                if id.contains(":free") {
                    free_models.push(id.to_string());
                    continue;
                }
                
                // 2. Check if "free" field is true
                if let Some(true) = model["free"].as_bool() {
                    free_models.push(id.to_string());
                    continue;
                }
                
                // 3. Check if "free_tokens" is greater than 0
                if let Some(tokens) = model["free_tokens"].as_u64() {
                    if tokens > 0 {
                        free_models.push(id.to_string());
                        continue;
                    }
                }
                
                // 4. Check if pricing is 0 for both prompt and completion
                if let Some(pricing) = model["pricing"].as_object() {
                    let prompt_price = pricing.get("prompt")
                        .and_then(|v| v.as_f64())
                        .unwrap_or(1.0);
                    
                    let completion_price = pricing.get("completion")
                        .and_then(|v| v.as_f64())
                        .unwrap_or(1.0);
                    
                    if prompt_price == 0.0 && completion_price == 0.0 {
                        free_models.push(id.to_string());
                        continue;
                    }
                }
            }
        }
        
        // If no free models found, try a second pass with more relaxed criteria
        if free_models.is_empty() {
            // Look for models with very low pricing (<= 0.0001)
            for model in data {
                if let Some(id) = model["id"].as_str() {
                    if let Some(pricing) = model["pricing"].as_object() {
                        let prompt_price = pricing.get("prompt")
                            .and_then(|v| v.as_f64())
                            .unwrap_or(1.0);
                        
                        let completion_price = pricing.get("completion")
                            .and_then(|v| v.as_f64())
                            .unwrap_or(1.0);
                        
                        // Consider very low-priced models as "effectively free"
                        if prompt_price <= 0.0001 && completion_price <= 0.0001 {
                            free_models.push(id.to_string());
                        }
                    }
                }
            }
        }
    }
    
    // If we still found no free models, fall back to predefined list
    if free_models.is_empty() {
        println!("Warning: No free models found from OpenRouter API");
        println!("Using predefined free models as fallback...");
        return fallback_to_preferred_models();
    }
    
    Ok(free_models)
}

// From: 036_function_fallback_to_preferred_models.rs
pub fn fallback_to_preferred_models() -> Result<Vec<String>, String> {
    let mut models = Vec::new();
    
    // Add all predefined free models
    for model in PREFERRED_FREE_MODELS {
        models.push(model.to_string());
    }
    
    if models.is_empty() {
        return Err("No free models available, and fallback model list is empty".to_string());
    }
    
    Ok(models)
}

// From: 037_function_find_best_available_model.rs
pub fn find_best_available_model(available_models: &[String], config: &SimpleFreeOpenRouterConfig) -> Option<String> {
    // Start by using the previously successful model if it's still available
    if let Some(last_model) = &config.last_used_model {
        if available_models.contains(last_model) {
            let stats = config.model_stats.get(last_model);
            if is_model_available(&stats) {
                return Some(last_model.clone());
            }
        }
    }

    // Filter models that are not in jail or blacklisted
    let available_candidates: Vec<&String> = available_models
        .iter()
        .filter(|model| {
            let stats = config.model_stats.get(*model);
            is_model_available(&stats)
        })
        .collect();
    
    // First try our curated list of preferred models in order
    for preferred in PREFERRED_FREE_MODELS {
        let preferred_str = preferred.to_string();
        if available_candidates.contains(&&preferred_str) {
            return Some(preferred_str);
        }
    }
    
    // If none of our preferred models are available, use an intelligent fallback approach
    // by analyzing model names for parameter sizes (like 70b, 32b, etc.)
    if !available_candidates.is_empty() {
        // Sort by estimated parameter count (highest first)
        let mut sorted_candidates = available_candidates.clone();
        sorted_candidates.sort_by(|a, b| {
            let a_size = extract_model_size(a);
            let b_size = extract_model_size(b);
            b_size.cmp(&a_size) // Reverse order (largest first)
        });
        
        // Return the largest available model
        return Some(sorted_candidates[0].clone());
    }
    
    // If all models are jailed or blacklisted, try the least recently jailed one
    if !available_models.is_empty() {
        // Get all jailed but not blacklisted models
        let mut jailed_models: Vec<(String, chrono::DateTime<chrono::Utc>)> = Vec::new();
        
        for model in available_models {
            if let Some(stats) = config.model_stats.get(model) {
                if !stats.blacklisted && stats.jail_until.is_some() {
                    jailed_models.push((model.clone(), stats.jail_until.unwrap()));
                }
            }
        }
        
        // Sort by jail expiry time (soonest first)
        if !jailed_models.is_empty() {
            jailed_models.sort_by_key(|x| x.1);
            return Some(jailed_models[0].0.clone());
        }
        
        // Last resort: just use any model, even blacklisted ones
        return Some(available_models[0].clone());
    }
    
    None
}

// From: 038_function_extract_model_size.rs
pub fn extract_model_size(model_name: &str) -> u32 {
    let lower_name = model_name.to_lowercase();
    
    // Look for patterns like "70b", "32b", "7b", etc.
    let patterns = [
        "253b", "235b", "200b", "124b",
        "70b", "80b", "90b", "72b", "65b", 
        "40b", "32b", "30b", "24b", "20b",
        "16b", "14b", "13b", "12b", "11b", "10b",
        "9b", "8b", "7b", "6b", "5b", "4b", "3b", "2b", "1b"
    ];
    
    for pattern in patterns {
        if lower_name.contains(pattern) {
            // Extract the number from the pattern (e.g., "70b" -> 70)
            if let Ok(size) = pattern.trim_end_matches(|c| c == 'b' || c == 'B').parse::<u32>() {
                return size;
            }
        }
    }
    
    // Default size if no pattern matches
    // Check for specific keywords that might indicate a more powerful model
    if lower_name.contains("large") || lower_name.contains("ultra") {
        return 15; // Assume it's a medium-large model
    } else if lower_name.contains("medium") {
        return 10;
    } else if lower_name.contains("small") || lower_name.contains("tiny") {
        return 5;
    }
    
    // Default fallback
    0
}

// From: 046_function_is_model_available.rs
pub fn is_model_available(model_stats: &Option<&ModelStats>) -> bool {
    match model_stats {
        None => true, // No stats yet, model is available
        Some(stats) => {
            // Check if blacklisted but should be retried
            if stats.blacklisted {
                if let Some(blacklisted_since) = stats.blacklisted_since {
                    let retry_duration = chrono::Duration::days(BLACKLIST_RETRY_DAYS);
                    let now = chrono::Utc::now();
                    
                    // If blacklisted for more than retry period, give it another chance
                    if now - blacklisted_since > retry_duration {
                        return true;
                    }
                    return false;
                }
                return false;
            }
            
            // Check if currently in jail
            if let Some(jail_until) = stats.jail_until {
                if chrono::Utc::now() < jail_until {
                    return false;
                }
            }
            
            true
        }
    }
}

// From: 047_function_record_model_success.rs
pub fn record_model_success(model_stats: &mut ModelStats) {
    model_stats.success_count += 1;
    model_stats.last_success = Some(chrono::Utc::now());
    
    // Reset consecutive failures if successful
    if model_stats.last_failure.is_none() || 
       model_stats.last_success.unwrap() > model_stats.last_failure.unwrap() {
        // The model is working now, remove any jail time
        model_stats.jail_until = None;
    }
}

// From: 048_function_record_model_failure.rs
pub fn record_model_failure(model_stats: &mut ModelStats) {
    let now = chrono::Utc::now();
    model_stats.failure_count += 1;
    model_stats.last_failure = Some(now);
    
    // Check if we have consecutive failures
    let has_consecutive_failures = match model_stats.last_success {
        None => true, // Never had a success
        Some(last_success) => {
            // If last success is older than last failure, we have consecutive failures
            model_stats.last_failure.unwrap() > last_success
        }
    };
    
    if has_consecutive_failures {
        // Count consecutive failures by comparing timestamps
        let consecutive_failures = if let Some(last_success) = model_stats.last_success {
            let hours_since_success = (now - last_success).num_hours();
            // If it's been more than a day since last success, count as consecutive failures
            if hours_since_success > 24 {
                model_stats.failure_count.min(MAX_CONSECUTIVE_FAILURES)
            } else {
                // Count failures since last success
                1 // This is at least 1 consecutive failure
            }
        } else {
            // No success ever, count all failures
            model_stats.failure_count.min(MAX_CONSECUTIVE_FAILURES)
        };
        
        // Jail if we hit the threshold
        if consecutive_failures >= MAX_CONSECUTIVE_FAILURES {
            // Calculate jail duration based on recidivism
            let jail_hours = INITIAL_JAIL_HOURS * JAIL_TIME_MULTIPLIER.pow(model_stats.jail_count as u32);
            let jail_hours = jail_hours.min(MAX_JAIL_HOURS); // Cap at maximum
            
            // Set jail expiration time
            model_stats.jail_until = Some(now + chrono::Duration::hours(jail_hours));
            model_stats.jail_count += 1;
            
            // Blacklist if consistently problematic
            if model_stats.jail_count >= BLACKLIST_AFTER_JAIL_COUNT {
                model_stats.blacklisted = true;
                model_stats.blacklisted_since = Some(now);
            }
        }
    }
}

// From: 049_function_format_model_status.rs
pub fn format_model_status(model: &str, stats: &ModelStats) -> String {
    let status = if stats.blacklisted {
        "BLACKLISTED".to_string()
    } else if let Some(jail_until) = stats.jail_until {
        if chrono::Utc::now() < jail_until {
            let remaining = jail_until - chrono::Utc::now();
            format!("JAILED ({}h remaining)", remaining.num_hours())
        } else {
            "ACTIVE".to_string()
        }
    } else {
        "ACTIVE".to_string()
    };
    
    let last_success = stats.last_success.map_or("Never".to_string(), |ts| {
        let ago = chrono::Utc::now() - ts;
        if ago.num_days() > 0 {
            format!("{} days ago", ago.num_days())
        } else if ago.num_hours() > 0 {
            format!("{} hours ago", ago.num_hours())
        } else {
            format!("{} minutes ago", ago.num_minutes())
        }
    });
    
    let last_failure = stats.last_failure.map_or("Never".to_string(), |ts| {
        let ago = chrono::Utc::now() - ts;
        if ago.num_days() > 0 {
            format!("{} days ago", ago.num_days())
        } else if ago.num_hours() > 0 {
            format!("{} hours ago", ago.num_hours())
        } else {
            format!("{} minutes ago", ago.num_minutes())
        }
    });
    
    format!("{}: {} (Success: {}, Failure: {}, Last success: {}, Last failure: {})",
            model, status, stats.success_count, stats.failure_count, last_success, last_failure)
}

// From: 050_function_display_model_jail_status.rs
pub fn display_model_jail_status(config: &SimpleFreeOpenRouterConfig) -> Result<(), String> {
    if config.model_stats.is_empty() {
        println!("No model statistics available yet.");
        return Ok(());
    }
    
    println!("\nModel Status Report:");
    println!("===================");
    
    // Group models by status
    let mut active_models = Vec::new();
    let mut jailed_models = Vec::new();
    let mut blacklisted_models = Vec::new();
    
    for (model, stats) in &config.model_stats {
        if stats.blacklisted {
            blacklisted_models.push(format_model_status(model, stats));
        } else if let Some(jail_until) = stats.jail_until {
            if chrono::Utc::now() < jail_until {
                jailed_models.push(format_model_status(model, stats));
            } else {
                active_models.push(format_model_status(model, stats));
            }
        } else {
            active_models.push(format_model_status(model, stats));
        }
    }
    
    // Sort and display each group
    if !active_models.is_empty() {
        println!("\nACTIVE MODELS:");
        active_models.sort();
        for model in active_models {
            println!("  {}", model);
        }
    }
    
    if !jailed_models.is_empty() {
        println!("\nJAILED MODELS:");
        jailed_models.sort();
        for model in jailed_models {
            println!("  {}", model);
        }
    }
    
    if !blacklisted_models.is_empty() {
        println!("\nBLACKLISTED MODELS:");
        blacklisted_models.sort();
        for model in blacklisted_models {
            println!("  {}", model);
        }
    }
    
    Ok(())
}

// From: 051_function_unjail_model.rs
pub fn unjail_model(config: &mut SimpleFreeOpenRouterConfig, model_id: &str) -> Result<(), String> {
    let model_found = if model_id == "*" {
        // Reset all models
        for (_, stats) in config.model_stats.iter_mut() {
            stats.jail_until = None;
            stats.blacklisted = false;
            stats.jail_count = 0;
        }
        true
    } else {
        // Reset specific model
        if let Some(stats) = config.model_stats.get_mut(model_id) {
            stats.jail_until = None;
            stats.blacklisted = false;
            stats.jail_count = 0;
            true
        } else {
            false
        }
    };
    
    if !model_found {
        return Err(format!("Model '{}' not found in statistics", model_id));
    }
    
    // Save updated config
    let config_path = dirs::home_dir()
        .ok_or_else(|| "Could not find home directory".to_string())?
        .join(".aicommit.json");
        
    let mut full_config = Config::load()?;
    
    // Update the provider in the full config
    for provider in &mut full_config.providers {
        if let ProviderConfig::SimpleFreeOpenRouter(simple_config) = provider {
            if simple_config.id == config.id {
                *simple_config = config.clone();
                break;
            }
        }
    }
    
    let content = serde_json::to_string_pretty(&full_config)
        .map_err(|e| format!("Failed to serialize config: {}", e))?;
        
    fs::write(&config_path, content)
        .map_err(|e| format!("Failed to write config file: {}", e))?;
    
    Ok(())
}

// From: 052_function_unjail_all_models.rs
pub fn unjail_all_models(config: &mut SimpleFreeOpenRouterConfig) -> Result<(), String> {
    unjail_model(config, "*")
}