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. An attached connection closes only its socket; the shared server
and its turns remain the server owner’s responsibility.
Implementations§
Source§impl AppServer
impl AppServer
Sourcepub fn builder(codex: &Codex) -> AppServerBuilder<'_>
pub fn builder(codex: &Codex) -> AppServerBuilder<'_>
Start configuring a server for codex.
Sourcepub fn server_info(&self) -> &ServerInfo
pub fn server_info(&self) -> &ServerInfo
What the server reported about itself during the handshake.
Sourcepub fn transport(&self) -> &AppServerTransport
pub fn transport(&self) -> &AppServerTransport
How this client reaches the server, and whether it owns it.
Sourcepub fn pid(&self) -> Option<u32>
pub fn pid(&self) -> Option<u32>
The owned server’s process ID while running. None for an attached
connection, which neither starts nor owns a process.
Sourcepub fn handle(&self) -> AppServerHandle
pub fn handle(&self) -> AppServerHandle
A cloneable handle for sending requests from other tasks.
Sourcepub async fn next_message(&mut self) -> Result<Option<ServerMessage>>
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, Error::AppServerLimitExceeded, or
Error::AppServerProtocol when the stream became unusable. The owned
process has been killed by then. The error
is returned once; later calls return Ok(None).
Sourcepub async fn shutdown(self) -> Result<()>
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. An attached connection closes its local socket immediately;
the shared server keeps running. Pending requests fail on both paths.
§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).
Sourcepub async fn terminate(self) -> Result<()>
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>§
Sourcepub async fn request(&self, method: &str, params: Value) -> Result<Value>
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.
Sourcepub fn notify(&self, method: &str, params: Value) -> Result<()>
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.
Sourcepub fn respond(&self, id: RequestId, result: Value) -> Result<()>
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.
Sourcepub fn respond_error(
&self,
id: RequestId,
code: i64,
message: impl Into<String>,
) -> Result<()>
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.
Sourcepub async fn thread_start(&self, params: ThreadStartParams) -> Result<Thread>
pub async fn thread_start(&self, params: ThreadStartParams) -> Result<Thread>
Sourcepub async fn turn_start(&self, params: TurnStartParams) -> Result<Turn>
pub async fn turn_start(&self, params: TurnStartParams) -> Result<Turn>
Sourcepub async fn turn_steer(
&self,
thread_id: &str,
expected_turn_id: &str,
input: Vec<UserInput>,
) -> Result<String>
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.
Sourcepub async fn turn_interrupt(&self, thread_id: &str, turn_id: &str) -> Result<()>
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.