Skip to content

Commit

Permalink
Defines a mock agent in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
jsibbison-square committed Nov 24, 2024
1 parent b75919d commit d27951b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 56 deletions.
21 changes: 21 additions & 0 deletions crates/goose-cli/src/agents/agent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use anyhow::Result;
use async_trait::async_trait;
use futures::stream::BoxStream;
use goose::{agent::Agent as GooseAgent, models::message::Message, systems::System};

#[async_trait]
pub trait Agent {
fn add_system(&mut self, system: Box<dyn System>);
async fn reply(&self, messages: &[Message]) -> Result<BoxStream<'_, Result<Message>>>;
}

#[async_trait]
impl Agent for GooseAgent {
fn add_system(&mut self, system: Box<dyn System>) {
self.add_system(system);
}

async fn reply(&self, messages: &[Message]) -> Result<BoxStream<'_, Result<Message>>> {
self.reply(messages).await
}
}
19 changes: 19 additions & 0 deletions crates/goose-cli/src/agents/mock_agent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use anyhow::Result;
use async_trait::async_trait;
use futures::stream::BoxStream;
use goose::{models::message::Message, systems::System};

use crate::agents::agent::Agent;

pub struct MockAgent;

#[async_trait]
impl Agent for MockAgent {
fn add_system(&mut self, _system: Box<dyn System>) {
();
}

async fn reply(&self, _messages: &[Message]) -> Result<BoxStream<'_, Result<Message>>> {
Ok(Box::pin(futures::stream::empty()))
}
}
4 changes: 4 additions & 0 deletions crates/goose-cli/src/agents/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod agent;

#[cfg(test)]
pub mod mock_agent;
1 change: 1 addition & 0 deletions crates/goose-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod commands {
pub mod session;
pub mod version;
}
pub mod agents;
mod inputs;
mod profile;
mod prompt;
Expand Down
46 changes: 0 additions & 46 deletions crates/goose-cli/src/session/mock_provider.rs

This file was deleted.

3 changes: 0 additions & 3 deletions crates/goose-cli/src/session/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
pub mod session;
pub mod session_file;

#[cfg(test)]
pub mod mock_provider;
13 changes: 6 additions & 7 deletions crates/goose-cli/src/session/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ use anyhow::Result;
use futures::StreamExt;
use std::path::PathBuf;

use crate::agents::agent::Agent;
use crate::prompt::prompt::{InputType, Prompt};
use crate::session::session_file::{persist_messages, readable_session_file};
use crate::systems::goose_hints::GooseHintsSystem;
use goose::agent::Agent;
use goose::developer::DeveloperSystem;
use goose::models::message::{Message, MessageContent};
use goose::models::role::Role;

use super::session_file::deserialize_messages;

pub struct Session<'a> {
agent: Box<Agent>,
agent: Box<dyn Agent>,
prompt: Box<dyn Prompt + 'a>,
session_file: PathBuf,
messages: Vec<Message>,
}

impl<'a> Session<'a> {
pub fn new(agent: Box<Agent>, prompt: Box<dyn Prompt + 'a>, session_file: PathBuf) -> Self {
pub fn new(agent: Box<dyn Agent>, prompt: Box<dyn Prompt + 'a>, session_file: PathBuf) -> Self {
let messages = match readable_session_file(&session_file) {
Ok(file) => deserialize_messages(file).unwrap_or_else(|e| {
eprintln!(
Expand Down Expand Up @@ -175,18 +175,17 @@ fn raw_message(content: &str) -> Box<Message> {

#[cfg(test)]
mod tests {
use crate::agents::mock_agent::MockAgent;
use crate::prompt::prompt::{self, Input};
use crate::session::mock_provider::MockProvider;

use super::*;
use goose::{errors::AgentResult, models::tool::ToolCall, providers::base::Provider};
use goose::{errors::AgentResult, models::tool::ToolCall};
use tempfile::NamedTempFile;

// Helper function to create a test session
fn create_test_session() -> Session<'static> {
let temp_file = NamedTempFile::new().unwrap();
let provider: Box<dyn Provider> = Box::new(MockProvider::new(Vec::new()));
let agent = Box::new(Agent::new(provider));
let agent = Box::new(MockAgent {});
let prompt = Box::new(MockPrompt {});
Session::new(agent, prompt, temp_file.path().to_path_buf())
}
Expand Down

0 comments on commit d27951b

Please sign in to comment.