lc-cli 0.1.3

LLM Client - A fast Rust-based LLM CLI tool with provider management and chat sessions
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
use crate::{
    config::Config,
    debug_log, // Import debug_log macro
    model_metadata::{extract_models_from_provider, ModelMetadata},
    provider::Provider,
};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tokio::fs;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachedProviderData {
    pub last_updated: u64,          // Unix timestamp
    pub raw_response: String,       // Raw JSON response from provider
    pub models: Vec<ModelMetadata>, // Extracted metadata
    // Cache the serialized JSON to avoid repeated serialization
    #[serde(skip)]
    pub cached_json: Option<String>,
}

impl CachedProviderData {
    fn new(raw_response: String, models: Vec<ModelMetadata>) -> Self {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or(std::time::Duration::from_secs(0))
            .as_secs();

        Self {
            last_updated: now,
            raw_response,
            models,
            cached_json: None,
        }
    }

    fn get_cached_json(&mut self) -> Result<&str> {
        if self.cached_json.is_none() {
            self.cached_json = Some(serde_json::to_string_pretty(self)?);
        }
        self.cached_json
            .as_ref()
            .map(|s| s.as_str())
            .ok_or_else(|| anyhow::anyhow!("Failed to get cached JSON"))
    }
}

// In-memory cache entry with TTL
#[derive(Debug, Clone)]
struct MemoryCacheEntry {
    data: CachedProviderData,
    expires_at: u64,
}

impl MemoryCacheEntry {
    fn new(data: CachedProviderData, ttl_seconds: u64) -> Self {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or(Duration::from_secs(0))
            .as_secs();

        Self {
            data,
            expires_at: now + ttl_seconds,
        }
    }

    fn is_expired(&self) -> bool {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or(Duration::from_secs(0))
            .as_secs();

        now >= self.expires_at
    }
}

// Global in-memory cache with efficient invalidation
lazy_static::lazy_static! {
    static ref MEMORY_CACHE: Arc<RwLock<HashMap<String, MemoryCacheEntry>>> =
        Arc::new(RwLock::new(HashMap::new()));
}

pub struct UnifiedCache;

impl UnifiedCache {
    /// Cache TTL in seconds (24 hours)
    const CACHE_TTL: u64 = 86400;

    /// Get the models directory path (cross-platform)
    pub fn models_dir() -> Result<PathBuf> {
        let config_dir =
            dirs::config_dir().ok_or_else(|| anyhow::anyhow!("Could not find config directory"))?;

        Ok(config_dir.join("lc").join("models"))
    }

    /// Get the cache file path for a specific provider
    pub fn provider_cache_path(provider: &str) -> Result<PathBuf> {
        let models_dir = Self::models_dir()?;
        Ok(models_dir.join(format!("{}.json", provider)))
    }

    /// Check in-memory cache first, then file cache
    pub async fn is_cache_fresh(provider: &str) -> Result<bool> {
        debug_log!("Checking cache freshness for provider '{}'", provider);

        // Check in-memory cache first
        if let Ok(cache) = MEMORY_CACHE.read() {
            if let Some(entry) = cache.get(provider) {
                if !entry.is_expired() {
                    debug_log!("Found fresh in-memory cache for provider '{}'", provider);
                    return Ok(true);
                } else {
                    debug_log!("In-memory cache expired for provider '{}'", provider);
                }
            }
        }

        // Fall back to file cache
        let cache_path = Self::provider_cache_path(provider)?;

        if !cache_path.exists() {
            debug_log!("Cache file does not exist for provider '{}'", provider);
            return Ok(false);
        }

        // Use async file I/O to avoid blocking
        let content = fs::read_to_string(&cache_path).await?;
        let cached_data: CachedProviderData = serde_json::from_str(&content)?;

        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or(Duration::from_secs(0))
            .as_secs();

        let age_seconds = now - cached_data.last_updated;
        let is_fresh = age_seconds < Self::CACHE_TTL;

        debug_log!(
            "File cache for provider '{}' is {} seconds old, fresh: {}",
            provider,
            age_seconds,
            is_fresh
        );

        // If file cache is fresh, populate in-memory cache
        if is_fresh {
            Self::populate_memory_cache(provider, cached_data);
        }

        Ok(is_fresh)
    }

    /// Populate in-memory cache with data
    fn populate_memory_cache(provider: &str, data: CachedProviderData) {
        if let Ok(mut cache) = MEMORY_CACHE.write() {
            let entry = MemoryCacheEntry::new(data, Self::CACHE_TTL);
            cache.insert(provider.to_string(), entry);
            debug_log!("Populated in-memory cache for provider '{}'", provider);
        }
    }

    /// Invalidate cache for a specific provider
    pub fn invalidate_provider_cache(provider: &str) {
        if let Ok(mut cache) = MEMORY_CACHE.write() {
            cache.remove(provider);
            debug_log!("Invalidated in-memory cache for provider '{}'", provider);
        }
    }

    /// Clear all in-memory cache
    #[allow(dead_code)]
    pub fn clear_memory_cache() {
        if let Ok(mut cache) = MEMORY_CACHE.write() {
            cache.clear();
            debug_log!("Cleared all in-memory cache");
        }
    }

    /// Get cache age in human-readable format (e.g., "5 mins ago", "2 hrs ago")
    pub async fn get_cache_age_display(provider: &str) -> Result<String> {
        // Check in-memory cache first
        if let Ok(cache) = MEMORY_CACHE.read() {
            if let Some(entry) = cache.get(provider) {
                let now = SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .unwrap_or(Duration::from_secs(0))
                    .as_secs();

                let age_seconds = now - entry.data.last_updated;
                return Ok(Self::format_age(age_seconds));
            }
        }

        // Fall back to file cache
        let cache_path = Self::provider_cache_path(provider)?;

        if !cache_path.exists() {
            return Ok("No cache".to_string());
        }

        let content = fs::read_to_string(&cache_path).await?;
        let cached_data: CachedProviderData = serde_json::from_str(&content)?;

        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or(Duration::from_secs(0))
            .as_secs();

        let age_seconds = now - cached_data.last_updated;
        Ok(Self::format_age(age_seconds))
    }

    /// Format age in seconds to human-readable string
    fn format_age(age_seconds: u64) -> String {
        if age_seconds < 60 {
            format!("{} secs ago", age_seconds)
        } else if age_seconds < 3600 {
            let minutes = age_seconds / 60;
            format!("{} min{} ago", minutes, if minutes == 1 { "" } else { "s" })
        } else if age_seconds < 86400 {
            let hours = age_seconds / 3600;
            format!("{} hr{} ago", hours, if hours == 1 { "" } else { "s" })
        } else {
            let days = age_seconds / 86400;
            format!("{} day{} ago", days, if days == 1 { "" } else { "s" })
        }
    }

    /// Load cached models for a provider (async with in-memory cache)
    pub async fn load_provider_models(provider: &str) -> Result<Vec<ModelMetadata>> {
        debug_log!("Loading cached models for provider '{}'", provider);

        // Check in-memory cache first
        if let Ok(cache) = MEMORY_CACHE.read() {
            if let Some(entry) = cache.get(provider) {
                if !entry.is_expired() {
                    debug_log!(
                        "Loaded {} models from in-memory cache for provider '{}'",
                        entry.data.models.len(),
                        provider
                    );
                    return Ok(entry.data.models.clone());
                } else {
                    debug_log!("In-memory cache expired for provider '{}'", provider);
                }
            }
        }

        // Fall back to file cache
        let cache_path = Self::provider_cache_path(provider)?;

        if !cache_path.exists() {
            debug_log!("No cache file found for provider '{}'", provider);
            return Ok(Vec::new());
        }

        let content = fs::read_to_string(&cache_path).await?;
        let cached_data: CachedProviderData = serde_json::from_str(&content)?;

        debug_log!(
            "Loaded {} models from file cache for provider '{}'",
            cached_data.models.len(),
            provider
        );

        // Populate in-memory cache if data is fresh
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or(Duration::from_secs(0))
            .as_secs();

        if now - cached_data.last_updated < Self::CACHE_TTL {
            Self::populate_memory_cache(provider, cached_data.clone());
        }

        Ok(cached_data.models)
    }

    /// Fetch and cache models for a provider
    pub async fn fetch_and_cache_provider_models(
        provider: &str,
        force_refresh: bool,
    ) -> Result<Vec<ModelMetadata>> {
        debug_log!(
            "Fetching models for provider '{}', force_refresh: {}",
            provider,
            force_refresh
        );

        // Check if we need to refresh
        if !force_refresh && Self::is_cache_fresh(provider).await? {
            debug_log!(
                "Using cached models for provider '{}' (cache is fresh)",
                provider
            );
            return Self::load_provider_models(provider).await;
        }

        debug_log!(
            "Cache is stale or refresh forced, fetching fresh models for provider '{}'",
            provider
        );
        println!("Fetching models from provider '{}'...", provider);

        // Invalidate existing cache
        Self::invalidate_provider_cache(provider);

        // Load config and create client
        let config = Config::load()?;
        // Load provider with authentication (API key, headers, tokens) from centralized keys
        let provider_config = config.get_provider_with_auth(provider)?;

        debug_log!(
            "Creating authenticated client for provider '{}' with endpoint: {}",
            provider,
            provider_config.endpoint
        );

        let mut config_mut = config.clone();
        let client = crate::chat::create_authenticated_client(&mut config_mut, provider).await?;

        // Save config if tokens were updated
        if config_mut.get_cached_token(provider) != config.get_cached_token(provider) {
            debug_log!(
                "Tokens were updated for provider '{}', saving config",
                provider
            );
            config_mut.save()?;
        }

        // Build the models URL
        let models_url = format!(
            "{}{}",
            provider_config.endpoint, provider_config.models_path
        );
        debug_log!("Fetching models from URL: {}", models_url);

        // Fetch raw response using the client's list_models method
        debug_log!(
            "Making API request to fetch models from provider '{}'",
            provider
        );

        // Make the actual API request to fetch models
        let models_list = client.list_models().await?;

        // Create a JSON response that matches the OpenAI models format
        // This is what we'll cache as the "raw response"
        let models_json = serde_json::json!({
            "object": "list",
            "data": models_list.iter().map(|m| {
                serde_json::json!({
                    "id": m.id,
                    "object": m.object,
                    "providers": m.providers.iter().map(|p| {
                        serde_json::json!({
                            "provider": p.provider,
                            "status": p.status,
                            "supports_tools": p.supports_tools,
                            "supports_structured_output": p.supports_structured_output
                        })
                    }).collect::<Vec<_>>()
                })
            }).collect::<Vec<_>>()
        });

        let raw_response = serde_json::to_string_pretty(&models_json)?;

        debug_log!(
            "Received raw response from provider '{}' ({} bytes)",
            provider,
            raw_response.len()
        );

        // Debug log the full response when -d flag is used
        debug_log!(
            "Full response from provider '{}': {}",
            provider,
            raw_response
        );

        // Extract metadata using the new generic approach
        debug_log!(
            "Extracting metadata from response for provider '{}'",
            provider
        );

        // Create a Provider object for the extractor
        let provider_obj = Provider {
            provider: provider.to_string(),
            status: "active".to_string(),
            supports_tools: false,
            supports_structured_output: false,
        };

        let models = extract_models_from_provider(&provider_obj, &raw_response)?;

        debug_log!(
            "Extracted {} models from provider '{}'",
            models.len(),
            provider
        );

        // Cache the data (both in-memory and file)
        debug_log!("Saving cache data for provider '{}'", provider);
        Self::save_provider_cache(provider, &raw_response, &models).await?;

        Ok(models)
    }

    /// Save provider data to cache (async with in-memory caching)
    async fn save_provider_cache(
        provider: &str,
        raw_response: &str,
        models: &[ModelMetadata],
    ) -> Result<()> {
        let cache_path = Self::provider_cache_path(provider)?;

        debug_log!(
            "Saving cache for provider '{}' to: {}",
            provider,
            cache_path.display()
        );

        // Create cached data
        let cached_data = CachedProviderData::new(raw_response.to_string(), models.to_vec());

        // Update in-memory cache first (fastest access)
        Self::populate_memory_cache(provider, cached_data.clone());

        // Ensure cache directory exists
        if let Some(parent) = cache_path.parent() {
            debug_log!("Creating cache directory: {}", parent.display());
            fs::create_dir_all(parent).await?;
        }

        // Use async file I/O to avoid blocking
        let mut cached_data_mut = cached_data;
        let content = cached_data_mut.get_cached_json()?;
        debug_log!(
            "Writing {} bytes to cache file for provider '{}'",
            content.len(),
            provider
        );
        fs::write(&cache_path, content).await?;

        debug_log!(
            "Successfully saved cache for provider '{}' with {} models",
            provider,
            models.len()
        );

        Ok(())
    }

    /// Load all cached models from all providers (async with in-memory cache)
    pub async fn load_all_cached_models() -> Result<Vec<ModelMetadata>> {
        let models_dir = Self::models_dir()?;
        let mut all_models = Vec::new();

        if !models_dir.exists() {
            return Ok(all_models);
        }

        let mut entries = fs::read_dir(&models_dir).await?;

        while let Some(entry) = entries.next_entry().await? {
            let path = entry.path();

            if let Some(extension) = path.extension() {
                if extension == "json" {
                    if let Some(provider_name) = path.file_stem().and_then(|s| s.to_str()) {
                        match Self::load_provider_models(provider_name).await {
                            Ok(mut models) => {
                                all_models.append(&mut models);
                            }
                            Err(e) => {
                                eprintln!(
                                    "Warning: Failed to load cached models for {}: {}",
                                    provider_name, e
                                );
                            }
                        }
                    }
                }
            }
        }

        // Sort by provider, then by model name
        all_models.sort_by(|a, b| a.provider.cmp(&b.provider).then(a.id.cmp(&b.id)));

        Ok(all_models)
    }

    /// Refresh all providers' caches
    pub async fn refresh_all_providers() -> Result<()> {
        let config = Config::load()?;
        let mut successful_providers = 0;
        let mut total_models = 0;

        println!("Refreshing models cache for all providers...");

        for (provider_name, _provider_config) in &config.providers {
            // Skip providers that have neither API key nor custom headers (after loading centralized auth)
            let pc_auth = match config.get_provider_with_auth(provider_name) {
                Ok(cfg) => cfg,
                Err(_) => continue,
            };
            if pc_auth.api_key.is_none() && pc_auth.headers.is_empty() {
                continue;
            }

            match Self::fetch_and_cache_provider_models(provider_name, true).await {
                Ok(models) => {
                    let count = models.len();
                    successful_providers += 1;
                    total_models += count;
                    println!("{} ({} models)", provider_name, count);
                }
                Err(e) => {
                    println!("{} ({})", provider_name, e);
                }
            }
        }

        println!(
            "\nCache updated: {} providers, {} total models",
            successful_providers, total_models
        );
        Ok(())
    }
}