mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
## Why `codex-app-server` currently owns both request-processing code and transport implementation details. Splitting the transport layer into its own crate makes that boundary explicit, reduces the amount of transport-specific dependency surface carried by `codex-app-server`, and gives future transport work a narrower place to evolve. ## What changed - Added `codex-app-server-transport` and moved the existing transport tree into it, including stdio, unix socket, websocket, remote-control transport, and websocket auth. - Moved shared transport-facing message types into the new crate so both the transport implementation and `codex-app-server` use the same definitions. - Kept processor-facing connection state and outbound routing in `codex-app-server`, with the routing tests moved next to that local wrapper. - Updated workspace metadata, Bazel crate metadata, and `codex-app-server` dependencies for the new crate boundary. ## Validation - `cargo metadata --locked --no-deps` - `git diff --check` - Attempted `cargo test -p codex-app-server-transport`, `cargo test -p codex-app-server`, `just fix -p codex-app-server-transport`, and `just fix -p codex-app-server`; all were blocked before compilation by the existing `packageproxy` resolution failure for locked `rustls-webpki = 0.103.13`. - Attempted Bazel build / lockfile validation; those were blocked by external fetch failures against BuildBuddy / GitHub while resolving `v8`.
114 lines
3.9 KiB
Rust
114 lines
3.9 KiB
Rust
use super::CHANNEL_CAPACITY;
|
|
use super::ConnectionOrigin;
|
|
use super::TransportEvent;
|
|
use super::forward_incoming_message;
|
|
use super::next_connection_id;
|
|
use super::serialize_outgoing_message;
|
|
use crate::outgoing_message::QueuedOutgoingMessage;
|
|
use codex_app_server_protocol::InitializeParams;
|
|
use codex_app_server_protocol::JSONRPCMessage;
|
|
use codex_app_server_protocol::JSONRPCRequest;
|
|
use std::io::ErrorKind;
|
|
use std::io::Result as IoResult;
|
|
use tokio::io;
|
|
use tokio::io::AsyncBufReadExt;
|
|
use tokio::io::AsyncWriteExt;
|
|
use tokio::io::BufReader;
|
|
use tokio::sync::mpsc;
|
|
use tokio::sync::oneshot;
|
|
use tokio::task::JoinHandle;
|
|
use tracing::debug;
|
|
use tracing::error;
|
|
use tracing::info;
|
|
|
|
pub async fn start_stdio_connection(
|
|
transport_event_tx: mpsc::Sender<TransportEvent>,
|
|
stdio_handles: &mut Vec<JoinHandle<()>>,
|
|
initialize_client_name_tx: oneshot::Sender<String>,
|
|
) -> IoResult<()> {
|
|
let connection_id = next_connection_id();
|
|
let (writer_tx, mut writer_rx) = mpsc::channel::<QueuedOutgoingMessage>(CHANNEL_CAPACITY);
|
|
let writer_tx_for_reader = writer_tx.clone();
|
|
transport_event_tx
|
|
.send(TransportEvent::ConnectionOpened {
|
|
connection_id,
|
|
origin: ConnectionOrigin::Stdio,
|
|
writer: writer_tx,
|
|
disconnect_sender: None,
|
|
})
|
|
.await
|
|
.map_err(|_| std::io::Error::new(ErrorKind::BrokenPipe, "processor unavailable"))?;
|
|
|
|
let transport_event_tx_for_reader = transport_event_tx.clone();
|
|
stdio_handles.push(tokio::spawn(async move {
|
|
let stdin = io::stdin();
|
|
let reader = BufReader::new(stdin);
|
|
let mut lines = reader.lines();
|
|
let mut initialize_client_name_tx = Some(initialize_client_name_tx);
|
|
|
|
loop {
|
|
match lines.next_line().await {
|
|
Ok(Some(line)) => {
|
|
if let Some(client_name) = stdio_initialize_client_name(&line)
|
|
&& let Some(initialize_client_name_tx) = initialize_client_name_tx.take()
|
|
{
|
|
let _ = initialize_client_name_tx.send(client_name);
|
|
}
|
|
if !forward_incoming_message(
|
|
&transport_event_tx_for_reader,
|
|
&writer_tx_for_reader,
|
|
connection_id,
|
|
&line,
|
|
)
|
|
.await
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
Ok(None) => break,
|
|
Err(err) => {
|
|
error!("Failed reading stdin: {err}");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
let _ = transport_event_tx_for_reader
|
|
.send(TransportEvent::ConnectionClosed { connection_id })
|
|
.await;
|
|
debug!("stdin reader finished (EOF)");
|
|
}));
|
|
|
|
stdio_handles.push(tokio::spawn(async move {
|
|
let mut stdout = io::stdout();
|
|
while let Some(queued_message) = writer_rx.recv().await {
|
|
let Some(mut json) = serialize_outgoing_message(queued_message.message) else {
|
|
continue;
|
|
};
|
|
json.push('\n');
|
|
if let Err(err) = stdout.write_all(json.as_bytes()).await {
|
|
error!("Failed to write to stdout: {err}");
|
|
break;
|
|
}
|
|
if let Some(write_complete_tx) = queued_message.write_complete_tx {
|
|
let _ = write_complete_tx.send(());
|
|
}
|
|
}
|
|
info!("stdout writer exited (channel closed)");
|
|
}));
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn stdio_initialize_client_name(line: &str) -> Option<String> {
|
|
let message = serde_json::from_str::<JSONRPCMessage>(line).ok()?;
|
|
let JSONRPCMessage::Request(JSONRPCRequest { method, params, .. }) = message else {
|
|
return None;
|
|
};
|
|
if method != "initialize" {
|
|
return None;
|
|
}
|
|
let params = serde_json::from_value::<InitializeParams>(params?).ok()?;
|
|
Some(params.client_info.name)
|
|
}
|