[codex][app-server] improve error response for client requests (#6050)
This commit is contained in:
@@ -64,64 +64,79 @@ impl MessageProcessor {
|
|||||||
|
|
||||||
pub(crate) async fn process_request(&mut self, request: JSONRPCRequest) {
|
pub(crate) async fn process_request(&mut self, request: JSONRPCRequest) {
|
||||||
let request_id = request.id.clone();
|
let request_id = request.id.clone();
|
||||||
if let Ok(request_json) = serde_json::to_value(request)
|
let request_json = match serde_json::to_value(&request) {
|
||||||
&& let Ok(codex_request) = serde_json::from_value::<ClientRequest>(request_json)
|
Ok(request_json) => request_json,
|
||||||
{
|
Err(err) => {
|
||||||
match codex_request {
|
let error = JSONRPCErrorError {
|
||||||
// Handle Initialize internally so CodexMessageProcessor does not have to concern
|
code: INVALID_REQUEST_ERROR_CODE,
|
||||||
// itself with the `initialized` bool.
|
message: format!("Invalid request: {err}"),
|
||||||
ClientRequest::Initialize { request_id, params } => {
|
data: None,
|
||||||
if self.initialized {
|
};
|
||||||
let error = JSONRPCErrorError {
|
self.outgoing.send_error(request_id, error).await;
|
||||||
code: INVALID_REQUEST_ERROR_CODE,
|
return;
|
||||||
message: "Already initialized".to_string(),
|
}
|
||||||
data: None,
|
};
|
||||||
};
|
|
||||||
self.outgoing.send_error(request_id, error).await;
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
let ClientInfo {
|
|
||||||
name,
|
|
||||||
title: _title,
|
|
||||||
version,
|
|
||||||
} = params.client_info;
|
|
||||||
let user_agent_suffix = format!("{name}; {version}");
|
|
||||||
if let Ok(mut suffix) = USER_AGENT_SUFFIX.lock() {
|
|
||||||
*suffix = Some(user_agent_suffix);
|
|
||||||
}
|
|
||||||
|
|
||||||
let user_agent = get_codex_user_agent();
|
let codex_request = match serde_json::from_value::<ClientRequest>(request_json) {
|
||||||
let response = InitializeResponse { user_agent };
|
Ok(codex_request) => codex_request,
|
||||||
self.outgoing.send_response(request_id, response).await;
|
Err(err) => {
|
||||||
|
let error = JSONRPCErrorError {
|
||||||
|
code: INVALID_REQUEST_ERROR_CODE,
|
||||||
|
message: format!("Invalid request: {err}"),
|
||||||
|
data: None,
|
||||||
|
};
|
||||||
|
self.outgoing.send_error(request_id, error).await;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
self.initialized = true;
|
match codex_request {
|
||||||
return;
|
// Handle Initialize internally so CodexMessageProcessor does not have to concern
|
||||||
}
|
// itself with the `initialized` bool.
|
||||||
}
|
ClientRequest::Initialize { request_id, params } => {
|
||||||
_ => {
|
if self.initialized {
|
||||||
if !self.initialized {
|
let error = JSONRPCErrorError {
|
||||||
let error = JSONRPCErrorError {
|
code: INVALID_REQUEST_ERROR_CODE,
|
||||||
code: INVALID_REQUEST_ERROR_CODE,
|
message: "Already initialized".to_string(),
|
||||||
message: "Not initialized".to_string(),
|
data: None,
|
||||||
data: None,
|
};
|
||||||
};
|
self.outgoing.send_error(request_id, error).await;
|
||||||
self.outgoing.send_error(request_id, error).await;
|
return;
|
||||||
return;
|
} else {
|
||||||
|
let ClientInfo {
|
||||||
|
name,
|
||||||
|
title: _title,
|
||||||
|
version,
|
||||||
|
} = params.client_info;
|
||||||
|
let user_agent_suffix = format!("{name}; {version}");
|
||||||
|
if let Ok(mut suffix) = USER_AGENT_SUFFIX.lock() {
|
||||||
|
*suffix = Some(user_agent_suffix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let user_agent = get_codex_user_agent();
|
||||||
|
let response = InitializeResponse { user_agent };
|
||||||
|
self.outgoing.send_response(request_id, response).await;
|
||||||
|
|
||||||
|
self.initialized = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
if !self.initialized {
|
||||||
|
let error = JSONRPCErrorError {
|
||||||
|
code: INVALID_REQUEST_ERROR_CODE,
|
||||||
|
message: "Not initialized".to_string(),
|
||||||
|
data: None,
|
||||||
|
};
|
||||||
|
self.outgoing.send_error(request_id, error).await;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.codex_message_processor
|
|
||||||
.process_request(codex_request)
|
|
||||||
.await;
|
|
||||||
} else {
|
|
||||||
let error = JSONRPCErrorError {
|
|
||||||
code: INVALID_REQUEST_ERROR_CODE,
|
|
||||||
message: "Invalid request".to_string(),
|
|
||||||
data: None,
|
|
||||||
};
|
|
||||||
self.outgoing.send_error(request_id, error).await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.codex_message_processor
|
||||||
|
.process_request(codex_request)
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn process_notification(&self, notification: JSONRPCNotification) {
|
pub(crate) async fn process_notification(&self, notification: JSONRPCNotification) {
|
||||||
|
|||||||
Reference in New Issue
Block a user