From 1b72449496ce3a057a43d002c8042d5e7a1d6576 Mon Sep 17 00:00:00 2001 From: soryu Date: Sat, 7 Feb 2026 16:36:19 +0000 Subject: Add directive init mechanism --- makima/src/server/handlers/directives.rs | 58 +++++++++++++++++++++++++++++++ makima/src/server/handlers/mesh_daemon.rs | 11 +++++- makima/src/server/mod.rs | 1 + makima/src/server/openapi.rs | 1 + 4 files changed, 70 insertions(+), 1 deletion(-) (limited to 'makima/src/server') diff --git a/makima/src/server/handlers/directives.rs b/makima/src/server/handlers/directives.rs index a74f8ff..560151b 100644 --- a/makima/src/server/handlers/directives.rs +++ b/makima/src/server/handlers/directives.rs @@ -13,6 +13,7 @@ use crate::db::models::{ DirectiveListResponse, DirectiveWithChains, UpdateDirectiveRequest, }; use crate::db::repository::{self, RepositoryError}; +use crate::orchestration; use crate::server::auth::Authenticated; use crate::server::messages::ApiError; use crate::server::state::SharedState; @@ -438,3 +439,60 @@ pub async fn get_chain( Json(ChainWithSteps { chain, steps }).into_response() } + +/// Start a directive: create a planning contract and begin orchestration. +#[utoipa::path( + post, + path = "/api/v1/directives/{id}/start", + params( + ("id" = Uuid, Path, description = "Directive ID") + ), + responses( + (status = 200, description = "Directive started", body = Directive), + (status = 400, description = "Directive not in draft status", body = ApiError), + (status = 401, description = "Unauthorized", body = ApiError), + (status = 404, description = "Directive not found", body = ApiError), + (status = 503, description = "Database not configured", body = ApiError), + (status = 500, description = "Internal server error", body = ApiError), + ), + security( + ("bearer_auth" = []), + ("api_key" = []) + ), + tag = "Directives" +)] +pub async fn start_directive( + State(state): State, + Authenticated(auth): Authenticated, + Path(id): Path, +) -> impl IntoResponse { + let Some(ref pool) = state.db_pool else { + return ( + StatusCode::SERVICE_UNAVAILABLE, + Json(ApiError::new("DB_UNAVAILABLE", "Database not configured")), + ) + .into_response(); + }; + + match orchestration::directive::init_directive(pool, &state, auth.owner_id, id).await { + Ok(directive) => Json(directive).into_response(), + Err(e) if e.contains("not found") => ( + StatusCode::NOT_FOUND, + Json(ApiError::new("NOT_FOUND", e)), + ) + .into_response(), + Err(e) if e.contains("must be in 'draft'") => ( + StatusCode::BAD_REQUEST, + Json(ApiError::new("INVALID_STATUS", e)), + ) + .into_response(), + Err(e) => { + tracing::error!("Failed to start directive {}: {}", id, e); + ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(ApiError::new("START_FAILED", e)), + ) + .into_response() + } + } +} diff --git a/makima/src/server/handlers/mesh_daemon.rs b/makima/src/server/handlers/mesh_daemon.rs index beb4c15..767d059 100644 --- a/makima/src/server/handlers/mesh_daemon.rs +++ b/makima/src/server/handlers/mesh_daemon.rs @@ -1303,7 +1303,16 @@ async fn handle_daemon_connection(socket: WebSocket, state: SharedState, auth_re }), ).await; - // TODO: Directive engine integration (removed for reimplementation) + // Directive engine integration + if let Err(e) = crate::orchestration::directive::on_task_completed( + &pool, &state, &updated_task, owner_id, + ).await { + tracing::warn!( + task_id = %task_id, + error = %e, + "Failed to process directive task completion" + ); + } } Ok(None) => { tracing::warn!( diff --git a/makima/src/server/mod.rs b/makima/src/server/mod.rs index a429612..1a59e12 100644 --- a/makima/src/server/mod.rs +++ b/makima/src/server/mod.rs @@ -181,6 +181,7 @@ pub fn make_router(state: SharedState) -> Router { .put(directives::update_directive) .delete(directives::delete_directive), ) + .route("/directives/{id}/start", post(directives::start_directive)) .route("/directives/{id}/chains", get(directives::list_chains)) .route("/directives/{id}/chains/{chain_id}", get(directives::get_chain)) // Contract supervisor resume endpoints diff --git a/makima/src/server/openapi.rs b/makima/src/server/openapi.rs index 0e6912a..96c19e0 100644 --- a/makima/src/server/openapi.rs +++ b/makima/src/server/openapi.rs @@ -111,6 +111,7 @@ use crate::server::messages::{ApiError, AudioEncoding, StartMessage, StopMessage directives::create_directive, directives::update_directive, directives::delete_directive, + directives::start_directive, directives::list_chains, directives::get_chain, ), -- cgit v1.2.3