memvid-cli 2.0.140

Command-line interface for Memvid v2 - AI memory with crash-safe, single-file storage
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Config management commands (set, get, list, unset, check)
//!
//! Commands for managing persistent CLI configuration including API keys.

use anyhow::{Context, Result};
use clap::{Args, Subcommand};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

/// Config management commands
#[derive(Subcommand)]
pub enum ConfigCommand {
    /// Set a configuration value
    Set(ConfigSetArgs),
    /// Get a configuration value
    Get(ConfigGetArgs),
    /// List all configuration values
    List(ConfigListArgs),
    /// Remove a configuration value
    Unset(ConfigUnsetArgs),
    /// Check configuration validity (verifies API key with server)
    Check(ConfigCheckArgs),
}

/// Arguments for the `config` command
#[derive(Args)]
pub struct ConfigArgs {
    #[command(subcommand)]
    pub command: ConfigCommand,
}

/// Arguments for `config set`
#[derive(Args)]
pub struct ConfigSetArgs {
    /// Configuration key (e.g., api_key, api_url)
    pub key: String,
    /// Value to set
    pub value: String,
    /// Output as JSON
    #[arg(long)]
    pub json: bool,
}

/// Arguments for `config get`
#[derive(Args)]
pub struct ConfigGetArgs {
    /// Configuration key to retrieve
    pub key: String,
    /// Output as JSON
    #[arg(long)]
    pub json: bool,
}

/// Arguments for `config list`
#[derive(Args)]
pub struct ConfigListArgs {
    /// Output as JSON
    #[arg(long)]
    pub json: bool,
    /// Show values (by default, sensitive values are masked)
    #[arg(long)]
    pub show_values: bool,
}

/// Arguments for `config unset`
#[derive(Args)]
pub struct ConfigUnsetArgs {
    /// Configuration key to remove
    pub key: String,
    /// Output as JSON
    #[arg(long)]
    pub json: bool,
}

/// Arguments for `config check`
#[derive(Args)]
pub struct ConfigCheckArgs {
    /// Output as JSON
    #[arg(long)]
    pub json: bool,
}

/// Persistent configuration stored in config file
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PersistentConfig {
    /// API key for authentication
    pub api_key: Option<String>,
    /// API URL (defaults to https://memvid.com) - legacy, use dashboard_url
    pub api_url: Option<String>,
    /// Dashboard URL (defaults to https://memvid.com)
    pub dashboard_url: Option<String>,
    /// Default memory ID for dashboard sync (legacy, use [memory] section)
    pub memory_id: Option<String>,
    /// Named memory IDs (e.g., memory.default, memory.work)
    #[serde(default)]
    pub memory: HashMap<String, String>,
    /// Default embedding provider
    pub default_embedding_provider: Option<String>,
    /// Default LLM provider
    pub default_llm_provider: Option<String>,
    /// Additional custom settings
    #[serde(flatten)]
    pub extra: HashMap<String, String>,
}

impl PersistentConfig {
    /// Get the config file path (~/.config/memvid/config.toml)
    pub fn config_path() -> Result<PathBuf> {
        let config_dir = dirs::config_dir()
            .context("Could not determine config directory")?
            .join("memvid");
        Ok(config_dir.join("config.toml"))
    }

    /// Load config from file, or return default if file doesn't exist
    pub fn load() -> Result<Self> {
        let path = Self::config_path()?;
        if !path.exists() {
            return Ok(Self::default());
        }
        let content = fs::read_to_string(&path)
            .with_context(|| format!("Failed to read config file: {}", path.display()))?;
        let config: PersistentConfig = toml::from_str(&content)
            .with_context(|| format!("Failed to parse config file: {}", path.display()))?;
        Ok(config)
    }

    /// Save config to file
    pub fn save(&self) -> Result<()> {
        let path = Self::config_path()?;
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).with_context(|| {
                format!("Failed to create config directory: {}", parent.display())
            })?;
        }
        let content = toml::to_string_pretty(self).context("Failed to serialize config")?;
        fs::write(&path, content)
            .with_context(|| format!("Failed to write config file: {}", path.display()))?;
        Ok(())
    }

    /// Get a value by key (supports memory.<name> syntax)
    pub fn get(&self, key: &str) -> Option<String> {
        // Handle memory.<name> syntax
        if let Some(name) = key.strip_prefix("memory.") {
            return self.memory.get(name).cloned();
        }

        match key {
            "api_key" => self.api_key.clone(),
            "api_url" => self.api_url.clone(),
            "dashboard_url" => self.dashboard_url.clone(),
            "memory_id" => self.memory_id.clone(),
            "default_embedding_provider" => self.default_embedding_provider.clone(),
            "default_llm_provider" => self.default_llm_provider.clone(),
            _ => self.extra.get(key).cloned(),
        }
    }

    /// Set a value by key (supports memory.<name> syntax)
    pub fn set(&mut self, key: &str, value: String) {
        // Handle memory.<name> syntax
        if let Some(name) = key.strip_prefix("memory.") {
            self.memory.insert(name.to_string(), value);
            return;
        }

        match key {
            "api_key" => self.api_key = Some(value),
            "api_url" => self.api_url = Some(value),
            "dashboard_url" => self.dashboard_url = Some(value),
            "memory_id" => self.memory_id = Some(value),
            "default_embedding_provider" => self.default_embedding_provider = Some(value),
            "default_llm_provider" => self.default_llm_provider = Some(value),
            _ => {
                self.extra.insert(key.to_string(), value);
            }
        }
    }

    /// Unset a value by key (supports memory.<name> syntax)
    pub fn unset(&mut self, key: &str) -> bool {
        // Handle memory.<name> syntax
        if let Some(name) = key.strip_prefix("memory.") {
            return self.memory.remove(name).is_some();
        }

        match key {
            "api_key" => {
                let had_value = self.api_key.is_some();
                self.api_key = None;
                had_value
            }
            "api_url" => {
                let had_value = self.api_url.is_some();
                self.api_url = None;
                had_value
            }
            "dashboard_url" => {
                let had_value = self.dashboard_url.is_some();
                self.dashboard_url = None;
                had_value
            }
            "memory_id" => {
                let had_value = self.memory_id.is_some();
                self.memory_id = None;
                had_value
            }
            "default_embedding_provider" => {
                let had_value = self.default_embedding_provider.is_some();
                self.default_embedding_provider = None;
                had_value
            }
            "default_llm_provider" => {
                let had_value = self.default_llm_provider.is_some();
                self.default_llm_provider = None;
                had_value
            }
            _ => self.extra.remove(key).is_some(),
        }
    }

    /// Get the default memory ID (from memory.default or legacy memory_id)
    pub fn default_memory_id(&self) -> Option<String> {
        self.memory
            .get("default")
            .cloned()
            .or_else(|| self.memory_id.clone())
    }

    /// Get a named memory ID
    pub fn get_memory(&self, name: &str) -> Option<String> {
        self.memory.get(name).cloned()
    }

    /// Get all keys and values as a map
    pub fn to_map(&self) -> HashMap<String, Option<String>> {
        let mut map = HashMap::new();
        map.insert("api_key".to_string(), self.api_key.clone());
        map.insert("dashboard_url".to_string(), self.dashboard_url.clone());
        // Include named memories
        for (name, id) in &self.memory {
            map.insert(format!("memory.{}", name), Some(id.clone()));
        }
        map.insert(
            "default_embedding_provider".to_string(),
            self.default_embedding_provider.clone(),
        );
        map.insert(
            "default_llm_provider".to_string(),
            self.default_llm_provider.clone(),
        );
        for (k, v) in &self.extra {
            map.insert(k.clone(), Some(v.clone()));
        }
        map
    }

    /// Check if a key is sensitive (should be masked in output)
    pub fn is_sensitive(key: &str) -> bool {
        key.contains("key")
            || key.contains("secret")
            || key.contains("token")
            || key.contains("password")
    }

    /// Mask a sensitive value for display
    pub fn mask_value(value: &str) -> String {
        if value.len() <= 8 {
            "*".repeat(value.len())
        } else {
            format!("{}...{}", &value[..4], &value[value.len() - 4..])
        }
    }
}

/// Known configuration keys for help text
const KNOWN_KEYS: &[(&str, &str)] = &[
    ("api_key", "Memvid API key for authentication"),
    (
        "dashboard_url",
        "Dashboard URL (default: https://memvid.com)",
    ),
    (
        "memory.<name>",
        "Named memory ID (e.g., memory.default, memory.work)",
    ),
    (
        "default_embedding_provider",
        "Default embedding provider (e.g., openai, local)",
    ),
    (
        "default_llm_provider",
        "Default LLM provider for ask commands",
    ),
];

pub fn handle_config(args: ConfigArgs) -> Result<()> {
    match args.command {
        ConfigCommand::Set(set_args) => handle_config_set(set_args),
        ConfigCommand::Get(get_args) => handle_config_get(get_args),
        ConfigCommand::List(list_args) => handle_config_list(list_args),
        ConfigCommand::Unset(unset_args) => handle_config_unset(unset_args),
        ConfigCommand::Check(check_args) => handle_config_check(check_args),
    }
}

fn handle_config_set(args: ConfigSetArgs) -> Result<()> {
    let mut config = PersistentConfig::load()?;
    config.set(&args.key, args.value.clone());
    config.save()?;

    let display_value = if PersistentConfig::is_sensitive(&args.key) {
        PersistentConfig::mask_value(&args.value)
    } else {
        args.value.clone()
    };

    if args.json {
        let output = json!({
            "success": true,
            "key": args.key,
            "value": display_value,
            "message": format!("Configuration '{}' has been set", args.key),
        });
        println!("{}", serde_json::to_string_pretty(&output)?);
    } else {
        println!("Set {} = {}", args.key, display_value);
    }

    Ok(())
}

fn handle_config_get(args: ConfigGetArgs) -> Result<()> {
    let config = PersistentConfig::load()?;

    match config.get(&args.key) {
        Some(value) => {
            let display_value = if PersistentConfig::is_sensitive(&args.key) {
                PersistentConfig::mask_value(&value)
            } else {
                value.clone()
            };

            if args.json {
                let output = json!({
                    "key": args.key,
                    "value": display_value,
                    "found": true,
                });
                println!("{}", serde_json::to_string_pretty(&output)?);
            } else {
                println!("{}", display_value);
            }
        }
        None => {
            if args.json {
                let output = json!({
                    "key": args.key,
                    "value": null,
                    "found": false,
                });
                println!("{}", serde_json::to_string_pretty(&output)?);
            } else {
                // Check if it's a known key
                if let Some((_, description)) = KNOWN_KEYS.iter().find(|(k, _)| *k == args.key) {
                    println!("'{}' is not set", args.key);
                    println!("Description: {}", description);
                } else {
                    println!("'{}' is not set", args.key);
                }
            }
        }
    }

    Ok(())
}

fn handle_config_list(args: ConfigListArgs) -> Result<()> {
    let config = PersistentConfig::load()?;
    let map = config.to_map();
    let config_path = PersistentConfig::config_path()?;

    if args.json {
        let mut values = serde_json::Map::new();
        for (key, value) in &map {
            if let Some(v) = value {
                let display_value = if !args.show_values && PersistentConfig::is_sensitive(&key) {
                    PersistentConfig::mask_value(v)
                } else {
                    v.clone()
                };
                values.insert(key.clone(), json!(display_value));
            }
        }
        let output = json!({
            "config_path": config_path.display().to_string(),
            "values": values,
        });
        println!("{}", serde_json::to_string_pretty(&output)?);
    } else {
        println!("Config file: {}", config_path.display());
        println!();

        let mut has_values = false;
        for (key, description) in KNOWN_KEYS {
            if let Some(Some(value)) = map.get(*key) {
                has_values = true;
                let display_value = if !args.show_values && PersistentConfig::is_sensitive(key) {
                    PersistentConfig::mask_value(value)
                } else {
                    value.clone()
                };
                println!("  {} = {}", key, display_value);
                println!("    {}", description);
            }
        }

        // Show named memories
        if !config.memory.is_empty() {
            has_values = true;
            println!();
            println!("  [memory]");
            for (name, id) in &config.memory {
                println!("    {} = {}", name, id);
            }
        }

        // Show any extra custom keys
        for (key, value) in &config.extra {
            has_values = true;
            let display_value = if !args.show_values && PersistentConfig::is_sensitive(&key) {
                PersistentConfig::mask_value(value)
            } else {
                value.clone()
            };
            println!("  {} = {}", key, display_value);
        }

        if !has_values {
            println!("  (no configuration set)");
            println!();
            println!("Available keys:");
            for (key, description) in KNOWN_KEYS {
                println!("  {} - {}", key, description);
            }
        }

        if !args.show_values {
            println!();
            println!("Use --show-values to display full values");
        }
    }

    Ok(())
}

fn handle_config_unset(args: ConfigUnsetArgs) -> Result<()> {
    let mut config = PersistentConfig::load()?;
    let was_set = config.unset(&args.key);
    config.save()?;

    if args.json {
        let output = json!({
            "success": true,
            "key": args.key,
            "was_set": was_set,
            "message": if was_set {
                format!("Configuration '{}' has been removed", args.key)
            } else {
                format!("Configuration '{}' was not set", args.key)
            },
        });
        println!("{}", serde_json::to_string_pretty(&output)?);
    } else {
        if was_set {
            println!("Removed '{}'", args.key);
        } else {
            println!("'{}' was not set", args.key);
        }
    }

    Ok(())
}

fn handle_config_check(args: ConfigCheckArgs) -> Result<()> {
    let config = PersistentConfig::load()?;
    let config_path = PersistentConfig::config_path()?;

    // Also check environment variables
    let env_api_key = std::env::var("MEMVID_API_KEY").ok();
    let env_api_url = std::env::var("MEMVID_API_URL").ok();

    // Determine effective values (env takes precedence)
    let effective_api_key = env_api_key.clone().or(config.api_key.clone());
    let effective_api_url = env_api_url
        .clone()
        .or(config.api_url.clone())
        .unwrap_or_else(|| "https://memvid.com".to_string());

    let has_api_key = effective_api_key.is_some();
    let api_key_source = if env_api_key.is_some() {
        "environment"
    } else if config.api_key.is_some() {
        "config file"
    } else {
        "not set"
    };

    let api_url_source = if env_api_url.is_some() {
        "environment"
    } else if config.api_url.is_some() {
        "config file"
    } else {
        "default"
    };

    // TODO: Actually verify the API key with the server
    // For now, just check if it's set
    let api_key_valid = has_api_key; // Placeholder - should verify with server

    if args.json {
        let output = json!({
            "config_path": config_path.display().to_string(),
            "api_key": {
                "set": has_api_key,
                "source": api_key_source,
                "valid": api_key_valid,
            },
            "api_url": {
                "value": effective_api_url,
                "source": api_url_source,
            },
            "ready": has_api_key && api_key_valid,
        });
        println!("{}", serde_json::to_string_pretty(&output)?);
    } else {
        println!("Configuration Check");
        println!("===================");
        println!();
        println!("Config file: {}", config_path.display());
        println!();

        // API Key status
        if has_api_key {
            println!(
                "API Key: {} (source: {})",
                if api_key_valid { "valid" } else { "invalid" },
                api_key_source
            );
            if let Some(key) = &effective_api_key {
                println!("  Value: {}", PersistentConfig::mask_value(key));
            }
        } else {
            println!("API Key: not configured");
            println!();
            println!("To set your API key:");
            println!("  memvid config set api_key <your-key>");
            println!("  OR");
            println!("  export MEMVID_API_KEY=<your-key>");
            println!();
            println!("Get your API key at: https://memvid.com/dashboard/api-keys");
        }

        println!();
        println!(
            "API URL: {} (source: {})",
            effective_api_url, api_url_source
        );

        println!();
        if has_api_key && api_key_valid {
            println!("Status: Ready to use");
        } else {
            println!("Status: API key required");
        }
    }

    Ok(())
}