2025-08-18 13:08:53 -07:00
|
|
|
use anyhow::Context;
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
use anyhow::anyhow;
|
fix: remove mcp-types from app server protocol (#4537)
We continue the separation between `codex app-server` and `codex
mcp-server`.
In particular, we introduce a new crate, `codex-app-server-protocol`,
and migrate `codex-rs/protocol/src/mcp_protocol.rs` into it, renaming it
`codex-rs/app-server-protocol/src/protocol.rs`.
Because `ConversationId` was defined in `mcp_protocol.rs`, we move it
into its own file, `codex-rs/protocol/src/conversation_id.rs`, and
because it is referenced in a ton of places, we have to touch a lot of
files as part of this PR.
We also decide to get away from proper JSON-RPC 2.0 semantics, so we
also introduce `codex-rs/app-server-protocol/src/jsonrpc_lite.rs`, which
is basically the same `JSONRPCMessage` type defined in `mcp-types`
except with all of the `"jsonrpc": "2.0"` removed.
Getting rid of `"jsonrpc": "2.0"` makes our serialization logic
considerably simpler, as we can lean heavier on serde to serialize
directly into the wire format that we use now.
2025-09-30 19:16:26 -07:00
|
|
|
use codex_app_server_protocol::ClientNotification;
|
|
|
|
|
use codex_app_server_protocol::ClientRequest;
|
|
|
|
|
use codex_app_server_protocol::ServerNotification;
|
|
|
|
|
use codex_app_server_protocol::ServerRequest;
|
|
|
|
|
use codex_app_server_protocol::export_client_responses;
|
|
|
|
|
use codex_app_server_protocol::export_server_responses;
|
2025-08-18 13:08:53 -07:00
|
|
|
use std::ffi::OsStr;
|
|
|
|
|
use std::fs;
|
|
|
|
|
use std::io::Read;
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
use ts_rs::TS;
|
|
|
|
|
|
|
|
|
|
const HEADER: &str = "// GENERATED CODE! DO NOT MODIFY BY HAND!\n\n";
|
|
|
|
|
|
|
|
|
|
pub fn generate_ts(out_dir: &Path, prettier: Option<&Path>) -> Result<()> {
|
|
|
|
|
ensure_dir(out_dir)?;
|
|
|
|
|
|
2025-09-30 18:06:05 -07:00
|
|
|
// Generate the TS bindings client -> server messages.
|
2025-09-30 14:08:43 -07:00
|
|
|
ClientRequest::export_all_to(out_dir)?;
|
2025-09-30 18:06:05 -07:00
|
|
|
export_client_responses(out_dir)?;
|
2025-09-30 14:08:43 -07:00
|
|
|
ClientNotification::export_all_to(out_dir)?;
|
2025-09-30 18:06:05 -07:00
|
|
|
|
|
|
|
|
// Generate the TS bindings server -> client messages.
|
2025-09-30 14:08:43 -07:00
|
|
|
ServerRequest::export_all_to(out_dir)?;
|
2025-09-30 18:06:05 -07:00
|
|
|
export_server_responses(out_dir)?;
|
2025-09-30 14:08:43 -07:00
|
|
|
ServerNotification::export_all_to(out_dir)?;
|
|
|
|
|
|
2025-09-30 18:06:05 -07:00
|
|
|
// Generate index.ts that re-exports all types.
|
2025-08-25 10:23:32 -07:00
|
|
|
generate_index_ts(out_dir)?;
|
|
|
|
|
|
2025-08-18 13:08:53 -07:00
|
|
|
// Prepend header to each generated .ts file
|
|
|
|
|
let ts_files = ts_files_in(out_dir)?;
|
|
|
|
|
for file in &ts_files {
|
|
|
|
|
prepend_header_if_missing(file)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Format with Prettier by passing individual files (no shell globbing)
|
2025-08-19 13:22:02 -07:00
|
|
|
if let Some(prettier_bin) = prettier
|
|
|
|
|
&& !ts_files.is_empty()
|
|
|
|
|
{
|
|
|
|
|
let status = Command::new(prettier_bin)
|
|
|
|
|
.arg("--write")
|
|
|
|
|
.args(ts_files.iter().map(|p| p.as_os_str()))
|
|
|
|
|
.status()
|
|
|
|
|
.with_context(|| format!("Failed to invoke Prettier at {}", prettier_bin.display()))?;
|
|
|
|
|
if !status.success() {
|
2025-09-30 03:10:33 -07:00
|
|
|
return Err(anyhow!("Prettier failed with status {status}"));
|
2025-08-18 13:08:53 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ensure_dir(dir: &Path) -> Result<()> {
|
|
|
|
|
fs::create_dir_all(dir)
|
|
|
|
|
.with_context(|| format!("Failed to create output directory {}", dir.display()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn prepend_header_if_missing(path: &Path) -> Result<()> {
|
|
|
|
|
let mut content = String::new();
|
|
|
|
|
{
|
|
|
|
|
let mut f = fs::File::open(path)
|
|
|
|
|
.with_context(|| format!("Failed to open {} for reading", path.display()))?;
|
|
|
|
|
f.read_to_string(&mut content)
|
|
|
|
|
.with_context(|| format!("Failed to read {}", path.display()))?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if content.starts_with(HEADER) {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut f = fs::File::create(path)
|
|
|
|
|
.with_context(|| format!("Failed to open {} for writing", path.display()))?;
|
|
|
|
|
f.write_all(HEADER.as_bytes())
|
|
|
|
|
.with_context(|| format!("Failed to write header to {}", path.display()))?;
|
|
|
|
|
f.write_all(content.as_bytes())
|
|
|
|
|
.with_context(|| format!("Failed to write content to {}", path.display()))?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ts_files_in(dir: &Path) -> Result<Vec<PathBuf>> {
|
|
|
|
|
let mut files = Vec::new();
|
|
|
|
|
for entry in
|
|
|
|
|
fs::read_dir(dir).with_context(|| format!("Failed to read dir {}", dir.display()))?
|
|
|
|
|
{
|
|
|
|
|
let entry = entry?;
|
|
|
|
|
let path = entry.path();
|
|
|
|
|
if path.is_file() && path.extension() == Some(OsStr::new("ts")) {
|
|
|
|
|
files.push(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-25 10:23:32 -07:00
|
|
|
files.sort();
|
2025-08-18 13:08:53 -07:00
|
|
|
Ok(files)
|
|
|
|
|
}
|
2025-08-25 10:23:32 -07:00
|
|
|
|
|
|
|
|
/// Generate an index.ts file that re-exports all generated types.
|
|
|
|
|
/// This allows consumers to import all types from a single file.
|
|
|
|
|
fn generate_index_ts(out_dir: &Path) -> Result<PathBuf> {
|
|
|
|
|
let mut entries: Vec<String> = Vec::new();
|
|
|
|
|
let mut stems: Vec<String> = ts_files_in(out_dir)?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter_map(|p| {
|
|
|
|
|
let stem = p.file_stem()?.to_string_lossy().into_owned();
|
|
|
|
|
if stem == "index" { None } else { Some(stem) }
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
stems.sort();
|
|
|
|
|
stems.dedup();
|
|
|
|
|
|
|
|
|
|
for name in stems {
|
|
|
|
|
entries.push(format!("export type {{ {name} }} from \"./{name}\";\n"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut content =
|
2025-09-22 20:30:16 +01:00
|
|
|
String::with_capacity(HEADER.len() + entries.iter().map(String::len).sum::<usize>());
|
2025-08-25 10:23:32 -07:00
|
|
|
content.push_str(HEADER);
|
|
|
|
|
for line in &entries {
|
|
|
|
|
content.push_str(line);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let index_path = out_dir.join("index.ts");
|
|
|
|
|
let mut f = fs::File::create(&index_path)
|
|
|
|
|
.with_context(|| format!("Failed to create {}", index_path.display()))?;
|
|
|
|
|
f.write_all(content.as_bytes())
|
|
|
|
|
.with_context(|| format!("Failed to write {}", index_path.display()))?;
|
|
|
|
|
Ok(index_path)
|
|
|
|
|
}
|