feagi-api 0.0.8

FEAGI REST API layer with HTTP and ZMQ transport adapters
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
// Copyright 2025 Neuraville Inc.
// Licensed under the Apache License, Version 2.0

//! Agent endpoints - exact port from Python `/v1/agent/*` routes
//!
//! These endpoints match the Python implementation at:
//! feagi-py/feagi/api/v1/feagi_agent.py

use std::collections::HashMap;

use crate::common::ApiState;
use crate::common::{ApiError, ApiResult, Json, Path, Query, State};
use crate::v1::agent_dtos::*;
use feagi_services::traits::agent_service::HeartbeatRequest as ServiceHeartbeatRequest;
use feagi_services::traits::agent_service::ManualStimulationMode;
use tracing::{info, warn};

#[cfg(feature = "feagi-agent")]
use crate::common::agent_registration::auto_create_cortical_areas_from_device_registrations as auto_create_cortical_areas_shared;
#[cfg(feature = "feagi-agent")]
use feagi_agent::{AgentCapabilities as RegistrationCapabilities, AuthToken};
#[cfg(feature = "feagi-agent")]
use feagi_io::AgentID;
#[cfg(feature = "feagi-agent")]
#[allow(dead_code)]
fn parse_auth_token(request: &AgentRegistrationRequest) -> ApiResult<AuthToken> {
    let token_b64 = request
        .auth_token
        .as_deref()
        .ok_or_else(|| ApiError::invalid_input("Missing auth_token for registration"))?;
    AuthToken::from_base64(token_b64).ok_or_else(|| {
        ApiError::invalid_input("Invalid auth_token (expected base64 32-byte token)")
    })
}

#[cfg(feature = "feagi-agent")]
#[allow(dead_code)]
fn derive_capabilities_from_device_registrations(
    device_registrations: &serde_json::Value,
) -> ApiResult<Vec<RegistrationCapabilities>> {
    let obj = device_registrations
        .as_object()
        .ok_or_else(|| ApiError::invalid_input("device_registrations must be a JSON object"))?;

    let input_units = obj
        .get("input_units_and_encoder_properties")
        .and_then(|v| v.as_object());
    let output_units = obj
        .get("output_units_and_decoder_properties")
        .and_then(|v| v.as_object());
    let feedbacks = obj.get("feedbacks").and_then(|v| v.as_object());

    let mut capabilities = Vec::new();
    if input_units.map(|m| !m.is_empty()).unwrap_or(false) {
        capabilities.push(RegistrationCapabilities::SendSensorData);
    }
    if output_units.map(|m| !m.is_empty()).unwrap_or(false) {
        capabilities.push(RegistrationCapabilities::ReceiveMotorData);
    }
    if feedbacks.map(|m| !m.is_empty()).unwrap_or(false) {
        capabilities.push(RegistrationCapabilities::ReceiveNeuronVisualizations);
    }

    if capabilities.is_empty() {
        return Err(ApiError::invalid_input(
            "device_registrations does not declare any input/output/feedback units",
        ));
    }

    Ok(capabilities)
}

/// Derive capabilities for visualization-only agents (no device_registrations).
/// Requires `capabilities.visualization` with valid `rate_hz`. Auth is still required by caller.
#[cfg(feature = "feagi-agent")]
#[allow(dead_code)]
fn derive_capabilities_from_visualization_capability(
    request: &AgentRegistrationRequest,
) -> ApiResult<Vec<RegistrationCapabilities>> {
    let viz = request
        .capabilities
        .get("visualization")
        .and_then(|v| v.as_object())
        .ok_or_else(|| {
            ApiError::invalid_input(
                "visualization-only registration requires capabilities.visualization object",
            )
        })?;
    let rate_hz = viz.get("rate_hz").and_then(|v| v.as_f64()).ok_or_else(|| {
        ApiError::invalid_input("capabilities.visualization must include rate_hz (number > 0)")
    })?;
    if rate_hz <= 0.0 {
        return Err(ApiError::invalid_input(
            "capabilities.visualization.rate_hz must be > 0",
        ));
    }
    Ok(vec![RegistrationCapabilities::ReceiveNeuronVisualizations])
}

#[cfg(feature = "feagi-agent")]
#[allow(dead_code)]
fn parse_capability_rate_hz(
    capabilities: &HashMap<String, serde_json::Value>,
    capability_key: &str,
) -> ApiResult<Option<f64>> {
    let Some(capability_value) = capabilities.get(capability_key) else {
        return Ok(None);
    };

    let Some(rate_value) = capability_value.get("rate_hz") else {
        return Ok(None);
    };

    let rate_hz = rate_value.as_f64().ok_or_else(|| {
        ApiError::invalid_input(format!(
            "Invalid rate_hz for capability '{}': expected number",
            capability_key
        ))
    })?;

    if rate_hz <= 0.0 {
        return Err(ApiError::invalid_input(format!(
            "Invalid rate_hz for capability '{}': must be > 0",
            capability_key
        )));
    }

    Ok(Some(rate_hz))
}

#[cfg(feature = "feagi-agent")]
#[allow(dead_code)]
fn capability_key(capability: &RegistrationCapabilities) -> &'static str {
    match capability {
        RegistrationCapabilities::SendSensorData => "send_sensor_data",
        RegistrationCapabilities::ReceiveMotorData => "receive_motor_data",
        RegistrationCapabilities::ReceiveNeuronVisualizations => "receive_neuron_visualizations",
        RegistrationCapabilities::ReceiveSystemMessages => "receive_system_messages",
    }
}

/// Resolve a human-readable agent name from agent_id.
/// Uses agent_handler descriptor when available; otherwise attempts to decode
/// 48-byte AgentDescriptor format (instance_id(4) + manufacturer(20) + agent_name(20) + version(4)).
fn get_agent_name_from_id(state: &ApiState, agent_id: &str) -> ApiResult<String> {
    #[cfg(feature = "feagi-agent")]
    {
        if let Some(handler) = &state.agent_handler {
            if let Ok(parsed) = parse_agent_id_base64(agent_id) {
                let guard = handler.lock().unwrap();
                if let Some((descriptor, _)) = guard.get_all_registered_agents().get(&parsed) {
                    return Ok(descriptor.agent_name().to_string());
                }
            }
        }
    }
    if let Ok(name) = try_extract_agent_name_from_descriptor_bytes(agent_id) {
        return Ok(name);
    }
    Ok(agent_id.to_string())
}

/// Attempt to extract agent_name from base64 agent_id if it is 48-byte AgentDescriptor format.
fn try_extract_agent_name_from_descriptor_bytes(agent_id: &str) -> Result<String, ()> {
    use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _};
    const AGENT_DESC_SIZE: usize = 48;
    const AGENT_NAME_OFFSET: usize = 4 + 20;
    const AGENT_NAME_LEN: usize = 20;
    let decoded = BASE64.decode(agent_id).map_err(|_| ())?;
    if decoded.len() != AGENT_DESC_SIZE {
        return Err(());
    }
    let name_bytes = &decoded[AGENT_NAME_OFFSET..AGENT_NAME_OFFSET + AGENT_NAME_LEN];
    let name = String::from_utf8_lossy(name_bytes)
        .trim_end_matches('\0')
        .trim()
        .to_string();
    if name.is_empty() || !name.chars().all(|c| c.is_ascii() && !c.is_control()) {
        return Err(());
    }
    Ok(name)
}

#[cfg(feature = "feagi-agent")]
fn registration_capabilities_to_map(
    capabilities: &[RegistrationCapabilities],
) -> HashMap<String, serde_json::Value> {
    let mut out = HashMap::new();
    for capability in capabilities {
        match capability {
            RegistrationCapabilities::SendSensorData => {
                out.insert("send_sensor_data".to_string(), serde_json::json!(true));
                out.insert("sensory".to_string(), serde_json::json!(true));
            }
            RegistrationCapabilities::ReceiveMotorData => {
                out.insert("receive_motor_data".to_string(), serde_json::json!(true));
                out.insert("motor".to_string(), serde_json::json!(true));
            }
            RegistrationCapabilities::ReceiveNeuronVisualizations => {
                out.insert(
                    "receive_neuron_visualizations".to_string(),
                    serde_json::json!(true),
                );
                out.insert(
                    "visualization".to_string(),
                    serde_json::json!({"rate_hz": 0.0}),
                );
            }
            RegistrationCapabilities::ReceiveSystemMessages => {
                out.insert(
                    "receive_system_messages".to_string(),
                    serde_json::json!(true),
                );
            }
        }
    }
    out
}

#[cfg(feature = "feagi-agent")]
fn list_agent_ids_from_handler(state: &ApiState) -> Vec<String> {
    if let Some(handler) = &state.agent_handler {
        let handler_guard = handler.lock().unwrap();
        return handler_guard
            .get_all_registered_agents()
            .keys()
            .map(|id| id.to_base64())
            .collect();
    }
    Vec::new()
}

#[cfg(feature = "feagi-agent")]
fn get_handler_capabilities_for_agent(
    state: &ApiState,
    agent_id: &str,
) -> Option<HashMap<String, serde_json::Value>> {
    let parsed_agent_id = parse_agent_id_base64(agent_id).ok()?;
    let handler = state.agent_handler.as_ref()?;
    let handler_guard = handler.lock().unwrap();
    let (_, capabilities) = handler_guard
        .get_all_registered_agents()
        .get(&parsed_agent_id)?;
    Some(registration_capabilities_to_map(capabilities))
}

#[cfg(feature = "feagi-agent")]
fn parse_agent_id_base64(agent_id: &str) -> ApiResult<AgentID> {
    AgentID::try_from_base64(agent_id).map_err(|e| {
        ApiError::invalid_input(format!("Invalid agent_id (expected AgentID base64): {}", e))
    })
}

#[cfg(feature = "feagi-agent")]
async fn auto_create_cortical_areas_from_device_registrations(
    state: &ApiState,
    device_registrations: &serde_json::Value,
) {
    auto_create_cortical_areas_shared(state, device_registrations).await;
}

#[cfg(not(feature = "feagi-agent"))]
async fn auto_create_cortical_areas_from_device_registrations(
    _state: &ApiState,
    _device_registrations: &serde_json::Value,
) {
    // No-op when feature is disabled
}

/// Register a new agent with FEAGI and receive connection details including transport configuration and ports.
#[utoipa::path(
    post,
    path = "/v1/agent/register",
    request_body = AgentRegistrationRequest,
    responses(
        (status = 200, description = "Agent registered successfully", body = AgentRegistrationResponse),
        (status = 500, description = "Registration failed", body = String)
    ),
    tag = "agent"
)]
pub async fn register_agent(
    State(state): State<ApiState>,
    Json(request): Json<AgentRegistrationRequest>,
) -> ApiResult<Json<AgentRegistrationResponse>> {
    info!(
        "đŸĻ€ [API] Registration request received for agent '{}' (type: {})",
        request.agent_id, request.agent_type
    );

    // Extract device_registrations from capabilities
    let device_registrations_opt = request
        .capabilities
        .get("device_registrations")
        .and_then(|v| v.as_object().map(|_| v.clone()));

    // Clone device_regs before async operations to avoid borrow issues
    let device_regs_for_autocreate = device_registrations_opt.clone();

    // Store device registrations in handler if provided (no await here)
    let handler_available = if let Some(device_regs) = &device_registrations_opt {
        if let Some(handler) = &state.agent_handler {
            let agent_id = parse_agent_id_base64(&request.agent_id)?;
            {
                let mut handler_guard = handler.lock().unwrap();
                handler_guard.set_device_registrations_by_agent(agent_id, device_regs.clone());
            } // Drop guard before any await
            info!(
                "✅ [API] Stored device registrations for agent '{}'",
                request.agent_id
            );
            true
        } else {
            false
        }
    } else {
        state.agent_handler.is_some()
    };

    // Now safe to await
    if let Some(device_regs) = device_regs_for_autocreate {
        auto_create_cortical_areas_from_device_registrations(&state, &device_regs).await;
    }

    // Register visualization subscription if visualization capability is present
    if let Some(viz) = request.capabilities.get("visualization") {
        if let Some(rate_hz) = viz.get("rate_hz").and_then(|v| v.as_f64()) {
            if rate_hz > 0.0 {
                match state
                    .runtime_service
                    .register_visualization_subscriptions(&request.agent_id, rate_hz)
                    .await
                {
                    Ok(_) => {
                        info!(
                            "✅ [API] Registered visualization subscription for agent '{}' at {}Hz",
                            request.agent_id, rate_hz
                        );
                    }
                    Err(e) => {
                        warn!(
                            "âš ī¸  [API] Failed to register visualization subscription for agent '{}': {}",
                            request.agent_id, e
                        );
                    }
                }
            }
        }
    }

    // Get available endpoints from handler and build TransportConfig objects
    use crate::v1::TransportConfig;
    let transports_array = if state.agent_handler.is_some() {
        // Load config to get port numbers
        use feagi_config::load_config;
        let config = load_config(None, None)
            .map_err(|e| ApiError::internal(format!("Failed to load config: {}", e)))?;

        let mut transport_configs = Vec::new();
        for transport in &config.transports.available {
            let transport_type = transport.to_lowercase();
            if transport_type != "zmq" && transport_type != "websocket" && transport_type != "ws" {
                continue;
            }

            // Build ports map from config
            let mut ports = HashMap::new();
            if transport_type == "websocket" || transport_type == "ws" {
                ports.insert(
                    "registration".to_string(),
                    config.websocket.registration_port,
                );
                ports.insert("sensory".to_string(), config.websocket.sensory_port);
                ports.insert("motor".to_string(), config.websocket.motor_port);
                ports.insert(
                    "visualization".to_string(),
                    config.websocket.visualization_port,
                );
            } else {
                ports.insert("registration".to_string(), config.agent.registration_port);
                ports.insert("sensory".to_string(), config.ports.zmq_sensory_port);
                ports.insert("motor".to_string(), config.ports.zmq_motor_port);
                ports.insert(
                    "visualization".to_string(),
                    config.ports.zmq_visualization_port,
                );
            }

            transport_configs.push(TransportConfig {
                transport_type: if transport_type == "ws" {
                    "websocket".to_string()
                } else {
                    transport_type
                },
                enabled: true,
                ports,
                host: if transport == "websocket" || transport == "ws" {
                    config.websocket.advertised_host.clone()
                } else {
                    config.zmq.advertised_host.clone()
                },
            });
        }

        transport_configs
    } else {
        if !handler_available {
            return Err(ApiError::internal("Agent handler not available"));
        }
        Vec::new()
    };

    Ok(Json(AgentRegistrationResponse {
        status: "success".to_string(),
        message: "Agent configuration stored. Connect via ZMQ/WebSocket for full registration"
            .to_string(),
        success: true,
        transport: None, // Legacy field - deprecated
        rates: None,
        transports: Some(transports_array),
        recommended_transport: Some("websocket".to_string()),
        shm_paths: None,
        cortical_areas: serde_json::json!({}),
    }))
}

/// Send a heartbeat to keep the agent registered and prevent timeout disconnection.
#[utoipa::path(
    post,
    path = "/v1/agent/heartbeat",
    request_body = HeartbeatRequest,
    responses(
        (status = 200, description = "Heartbeat recorded", body = HeartbeatResponse),
        (status = 404, description = "Agent not found"),
        (status = 500, description = "Heartbeat failed")
    ),
    tag = "agent"
)]
pub async fn heartbeat(
    State(state): State<ApiState>,
    Json(request): Json<HeartbeatRequest>,
) -> ApiResult<Json<HeartbeatResponse>> {
    let agent_service = state
        .agent_service
        .as_ref()
        .ok_or_else(|| ApiError::internal("Agent service not available"))?;

    let service_request = ServiceHeartbeatRequest {
        agent_id: request.agent_id.clone(),
    };

    match agent_service.heartbeat(service_request).await {
        Ok(_) => Ok(Json(HeartbeatResponse {
            message: "heartbeat_ok".to_string(),
            success: true,
        })),
        Err(_) => Err(ApiError::not_found(
            "agent",
            &format!("Agent {} not in registry", request.agent_id),
        )),
    }
}

/// Get a list of all currently registered agent IDs.
#[utoipa::path(
    get,
    path = "/v1/agent/list",
    responses(
        (status = 200, description = "List of agent IDs", body = Vec<String>),
        (status = 503, description = "Registration service unavailable")
    ),
    tag = "agent"
)]
pub async fn list_agents(State(state): State<ApiState>) -> ApiResult<Json<Vec<String>>> {
    if let Some(agent_service) = state.agent_service.as_ref() {
        match agent_service.list_agents().await {
            Ok(agent_ids) if !agent_ids.is_empty() => return Ok(Json(agent_ids)),
            Ok(_) => {}
            Err(e) => {
                warn!(
                    "âš ī¸ [API] AgentService list_agents failed ({}); falling back to agent_handler",
                    e
                );
            }
        }
    }

    #[cfg(feature = "feagi-agent")]
    {
        Ok(Json(list_agent_ids_from_handler(&state)))
    }

    #[cfg(not(feature = "feagi-agent"))]
    {
        Err(ApiError::internal("Agent service not available"))
    }
}

/// Get agent properties including type, capabilities, version, and connection details. Uses query parameter ?agent_id=xxx.
#[utoipa::path(
    get,
    path = "/v1/agent/properties",
    params(
        ("agent_id" = String, Query, description = "Agent ID to get properties for")
    ),
    responses(
        (status = 200, description = "Agent properties", body = AgentPropertiesResponse),
        (status = 404, description = "Agent not found"),
        (status = 500, description = "Failed to get agent properties")
    ),
    tag = "agent"
)]
pub async fn get_agent_properties(
    State(state): State<ApiState>,
    Query(params): Query<HashMap<String, String>>,
) -> ApiResult<Json<AgentPropertiesResponse>> {
    let agent_id = params
        .get("agent_id")
        .ok_or_else(|| ApiError::invalid_input("Missing agent_id query parameter"))?;

    let agent_service = state
        .agent_service
        .as_ref()
        .ok_or_else(|| ApiError::internal("Agent service not available"))?;

    let agent_name = get_agent_name_from_id(&state, agent_id)?;
    match agent_service.get_agent_properties(agent_id).await {
        Ok(properties) => Ok(Json(AgentPropertiesResponse {
            agent_name,
            agent_type: properties.agent_type,
            agent_ip: properties.agent_ip,
            agent_data_port: properties.agent_data_port,
            agent_router_address: properties.agent_router_address,
            agent_version: properties.agent_version,
            controller_version: properties.controller_version,
            capabilities: properties.capabilities,
            chosen_transport: properties.chosen_transport,
        })),
        Err(e) => Err(ApiError::not_found("agent", &format!("{}", e))),
    }
}

/// Get shared memory configuration and paths for all registered agents using shared memory transport.
#[utoipa::path(
    get,
    path = "/v1/agent/shared_mem",
    responses(
        (status = 200, description = "Shared memory info", body = HashMap<String, HashMap<String, serde_json::Value>>),
        (status = 500, description = "Failed to get shared memory info")
    ),
    tag = "agent"
)]
pub async fn get_shared_memory(
    State(state): State<ApiState>,
) -> ApiResult<Json<HashMap<String, HashMap<String, serde_json::Value>>>> {
    let agent_service = state
        .agent_service
        .as_ref()
        .ok_or_else(|| ApiError::internal("Agent service not available"))?;

    match agent_service.get_shared_memory_info().await {
        Ok(shm_info) => Ok(Json(shm_info)),
        Err(e) => Err(ApiError::internal(format!(
            "Failed to get shared memory info: {}",
            e
        ))),
    }
}

/// Deregister an agent from FEAGI and clean up its resources.
#[utoipa::path(
    delete,
    path = "/v1/agent/deregister",
    request_body = AgentDeregistrationRequest,
    responses(
        (status = 200, description = "Agent deregistered successfully", body = SuccessResponse),
        (status = 500, description = "Deregistration failed")
    ),
    tag = "agent"
)]
pub async fn deregister_agent(
    State(state): State<ApiState>,
    Json(request): Json<AgentDeregistrationRequest>,
) -> ApiResult<Json<SuccessResponse>> {
    let agent_service = state
        .agent_service
        .as_ref()
        .ok_or_else(|| ApiError::internal("Agent service not available"))?;

    match agent_service.deregister_agent(&request.agent_id).await {
        Ok(_) => Ok(Json(SuccessResponse {
            message: format!("Agent '{}' deregistered successfully", request.agent_id),
            success: Some(true),
        })),
        Err(e) => Err(ApiError::internal(format!("Deregistration failed: {}", e))),
    }
}

/// Manually stimulate neurons at specific coordinates across multiple cortical areas for testing and debugging.
#[utoipa::path(
    post,
    path = "/v1/agent/manual_stimulation",
    request_body = ManualStimulationRequest,
    responses(
        (status = 200, description = "Manual stimulation result", body = ManualStimulationResponse),
        (status = 500, description = "Stimulation failed")
    ),
    tag = "agent"
)]
pub async fn manual_stimulation(
    State(state): State<ApiState>,
    Json(request): Json<ManualStimulationRequest>,
) -> ApiResult<Json<ManualStimulationResponse>> {
    let agent_service = state
        .agent_service
        .as_ref()
        .ok_or_else(|| ApiError::internal("Agent service not available"))?;

    // Ensure runtime service is connected to agent service (if not already connected)
    // This allows runtime_service to be set after AgentServiceImpl is wrapped in Arc
    agent_service.try_set_runtime_service(state.runtime_service.clone());

    let mode = match request.mode.as_deref() {
        Some("force_fire") => ManualStimulationMode::ForceFire,
        Some("candidate") | None => ManualStimulationMode::Candidate,
        Some(other) => {
            return Err(ApiError::invalid_input(format!(
                "Invalid stimulation mode '{}'; expected 'candidate' or 'force_fire'",
                other
            )));
        }
    };

    match agent_service
        .manual_stimulation(request.stimulation_payload, mode)
        .await
    {
        Ok(result) => {
            let success = result
                .get("success")
                .and_then(|v| v.as_bool())
                .unwrap_or(false);
            let total_coordinates = result
                .get("total_coordinates")
                .and_then(|v| v.as_u64())
                .unwrap_or(0) as usize;
            let successful_areas = result
                .get("successful_areas")
                .and_then(|v| v.as_u64())
                .unwrap_or(0) as usize;
            let requested_coordinates = result
                .get("requested_coordinates")
                .and_then(|v| v.as_u64())
                .unwrap_or(0) as usize;
            let matched_coordinates = result
                .get("matched_coordinates")
                .and_then(|v| v.as_u64())
                .unwrap_or(0) as usize;
            let unique_neuron_ids = result
                .get("unique_neuron_ids")
                .and_then(|v| v.as_u64())
                .unwrap_or(0) as usize;
            let mode = result
                .get("mode")
                .and_then(|v| v.as_str())
                .unwrap_or("candidate")
                .to_string();
            let failed_areas = result
                .get("failed_areas")
                .and_then(|v| v.as_array())
                .map(|arr| {
                    arr.iter()
                        .filter_map(|v| v.as_str().map(String::from))
                        .collect()
                })
                .unwrap_or_default();
            let error = result
                .get("error")
                .and_then(|v| v.as_str())
                .map(String::from);

            Ok(Json(ManualStimulationResponse {
                success,
                total_coordinates,
                requested_coordinates,
                matched_coordinates,
                unique_neuron_ids,
                mode,
                successful_areas,
                failed_areas,
                error,
            }))
        }
        Err(e) => Err(ApiError::internal(format!("Stimulation failed: {}", e))),
    }
}

/// Get Fire Queue (FQ) sampler coordination status including visualization and motor sampling configuration.
#[utoipa::path(
    get,
    path = "/v1/agent/fq_sampler_status",
    responses(
        (status = 200, description = "FQ sampler status", body = HashMap<String, serde_json::Value>),
        (status = 500, description = "Failed to get FQ sampler status")
    ),
    tag = "agent"
)]
pub async fn get_fq_sampler_status(
    State(state): State<ApiState>,
) -> ApiResult<Json<HashMap<String, serde_json::Value>>> {
    let agent_service = state
        .agent_service
        .as_ref()
        .ok_or_else(|| ApiError::internal("Agent service not available"))?;

    let runtime_service = state.runtime_service.as_ref();

    // Get all agents
    let agent_ids = agent_service
        .list_agents()
        .await
        .map_err(|e| ApiError::internal(format!("Failed to list agents: {}", e)))?;

    // Get FCL sampler config from RuntimeService
    let (fcl_frequency, fcl_consumer) = runtime_service
        .get_fcl_sampler_config()
        .await
        .map_err(|e| ApiError::internal(format!("Failed to get sampler config: {}", e)))?;

    // Build response matching Python structure
    let mut visualization_agents = Vec::new();
    let mut motor_agents = Vec::new();

    for agent_id in &agent_ids {
        if let Ok(props) = agent_service.get_agent_properties(agent_id).await {
            if props.capabilities.contains_key("visualization") {
                visualization_agents.push(agent_id.clone());
            }
            if props.capabilities.contains_key("motor") {
                motor_agents.push(agent_id.clone());
            }
        }
    }

    let mut fq_coordination = HashMap::new();

    let mut viz_sampler = HashMap::new();
    viz_sampler.insert(
        "enabled".to_string(),
        serde_json::json!(!visualization_agents.is_empty()),
    );
    viz_sampler.insert(
        "reason".to_string(),
        serde_json::json!(if visualization_agents.is_empty() {
            "No visualization agents connected".to_string()
        } else {
            format!(
                "{} visualization agent(s) connected",
                visualization_agents.len()
            )
        }),
    );
    viz_sampler.insert(
        "agents_requiring".to_string(),
        serde_json::json!(visualization_agents),
    );
    viz_sampler.insert("frequency_hz".to_string(), serde_json::json!(fcl_frequency));
    fq_coordination.insert(
        "visualization_fq_sampler".to_string(),
        serde_json::json!(viz_sampler),
    );

    let mut motor_sampler = HashMap::new();
    motor_sampler.insert(
        "enabled".to_string(),
        serde_json::json!(!motor_agents.is_empty()),
    );
    motor_sampler.insert(
        "reason".to_string(),
        serde_json::json!(if motor_agents.is_empty() {
            "No motor agents connected".to_string()
        } else {
            format!("{} motor agent(s) connected", motor_agents.len())
        }),
    );
    motor_sampler.insert(
        "agents_requiring".to_string(),
        serde_json::json!(motor_agents),
    );
    motor_sampler.insert("frequency_hz".to_string(), serde_json::json!(100.0));
    fq_coordination.insert(
        "motor_fq_sampler".to_string(),
        serde_json::json!(motor_sampler),
    );

    let mut response = HashMap::new();
    response.insert(
        "fq_sampler_coordination".to_string(),
        serde_json::json!(fq_coordination),
    );
    response.insert(
        "agent_registry".to_string(),
        serde_json::json!({
            "total_agents": agent_ids.len(),
            "agent_ids": agent_ids
        }),
    );
    response.insert(
        "system_status".to_string(),
        serde_json::json!("coordinated_via_registration_manager"),
    );
    response.insert(
        "fcl_sampler_consumer".to_string(),
        serde_json::json!(fcl_consumer),
    );

    Ok(Json(response))
}

/// Get list of all supported agent types and capability types (sensory, motor, visualization, etc.).
#[utoipa::path(
    get,
    path = "/v1/agent/capabilities",
    responses(
        (status = 200, description = "List of capabilities", body = HashMap<String, Vec<String>>),
        (status = 500, description = "Failed to get capabilities")
    ),
    tag = "agent"
)]
pub async fn get_capabilities(
    State(_state): State<ApiState>,
) -> ApiResult<Json<HashMap<String, Vec<String>>>> {
    let mut response = HashMap::new();
    response.insert(
        "agent_types".to_string(),
        vec![
            "sensory".to_string(),
            "motor".to_string(),
            "both".to_string(),
            "visualization".to_string(),
            "infrastructure".to_string(),
        ],
    );
    response.insert(
        "capability_types".to_string(),
        vec![
            "vision".to_string(),
            "motor".to_string(),
            "visualization".to_string(),
            "sensory".to_string(),
        ],
    );

    Ok(Json(response))
}

/// Get capabilities for all agents with optional filtering and payload includes.
#[utoipa::path(
    get,
    path = "/v1/agent/capabilities/all",
    params(
        ("agent_type" = Option<String>, Query, description = "Filter by agent type (exact match)"),
        ("capability" = Option<String>, Query, description = "Filter by capability key(s), comma-separated"),
        ("include_device_registrations" = Option<bool>, Query, description = "Include device registration payloads per agent")
    ),
    responses(
        (status = 200, description = "Agent capabilities map", body = HashMap<String, AgentCapabilitiesSummary>),
        (status = 400, description = "Invalid query"),
        (status = 500, description = "Failed to get agent capabilities")
    ),
    tag = "agent"
)]
pub async fn get_all_agent_capabilities(
    State(state): State<ApiState>,
    Query(params): Query<AgentCapabilitiesAllQuery>,
) -> ApiResult<Json<HashMap<String, AgentCapabilitiesSummary>>> {
    let agent_service = state.agent_service.as_ref();

    let include_device_registrations = params.include_device_registrations.unwrap_or(false);
    let capability_filters: Option<Vec<String>> = params.capability.as_ref().and_then(|value| {
        let filters: Vec<String> = value
            .split(',')
            .map(|item| item.trim())
            .filter(|item| !item.is_empty())
            .map(String::from)
            .collect();
        if filters.is_empty() {
            None
        } else {
            Some(filters)
        }
    });

    let mut agent_ids: Vec<String> = Vec::new();
    if let Some(service) = agent_service {
        match service.list_agents().await {
            Ok(ids) => agent_ids = ids,
            Err(e) => {
                warn!(
                    "âš ī¸ [API] AgentService list_agents failed ({}); falling back to agent_handler",
                    e
                );
            }
        }
    }
    #[cfg(feature = "feagi-agent")]
    if agent_ids.is_empty() {
        agent_ids = list_agent_ids_from_handler(&state);
    }

    let mut response: HashMap<String, AgentCapabilitiesSummary> = HashMap::new();

    for agent_id in agent_ids {
        let agent_name = get_agent_name_from_id(&state, &agent_id)?;
        let (agent_type, capabilities_map) = if let Some(service) = agent_service {
            match service.get_agent_properties(&agent_id).await {
                Ok(props) => (props.agent_type, props.capabilities),
                Err(_) => {
                    #[cfg(feature = "feagi-agent")]
                    {
                        if let Some(caps) = get_handler_capabilities_for_agent(&state, &agent_id) {
                            ("unknown".to_string(), caps)
                        } else {
                            continue;
                        }
                    }
                    #[cfg(not(feature = "feagi-agent"))]
                    {
                        continue;
                    }
                }
            }
        } else {
            #[cfg(feature = "feagi-agent")]
            {
                if let Some(caps) = get_handler_capabilities_for_agent(&state, &agent_id) {
                    ("unknown".to_string(), caps)
                } else {
                    continue;
                }
            }
            #[cfg(not(feature = "feagi-agent"))]
            {
                continue;
            }
        };

        if let Some(ref agent_type_filter) = params.agent_type {
            if agent_type != *agent_type_filter {
                continue;
            }
        }

        if let Some(ref filters) = capability_filters {
            let has_match = filters
                .iter()
                .any(|capability| capabilities_map.contains_key(capability));
            if !has_match {
                continue;
            }
        }

        let device_registrations = if include_device_registrations {
            #[cfg(feature = "feagi-agent")]
            {
                match export_device_registrations_from_connector(&state, &agent_id) {
                    Ok(v) => Some(v),
                    Err(e) => {
                        warn!(
                            "âš ī¸ [API] Could not export device registrations for '{}': {:?}",
                            agent_id, e
                        );
                        None
                    }
                }
            }
            #[cfg(not(feature = "feagi-agent"))]
            {
                None
            }
        } else {
            None
        };
        response.insert(
            agent_id,
            AgentCapabilitiesSummary {
                agent_name,
                capabilities: capabilities_map,
                device_registrations,
            },
        );
    }

    Ok(Json(response))
}

#[cfg(feature = "feagi-agent")]
fn export_device_registrations_from_connector(
    state: &ApiState,
    agent_id: &str,
) -> ApiResult<serde_json::Value> {
    let parsed_agent_id = parse_agent_id_base64(agent_id)?;
    if let Some(handler) = &state.agent_handler {
        let handler_guard = handler.lock().unwrap();
        if let Some(regs) = handler_guard.get_device_registrations_by_agent(parsed_agent_id) {
            return Ok(regs.clone());
        }
    }

    Err(ApiError::not_found(
        "device_registrations",
        &format!("No device registrations found for agent '{}'", agent_id),
    ))
}

/// Get comprehensive agent information including status, capabilities, version, and connection details.
#[utoipa::path(
    get,
    path = "/v1/agent/info/{agent_id}",
    params(
        ("agent_id" = String, Path, description = "Agent ID")
    ),
    responses(
        (status = 200, description = "Agent detailed info", body = HashMap<String, serde_json::Value>),
        (status = 404, description = "Agent not found"),
        (status = 500, description = "Failed to get agent info")
    ),
    tag = "agent"
)]
pub async fn get_agent_info(
    State(state): State<ApiState>,
    Path(agent_id): Path<String>,
) -> ApiResult<Json<HashMap<String, serde_json::Value>>> {
    let agent_service = state
        .agent_service
        .as_ref()
        .ok_or_else(|| ApiError::internal("Agent service not available"))?;

    let properties = agent_service
        .get_agent_properties(&agent_id)
        .await
        .map_err(|e| ApiError::not_found("agent", &e.to_string()))?;

    let mut response = HashMap::new();
    response.insert("agent_id".to_string(), serde_json::json!(agent_id));
    response.insert(
        "agent_name".to_string(),
        serde_json::json!(get_agent_name_from_id(&state, &agent_id)?),
    );
    response.insert(
        "agent_type".to_string(),
        serde_json::json!(properties.agent_type),
    );
    response.insert(
        "agent_ip".to_string(),
        serde_json::json!(properties.agent_ip),
    );
    response.insert(
        "agent_data_port".to_string(),
        serde_json::json!(properties.agent_data_port),
    );
    response.insert(
        "capabilities".to_string(),
        serde_json::json!(properties.capabilities),
    );
    response.insert(
        "agent_version".to_string(),
        serde_json::json!(properties.agent_version),
    );
    response.insert(
        "controller_version".to_string(),
        serde_json::json!(properties.controller_version),
    );
    response.insert("status".to_string(), serde_json::json!("active"));
    if let Some(ref transport) = properties.chosen_transport {
        response.insert("chosen_transport".to_string(), serde_json::json!(transport));
    }

    Ok(Json(response))
}

/// Get agent properties using path parameter. Same as /v1/agent/properties but with agent_id in the URL path.
#[utoipa::path(
    get,
    path = "/v1/agent/properties/{agent_id}",
    params(
        ("agent_id" = String, Path, description = "Agent ID")
    ),
    responses(
        (status = 200, description = "Agent properties", body = AgentPropertiesResponse),
        (status = 404, description = "Agent not found"),
        (status = 500, description = "Failed to get agent properties")
    ),
    tag = "agent"
)]
pub async fn get_agent_properties_path(
    State(state): State<ApiState>,
    Path(agent_id): Path<String>,
) -> ApiResult<Json<AgentPropertiesResponse>> {
    let agent_service = state
        .agent_service
        .as_ref()
        .ok_or_else(|| ApiError::internal("Agent service not available"))?;

    match agent_service.get_agent_properties(&agent_id).await {
        Ok(properties) => Ok(Json(AgentPropertiesResponse {
            agent_name: get_agent_name_from_id(&state, &agent_id)?,
            agent_type: properties.agent_type,
            agent_ip: properties.agent_ip,
            agent_data_port: properties.agent_data_port,
            agent_router_address: properties.agent_router_address,
            agent_version: properties.agent_version,
            controller_version: properties.controller_version,
            capabilities: properties.capabilities,
            chosen_transport: properties.chosen_transport,
        })),
        Err(e) => Err(ApiError::not_found("agent", &format!("{}", e))),
    }
}

/// Configure agent parameters and settings. (Not yet implemented)
#[utoipa::path(
    post,
    path = "/v1/agent/configure",
    responses(
        (status = 200, description = "Agent configured", body = HashMap<String, String>),
        (status = 400, description = "Invalid input"),
        (status = 500, description = "Failed to configure agent")
    ),
    tag = "agent"
)]
pub async fn post_configure(
    State(_state): State<ApiState>,
    Json(config): Json<HashMap<String, serde_json::Value>>,
) -> ApiResult<Json<HashMap<String, String>>> {
    tracing::info!(target: "feagi-api", "Agent configuration requested: {} params", config.len());

    Ok(Json(HashMap::from([
        (
            "message".to_string(),
            "Agent configuration updated".to_string(),
        ),
        ("status".to_string(), "not_yet_implemented".to_string()),
    ])))
}

/// Export device registrations for an agent
///
/// Returns the complete device registration configuration including
/// sensor and motor device registrations, encoder/decoder properties,
/// and feedback configurations in the format compatible with
/// ConnectorAgent::get_device_registration_json.
#[utoipa::path(
    get,
    path = "/v1/agent/{agent_id}/device_registrations",
    params(
        ("agent_id" = String, Path, description = "Agent ID")
    ),
    responses(
        (status = 200, description = "Device registrations exported successfully", body = DeviceRegistrationExportResponse),
        (status = 404, description = "Agent not found"),
        (status = 500, description = "Failed to export device registrations")
    ),
    tag = "agent"
)]
pub async fn export_device_registrations(
    State(state): State<ApiState>,
    Path(agent_id): Path<String>,
) -> ApiResult<Json<DeviceRegistrationExportResponse>> {
    info!(
        "đŸĻ€ [API] Device registration export requested for agent '{}'",
        agent_id
    );

    // Best-effort existence check via AgentService.
    // Do not fail hard here: ZMQ-only registrations can exist in agent_handler
    // before/without AgentService registry visibility.
    if let Some(agent_service) = state.agent_service.as_ref() {
        if let Err(e) = agent_service.get_agent_properties(&agent_id).await {
            info!(
                "â„šī¸ [API] Agent '{}' not found in AgentService during export ({}); falling back to agent_handler lookup",
                agent_id, e
            );
        }
    }

    // Get device registrations from agent_handler
    #[cfg(feature = "feagi-agent")]
    let device_registrations = {
        let parsed_agent_id = parse_agent_id_base64(&agent_id)?;
        if let Some(handler) = &state.agent_handler {
            let handler_guard = handler.lock().unwrap();
            if let Some(regs) = handler_guard.get_device_registrations_by_agent(parsed_agent_id) {
                info!(
                    "📤 [API] Found device registrations for agent '{}'",
                    agent_id
                );
                regs.clone()
            } else {
                warn!(
                    "âš ī¸ [API] No device registrations found for agent '{}'",
                    agent_id
                );
                serde_json::json!({
                    "input_units_and_encoder_properties": {},
                    "output_units_and_decoder_properties": {},
                    "feedbacks": []
                })
            }
        } else {
            warn!("âš ī¸ [API] No agent_handler available");
            serde_json::json!({
                "input_units_and_encoder_properties": {},
                "output_units_and_decoder_properties": {},
                "feedbacks": []
            })
        }
    };

    #[cfg(not(feature = "feagi-agent"))]
    // @architecture:acceptable - fallback when feature is disabled
    // Returns empty structure when feagi-agent feature is not compiled in
    let device_registrations = serde_json::json!({
        "input_units_and_encoder_properties": {},
        "output_units_and_decoder_properties": {},
        "feedbacks": []
    });

    info!(
        "✅ [API] Device registration export succeeded for agent '{}'",
        agent_id
    );

    Ok(Json(DeviceRegistrationExportResponse {
        device_registrations,
        agent_id,
    }))
}

/// Import device registrations for an agent
///
/// Imports a device registration configuration, replacing all existing
/// device registrations for the agent. The configuration must be in
/// the format compatible with ConnectorAgent::set_device_registrations_from_json.
///
/// # Warning
/// This operation **wipes all existing registered devices** before importing
/// the new configuration.
#[utoipa::path(
    post,
    path = "/v1/agent/{agent_id}/device_registrations",
    params(
        ("agent_id" = String, Path, description = "Agent ID")
    ),
    request_body = DeviceRegistrationImportRequest,
    responses(
        (status = 200, description = "Device registrations imported successfully", body = DeviceRegistrationImportResponse),
        (status = 400, description = "Invalid device registration configuration"),
        (status = 404, description = "Agent not found"),
        (status = 500, description = "Failed to import device registrations")
    ),
    tag = "agent"
)]
pub async fn import_device_registrations(
    State(state): State<ApiState>,
    Path(agent_id): Path<String>,
    Json(request): Json<DeviceRegistrationImportRequest>,
) -> ApiResult<Json<DeviceRegistrationImportResponse>> {
    info!(
        "đŸĻ€ [API] Device registration import requested for agent '{}'",
        agent_id
    );

    // Best-effort existence check via AgentService.
    // Do not fail hard here: ZMQ-only registrations can be handled via agent_handler.
    if let Some(agent_service) = state.agent_service.as_ref() {
        if let Err(e) = agent_service.get_agent_properties(&agent_id).await {
            info!(
                "â„šī¸ [API] Agent '{}' not found in AgentService during import ({}); continuing with agent_handler",
                agent_id, e
            );
        }
    }

    // Validate the device registration JSON structure
    // Check that it has the expected fields
    if !request.device_registrations.is_object() {
        return Err(ApiError::invalid_input(
            "Device registrations must be a JSON object",
        ));
    }

    // Validate required fields exist
    let obj = request.device_registrations.as_object().unwrap();
    if !obj.contains_key("input_units_and_encoder_properties")
        || !obj.contains_key("output_units_and_decoder_properties")
        || !obj.contains_key("feedbacks")
    {
        return Err(ApiError::invalid_input(
            "Device registrations must contain: input_units_and_encoder_properties, output_units_and_decoder_properties, and feedbacks",
        ));
    }

    // Store device registrations in agent_handler
    #[cfg(feature = "feagi-agent")]
    {
        let parsed_agent_id = parse_agent_id_base64(&agent_id)?;
        if let Some(handler) = &state.agent_handler {
            let mut handler_guard = handler.lock().unwrap();
            let device_regs = request.device_registrations.clone();
            handler_guard.set_device_registrations_by_agent(parsed_agent_id, device_regs.clone());
            // Also set by descriptor so feagi-rs motor subscription lookup finds it
            let descriptor_opt = handler_guard
                .get_all_registered_agents()
                .get(&parsed_agent_id)
                .map(|(d, _)| d.clone());
            if let Some(descriptor) = descriptor_opt {
                handler_guard.set_device_registrations_by_descriptor(
                    agent_id.clone(),
                    descriptor,
                    device_regs,
                );
            }
            info!(
                "đŸ“Ĩ [API] Imported device registrations for agent '{}'",
                agent_id
            );
        } else {
            warn!("âš ī¸ [API] No agent_handler available to store device registrations");
        }

        auto_create_cortical_areas_from_device_registrations(&state, &request.device_registrations)
            .await;

        Ok(Json(DeviceRegistrationImportResponse {
            success: true,
            message: format!(
                "Device registrations imported successfully for agent '{}'",
                agent_id
            ),
            agent_id,
        }))
    }

    #[cfg(not(feature = "feagi-agent"))]
    {
        info!(
            "✅ [API] Device registration import succeeded for agent '{}' (feagi-agent feature not enabled)",
            agent_id
        );
        Ok(Json(DeviceRegistrationImportResponse {
            success: true,
            message: format!(
                "Device registrations imported successfully for agent '{}' (feature not enabled)",
                agent_id
            ),
            agent_id,
        }))
    }
}