Skip to content

Commit

Permalink
feat: StateFeatures::caller() return Principal
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Feb 18, 2025
1 parent 2264f5b commit c995056
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 50 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion agents/anda_bot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "anda_bot"
description = "I'm Anda ICP, Digital panda 🐼 by Anda framework. Secured in TEE, memories on ICP chain.✨"
repository = "https://github.com/ldclabs/anda/tree/main/agents/anda_bot"
publish = false
version = "0.4.0"
version = "0.4.1"
edition.workspace = true
keywords.workspace = true
categories.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion agents/anda_bot/nitro_enclave/amd64.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ RUN chmod +x ic_tee_daemon
RUN wget -O ic_tee_nitro_gateway https://github.com/ldclabs/ic-tee/releases/download/v0.3.0/ic_tee_nitro_gateway
RUN chmod +x ic_tee_nitro_gateway

RUN wget -O anda_bot https://github.com/ldclabs/anda/releases/download/v0.4.0/anda_bot
RUN wget -O anda_bot https://github.com/ldclabs/anda/releases/download/v0.4.1/anda_bot
RUN chmod +x anda_bot

FROM debian:bookworm-slim AS runtime
Expand Down
13 changes: 8 additions & 5 deletions agents/anda_bot/src/twitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use anda_core::{
Agent, BoxError, CacheFeatures, CompletionFeatures, Path, PutMode, StateFeatures, StoreFeatures,
};
use anda_engine::{
context::AgentCtx, engine::Engine, extension::character::CharacterAgent, rand_number,
context::{AgentCtx, ANONYMOUS},
engine::Engine,
extension::character::CharacterAgent,
rand_number,
};
use anda_lancedb::knowledge::KnowledgeStore;
use ciborium::from_reader;
Expand Down Expand Up @@ -85,7 +88,7 @@ impl TwitterDaemon {

pub async fn run(&self, cancel_token: CancellationToken) -> Result<(), BoxError> {
{
let ctx = self.engine.ctx_with(self.agent.as_ref(), None, None)?;
let ctx = self.engine.ctx_with(self.agent.as_ref(), ANONYMOUS, None)?;
// load seen_tweet_ids from store
let count = self.init_seen_tweet_ids(&ctx).await;

Expand Down Expand Up @@ -180,8 +183,8 @@ impl TwitterDaemon {
log::info!(target: LOG_TARGET, "post new tweet with {} knowledges", knowledges.len());
let ctx = self.engine.ctx_with(
self.agent.as_ref(),
ANONYMOUS,
Some(self.agent.character.username.clone()),
None,
)?;
let req = self
.agent
Expand Down Expand Up @@ -213,8 +216,8 @@ impl TwitterDaemon {
async fn handle_home_timeline(&self) -> Result<(), BoxError> {
let ctx = self.engine.ctx_with(
self.agent.as_ref(),
ANONYMOUS,
Some(self.agent.character.username.clone()),
None,
)?;

let mut seen_tweet_ids: Vec<String> = self.get_seen_tweet_ids(&ctx).await;
Expand Down Expand Up @@ -299,7 +302,7 @@ impl TwitterDaemon {
}
let ctx = self
.engine
.ctx_with(self.agent.as_ref(), Some(tweet_user.clone()), None)?;
.ctx_with(self.agent.as_ref(), ANONYMOUS, Some(tweet_user.clone()))?;
let mut seen_tweet_ids: Vec<String> = self.get_seen_tweet_ids(&ctx).await;

if seen_tweet_ids.contains(&tweet_id) {
Expand Down
2 changes: 1 addition & 1 deletion anda_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "anda_core"
description = "Core types and traits for Anda -- an AI agent framework built with Rust, powered by ICP and TEEs."
repository = "https://github.com/ldclabs/anda/tree/main/anda_core"
publish = true
version = "0.4.1"
version = "0.4.4"
edition.workspace = true
keywords.workspace = true
categories.workspace = true
Expand Down
10 changes: 5 additions & 5 deletions anda_core/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,17 @@ pub trait BaseContext:
pub trait StateFeatures: Sized {
/// Gets the engine ID, which comes from the TEE host
fn id(&self) -> Principal;
/// Gets the verified caller principal if available.
/// A non anonymous Principal indicates the request has been verified
/// using ICP blockchain's signature verification algorithm.
fn caller(&self) -> Principal;

/// Gets the username from request context.
/// Note: This is not verified and should not be used as a trusted identifier.
/// For example, if triggered by a bot of X platform, this might be the username
/// of the user interacting with the bot.
fn user(&self) -> Option<String>;

/// Gets the verified caller principal if available.
/// A non-None value indicates the request has been verified
/// using ICP blockchain's signature verification algorithm.
fn caller(&self) -> Option<Principal>;

/// Gets the cancellation token for the current execution context.
/// Each call level has its own token scope.
/// For example, when an agent calls a tool, the tool receives
Expand Down
2 changes: 1 addition & 1 deletion anda_engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "anda_engine"
description = "Agents engine for Anda -- an AI agent framework built with Rust, powered by ICP and TEEs."
repository = "https://github.com/ldclabs/anda/tree/main/anda_engine"
publish = true
version = "0.4.3"
version = "0.4.4"
edition.workspace = true
keywords.workspace = true
categories.workspace = true
Expand Down
23 changes: 13 additions & 10 deletions anda_engine/src/context/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ impl AgentCtx {
pub(crate) fn child_with(
&self,
agent_name: &str,
caller: Principal,
user: Option<String>,
caller: Option<Principal>,
) -> Result<Self, BoxError> {
Ok(Self {
base: self
.base
.child_with(format!("A:{}", agent_name), user, caller)?,
.child_with(format!("A:{}", agent_name), caller, user)?,
model: self.model.clone(),
tools: self.tools.clone(),
agents: self.agents.clone(),
Expand All @@ -122,16 +122,17 @@ impl AgentCtx {
///
/// # Arguments
/// * `tool_name` - Name of the tool
/// * `user` - Optional user identifier
/// * `caller` - Optional caller principal
/// * `user` - Optional user identifier
///
pub(crate) fn child_base_with(
&self,
tool_name: &str,
caller: Principal,
user: Option<String>,
caller: Option<Principal>,
) -> Result<BaseCtx, BoxError> {
self.base
.child_with(format!("T:{}", tool_name), user, caller)
.child_with(format!("T:{}", tool_name), caller, user)
}
}

Expand Down Expand Up @@ -355,19 +356,21 @@ impl EmbeddingFeatures for AgentCtx {
impl BaseContext for AgentCtx {}

impl StateFeatures for AgentCtx {
/// agent ID
fn id(&self) -> Principal {
self.base.id()
}

/// caller ID
fn caller(&self) -> Principal {
self.base.caller()
}

/// Gets the current user identifier, if available
fn user(&self) -> Option<String> {
self.base.user()
}

/// Gets the current caller principal, if available
fn caller(&self) -> Option<Principal> {
self.base.caller()
}

/// Gets the cancellation token for the current context
fn cancellation_token(&self) -> CancellationToken {
self.base.cancellation_token()
Expand Down
20 changes: 11 additions & 9 deletions anda_engine/src/context/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ use std::{
const CONTEXT_MAX_DEPTH: u8 = 42;
const CACHE_MAX_CAPACITY: u64 = 1000000;

pub const ANONYMOUS: Principal = Principal::anonymous();

use super::{
cache::CacheService,
web3::{Web3Client, Web3SDK},
Expand All @@ -52,8 +54,8 @@ use crate::store::Store;
#[derive(Clone)]
pub struct BaseCtx {
pub(crate) id: Principal,
pub(crate) caller: Principal,
pub(crate) user: Option<String>,
pub(crate) caller: Option<Principal>,
pub(crate) path: Path,
pub(crate) cancellation_token: CancellationToken,
pub(crate) start_at: Instant,
Expand Down Expand Up @@ -93,7 +95,7 @@ impl BaseCtx {
Self {
id,
user: None,
caller: None,
caller: ANONYMOUS,
path: Path::default(),
cancellation_token,
start_at: Instant::now(),
Expand Down Expand Up @@ -145,22 +147,22 @@ impl BaseCtx {
///
/// # Arguments
/// * `path` - New path for the child context
/// * `caller` - caller principal (or ANONYMOUS)
/// * `user` - Optional user identifier
/// * `caller` - Optional caller principal
///
/// # Errors
/// Returns an error if the context depth exceeds CONTEXT_MAX_DEPTH
pub(crate) fn child_with(
&self,
path: String,
caller: Principal,
user: Option<String>,
caller: Option<Principal>,
) -> Result<Self, BoxError> {
let path = Path::parse(path)?;
let child = Self {
id: self.id,
user,
caller,
user,
path,
cancellation_token: self.cancellation_token.child_token(),
start_at: Instant::now(),
Expand All @@ -184,12 +186,12 @@ impl StateFeatures for BaseCtx {
self.id
}

fn user(&self) -> Option<String> {
self.user.clone()
fn caller(&self) -> Principal {
self.caller
}

fn caller(&self) -> Option<Principal> {
self.caller
fn user(&self) -> Option<String> {
self.user.clone()
}

fn cancellation_token(&self) -> CancellationToken {
Expand Down
13 changes: 7 additions & 6 deletions anda_engine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ impl Engine {
pub fn ctx_with<A>(
&self,
agent: &A,
caller: Principal,
user: Option<String>,
caller: Option<Principal>,
) -> Result<AgentCtx, BoxError>
where
A: Agent<AgentCtx>,
Expand All @@ -132,11 +132,12 @@ impl Engine {
if !self.ctx.agents.contains(&name) {
return Err(format!("agent {} not found", name).into());
}

if let Some(user) = &user {
validate_path_part(user)?;
}

self.ctx.child_with(&name, user, caller)
self.ctx.child_with(&name, caller, user)
}

/// Executes an agent with the specified parameters.
Expand All @@ -147,8 +148,8 @@ impl Engine {
agent_name: Option<String>,
prompt: String,
attachment: Option<ByteBuf>,
caller: Principal,
user: Option<String>,
caller: Option<Principal>,
) -> Result<AgentOutput, BoxError> {
let name = agent_name.unwrap_or(self.default_agent.clone());
if !self.ctx.agents.contains(&name) {
Expand All @@ -159,7 +160,7 @@ impl Engine {
validate_path_part(user)?;
}

let ctx = self.ctx.child_with(&name, user, caller)?;
let ctx = self.ctx.child_with(&name, caller, user)?;
let mut res = self
.ctx
.agents
Expand All @@ -175,8 +176,8 @@ impl Engine {
&self,
name: String,
args: String,
caller: Principal,
user: Option<String>,
caller: Option<Principal>,
) -> Result<(String, bool), BoxError> {
if !self.ctx.tools.contains(&name) {
return Err(format!("tool {} not found", name).into());
Expand All @@ -186,7 +187,7 @@ impl Engine {
validate_path_part(user)?;
}

let ctx = self.ctx.child_base_with(&name, user, caller)?;
let ctx = self.ctx.child_base_with(&name, caller, user)?;
self.ctx.tools.call(&name, ctx, args).await
}

Expand Down
2 changes: 1 addition & 1 deletion anda_engine_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "anda_engine_cli"
description = "The command line interface for Anda engine server."
repository = "https://github.com/ldclabs/anda/tree/main/anda_engine_cli"
publish = false # can't publish this crate because `anda_web3_client` is not published
version = "0.1.1"
version = "0.1.2"
edition.workspace = true
keywords.workspace = true
categories.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion anda_engine_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "anda_engine_server"
description = "A http server to serve multiple Anda engines."
repository = "https://github.com/ldclabs/anda/tree/main/anda_engine_server"
publish = false
version = "0.1.0"
version = "0.1.2"
edition.workspace = true
keywords.workspace = true
categories.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions anda_engine_server/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ async fn engine_run(
from_reader(req.params.as_slice())
.map_err(|err| format!("failed to decode params: {err:?}"))?;
let res = engine
.agent_run(args.0, args.1, args.2, None, Some(caller))
.agent_run(args.0, args.1, args.2, caller, None)
.await
.map_err(|err| format!("failed to run agent: {err:?}"))?;
Ok(to_cbor_bytes(&res).into())
Expand All @@ -112,7 +112,7 @@ async fn engine_run(
let args: (String, String) = from_reader(req.params.as_slice())
.map_err(|err| format!("failed to decode params: {err:?}"))?;
let res = engine
.tool_call(args.0, args.1, None, Some(caller))
.tool_call(args.0, args.1, caller, None)
.await
.map_err(|err| format!("failed to call tool: {err:?}"))?;
Ok(to_cbor_bytes(&res).into())
Expand Down
Loading

0 comments on commit c995056

Please sign in to comment.