Skip to content

Commit

Permalink
fix: fix agent context path
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Feb 20, 2025
1 parent d9d11d9 commit 24be033
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
16 changes: 10 additions & 6 deletions agents/anda_bot/src/twitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ impl TwitterDaemon {

pub async fn run(&self, cancel_token: CancellationToken) -> Result<(), BoxError> {
{
let ctx = self.engine.ctx_with(self.agent.as_ref(), ANONYMOUS, None)?;
let ctx = self
.engine
.ctx_with(&self.agent.as_ref().name(), ANONYMOUS, None)?;
// load seen_tweet_ids from store
ctx.cache_store_init("seen_tweet_ids", async { Ok(Vec::<String>::new()) })
.await?;
Expand Down Expand Up @@ -142,7 +144,7 @@ impl TwitterDaemon {

log::info!("post new tweet with {} knowledges", knowledges.len());
let ctx = self.engine.ctx_with(
self.agent.as_ref(),
&self.agent.as_ref().name(),
ANONYMOUS,
Some(self.agent.character.username.clone()),
)?;
Expand Down Expand Up @@ -175,7 +177,7 @@ impl TwitterDaemon {

async fn handle_home_timeline(&self) -> Result<(), BoxError> {
let ctx = self.engine.ctx_with(
self.agent.as_ref(),
&self.agent.as_ref().name(),
ANONYMOUS,
Some(self.agent.character.username.clone()),
)?;
Expand Down Expand Up @@ -265,9 +267,11 @@ impl TwitterDaemon {
// not replying to bot itself
return Ok(());
}
let ctx = self
.engine
.ctx_with(self.agent.as_ref(), ANONYMOUS, Some(tweet_user.clone()))?;
let ctx = self.engine.ctx_with(
&self.agent.as_ref().name(),
ANONYMOUS,
Some(tweet_user.clone()),
)?;
let mut seen_tweet_ids: Vec<String> = ctx.cache_store_get("seen_tweet_ids").await?;

if seen_tweet_ids.contains(&tweet_id) {
Expand Down
2 changes: 1 addition & 1 deletion anda_core/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ where
C: AgentContext + Send + Sync,
{
/// Returns the agent's name as a String
/// The unique name of the agent. This name should be valid Path string and unique within the engine in lowercase.
/// The unique name of the agent. This name should be valid Path string ([`validate_path_part`]) and unique within the engine in lowercase.
fn name(&self) -> String;

/// Returns the agent's capabilities description in a short string
Expand Down
24 changes: 7 additions & 17 deletions anda_engine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,15 @@ impl Engine {
self.ctx.base.cancellation_token.child_token()
}

/// Creates a new [`AgentCtx`] with the specified agent, user, and caller.
/// Creates a new [`AgentCtx`] with the specified agent name, user, and caller.
/// Returns an error if the agent is not found or if the user name is invalid.
pub fn ctx_with<A>(
pub fn ctx_with(
&self,
agent: &A,
agent_name: &str,
caller: Principal,
user: Option<String>,
) -> Result<AgentCtx, BoxError>
where
A: Agent<AgentCtx>,
{
let name = agent.name().to_ascii_lowercase();
) -> Result<AgentCtx, BoxError> {
let name = agent_name.to_ascii_lowercase();
if !self.ctx.agents.contains(&name) {
return Err(format!("agent {} not found", name).into());
}
Expand All @@ -151,16 +148,9 @@ impl Engine {
caller: Principal,
user: Option<String>,
) -> Result<AgentOutput, BoxError> {
let name = agent_name.unwrap_or(self.default_agent.clone());
if !self.ctx.agents.contains(&name) {
return Err(format!("agent {} not found", name).into());
}

if let Some(user) = &user {
validate_path_part(user)?;
}
let name = agent_name.unwrap_or_else(|| self.default_agent.clone());
let ctx = self.ctx_with(&name, caller, user)?;

let ctx = self.ctx.child_with(&name, caller, user)?;
let mut res = self
.ctx
.agents
Expand Down

0 comments on commit 24be033

Please sign in to comment.