Skip to main content

feagi_api/transports/http/
server.rs

1// Copyright 2025 Neuraville Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4// HTTP server implementation (Axum)
5//
6// This module sets up the HTTP API server with Axum, including routing,
7// middleware, and state management.
8
9use axum::{
10    body::Body,
11    extract::State,
12    http::{Request, StatusCode},
13    middleware::{self, Next},
14    response::{Html, IntoResponse, Json, Redirect, Response},
15    routing::{get, put},
16    Router,
17};
18use http_body_util::BodyExt;
19use std::sync::Arc;
20use std::time::Duration;
21use tower_http::{
22    cors::{Any, CorsLayer},
23    trace::TraceLayer,
24};
25use utoipa::OpenApi;
26
27use crate::amalgamation;
28#[cfg(feature = "http")]
29use crate::openapi::ApiDoc;
30#[cfg(feature = "feagi-agent")]
31use feagi_config::load_config;
32#[cfg(feature = "feagi-agent")]
33use feagi_io::protocol_implementations::websocket::websocket_std::{
34    FeagiWebSocketServerPublisherProperties, FeagiWebSocketServerPullerProperties,
35};
36#[cfg(feature = "feagi-agent")]
37use feagi_io::protocol_implementations::zmq::{
38    FeagiZmqServerPublisherProperties, FeagiZmqServerPullerProperties,
39};
40#[cfg(feature = "services")]
41use feagi_services::traits::{AgentService, SystemService};
42#[cfg(feature = "services")]
43use feagi_services::{
44    AnalyticsService, ConnectomeService, GenomeService, NeuronService, RuntimeService,
45};
46
47/// Application state shared across all HTTP handlers
48#[derive(Clone)]
49pub struct ApiState {
50    /// Optional provider for GET /v1/network/connection_info (None in tests/WASM)
51    pub network_connection_info_provider:
52        Option<Arc<dyn crate::endpoints::network::NetworkConnectionInfoProvider>>,
53    pub agent_service: Option<Arc<dyn AgentService + Send + Sync>>,
54    pub analytics_service: Arc<dyn AnalyticsService + Send + Sync>,
55    pub connectome_service: Arc<dyn ConnectomeService + Send + Sync>,
56    pub genome_service: Arc<dyn GenomeService + Send + Sync>,
57    pub neuron_service: Arc<dyn NeuronService + Send + Sync>,
58    pub runtime_service: Arc<dyn RuntimeService + Send + Sync>,
59    pub system_service: Arc<dyn SystemService + Send + Sync>,
60    pub snapshot_service: Option<Arc<dyn feagi_services::SnapshotService + Send + Sync>>,
61    /// FEAGI session timestamp in milliseconds (Unix timestamp when FEAGI started)
62    /// This is a unique identifier for each FEAGI instance/session
63    pub feagi_session_timestamp: i64,
64    /// Memory area stats cache (updated by plasticity service, read by health check)
65    pub memory_stats_cache: Option<feagi_npu_plasticity::MemoryStatsCache>,
66    /// In-memory amalgamation state (pending request + history), surfaced via health_check.
67    pub amalgamation_state: amalgamation::SharedAmalgamationState,
68    /// Agent handler for device registrations and transport management
69    #[cfg(feature = "feagi-agent")]
70    pub agent_handler: Option<Arc<std::sync::Mutex<feagi_agent::server::FeagiAgentHandler>>>,
71}
72
73impl ApiState {
74    /// Initialize the agent handler (deprecated - use external initialization).
75    #[cfg(feature = "feagi-agent")]
76    pub fn init_agent_registration_handler(
77    ) -> Arc<std::sync::Mutex<feagi_agent::server::FeagiAgentHandler>> {
78        let config = load_config(None, None).expect("Failed to load FEAGI configuration");
79        let liveness_config = feagi_agent::server::AgentLivenessConfig {
80            heartbeat_timeout: Duration::from_millis(config.zmq.client_heartbeat_timeout),
81            stale_check_interval: Duration::from_millis(config.zmq.polling_timeout),
82        };
83        let mut handler = feagi_agent::server::FeagiAgentHandler::new_with_liveness_config(
84            Box::new(feagi_agent::server::auth::DummyAuth {}),
85            liveness_config,
86        );
87        let available_transports: Vec<String> = config
88            .transports
89            .available
90            .iter()
91            .map(|transport| transport.to_lowercase())
92            .collect();
93
94        if available_transports
95            .iter()
96            .any(|transport| transport == "zmq")
97        {
98            let sensory_address =
99                format_tcp_endpoint(&config.zmq.bind_host, config.ports.zmq_sensory_port);
100            let sensory_adv_address =
101                format_tcp_endpoint(&config.zmq.advertised_host, config.ports.zmq_sensory_port);
102            let motor_address =
103                format_tcp_endpoint(&config.zmq.bind_host, config.ports.zmq_motor_port);
104            let motor_adv_address =
105                format_tcp_endpoint(&config.zmq.advertised_host, config.ports.zmq_motor_port);
106            let visualization_address =
107                format_tcp_endpoint(&config.zmq.bind_host, config.ports.zmq_visualization_port);
108            let visualization_adv_address = format_tcp_endpoint(
109                &config.zmq.advertised_host,
110                config.ports.zmq_visualization_port,
111            );
112
113            let sensory =
114                FeagiZmqServerPullerProperties::new(&sensory_address, &sensory_adv_address)
115                    .expect("Failed to create ZMQ sensory puller properties");
116            handler.add_puller_server(Box::new(sensory));
117
118            let motor = FeagiZmqServerPublisherProperties::new(&motor_address, &motor_adv_address)
119                .expect("Failed to create ZMQ motor publisher properties");
120            let visualization = FeagiZmqServerPublisherProperties::new(
121                &visualization_address,
122                &visualization_adv_address,
123            )
124            .expect("Failed to create ZMQ visualization publisher properties");
125            handler.add_publisher_server(Box::new(motor));
126            handler.add_publisher_server(Box::new(visualization));
127        }
128
129        if available_transports
130            .iter()
131            .any(|transport| transport == "websocket" || transport == "ws")
132        {
133            let sensory_address =
134                format_ws_address(&config.websocket.bind_host, config.websocket.sensory_port);
135            let sensory_adv_address = format_ws_address(
136                &config.websocket.advertised_host,
137                config.websocket.sensory_port,
138            );
139            let motor_address =
140                format_ws_address(&config.websocket.bind_host, config.websocket.motor_port);
141            let motor_adv_address = format_ws_address(
142                &config.websocket.advertised_host,
143                config.websocket.motor_port,
144            );
145            let visualization_address = format_ws_address(
146                &config.websocket.bind_host,
147                config.websocket.visualization_port,
148            );
149            let visualization_adv_address = format_ws_address(
150                &config.websocket.advertised_host,
151                config.websocket.visualization_port,
152            );
153
154            let sensory = FeagiWebSocketServerPullerProperties::new_with_remote(
155                &sensory_address,
156                &sensory_adv_address,
157            )
158            .expect("Failed to create WebSocket sensory puller properties");
159            handler.add_puller_server(Box::new(sensory));
160
161            let motor =
162                FeagiWebSocketServerPublisherProperties::new(&motor_address, &motor_adv_address)
163                    .expect("Failed to create WebSocket motor publisher properties");
164            let visualization = FeagiWebSocketServerPublisherProperties::new(
165                &visualization_address,
166                &visualization_adv_address,
167            )
168            .expect("Failed to create WebSocket visualization publisher properties");
169            handler.add_publisher_server(Box::new(motor));
170            handler.add_publisher_server(Box::new(visualization));
171        }
172
173        Arc::new(std::sync::Mutex::new(handler))
174    }
175
176    /// Initialize amalgamation_state field (empty state).
177    pub fn init_amalgamation_state() -> amalgamation::SharedAmalgamationState {
178        amalgamation::new_shared_state()
179    }
180}
181
182#[cfg(feature = "feagi-agent")]
183fn format_tcp_endpoint(host: &str, port: u16) -> String {
184    if host.contains(':') {
185        format!("tcp://[{host}]:{port}")
186    } else {
187        format!("tcp://{host}:{port}")
188    }
189}
190
191#[cfg(feature = "feagi-agent")]
192fn format_ws_address(host: &str, port: u16) -> String {
193    if host.contains(':') {
194        format!("[{host}]:{port}")
195    } else {
196        format!("{host}:{port}")
197    }
198}
199
200/// Create the main HTTP server application
201pub fn create_http_server(state: ApiState) -> Router {
202    Router::new()
203        // Root redirect to custom Swagger UI
204        .route("/", get(root_redirect))
205
206        // Custom Swagger UI with FEAGI branding at /swagger-ui/
207        .route("/swagger-ui/", get(custom_swagger_ui))
208
209        // OpenAPI spec endpoint
210        .route("/api-docs/openapi.json", get(|| async {
211            Json(ApiDoc::openapi())
212        }))
213
214        // Python-compatible paths: /v1/* (ONLY this, matching Python exactly)
215        .nest("/v1", create_v1_router())
216
217        // Catch-all route for debugging unmatched requests
218        .fallback(|| async {
219            tracing::warn!(target: "feagi-api", "Unmatched request - 404 Not Found");
220            (StatusCode::NOT_FOUND, "404 Not Found")
221        })
222
223        // Add state
224        .with_state(state)
225
226        // Add middleware
227        .layer(middleware::from_fn(log_request_response_bodies))
228        .layer(create_cors_layer())
229        .layer(
230            TraceLayer::new_for_http()
231                .make_span_with(|request: &axum::http::Request<_>| {
232                    tracing::span!(
233                        target: "feagi-api",
234                        tracing::Level::TRACE,
235                        "request",
236                        method = %request.method(),
237                        uri = %request.uri(),
238                        version = ?request.version(),
239                    )
240                })
241                .on_request(|request: &axum::http::Request<_>, _span: &tracing::Span| {
242                    tracing::trace!(target: "feagi-api", "Incoming request: {} {}", request.method(), request.uri());
243                })
244                .on_response(|response: &axum::http::Response<_>, latency: std::time::Duration, span: &tracing::Span| {
245                    tracing::trace!(
246                        target: "feagi-api",
247                        "Response: status={}, latency={:?}",
248                        response.status(),
249                        latency
250                    );
251                    span.record("status", response.status().as_u16());
252                    span.record("latency_ms", latency.as_millis());
253                })
254                .on_body_chunk(|chunk: &axum::body::Bytes, latency: std::time::Duration, _span: &tracing::Span| {
255                    tracing::trace!(target: "feagi-api", "Response chunk: {} bytes, latency={:?}", chunk.len(), latency);
256                })
257                .on_eos(|_trailers: Option<&axum::http::HeaderMap>, stream_duration: std::time::Duration, _span: &tracing::Span| {
258                    tracing::trace!(target: "feagi-api", "Stream ended, duration={:?}", stream_duration);
259                })
260                .on_failure(|error: tower_http::classify::ServerErrorsFailureClass, latency: std::time::Duration, _span: &tracing::Span| {
261                    tracing::error!(
262                        target: "feagi-api", 
263                        "Request failed: error_class={:?}, latency={:?}", 
264                        error, latency
265                    );
266                })
267        )
268}
269
270/// Create V1 API router - Match Python structure EXACTLY
271/// Format: /v1/{module}/{snake_case_endpoint}
272fn create_v1_router() -> Router<ApiState> {
273    use crate::endpoints::agent;
274    use crate::endpoints::burst_engine;
275    use crate::endpoints::connectome;
276    use crate::endpoints::cortical_area;
277    use crate::endpoints::cortical_mapping;
278    use crate::endpoints::evolution;
279    use crate::endpoints::genome;
280    use crate::endpoints::input;
281    use crate::endpoints::insight;
282    use crate::endpoints::monitoring;
283    use crate::endpoints::morphology;
284    use crate::endpoints::network;
285    use crate::endpoints::neuroplasticity;
286    use crate::endpoints::outputs;
287    use crate::endpoints::physiology;
288    use crate::endpoints::region;
289    use crate::endpoints::simulation;
290    use crate::endpoints::system;
291    use crate::endpoints::training;
292    use crate::endpoints::visualization; //use crate::endpoints::{agent, system};
293
294    Router::new()
295        // ===== AGENT MODULE =====
296        .route(
297            "/agent/register",
298            axum::routing::post(agent::register_agent),
299        )
300        .route("/agent/heartbeat", axum::routing::post(agent::heartbeat))
301        .route("/agent/list", get(agent::list_agents))
302        .route("/agent/properties", get(agent::get_agent_properties))
303        .route("/agent/shared_mem", get(agent::get_shared_memory))
304        .route(
305            "/agent/deregister",
306            axum::routing::delete(agent::deregister_agent),
307        )
308        .route(
309            "/agent/manual_stimulation",
310            axum::routing::post(agent::manual_stimulation),
311        )
312        .route(
313            "/agent/fq_sampler_status",
314            get(agent::get_fq_sampler_status),
315        )
316        .route("/agent/capabilities", get(agent::get_capabilities))
317        .route(
318            "/agent/capabilities/all",
319            get(agent::get_all_agent_capabilities),
320        )
321        .route("/agent/info/{agent_id}", get(agent::get_agent_info))
322        .route(
323            "/agent/properties/{agent_id}",
324            get(agent::get_agent_properties_path),
325        )
326        .route(
327            "/agent/configure",
328            axum::routing::post(agent::post_configure),
329        )
330        .route(
331            "/agent/{agent_id}/device_registrations",
332            get(agent::export_device_registrations).post(agent::import_device_registrations),
333        )
334        // ===== SYSTEM MODULE (21 endpoints) =====
335        .route("/system/health_check", get(system::get_health_check))
336        .route(
337            "/system/cortical_area_visualization_skip_rate",
338            get(system::get_cortical_area_visualization_skip_rate)
339                .put(system::set_cortical_area_visualization_skip_rate),
340        )
341        .route(
342            "/system/cortical_area_visualization_suppression_threshold",
343            get(system::get_cortical_area_visualization_suppression_threshold)
344                .put(system::set_cortical_area_visualization_suppression_threshold),
345        )
346        .route("/system/version", get(system::get_version))
347        .route("/system/versions", get(system::get_versions))
348        .route("/system/configuration", get(system::get_configuration))
349        .route(
350            "/system/user_preferences",
351            get(system::get_user_preferences).put(system::put_user_preferences),
352        )
353        .route(
354            "/system/cortical_area_types",
355            get(system::get_cortical_area_types_list),
356        )
357        .route(
358            "/system/enable_visualization_fq_sampler",
359            axum::routing::post(system::post_enable_visualization_fq_sampler),
360        )
361        .route(
362            "/system/disable_visualization_fq_sampler",
363            axum::routing::post(system::post_disable_visualization_fq_sampler),
364        )
365        .route("/system/fcl_status", get(system::get_fcl_status_system))
366        .route(
367            "/system/fcl_reset",
368            axum::routing::post(system::post_fcl_reset_system),
369        )
370        .route("/system/processes", get(system::get_processes))
371        .route("/system/unique_logs", get(system::get_unique_logs))
372        .route("/system/logs", axum::routing::post(system::post_logs))
373        .route(
374            "/system/beacon/subscribers",
375            get(system::get_beacon_subscribers),
376        )
377        .route(
378            "/system/beacon/subscribe",
379            axum::routing::post(system::post_beacon_subscribe),
380        )
381        .route(
382            "/system/beacon/unsubscribe",
383            axum::routing::delete(system::delete_beacon_unsubscribe),
384        )
385        .route(
386            "/system/global_activity_visualization",
387            get(system::get_global_activity_visualization)
388                .put(system::put_global_activity_visualization),
389        )
390        .route(
391            "/system/circuit_library_path",
392            axum::routing::post(system::post_circuit_library_path),
393        )
394        .route("/system/db/influxdb/test", get(system::get_influxdb_test))
395        .route(
396            "/system/register",
397            axum::routing::post(system::post_register_system),
398        )
399        // ===== CORTICAL_AREA MODULE (25 endpoints) =====
400        .route("/cortical_area/ipu", get(cortical_area::get_ipu))
401        .route(
402            "/cortical_area/ipu/types",
403            get(cortical_area::get_ipu_types),
404        )
405        .route("/cortical_area/opu", get(cortical_area::get_opu))
406        .route(
407            "/cortical_area/opu/types",
408            get(cortical_area::get_opu_types),
409        )
410        .route(
411            "/cortical_area/cortical_area_id_list",
412            get(cortical_area::get_cortical_area_id_list),
413        )
414        .route(
415            "/cortical_area/cortical_area_name_list",
416            get(cortical_area::get_cortical_area_name_list),
417        )
418        .route(
419            "/cortical_area/cortical_id_name_mapping",
420            get(cortical_area::get_cortical_id_name_mapping),
421        )
422        .route(
423            "/cortical_area/cortical_types",
424            get(cortical_area::get_cortical_types),
425        )
426        .route(
427            "/cortical_area/cortical_map_detailed",
428            get(cortical_area::get_cortical_map_detailed),
429        )
430        .route(
431            "/cortical_area/cortical_locations_2d",
432            get(cortical_area::get_cortical_locations_2d),
433        )
434        .route(
435            "/cortical_area/cortical_area/geometry",
436            get(cortical_area::get_cortical_area_geometry),
437        )
438        .route(
439            "/cortical_area/cortical_visibility",
440            get(cortical_area::get_cortical_visibility),
441        )
442        .route(
443            "/cortical_area/cortical_name_location",
444            axum::routing::post(cortical_area::post_cortical_name_location),
445        )
446        .route(
447            "/cortical_area/cortical_area_properties",
448            axum::routing::post(cortical_area::post_cortical_area_properties),
449        )
450        .route(
451            "/cortical_area/multi/cortical_area_properties",
452            axum::routing::post(cortical_area::post_multi_cortical_area_properties),
453        )
454        .route(
455            "/cortical_area/cortical_area",
456            axum::routing::post(cortical_area::post_cortical_area)
457                .put(cortical_area::put_cortical_area)
458                .delete(cortical_area::delete_cortical_area),
459        )
460        .route(
461            "/cortical_area/custom_cortical_area",
462            axum::routing::post(cortical_area::post_custom_cortical_area),
463        )
464        .route(
465            "/cortical_area/clone",
466            axum::routing::post(cortical_area::post_clone),
467        )
468        .route(
469            "/cortical_area/multi/cortical_area",
470            put(cortical_area::put_multi_cortical_area)
471                .delete(cortical_area::delete_multi_cortical_area),
472        )
473        .route("/cortical_area/coord_2d", put(cortical_area::put_coord_2d))
474        .route(
475            "/cortical_area/suppress_cortical_visibility",
476            put(cortical_area::put_suppress_cortical_visibility),
477        )
478        .route("/cortical_area/reset", put(cortical_area::put_reset))
479        .route(
480            "/cortical_area/visualization",
481            get(cortical_area::get_visualization),
482        )
483        .route(
484            "/cortical_area/batch_operations",
485            axum::routing::post(cortical_area::post_batch_operations),
486        )
487        .route("/cortical_area/ipu/list", get(cortical_area::get_ipu_list))
488        .route("/cortical_area/opu/list", get(cortical_area::get_opu_list))
489        .route(
490            "/cortical_area/coordinates_3d",
491            put(cortical_area::put_coordinates_3d),
492        )
493        .route(
494            "/cortical_area/bulk_delete",
495            axum::routing::delete(cortical_area::delete_bulk),
496        )
497        .route(
498            "/cortical_area/resize",
499            axum::routing::post(cortical_area::post_resize),
500        )
501        .route(
502            "/cortical_area/reposition",
503            axum::routing::post(cortical_area::post_reposition),
504        )
505        .route(
506            "/cortical_area/voxel_neurons",
507            axum::routing::post(cortical_area::post_voxel_neurons),
508        )
509        .route(
510            "/cortical_area/cortical_area_index_list",
511            get(cortical_area::get_cortical_area_index_list),
512        )
513        .route(
514            "/cortical_area/cortical_idx_mapping",
515            get(cortical_area::get_cortical_idx_mapping),
516        )
517        .route(
518            "/cortical_area/mapping_restrictions",
519            get(cortical_area::get_mapping_restrictions_query)
520                .post(cortical_area::post_mapping_restrictions),
521        )
522        .route(
523            "/cortical_area/:cortical_id/memory_usage",
524            get(cortical_area::get_memory_usage),
525        )
526        .route(
527            "/cortical_area/:cortical_id/neuron_count",
528            get(cortical_area::get_area_neuron_count),
529        )
530        .route(
531            "/cortical_area/cortical_type_options",
532            axum::routing::post(cortical_area::post_cortical_type_options),
533        )
534        .route(
535            "/cortical_area/mapping_restrictions_between_areas",
536            axum::routing::post(cortical_area::post_mapping_restrictions_between_areas),
537        )
538        .route("/cortical_area/coord_3d", put(cortical_area::put_coord_3d))
539        // ===== MORPHOLOGY MODULE (14 endpoints) =====
540        .route(
541            "/morphology/morphology_list",
542            get(morphology::get_morphology_list),
543        )
544        .route(
545            "/morphology/morphology_types",
546            get(morphology::get_morphology_types),
547        )
548        .route("/morphology/list/types", get(morphology::get_list_types))
549        .route(
550            "/morphology/morphologies",
551            get(morphology::get_morphologies),
552        )
553        .route(
554            "/morphology/morphology",
555            axum::routing::post(morphology::post_morphology)
556                .put(morphology::put_morphology)
557                .delete(morphology::delete_morphology_by_name),
558        )
559        .route(
560            "/morphology/morphology_properties",
561            axum::routing::post(morphology::post_morphology_properties),
562        )
563        .route(
564            "/morphology/morphology_usage",
565            axum::routing::post(morphology::post_morphology_usage),
566        )
567        .route("/morphology/list", get(morphology::get_list))
568        .route("/morphology/info/:morphology_id", get(morphology::get_info))
569        .route(
570            "/morphology/create",
571            axum::routing::post(morphology::post_create),
572        )
573        .route(
574            "/morphology/update",
575            axum::routing::put(morphology::put_update),
576        )
577        .route(
578            "/morphology/delete/:morphology_id",
579            axum::routing::delete(morphology::delete_morphology),
580        )
581        // ===== REGION MODULE (12 endpoints) =====
582        .route("/region/regions_members", get(region::get_regions_members))
583        .route(
584            "/region/region",
585            axum::routing::post(region::post_region)
586                .put(region::put_region)
587                .delete(region::delete_region),
588        )
589        .route("/region/clone", axum::routing::post(region::post_clone))
590        .route(
591            "/region/relocate_members",
592            put(region::put_relocate_members),
593        )
594        .route(
595            "/region/region_and_members",
596            axum::routing::delete(region::delete_region_and_members),
597        )
598        .route("/region/regions", get(region::get_regions))
599        .route("/region/region_titles", get(region::get_region_titles))
600        .route("/region/region/:region_id", get(region::get_region_detail))
601        .route(
602            "/region/change_region_parent",
603            put(region::put_change_region_parent),
604        )
605        .route(
606            "/region/change_cortical_area_region",
607            put(region::put_change_cortical_area_region),
608        )
609        // ===== CORTICAL_MAPPING MODULE (8 endpoints) =====
610        .route(
611            "/cortical_mapping/afferents",
612            axum::routing::post(cortical_mapping::post_afferents),
613        )
614        .route(
615            "/cortical_mapping/efferents",
616            axum::routing::post(cortical_mapping::post_efferents),
617        )
618        .route(
619            "/cortical_mapping/mapping_properties",
620            axum::routing::post(cortical_mapping::post_mapping_properties)
621                .put(cortical_mapping::put_mapping_properties),
622        )
623        .route(
624            "/cortical_mapping/mapping",
625            get(cortical_mapping::get_mapping).delete(cortical_mapping::delete_mapping),
626        )
627        .route(
628            "/cortical_mapping/mapping_list",
629            get(cortical_mapping::get_mapping_list),
630        )
631        .route(
632            "/cortical_mapping/batch_update",
633            axum::routing::post(cortical_mapping::post_batch_update),
634        )
635        .route(
636            "/cortical_mapping/mapping",
637            axum::routing::post(cortical_mapping::post_mapping).put(cortical_mapping::put_mapping),
638        )
639        // ===== CONNECTOME MODULE (21 endpoints) =====
640        .route(
641            "/connectome/cortical_areas/list/detailed",
642            get(connectome::get_cortical_areas_list_detailed),
643        )
644        .route(
645            "/connectome/properties/dimensions",
646            get(connectome::get_properties_dimensions),
647        )
648        .route(
649            "/connectome/properties/mappings",
650            get(connectome::get_properties_mappings),
651        )
652        .route("/connectome/snapshot", get(connectome::get_snapshot))
653        .route("/connectome/stats", get(connectome::get_stats))
654        .route(
655            "/connectome/batch_neuron_operations",
656            axum::routing::post(connectome::post_batch_neuron_operations),
657        )
658        .route(
659            "/connectome/batch_synapse_operations",
660            axum::routing::post(connectome::post_batch_synapse_operations),
661        )
662        .route(
663            "/connectome/neuron_count",
664            get(connectome::get_neuron_count),
665        )
666        .route(
667            "/connectome/synapse_count",
668            get(connectome::get_synapse_count),
669        )
670        .route("/connectome/paths", get(connectome::get_paths))
671        .route(
672            "/connectome/cumulative_stats",
673            get(connectome::get_cumulative_stats),
674        )
675        .route(
676            "/connectome/area_details",
677            get(connectome::get_area_details),
678        )
679        .route(
680            "/connectome/rebuild",
681            axum::routing::post(connectome::post_rebuild),
682        )
683        .route("/connectome/structure", get(connectome::get_structure))
684        .route(
685            "/connectome/clear",
686            axum::routing::post(connectome::post_clear),
687        )
688        .route("/connectome/validation", get(connectome::get_validation))
689        .route("/connectome/topology", get(connectome::get_topology))
690        .route(
691            "/connectome/optimize",
692            axum::routing::post(connectome::post_optimize),
693        )
694        .route(
695            "/connectome/connectivity_matrix",
696            get(connectome::get_connectivity_matrix),
697        )
698        .route(
699            "/connectome/neurons/batch",
700            axum::routing::post(connectome::post_neurons_batch),
701        )
702        .route(
703            "/connectome/synapses/batch",
704            axum::routing::post(connectome::post_synapses_batch),
705        )
706        .route(
707            "/connectome/cortical_areas/list/summary",
708            get(connectome::get_cortical_areas_list_summary),
709        )
710        .route(
711            "/connectome/cortical_areas/list/transforming",
712            get(connectome::get_cortical_areas_list_transforming),
713        )
714        .route(
715            "/connectome/cortical_area/list/types",
716            get(connectome::get_cortical_area_list_types),
717        )
718        .route(
719            "/connectome/cortical_area/:cortical_id/neurons",
720            get(connectome::get_cortical_area_neurons),
721        )
722        .route(
723            "/connectome/:cortical_area_id/synapses",
724            get(connectome::get_area_synapses),
725        )
726        .route(
727            "/connectome/cortical_info/:cortical_area",
728            get(connectome::get_cortical_info),
729        )
730        .route(
731            "/connectome/stats/cortical/cumulative/:cortical_area",
732            get(connectome::get_stats_cortical_cumulative),
733        )
734        .route(
735            "/connectome/neuron/:neuron_id/properties",
736            get(connectome::get_neuron_properties_by_id),
737        )
738        .route(
739            "/connectome/neuron_properties",
740            get(connectome::get_neuron_properties_query),
741        )
742        .route(
743            "/connectome/neuron_properties_at",
744            get(connectome::get_neuron_properties_at_query),
745        )
746        .route(
747            "/connectome/area_neurons",
748            get(connectome::get_area_neurons_query),
749        )
750        .route(
751            "/connectome/fire_queue/:cortical_area",
752            get(connectome::get_fire_queue_area),
753        )
754        .route(
755            "/connectome/plasticity",
756            get(connectome::get_plasticity_info),
757        )
758        .route("/connectome/path", get(connectome::get_path_query))
759        .route(
760            "/connectome/download",
761            get(connectome::get_download_connectome),
762        )
763        .route(
764            "/connectome/download-cortical-area/:cortical_area",
765            get(connectome::get_download_cortical_area),
766        )
767        .route(
768            "/connectome/upload",
769            axum::routing::post(connectome::post_upload_connectome),
770        )
771        .route(
772            "/connectome/upload-cortical-area",
773            axum::routing::post(connectome::post_upload_cortical_area),
774        )
775        // ===== BURST_ENGINE MODULE (14 endpoints) =====
776        .route(
777            "/burst_engine/simulation_timestep",
778            get(burst_engine::get_simulation_timestep).post(burst_engine::post_simulation_timestep),
779        )
780        .route("/burst_engine/fcl", get(burst_engine::get_fcl))
781        .route(
782            "/burst_engine/fcl/neuron",
783            get(burst_engine::get_fcl_neuron),
784        )
785        .route(
786            "/burst_engine/fire_queue",
787            get(burst_engine::get_fire_queue),
788        )
789        .route(
790            "/burst_engine/fire_queue/neuron",
791            get(burst_engine::get_fire_queue_neuron),
792        )
793        .route(
794            "/burst_engine/fcl_reset",
795            axum::routing::post(burst_engine::post_fcl_reset),
796        )
797        .route(
798            "/burst_engine/fcl_status",
799            get(burst_engine::get_fcl_status),
800        )
801        .route(
802            "/burst_engine/fcl_sampler/config",
803            get(burst_engine::get_fcl_sampler_config).post(burst_engine::post_fcl_sampler_config),
804        )
805        .route(
806            "/burst_engine/fcl_sampler/area/:area_id/sample_rate",
807            get(burst_engine::get_area_fcl_sample_rate)
808                .post(burst_engine::post_area_fcl_sample_rate),
809        )
810        .route(
811            "/burst_engine/fire_ledger/default_window_size",
812            get(burst_engine::get_fire_ledger_default_window_size)
813                .put(burst_engine::put_fire_ledger_default_window_size),
814        )
815        .route(
816            "/burst_engine/fire_ledger/areas_window_config",
817            get(burst_engine::get_fire_ledger_areas_window_config),
818        )
819        .route("/burst_engine/stats", get(burst_engine::get_stats))
820        .route("/burst_engine/status", get(burst_engine::get_status))
821        .route(
822            "/burst_engine/control",
823            axum::routing::post(burst_engine::post_control),
824        )
825        .route(
826            "/burst_engine/burst_counter",
827            get(burst_engine::get_burst_counter),
828        )
829        .route(
830            "/burst_engine/start",
831            axum::routing::post(burst_engine::post_start),
832        )
833        .route(
834            "/burst_engine/stop",
835            axum::routing::post(burst_engine::post_stop),
836        )
837        .route(
838            "/burst_engine/hold",
839            axum::routing::post(burst_engine::post_hold),
840        )
841        .route(
842            "/burst_engine/resume",
843            axum::routing::post(burst_engine::post_resume),
844        )
845        .route(
846            "/burst_engine/config",
847            get(burst_engine::get_config).put(burst_engine::put_config),
848        )
849        .route(
850            "/burst_engine/fire_ledger/area/:area_id/window_size",
851            get(burst_engine::get_fire_ledger_area_window_size)
852                .put(burst_engine::put_fire_ledger_area_window_size),
853        )
854        .route(
855            "/burst_engine/fire_ledger/area/:area_id/history",
856            get(burst_engine::get_fire_ledger_history),
857        )
858        .route(
859            "/burst_engine/membrane_potentials",
860            get(burst_engine::get_membrane_potentials).put(burst_engine::put_membrane_potentials),
861        )
862        .route(
863            "/burst_engine/frequency_status",
864            get(burst_engine::get_frequency_status),
865        )
866        .route(
867            "/burst_engine/measure_frequency",
868            axum::routing::post(burst_engine::post_measure_frequency),
869        )
870        .route(
871            "/burst_engine/frequency_history",
872            get(burst_engine::get_frequency_history),
873        )
874        .route(
875            "/burst_engine/force_connectome_integration",
876            axum::routing::post(burst_engine::post_force_connectome_integration),
877        )
878        // ===== GENOME MODULE (22 endpoints) =====
879        .route("/genome/file_name", get(genome::get_file_name))
880        .route("/genome/circuits", get(genome::get_circuits))
881        .route(
882            "/genome/amalgamation_destination",
883            axum::routing::post(genome::post_amalgamation_destination),
884        )
885        .route(
886            "/genome/amalgamation_cancellation",
887            axum::routing::delete(genome::delete_amalgamation_cancellation),
888        )
889        .route(
890            "/feagi/genome/append",
891            axum::routing::post(genome::post_genome_append),
892        )
893        .route(
894            "/genome/upload/barebones",
895            axum::routing::post(genome::post_upload_barebones_genome),
896        )
897        .route(
898            "/genome/upload/essential",
899            axum::routing::post(genome::post_upload_essential_genome),
900        )
901        .route("/genome/name", get(genome::get_name))
902        .route("/genome/timestamp", get(genome::get_timestamp))
903        .route("/genome/save", axum::routing::post(genome::post_save))
904        .route("/genome/load", axum::routing::post(genome::post_load))
905        .route("/genome/upload", axum::routing::post(genome::post_upload))
906        .route("/genome/download", get(genome::get_download))
907        .route("/genome/properties", get(genome::get_properties))
908        .route(
909            "/genome/validate",
910            axum::routing::post(genome::post_validate),
911        )
912        .route(
913            "/genome/transform",
914            axum::routing::post(genome::post_transform),
915        )
916        .route("/genome/clone", axum::routing::post(genome::post_clone))
917        .route("/genome/reset", axum::routing::post(genome::post_reset))
918        .route("/genome/metadata", get(genome::get_metadata))
919        .route("/genome/merge", axum::routing::post(genome::post_merge))
920        .route("/genome/diff", get(genome::get_diff))
921        .route(
922            "/genome/export_format",
923            axum::routing::post(genome::post_export_format),
924        )
925        .route("/genome/amalgamation", get(genome::get_amalgamation))
926        .route(
927            "/genome/amalgamation_history",
928            get(genome::get_amalgamation_history_exact),
929        )
930        .route(
931            "/genome/cortical_template",
932            get(genome::get_cortical_template),
933        )
934        .route("/genome/defaults/files", get(genome::get_defaults_files))
935        .route("/genome/download_region", get(genome::get_download_region))
936        .route("/genome/genome_number", get(genome::get_genome_number))
937        .route(
938            "/genome/amalgamation_by_filename",
939            axum::routing::post(genome::post_amalgamation_by_filename),
940        )
941        .route(
942            "/genome/amalgamation_by_payload",
943            axum::routing::post(genome::post_amalgamation_by_payload),
944        )
945        .route(
946            "/genome/amalgamation_by_upload",
947            axum::routing::post(genome::post_amalgamation_by_upload),
948        )
949        .route(
950            "/genome/append-file",
951            axum::routing::post(genome::post_append_file),
952        )
953        .route(
954            "/genome/upload/file",
955            axum::routing::post(genome::post_upload_file),
956        )
957        .route(
958            "/genome/upload/file/edit",
959            axum::routing::post(genome::post_upload_file_edit),
960        )
961        .route(
962            "/genome/upload/string",
963            axum::routing::post(genome::post_upload_string),
964        )
965        // ===== NEUROPLASTICITY MODULE (7 endpoints) =====
966        .route(
967            "/neuroplasticity/plasticity_queue_depth",
968            get(neuroplasticity::get_plasticity_queue_depth)
969                .put(neuroplasticity::put_plasticity_queue_depth),
970        )
971        .route("/neuroplasticity/status", get(neuroplasticity::get_status))
972        .route(
973            "/neuroplasticity/transforming",
974            get(neuroplasticity::get_transforming),
975        )
976        .route(
977            "/neuroplasticity/configure",
978            axum::routing::post(neuroplasticity::post_configure),
979        )
980        .route(
981            "/neuroplasticity/enable/:area_id",
982            axum::routing::post(neuroplasticity::post_enable_area),
983        )
984        .route(
985            "/neuroplasticity/disable/:area_id",
986            axum::routing::post(neuroplasticity::post_disable_area),
987        )
988        // ===== INSIGHT MODULE (6 endpoints) =====
989        .route(
990            "/insight/neurons/membrane_potential_status",
991            axum::routing::post(insight::post_neurons_membrane_potential_status),
992        )
993        .route(
994            "/insight/neuron/synaptic_potential_status",
995            axum::routing::post(insight::post_neuron_synaptic_potential_status),
996        )
997        .route(
998            "/insight/neurons/membrane_potential_set",
999            axum::routing::post(insight::post_neurons_membrane_potential_set),
1000        )
1001        .route(
1002            "/insight/neuron/synaptic_potential_set",
1003            axum::routing::post(insight::post_neuron_synaptic_potential_set),
1004        )
1005        .route("/insight/analytics", get(insight::get_analytics))
1006        .route("/insight/data", get(insight::get_data))
1007        // ===== INPUT MODULE (4 endpoints) =====
1008        .route(
1009            "/input/vision",
1010            get(input::get_vision).post(input::post_vision),
1011        )
1012        .route("/input/sources", get(input::get_sources))
1013        .route(
1014            "/input/configure",
1015            axum::routing::post(input::post_configure),
1016        )
1017        // ===== OUTPUTS MODULE (2 endpoints) - Python uses /v1/output (singular)
1018        .route("/output/targets", get(outputs::get_targets))
1019        .route(
1020            "/output/configure",
1021            axum::routing::post(outputs::post_configure),
1022        )
1023        // ===== PHYSIOLOGY MODULE (2 endpoints) =====
1024        .route(
1025            "/physiology/",
1026            get(physiology::get_physiology).put(physiology::put_physiology),
1027        )
1028        // ===== SIMULATION MODULE (6 endpoints) =====
1029        .route(
1030            "/simulation/upload/string",
1031            axum::routing::post(simulation::post_stimulation_upload),
1032        )
1033        .route(
1034            "/simulation/reset",
1035            axum::routing::post(simulation::post_reset),
1036        )
1037        .route("/simulation/status", get(simulation::get_status))
1038        .route("/simulation/stats", get(simulation::get_stats))
1039        .route(
1040            "/simulation/config",
1041            axum::routing::post(simulation::post_config),
1042        )
1043        // ===== TRAINING MODULE (25 endpoints) =====
1044        .route("/training/shock", axum::routing::post(training::post_shock))
1045        .route("/training/shock/options", get(training::get_shock_options))
1046        .route("/training/shock/status", get(training::get_shock_status))
1047        .route(
1048            "/training/shock/activate",
1049            axum::routing::post(training::post_shock_activate),
1050        )
1051        .route(
1052            "/training/reward/intensity",
1053            axum::routing::post(training::post_reward_intensity),
1054        )
1055        .route(
1056            "/training/reward",
1057            axum::routing::post(training::post_reward),
1058        )
1059        .route(
1060            "/training/punishment/intensity",
1061            axum::routing::post(training::post_punishment_intensity),
1062        )
1063        .route(
1064            "/training/punishment",
1065            axum::routing::post(training::post_punishment),
1066        )
1067        .route(
1068            "/training/gameover",
1069            axum::routing::post(training::post_gameover),
1070        )
1071        .route("/training/brain_fitness", get(training::get_brain_fitness))
1072        .route(
1073            "/training/fitness_criteria",
1074            get(training::get_fitness_criteria)
1075                .put(training::put_fitness_criteria)
1076                .post(training::post_fitness_criteria),
1077        )
1078        .route(
1079            "/training/fitness_stats",
1080            get(training::get_fitness_stats)
1081                .put(training::put_fitness_stats)
1082                .delete(training::delete_fitness_stats),
1083        )
1084        .route(
1085            "/training/reset_fitness_stats",
1086            axum::routing::delete(training::delete_reset_fitness_stats),
1087        )
1088        .route(
1089            "/training/training_report",
1090            get(training::get_training_report),
1091        )
1092        .route("/training/status", get(training::get_status))
1093        .route("/training/stats", get(training::get_stats))
1094        .route(
1095            "/training/config",
1096            axum::routing::post(training::post_config),
1097        )
1098        // ===== VISUALIZATION MODULE (4 endpoints) =====
1099        .route(
1100            "/visualization/register_client",
1101            axum::routing::post(visualization::post_register_client),
1102        )
1103        .route(
1104            "/visualization/unregister_client",
1105            axum::routing::post(visualization::post_unregister_client),
1106        )
1107        .route(
1108            "/visualization/heartbeat",
1109            axum::routing::post(visualization::post_heartbeat),
1110        )
1111        .route("/visualization/status", get(visualization::get_status))
1112        // ===== MONITORING MODULE (4 endpoints) =====
1113        .route("/monitoring/status", get(monitoring::get_status))
1114        .route("/monitoring/metrics", get(monitoring::get_metrics))
1115        .route("/monitoring/data", get(monitoring::get_data))
1116        .route("/monitoring/performance", get(monitoring::get_performance))
1117        // ===== EVOLUTION MODULE (3 endpoints) =====
1118        .route("/evolution/status", get(evolution::get_status))
1119        .route(
1120            "/evolution/config",
1121            axum::routing::post(evolution::post_config),
1122        )
1123        // ===== SNAPSHOT MODULE (12 endpoints) =====
1124        // TODO: Implement snapshot endpoints
1125        // .route("/snapshot/create", axum::routing::post(snapshot::post_create))
1126        // .route("/snapshot/restore", axum::routing::post(snapshot::post_restore))
1127        // .route("/snapshot/", get(snapshot::get_list))
1128        // .route("/snapshot/:snapshot_id", axum::routing::delete(snapshot::delete_snapshot))
1129        // .route("/snapshot/:snapshot_id/artifact/:fmt", get(snapshot::get_artifact))
1130        // .route("/snapshot/compare", axum::routing::post(snapshot::post_compare))
1131        // .route("/snapshot/upload", axum::routing::post(snapshot::post_upload))
1132        // // Python uses /v1/snapshots/* (note the S)
1133        // .route("/snapshots/connectome", axum::routing::post(snapshot::post_snapshots_connectome))
1134        // .route("/snapshots/connectome/:snapshot_id/restore", axum::routing::post(snapshot::post_snapshots_connectome_restore))
1135        // .route("/snapshots/:snapshot_id/restore", axum::routing::post(snapshot::post_snapshots_restore))
1136        // .route("/snapshots/:snapshot_id", axum::routing::delete(snapshot::delete_snapshots_by_id))
1137        // .route("/snapshots/:snapshot_id/artifact/:fmt", get(snapshot::get_snapshots_artifact))
1138        // ===== NETWORK MODULE (3 endpoints) =====
1139        .route("/network/status", get(network::get_status))
1140        .route("/network/config", axum::routing::post(network::post_config))
1141        .route(
1142            "/network/connection_info",
1143            get(network::get_connection_info),
1144        )
1145}
1146
1147/// OpenAPI spec handler
1148#[allow(dead_code)] // In development - will be wired to OpenAPI route
1149async fn openapi_spec() -> Json<utoipa::openapi::OpenApi> {
1150    Json(ApiDoc::openapi())
1151}
1152
1153// ============================================================================
1154// CORS CONFIGURATION
1155// ============================================================================
1156
1157/// Create CORS layer for the API
1158///
1159/// TODO: Configure for production:
1160/// - Restrict allowed origins
1161/// - Allowed methods restricted
1162/// - Credentials support as needed
1163fn create_cors_layer() -> CorsLayer {
1164    CorsLayer::new()
1165        .allow_origin(Any)
1166        .allow_methods(Any)
1167        .allow_headers(Any)
1168}
1169
1170/// Middleware to log request and response bodies for debugging
1171async fn log_request_response_bodies(
1172    request: Request<Body>,
1173    next: Next,
1174) -> Result<Response, StatusCode> {
1175    let (parts, body) = request.into_parts();
1176
1177    // Only log bodies for POST/PUT/PATCH/DELETE requests
1178    let should_log_request = matches!(parts.method.as_str(), "POST" | "PUT" | "PATCH" | "DELETE");
1179
1180    let body_bytes = if should_log_request {
1181        // Collect body bytes
1182        match body.collect().await {
1183            Ok(collected) => {
1184                let bytes = collected.to_bytes();
1185                // Log request body if it's JSON
1186                if let Ok(body_str) = String::from_utf8(bytes.to_vec()) {
1187                    if !body_str.is_empty() {
1188                        tracing::trace!(target: "feagi-api", "Request body: {}", body_str);
1189                    }
1190                }
1191                bytes
1192            }
1193            Err(_) => {
1194                return Err(StatusCode::INTERNAL_SERVER_ERROR);
1195            }
1196        }
1197    } else {
1198        axum::body::Bytes::new()
1199    };
1200
1201    // Reconstruct request with original body
1202    let request = Request::from_parts(parts, Body::from(body_bytes));
1203
1204    // Call the next handler
1205    let response = next.run(request).await;
1206
1207    // Log response body
1208    let (parts, body) = response.into_parts();
1209
1210    match body.collect().await {
1211        Ok(collected) => {
1212            let bytes = collected.to_bytes();
1213            // Log response body if it's JSON and not too large
1214            if bytes.len() < 10000 {
1215                // Only log responses < 10KB
1216                if let Ok(body_str) = String::from_utf8(bytes.to_vec()) {
1217                    if !body_str.is_empty() && body_str.starts_with('{') {
1218                        tracing::trace!(target: "feagi-api", "Response body: {}", body_str);
1219                    }
1220                }
1221            }
1222            // Reconstruct response
1223            Ok(Response::from_parts(parts, Body::from(bytes)))
1224        }
1225        Err(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
1226    }
1227}
1228
1229// ============================================================================
1230// HELPER HANDLERS
1231// ============================================================================
1232
1233/// Root redirect handler - redirects to Swagger UI
1234async fn root_redirect() -> Redirect {
1235    Redirect::permanent("/swagger-ui/")
1236}
1237
1238// Custom Swagger UI with FEAGI branding and dark/light themes
1239// Embedded from templates/custom-swagger-ui.html at compile time
1240async fn custom_swagger_ui() -> Html<&'static str> {
1241    const CUSTOM_SWAGGER_HTML: &str = include_str!("../../../templates/custom-swagger-ui.html");
1242    Html(CUSTOM_SWAGGER_HTML)
1243}
1244
1245// ============================================================================
1246// PLACEHOLDER HANDLERS (for endpoints not yet implemented)
1247// ============================================================================
1248
1249/// Placeholder handler for unimplemented endpoints
1250/// Returns 501 Not Implemented with a clear message
1251#[allow(dead_code)] // In development - will be used for placeholder routes
1252async fn placeholder_handler(State(_state): State<ApiState>) -> Response {
1253    (
1254        StatusCode::NOT_IMPLEMENTED,
1255        Json(serde_json::json!({
1256            "error": "Not yet implemented",
1257            "message": "This endpoint is registered but not yet implemented in Rust. See Python implementation."
1258        }))
1259    ).into_response()
1260}
1261
1262/// Placeholder health check - returns basic response
1263#[allow(dead_code)] // In development - will be used for basic health route
1264async fn placeholder_health_check(State(_state): State<ApiState>) -> Response {
1265    (
1266        StatusCode::OK,
1267        Json(serde_json::json!({
1268            "status": "ok",
1269            "message": "Health check placeholder - Python-compatible path structure confirmed",
1270            "burst_engine": false,
1271            "brain_readiness": false
1272        })),
1273    )
1274        .into_response()
1275}