Skip to main content

Session

Struct Session 

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

Stateful multi-turn session manager.

Wraps a Codex client and automatically threads conversation state across turns. On the first turn, an ExecCommand is used; on subsequent turns, an ExecResumeCommand resumes the session using the thread_id extracted from the JSONL event stream.

The thread_id is preserved even when a turn fails, as long as at least one event in the output carried it.

§Example

use std::sync::Arc;
use codex_wrapper::{Codex, Session};

let codex = Arc::new(Codex::builder().build()?);
let mut session = Session::new(codex);

let events = session.send("summarize this repo").await?;
assert!(session.id().is_some());
assert_eq!(session.total_turns(), 1);

let events = session.send("now add more detail").await?;
assert_eq!(session.total_turns(), 2);

Implementations§

Source§

impl Session

Source

pub fn new(codex: Arc<Codex>) -> Self

Create a new session with no prior state.

The first call to send will use ExecCommand.

Source

pub fn resume(codex: Arc<Codex>, thread_id: impl Into<String>) -> Self

Resume an existing session by its thread_id.

The next call to send will use ExecResumeCommand with the provided ID.

Source

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

Send a prompt, automatically routing to exec or exec resume.

On the first turn (no thread_id), dispatches via ExecCommand. On subsequent turns, dispatches via ExecResumeCommand with the captured thread_id.

Returns the parsed JSONL events for this turn.

Source

pub async fn execute(&mut self, cmd: ExecCommand) -> Result<Vec<JsonLineEvent>>

Execute an ExecCommand with full control over its options.

Use this when you need to configure model, sandbox, approval policy, or other flags beyond what send provides. The session still captures the thread_id from the output.

Source

pub async fn execute_resume( &mut self, cmd: ExecResumeCommand, ) -> Result<Vec<JsonLineEvent>>

Execute an ExecResumeCommand with full control over its options.

Use this when you need to configure flags on the resume command beyond what send provides. The session still captures the thread_id from the output.

Source

pub fn id(&self) -> Option<&str>

Returns the thread_id captured from the most recent turn, if any.

Source

pub fn total_turns(&self) -> usize

Total number of completed turns in this session.

Source

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

Borrow the full turn history.

Trait Implementations§

Source§

impl Debug for Session

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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> 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, 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