Skip to content

Commit

Permalink
new: implemented --sleep argument to introduce a delay between each a…
Browse files Browse the repository at this point in the history
…gent step
  • Loading branch information
evilsocket committed Feb 6, 2025
1 parent ea60d2a commit dfa7584
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/agent/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub enum EventType {
StateUpdate(StateUpdate),
EmptyResponse,
Thinking(String),
Sleeping(usize),
InvalidResponse(String),
InvalidAction {
invocation: Invocation,
Expand Down
4 changes: 4 additions & 0 deletions src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@ impl Agent {
self.events_chan.send(event).map_err(|e| anyhow!(e))
}

pub fn on_event_type(&self, event_type: EventType) -> Result<()> {
self.on_event(Event::new(event_type))
}

pub async fn step(&mut self) -> Result<()> {
let options = self.prepare_step().await?;

Expand Down
3 changes: 3 additions & 0 deletions src/cli/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ pub struct Args {
/// Maximum number of steps to complete the task or 0 for no limit.
#[arg(long, default_value_t = 0)]
pub max_iterations: usize,
/// If set, the agent will sleep for the given number of seconds between each step.
#[arg(long)]
pub sleep: Option<usize>,
/// Record every event of the session to a JSONL file.
#[arg(long)]
pub record_to: Option<String>,
Expand Down
3 changes: 3 additions & 0 deletions src/cli/ui/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ pub async fn consume_events(mut events_rx: Receiver, args: Args, is_workflow: bo

match event.event {
EventType::TaskStarted(_task) => {}
EventType::Sleeping(seconds) => {
log::info!("💤 sleeping for {} seconds ...", seconds);
}
EventType::MetricsUpdate(metrics) => {
if !is_workflow {
println!("{}", metrics.to_string().dimmed());
Expand Down
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod cli;
use std::{collections::HashMap, path::PathBuf};

use agent::{
events::{Event, EventType},
events::EventType,
task::variables::{define_variable, interpolate_variables},
workflow::Workflow,
};
Expand All @@ -34,7 +34,7 @@ async fn run_task(args: Args, for_workflow: bool) -> Result<HashMap<String, Stri
));

// signal the task start
agent.on_event(Event::new(EventType::TaskStarted(tasklet)))?;
agent.on_event_type(EventType::TaskStarted(tasklet))?;

// keep going until the task is complete or a fatal error is reached
while !agent.is_done().await {
Expand All @@ -43,6 +43,13 @@ async fn run_task(args: Args, for_workflow: bool) -> Result<HashMap<String, Stri
log::error!("{}", error.to_string());
return Err(error);
}

if let Some(sleep_seconds) = args.sleep {
// signal the agent is sleeping
agent.on_event_type(EventType::Sleeping(sleep_seconds))?;
// sleep for the given number of seconds
tokio::time::sleep(std::time::Duration::from_secs(sleep_seconds as u64)).await;
}
}

agent.on_end().await?;
Expand Down

0 comments on commit dfa7584

Please sign in to comment.