Files
leleka/control-plane/src/serve.rs
T

97 lines
3.6 KiB
Rust

//! `leleka-api serve`: the composition root. It reads the config, builds the adapters behind the gateway ports
//! (response history, MCP hub, vision encoder, engine channel, workers) and runs the HTTP server until shutdown.
//!
//! `serve` is the list of phases in the order they must run. The phases are functions in the children:
//! `state` (what the handlers see), `engine` (the engine side) and `http` (the server and the orderly stop).
use std::path::Path;
use tokio::{sync::watch, task::JoinHandle};
use leleka_control_plane::{BoxError, config::Config, responses::ResponseHistory};
mod engine;
mod http;
mod state;
use engine::start_engine;
use http::{run_http, stop_services};
use state::{
apply_prompt_settings, attach_codec, build_state, start_mcp, start_vision, warn_open_cors,
};
type Vision = leleka_control_plane::vision::Vision;
type GrpcBackend = leleka_runtime_client::backend::GrpcBackend;
/// The tasks that must end before the process does.
#[derive(Default)]
struct Tasks {
workers: Vec<JoinHandle<()>>,
dispatcher: Option<JoinHandle<()>>,
runtime: Option<JoinHandle<()>>,
}
pub async fn serve(config_path: Option<&Path>) -> Result<(), BoxError> {
let (_embedded, mut config, listener, mut state) = boot(config_path).await?;
let vision = start_vision(&mut state, &mut config).await?;
let (shutdown_tx, shutdown_rx) = watch::channel(false);
apply_prompt_settings(&config);
let tasks = start_engine(&mut state, &mut config, vision.clone(), &shutdown_rx).await?;
engine::start_idle_unload(&state, &config);
let hub = state.services.mcp.clone();
let result = run_http(state, &config, listener, &shutdown_tx).await;
let _ = shutdown_tx.send(true);
stop_services(hub, vision, tasks).await?;
result?;
Ok(())
}
/// The phases before the engine starts: config, host check, embedded engine, provisioning, listener and handler state.
/// The returned guard keeps an unpacked `@embedded` worker until `serve` ends.
async fn boot(
config_path: Option<&Path>,
) -> Result<
(
Option<leleka_control_plane::embedded_engine::EmbeddedEngine>,
Config,
tokio::net::TcpListener,
leleka_control_plane::app::AppState,
),
BoxError,
> {
let (mut config, response_history) = load_config(config_path).await?;
config.probe_host().await?;
let embedded = resolve_embedded_engine(&mut config)?;
config.provision().await?;
let listener = tokio::net::TcpListener::bind(config.bind).await?;
let mut state = build_state(&config, response_history)?;
start_mcp(&mut state, &mut config);
warn_open_cors(&config);
attach_codec(&mut state, &config)?;
Ok((embedded, config, listener, state))
}
/// The config file (or the environment) and the stored responses it points at.
async fn load_config(config_path: Option<&Path>) -> Result<(Config, ResponseHistory), BoxError> {
let config = Config::load_with_path(config_path)?;
let history_dir = config.response_history_dir.clone();
let history =
tokio::task::spawn_blocking(move || ResponseHistory::open(history_dir.as_deref()))
.await??;
Ok((config, history))
}
/// An `@embedded` worker executable is unpacked; the returned guard keeps it until `serve` ends.
fn resolve_embedded_engine(
config: &mut Config,
) -> Result<Option<leleka_control_plane::embedded_engine::EmbeddedEngine>, BoxError> {
let Some(worker) = config.worker.as_mut() else {
return Ok(None);
};
let embedded = leleka_control_plane::embedded_engine::resolve(&worker.executable)?;
if let Some(engine) = &embedded {
worker.executable = engine.executable.clone().into_os_string();
}
Ok(embedded)
}