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
impl Session
Sourcepub fn new(codex: Arc<Codex>) -> Self
pub fn new(codex: Arc<Codex>) -> Self
Create a new session with no prior state.
The first call to send will use ExecCommand.
Sourcepub fn resume(codex: Arc<Codex>, thread_id: impl Into<String>) -> Self
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.
Sourcepub async fn send(
&mut self,
prompt: impl Into<String>,
) -> Result<Vec<JsonLineEvent>>
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.
Sourcepub async fn execute(&mut self, cmd: ExecCommand) -> Result<Vec<JsonLineEvent>>
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.
Sourcepub async fn execute_resume(
&mut self,
cmd: ExecResumeCommand,
) -> Result<Vec<JsonLineEvent>>
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.
Sourcepub fn id(&self) -> Option<&str>
pub fn id(&self) -> Option<&str>
Returns the thread_id captured from the most recent turn, if any.
Sourcepub fn total_turns(&self) -> usize
pub fn total_turns(&self) -> usize
Total number of completed turns in this session.
Sourcepub fn history(&self) -> &[TurnRecord]
pub fn history(&self) -> &[TurnRecord]
Borrow the full turn history.