[hygiene][app-server] have a helper function for duplicate code in turn APIs (#6488)
turn_start and turn_interrupt have some logic that can be shared. have a helper function for it.
This commit is contained in:
@@ -198,6 +198,30 @@ enum ApiVersion {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl CodexMessageProcessor {
|
impl CodexMessageProcessor {
|
||||||
|
async fn conversation_from_thread_id(
|
||||||
|
&self,
|
||||||
|
thread_id: &str,
|
||||||
|
) -> Result<(ConversationId, Arc<CodexConversation>), JSONRPCErrorError> {
|
||||||
|
// Resolve conversation id from v2 thread id string.
|
||||||
|
let conversation_id =
|
||||||
|
ConversationId::from_string(thread_id).map_err(|err| JSONRPCErrorError {
|
||||||
|
code: INVALID_REQUEST_ERROR_CODE,
|
||||||
|
message: format!("invalid thread id: {err}"),
|
||||||
|
data: None,
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let conversation = self
|
||||||
|
.conversation_manager
|
||||||
|
.get_conversation(conversation_id)
|
||||||
|
.await
|
||||||
|
.map_err(|_| JSONRPCErrorError {
|
||||||
|
code: INVALID_REQUEST_ERROR_CODE,
|
||||||
|
message: format!("conversation not found: {conversation_id}"),
|
||||||
|
data: None,
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok((conversation_id, conversation))
|
||||||
|
}
|
||||||
pub fn new(
|
pub fn new(
|
||||||
auth_manager: Arc<AuthManager>,
|
auth_manager: Arc<AuthManager>,
|
||||||
conversation_manager: Arc<ConversationManager>,
|
conversation_manager: Arc<ConversationManager>,
|
||||||
@@ -2145,34 +2169,14 @@ impl CodexMessageProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn turn_start(&self, request_id: RequestId, params: TurnStartParams) {
|
async fn turn_start(&self, request_id: RequestId, params: TurnStartParams) {
|
||||||
// Resolve conversation id from v2 thread id string.
|
let (_, conversation) = match self.conversation_from_thread_id(¶ms.thread_id).await {
|
||||||
let conversation_id = match ConversationId::from_string(¶ms.thread_id) {
|
Ok(v) => v,
|
||||||
Ok(id) => id,
|
Err(error) => {
|
||||||
Err(err) => {
|
|
||||||
let error = JSONRPCErrorError {
|
|
||||||
code: INVALID_REQUEST_ERROR_CODE,
|
|
||||||
message: format!("invalid thread id: {err}"),
|
|
||||||
data: None,
|
|
||||||
};
|
|
||||||
self.outgoing.send_error(request_id, error).await;
|
self.outgoing.send_error(request_id, error).await;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let Ok(conversation) = self
|
|
||||||
.conversation_manager
|
|
||||||
.get_conversation(conversation_id)
|
|
||||||
.await
|
|
||||||
else {
|
|
||||||
let error = JSONRPCErrorError {
|
|
||||||
code: INVALID_REQUEST_ERROR_CODE,
|
|
||||||
message: format!("conversation not found: {conversation_id}"),
|
|
||||||
data: None,
|
|
||||||
};
|
|
||||||
self.outgoing.send_error(request_id, error).await;
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Keep a copy of v2 inputs for the notification payload.
|
// Keep a copy of v2 inputs for the notification payload.
|
||||||
let v2_inputs_for_notif = params.input.clone();
|
let v2_inputs_for_notif = params.input.clone();
|
||||||
|
|
||||||
@@ -2246,33 +2250,14 @@ impl CodexMessageProcessor {
|
|||||||
async fn turn_interrupt(&mut self, request_id: RequestId, params: TurnInterruptParams) {
|
async fn turn_interrupt(&mut self, request_id: RequestId, params: TurnInterruptParams) {
|
||||||
let TurnInterruptParams { thread_id, .. } = params;
|
let TurnInterruptParams { thread_id, .. } = params;
|
||||||
|
|
||||||
// Resolve conversation id from v2 thread id string.
|
let (conversation_id, conversation) =
|
||||||
let conversation_id = match ConversationId::from_string(&thread_id) {
|
match self.conversation_from_thread_id(&thread_id).await {
|
||||||
Ok(id) => id,
|
Ok(v) => v,
|
||||||
Err(err) => {
|
Err(error) => {
|
||||||
let error = JSONRPCErrorError {
|
self.outgoing.send_error(request_id, error).await;
|
||||||
code: INVALID_REQUEST_ERROR_CODE,
|
return;
|
||||||
message: format!("invalid thread id: {err}"),
|
}
|
||||||
data: None,
|
|
||||||
};
|
|
||||||
self.outgoing.send_error(request_id, error).await;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let Ok(conversation) = self
|
|
||||||
.conversation_manager
|
|
||||||
.get_conversation(conversation_id)
|
|
||||||
.await
|
|
||||||
else {
|
|
||||||
let error = JSONRPCErrorError {
|
|
||||||
code: INVALID_REQUEST_ERROR_CODE,
|
|
||||||
message: format!("conversation not found: {conversation_id}"),
|
|
||||||
data: None,
|
|
||||||
};
|
};
|
||||||
self.outgoing.send_error(request_id, error).await;
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Record the pending interrupt so we can reply when TurnAborted arrives.
|
// Record the pending interrupt so we can reply when TurnAborted arrives.
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user