fix: use macros to ensure request/response symmetry (#4529)

Manually curating `protocol-ts/src/lib.rs` was error-prone, as expected.
I finally asked Codex to write some Rust macros so we can ensure that:

- For every variant of `ClientRequest` and `ServerRequest`, there is an
associated `params` and `response` type.
- All response types are included automatically in the output of `codex
generate-ts`.
This commit is contained in:
Michael Bolin
2025-09-30 18:06:05 -07:00
committed by GitHub
parent 7fc3edf8a7
commit 32853ecbc5
9 changed files with 254 additions and 134 deletions

View File

@@ -1,6 +1,12 @@
use anyhow::Context;
use anyhow::Result;
use anyhow::anyhow;
use codex_protocol::mcp_protocol::ClientNotification;
use codex_protocol::mcp_protocol::ClientRequest;
use codex_protocol::mcp_protocol::ServerNotification;
use codex_protocol::mcp_protocol::ServerRequest;
use codex_protocol::mcp_protocol::export_client_responses;
use codex_protocol::mcp_protocol::export_server_responses;
use std::ffi::OsStr;
use std::fs;
use std::io::Read;
@@ -15,44 +21,17 @@ 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)?;
use codex_protocol::mcp_protocol::*;
// Generating the TS bindings for these top-level enums ensures all types
// reachable from them will be generated by induction, so they do not need
// to be listed individually.
// Generate the TS bindings client -> server messages.
ClientRequest::export_all_to(out_dir)?;
export_client_responses(out_dir)?;
ClientNotification::export_all_to(out_dir)?;
// Generate the TS bindings server -> client messages.
ServerRequest::export_all_to(out_dir)?;
export_server_responses(out_dir)?;
ServerNotification::export_all_to(out_dir)?;
// Response types for ClientRequest (mirror enum order).
InitializeResponse::export_all_to(out_dir)?;
NewConversationResponse::export_all_to(out_dir)?;
ListConversationsResponse::export_all_to(out_dir)?;
ResumeConversationResponse::export_all_to(out_dir)?;
ArchiveConversationResponse::export_all_to(out_dir)?;
SendUserMessageResponse::export_all_to(out_dir)?;
SendUserTurnResponse::export_all_to(out_dir)?;
InterruptConversationResponse::export_all_to(out_dir)?;
AddConversationSubscriptionResponse::export_all_to(out_dir)?;
RemoveConversationSubscriptionResponse::export_all_to(out_dir)?;
GitDiffToRemoteResponse::export_all_to(out_dir)?;
LoginApiKeyResponse::export_all_to(out_dir)?;
LoginChatGptResponse::export_all_to(out_dir)?;
CancelLoginChatGptResponse::export_all_to(out_dir)?;
LogoutChatGptResponse::export_all_to(out_dir)?;
GetAuthStatusResponse::export_all_to(out_dir)?;
GetUserSavedConfigResponse::export_all_to(out_dir)?;
SetDefaultModelResponse::export_all_to(out_dir)?;
GetUserAgentResponse::export_all_to(out_dir)?;
UserInfoResponse::export_all_to(out_dir)?;
FuzzyFileSearchResponse::export_all_to(out_dir)?;
ExecArbitraryCommandResponse::export_all_to(out_dir)?;
// Response types for ServerRequest (mirror enum order).
ApplyPatchApprovalResponse::export_all_to(out_dir)?;
ExecCommandApprovalResponse::export_all_to(out_dir)?;
// Generate index.ts that re-exports all types.
generate_index_ts(out_dir)?;
// Prepend header to each generated .ts file