1use anyhow::{bail, Context, Result};
12use fastembed::{EmbeddingModel, TextEmbedding, TextInitOptions};
13use std::path::PathBuf;
14use std::sync::{Mutex, OnceLock};
15
16pub const EMBED_SLICE: usize = 256;
22
23pub const EMBED_BATCH: usize = 64;
25
26pub const QUERY_PREFIX: &str = "query: ";
28pub const PASSAGE_PREFIX: &str = "passage: ";
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum Model {
33 BgeSmall,
35 E5Multilingual,
37 E5Base,
39}
40
41impl Model {
42 pub fn from_env() -> Self {
45 match std::env::var("POWERSHELL_MCP_EMBED_MODEL").as_deref() {
46 Ok("e5-multilingual") | Ok("multilingual-e5-small") => Model::E5Multilingual,
47 Ok("e5-base") | Ok("multilingual-e5-base") => Model::E5Base,
48 _ => Model::BgeSmall,
49 }
50 }
51
52 fn fastembed(self) -> EmbeddingModel {
53 match self {
54 Model::BgeSmall => EmbeddingModel::BGESmallENV15,
55 Model::E5Multilingual => EmbeddingModel::MultilingualE5Small,
56 Model::E5Base => EmbeddingModel::MultilingualE5Base,
57 }
58 }
59
60 pub fn id(self) -> &'static str {
63 match self {
64 Model::BgeSmall => "bge-small-en-v1.5",
65 Model::E5Multilingual => "multilingual-e5-small",
66 Model::E5Base => "multilingual-e5-base",
67 }
68 }
69
70 pub fn dim(self) -> usize {
72 match self {
73 Model::BgeSmall => 384,
74 Model::E5Multilingual => 384,
75 Model::E5Base => 768,
76 }
77 }
78
79 pub fn uses_prefixes(self) -> bool {
82 matches!(self, Model::E5Multilingual | Model::E5Base)
83 }
84}
85
86pub fn current_model() -> Model {
88 Model::from_env()
89}
90
91static EMBEDDER: OnceLock<Mutex<TextEmbedding>> = OnceLock::new();
92
93pub fn embed_texts(texts: &[String]) -> Result<Vec<Vec<f32>>> {
97 let emb = embedder()?;
98 let mut out = Vec::with_capacity(texts.len());
99 for slice in texts.chunks(EMBED_SLICE) {
100 let vectors = emb
101 .lock()
102 .map_err(|_| anyhow::anyhow!("embedder mutex poisoned"))?
103 .embed(slice.to_vec(), Some(EMBED_BATCH))
104 .context("embed texts")?;
105 if vectors.len() != slice.len() {
106 bail!(
107 "embedding returned {} vectors for {} texts",
108 vectors.len(),
109 slice.len()
110 );
111 }
112 out.extend(vectors);
113 }
114 Ok(out)
115}
116
117pub fn model_cache_dir() -> Option<PathBuf> {
121 std::env::var("FASTEMBED_CACHE")
122 .map(PathBuf::from)
123 .ok()
124 .or_else(|| {
125 std::env::var("HOME")
126 .ok()
127 .map(|h| PathBuf::from(h).join(".cache").join("fastembed"))
128 })
129}
130
131pub fn active_model_cached() -> bool {
134 let Some(cache) = model_cache_dir() else {
135 return false;
136 };
137 let Ok(entries) = std::fs::read_dir(&cache) else {
138 return false;
139 };
140 entries.flatten().any(|e| {
141 e.file_name()
142 .to_string_lossy()
143 .contains(current_model().id())
144 })
145}
146
147fn embedder() -> Result<&'static Mutex<TextEmbedding>> {
148 if let Some(e) = EMBEDDER.get() {
149 return Ok(e);
150 }
151 let mut init = TextInitOptions::default();
152 init.model_name = current_model().fastembed();
153 init.show_download_progress = true;
154 init.cache_dir = model_cache_dir().unwrap_or_else(|| PathBuf::from(".fastembed_cache"));
155 let emb = TextEmbedding::try_new(init)
156 .context("init embedding model (first run downloads the model binary)")?;
157 let _ = EMBEDDER.set(Mutex::new(emb));
159 Ok(EMBEDDER.get().expect("embedder set"))
160}
161
162#[cfg(test)]
163mod tests {
164 use super::*;
165
166 #[test]
167 fn bge_small_is_the_default() {
168 std::env::remove_var("POWERSHELL_MCP_EMBED_MODEL");
169 assert_eq!(current_model(), Model::BgeSmall);
170 assert_eq!(Model::BgeSmall.id(), "bge-small-en-v1.5");
171 assert_eq!(Model::BgeSmall.dim(), 384);
172 assert!(!Model::BgeSmall.uses_prefixes());
173 }
174
175 #[test]
176 fn e5_models_use_prefixes() {
177 assert!(Model::E5Multilingual.uses_prefixes());
178 assert!(Model::E5Base.uses_prefixes());
179 }
180}