Skip to main content

AgentRuntime

Struct AgentRuntime 

Source
pub struct AgentRuntime { /* private fields */ }

Implementations§

Source§

impl AgentRuntime

Source

pub async fn create_session(&self) -> SessionId

Source

pub async fn restore_session( &self, session_id: &SessionId, ) -> Option<AgentSession>

Source

pub async fn session(&self, session_id: &SessionId) -> Option<AgentSession>

Source

pub async fn session_or_err( &self, session_id: &SessionId, ) -> Result<AgentSession, AgentError>

Source

pub async fn with_session_mut<F, R>( &self, session_id: &SessionId, f: F, ) -> Result<R, AgentError>
where F: FnOnce(&mut AgentSession) -> R,

Source

pub fn emit_event(&self, event: RuntimeEvent)

Source

pub fn subscribe_runtime_events(&self) -> Receiver<RuntimeEvent>

Subscribe to runtime events from the internal broadcast channel.

Events are delivered directly from the runtime’s event bus (capacity 2048). Slow consumers may receive Lagged(n) errors if they cannot keep up — ensure the receiver loop processes events promptly or use a buffering layer in the consumer if backpressure is a concern.

Source

pub fn session_manager(&self) -> &SessionManager

Source

pub fn llm_engine(&self) -> &LlmEngine

Source

pub fn client(&self) -> Arc<dyn LlmClient>

Source

pub fn set_client(&mut self, client: Arc<dyn LlmClient>)

Replace the LLM client at runtime (e.g., model switch). Requires &mut self — obtain via runtime.lock().await.

Source

pub fn tools_mut(&self) -> Arc<RwLock<ToolRegistry>>

Source

pub fn inject_framework_deps(&self, tools: &ToolRegistry)

Inject the internal EventBus into framework tools in the given registry. Call this after replacing tools in the registry (e.g., in build_tools).

Source

pub fn config(&self) -> RwLockReadGuard<'_, AgentConfig>

Source

pub async fn set_reasoning_effort(&self, effort: ReasoningEffort)

设置 reasoning effort(异步版本)

Source

pub fn set_reasoning_effort_sync(&self, effort: ReasoningEffort)

设置 reasoning effort(同步版本,只在同步上下文中使用)

Source

pub fn approval_handler(&self) -> Option<&Arc<dyn ApprovalHandler>>

Source

pub async fn cached_approval( &self, session_id: &SessionId, action_key: &str, ) -> bool

Source

pub async fn cache_approval(&self, session_id: &SessionId, action_key: String)

Source

pub async fn save_checkpoint( &self, session_id: &SessionId, checkpoint: CheckpointData, ) -> Result<(), AgentError>

Source

pub async fn load_checkpoint( &self, _session_id: &SessionId, _checkpoint: &CheckpointData, ) -> Result<Option<CheckpointData>, AgentError>

Source

pub async fn run<F>( &self, session_id: SessionId, on_event: F, ) -> Result<RunOutcome, AgentError>

Source

pub async fn run_turn<F>( &self, session_id: SessionId, user_input: &str, on_event: F, ) -> Result<RunOutcome, AgentError>

Source

pub async fn run_turn_collect( &self, session_id: SessionId, user_input: &str, ) -> Result<(Vec<RuntimeEvent>, RunOutcome), AgentError>

Source

pub async fn add_user_message( &self, session_id: &SessionId, text: impl Into<String>, ) -> Result<(), AgentError>

Source

pub async fn add_system_message( &self, session_id: &SessionId, text: impl Into<String>, ) -> Result<(), AgentError>

Source

pub async fn add_tool_result( &self, session_id: &SessionId, tool_call_id: &str, summary: impl Into<String>, ) -> Result<(), AgentError>

Source

pub async fn get_messages( &self, session_id: &SessionId, ) -> Result<Vec<ChatMessage>, AgentError>

Source

pub async fn set_messages( &self, session_id: &SessionId, messages: Vec<ChatMessage>, ) -> Result<(), AgentError>

Replace the chat messages for a session — only for persistence restore. Validates message sequence before applying.

仅供持久化恢复使用。

Source

pub async fn validate_session( &self, session_id: &SessionId, ) -> Result<(), AgentError>

Source

pub fn session_store(&self) -> Arc<dyn SessionStore>

Source

pub fn on_turn_end<F>(&self, f: F)
where F: Fn(&TurnContext) + Send + Sync + 'static,

Register a turn-end callback. The callback receives a TurnContext with raw data about the completed turn iteration. Consumers (e.g. phi-telemetry) use this to build their own metrics without agent-base knowing anything about metrics.

Source

pub fn cancel(&self)

Cancel the currently executing run_turn / run. No-op if there is no current execution.

Source

pub fn reset_cancel(&self)

Reset the cancel token (called automatically before each run_turn)

Source

pub fn cancel_token(&self) -> CancellationToken

Get a clone of the cancel token

Source

pub fn is_cancelled(&self) -> bool

Check if cancellation has been requested

Trait Implementations§

Source§

impl Clone for AgentRuntime

Source§

fn clone(&self) -> AgentRuntime

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§

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> 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> 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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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