#![allow(clippy::print_stdout)]
use std::collections::HashMap;
use std::sync::Arc;
use entelix::ir::Message;
use entelix::{
BufferMemory, EntityMemory, EntityRecord, ExecutionContext, InMemoryStore, Namespace, Result,
Store, TenantId,
};
#[tokio::main]
async fn main() -> Result<()> {
let buffer_store: Arc<dyn Store<Vec<Message>>> = Arc::new(InMemoryStore::<Vec<Message>>::new());
let entity_store: Arc<dyn Store<HashMap<String, EntityRecord>>> =
Arc::new(InMemoryStore::<HashMap<String, EntityRecord>>::new());
let ctx = ExecutionContext::new();
let alice_buf = BufferMemory::new(buffer_store.clone(), namespace_for("alice"), 5);
let bob_buf = BufferMemory::new(buffer_store, namespace_for("bob"), 5);
let alice_entities = EntityMemory::new(entity_store.clone(), namespace_for("alice"));
let bob_entities = EntityMemory::new(entity_store, namespace_for("bob"));
alice_entities
.set_entity(&ctx, "preference", "vegetarian")
.await?;
alice_entities.set_entity(&ctx, "language", "ko").await?;
bob_entities
.set_entity(&ctx, "preference", "spicy food")
.await?;
bob_entities.set_entity(&ctx, "language", "en").await?;
for i in 1..=7 {
alice_buf
.append(&ctx, Message::user(format!("alice turn {i}")))
.await?;
}
bob_buf.append(&ctx, Message::user("hi from bob")).await?;
println!("=== Alice's view ===");
println!(
"buffer ({} retained, max 5):",
alice_buf.messages(&ctx).await?.len()
);
for msg in alice_buf.messages(&ctx).await? {
if let Some(entelix::ir::ContentPart::Text { text, .. }) = msg.content.first() {
println!(" - {text}");
}
}
println!("entities:");
for (k, v) in alice_entities.all(&ctx).await? {
println!(" - {k}: {v}");
}
println!();
println!("=== Bob's view ===");
println!(
"buffer ({} retained, max 5):",
bob_buf.messages(&ctx).await?.len()
);
for msg in bob_buf.messages(&ctx).await? {
if let Some(entelix::ir::ContentPart::Text { text, .. }) = msg.content.first() {
println!(" - {text}");
}
}
println!("entities:");
for (k, v) in bob_entities.all(&ctx).await? {
println!(" - {k}: {v}");
}
println!();
println!("=== F2 verification ===");
let across_users = alice_entities.entity(&ctx, "bob-only-key").await?;
println!("alice's view of 'bob-only-key' (no path across users): {across_users:?}");
Ok(())
}
fn namespace_for(user: &str) -> Namespace {
Namespace::new(TenantId::new("tenant-acme"))
.with_scope("agent-helper")
.with_scope(format!("user-{user}"))
}