MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / vault_embed

Method vault_embed

atomic-repository/src/repository/vault_embeddings.rs:51–129  ·  view source on GitHub ↗

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,
    )

Source from the content-addressed store, hash-verified

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 }

Callers 5

runMethod · 0.80
vault_embed_allMethod · 0.80
vault_auto_indexMethod · 0.80

Calls 10

chunk_by_headingFunction · 0.85
vault_retrieveMethod · 0.80
read_txnMethod · 0.80
write_txnMethod · 0.80
del_embeddingsMethod · 0.80
commitMethod · 0.80
put_embeddingMethod · 0.80
get_embeddingMethod · 0.45
iterMethod · 0.45
is_emptyMethod · 0.45

Tested by 2