fix: update OutgoingMessageSender::send_response() to take Serialize (#2263)

This makes `send_response()` easier to work with.
This commit is contained in:
Michael Bolin
2025-08-13 14:29:13 -07:00
committed by GitHub
parent d4533a0bb3
commit 37fc4185ef
5 changed files with 29 additions and 17 deletions

View File

@@ -61,7 +61,7 @@ pub async fn run_codex_tool_session(
is_error: Some(true), is_error: Some(true),
structured_content: None, structured_content: None,
}; };
outgoing.send_response(id.clone(), result.into()).await; outgoing.send_response(id.clone(), result).await;
return; return;
} }
}; };
@@ -235,9 +235,7 @@ async fn run_codex_tool_session_inner(
is_error: None, is_error: None,
structured_content: None, structured_content: None,
}; };
outgoing outgoing.send_response(request_id.clone(), result).await;
.send_response(request_id.clone(), result.into())
.await;
// unregister the id so we don't keep it in the map // unregister the id so we don't keep it in the map
running_requests_id_to_codex_uuid running_requests_id_to_codex_uuid
.lock() .lock()
@@ -296,9 +294,7 @@ async fn run_codex_tool_session_inner(
// structured way. // structured way.
structured_content: None, structured_content: None,
}; };
outgoing outgoing.send_response(request_id.clone(), result).await;
.send_response(request_id.clone(), result.into())
.await;
break; break;
} }
} }

View File

@@ -0,0 +1,2 @@
pub(crate) const INVALID_REQUEST_ERROR_CODE: i64 = -32600;
pub(crate) const INTERNAL_ERROR_CODE: i64 = -32603;

View File

@@ -18,6 +18,7 @@ use tracing_subscriber::EnvFilter;
mod codex_tool_config; mod codex_tool_config;
mod codex_tool_runner; mod codex_tool_runner;
mod conversation_loop; mod conversation_loop;
mod error_code;
mod exec_approval; mod exec_approval;
mod json_to_toml; mod json_to_toml;
pub mod mcp_protocol; pub mod mcp_protocol;

View File

@@ -7,6 +7,7 @@ use crate::codex_tool_config::CodexToolCallParam;
use crate::codex_tool_config::CodexToolCallReplyParam; use crate::codex_tool_config::CodexToolCallReplyParam;
use crate::codex_tool_config::create_tool_for_codex_tool_call_param; use crate::codex_tool_config::create_tool_for_codex_tool_call_param;
use crate::codex_tool_config::create_tool_for_codex_tool_call_reply_param; use crate::codex_tool_config::create_tool_for_codex_tool_call_reply_param;
use crate::error_code::INVALID_REQUEST_ERROR_CODE;
use crate::mcp_protocol::ToolCallRequestParams; use crate::mcp_protocol::ToolCallRequestParams;
use crate::mcp_protocol::ToolCallResponse; use crate::mcp_protocol::ToolCallResponse;
use crate::mcp_protocol::ToolCallResponseResult; use crate::mcp_protocol::ToolCallResponseResult;
@@ -191,7 +192,7 @@ impl MessageProcessor {
if self.initialized { if self.initialized {
// Already initialised: send JSON-RPC error response. // Already initialised: send JSON-RPC error response.
let error = JSONRPCErrorError { let error = JSONRPCErrorError {
code: -32600, // Invalid Request code: INVALID_REQUEST_ERROR_CODE,
message: "initialize called more than once".to_string(), message: "initialize called more than once".to_string(),
data: None, data: None,
}; };
@@ -230,9 +231,6 @@ impl MessageProcessor {
where where
T: ModelContextProtocolRequest, T: ModelContextProtocolRequest,
{ {
// result has `Serialized` instance so should never fail
#[expect(clippy::unwrap_used)]
let result = serde_json::to_value(result).unwrap();
self.outgoing.send_response(id, result).await; self.outgoing.send_response(id, result).await;
} }
@@ -533,9 +531,7 @@ impl MessageProcessor {
is_error: Some(true), is_error: Some(true),
structured_content: None, structured_content: None,
}; };
outgoing outgoing.send_response(request_id, result).await;
.send_response(request_id, serde_json::to_value(result).unwrap_or_default())
.await;
return; return;
} }
}; };

View File

@@ -18,6 +18,8 @@ use tokio::sync::mpsc;
use tokio::sync::oneshot; use tokio::sync::oneshot;
use tracing::warn; use tracing::warn;
use crate::error_code::INTERNAL_ERROR_CODE;
/// Sends messages to the client and manages request callbacks. /// Sends messages to the client and manages request callbacks.
pub(crate) struct OutgoingMessageSender { pub(crate) struct OutgoingMessageSender {
next_request_id: AtomicI64, next_request_id: AtomicI64,
@@ -74,9 +76,24 @@ impl OutgoingMessageSender {
} }
} }
pub(crate) async fn send_response(&self, id: RequestId, result: Result) { pub(crate) async fn send_response<T: Serialize>(&self, id: RequestId, response: T) {
let outgoing_message = OutgoingMessage::Response(OutgoingResponse { id, result }); match serde_json::to_value(response) {
let _ = self.sender.send(outgoing_message).await; Ok(result) => {
let outgoing_message = OutgoingMessage::Response(OutgoingResponse { id, result });
let _ = self.sender.send(outgoing_message).await;
}
Err(err) => {
self.send_error(
id,
JSONRPCErrorError {
code: INTERNAL_ERROR_CODE,
message: format!("failed to serialize response: {err}"),
data: None,
},
)
.await;
}
}
} }
pub(crate) async fn send_event_as_notification( pub(crate) async fn send_event_as_notification(