Embed a single vault entry using the provided embedding function. Chunks the content by heading/paragraph, computes embeddings via `embed_fn`, and stores them in redb. Returns the number of chunks stored. Staleness detection: skips re-embedding if the content hash matches the existing stored hash.
(
&self,
path: &str,
embed_fn: &F,
config: &EmbedConfig,
)
| 49 | /// Staleness detection: skips re-embedding if the content hash matches |
| 50 | /// the existing stored hash. |
| 51 | pub fn vault_embed<F>( |
| 52 | &self, |
| 53 | path: &str, |
| 54 | embed_fn: &F, |
| 55 | config: &EmbedConfig, |
| 56 | ) -> Result<usize, RepositoryError> |
| 57 | where |
| 58 | F: Fn(&str) -> Vec<f32>, |
| 59 | { |
| 60 | let entry = self |
| 61 | .vault_retrieve(path)? |
| 62 | .ok_or_else(|| RepositoryError::FileNotFound { |
| 63 | path: std::path::PathBuf::from(path), |
| 64 | })?; |
| 65 | |
| 66 | let content = String::from_utf8_lossy(&entry.content_bytes); |
| 67 | let content_hash = entry.content_hash; |
| 68 | |
| 69 | // Check staleness — if first chunk has same hash, skip |
| 70 | { |
| 71 | let txn = self |
| 72 | .pristine |
| 73 | .read_txn() |
| 74 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 75 | if let Ok(Some(existing)) = txn.get_embedding(path, 0) { |
| 76 | if existing.content_hash == content_hash { |
| 77 | return Ok(0); // Content unchanged, skip |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Chunk the content |
| 83 | let chunks = chunk_by_heading(&content, config.max_chunk_tokens); |
| 84 | |
| 85 | // Delete old embeddings for this path |
| 86 | { |
| 87 | let mut txn = self |
| 88 | .pristine |
| 89 | .write_txn() |
| 90 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 91 | let _ = txn |
| 92 | .del_embeddings(path) |
| 93 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 94 | txn.commit() |
| 95 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 96 | } |
| 97 | |
| 98 | // Embed and store each chunk |
| 99 | let mut txn = self |
| 100 | .pristine |
| 101 | .write_txn() |
| 102 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 103 | |
| 104 | let mut count = 0; |
| 105 | for (idx, chunk) in chunks.iter().enumerate() { |
| 106 | if chunk.text.trim().is_empty() { |
| 107 | continue; |
| 108 | } |