frankensearch-embed 0.2.2

Embedder implementations for frankensearch (hash, model2vec, fastembed)
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
//! XDG-compliant model cache directory layout.
//!
//! Resolution order for the model cache root:
//!
//! 1. `FRANKENSEARCH_MODEL_DIR` — explicit override (CI, Docker, shared cache)
//! 2. `FRANKENSEARCH_DATA_DIR` — override data home for all frankensearch data
//! 3. `XDG_DATA_HOME/frankensearch/models/` — XDG spec (Linux)
//! 4. `~/Library/Application Support/frankensearch/models/` — macOS (when XDG unset)
//! 5. `~/.local/share/frankensearch/models/` — POSIX fallback
//!
//! Within the cache root, each model gets a base directory. Versioned
//! subdirectories are also provisioned for forward-compatible migrations:
//!
//! ```text
//! <root>/
//!   potion-base-128M/
//!   all-MiniLM-L6-v2/
//!     onnx/model.onnx
//!     tokenizer.json
//!     config.json
//!     manifest.json
//!   potion-base-128M/
//!     v1/
//!       model.safetensors
//!       tokenizer.json
//!       config.json
//!       manifest.json
//! ```

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

use frankensearch_core::error::SearchResult;

// ─── Constants ──────────────────────────────────────────────────────────────

/// Environment variable: explicit model cache directory override.
pub const ENV_MODEL_DIR: &str = "FRANKENSEARCH_MODEL_DIR";

/// Environment variable: override data home for all frankensearch data.
pub const ENV_DATA_DIR: &str = "FRANKENSEARCH_DATA_DIR";

/// Environment variable: XDG data home (standard Linux convention).
const ENV_XDG_DATA_HOME: &str = "XDG_DATA_HOME";

/// Subdirectory under the data home for frankensearch.
const FRANKENSEARCH_SUBDIR: &str = "frankensearch";

/// Subdirectory within frankensearch data for model files.
const MODELS_SUBDIR: &str = "models";

/// Schema version for the cache layout format.
pub const MODEL_CACHE_LAYOUT_VERSION: u32 = 1;

/// Known model directory names and their current version tags.
const KNOWN_MODELS: &[KnownModel] = &[
    KnownModel {
        dir_name: "potion-base-128M",
        version: "v1",
        description: "Potion 128M fast embedder (256d)",
    },
    KnownModel {
        dir_name: "potion-multilingual-128M",
        version: "v1",
        description: "Potion multilingual 128M embedder (256d)",
    },
    KnownModel {
        dir_name: "all-MiniLM-L6-v2",
        version: "v1",
        description: "MiniLM-L6-v2 quality embedder (384d)",
    },
    KnownModel {
        dir_name: "ms-marco-MiniLM-L-6-v2",
        version: "v1",
        description: "MS MARCO MiniLM reranker",
    },
];

// ─── Known Model Metadata ──────────────────────────────────────────────────

/// Metadata for a known model in the cache layout.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KnownModel {
    /// Directory name under the cache root.
    pub dir_name: &'static str,
    /// Current version tag.
    pub version: &'static str,
    /// Human-readable description.
    pub description: &'static str,
}

/// Return all known models in the cache layout.
#[must_use]
pub const fn known_models() -> &'static [KnownModel] {
    KNOWN_MODELS
}

// ─── Cache Root Resolution ──────────────────────────────────────────────────

/// Resolve the model cache root directory using the priority chain.
///
/// Does NOT create directories — use [`ensure_cache_layout`] for that.
#[must_use]
pub fn resolve_cache_root() -> PathBuf {
    resolve_cache_root_with(&EnvReader::Real)
}

/// Resolve the cache root with a custom environment reader (for testing).
fn resolve_cache_root_with(env: &dyn EnvLookup) -> PathBuf {
    // 1. FRANKENSEARCH_MODEL_DIR
    if let Some(path) = env_var_if_non_empty(env, ENV_MODEL_DIR) {
        return PathBuf::from(path);
    }

    // 2. FRANKENSEARCH_DATA_DIR
    if let Some(path) = env_var_if_non_empty(env, ENV_DATA_DIR) {
        return PathBuf::from(path).join(MODELS_SUBDIR);
    }

    // 3. XDG_DATA_HOME
    if let Some(path) = env_var_if_non_empty(env, ENV_XDG_DATA_HOME) {
        return PathBuf::from(path)
            .join(FRANKENSEARCH_SUBDIR)
            .join(MODELS_SUBDIR);
    }

    // 4. macOS Application Support (when XDG unset)
    #[cfg(target_os = "macos")]
    {
        if let Some(path) = frankensearch_core::platform_dirs::data_local_dir() {
            return path.join(FRANKENSEARCH_SUBDIR).join(MODELS_SUBDIR);
        }
    }

    // 5. ~/.local/share/frankensearch/models/
    if let Some(home) = frankensearch_core::platform_dirs::home_dir() {
        return home
            .join(".local")
            .join("share")
            .join(FRANKENSEARCH_SUBDIR)
            .join(MODELS_SUBDIR);
    }

    // Ultimate fallback: data_local_dir or ./models
    frankensearch_core::platform_dirs::data_local_dir().map_or_else(
        || PathBuf::from(MODELS_SUBDIR),
        |p| p.join(FRANKENSEARCH_SUBDIR).join(MODELS_SUBDIR),
    )
}

// ─── Cache Layout ───────────────────────────────────────────────────────────

/// Description of the full cache directory tree.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModelCacheLayout {
    /// Root directory of the model cache.
    pub root: PathBuf,
    /// Per-model versioned directories.
    pub model_dirs: Vec<ModelDirEntry>,
}

/// One model's directory entry within the cache layout.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModelDirEntry {
    /// Model directory name.
    pub name: String,
    /// Version tag.
    pub version: String,
    /// Full path to the model base directory.
    pub path: PathBuf,
}

impl ModelCacheLayout {
    /// Build the layout description for a given root.
    #[must_use]
    pub fn for_root(root: PathBuf) -> Self {
        let model_dirs = KNOWN_MODELS
            .iter()
            .map(|m| ModelDirEntry {
                name: m.dir_name.to_string(),
                version: m.version.to_string(),
                path: root.join(m.dir_name),
            })
            .collect();
        Self { root, model_dirs }
    }

    /// Build the layout using the default resolved root.
    #[must_use]
    pub fn default_layout() -> Self {
        Self::for_root(resolve_cache_root())
    }

    /// Get the base path for a model by directory name.
    #[must_use]
    pub fn model_path(&self, dir_name: &str) -> Option<&Path> {
        self.model_dirs
            .iter()
            .find(|e| e.name == dir_name)
            .map(|e| e.path.as_path())
    }

    /// Get the versioned path for a model by directory name.
    #[must_use]
    pub fn versioned_model_path(&self, dir_name: &str) -> Option<PathBuf> {
        self.model_dirs
            .iter()
            .find(|e| e.name == dir_name)
            .map(|e| e.path.join(&e.version))
    }

    /// Get the model's parent directory (without the version suffix).
    #[must_use]
    pub fn model_base_path(&self, dir_name: &str) -> Option<PathBuf> {
        self.model_dirs
            .iter()
            .find(|e| e.name == dir_name)
            .map(|e| self.root.join(&e.name))
    }
}

// ─── Ensure Layout Exists ──────────────────────────────────────────────────

/// Ensure the cache layout directories exist on disk.
///
/// Creates the root and all known model version directories. Safe to call
/// multiple times (idempotent).
///
/// # Errors
///
/// Returns `SearchError` if directory creation fails.
pub fn ensure_cache_layout(layout: &ModelCacheLayout) -> SearchResult<()> {
    std::fs::create_dir_all(&layout.root)?;

    for entry in &layout.model_dirs {
        std::fs::create_dir_all(&entry.path)?;
        std::fs::create_dir_all(entry.path.join(&entry.version))?;
    }

    Ok(())
}

/// Resolve the cache root and ensure all directories exist.
///
/// This is the primary entry point for consumers who just want a working
/// model cache.
///
/// # Errors
///
/// Returns `SearchError` if directory creation fails.
pub fn ensure_default_cache() -> SearchResult<ModelCacheLayout> {
    let layout = ModelCacheLayout::default_layout();
    ensure_cache_layout(&layout)?;
    Ok(layout)
}

// ─── Model Path Resolution ─────────────────────────────────────────────────

/// Resolve the expected path for a specific model file within the cache.
///
/// Returns `None` if the model directory name is not in the known layout.
#[must_use]
pub fn model_file_path(
    layout: &ModelCacheLayout,
    model_dir: &str,
    file_name: &str,
) -> Option<PathBuf> {
    layout.model_path(model_dir).map(|p| p.join(file_name))
}

/// Check whether a specific model appears installed (all expected files present).
#[must_use]
pub fn is_model_installed(model_versioned_dir: &Path, required_files: &[&str]) -> bool {
    if !model_versioned_dir.is_dir() {
        return false;
    }
    let all_present_in = |dir: &Path| required_files.iter().all(|f| dir.join(f).is_file());
    if all_present_in(model_versioned_dir) {
        return true;
    }
    let version_fallbacks = ["v1", "v2"];
    version_fallbacks
        .iter()
        .map(|version| model_versioned_dir.join(version))
        .any(|candidate| candidate.is_dir() && all_present_in(&candidate))
}

// ─── Environment Abstraction (for testing) ─────────────────────────────────

trait EnvLookup {
    fn var(&self, key: &str) -> Option<String>;
}

fn env_var_if_non_empty(env: &dyn EnvLookup, key: &str) -> Option<String> {
    env.var(key).filter(|value| !value.trim().is_empty())
}

enum EnvReader {
    Real,
    #[cfg(test)]
    Mock(std::collections::HashMap<String, String>),
}

impl EnvLookup for EnvReader {
    fn var(&self, key: &str) -> Option<String> {
        match self {
            Self::Real => std::env::var(key).ok(),
            #[cfg(test)]
            Self::Mock(map) => map.get(key).cloned(),
        }
    }
}

// ─── Tests ──────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    fn mock_env(pairs: &[(&str, &str)]) -> EnvReader {
        let map: HashMap<String, String> = pairs
            .iter()
            .map(|(k, v)| ((*k).to_string(), (*v).to_string()))
            .collect();
        EnvReader::Mock(map)
    }

    #[test]
    fn resolve_frankensearch_model_dir_takes_priority() {
        let env = mock_env(&[
            (ENV_MODEL_DIR, "/custom/models"),
            (ENV_DATA_DIR, "/custom/data"),
            (ENV_XDG_DATA_HOME, "/xdg"),
        ]);
        let root = resolve_cache_root_with(&env);
        assert_eq!(root, PathBuf::from("/custom/models"));
    }

    #[test]
    fn resolve_frankensearch_data_dir_adds_models_subdir() {
        let env = mock_env(&[(ENV_DATA_DIR, "/custom/data")]);
        let root = resolve_cache_root_with(&env);
        assert_eq!(root, PathBuf::from("/custom/data/models"));
    }

    #[test]
    fn resolve_empty_model_dir_falls_back_to_data_dir() {
        let env = mock_env(&[(ENV_MODEL_DIR, ""), (ENV_DATA_DIR, "/custom/data")]);
        let root = resolve_cache_root_with(&env);
        assert_eq!(root, PathBuf::from("/custom/data/models"));
    }

    #[test]
    fn resolve_empty_data_dir_falls_back_to_xdg() {
        let env = mock_env(&[(ENV_DATA_DIR, "   "), (ENV_XDG_DATA_HOME, "/xdg/data")]);
        let root = resolve_cache_root_with(&env);
        assert_eq!(root, PathBuf::from("/xdg/data/frankensearch/models"));
    }

    #[test]
    fn resolve_xdg_data_home_adds_frankensearch_models() {
        let env = mock_env(&[(ENV_XDG_DATA_HOME, "/xdg/data")]);
        let root = resolve_cache_root_with(&env);
        assert_eq!(root, PathBuf::from("/xdg/data/frankensearch/models"));
    }

    #[test]
    fn resolve_empty_env_falls_back_to_home() {
        let env = mock_env(&[]);
        let root = resolve_cache_root_with(&env);
        // Should contain "frankensearch" and "models" somewhere in the path.
        let path_str = root.to_string_lossy();
        assert!(
            path_str.contains("frankensearch") && path_str.contains("models"),
            "expected frankensearch/models in path, got: {path_str}"
        );
    }

    #[test]
    fn layout_for_root_creates_correct_entries() {
        let layout = ModelCacheLayout::for_root(PathBuf::from("/test/models"));
        assert_eq!(layout.root, PathBuf::from("/test/models"));
        assert_eq!(layout.model_dirs.len(), KNOWN_MODELS.len());

        let potion = layout
            .model_dirs
            .iter()
            .find(|e| e.name == "potion-base-128M")
            .expect("potion entry");
        assert_eq!(potion.version, "v1");
        assert_eq!(potion.path, PathBuf::from("/test/models/potion-base-128M"));
    }

    #[test]
    fn layout_model_path_returns_base_path() {
        let layout = ModelCacheLayout::for_root(PathBuf::from("/m"));
        let path = layout.model_path("all-MiniLM-L6-v2").unwrap();
        assert_eq!(path, Path::new("/m/all-MiniLM-L6-v2"));
    }

    #[test]
    fn layout_versioned_model_path_returns_versioned_path() {
        let layout = ModelCacheLayout::for_root(PathBuf::from("/m"));
        let path = layout.versioned_model_path("all-MiniLM-L6-v2").unwrap();
        assert_eq!(path, Path::new("/m/all-MiniLM-L6-v2/v1"));
    }

    #[test]
    fn layout_model_path_unknown_returns_none() {
        let layout = ModelCacheLayout::for_root(PathBuf::from("/m"));
        assert!(layout.model_path("nonexistent-model").is_none());
    }

    #[test]
    fn layout_model_base_path_strips_version() {
        let layout = ModelCacheLayout::for_root(PathBuf::from("/m"));
        let base = layout.model_base_path("all-MiniLM-L6-v2").unwrap();
        assert_eq!(base, PathBuf::from("/m/all-MiniLM-L6-v2"));
    }

    #[test]
    fn model_file_path_resolves_correctly() {
        let layout = ModelCacheLayout::for_root(PathBuf::from("/cache"));
        let path = model_file_path(&layout, "all-MiniLM-L6-v2", "onnx/model.onnx");
        assert_eq!(
            path,
            Some(PathBuf::from("/cache/all-MiniLM-L6-v2/onnx/model.onnx"))
        );
    }

    #[test]
    fn model_file_path_unknown_model_returns_none() {
        let layout = ModelCacheLayout::for_root(PathBuf::from("/cache"));
        assert!(model_file_path(&layout, "unknown", "file.bin").is_none());
    }

    #[test]
    fn ensure_cache_layout_creates_directories() {
        let temp = tempfile::tempdir().unwrap();
        let layout = ModelCacheLayout::for_root(temp.path().join("models"));
        ensure_cache_layout(&layout).unwrap();

        assert!(layout.root.is_dir());
        for entry in &layout.model_dirs {
            assert!(
                entry.path.is_dir(),
                "expected dir: {}",
                entry.path.display()
            );
            assert!(
                entry.path.join(&entry.version).is_dir(),
                "expected version dir: {}",
                entry.path.join(&entry.version).display()
            );
        }
    }

    #[test]
    fn ensure_cache_layout_idempotent() {
        let temp = tempfile::tempdir().unwrap();
        let layout = ModelCacheLayout::for_root(temp.path().join("models"));
        ensure_cache_layout(&layout).unwrap();
        ensure_cache_layout(&layout).unwrap();
        assert!(layout.root.is_dir());
    }

    #[test]
    fn ensure_default_cache_returns_working_layout() {
        // Instead of mutating env (unsafe in edition 2024), test the
        // ensure_cache_layout + for_root path which is what ensure_default_cache
        // delegates to.
        let temp = tempfile::tempdir().unwrap();
        let root = temp.path().join("isolated-models");
        let layout = ModelCacheLayout::for_root(root.clone());
        ensure_cache_layout(&layout).unwrap();

        assert_eq!(layout.root, root);
        assert!(root.is_dir());
        // Verify all model dirs were created.
        for entry in &layout.model_dirs {
            assert!(entry.path.is_dir());
            assert!(entry.path.join(&entry.version).is_dir());
        }
    }

    #[test]
    fn is_model_installed_accepts_base_dir_with_version_subdir() {
        let temp = tempfile::tempdir().unwrap();
        let model_base = temp.path().join("model");
        let versioned = model_base.join("v1");
        std::fs::create_dir_all(&versioned).unwrap();
        std::fs::write(versioned.join("tokenizer.json"), b"stub").unwrap();
        std::fs::write(versioned.join("model.onnx"), b"stub").unwrap();
        assert!(is_model_installed(
            &model_base,
            &["tokenizer.json", "model.onnx"]
        ));
    }

    #[test]
    fn is_model_installed_false_when_dir_missing() {
        let temp = tempfile::tempdir().unwrap();
        let missing = temp.path().join("nonexistent");
        assert!(!is_model_installed(&missing, &["model.onnx"]));
    }

    #[test]
    fn is_model_installed_false_when_files_missing() {
        let temp = tempfile::tempdir().unwrap();
        let model_dir = temp.path().join("model/v1");
        std::fs::create_dir_all(&model_dir).unwrap();
        std::fs::write(model_dir.join("tokenizer.json"), b"stub").unwrap();
        assert!(!is_model_installed(
            &model_dir,
            &["tokenizer.json", "model.onnx"]
        ));
    }

    #[test]
    fn is_model_installed_true_when_all_present() {
        let temp = tempfile::tempdir().unwrap();
        let model_dir = temp.path().join("model/v1");
        std::fs::create_dir_all(&model_dir).unwrap();
        std::fs::write(model_dir.join("tokenizer.json"), b"stub").unwrap();
        std::fs::write(model_dir.join("model.onnx"), b"stub").unwrap();
        assert!(is_model_installed(
            &model_dir,
            &["tokenizer.json", "model.onnx"]
        ));
    }

    #[test]
    fn is_model_installed_handles_nested_files() {
        let temp = tempfile::tempdir().unwrap();
        let model_dir = temp.path().join("model/v1");
        std::fs::create_dir_all(model_dir.join("onnx")).unwrap();
        std::fs::write(model_dir.join("onnx/model.onnx"), b"stub").unwrap();
        std::fs::write(model_dir.join("tokenizer.json"), b"stub").unwrap();
        assert!(is_model_installed(
            &model_dir,
            &["onnx/model.onnx", "tokenizer.json"]
        ));
    }

    #[test]
    fn known_models_is_not_empty() {
        assert!(!known_models().is_empty());
        for m in known_models() {
            assert!(!m.dir_name.is_empty());
            assert!(!m.version.is_empty());
            assert!(!m.description.is_empty());
        }
    }

    #[test]
    fn layout_schema_version() {
        assert_eq!(MODEL_CACHE_LAYOUT_VERSION, 1);
    }

    #[test]
    fn env_constants_match_expected_values() {
        assert_eq!(ENV_MODEL_DIR, "FRANKENSEARCH_MODEL_DIR");
        assert_eq!(ENV_DATA_DIR, "FRANKENSEARCH_DATA_DIR");
    }
}