trustformers-mobile 0.1.1

Mobile deployment support for TrustformeRS (iOS, Android)
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
//! Flutter Integration for TrustformeRS Mobile
//!
//! This module provides comprehensive Flutter platform channel integration,
//! enabling Flutter applications to leverage TrustformeRS mobile inference
//! capabilities with optimized performance and native platform features.

use crate::{
    inference::MobileInferenceEngine, MobileBackend, MobileConfig, MobilePlatform, MobileStats,
};
use serde::{Deserialize, Serialize};
use std::{
    collections::HashMap,
    ffi::{CStr, CString},
    os::raw::c_char,
    sync::{Arc, Mutex},
};
use trustformers_core::error::Result;
use trustformers_core::Tensor;

/// Flutter platform channel manager for TrustformeRS
pub struct FlutterChannelManager {
    engines: Arc<Mutex<HashMap<String, MobileInferenceEngine>>>,
    configurations: Arc<Mutex<HashMap<String, MobileConfig>>>,
    statistics: Arc<Mutex<HashMap<String, MobileStats>>>,
    event_sink: Option<FlutterEventSink>,
}

impl std::fmt::Debug for FlutterChannelManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FlutterChannelManager")
            .field("engines", &"<HashMap<String, MobileInferenceEngine>>")
            .field("configurations", &"<HashMap<String, MobileConfig>>")
            .field("statistics", &"<HashMap<String, MobileStats>>")
            .field("event_sink", &self.event_sink.is_some())
            .finish()
    }
}

/// Flutter event sink for streaming events to Dart
pub type FlutterEventSink = Box<dyn Fn(&str) + Send + Sync>;

/// Flutter method call structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FlutterMethodCall {
    pub method: String,
    pub arguments: Option<serde_json::Value>,
}

/// Flutter method result
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum FlutterMethodResult {
    Success(serde_json::Value),
    Error {
        code: String,
        message: String,
        details: Option<serde_json::Value>,
    },
}

/// Flutter TrustformeRS configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FlutterTrustformersConfig {
    pub engine_id: String,
    pub model_path: String,
    pub platform: String,
    pub backend: String,
    pub memory_optimization: String,
    pub max_memory_mb: u32,
    pub use_fp16: bool,
    pub quantization: Option<FlutterQuantizationConfig>,
    pub num_threads: u32,
    pub enable_batching: bool,
    pub max_batch_size: u32,
}

/// Flutter quantization configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FlutterQuantizationConfig {
    pub scheme: String,
    pub dynamic: bool,
    pub per_channel: bool,
}

/// Flutter inference request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FlutterInferenceRequest {
    pub engine_id: String,
    pub input_ids: Vec<i64>,
    pub attention_mask: Option<Vec<i64>>,
    pub token_type_ids: Option<Vec<i64>>,
    pub max_length: Option<u32>,
    pub temperature: Option<f32>,
    pub top_p: Option<f32>,
    pub top_k: Option<u32>,
    pub do_sample: bool,
}

/// Flutter inference response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FlutterInferenceResponse {
    pub tokens: Vec<i64>,
    pub logits: Option<Vec<f32>>,
    pub attention_weights: Option<Vec<Vec<f32>>>,
    pub inference_time_ms: f32,
    pub memory_usage_mb: u32,
}

/// Flutter device info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FlutterDeviceInfo {
    pub platform: String,
    pub model: String,
    pub memory_total_mb: u32,
    pub memory_available_mb: u32,
    pub cpu_cores: u32,
    pub gpu_available: bool,
    pub neural_engine_available: bool,
}

/// Flutter performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FlutterPerformanceMetrics {
    pub engine_id: String,
    pub total_inferences: u64,
    pub avg_inference_time_ms: f32,
    pub peak_memory_mb: u32,
    pub current_memory_mb: u32,
    pub throughput_tokens_per_sec: f32,
}

impl FlutterChannelManager {
    /// Create a new Flutter channel manager
    pub fn new() -> Self {
        Self {
            engines: Arc::new(Mutex::new(HashMap::new())),
            configurations: Arc::new(Mutex::new(HashMap::new())),
            statistics: Arc::new(Mutex::new(HashMap::new())),
            event_sink: None,
        }
    }

    /// Set event sink for streaming updates to Dart
    pub fn set_event_sink(&mut self, sink: FlutterEventSink) {
        self.event_sink = Some(sink);
    }

    /// Handle Flutter method call
    pub fn handle_method_call(&self, call: FlutterMethodCall) -> FlutterMethodResult {
        match call.method.as_str() {
            "initialize" => self.handle_initialize(call.arguments),
            "loadModel" => self.handle_load_model(call.arguments),
            "inference" => self.handle_inference(call.arguments),
            "getDeviceInfo" => self.handle_get_device_info(),
            "getPerformanceMetrics" => self.handle_get_performance_metrics(call.arguments),
            "dispose" => self.handle_dispose(call.arguments),
            "getBatchInference" => self.handle_batch_inference(call.arguments),
            "getModelInfo" => self.handle_get_model_info(call.arguments),
            "optimizeForDevice" => self.handle_optimize_for_device(call.arguments),
            _ => FlutterMethodResult::Error {
                code: "METHOD_NOT_FOUND".to_string(),
                message: format!("Method '{}' not implemented", call.method),
                details: None,
            },
        }
    }

    /// Handle engine initialization
    fn handle_initialize(&self, args: Option<serde_json::Value>) -> FlutterMethodResult {
        let config: FlutterTrustformersConfig = match args {
            Some(value) => match serde_json::from_value(value) {
                Ok(config) => config,
                Err(e) => {
                    return FlutterMethodResult::Error {
                        code: "INVALID_ARGUMENTS".to_string(),
                        message: format!("Failed to parse configuration: {}", e),
                        details: None,
                    }
                },
            },
            None => {
                return FlutterMethodResult::Error {
                    code: "MISSING_ARGUMENTS".to_string(),
                    message: "Configuration is required".to_string(),
                    details: None,
                }
            },
        };

        let mobile_config = match self.convert_flutter_config_to_mobile(&config) {
            Ok(config) => config,
            Err(e) => {
                return FlutterMethodResult::Error {
                    code: "CONFIGURATION_ERROR".to_string(),
                    message: format!("Invalid configuration: {}", e),
                    details: None,
                }
            },
        };

        // Store configuration
        {
            let mut configs = self.configurations.lock().expect("Lock poisoned");
            configs.insert(config.engine_id.clone(), mobile_config.clone());
        }

        // Initialize statistics
        {
            let mut stats = self.statistics.lock().expect("Lock poisoned");
            stats.insert(config.engine_id.clone(), MobileStats::new(&mobile_config));
        }

        FlutterMethodResult::Success(serde_json::json!({
            "engine_id": config.engine_id,
            "status": "initialized"
        }))
    }

    /// Handle model loading
    fn handle_load_model(&self, args: Option<serde_json::Value>) -> FlutterMethodResult {
        let request: serde_json::Map<String, serde_json::Value> = match args {
            Some(serde_json::Value::Object(map)) => map,
            _ => {
                return FlutterMethodResult::Error {
                    code: "INVALID_ARGUMENTS".to_string(),
                    message: "Expected object with engine_id and model_path".to_string(),
                    details: None,
                }
            },
        };

        let engine_id = match request.get("engine_id") {
            Some(serde_json::Value::String(id)) => id.clone(),
            _ => {
                return FlutterMethodResult::Error {
                    code: "MISSING_ENGINE_ID".to_string(),
                    message: "engine_id is required".to_string(),
                    details: None,
                }
            },
        };

        let model_path = match request.get("model_path") {
            Some(serde_json::Value::String(path)) => path.clone(),
            _ => {
                return FlutterMethodResult::Error {
                    code: "MISSING_MODEL_PATH".to_string(),
                    message: "model_path is required".to_string(),
                    details: None,
                }
            },
        };

        // Get configuration
        let config = {
            let configs = self.configurations.lock().expect("Lock poisoned");
            match configs.get(&engine_id) {
                Some(config) => config.clone(),
                None => {
                    return FlutterMethodResult::Error {
                        code: "ENGINE_NOT_INITIALIZED".to_string(),
                        message: format!("Engine '{}' not initialized", engine_id),
                        details: None,
                    }
                },
            }
        };

        // Create and load inference engine
        match MobileInferenceEngine::new(config) {
            Ok(mut engine) => {
                match engine.load_model_from_file(&model_path) {
                    Ok(_) => {
                        // Store the engine
                        {
                            let mut engines = self.engines.lock().expect("Lock poisoned");
                            engines.insert(engine_id.clone(), engine);
                        }

                        // Send event if sink is available
                        if let Some(ref sink) = self.event_sink {
                            let event = serde_json::json!({
                                "type": "model_loaded",
                                "engine_id": engine_id,
                                "model_path": model_path
                            });
                            sink(&event.to_string());
                        }

                        FlutterMethodResult::Success(serde_json::json!({
                            "engine_id": engine_id,
                            "status": "model_loaded",
                            "model_path": model_path
                        }))
                    },
                    Err(e) => FlutterMethodResult::Error {
                        code: "MODEL_LOAD_ERROR".to_string(),
                        message: format!("Failed to load model: {}", e),
                        details: None,
                    },
                }
            },
            Err(e) => FlutterMethodResult::Error {
                code: "ENGINE_CREATION_ERROR".to_string(),
                message: format!("Failed to create inference engine: {}", e),
                details: None,
            },
        }
    }

    /// Handle inference request
    fn handle_inference(&self, args: Option<serde_json::Value>) -> FlutterMethodResult {
        let request: FlutterInferenceRequest = match args {
            Some(value) => match serde_json::from_value(value) {
                Ok(request) => request,
                Err(e) => {
                    return FlutterMethodResult::Error {
                        code: "INVALID_ARGUMENTS".to_string(),
                        message: format!("Failed to parse inference request: {}", e),
                        details: None,
                    }
                },
            },
            None => {
                return FlutterMethodResult::Error {
                    code: "MISSING_ARGUMENTS".to_string(),
                    message: "Inference request is required".to_string(),
                    details: None,
                }
            },
        };

        // Perform inference with mutex locked
        let start_time = std::time::Instant::now();
        let result = {
            let mut engines = self.engines.lock().expect("Lock poisoned");
            match engines.get_mut(&request.engine_id) {
                Some(engine) => self.perform_inference(engine, &request),
                None => {
                    return FlutterMethodResult::Error {
                        code: "ENGINE_NOT_FOUND".to_string(),
                        message: format!("Engine '{}' not found", request.engine_id),
                        details: None,
                    }
                },
            }
        };

        match result {
            Ok(mut response) => {
                let inference_time = start_time.elapsed().as_millis() as f32;
                response.inference_time_ms = inference_time;

                // Update statistics
                {
                    let mut stats = self.statistics.lock().expect("Lock poisoned");
                    if let Some(stat) = stats.get_mut(&request.engine_id) {
                        stat.update_inference(inference_time);
                        stat.update_memory(response.memory_usage_mb as usize);
                    }
                }

                // Send event if sink is available
                if let Some(ref sink) = self.event_sink {
                    let event = serde_json::json!({
                        "type": "inference_completed",
                        "engine_id": request.engine_id,
                        "inference_time_ms": inference_time
                    });
                    sink(&event.to_string());
                }

                FlutterMethodResult::Success(
                    serde_json::to_value(response).expect("JSON serialization failed"),
                )
            },
            Err(e) => FlutterMethodResult::Error {
                code: "INFERENCE_ERROR".to_string(),
                message: format!("Inference failed: {}", e),
                details: None,
            },
        }
    }

    /// Handle device info request
    fn handle_get_device_info(&self) -> FlutterMethodResult {
        match crate::device_info::MobileDeviceDetector::detect() {
            Ok(device_info) => {
                let flutter_device_info = FlutterDeviceInfo {
                    platform: format!("{:?}", device_info.platform),
                    model: device_info.basic_info.model,
                    memory_total_mb: device_info.memory_info.total_memory as u32,
                    memory_available_mb: device_info.memory_info.available_memory as u32,
                    cpu_cores: device_info.cpu_info.core_count as u32,
                    gpu_available: device_info.gpu_info.is_some(),
                    neural_engine_available: device_info.npu_info.is_some(),
                };

                FlutterMethodResult::Success(
                    serde_json::to_value(flutter_device_info).expect("JSON serialization failed"),
                )
            },
            Err(e) => FlutterMethodResult::Error {
                code: "DEVICE_INFO_ERROR".to_string(),
                message: format!("Failed to get device info: {}", e),
                details: None,
            },
        }
    }

    /// Handle performance metrics request
    fn handle_get_performance_metrics(
        &self,
        args: Option<serde_json::Value>,
    ) -> FlutterMethodResult {
        let engine_id = match args {
            Some(serde_json::Value::String(id)) => id,
            Some(serde_json::Value::Object(map)) => match map.get("engine_id") {
                Some(serde_json::Value::String(id)) => id.clone(),
                _ => {
                    return FlutterMethodResult::Error {
                        code: "INVALID_ENGINE_ID".to_string(),
                        message: "engine_id must be a string".to_string(),
                        details: None,
                    }
                },
            },
            _ => {
                return FlutterMethodResult::Error {
                    code: "MISSING_ENGINE_ID".to_string(),
                    message: "engine_id is required".to_string(),
                    details: None,
                }
            },
        };

        let stats = self.statistics.lock().expect("Lock poisoned");
        match stats.get(&engine_id) {
            Some(stat) => {
                let metrics = FlutterPerformanceMetrics {
                    engine_id: engine_id.clone(),
                    total_inferences: stat.total_inferences as u64,
                    avg_inference_time_ms: stat.avg_inference_time_ms,
                    peak_memory_mb: stat.peak_memory_mb as u32,
                    current_memory_mb: stat.memory_usage_mb as u32,
                    throughput_tokens_per_sec: if stat.avg_inference_time_ms > 0.0 {
                        1000.0 / stat.avg_inference_time_ms
                    } else {
                        0.0
                    },
                };

                FlutterMethodResult::Success(
                    serde_json::to_value(metrics).expect("JSON serialization failed"),
                )
            },
            None => FlutterMethodResult::Error {
                code: "ENGINE_NOT_FOUND".to_string(),
                message: format!("Engine '{}' not found", engine_id),
                details: None,
            },
        }
    }

    /// Handle engine disposal
    fn handle_dispose(&self, args: Option<serde_json::Value>) -> FlutterMethodResult {
        let engine_id = match args {
            Some(serde_json::Value::String(id)) => id,
            Some(serde_json::Value::Object(map)) => match map.get("engine_id") {
                Some(serde_json::Value::String(id)) => id.clone(),
                _ => {
                    return FlutterMethodResult::Error {
                        code: "INVALID_ENGINE_ID".to_string(),
                        message: "engine_id must be a string".to_string(),
                        details: None,
                    }
                },
            },
            _ => {
                return FlutterMethodResult::Error {
                    code: "MISSING_ENGINE_ID".to_string(),
                    message: "engine_id is required".to_string(),
                    details: None,
                }
            },
        };

        // Remove engine and associated data
        {
            let mut engines = self.engines.lock().expect("Lock poisoned");
            engines.remove(&engine_id);
        }
        {
            let mut configs = self.configurations.lock().expect("Lock poisoned");
            configs.remove(&engine_id);
        }
        {
            let mut stats = self.statistics.lock().expect("Lock poisoned");
            stats.remove(&engine_id);
        }

        FlutterMethodResult::Success(serde_json::json!({
            "engine_id": engine_id,
            "status": "disposed"
        }))
    }

    /// Handle batch inference request
    fn handle_batch_inference(&self, args: Option<serde_json::Value>) -> FlutterMethodResult {
        let request: serde_json::Map<String, serde_json::Value> = match args {
            Some(serde_json::Value::Object(map)) => map,
            _ => {
                return FlutterMethodResult::Error {
                    code: "INVALID_ARGUMENTS".to_string(),
                    message: "Expected object with engine_id and requests".to_string(),
                    details: None,
                }
            },
        };

        let engine_id = match request.get("engine_id") {
            Some(serde_json::Value::String(id)) => id.clone(),
            _ => {
                return FlutterMethodResult::Error {
                    code: "MISSING_ENGINE_ID".to_string(),
                    message: "engine_id is required".to_string(),
                    details: None,
                }
            },
        };

        let requests: Vec<FlutterInferenceRequest> = match request.get("requests") {
            Some(serde_json::Value::Array(arr)) => {
                match arr
                    .iter()
                    .map(|v| serde_json::from_value(v.clone()))
                    .collect::<std::result::Result<Vec<_>, _>>()
                {
                    Ok(reqs) => reqs,
                    Err(e) => {
                        return FlutterMethodResult::Error {
                            code: "INVALID_REQUESTS".to_string(),
                            message: format!("Failed to parse requests: {}", e),
                            details: None,
                        }
                    },
                }
            },
            _ => {
                return FlutterMethodResult::Error {
                    code: "MISSING_REQUESTS".to_string(),
                    message: "requests array is required".to_string(),
                    details: None,
                }
            },
        };

        // Process batch inference with mutex locked
        let start_time = std::time::Instant::now();
        let (results, total_memory) = {
            let mut engines = self.engines.lock().expect("Lock poisoned");
            match engines.get_mut(&engine_id) {
                Some(engine) => {
                    let mut results = Vec::new();
                    let mut total_memory = 0u32;

                    for req in requests {
                        match self.perform_inference(engine, &req) {
                            Ok(result) => {
                                total_memory += result.memory_usage_mb;
                                results.push(result);
                            },
                            Err(e) => {
                                results.push(FlutterInferenceResponse {
                                    tokens: vec![],
                                    logits: None,
                                    attention_weights: None,
                                    inference_time_ms: 0.0,
                                    memory_usage_mb: 0,
                                });
                            },
                        }
                    }
                    (results, total_memory)
                },
                None => {
                    return FlutterMethodResult::Error {
                        code: "ENGINE_NOT_FOUND".to_string(),
                        message: format!("Engine '{}' not found", engine_id),
                        details: None,
                    }
                },
            }
        };

        let total_time = start_time.elapsed().as_millis() as f32;

        // Update statistics
        {
            let mut stats = self.statistics.lock().expect("Lock poisoned");
            if let Some(stat) = stats.get_mut(&engine_id) {
                stat.update_inference(total_time);
                stat.update_memory(total_memory as usize);
            }
        }

        FlutterMethodResult::Success(serde_json::json!({
            "results": results,
            "total_time_ms": total_time,
            "total_memory_mb": total_memory,
            "success_count": results.len(),
            "error_count": 0 // Simplified for now
        }))
    }

    /// Handle model info request
    fn handle_get_model_info(&self, args: Option<serde_json::Value>) -> FlutterMethodResult {
        let engine_id = match args {
            Some(serde_json::Value::String(id)) => id,
            Some(serde_json::Value::Object(map)) => match map.get("engine_id") {
                Some(serde_json::Value::String(id)) => id.clone(),
                _ => {
                    return FlutterMethodResult::Error {
                        code: "INVALID_ENGINE_ID".to_string(),
                        message: "engine_id must be a string".to_string(),
                        details: None,
                    }
                },
            },
            _ => {
                return FlutterMethodResult::Error {
                    code: "MISSING_ENGINE_ID".to_string(),
                    message: "engine_id is required".to_string(),
                    details: None,
                }
            },
        };

        // Get configuration for the engine
        let config = {
            let configs = self.configurations.lock().expect("Lock poisoned");
            match configs.get(&engine_id) {
                Some(config) => config.clone(),
                None => {
                    return FlutterMethodResult::Error {
                        code: "ENGINE_NOT_FOUND".to_string(),
                        message: format!("Engine '{}' not found", engine_id),
                        details: None,
                    }
                },
            }
        };

        // Get statistics for the engine
        let stats = {
            let stats = self.statistics.lock().expect("Lock poisoned");
            stats.get(&engine_id).cloned()
        };

        let mut model_info = serde_json::json!({
            "engine_id": engine_id,
            "platform": format!("{:?}", config.platform),
            "backend": format!("{:?}", config.backend),
            "memory_optimization": format!("{:?}", config.memory_optimization),
            "max_memory_mb": config.max_memory_mb,
            "use_fp16": config.use_fp16,
            "num_threads": config.num_threads,
            "enable_batching": config.enable_batching,
            "max_batch_size": config.max_batch_size,
            "model_loaded": self.engines.lock().expect("Operation failed").contains_key(&engine_id),
        });

        // Add quantization info if available
        if let Some(ref quantization) = config.quantization {
            model_info["quantization"] = serde_json::json!({
                "scheme": format!("{:?}", quantization.scheme),
                "dynamic": quantization.dynamic,
                "per_channel": quantization.per_channel,
            });
        }

        // Add performance stats if available
        if let Some(stat) = stats {
            model_info["performance"] = serde_json::json!({
                "total_inferences": stat.total_inferences,
                "avg_inference_time_ms": stat.avg_inference_time_ms,
                "peak_memory_mb": stat.peak_memory_mb,
                "current_memory_mb": stat.memory_usage_mb,
            });
        }

        FlutterMethodResult::Success(model_info)
    }

    /// Handle device optimization request
    fn handle_optimize_for_device(&self, args: Option<serde_json::Value>) -> FlutterMethodResult {
        let request: serde_json::Map<String, serde_json::Value> = match args {
            Some(serde_json::Value::Object(map)) => map,
            _ => {
                return FlutterMethodResult::Error {
                    code: "INVALID_ARGUMENTS".to_string(),
                    message: "Expected object with engine_id and current_config".to_string(),
                    details: None,
                }
            },
        };

        let engine_id = match request.get("engine_id") {
            Some(serde_json::Value::String(id)) => id.clone(),
            _ => {
                return FlutterMethodResult::Error {
                    code: "MISSING_ENGINE_ID".to_string(),
                    message: "engine_id is required".to_string(),
                    details: None,
                }
            },
        };

        let current_config: FlutterTrustformersConfig = match request.get("current_config") {
            Some(value) => match serde_json::from_value(value.clone()) {
                Ok(config) => config,
                Err(e) => {
                    return FlutterMethodResult::Error {
                        code: "INVALID_CONFIG".to_string(),
                        message: format!("Failed to parse current configuration: {}", e),
                        details: None,
                    }
                },
            },
            None => {
                return FlutterMethodResult::Error {
                    code: "MISSING_CONFIG".to_string(),
                    message: "current_config is required".to_string(),
                    details: None,
                }
            },
        };

        // Get device information for optimization
        let device_info = match crate::device_info::MobileDeviceDetector::detect() {
            Ok(info) => info,
            Err(e) => {
                return FlutterMethodResult::Error {
                    code: "DEVICE_INFO_ERROR".to_string(),
                    message: format!("Failed to get device info: {}", e),
                    details: None,
                }
            },
        };

        // Create optimized configuration based on device capabilities
        let mut optimized_config = current_config.clone();

        // Optimize backend based on platform and hardware
        let optimal_backend = match device_info.platform {
            crate::MobilePlatform::Ios => {
                if device_info.npu_info.is_some() {
                    "coreml".to_string()
                } else if device_info.gpu_info.is_some() {
                    "gpu".to_string()
                } else {
                    "cpu".to_string()
                }
            },
            crate::MobilePlatform::Android => {
                if device_info.npu_info.is_some() {
                    "nnapi".to_string()
                } else if device_info.gpu_info.is_some() {
                    "gpu".to_string()
                } else {
                    "cpu".to_string()
                }
            },
            _ => "cpu".to_string(),
        };
        optimized_config.backend = optimal_backend;

        // Optimize memory settings based on available memory
        let available_memory_mb = device_info.memory_info.available_memory as u32;
        if available_memory_mb < 512 {
            optimized_config.memory_optimization = "maximum".to_string();
            optimized_config.max_memory_mb = (available_memory_mb * 2 / 3).min(256);
        } else if available_memory_mb < 1024 {
            optimized_config.memory_optimization = "balanced".to_string();
            optimized_config.max_memory_mb = (available_memory_mb / 2).min(512);
        } else {
            optimized_config.memory_optimization = "minimal".to_string();
            optimized_config.max_memory_mb = (available_memory_mb / 3).min(1024);
        }

        // Optimize quantization based on device tier
        let device_tier = if device_info.memory_info.total_memory >= 8192
            && device_info.cpu_info.core_count >= 8
        {
            "high"
        } else if device_info.memory_info.total_memory >= 4096
            && device_info.cpu_info.core_count >= 4
        {
            "medium"
        } else {
            "low"
        };

        optimized_config.quantization = Some(FlutterQuantizationConfig {
            scheme: match device_tier {
                "high" => "fp16".to_string(),
                "medium" => "int8".to_string(),
                "low" => "int4".to_string(),
                _ => "dynamic".to_string(),
            },
            dynamic: device_tier == "low",
            per_channel: device_tier != "low",
        });

        // Optimize threading based on CPU cores
        optimized_config.num_threads = (device_info.cpu_info.core_count as u32 / 2).max(1).min(8);

        // Optimize batching based on device capabilities
        optimized_config.enable_batching = device_tier != "low";
        optimized_config.max_batch_size = match device_tier {
            "high" => 4,
            "medium" => 2,
            "low" => 1,
            _ => 1,
        };

        // Store optimized configuration
        if let Ok(mobile_config) = self.convert_flutter_config_to_mobile(&optimized_config) {
            let mut configs = self.configurations.lock().expect("Lock poisoned");
            configs.insert(engine_id.clone(), mobile_config);
        }

        FlutterMethodResult::Success(serde_json::json!({
            "engine_id": engine_id,
            "optimized_config": optimized_config,
            "device_tier": device_tier,
            "optimization_applied": true,
            "recommendations": [
                format!("Backend optimized to: {}", optimized_config.backend),
                format!("Memory limit optimized to: {}MB", optimized_config.max_memory_mb),
                format!("Quantization scheme: {}", optimized_config.quantization.as_ref().expect("Operation failed").scheme),
                format!("Thread count: {}", optimized_config.num_threads),
                format!("Batching enabled: {}", optimized_config.enable_batching),
            ]
        }))
    }

    /// Convert Flutter config to mobile config
    fn convert_flutter_config_to_mobile(
        &self,
        flutter_config: &FlutterTrustformersConfig,
    ) -> Result<MobileConfig> {
        let platform = match flutter_config.platform.as_str() {
            "ios" => MobilePlatform::Ios,
            "android" => MobilePlatform::Android,
            _ => MobilePlatform::Generic,
        };

        let backend = match flutter_config.backend.as_str() {
            "cpu" => MobileBackend::CPU,
            "coreml" => MobileBackend::CoreML,
            "nnapi" => MobileBackend::NNAPI,
            "gpu" => MobileBackend::GPU,
            _ => MobileBackend::CPU,
        };

        let memory_optimization = match flutter_config.memory_optimization.as_str() {
            "minimal" => crate::MemoryOptimization::Minimal,
            "balanced" => crate::MemoryOptimization::Balanced,
            "maximum" => crate::MemoryOptimization::Maximum,
            _ => crate::MemoryOptimization::Balanced,
        };

        let quantization = flutter_config.quantization.as_ref().map(|q| {
            let scheme = match q.scheme.as_str() {
                "int8" => crate::MobileQuantizationScheme::Int8,
                "int4" => crate::MobileQuantizationScheme::Int4,
                "fp16" => crate::MobileQuantizationScheme::FP16,
                _ => crate::MobileQuantizationScheme::Dynamic,
            };

            crate::MobileQuantizationConfig {
                scheme,
                dynamic: q.dynamic,
                per_channel: q.per_channel,
            }
        });

        let config = MobileConfig {
            platform,
            backend,
            memory_optimization,
            max_memory_mb: flutter_config.max_memory_mb as usize,
            use_fp16: flutter_config.use_fp16,
            quantization,
            num_threads: flutter_config.num_threads as usize,
            enable_batching: flutter_config.enable_batching,
            max_batch_size: flutter_config.max_batch_size as usize,
        };

        config.validate()?;
        Ok(config)
    }

    /// Perform inference using the mobile engine
    fn perform_inference(
        &self,
        engine: &mut MobileInferenceEngine,
        request: &FlutterInferenceRequest,
    ) -> Result<FlutterInferenceResponse> {
        // Convert input data to tensors
        let input_ids = Tensor::from_vec(
            request.input_ids.clone().into_iter().map(|x| x as f32).collect(),
            &[1, request.input_ids.len()],
        )?;

        let attention_mask = request
            .attention_mask
            .as_ref()
            .map(|mask| {
                Tensor::from_vec(
                    mask.clone().into_iter().map(|x| x as f32).collect(),
                    &[1, mask.len()],
                )
            })
            .transpose()?;

        let token_type_ids = request
            .token_type_ids
            .as_ref()
            .map(|ids| {
                Tensor::from_vec(
                    ids.clone().into_iter().map(|x| x as f32).collect(),
                    &[1, ids.len()],
                )
            })
            .transpose()?;

        // Prepare inference options
        let mut inference_options = std::collections::HashMap::new();
        if let Some(max_length) = request.max_length {
            inference_options.insert("max_length".to_string(), max_length.to_string());
        }
        if let Some(temperature) = request.temperature {
            inference_options.insert("temperature".to_string(), temperature.to_string());
        }
        if let Some(top_p) = request.top_p {
            inference_options.insert("top_p".to_string(), top_p.to_string());
        }
        if let Some(top_k) = request.top_k {
            inference_options.insert("top_k".to_string(), top_k.to_string());
        }
        inference_options.insert("do_sample".to_string(), request.do_sample.to_string());

        // Perform inference
        let output = engine.inference(&input_ids)?;

        // Extract tokens from output
        let tokens = output.data()?.iter().map(|&x| x as i64).collect::<Vec<_>>();

        // Get current memory usage (simplified)
        let memory_usage_mb = 128; // This would be calculated from actual memory usage

        Ok(FlutterInferenceResponse {
            tokens,
            logits: None,            // Could be extracted from output if needed
            attention_weights: None, // Could be extracted if available
            inference_time_ms: 0.0,  // Will be set by caller
            memory_usage_mb,
        })
    }
}

impl Default for FlutterChannelManager {
    fn default() -> Self {
        Self::new()
    }
}

// C FFI exports for Flutter Dart FFI integration

/// Initialize Flutter channel manager
#[no_mangle]
pub extern "C" fn flutter_trustformers_init() -> *mut FlutterChannelManager {
    Box::into_raw(Box::new(FlutterChannelManager::new()))
}

/// Handle method call from Flutter
#[no_mangle]
pub extern "C" fn flutter_trustformers_handle_call(
    manager: *mut FlutterChannelManager,
    method_call_json: *const c_char,
) -> *mut c_char {
    if manager.is_null() || method_call_json.is_null() {
        return std::ptr::null_mut();
    }

    let manager = unsafe { &*manager };
    let method_call_str = unsafe { CStr::from_ptr(method_call_json) };

    match method_call_str.to_str() {
        Ok(json_str) => match serde_json::from_str::<FlutterMethodCall>(json_str) {
            Ok(call) => {
                let result = manager.handle_method_call(call);
                match serde_json::to_string(&result) {
                    Ok(result_json) => match CString::new(result_json) {
                        Ok(c_str) => c_str.into_raw(),
                        Err(_) => std::ptr::null_mut(),
                    },
                    Err(_) => std::ptr::null_mut(),
                }
            },
            Err(_) => std::ptr::null_mut(),
        },
        Err(_) => std::ptr::null_mut(),
    }
}

/// Set event sink for streaming updates
#[no_mangle]
pub extern "C" fn flutter_trustformers_set_event_sink(
    manager: *mut FlutterChannelManager,
    event_sink: extern "C" fn(*const c_char),
) {
    if manager.is_null() {
        return;
    }

    let manager = unsafe { &mut *manager };
    let sink: FlutterEventSink = Box::new(move |event: &str| {
        if let Ok(c_str) = CString::new(event) {
            event_sink(c_str.as_ptr());
        }
    });

    manager.set_event_sink(sink);
}

/// Dispose Flutter channel manager
#[no_mangle]
pub extern "C" fn flutter_trustformers_dispose(manager: *mut FlutterChannelManager) {
    if !manager.is_null() {
        unsafe {
            let _ = Box::from_raw(manager);
        }
    }
}

/// Free C string allocated by Rust
#[no_mangle]
pub extern "C" fn flutter_trustformers_free_string(s: *mut c_char) {
    if !s.is_null() {
        unsafe {
            let _ = CString::from_raw(s);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_flutter_channel_manager_creation() {
        let manager = FlutterChannelManager::new();
        assert!(manager.engines.lock().expect("Operation failed").is_empty());
        assert!(manager.configurations.lock().expect("Operation failed").is_empty());
        assert!(manager.statistics.lock().expect("Operation failed").is_empty());
    }

    #[test]
    fn test_flutter_config_conversion() {
        let manager = FlutterChannelManager::new();
        let flutter_config = FlutterTrustformersConfig {
            engine_id: "test".to_string(),
            model_path: "/test/model".to_string(),
            platform: "ios".to_string(),
            backend: "coreml".to_string(),
            memory_optimization: "balanced".to_string(),
            max_memory_mb: 512,
            use_fp16: true,
            quantization: Some(FlutterQuantizationConfig {
                scheme: "fp16".to_string(),
                dynamic: false,
                per_channel: true,
            }),
            num_threads: 4,
            enable_batching: true,
            max_batch_size: 4,
        };

        let mobile_config = manager
            .convert_flutter_config_to_mobile(&flutter_config)
            .expect("Operation failed");
        assert_eq!(mobile_config.platform, MobilePlatform::Ios);
        assert_eq!(mobile_config.backend, MobileBackend::CoreML);
        assert!(mobile_config.use_fp16);
        assert!(mobile_config.enable_batching);
    }

    #[test]
    fn test_method_call_handling() {
        let manager = FlutterChannelManager::new();

        // Test unknown method
        let call = FlutterMethodCall {
            method: "unknown_method".to_string(),
            arguments: None,
        };

        let result = manager.handle_method_call(call);
        match result {
            FlutterMethodResult::Error { code, .. } => {
                assert_eq!(code, "METHOD_NOT_FOUND");
            },
            result => panic!(
                "Expected FlutterMethodResult::Error for unknown method, got {:?}",
                result
            ),
        }
    }

    #[test]
    fn test_device_info_handling() {
        let manager = FlutterChannelManager::new();
        let result = manager.handle_get_device_info();

        // Should return either success or error, but not panic
        match result {
            FlutterMethodResult::Success(_) => {},
            FlutterMethodResult::Error { .. } => {},
        }
    }

    #[test]
    fn test_initialization_handling() {
        let manager = FlutterChannelManager::new();
        let config = FlutterTrustformersConfig {
            engine_id: "test_engine".to_string(),
            model_path: "/test/model".to_string(),
            platform: "generic".to_string(),
            backend: "cpu".to_string(),
            memory_optimization: "balanced".to_string(),
            max_memory_mb: 512,
            use_fp16: false,
            quantization: None,
            num_threads: 2,
            enable_batching: false,
            max_batch_size: 1,
        };

        let call = FlutterMethodCall {
            method: "initialize".to_string(),
            arguments: Some(serde_json::to_value(config).expect("Operation failed")),
        };

        let result = manager.handle_method_call(call);
        match result {
            FlutterMethodResult::Success(value) => {
                assert!(value.get("engine_id").is_some());
                assert_eq!(
                    value.get("status"),
                    Some(&serde_json::Value::String("initialized".to_string()))
                );
            },
            FlutterMethodResult::Error { code, message, .. } => {
                panic!(
                    "Initialization should have succeeded, but failed with error: {} - {}",
                    code, message
                );
            },
        }

        // Verify configuration was stored
        assert!(manager
            .configurations
            .lock()
            .expect("Operation failed")
            .contains_key("test_engine"));
        assert!(manager.statistics.lock().expect("Operation failed").contains_key("test_engine"));
    }

    #[test]
    fn test_performance_metrics_handling() {
        let manager = FlutterChannelManager::new();

        // Test with non-existent engine
        let call = FlutterMethodCall {
            method: "getPerformanceMetrics".to_string(),
            arguments: Some(serde_json::Value::String("non_existent".to_string())),
        };

        let result = manager.handle_method_call(call);
        match result {
            FlutterMethodResult::Error { code, .. } => {
                assert_eq!(code, "ENGINE_NOT_FOUND");
            },
            result => panic!(
                "Expected FlutterMethodResult::Error for non-existent engine, got {:?}",
                result
            ),
        }
    }
}