Skip to content

Commit efe383a

Browse files
committed
feat(anda_engine): add get_or_init_caller method to MemoryManagement
1 parent 28e9cd8 commit efe383a

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

agents/anda_assistant/src/agent.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,8 @@ impl Agent<AgentCtx> for Assistant {
203203

204204
let caller_info = self
205205
.memory
206-
.describe_caller(caller)
207-
.await
208-
.unwrap_or_else(|_| {
209-
serde_json::json!({
210-
"type": "Person",
211-
"name": caller.to_string(),
212-
"attributes": {},
213-
"metadata": {},
214-
})
215-
});
216-
206+
.get_or_init_caller(caller, ctx.meta().user.clone())
207+
.await?;
217208
let primer = self.memory.describe_primer().await?;
218209
let instructions = format!(
219210
"{}\n---\n# Your Identity & Knowledge Domain Map\n{}\n",

anda_engine/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "anda_engine"
33
description = "Agents engine for Anda -- an AI agent framework built with Rust, powered by ICP and TEEs."
44
repository = "https://github.com/ldclabs/anda/tree/main/anda_engine"
55
publish = true
6-
version = "0.9.10"
6+
version = "0.9.11"
77
edition.workspace = true
88
keywords.workspace = true
99
categories.workspace = true

anda_engine/src/engine.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ impl Engine {
158158
)
159159
.into());
160160
}
161+
if let Some(user) = &meta.user {
162+
let u = user.trim();
163+
if u.is_empty() || u != user || u.len() > 32 {
164+
return Err(format!("invalid user name {:?}", user).into());
165+
}
166+
}
161167

162168
input.name = if input.name.is_empty() {
163169
self.default_agent.clone()
@@ -202,6 +208,12 @@ impl Engine {
202208
)
203209
.into());
204210
}
211+
if let Some(user) = &meta.user {
212+
let u = user.trim();
213+
if u.is_empty() || u != user || u.len() > 32 {
214+
return Err(format!("invalid user name {:?}", user).into());
215+
}
216+
}
205217

206218
// manager can call any tool
207219
if !self.export_tools.contains(&input.name) && !self.management.is_manager(&caller) {

anda_engine/src/memory.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use ciborium::cbor;
2121
use ic_auth_types::ByteBufB64;
2222
use schemars::JsonSchema;
2323
use serde::{Deserialize, Serialize};
24-
use serde_json::json;
24+
use serde_json::{Map, json};
2525
use std::{
2626
collections::BTreeMap,
2727
fmt,
@@ -395,6 +395,33 @@ impl MemoryManagement {
395395
Ok(user.to_concept_node())
396396
}
397397

398+
pub async fn get_or_init_caller(
399+
&self,
400+
id: &Principal,
401+
name: Option<String>,
402+
) -> Result<Json, KipError> {
403+
let mut attributes = Map::new();
404+
let mut metadata = Map::new();
405+
attributes.insert("id".to_string(), id.to_string().into());
406+
attributes.insert("person_class".to_string(), "Human".into());
407+
if let Some(name) = name {
408+
attributes.insert("name".to_string(), name.into());
409+
}
410+
metadata.insert("author".to_string(), "$system".into());
411+
metadata.insert("status".to_string(), "active".into());
412+
let user = self
413+
.nexus
414+
.get_or_init_concept(
415+
PERSON_TYPE.to_string(),
416+
id.to_string(),
417+
attributes,
418+
metadata,
419+
)
420+
.await?;
421+
422+
Ok(user.to_concept_node())
423+
}
424+
398425
pub async fn add_resource(&self, resource: ResourceRef<'_>) -> Result<u64, DBError> {
399426
let id = self.resources.add_from(&resource).await?;
400427
self.resources.flush(unix_ms()).await?;

0 commit comments

Comments
 (0)