diff options
Diffstat (limited to 'makima/src/server')
| -rw-r--r-- | makima/src/server/handlers/mesh_supervisor.rs | 69 |
1 files changed, 65 insertions, 4 deletions
diff --git a/makima/src/server/handlers/mesh_supervisor.rs b/makima/src/server/handlers/mesh_supervisor.rs index 668ea7b..0bb58ed 100644 --- a/makima/src/server/handlers/mesh_supervisor.rs +++ b/makima/src/server/handlers/mesh_supervisor.rs @@ -1589,9 +1589,40 @@ pub async fn ask_question( ).await; } - // If non_blocking mode or phaseguard is enabled, return immediately with the question_id - // Phaseguard questions persist until answered (no timeout) and are displayed by the frontend - if request.non_blocking || request.phaseguard { + // If non_blocking mode, return immediately + if request.non_blocking { + return ( + StatusCode::OK, + Json(AskQuestionResponse { + question_id, + response: None, + timed_out: false, + }), + ).into_response(); + } + + // If phaseguard is enabled, pause the supervisor task and return + // The task will be auto-resumed when a message is sent to it (e.g., when user answers) + if request.phaseguard { + // Pause the supervisor task + if let Some(daemon_id) = supervisor.daemon_id { + let cmd = DaemonCommand::PauseTask { task_id: supervisor_id }; + if let Err(e) = state.send_daemon_command(daemon_id, cmd).await { + tracing::warn!(supervisor_id = %supervisor_id, error = %e, "Failed to pause supervisor for phaseguard"); + } else { + tracing::info!(supervisor_id = %supervisor_id, "Paused supervisor for phaseguard question"); + } + } + + // Update task status to paused in DB + let update = crate::db::models::UpdateTaskRequest { + status: Some("paused".to_string()), + ..Default::default() + }; + if let Err(e) = repository::update_task_for_owner(pool, supervisor_id, owner_id, update).await { + tracing::warn!(supervisor_id = %supervisor_id, error = %e, "Failed to update task status to paused"); + } + return ( StatusCode::OK, Json(AskQuestionResponse { @@ -1725,7 +1756,7 @@ pub async fn answer_question( }; // Submit the response - let success = state.submit_question_response(question_id, request.response); + let success = state.submit_question_response(question_id, request.response.clone()); if success { tracing::info!( @@ -1733,6 +1764,36 @@ pub async fn answer_question( task_id = %question.task_id, "User answered supervisor question" ); + + // Send the response to the task as a message + // This will auto-resume the task if it was paused (phaseguard) + let pool = state.db_pool.as_ref().unwrap(); + if let Ok(Some(task)) = repository::get_task_for_owner(pool, question.task_id, auth.owner_id).await { + if let Some(daemon_id) = task.daemon_id { + // Format the response message + let response_msg = format!( + "\n[User Response to Question]\nQuestion: {}\nAnswer: {}\n", + question.question, + request.response + ); + let cmd = DaemonCommand::SendMessage { + task_id: question.task_id, + message: response_msg, + }; + if let Err(e) = state.send_daemon_command(daemon_id, cmd).await { + tracing::warn!( + task_id = %question.task_id, + error = %e, + "Failed to send response message to task" + ); + } else { + tracing::info!( + task_id = %question.task_id, + "Sent response message to task (will auto-resume if paused)" + ); + } + } + } } Json(AnswerQuestionResponse { success }).into_response() |
