use anyhow::Result;
use sha2::{Digest, Sha256};
use super::local_semantic::LocalEmbeddingInputKind;
use super::{EmbeddingFallbackCache, TextEmbedding};
const MEMORY_INDEX_HASH_SCHEMA: &str = "memory-index-v2";
fn memory_index_text(
title: &str,
content: &str,
memory_type: &str,
topic_key: Option<&str>,
search_context: &str,
) -> String {
let mut text = super::memory_embedding_text(title, content, memory_type, topic_key);
if !search_context.trim().is_empty() {
text.push('\n');
text.push_str(search_context);
}
text
}
pub fn embed_memory_index(
title: &str,
content: &str,
memory_type: &str,
topic_key: Option<&str>,
search_context: &str,
) -> Result<TextEmbedding> {
let text = memory_index_text(title, content, memory_type, topic_key, search_context);
super::embed_text(&text, LocalEmbeddingInputKind::Passage)
}
pub(crate) fn embed_memory_index_with_fallback_cache(
title: &str,
content: &str,
memory_type: &str,
topic_key: Option<&str>,
search_context: &str,
cache: &mut EmbeddingFallbackCache,
) -> Result<TextEmbedding> {
let text = memory_index_text(title, content, memory_type, topic_key, search_context);
super::embed_text_with_fallback_cache(&text, LocalEmbeddingInputKind::Passage, cache)
}
pub fn memory_index_hash(
title: &str,
content: &str,
memory_type: &str,
topic_key: Option<&str>,
search_context: &str,
) -> String {
let mut hasher = Sha256::new();
hasher.update(MEMORY_INDEX_HASH_SCHEMA.as_bytes());
hasher.update([0]);
let text = memory_index_text(title, content, memory_type, topic_key, search_context);
hasher.update(text.as_bytes());
let digest = hasher.finalize();
digest.iter().map(|byte| format!("{byte:02x}")).collect()
}