Skip to main content

AppServer

Struct AppServer 

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

A running codex app-server.

Requests go through AppServerHandle, which this type dereferences to, so server.turn_start(..) works directly. handle gives a clone that can be moved to another task. Events are read with next_message, which needs &mut self.

Dropping the value kills the server’s process group without waiting; use shutdown or terminate to stop it and wait.

Implementations§

Source§

impl AppServer

Source

pub fn builder(codex: &Codex) -> AppServerBuilder<'_>

Start configuring a server for codex.

Source

pub fn server_info(&self) -> &ServerInfo

What the server reported about itself during the handshake.

Source

pub fn pid(&self) -> Option<u32>

The server process’s id, while it is running.

Source

pub fn handle(&self) -> AppServerHandle

A cloneable handle for sending requests from other tasks.

Source

pub async fn next_message(&mut self) -> Result<Option<ServerMessage>>

The next notification or server request, in the order the server sent them.

Returns Ok(None) once the server’s output has ended, after every earlier message has been returned. That is how a server that exited on its own looks; shutdown then reports its exit status. Cancel-safe: dropping the future loses no message.

§Errors

Error::OutputLimitExceeded or Error::AppServerProtocol when the stream became unusable. The server has been killed by then. The error is returned once; later calls return Ok(None).

Source

pub async fn shutdown(self) -> Result<()>

End the session and wait for the server to exit.

Closes the server’s stdin and waits up to the client’s termination grace for it to exit on its own. If it does not, this falls back to terminate and returns Ok, since the caller asked for the end.

§Errors

If the server exits with a non-zero status by itself, the failure is classified from its stderr like any failed command (Error::CommandFailed and its refinements).

Source

pub async fn terminate(self) -> Result<()>

Stop the server and its process group, and wait until it is gone.

Sends the client’s configured termination signal to the group, waits through the client’s termination grace, kills what remains, and reaps the child. Requests still waiting fail.

Shell commands the server started run in groups of their own and are not reached by group signaling. Cooperative cleanup depends on the server; see the module documentation. shutdown is the way to stop those.

§Errors

Error::Io if the child cannot be waited on.

Methods from Deref<Target = AppServerHandle>§

Source

pub async fn request(&self, method: &str, params: Value) -> Result<Value>

Send a request and wait for its answer, returning the result value.

Requests may run concurrently; each answer is matched by id. A JSON-RPC error from the server is Error::AppServerRpc. The client’s timeout, when set, bounds the wait and yields Error::Timeout. Pass Value::Null for no parameters.

§Errors

Fails if the server rejects the request, the wait times out, or the session ends first.

Source

pub fn notify(&self, method: &str, params: Value) -> Result<()>

Send a notification, which has no answer. Returns once it is queued for writing.

§Errors

Fails if the session has ended.

Source

pub fn respond(&self, id: RequestId, result: Value) -> Result<()>

Answer a ServerRequest with a result. Returns once it is queued for writing.

§Errors

Fails if the session has ended.

Source

pub fn respond_error( &self, id: RequestId, code: i64, message: impl Into<String>, ) -> Result<()>

Answer a ServerRequest with a JSON-RPC error. Use it for a request the caller does not handle, so the turn that raised it is not left waiting. Returns once it is queued for writing.

§Errors

Fails if the session has ended.

Source

pub async fn thread_start(&self, params: ThreadStartParams) -> Result<Thread>

Start a thread (thread/start).

§Errors

As request, and a protocol error if the response has no thread id.

Source

pub async fn turn_start(&self, params: TurnStartParams) -> Result<Turn>

Start a turn (turn/start). The turn runs in the background: watch for its turn/completed notification.

§Errors

As request, and a protocol error if the response has no turn.

Source

pub async fn turn_steer( &self, thread_id: &str, expected_turn_id: &str, input: Vec<UserInput>, ) -> Result<String>

Add input to a running turn (turn/steer) and return the id of the turn it landed on.

The server delivers it to the model at the next item boundary, so a long tool call delays it. expected_turn_id must be the active turn. With no active turn the server fails the request with no active turn to steer, and with a different one with expected active turn id ...; both are Error::AppServerRpc.

§Errors

As request.

Source

pub async fn turn_interrupt(&self, thread_id: &str, turn_id: &str) -> Result<()>

Stop a running turn (turn/interrupt). The turn then ends with a turn/completed notification whose status is TurnStatus::Interrupted.

With no active turn the server fails the request with no active turn to interrupt, and with a different one with expected active turn id ...; both are Error::AppServerRpc.

§Errors

As request.

Trait Implementations§

Source§

impl Debug for AppServer

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Deref for AppServer

Source§

type Target = AppServerHandle

The resulting type after dereferencing.
Source§

fn deref(&self) -> &AppServerHandle

Dereferences the value.
Source§

impl Drop for AppServer

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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