basemyai 0.1.0

Local memory engine for AI agents: 4 memory layers, temporal RAG, per-agent isolation, mandatory encryption. Built on basemyai-core.
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
//! Setup **hardware-aware** (ADR-010), façon AnythingLLM. Détecte le matériel,
//! choisit modèle + device, fait un fetch **explicite** (jamais silencieux),
//! et persiste le choix dans `~/.local/share/basemyai/provision.json` (Linux /
//! `%APPDATA%` Windows / `~/Library/Application Support` macOS).

use std::path::{Path, PathBuf};

use basemyai_core::{CoreError, Device};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use sysinfo::System;

/// Modèle baseline garanti partout en V1 (compat `.idx` ForgeMyAI).
pub const BASELINE_MODEL_ID: &str = "all-MiniLM-L6-v2";
/// Dimension du baseline.
pub const BASELINE_DIM: usize = 384;

/// URL de base HuggingFace pour le baseline.
const HF_BASE_URL: &str = "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/";

/// Fichiers attendus dans le dossier d'un modèle BERT provisionné.
const REQUIRED_MODEL_FILES: [&str; 3] = ["config.json", "tokenizer.json", "model.safetensors"];

/// SHA-256 officiels, révision `main` du 12 juin 2026 (ADR-010).
const EXPECTED_SHA256: &[(&str, &str)] = &[
    (
        "config.json",
        "953f9c0d463486b10a6871cc2fd59f223b2c70184f49815e7efbcab5d8908b41",
    ),
    (
        "tokenizer.json",
        "be50c3628f2bf5bb5e3a7f17b1f74611b2561a3a27eeab05e5aa30f411572037",
    ),
    (
        "model.safetensors",
        "53aa51172d142c89d9012cce15ae4d6cc0ca6895895114379cacb4fab128d9db",
    ),
];

// ── Types publics ─────────────────────────────────────────────────────────────

/// Specs machine détectées.
#[derive(Debug, Clone)]
pub struct HardwareProfile {
    pub total_ram_mb: u64,
    /// `None` si aucune détection GPU n'a abouti.
    pub gpu_vram_mb: Option<u64>,
    pub cpu_cores: usize,
    pub device: Device,
}

/// Résultat du provisioning : ce que l'`Embedder` du core recevra, déjà résolu.
#[derive(Debug, Clone)]
pub struct ModelProvision {
    pub model_id: String,
    pub dim: usize,
    pub model_path: PathBuf,
    pub device: Device,
}

// ── Config persistée ──────────────────────────────────────────────────────────

/// Version sérialisable de [`Device`] (le core ne dérive pas Serialize).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
enum PersistedDevice {
    Cpu,
    Cuda(usize),
    Metal,
}

impl From<Device> for PersistedDevice {
    fn from(d: Device) -> Self {
        match d {
            Device::Cpu => Self::Cpu,
            Device::Cuda(i) => Self::Cuda(i),
            Device::Metal => Self::Metal,
            _ => Self::Cpu,
        }
    }
}

impl From<PersistedDevice> for Device {
    fn from(d: PersistedDevice) -> Self {
        match d {
            PersistedDevice::Cpu => Self::Cpu,
            PersistedDevice::Cuda(i) => Self::Cuda(i),
            PersistedDevice::Metal => Self::Metal,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct PersistedProvision {
    model_id: String,
    dim: usize,
    model_path: PathBuf,
    device: PersistedDevice,
}

impl From<&ModelProvision> for PersistedProvision {
    fn from(p: &ModelProvision) -> Self {
        Self {
            model_id: p.model_id.clone(),
            dim: p.dim,
            model_path: p.model_path.clone(),
            device: p.device.into(),
        }
    }
}

impl From<PersistedProvision> for ModelProvision {
    fn from(p: PersistedProvision) -> Self {
        Self {
            model_id: p.model_id,
            dim: p.dim,
            model_path: p.model_path,
            device: p.device.into(),
        }
    }
}

// ── API publique ──────────────────────────────────────────────────────────────

/// Détecte le matériel (RAM, VRAM GPU best-effort, cœurs) et résout le device.
#[must_use]
pub fn detect_hardware() -> HardwareProfile {
    let mut sys = System::new();
    sys.refresh_memory();

    let total_ram_mb = sys.total_memory() / (1024 * 1024);
    let cpu_cores = std::thread::available_parallelism().map(usize::from).unwrap_or(1);
    let gpu_vram_mb = detect_vram_mb();
    let device = resolve_device();

    HardwareProfile {
        total_ram_mb,
        gpu_vram_mb,
        cpu_cores,
        device,
    }
}

/// Provisionne le modèle de façon hardware-aware.
///
/// - Lit d'abord la config persistée ; si le modèle est encore présent, retourne
///   immédiatement sans re-détecter le matériel.
/// - Si le modèle est en cache mais sans config : re-détecte et persiste.
/// - Si absent et `consent_to_fetch = false` : erreur propre, aucun download.
/// - Si absent et `consent_to_fetch = true` : télécharge depuis HuggingFace avec
///   vérification SHA-256 des fichiers officiels HuggingFace, puis persiste.
///
/// # Errors
/// [`CoreError::ModelNotProvisioned`] si le modèle est absent, le consentement
/// refusé, ou si le download/vérification échoue.
pub async fn provision(consent_to_fetch: bool) -> crate::Result<ModelProvision> {
    provision_inner(consent_to_fetch, |_, _| {}).await
}

/// Comme [`provision`] mais signale la progression du téléchargement via
/// `on_progress(bytes_reçus, total_octets_optionnel)`.
pub async fn provision_with_progress(
    consent_to_fetch: bool,
    on_progress: impl Fn(u64, Option<u64>),
) -> crate::Result<ModelProvision> {
    provision_inner(consent_to_fetch, on_progress).await
}

// ── Implémentation interne ────────────────────────────────────────────────────

async fn provision_inner(
    consent_to_fetch: bool,
    on_progress: impl Fn(u64, Option<u64>),
) -> crate::Result<ModelProvision> {
    // Fast path : config persistée + modèle toujours présent.
    if let Some(cached) = load_persisted_provision() {
        return Ok(cached);
    }

    let hw = detect_hardware();
    let model_path = baseline_cache_dir();

    if model_present(&model_path) {
        let result = ModelProvision {
            model_id: BASELINE_MODEL_ID.to_string(),
            dim: BASELINE_DIM,
            model_path,
            device: hw.device,
        };
        save_provision(&result);
        return Ok(result);
    }

    if !consent_to_fetch {
        return Err(CoreError::ModelNotProvisioned(format!(
            "modèle '{BASELINE_MODEL_ID}' absent du cache ({}). Lancez le setup \
             hardware-aware avec consentement explicite pour le récupérer.",
            model_path.display()
        ))
        .into());
    }

    fetch_model_files(&model_path, on_progress).await?;

    let result = ModelProvision {
        model_id: BASELINE_MODEL_ID.to_string(),
        dim: BASELINE_DIM,
        model_path,
        device: hw.device,
    };
    save_provision(&result);
    Ok(result)
}

/// Télécharge les fichiers du modèle baseline et appelle `on_progress` à chaque
/// chunk reçu (par fichier — pas de compteur global).
async fn fetch_model_files(target_dir: &Path, on_progress: impl Fn(u64, Option<u64>)) -> crate::Result<()> {
    std::fs::create_dir_all(target_dir).map_err(|e| {
        CoreError::ModelNotProvisioned(format!(
            "impossible de créer le dossier modèle {} : {e}",
            target_dir.display()
        ))
    })?;

    let client = reqwest::Client::new();

    for filename in REQUIRED_MODEL_FILES {
        let url = format!("{HF_BASE_URL}{filename}");
        let dest = target_dir.join(filename);
        let expected = expected_sha256_for(filename);
        download_and_verify(&client, &url, &dest, expected, &on_progress).await?;
    }

    Ok(())
}

/// Télécharge `url` vers `dest` avec vérification SHA-256 et suivi de progression.
///
/// Protocole atomique :
/// 1. Télécharge en streaming par chunks vers `dest.with_extension("tmp")`.
/// 2. Calcule le SHA-256 à la volée.
/// 3. Vérifie contre `expected_sha256` si fourni (erreur dure) ; sinon, vérifie/
///    crée un fichier companion `dest.with_extension("sha256")`.
/// 4. Renomme `.tmp` → `dest` (write atomique).
async fn download_and_verify(
    client: &reqwest::Client,
    url: &str,
    dest: &Path,
    expected_sha256: Option<&str>,
    on_progress: &impl Fn(u64, Option<u64>),
) -> crate::Result<()> {
    let tmp = dest.with_extension("tmp");

    let response = client
        .get(url)
        .send()
        .await
        .map_err(|e| CoreError::ModelNotProvisioned(format!("téléchargement échoué ({url}) : {e}")))?;

    if !response.status().is_success() {
        return Err(CoreError::ModelNotProvisioned(format!("HTTP {} pour {url}", response.status())).into());
    }

    let total = response.content_length();
    let mut received = 0u64;
    let mut hasher = Sha256::new();
    let mut data: Vec<u8> = Vec::with_capacity(total.unwrap_or(0) as usize);

    // Chunk-by-chunk : calcule SHA-256 + signale progression.
    let mut response = response;
    while let Some(chunk) = response
        .chunk()
        .await
        .map_err(|e| CoreError::ModelNotProvisioned(format!("erreur stream ({url}) : {e}")))?
    {
        hasher.update(&chunk);
        received += chunk.len() as u64;
        data.extend_from_slice(&chunk);
        on_progress(received, total);
    }

    let computed = format!("{:x}", hasher.finalize());

    // ── Vérification SHA-256 ────────────────────────────────────────────────
    let sha_path = dest.with_extension("sha256");
    match expected_sha256 {
        Some(expected) => {
            if computed != expected {
                return Err(CoreError::ModelNotProvisioned(format!(
                    "SHA-256 mismatch pour {} : attendu {expected}, calculé {computed}",
                    dest.display()
                ))
                .into());
            }
        }
        None => {
            if sha_path.exists() {
                let stored = std::fs::read_to_string(&sha_path)
                    .map_err(|e| CoreError::ModelNotProvisioned(format!("lecture sha256 companion : {e}")))?;
                if stored.trim() != computed {
                    return Err(CoreError::ModelNotProvisioned(format!(
                        "SHA-256 mismatch (companion) pour {} : stocké {}, calculé {computed}",
                        dest.display(),
                        stored.trim()
                    ))
                    .into());
                }
            } else {
                std::fs::write(&sha_path, &computed)
                    .map_err(|e| CoreError::ModelNotProvisioned(format!("écriture sha256 companion : {e}")))?;
            }
        }
    }

    // Écriture atomique : .tmp → dest.
    std::fs::write(&tmp, &data)
        .map_err(|e| CoreError::ModelNotProvisioned(format!("écriture tmp {} : {e}", tmp.display())))?;
    std::fs::rename(&tmp, dest).map_err(|e| {
        CoreError::ModelNotProvisioned(format!("renommage {}{} : {e}", tmp.display(), dest.display()))
    })?;

    Ok(())
}

// ── Détection VRAM ────────────────────────────────────────────────────────────

/// Détection best-effort de la VRAM GPU : NVIDIA via `nvidia-smi`, puis
/// plateforme-spécifique (macOS via `system_profiler`).
fn detect_vram_mb() -> Option<u64> {
    detect_vram_nvidia_smi().or_else(detect_vram_platform)
}

/// Interroge `nvidia-smi` pour obtenir la VRAM totale du GPU 0, en Mo.
fn detect_vram_nvidia_smi() -> Option<u64> {
    let output = std::process::Command::new("nvidia-smi")
        .args(["--query-gpu=memory.total", "--format=csv,noheader,nounits"])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    // Sortie : une ligne par GPU, valeur en MiB. On prend le premier.
    String::from_utf8_lossy(&output.stdout)
        .lines()
        .next()?
        .trim()
        .parse::<u64>()
        .ok()
}

#[cfg(target_os = "macos")]
fn detect_vram_platform() -> Option<u64> {
    detect_vram_macos()
}

#[cfg(not(target_os = "macos"))]
fn detect_vram_platform() -> Option<u64> {
    None
}

/// Interroge `system_profiler SPDisplaysDataType -json` pour la VRAM (macOS).
/// Fonctionne pour Apple Silicon (mémoire unifiée) et Intel/AMD discrets.
#[cfg(target_os = "macos")]
fn detect_vram_macos() -> Option<u64> {
    let output = std::process::Command::new("system_profiler")
        .args(["SPDisplaysDataType", "-json"])
        .output()
        .ok()?;
    let json: serde_json::Value = serde_json::from_slice(&output.stdout).ok()?;
    let vram_str = json["SPDisplaysDataType"][0]["spdisplays_vram"].as_str()?;
    parse_vram_mb(vram_str)
}

/// Parse une chaîne VRAM style `"8 GB"` ou `"512 MB"` en mégaoctets.
#[cfg(target_os = "macos")]
fn parse_vram_mb(s: &str) -> Option<u64> {
    let s = s.trim().to_ascii_lowercase();
    if let Some(n) = s.strip_suffix(" gb") {
        n.trim().parse::<u64>().ok().map(|gb| gb * 1024)
    } else if let Some(n) = s.strip_suffix(" mb") {
        n.trim().parse::<u64>().ok()
    } else {
        None
    }
}

// ── Device & cache ────────────────────────────────────────────────────────────

/// Choisit le device : **CUDA > Metal > CPU**.
#[must_use]
fn resolve_device() -> Device {
    if cuda_available() {
        Device::Cuda(0)
    } else if metal_available() {
        Device::Metal
    } else {
        Device::Cpu
    }
}

fn cuda_available() -> bool {
    std::env::var_os("CUDA_PATH").is_some() || std::env::var_os("CUDA_HOME").is_some()
}

fn metal_available() -> bool {
    cfg!(target_os = "macos")
}

/// Dossier de cache du modèle baseline : `<cache>/basemyai/models/<model_id>`.
#[must_use]
fn baseline_cache_dir() -> PathBuf {
    let base = dirs::cache_dir()
        .or_else(dirs::home_dir)
        .or_else(|| std::env::var_os("USERPROFILE").map(PathBuf::from))
        .or_else(|| std::env::var_os("HOME").map(PathBuf::from))
        .unwrap_or_else(|| PathBuf::from("."));
    base.join("basemyai").join("models").join(BASELINE_MODEL_ID)
}

/// `true` si tous les fichiers requis existent dans `dir`.
#[must_use]
fn model_present(dir: &Path) -> bool {
    REQUIRED_MODEL_FILES.iter().all(|f| dir.join(f).is_file())
}

/// Retourne le SHA-256 attendu pour `filename`, ou `None` si non ancré.
fn expected_sha256_for(filename: &str) -> Option<&'static str> {
    EXPECTED_SHA256.iter().find(|(f, _)| *f == filename).map(|(_, h)| *h)
}

// ── Persistance config ────────────────────────────────────────────────────────

/// Chemin vers la config persistée : `<data_dir>/basemyai/provision.json`.
fn provision_config_path() -> PathBuf {
    dirs::data_dir()
        .or_else(dirs::home_dir)
        .or_else(|| std::env::var_os("USERPROFILE").map(PathBuf::from))
        .or_else(|| std::env::var_os("HOME").map(PathBuf::from))
        .unwrap_or_else(|| PathBuf::from("."))
        .join("basemyai")
        .join("provision.json")
}

/// Charge la config persistée si elle existe et si le modèle est encore présent.
fn load_persisted_provision() -> Option<ModelProvision> {
    let text = std::fs::read_to_string(provision_config_path()).ok()?;
    let p: PersistedProvision = serde_json::from_str(&text).ok()?;
    let result: ModelProvision = p.into();
    if model_present(&result.model_path) {
        Some(result)
    } else {
        None
    }
}

/// Persiste la config (best-effort — échec silencieux pour ne pas bloquer l'usage).
fn save_provision(provision: &ModelProvision) {
    let path = provision_config_path();
    if let Some(parent) = path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }
    if let Ok(json) = serde_json::to_string_pretty(&PersistedProvision::from(provision)) {
        let _ = std::fs::write(path, json);
    }
}