Skip to main content

AgentSession

Struct AgentSession 

Source
pub struct AgentSession {
Show 15 fields pub cwd: PathBuf, pub messages: Vec<Message>, pub system_prompt: String, pub model: Model, pub thinking: ThinkingLevel, pub tools: Vec<Arc<dyn AgentTool>>, pub client: Client, pub gate: Arc<dyn ToolGate>, pub api_key: Option<String>, pub auth_token: Option<String>, pub session_file: Option<PathBuf>, pub total_usage: Usage, pub max_tokens: Option<usize>, pub cache_control: bool, pub compact_model: Option<Model>,
}

Fields§

§cwd: PathBuf§messages: Vec<Message>§system_prompt: String§model: Model§thinking: ThinkingLevel§tools: Vec<Arc<dyn AgentTool>>§client: Client§gate: Arc<dyn ToolGate>§api_key: Option<String>§auth_token: Option<String>§session_file: Option<PathBuf>§total_usage: Usage§max_tokens: Option<usize>

Override the model’s default output-token cap, if set.

§cache_control: bool

Attach Anthropic prompt-caching markers. Default true (--no-cache flips it).

§compact_model: Option<Model>

Override the model used for background compaction summaries. None (default) resolves via Self::default_compact_model — the cheapest tier (haiku), or PIXIE_PI_COMPACT_MODEL. Pinned per-session in tests.

Implementations§

Source§

impl AgentSession

Source

pub fn new( cwd: PathBuf, system_prompt: String, model: Model, thinking: ThinkingLevel, tools: Vec<Arc<dyn AgentTool>>, client: Client, ) -> Self

Construct a session from its core parts, with sensible defaults for token caps, caching, the allow-all gate, and an empty transcript.

Source

pub fn stream_options(&self) -> SimpleStreamOptions

Build the streaming options for the current model + thinking level.

Source

pub fn run( &self, prompts: Vec<Message>, cancel: CancellationToken, ) -> AgentEventStream

Start an agent run for the given prompt messages.

Source

pub fn estimated_tokens(&self) -> usize

Estimated token count of the current transcript.

Source

pub fn context_usage(&self) -> f64

Token-budget fraction used (0.0–1.0+).

Source

pub fn should_compact(&self) -> bool

True when the transcript exceeds the compaction budget.

Source

pub async fn compact(&mut self) -> usize

Compaction: summarize the oldest messages (cut at a user boundary) and replace them with the summary, keeping the recent tail. Falls back to a plain “messages dropped” note if the model is unavailable or fails, so compaction always reduces the transcript and never hard-fails. Returns the number of messages dropped.

Source

pub fn save(&self) -> Result<()>

Persist the transcript to the session JSONL file (if any).

Source

pub fn load(path: &Path) -> Result<Vec<Message>>

Load a transcript from a JSONL session file.

Source

pub fn add_usage(&mut self, usage: &Usage)

Accumulate usage from a turn into the session total.

Source

pub fn tool_names(&self) -> Vec<String>

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> 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> 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, 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