Skip to main content

Agent

Struct Agent 

Source
pub struct Agent { /* private fields */ }
Expand description

The Agent orchestrates conversations between users, LLMs, and tools

Implementations§

Source§

impl Agent

Source

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

Source

pub fn with_session( provider: Arc<dyn Provider>, config: AgentConfig, prompt_store: Arc<PromptStore>, session: Session, ) -> Self

Create an agent with an existing session

Source

pub fn tools_mut(&mut self) -> &mut ToolRegistry

Get a reference to the tool registry for registration

Source

pub fn tools(&self) -> &ToolRegistry

Get a reference to the tool registry

Source

pub fn session(&self) -> &Session

Get a reference to the current session

Source

pub fn session_mut(&mut self) -> &mut Session

Get a mutable reference to the session

Source

pub fn event_bus(&self) -> &EventBus

Get the event bus for subscribing to events

Source

pub fn config(&self) -> &AgentConfig

Get the configuration

Source

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:

  1. Adding the user message to the session
  2. Calling the LLM for a response
  3. Executing any tool calls
  4. Repeating until the LLM stops or max steps is reached
Source

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),
}
Source

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

Source

pub async fn continue_turn_with_cancellation( &mut self, cancel_token: Option<CancellationToken>, ) -> Result<TurnResult, AgentRunError>

Continue the conversation with cancellation support

Auto Trait Implementations§

§

impl !RefUnwindSafe for Agent

§

impl !UnwindSafe for Agent

§

impl Freeze for Agent

§

impl Send for Agent

§

impl Sync for Agent

§

impl Unpin for Agent

§

impl UnsafeUnpin for Agent

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more