pub struct AgentSession {
pub manager: Mutex<SessionManager>,
pub registry: Arc<Registry>,
/* private fields */
}Fields§
§manager: Mutex<SessionManager>Lock order: take manager before phase, never the other way; take
context_file before manager.
registry: Arc<Registry>Implementations§
Source§impl AgentSession
impl AgentSession
pub fn new( manager: SessionManager, tools: Vec<DynTool>, registry: impl Into<Arc<Registry>>, settings: Settings, system_prompt: String, model: Model, thinking: ThinkingLevel, api_key_override: Option<(String, String)>, sink: SessionEventSink, ) -> Arc<Self> ⓘ
pub fn new_with_subagents_allowed( manager: SessionManager, tools: Vec<DynTool>, registry: impl Into<Arc<Registry>>, settings: Settings, system_prompt: String, model: Model, thinking: ThinkingLevel, api_key_override: Option<(String, String)>, sink: SessionEventSink, subagents_allowed: bool, ) -> Arc<Self> ⓘ
Sourcepub fn set_stream_fn(&self, stream_fn: Option<StreamFn>)
pub fn set_stream_fn(&self, stream_fn: Option<StreamFn>)
Replace the provider streaming function used by every later request.
Pass None to restore the real provider. Child sessions created after
this call inherit the override so a scripted fake model also serves
subagents and compaction.
pub fn model(&self) -> Model
Sourcepub fn advisor(&self) -> Option<(Model, ThinkingLevel)>
pub fn advisor(&self) -> Option<(Model, ThinkingLevel)>
The model and thinking level the advisor tool asks, if one is set.
Sourcepub fn set_advisor(&self, advisor: Option<(Model, ThinkingLevel)>)
pub fn set_advisor(&self, advisor: Option<(Model, ThinkingLevel)>)
Set or clear the advisor for the next request. A child session or a
session without tools has no advisor, so this does nothing there.
Saving the choice is the caller’s job (Settings::advisor_model).
Sourcepub fn resolve_advisor(
registry: &Registry,
pattern: &str,
) -> Option<(Model, ThinkingLevel)>
pub fn resolve_advisor( registry: &Registry, pattern: &str, ) -> Option<(Model, ThinkingLevel)>
The advisor for a pattern such as anthropic/claude-opus-5-5 or
openai/gpt-5.2:xhigh. Without a thinking suffix the advisor thinks
at high, or the nearest level the model supports.
Sourcepub fn advisor_max_uses(&self) -> u32
pub fn advisor_max_uses(&self) -> u32
Advisor calls allowed for each user prompt.
pub fn thinking_level(&self) -> ThinkingLevel
pub fn fast_mode(&self) -> bool
pub fn set_fast_mode(&self, enabled: bool)
pub fn totals(&self) -> Usage
pub fn run_phase(&self) -> RunPhase
pub fn is_running(&self) -> bool
pub fn settings(&self) -> Settings
pub fn update_settings(&self, settings: Settings)
Sourcepub fn reload_runtime(
&self,
settings: Settings,
system_prompt: String,
tools: Vec<DynTool>,
)
pub fn reload_runtime( &self, settings: Settings, system_prompt: String, tools: Vec<DynTool>, )
Replace resources used by the next model request.
Sourcepub fn install_session_tool(&self, tool: DynTool)
pub fn install_session_tool(&self, tool: DynTool)
Add or replace one tool for this process without changing saved tool configuration.
Sourcepub fn remove_session_tool(&self, name: &str) -> bool
pub fn remove_session_tool(&self, name: &str) -> bool
Remove one process-only tool. Return true when a tool was removed.
Sourcepub fn workflows_enabled(&self) -> bool
pub fn workflows_enabled(&self) -> bool
Dynamic workflows are built on child agents, so they need subagents on as well as their own setting.
Sourcepub fn prompt_mode_for(&self, text: &str) -> PromptMode
pub fn prompt_mode_for(&self, text: &str) -> PromptMode
Select workflow mode from one submitted user prompt.
pub fn workflows(&self) -> Option<Arc<WorkflowRuntime>>
pub fn iterative_jobs(&self) -> Option<Arc<IterativeRuntime>>
Sourcepub fn set_workflow_approver(&self, approver: WorkflowApprover)
pub fn set_workflow_approver(&self, approver: WorkflowApprover)
Install the callback that asks the user to approve a run.
Sourcepub fn system_context(&self) -> (String, Vec<(String, String)>)
pub fn system_context(&self) -> (String, Vec<(String, String)>)
The system prompt and each active tool’s name and description.
pub fn available_tool_names(&self) -> Vec<String>
Sourcepub fn new_session(&self) -> Result<()>
pub fn new_session(&self) -> Result<()>
Switch the active session without appending synthetic history. Refuse replacement while a prompt or compaction is active. Start a sibling session after admission, preserving the storage mode.
Sourcepub fn replace_manager(
&self,
create: impl FnOnce(&SessionManager) -> Result<SessionManager>,
) -> Result<()>
pub fn replace_manager( &self, create: impl FnOnce(&SessionManager) -> Result<SessionManager>, ) -> Result<()>
Replace the session with the one create builds from the current
one. create runs only once the session is idle, so a refused
replacement writes no new session file.
Sourcepub fn record_user_shell(&self, session_id: &str, message: AgentMessage)
pub fn record_user_shell(&self, session_id: &str, message: AgentMessage)
Save the result of a shell command the user ran in session
session_id. It is dropped when that session was replaced meanwhile.
While a run is active it waits and is saved when the run ends, so it
never lands between a tool call and its result.
Sourcepub fn set_model(&self, model: Model)
pub fn set_model(&self, model: Model)
Switch the model. A thinking level the new model does not support moves to the nearest one it does, and that change is recorded too.
Sourcepub fn set_thinking_level(&self, level: ThinkingLevel)
pub fn set_thinking_level(&self, level: ThinkingLevel)
Set the thinking level, moved to the nearest level the current model supports.
Move the active leaf in the current session tree, with an optional summary of the branch that is no longer active.
pub fn queue_steering(&self, message: AgentMessage)
pub fn queue_follow_up(&self, message: AgentMessage)
Sourcepub fn add_for_next_turn(&self, message: AgentMessage)
pub fn add_for_next_turn(&self, message: AgentMessage)
Give the next model request a message without starting one. A busy session delivers it as a follow-up. An idle session adds it to history, so that a later prompt sees it without an extra model turn.
Sourcepub fn reclaim_queued(&self) -> Vec<AgentMessage>
pub fn reclaim_queued(&self) -> Vec<AgentMessage>
Drain both queues back to the caller (Escape restores to editor).
pub fn abort(&self)
Sourcepub async fn answer_btw(
self: &Arc<Self>,
question: &str,
cancel: CancellationToken,
) -> Result<EphemeralResponse>
pub async fn answer_btw( self: &Arc<Self>, question: &str, cancel: CancellationToken, ) -> Result<EphemeralResponse>
Answer a short side question without changing the active session.
Sourcepub async fn generate_session_title(
self: &Arc<Self>,
prompt: &str,
cancel: CancellationToken,
) -> Result<String>
pub async fn generate_session_title( self: &Arc<Self>, prompt: &str, cancel: CancellationToken, ) -> Result<String>
Generate a short session title without changing conversation history.
Sourcepub async fn generate_recap(
self: &Arc<Self>,
previous_recap: Option<&str>,
cancel: CancellationToken,
) -> Result<EphemeralResponse>
pub async fn generate_recap( self: &Arc<Self>, previous_recap: Option<&str>, cancel: CancellationToken, ) -> Result<EphemeralResponse>
Create a one-line recap without changing the active session.
Sourcepub fn prompt(
self: &Arc<Self>,
prompts: Vec<AgentMessage>,
) -> impl Future<Output = ()> + Send + 'static + use<>
pub fn prompt( self: &Arc<Self>, prompts: Vec<AgentMessage>, ) -> impl Future<Output = ()> + Send + 'static + use<>
Run one prompt to completion, including retry and auto-compaction.
The session decides during this call whether the prompt starts or joins the steering queue, before the returned future is first polled.
Sourcepub fn prompt_with_mode(
self: &Arc<Self>,
prompts: Vec<AgentMessage>,
prompt_mode: PromptMode,
) -> impl Future<Output = ()> + Send + 'static + use<>
pub fn prompt_with_mode( self: &Arc<Self>, prompts: Vec<AgentMessage>, prompt_mode: PromptMode, ) -> impl Future<Output = ()> + Send + 'static + use<>
Run one prompt with tools and instructions selected for this turn.
Sourcepub fn try_start_prompt(
self: &Arc<Self>,
message: AgentMessage,
prompt_mode: PromptMode,
) -> Option<BoxFuture<'static, ()>>
pub fn try_start_prompt( self: &Arc<Self>, message: AgentMessage, prompt_mode: PromptMode, ) -> Option<BoxFuture<'static, ()>>
Start a run, or return None when the session is busy.
Sourcepub fn submit(
self: &Arc<Self>,
prompts: Vec<AgentMessage>,
prompt_mode: PromptMode,
kind: QueueKind,
) -> Option<BoxFuture<'static, ()>>
pub fn submit( self: &Arc<Self>, prompts: Vec<AgentMessage>, prompt_mode: PromptMode, kind: QueueKind, ) -> Option<BoxFuture<'static, ()>>
Start a run, or queue the prompts when the session is busy. The caller must drive a returned future to run them.
Sourcepub fn compact(
self: &Arc<Self>,
custom_instructions: Option<String>,
) -> impl Future<Output = Result<(), String>> + Send + 'static + use<>
pub fn compact( self: &Arc<Self>, custom_instructions: Option<String>, ) -> impl Future<Output = Result<(), String>> + Send + 'static + use<>
Compact on request. The session refuses during this call when a prompt or another compaction is active. Prompts that arrive during compaction run when it ends.
Sourcepub fn context_usage(&self) -> (u64, u64)
pub fn context_usage(&self) -> (u64, u64)
Current estimated context tokens and window fraction.
Auto Trait Implementations§
impl !Freeze for AgentSession
impl !RefUnwindSafe for AgentSession
impl !UnwindSafe for AgentSession
impl Forget for AgentSession
impl Move for AgentSession
impl Send for AgentSession
impl Sync for AgentSession
impl Unpin for AgentSession
impl UnsafeUnpin for AgentSession
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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