Skip to main content

Agent

Struct Agent 

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

A stateful agent: configuration, a model transport, a tool set, and the running conversation. Drive it with Agent::send.

Implementations§

Source§

impl Agent

Source

pub fn new(config: Config) -> Result<Self>

Build an agent backed by an OpenAI-compatible endpoint (OpenRouter by default). The API key is taken from Config::api_key or the configured environment variable.

Source

pub fn with_provider(config: Config, provider: Box<dyn Provider>) -> Self

Build an agent with an explicit provider and the built-in tools. Handy for tests (inject a mock provider) or custom transports.

Source

pub fn with_parts( config: Config, provider: Box<dyn Provider>, registry: ToolRegistry, ) -> Self

Build an agent from all three parts.

Source

pub fn provider_arc(&self) -> Arc<dyn Provider>

A handle to this agent’s model transport, for sharing with subagents.

Source

pub async fn run_subagent( &self, system: impl Into<String>, task: impl Into<String>, ) -> Result<String>

Spawn a subagent that shares this agent’s model transport, runs task to completion with its own fresh conversation (seeded with system), and returns its final answer. The analog of Agent / spawn_agent.

Source

pub fn with_provider_arc(config: Config, provider: Arc<dyn Provider>) -> Self

Like Self::with_provider but sharing an existing transport handle.

Source

pub fn run_in_background( self, prompt: impl Into<String>, ) -> JoinHandle<(Self, Result<String>)>
where Self: Send + 'static,

Run a prompt on a background task, returning a handle that resolves to the final answer (and the agent, so the caller can continue it). The analog of background/async agent runs.

Source

pub fn resume(config: Config, session: Session) -> Result<Self>

Build an agent and seed it with a previously-recorded Session so it can continue where Claude Code or Codex left off.

Source

pub fn load_session(&mut self, session: Session)

Replace the conversation with a loaded session, keeping this agent’s own system prompt at the front. The session’s own system/developer turns are preserved after it for context.

Source

pub fn save_transcript(&self, path: impl AsRef<Path>) -> Result<()>

Persist the live conversation to path as JSONL (one ChatMessage per line) so the session can be resumed later — supercode’s own sessions become first-class, resumable artifacts.

Source

pub fn load_transcript(&mut self, path: impl AsRef<Path>) -> Result<()>

Restore a conversation previously written with Self::save_transcript, replacing the current history.

Source

pub fn checkpoint(&self) -> usize

Take a checkpoint of the current conversation position. Pass it to Self::rewind_to to discard everything sent since (the rewind/undo analog of fork/checkpoint).

Source

pub fn rewind_to(&mut self, checkpoint: usize)

Rewind the conversation to a Self::checkpoint, discarding later turns.

Source

pub async fn send_with_files( &mut self, text: impl Into<String>, files: &[PathBuf], ) -> Result<String>

Send a message with file inputs attached — the --file / -i analog. Each file’s contents are injected into the prompt: UTF-8 text inline, binary (e.g. images) noted with a size marker. (Native image vision would additionally require multimodal content parts.)

Source

pub async fn send_with_images( &mut self, text: impl Into<String>, image_urls: &[String], ) -> Result<String>

Send a message with image inputs to a vision model — the -i/--image analog. image_urls may be https://… links or data:image/…;base64,… URLs; they’re attached as multimodal image_url content parts.

Source

pub fn expand_prompt(&self, input: &str) -> String

Expand a /<name> <args> slash command against the registered prompt templates ({args} is replaced with the trailing text). Non-matching input is returned unchanged.

Source

pub fn maybe_compact(&mut self) -> bool

Compact the conversation if it has grown past the configured threshold: keep the system prompt and the most recent turns, and replace the older middle turns with a single summary marker. Returns whether it compacted.

Source

pub fn register_tool(&mut self, tool: impl Tool + 'static)

Register an additional tool (e.g. your own capability).

Source

pub fn history(&self) -> &[ChatMessage]

The current conversation, including the system prompt.

Source

pub async fn send(&mut self, user_input: impl Into<String>) -> Result<String>

Send a user message and run the loop until the model produces a final answer (text with no tool calls) or the iteration budget is exhausted.

Source

pub fn turn_count(&self) -> usize

Number of non-system messages exchanged so far.

Source

pub fn total_output_tokens(&self) -> u64

Cumulative output (completion) tokens reported by the provider across every send on this agent. Zero if the provider reports no usage.

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