use crate::error::Result;
use crate::framework_events::MemoryEvent;
use crate::hyperdim::HVec10240;
use crate::singularity::ConceptBuilder;
use std::collections::HashMap;
use tracing::instrument;
impl crate::framework::ChaoticSemanticFramework {
#[instrument(err, skip(self, id, vector))]
pub async fn inject_concept_with_ttl(
&self,
id: impl Into<String>,
vector: HVec10240,
ttl_seconds: u64,
) -> Result<()> {
let id = id.into();
Self::validate_concept_id(&id)?;
let concept = ConceptBuilder::new(id.clone())
.with_vector(vector)
.with_ttl(ttl_seconds)
.build()?;
{
let mut sing = self.singularity.write().await;
sing.inject(concept.clone())?;
}
if let Some(ref persistence) = self.persistence {
persistence.save_concept(&concept).await?;
}
self.metrics.inc_concepts_injected(1);
self.emit_event(MemoryEvent::ConceptInjected {
id,
timestamp: concept.modified_at,
});
Ok(())
}
#[instrument(err, skip(self, text))]
pub async fn inject_text_with_ttl(&self, id: &str, text: &str, ttl_seconds: u64) -> Result<()> {
let encoder = crate::encoder::TextEncoder::new();
let vector = encoder.encode(text);
self.inject_concept_with_ttl(id, vector, ttl_seconds).await
}
#[instrument(err, skip(self))]
pub async fn purge_expired(&self) -> Result<usize> {
let mut sing = self.singularity.write().await;
let count = sing.purge_expired();
Ok(count)
}
pub async fn inject_text(&self, id: &str, text: &str) -> Result<()> {
let encoder = crate::encoder::TextEncoder::new();
let vector = encoder.encode(text);
self.inject_concept(id, vector).await
}
pub async fn inject_text_with_metadata(
&self,
id: &str,
text: &str,
metadata: HashMap<String, serde_json::Value>,
) -> Result<()> {
let encoder = crate::encoder::TextEncoder::new();
let vector = encoder.encode(text);
self.inject_concept_with_metadata(id, vector, metadata)
.await
}
pub async fn probe_text(&self, query: &str, top_k: usize) -> Result<Vec<(String, f32)>> {
let encoder = crate::encoder::TextEncoder::new();
let vector = encoder.encode(query);
self.probe(vector, top_k).await
}
}