Skip to main content

Agent

Struct Agent 

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

An agent is configuration: a stable id, a model, instructions, tools and hooks. It holds no per-run state, is cheap to clone, and can run many sessions concurrently.

Implementations§

Source§

impl Agent

Source

pub fn builder(id: impl Into<String>) -> AgentBuilder

Source

pub fn id(&self) -> &str

Source

pub fn tool_specs(&self) -> &[ToolSpec]

Source

pub fn limits(&self) -> &Limits

Source

pub fn redaction_scope(&self) -> &RedactionScope

The registered secrets this agent’s runs redact (AgentBuilder::redaction_scope).

Source

pub fn with_limits(&self, limits: Limits) -> Agent

A copy of this agent with different limits (same id, tools, hooks). Used to give a child agent its own budget.

Source

pub fn with_hook(&self, hook: impl Hook + 'static) -> Agent

A copy of this agent with one more hook, run after the existing ones.

Source

pub async fn run( &self, session_id: &str, input: impl Into<Message>, ) -> Result<RunOutcome, CoreError>

Start a new run in session_id with one user message: text, or a crate::Message built with Message::user(..).with_images(..).

Source

pub async fn run_with_cancel( &self, session_id: &str, input: impl Into<Message>, cancel: CancellationToken, ) -> Result<RunOutcome, CoreError>

Source

pub async fn run_from( &self, state: RunState, cancel: CancellationToken, ) -> Result<RunOutcome, CoreError>

Continue from saved state that is not interrupted, typically previous.state.with_user_message("next question").

Source

pub async fn run_resolving( &self, state: RunState, cancel: CancellationToken, resolver: &dyn InterruptResolver, ) -> Result<RunOutcome, CoreError>

Like Agent::run_from, but settles each interruption with resolver and continues, until the run ends or the resolver returns None (the interrupted outcome is then returned as is). It has no iteration cap of its own: a resolver that keeps resolving is bounded by the run’s limits (turns, tool calls, tokens).

Hooks see one run end (Hook::on_run_end), for the final outcome; the interruptions settled on the way are not run ends.

Source

pub async fn end_in_flight( &self, state: RunState, ) -> Result<RunOutcome, CoreError>

End a run a crash or restart left mid tool batch without running anything more: unlike Agent::run_from, no call is re-run and no model call is made. A call that had started gets an crate::ReceiptStatus::Indeterminate receipt, one that never started a crate::ReceiptStatus::Cancelled one, and the run ends RunStatus::Cancelled. A state that is not in flight just ends.

Source

pub async fn resume_resolving( &self, state: RunState, resolution: Resolution, cancel: CancellationToken, resolver: &dyn InterruptResolver, ) -> Result<RunOutcome, CoreError>

Like Agent::resume_with_cancel, but settles each later interruption with resolver, as Agent::run_resolving does.

Source

pub async fn resume( &self, state: RunState, resolution: Resolution, ) -> Result<RunOutcome, CoreError>

Settle the interruption in state and keep going. state may come from another process: it is plain data. A run paused for a child (crate::Pending::child) passes the answer down to the run that is waiting, at any depth; RunState::waiting_run says which that is.

Source

pub async fn resume_with_cancel( &self, state: RunState, resolution: Resolution, cancel: CancellationToken, ) -> Result<RunOutcome, CoreError>

Trait Implementations§

Source§

impl AgentExt for Agent

Source§

fn as_tool( &self, name: impl Into<String>, description: impl Into<String>, ) -> AgentTool

Source§

impl AgentStructuredExt for Agent

Source§

async fn run_typed<T>( &self, session_id: &str, input: impl Into<Message> + Send, ) -> Result<TypedOutcome<T>, StructuredError>

Agent::run, with the final answer read as T (one repair attempt). Tools still run as usual; only the final answer must match.
Source§

async fn run_typed_from<T>( &self, state: RunState, cancel: CancellationToken, ) -> Result<TypedOutcome<T>, StructuredError>

Agent::run_from (a saved, not interrupted state), typed.
Source§

async fn resume_typed<T>( &self, state: RunState, resolution: Resolution, ) -> Result<TypedOutcome<T>, StructuredError>

Agent::resume for a run started with AgentStructuredExt::run_typed, typed. May end StructuredError::NotFinished again at the next interruption.
Source§

async fn finish_typed<T>( &self, outcome: RunOutcome, ) -> Result<TypedOutcome<T>, StructuredError>

Read the final answer of an outcome you already have (for example after resuming an interrupted run) as T, with one repair attempt.
Source§

impl Clone for Agent

Source§

fn clone(&self) -> Agent

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Agent

§

impl !UnwindSafe for Agent

§

impl Forget for Agent

§

impl Freeze for Agent
where *const ArcInner<AgentInner> is !null: Freeze, PhantomData<ArcInner<AgentInner>>: Freeze,

§

impl Move for Agent

§

impl Send for Agent
where Arc<AgentInner>: Send,

§

impl Sync for Agent
where Arc<AgentInner>: Sync,

§

impl Unpin for Agent
where Arc<AgentInner>: Unpin,

§

impl UnsafeUnpin for Agent
where *const ArcInner<AgentInner> is !null: UnsafeUnpin, PhantomData<ArcInner<AgentInner>>: UnsafeUnpin,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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