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
//! API client for Memvid control plane
//!
//! This module provides HTTP client functionality for interacting with the Memvid
//! control plane API, specifically for ticket synchronization and application.

use std::time::Duration;

use anyhow::{anyhow, bail, Context, Result};
use reqwest::blocking::{Client, Response};
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::commands::config::PersistentConfig;
use crate::config::CliConfig;

const API_KEY_HEADER: &str = "X-API-KEY";
const JSON_CONTENT_TYPE: &str = "application/json";
const DEFAULT_DASHBOARD_URL: &str = "https://memvid.com";

/// Get the dashboard URL from environment or config file
fn get_dashboard_url() -> String {
    // Priority: env var > config file > default
    if let Ok(url) = std::env::var("MEMVID_DASHBOARD_URL") {
        if !url.trim().is_empty() {
            return url;
        }
    }
    if let Ok(config) = PersistentConfig::load() {
        if let Some(url) = config.dashboard_url {
            return url;
        }
    }
    DEFAULT_DASHBOARD_URL.to_string()
}

/// Wrapper for ticket data in sync response
#[derive(Debug, Deserialize)]
pub struct TicketSyncData {
    pub ticket: TicketSyncPayload,
}

/// Payload for ticket synchronization from the control plane
#[derive(Debug, Deserialize)]
pub struct TicketSyncPayload {
    pub memory_id: Uuid,
    #[serde(alias = "seq_no")]
    pub sequence: i64,
    pub issuer: String,
    pub expires_in: u64,
    #[serde(default)]
    pub capacity_bytes: Option<u64>,
    pub signature: String,
}

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct ApiEnvelope<T> {
    status: String,
    request_id: String,
    data: Option<T>,
    error: Option<ApiErrorBody>,
    signature: String,
}

#[derive(Debug, Deserialize)]
struct ApiErrorBody {
    code: String,
    message: String,
}

pub struct TicketSyncResponse {
    pub payload: TicketSyncPayload,
    pub request_id: String,
}

/// Dashboard API response format (simpler than control plane)
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct DashboardTicketResponse {
    data: TicketSyncData,
    request_id: String,
}

pub fn fetch_ticket(config: &CliConfig, memory_id: &Uuid) -> Result<TicketSyncResponse> {
    let api_key = require_api_key(config)?;
    let client = http_client()?;

    // Use the dashboard URL for per-memory tickets
    let base_url = get_dashboard_url();
    // Handle both http://localhost:3001 and http://localhost:3001/api
    let base = base_url.trim_end_matches('/').trim_end_matches("/api");
    let url = format!("{}/api/memories/{}/tickets/sync", base, memory_id);

    let response = client
        .post(&url)
        .headers(auth_headers(api_key)?)
        .header(CONTENT_TYPE, JSON_CONTENT_TYPE)
        .body("{}")
        .send()
        .with_context(|| format!("failed to contact ticket sync endpoint at {}", url))?;

    let status = response.status();
    if !status.is_success() {
        let error_text = response
            .text()
            .unwrap_or_else(|_| "unknown error".to_string());
        if status.as_u16() == 401 {
            bail!("Invalid API key. Get a valid key at https://memvid.com/dashboard/api-keys");
        }
        if status.as_u16() == 404 {
            bail!("Memory not found. Create it first at https://memvid.com/dashboard");
        }
        if status.as_u16() == 403 {
            bail!("You don't have access to this memory");
        }
        bail!("Ticket sync error ({}): {}", status, error_text);
    }

    let dashboard_response: DashboardTicketResponse = response
        .json()
        .context("failed to parse ticket sync response")?;

    Ok(TicketSyncResponse {
        payload: dashboard_response.data.ticket,
        request_id: dashboard_response.request_id,
    })
}

#[derive(serde::Serialize)]
pub struct ApplyTicketRequest<'a> {
    pub issuer: &'a str,
    pub seq_no: i64,
    pub expires_in: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub capacity_bytes: Option<u64>,
    pub signature: &'a str,
}

pub fn apply_ticket(
    config: &CliConfig,
    memory_id: &Uuid,
    request: &ApplyTicketRequest<'_>,
) -> Result<String> {
    let api_key = require_api_key(config)?;
    let client = http_client()?;
    let url = format!(
        "{}/memories/{}/tickets/apply",
        config.api_url.trim_end_matches('/'),
        memory_id
    );
    let response = client
        .post(url)
        .headers(auth_headers(api_key)?)
        .json(request)
        .send()
        .with_context(|| "failed to contact ticket apply endpoint")?;
    let envelope: ApiEnvelope<serde_json::Value> = parse_envelope(response)?;
    Ok(envelope.request_id)
}

#[derive(Debug, Serialize)]
pub struct RegisterFileRequest<'a> {
    pub file_name: &'a str,
    pub file_path: &'a str,
    pub file_size: i64,
    pub machine_id: &'a str,
}

/// Response from file registration
#[derive(Debug, Deserialize)]
pub struct RegisterFileResponse {
    pub id: String,
    pub memory_id: String,
    pub file_name: String,
    pub file_path: String,
    pub file_size: i64,
    pub machine_id: String,
    pub last_synced: String,
    pub created_at: String,
}

/// Dashboard file registration response format
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
struct DashboardFileResponse {
    data: RegisterFileResponse,
    request_id: String,
}

/// Register a file with the dashboard
pub fn register_file(
    _config: &CliConfig,
    memory_id: &Uuid,
    request: &RegisterFileRequest<'_>,
    api_key: &str,
) -> Result<RegisterFileResponse> {
    let client = http_client()?;

    // Use the dashboard URL for file registration
    let base_url = get_dashboard_url();
    let url = format!(
        "{}/api/memories/{}/files",
        base_url.trim_end_matches('/'),
        memory_id
    );

    let response = client
        .post(&url)
        .headers(auth_headers(api_key)?)
        .json(request)
        .send()
        .with_context(|| "failed to contact file registration endpoint")?;

    let status = response.status();
    if !status.is_success() {
        let error_text = response
            .text()
            .unwrap_or_else(|_| "unknown error".to_string());
        if status.as_u16() == 409 {
            // Try to extract the specific message from the API response
            if let Ok(json) = serde_json::from_str::<serde_json::Value>(&error_text) {
                if let Some(msg) = json
                    .get("error")
                    .and_then(|e| e.get("message"))
                    .and_then(|m| m.as_str())
                {
                    bail!("{}", msg);
                }
            }
            bail!("This memory is already bound to another file. Each memory can only be bound to one MV2 file.");
        }
        bail!("File registration error ({}): {}", status, error_text);
    }

    let dashboard_response: DashboardFileResponse = response
        .json()
        .context("failed to parse file registration response")?;

    Ok(dashboard_response.data)
}

fn http_client() -> Result<Client> {
    crate::http::blocking_client(Duration::from_secs(15))
}

fn auth_headers(api_key: &str) -> Result<HeaderMap> {
    let mut headers = HeaderMap::new();
    let value = HeaderValue::from_str(api_key)
        .map_err(|_| anyhow!("API key contains invalid characters"))?;
    headers.insert(API_KEY_HEADER, value);
    Ok(headers)
}

fn require_api_key(config: &CliConfig) -> Result<&str> {
    config
        .api_key
        .as_deref()
        .ok_or_else(|| anyhow!("MEMVID_API_KEY is not set"))
}

fn parse_envelope<T: DeserializeOwned>(response: Response) -> Result<ApiEnvelope<T>> {
    let status = response.status();
    let envelope = response.json::<ApiEnvelope<T>>()?;
    if envelope.status == "ok" {
        return Ok(envelope);
    }

    let message = envelope
        .error
        .map(|err| format!("{}: {}", err.code, err.message))
        .unwrap_or_else(|| format!("request failed with status {}", status));
    bail!(message);
}

// ===========================================================================
// Org-level ticket API (for plan-based capacity validation)
// ===========================================================================

/// Organisation-level signed ticket from the dashboard
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrgTicket {
    pub version: i32,
    pub user_id: String,
    pub org_id: String,
    pub plan_id: String,
    pub capacity_bytes: u64,
    pub features: Vec<String>,
    pub expires_at: i64,
    pub signature: String,
}

impl OrgTicket {
    /// Check if this ticket has expired
    pub fn is_expired(&self) -> bool {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs() as i64)
            .unwrap_or(0);
        self.expires_at < now
    }

    /// Check if the plan is a paid plan
    pub fn is_paid(&self) -> bool {
        matches!(self.plan_id.as_str(), "starter" | "pro" | "enterprise")
    }

    /// Get remaining time in seconds (0 if expired)
    pub fn expires_in_secs(&self) -> u64 {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs() as i64)
            .unwrap_or(0);
        if self.expires_at > now {
            (self.expires_at - now) as u64
        } else {
            0
        }
    }
}

/// Plan information from the ticket response
#[derive(Debug, Clone, Deserialize)]
pub struct PlanInfo {
    pub id: String,
    pub name: String,
    pub limits: PlanLimits,
    pub features: Vec<String>,
}

/// Plan limits
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PlanLimits {
    pub capacity_bytes: u64,
    #[serde(default)]
    pub memory_files: Option<u64>,
    #[serde(default)]
    pub max_file_size: Option<u64>,
}

/// Subscription status information
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SubscriptionInfo {
    pub status: String,
    pub expires_at: i64,
    /// ISO date when paid plan started
    #[serde(default)]
    pub plan_start_date: Option<String>,
    /// ISO date when current billing period ends (renews)
    #[serde(default)]
    pub current_period_end: Option<String>,
    /// ISO date when paid plan ends (for canceled subscriptions in grace period)
    #[serde(default)]
    pub plan_end_date: Option<String>,
}

/// Organisation information
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OrgInfo {
    pub id: String,
    pub name: String,
    pub total_storage_bytes: u64,
}

/// Response from the /api/ticket endpoint
#[derive(Debug, Clone, Deserialize)]
pub struct OrgTicketResponse {
    pub ticket: OrgTicket,
    pub plan: PlanInfo,
    pub subscription: SubscriptionInfo,
    pub organisation: OrgInfo,
}

/// Fetch an org-level ticket from the dashboard API
///
/// This endpoint returns a signed ticket containing the user's plan capacity
/// and features. The ticket can be used to validate large file operations
/// and feature access.
pub fn fetch_org_ticket(config: &CliConfig) -> Result<OrgTicketResponse> {
    let api_key = require_api_key(config)?;
    let client = http_client()?;

    // Use the dashboard API URL (different from control plane)
    let base_url = get_dashboard_url();
    // Handle both http://localhost:3001 and http://localhost:3001/api
    let base = base_url.trim_end_matches('/').trim_end_matches("/api");
    let url = format!("{}/api/ticket", base);

    let response = client
        .get(&url)
        .headers(auth_headers(api_key)?)
        .send()
        .with_context(|| format!("failed to contact ticket endpoint at {}", url))?;

    let status = response.status();
    if !status.is_success() {
        let error_text = response
            .text()
            .unwrap_or_else(|_| "unknown error".to_string());
        if status.as_u16() == 401 {
            bail!("Invalid API key. Get a valid key at https://memvid.com/dashboard/api-keys");
        }
        bail!("Ticket API error ({}): {}", status, error_text);
    }

    let wrapper: serde_json::Value = response.json().context("failed to parse ticket response")?;

    // Handle the API response wrapper
    // The dashboard API returns: { "data": { "ticket": ..., "plan": ..., ... }, "requestId": ... }
    let data = if let Some(data_field) = wrapper.get("data") {
        data_field.clone()
    } else if wrapper.get("status").and_then(|s| s.as_str()) == Some("ok") {
        wrapper.get("data").cloned().unwrap_or(wrapper.clone())
    } else if wrapper.get("ticket").is_some() {
        // Direct response without wrapper
        wrapper
    } else {
        wrapper
    };

    let ticket_response: OrgTicketResponse =
        serde_json::from_value(data).context("failed to parse ticket data")?;

    Ok(ticket_response)
}

// ===========================================================================
// Query Usage Tracking
// ===========================================================================

/// Response from the /api/v1/query endpoint
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct QueryTrackingResponse {
    pub success: bool,
    #[serde(default)]
    pub queries_used: Option<u64>,
    #[serde(default)]
    pub queries_limit: Option<u64>,
    #[serde(default)]
    pub queries_remaining: Option<u64>,
}

/// Track a query against the user's plan quota.
///
/// This is called before find() and ask() operations to record usage.
/// Returns Ok(()) on success, or an error if quota is exceeded.
/// Network errors are logged but don't fail the operation (best-effort tracking).
pub fn track_query_usage(config: &CliConfig, count: u64) -> Result<()> {
    let api_key = match require_api_key(config) {
        Ok(key) => key,
        Err(_) => {
            // No API key configured - skip tracking
            return Ok(());
        }
    };

    let client = http_client()?;
    let base_url = get_dashboard_url();
    let url = format!("{}/api/v1/query", base_url);

    let body = serde_json::json!({ "count": count });

    let response = match client
        .post(&url)
        .header("Content-Type", "application/json")
        .header("X-API-Key", &*api_key)
        .timeout(std::time::Duration::from_secs(5))
        .body(body.to_string())
        .send()
    {
        Ok(resp) => resp,
        Err(e) => {
            // Network error - log but don't fail the query
            log::warn!("Query tracking failed: {}", e);
            return Ok(());
        }
    };

    let status = response.status();

    if status.as_u16() == 429 {
        // Quota exceeded - this is a hard error
        let body_text = response.text().unwrap_or_default();

        // Try to parse error details
        if let Ok(data) = serde_json::from_str::<serde_json::Value>(&body_text) {
            let message = data
                .get("message")
                .and_then(|v| v.as_str())
                .unwrap_or("Monthly query quota exceeded");
            let limit = data.get("limit").and_then(|v| v.as_u64());
            let used = data.get("used").and_then(|v| v.as_u64());
            let reset_date = data.get("resetDate").and_then(|v| v.as_str());

            let mut error_msg = format!("{}", message);
            if let (Some(used), Some(limit)) = (used, limit) {
                error_msg.push_str(&format!("\nUsed: {} / {}", used, limit));
            }
            if let Some(reset) = reset_date {
                error_msg.push_str(&format!("\nResets: {}", reset));
            }
            error_msg.push_str(&format!("\nUpgrade at: {}/dashboard/plan", base_url));

            bail!(error_msg);
        } else {
            bail!(
                "Monthly query quota exceeded. Upgrade at: {}/dashboard/plan",
                base_url
            );
        }
    }

    if !status.is_success() {
        // Other error - log but don't fail
        log::warn!("Query tracking returned status {}", status);
    }

    Ok(())
}