|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::path::PathBuf; |
| 3 | +use std::sync::Arc; |
| 4 | + |
| 5 | +use codex_core::ConversationManager; |
| 6 | +use codex_core::NewConversation; |
| 7 | +use codex_core::config::Config; |
| 8 | +use codex_core::config::ConfigOverrides; |
| 9 | +use mcp_types::JSONRPCErrorError; |
| 10 | +use mcp_types::RequestId; |
| 11 | +use tokio::sync::oneshot; |
| 12 | +use uuid::Uuid; |
| 13 | + |
| 14 | +use crate::error_code::INTERNAL_ERROR_CODE; |
| 15 | +use crate::error_code::INVALID_REQUEST_ERROR_CODE; |
| 16 | +use crate::json_to_toml::json_to_toml; |
| 17 | +use crate::outgoing_message::OutgoingMessageSender; |
| 18 | +use crate::outgoing_message::OutgoingNotificationMeta; |
| 19 | +use crate::wire_format::AddConversationListenerParams; |
| 20 | +use crate::wire_format::AddConversationSubscriptionResponse; |
| 21 | +use crate::wire_format::CodexRequest; |
| 22 | +use crate::wire_format::ConversationId; |
| 23 | +use crate::wire_format::InputItem as WireInputItem; |
| 24 | +use crate::wire_format::NewConversationParams; |
| 25 | +use crate::wire_format::NewConversationResponse; |
| 26 | +use crate::wire_format::RemoveConversationListenerParams; |
| 27 | +use crate::wire_format::RemoveConversationSubscriptionResponse; |
| 28 | +use crate::wire_format::SendUserMessageParams; |
| 29 | +use crate::wire_format::SendUserMessageResponse; |
| 30 | +use codex_core::protocol::InputItem as CoreInputItem; |
| 31 | +use codex_core::protocol::Op; |
| 32 | + |
| 33 | +/// Handles JSON-RPC messages for Codex conversations. |
| 34 | +pub(crate) struct CodexMessageProcessor { |
| 35 | + conversation_manager: Arc<ConversationManager>, |
| 36 | + outgoing: Arc<OutgoingMessageSender>, |
| 37 | + codex_linux_sandbox_exe: Option<PathBuf>, |
| 38 | + conversation_listeners: HashMap<Uuid, oneshot::Sender<()>>, |
| 39 | +} |
| 40 | + |
| 41 | +impl CodexMessageProcessor { |
| 42 | + pub fn new( |
| 43 | + conversation_manager: Arc<ConversationManager>, |
| 44 | + outgoing: Arc<OutgoingMessageSender>, |
| 45 | + codex_linux_sandbox_exe: Option<PathBuf>, |
| 46 | + ) -> Self { |
| 47 | + Self { |
| 48 | + conversation_manager, |
| 49 | + outgoing, |
| 50 | + codex_linux_sandbox_exe, |
| 51 | + conversation_listeners: HashMap::new(), |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + pub async fn process_request(&mut self, request: CodexRequest) { |
| 56 | + match request { |
| 57 | + CodexRequest::NewConversation { request_id, params } => { |
| 58 | + // Do not tokio::spawn() to process new_conversation() |
| 59 | + // asynchronously because we need to ensure the conversation is |
| 60 | + // created before processing any subsequent messages. |
| 61 | + self.process_new_conversation(request_id, params).await; |
| 62 | + } |
| 63 | + CodexRequest::SendUserMessage { request_id, params } => { |
| 64 | + self.send_user_message(request_id, params).await; |
| 65 | + } |
| 66 | + CodexRequest::AddConversationListener { request_id, params } => { |
| 67 | + self.add_conversation_listener(request_id, params).await; |
| 68 | + } |
| 69 | + CodexRequest::RemoveConversationListener { request_id, params } => { |
| 70 | + self.remove_conversation_listener(request_id, params).await; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + async fn process_new_conversation(&self, request_id: RequestId, params: NewConversationParams) { |
| 76 | + let config = match derive_config_from_params(params, self.codex_linux_sandbox_exe.clone()) { |
| 77 | + Ok(config) => config, |
| 78 | + Err(err) => { |
| 79 | + let error = JSONRPCErrorError { |
| 80 | + code: INVALID_REQUEST_ERROR_CODE, |
| 81 | + message: format!("error deriving config: {err}"), |
| 82 | + data: None, |
| 83 | + }; |
| 84 | + self.outgoing.send_error(request_id, error).await; |
| 85 | + return; |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + match self.conversation_manager.new_conversation(config).await { |
| 90 | + Ok(conversation_id) => { |
| 91 | + let NewConversation { |
| 92 | + conversation_id, |
| 93 | + session_configured, |
| 94 | + .. |
| 95 | + } = conversation_id; |
| 96 | + let response = NewConversationResponse { |
| 97 | + conversation_id: ConversationId(conversation_id), |
| 98 | + model: session_configured.model, |
| 99 | + }; |
| 100 | + self.outgoing.send_response(request_id, response).await; |
| 101 | + } |
| 102 | + Err(err) => { |
| 103 | + let error = JSONRPCErrorError { |
| 104 | + code: INTERNAL_ERROR_CODE, |
| 105 | + message: format!("error creating conversation: {err}"), |
| 106 | + data: None, |
| 107 | + }; |
| 108 | + self.outgoing.send_error(request_id, error).await; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + async fn send_user_message(&self, request_id: RequestId, params: SendUserMessageParams) { |
| 114 | + let SendUserMessageParams { |
| 115 | + conversation_id, |
| 116 | + items, |
| 117 | + } = params; |
| 118 | + let Ok(conversation) = self |
| 119 | + .conversation_manager |
| 120 | + .get_conversation(conversation_id.0) |
| 121 | + .await |
| 122 | + else { |
| 123 | + let error = JSONRPCErrorError { |
| 124 | + code: INVALID_REQUEST_ERROR_CODE, |
| 125 | + message: format!("conversation not found: {conversation_id}"), |
| 126 | + data: None, |
| 127 | + }; |
| 128 | + self.outgoing.send_error(request_id, error).await; |
| 129 | + return; |
| 130 | + }; |
| 131 | + |
| 132 | + let mapped_items: Vec<CoreInputItem> = items |
| 133 | + .into_iter() |
| 134 | + .map(|item| match item { |
| 135 | + WireInputItem::Text { text } => CoreInputItem::Text { text }, |
| 136 | + WireInputItem::Image { image_url } => CoreInputItem::Image { image_url }, |
| 137 | + WireInputItem::LocalImage { path } => CoreInputItem::LocalImage { path }, |
| 138 | + }) |
| 139 | + .collect(); |
| 140 | + |
| 141 | + // Submit user input to the conversation. |
| 142 | + let _ = conversation |
| 143 | + .submit(Op::UserInput { |
| 144 | + items: mapped_items, |
| 145 | + }) |
| 146 | + .await; |
| 147 | + |
| 148 | + // Acknowledge with an empty result. |
| 149 | + self.outgoing |
| 150 | + .send_response(request_id, SendUserMessageResponse {}) |
| 151 | + .await; |
| 152 | + } |
| 153 | + |
| 154 | + async fn add_conversation_listener( |
| 155 | + &mut self, |
| 156 | + request_id: RequestId, |
| 157 | + params: AddConversationListenerParams, |
| 158 | + ) { |
| 159 | + let AddConversationListenerParams { conversation_id } = params; |
| 160 | + let Ok(conversation) = self |
| 161 | + .conversation_manager |
| 162 | + .get_conversation(conversation_id.0) |
| 163 | + .await |
| 164 | + else { |
| 165 | + let error = JSONRPCErrorError { |
| 166 | + code: INVALID_REQUEST_ERROR_CODE, |
| 167 | + message: format!("conversation not found: {}", conversation_id.0), |
| 168 | + data: None, |
| 169 | + }; |
| 170 | + self.outgoing.send_error(request_id, error).await; |
| 171 | + return; |
| 172 | + }; |
| 173 | + |
| 174 | + let subscription_id = Uuid::new_v4(); |
| 175 | + let (cancel_tx, mut cancel_rx) = oneshot::channel(); |
| 176 | + self.conversation_listeners |
| 177 | + .insert(subscription_id, cancel_tx); |
| 178 | + let outgoing_for_task = self.outgoing.clone(); |
| 179 | + let add_listener_request_id = request_id.clone(); |
| 180 | + tokio::spawn(async move { |
| 181 | + loop { |
| 182 | + tokio::select! { |
| 183 | + _ = &mut cancel_rx => { |
| 184 | + // User has unsubscribed, so exit this task. |
| 185 | + break; |
| 186 | + } |
| 187 | + event = conversation.next_event() => { |
| 188 | + let event = match event { |
| 189 | + Ok(event) => event, |
| 190 | + Err(err) => { |
| 191 | + tracing::warn!("conversation.next_event() failed with: {err}"); |
| 192 | + break; |
| 193 | + } |
| 194 | + }; |
| 195 | + |
| 196 | + outgoing_for_task.send_event_as_notification( |
| 197 | + &event, |
| 198 | + Some(OutgoingNotificationMeta::new(Some(add_listener_request_id.clone()))), |
| 199 | + ) |
| 200 | + .await; |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + }); |
| 205 | + let response = AddConversationSubscriptionResponse { subscription_id }; |
| 206 | + self.outgoing.send_response(request_id, response).await; |
| 207 | + } |
| 208 | + |
| 209 | + async fn remove_conversation_listener( |
| 210 | + &mut self, |
| 211 | + request_id: RequestId, |
| 212 | + params: RemoveConversationListenerParams, |
| 213 | + ) { |
| 214 | + let RemoveConversationListenerParams { subscription_id } = params; |
| 215 | + match self.conversation_listeners.remove(&subscription_id) { |
| 216 | + Some(sender) => { |
| 217 | + // Signal the spawned task to exit and acknowledge. |
| 218 | + let _ = sender.send(()); |
| 219 | + let response = RemoveConversationSubscriptionResponse {}; |
| 220 | + self.outgoing.send_response(request_id, response).await; |
| 221 | + } |
| 222 | + None => { |
| 223 | + let error = JSONRPCErrorError { |
| 224 | + code: INVALID_REQUEST_ERROR_CODE, |
| 225 | + message: format!("subscription not found: {subscription_id}"), |
| 226 | + data: None, |
| 227 | + }; |
| 228 | + self.outgoing.send_error(request_id, error).await; |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +fn derive_config_from_params( |
| 235 | + params: NewConversationParams, |
| 236 | + codex_linux_sandbox_exe: Option<PathBuf>, |
| 237 | +) -> std::io::Result<Config> { |
| 238 | + let NewConversationParams { |
| 239 | + model, |
| 240 | + profile, |
| 241 | + cwd, |
| 242 | + approval_policy, |
| 243 | + sandbox, |
| 244 | + config: cli_overrides, |
| 245 | + base_instructions, |
| 246 | + include_plan_tool, |
| 247 | + } = params; |
| 248 | + let overrides = ConfigOverrides { |
| 249 | + model, |
| 250 | + config_profile: profile, |
| 251 | + cwd: cwd.map(PathBuf::from), |
| 252 | + approval_policy: approval_policy.map(Into::into), |
| 253 | + sandbox_mode: sandbox.map(Into::into), |
| 254 | + model_provider: None, |
| 255 | + codex_linux_sandbox_exe, |
| 256 | + base_instructions, |
| 257 | + include_plan_tool, |
| 258 | + disable_response_storage: None, |
| 259 | + show_raw_agent_reasoning: None, |
| 260 | + }; |
| 261 | + |
| 262 | + let cli_overrides = cli_overrides |
| 263 | + .unwrap_or_default() |
| 264 | + .into_iter() |
| 265 | + .map(|(k, v)| (k, json_to_toml(v))) |
| 266 | + .collect(); |
| 267 | + |
| 268 | + Config::load_with_cli_overrides(cli_overrides, overrides) |
| 269 | +} |
0 commit comments