pub struct Agent { /* private fields */ }Expand description
The Agent orchestrates conversations between users, LLMs, and tools
Implementations§
Source§impl Agent
impl Agent
Sourcepub fn new(
provider: Arc<dyn Provider>,
config: AgentConfig,
prompt_store: Arc<PromptStore>,
) -> Self
pub fn new( provider: Arc<dyn Provider>, config: AgentConfig, prompt_store: Arc<PromptStore>, ) -> Self
Create a new agent with the given provider, configuration, and prompt store
Sourcepub fn with_session(
provider: Arc<dyn Provider>,
config: AgentConfig,
prompt_store: Arc<PromptStore>,
session: Session,
) -> Self
pub fn with_session( provider: Arc<dyn Provider>, config: AgentConfig, prompt_store: Arc<PromptStore>, session: Session, ) -> Self
Create an agent with an existing session
Sourcepub fn tools_mut(&mut self) -> &mut ToolRegistry
pub fn tools_mut(&mut self) -> &mut ToolRegistry
Get a reference to the tool registry for registration
Sourcepub fn tools(&self) -> &ToolRegistry
pub fn tools(&self) -> &ToolRegistry
Get a reference to the tool registry
Sourcepub fn session_mut(&mut self) -> &mut Session
pub fn session_mut(&mut self) -> &mut Session
Get a mutable reference to the session
Sourcepub fn config(&self) -> &AgentConfig
pub fn config(&self) -> &AgentConfig
Get the configuration
Sourcepub async fn run(
&mut self,
user_message: impl Into<String>,
) -> Result<TurnResult, AgentRunError>
pub async fn run( &mut self, user_message: impl Into<String>, ) -> Result<TurnResult, AgentRunError>
Run a single conversation turn with the given user message
This is the main entry point for interacting with the agent. It handles:
- Adding the user message to the session
- Calling the LLM for a response
- Executing any tool calls
- Repeating until the LLM stops or max steps is reached
Sourcepub async fn run_with_cancellation(
&mut self,
user_message: impl Into<String>,
cancel_token: Option<CancellationToken>,
) -> Result<TurnResult, AgentRunError>
pub async fn run_with_cancellation( &mut self, user_message: impl Into<String>, cancel_token: Option<CancellationToken>, ) -> Result<TurnResult, AgentRunError>
Run a single conversation turn with cancellation support
Same as run() but can be cancelled via the provided CancellationToken.
When cancelled, returns AgentRunError::Cancelled.
§Example
use codive_core::{Agent, CancellationToken};
let token = CancellationToken::new();
let cancel_token = token.clone();
// Cancel after 5 seconds
tokio::spawn(async move {
tokio::time::sleep(Duration::from_secs(5)).await;
cancel_token.cancel();
});
match agent.run_with_cancellation("Hello", Some(token)).await {
Ok(result) => println!("Got response: {}", result.text),
Err(AgentRunError::Cancelled) => println!("Operation was cancelled"),
Err(e) => eprintln!("Error: {}", e),
}Sourcepub async fn continue_turn(&mut self) -> Result<TurnResult, AgentRunError>
pub async fn continue_turn(&mut self) -> Result<TurnResult, AgentRunError>
Continue the conversation without adding a new user message
Useful after adding tool results or for multi-turn interactions
Sourcepub async fn continue_turn_with_cancellation(
&mut self,
cancel_token: Option<CancellationToken>,
) -> Result<TurnResult, AgentRunError>
pub async fn continue_turn_with_cancellation( &mut self, cancel_token: Option<CancellationToken>, ) -> Result<TurnResult, AgentRunError>
Continue the conversation with cancellation support