hippox 0.3.6

🦛A reliable AI agent and skills orchestration runtime engine.
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
//! # Archive Module
//!
//! This module provides skills for creating and extracting compressed archives
//! in various formats including ZIP, TAR (with gzip/bzip2 compression), and
//! standalone compression (gzip/bzip2).
//!
//! ## Available Skills
//!
//! - `ArchiveZipCreateSkill`: Create ZIP archives from files/directories
//! - `ArchiveZipExtractSkill`: Extract ZIP archives
//! - `ArchiveTarCreateSkill`: Create TAR archives (optionally compressed)
//! - `ArchiveTarExtractSkill`: Extract TAR archives (optionally compressed)
//! - `ArchiveCompressSkill`: Compress single files using gzip or bzip2
//!
//! ## Usage Examples
//!
//! ```rust,ignore
//! // Create a ZIP archive
//! let skill = ArchiveZipCreateSkill;
//! let params = HashMap::from([
//!     ("sources", json!(["/path/to/file1", "/path/to/dir"])),
//!     ("destination", json!("/path/to/archive.zip")),
//! ]);
//! let result = skill.execute(&params).await?;
//!
//! // Extract a TAR.GZ archive
//! let skill = ArchiveTarExtractSkill;
//! let params = HashMap::from([
//!     ("archive", json!("/path/to/archive.tar.gz")),
//!     ("destination", json!("/path/to/extract")),
//! ]);
//! let result = skill.execute(&params).await?;
//! ```

use anyhow::Result;
use bzip2::Compression as BzCompression;
use bzip2::read::BzDecoder;
use bzip2::write::BzEncoder;
use flate2::Compression;
use flate2::read::GzDecoder;
use flate2::write::GzEncoder;
use serde_json::{Value, json};
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::{BufReader, BufWriter, Read, Write};
use std::path::{Path, PathBuf};
use tar::{Archive, Builder};
use walkdir::WalkDir;
use zip::ZipWriter;
use zip::read::ZipArchive;
use zip::write::FileOptions;

use crate::executors::types::{Skill, SkillParameter};
use crate::executors::{file_exists, validate_path};

/// Skill for creating ZIP archives from files and directories.
///
/// This skill compresses multiple files and directories into a single ZIP archive.
/// It supports configurable compression levels and preserves directory structure
/// optionally.
///
/// # Parameters
///
/// * `sources` (required): Array of file or directory paths to include
/// * `destination` (required): Output path (must end with .zip)
/// * `compression_level` (optional): 0-9, default 6
/// * `preserve_paths` (optional): Keep directory structure, default true
///
/// # Example
///
/// ```json
/// {
///     "action": "archive_zip_create",
///     "parameters": {
///         "sources": ["/home/user/docs", "/home/user/readme.txt"],
///         "destination": "/home/user/backup.zip",
///         "compression_level": 9
///     }
/// }
/// ```
#[derive(Debug)]
pub struct ArchiveZipCreateSkill;

#[async_trait::async_trait]
impl Skill for ArchiveZipCreateSkill {
    fn name(&self) -> &str {
        "archive_zip_create"
    }

    fn description(&self) -> &str {
        "Create a ZIP archive from files or directories"
    }

    fn usage_hint(&self) -> &str {
        "Use this skill when you need to compress multiple files or directories into a single ZIP archive. Provide source paths and the destination archive path."
    }

    fn parameters(&self) -> Vec<SkillParameter> {
        vec![
            SkillParameter {
                name: "sources".to_string(),
                param_type: "array".to_string(),
                description: "Array of file or directory paths to include in the archive"
                    .to_string(),
                required: true,
                default: None,
                example: Some(json!(["/home/user/docs", "/home/user/notes.txt"])),
                enum_values: None,
            },
            SkillParameter {
                name: "destination".to_string(),
                param_type: "string".to_string(),
                description: "Path where to create the ZIP archive (should end with .zip)"
                    .to_string(),
                required: true,
                default: None,
                example: Some(json!("/home/user/archive.zip")),
                enum_values: None,
            },
            SkillParameter {
                name: "compression_level".to_string(),
                param_type: "integer".to_string(),
                description: "Compression level (0-9, where 0=none, 9=best compression)"
                    .to_string(),
                required: false,
                default: Some(Value::Number(6.into())),
                example: Some(Value::Number(9.into())),
                enum_values: None,
            },
            SkillParameter {
                name: "preserve_paths".to_string(),
                param_type: "boolean".to_string(),
                description: "Whether to preserve directory structure in the archive".to_string(),
                required: false,
                default: Some(Value::Bool(true)),
                example: Some(Value::Bool(true)),
                enum_values: None,
            },
        ]
    }

    fn example_call(&self) -> Value {
        json!({
            "action": "archive_zip_create",
            "parameters": {
                "sources": ["/home/user/documents", "/home/user/readme.txt"],
                "destination": "/home/user/backup.zip",
                "compression_level": 6,
                "preserve_paths": true
            }
        })
    }

    fn example_output(&self) -> String {
        "Successfully created ZIP archive at /home/user/backup.zip containing 15 files".to_string()
    }

    fn category(&self) -> &str {
        "archive"
    }

    async fn execute(&self, parameters: &HashMap<String, Value>) -> Result<String> {
        let sources = parameters
            .get("sources")
            .and_then(|v| v.as_array())
            .ok_or_else(|| {
                anyhow::anyhow!("Missing or invalid 'sources' parameter (expected array)")
            })?;
        let destination = parameters
            .get("destination")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Missing 'destination' parameter"))?;
        let compression_level = parameters
            .get("compression_level")
            .and_then(|v| v.as_u64())
            .unwrap_or(6)
            .min(9) as u16;
        let preserve_paths = parameters
            .get("preserve_paths")
            .and_then(|v| v.as_bool())
            .unwrap_or(true);
        let dest_path = validate_path(destination, None)?;
        if !destination.ends_with(".zip") {
            anyhow::bail!("Destination file must have .zip extension");
        }
        if let Some(parent) = dest_path.parent() {
            fs::create_dir_all(parent)?;
        }
        let file = File::create(&dest_path)?;
        let mut zip = ZipWriter::new(BufWriter::new(file));
        let options: FileOptions<()> = FileOptions::default()
            .compression_method(zip::CompressionMethod::Deflated)
            .compression_level(Some(compression_level.into()));
        let mut file_count = 0;
        let mut total_size = 0;
        for source in sources {
            let source_path_str = source
                .as_str()
                .ok_or_else(|| anyhow::anyhow!("Source path must be a string"))?;
            let source_path = validate_path(source_path_str, None)?;
            if !file_exists(&source_path.to_string_lossy()) {
                anyhow::bail!("Source path not found: {}", source_path_str);
            }
            let base_path = if preserve_paths {
                source_path.parent().unwrap_or(&source_path).to_path_buf()
            } else {
                PathBuf::new()
            };
            if source_path.is_dir() {
                for entry in WalkDir::new(&source_path)
                    .into_iter()
                    .filter_map(|e| e.ok())
                    .filter(|e| e.path().is_file())
                {
                    let file_path = entry.path();
                    let relative_path: &Path = if preserve_paths {
                        file_path.strip_prefix(&base_path).unwrap_or(file_path)
                    } else {
                        let file_name = file_path.file_name().unwrap_or_default();
                        Path::new(file_name)
                    };
                    zip.start_file(relative_path.to_string_lossy(), options)?;
                    let mut file = File::open(file_path)?;
                    let mut buffer = Vec::new();
                    file.read_to_end(&mut buffer)?;
                    total_size += buffer.len();
                    zip.write_all(&buffer)?;
                    file_count += 1;
                }
            } else {
                let relative_path: &Path = if preserve_paths {
                    source_path.strip_prefix(&base_path).unwrap_or(&source_path)
                } else {
                    let file_name = source_path.file_name().unwrap_or_default();
                    Path::new(file_name)
                };
                zip.start_file(relative_path.to_string_lossy(), options)?;
                let mut file = File::open(&source_path)?;
                let mut buffer = Vec::new();
                file.read_to_end(&mut buffer)?;
                total_size += buffer.len();
                zip.write_all(&buffer)?;
                file_count += 1;
            }
        }
        zip.finish()?;
        Ok(format!(
            "Successfully created ZIP archive at {} containing {} file(s) (total size: {} bytes)",
            destination, file_count, total_size
        ))
    }

    fn validate(&self, parameters: &HashMap<String, Value>) -> Result<()> {
        if parameters
            .get("sources")
            .and_then(|v| v.as_array())
            .is_none()
        {
            anyhow::bail!("Missing or invalid 'sources' parameter (expected array)");
        }
        if parameters
            .get("destination")
            .and_then(|v| v.as_str())
            .is_none()
        {
            anyhow::bail!("Missing 'destination' parameter");
        }
        Ok(())
    }
}

/// Skill for extracting ZIP archives to a destination directory.
///
/// This skill extracts all files from a ZIP archive, preserving directory structure.
/// It supports optional overwriting of existing files.
///
/// # Parameters
///
/// * `archive` (required): Path to the ZIP archive file
/// * `destination` (required): Directory where to extract the files
/// * `overwrite` (optional): Whether to overwrite existing files, default false
///
/// # Example
///
/// ```json
/// {
///     "action": "archive_zip_extract",
///     "parameters": {
///         "archive": "/home/user/backup.zip",
///         "destination": "/home/user/extracted",
///         "overwrite": true
///     }
/// }
/// ```
#[derive(Debug)]
pub struct ArchiveZipExtractSkill;

#[async_trait::async_trait]
impl Skill for ArchiveZipExtractSkill {
    fn name(&self) -> &str {
        "archive_zip_extract"
    }

    fn description(&self) -> &str {
        "Extract a ZIP archive to a destination directory"
    }

    fn usage_hint(&self) -> &str {
        "Use this skill when you need to extract files from a ZIP archive. Provide the archive path and the destination directory."
    }

    fn parameters(&self) -> Vec<SkillParameter> {
        vec![
            SkillParameter {
                name: "archive".to_string(),
                param_type: "string".to_string(),
                description: "Path to the ZIP archive file".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("/home/user/archive.zip".to_string())),
                enum_values: None,
            },
            SkillParameter {
                name: "destination".to_string(),
                param_type: "string".to_string(),
                description: "Directory where to extract the files".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("/home/user/extracted".to_string())),
                enum_values: None,
            },
            SkillParameter {
                name: "overwrite".to_string(),
                param_type: "boolean".to_string(),
                description: "Whether to overwrite existing files".to_string(),
                required: false,
                default: Some(Value::Bool(false)),
                example: Some(Value::Bool(true)),
                enum_values: None,
            },
        ]
    }

    fn example_call(&self) -> Value {
        json!({
            "action": "archive_zip_extract",
            "parameters": {
                "archive": "/home/user/backup.zip",
                "destination": "/home/user/extracted",
                "overwrite": true
            }
        })
    }

    fn example_output(&self) -> String {
        "Successfully extracted 15 files from /home/user/backup.zip to /home/user/extracted"
            .to_string()
    }

    fn category(&self) -> &str {
        "archive"
    }

    async fn execute(&self, parameters: &HashMap<String, Value>) -> Result<String> {
        let archive = parameters
            .get("archive")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Missing 'archive' parameter"))?;
        let destination = parameters
            .get("destination")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Missing 'destination' parameter"))?;
        let overwrite = parameters
            .get("overwrite")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);
        let archive_path = validate_path(archive, None)?;
        let dest_path = validate_path(destination, None)?;
        if !file_exists(&archive_path.to_string_lossy()) {
            anyhow::bail!("Archive file not found: {}", archive);
        }
        if !archive.ends_with(".zip") {
            anyhow::bail!("File must have .zip extension");
        }
        fs::create_dir_all(&dest_path)?;
        let file = File::open(&archive_path)?;
        let mut zip = ZipArchive::new(BufReader::new(file))?;
        let mut file_count = 0;
        let mut extracted_size = 0;
        for i in 0..zip.len() {
            let mut entry = zip.by_index(i)?;
            let entry_path = dest_path.join(entry.name());
            if entry.is_dir() {
                fs::create_dir_all(&entry_path)?;
                continue;
            }
            if entry_path.exists() && !overwrite {
                continue;
            }
            if let Some(parent) = entry_path.parent() {
                fs::create_dir_all(parent)?;
            }
            let mut outfile = File::create(&entry_path)?;
            let mut buffer = Vec::new();
            std::io::copy(&mut entry, &mut buffer)?;
            extracted_size += buffer.len();
            outfile.write_all(&buffer)?;
            file_count += 1;
        }
        Ok(format!(
            "Successfully extracted {} file(s) from {} to {} (total size: {} bytes)",
            file_count, archive, destination, extracted_size
        ))
    }

    fn validate(&self, parameters: &HashMap<String, Value>) -> Result<()> {
        if parameters.get("archive").and_then(|v| v.as_str()).is_none() {
            anyhow::bail!("Missing 'archive' parameter");
        }
        if parameters
            .get("destination")
            .and_then(|v| v.as_str())
            .is_none()
        {
            anyhow::bail!("Missing 'destination' parameter");
        }
        Ok(())
    }
}

/// Skill for creating TAR archives (optionally with gzip/bzip2 compression).
///
/// This skill creates TAR archives that can optionally be compressed with gzip or bzip2.
/// It supports adding multiple files and directories while preserving directory structure.
///
/// # Parameters
///
/// * `sources` (required): Array of file or directory paths to include
/// * `destination` (required): Output path (.tar, .tar.gz, .tgz, .tar.bz2, .tbz2)
/// * `preserve_paths` (optional): Keep directory structure, default true
///
/// # Example
///
/// ```json
/// {
///     "action": "archive_tar_create",
///     "parameters": {
///         "sources": ["/home/user/documents", "/home/user/notes.txt"],
///         "destination": "/home/user/backup.tar.gz"
///     }
/// }
/// ```
#[derive(Debug)]
pub struct ArchiveTarCreateSkill;

#[async_trait::async_trait]
impl Skill for ArchiveTarCreateSkill {
    fn name(&self) -> &str {
        "archive_tar_create"
    }

    fn description(&self) -> &str {
        "Create a TAR archive from files or directories (optionally compressed)"
    }

    fn usage_hint(&self) -> &str {
        "Use this skill when you need to create a TAR archive (optionally with gzip/bzip2 compression). Provide source paths and the destination archive path."
    }

    fn parameters(&self) -> Vec<SkillParameter> {
        vec![
            SkillParameter {
                name: "sources".to_string(),
                param_type: "array".to_string(),
                description: "Array of file or directory paths to include in the archive".to_string(),
                required: true,
                default: None,
                example: Some(json!(["/home/user/data", "/home/user/config.json"])),
                enum_values: None,
            },
            SkillParameter {
                name: "destination".to_string(),
                param_type: "string".to_string(),
                description: "Path where to create the TAR archive (can end with .tar, .tar.gz, .tgz, .tar.bz2, .tbz2)".to_string(),
                required: true,
                default: None,
                example: Some(json!("/home/user/archive.tar.gz")),
                enum_values: None,
            },
            SkillParameter {
                name: "preserve_paths".to_string(),
                param_type: "boolean".to_string(),
                description: "Whether to preserve directory structure in the archive".to_string(),
                required: false,
                default: Some(Value::Bool(true)),
                example: Some(Value::Bool(true)),
                enum_values: None,
            },
        ]
    }

    fn example_call(&self) -> Value {
        json!({
            "action": "archive_tar_create",
            "parameters": {
                "sources": ["/home/user/documents", "/home/user/notes.txt"],
                "destination": "/home/user/backup.tar.gz",
                "preserve_paths": true
            }
        })
    }

    fn example_output(&self) -> String {
        "Successfully created TAR archive at /home/user/backup.tar.gz containing 8 files"
            .to_string()
    }

    fn category(&self) -> &str {
        "archive"
    }

    async fn execute(&self, parameters: &HashMap<String, Value>) -> Result<String> {
        let sources = parameters
            .get("sources")
            .and_then(|v| v.as_array())
            .ok_or_else(|| {
                anyhow::anyhow!("Missing or invalid 'sources' parameter (expected array)")
            })?;
        let destination = parameters
            .get("destination")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Missing 'destination' parameter"))?;
        let preserve_paths = parameters
            .get("preserve_paths")
            .and_then(|v| v.as_bool())
            .unwrap_or(true);
        let dest_path = validate_path(destination, None)?;
        if let Some(parent) = dest_path.parent() {
            fs::create_dir_all(parent)?;
        }
        let dest_str = destination.to_lowercase();
        let is_gzip = dest_str.ends_with(".tar.gz") || dest_str.ends_with(".tgz");
        let is_bzip2 = dest_str.ends_with(".tar.bz2") || dest_str.ends_with(".tbz2");
        if !is_gzip && !is_bzip2 && !dest_str.ends_with(".tar") {
            anyhow::bail!("Destination must have .tar, .tar.gz/.tgz, or .tar.bz2/.tbz2 extension");
        }
        let file = File::create(&dest_path)?;
        let mut file_count = 0;
        let total_size = 0;
        let result: Result<String> = if is_gzip {
            let gz_encoder = GzEncoder::new(file, Compression::default());
            let mut tar_builder = Builder::new(gz_encoder);
            file_count = Self::add_to_tar(&mut tar_builder, sources, preserve_paths)?;
            tar_builder.finish()?;
            Ok(format!(
                "Successfully created GZIP-compressed TAR archive at {} containing {} file(s)",
                destination, file_count
            ))
        } else if is_bzip2 {
            let bz_encoder = BzEncoder::new(file, BzCompression::default());
            let mut tar_builder = Builder::new(bz_encoder);
            file_count = Self::add_to_tar(&mut tar_builder, sources, preserve_paths)?;
            tar_builder.finish()?;
            Ok(format!(
                "Successfully created BZIP2-compressed TAR archive at {} containing {} file(s)",
                destination, file_count
            ))
        } else {
            let mut tar_builder = Builder::new(file);
            file_count = Self::add_to_tar(&mut tar_builder, sources, preserve_paths)?;
            tar_builder.finish()?;
            Ok(format!(
                "Successfully created TAR archive at {} containing {} file(s)",
                destination, file_count
            ))
        };
        result
    }

    fn validate(&self, parameters: &HashMap<String, Value>) -> Result<()> {
        if parameters
            .get("sources")
            .and_then(|v| v.as_array())
            .is_none()
        {
            anyhow::bail!("Missing or invalid 'sources' parameter (expected array)");
        }
        if parameters
            .get("destination")
            .and_then(|v| v.as_str())
            .is_none()
        {
            anyhow::bail!("Missing 'destination' parameter");
        }
        Ok(())
    }
}

impl ArchiveTarCreateSkill {
    /// Helper function to add files and directories to a TAR archive.
    ///
    /// Recursively walks through directories and adds all files to the archive builder.
    /// If preserve_paths is false, only file names are used (flattening the structure).
    fn add_to_tar<W: Write>(
        tar_builder: &mut Builder<W>,
        sources: &[Value],
        preserve_paths: bool,
    ) -> Result<usize> {
        let mut file_count = 0;
        for source in sources {
            let source_path_str = source
                .as_str()
                .ok_or_else(|| anyhow::anyhow!("Source path must be a string"))?;
            let source_path = validate_path(source_path_str, None)?;
            if !file_exists(&source_path.to_string_lossy()) {
                anyhow::bail!("Source path not found: {}", source_path_str);
            }
            let base_path = if preserve_paths {
                source_path.parent().unwrap_or(&source_path).to_path_buf()
            } else {
                PathBuf::new()
            };
            if source_path.is_dir() {
                for entry in WalkDir::new(&source_path)
                    .into_iter()
                    .filter_map(|e| e.ok())
                    .filter(|e| e.path().is_file())
                {
                    let file_path = entry.path();
                    let relative_path: &Path = if preserve_paths {
                        file_path.strip_prefix(&base_path).unwrap_or(file_path)
                    } else {
                        let file_name = file_path.file_name().unwrap_or_default();
                        Path::new(file_name)
                    };
                    tar_builder.append_file(relative_path, &mut File::open(file_path)?)?;
                    file_count += 1;
                }
            } else {
                let relative_path: &Path = if preserve_paths {
                    source_path.strip_prefix(&base_path).unwrap_or(&source_path)
                } else {
                    let file_name = source_path.file_name().unwrap_or_default();
                    Path::new(file_name)
                };
                tar_builder.append_file(relative_path, &mut File::open(&source_path)?)?;
                file_count += 1;
            }
        }
        Ok(file_count)
    }
}

// Helper trait extensions (unused, kept for potential future use)
trait TryIntoGz {
    fn try_into_gz(self) -> Option<GzEncoder<Vec<u8>>>;
}

trait TryIntoBz {
    fn try_into_bz(self) -> Option<BzEncoder<Vec<u8>>>;
}

impl TryIntoGz for GzEncoder<File> {
    fn try_into_gz(self) -> Option<GzEncoder<Vec<u8>>> {
        None
    }
}

impl TryIntoBz for BzEncoder<File> {
    fn try_into_bz(self) -> Option<BzEncoder<Vec<u8>>> {
        None
    }
}

/// Skill for extracting TAR archives (optionally with gzip/bzip2 compression).
///
/// This skill extracts files from TAR archives, automatically detecting and handling
/// gzip or bzip2 compression based on the file extension.
///
/// # Parameters
///
/// * `archive` (required): Path to the TAR archive file
/// * `destination` (required): Directory where to extract the files
/// * `overwrite` (optional): Whether to overwrite existing files, default false
///
/// # Supported Formats
///
/// - `.tar` - Uncompressed TAR
/// - `.tar.gz`, `.tgz` - Gzip-compressed TAR
/// - `.tar.bz2`, `.tbz2` - Bzip2-compressed TAR
///
/// # Example
///
/// ```json
/// {
///     "action": "archive_tar_extract",
///     "parameters": {
///         "archive": "/home/user/backup.tar.gz",
///         "destination": "/home/user/extracted",
///         "overwrite": true
///     }
/// }
/// ```
#[derive(Debug)]
pub struct ArchiveTarExtractSkill;

#[async_trait::async_trait]
impl Skill for ArchiveTarExtractSkill {
    fn name(&self) -> &str {
        "archive_tar_extract"
    }

    fn description(&self) -> &str {
        "Extract a TAR archive (optionally compressed with gzip/bzip2) to a destination directory"
    }

    fn usage_hint(&self) -> &str {
        "Use this skill when you need to extract files from a TAR archive. Provide the archive path and the destination directory. Supports .tar, .tar.gz, .tgz, .tar.bz2, .tbz2 formats."
    }

    fn parameters(&self) -> Vec<SkillParameter> {
        vec![
            SkillParameter {
                name: "archive".to_string(),
                param_type: "string".to_string(),
                description: "Path to the TAR archive file".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("/home/user/archive.tar.gz".to_string())),
                enum_values: None,
            },
            SkillParameter {
                name: "destination".to_string(),
                param_type: "string".to_string(),
                description: "Directory where to extract the files".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("/home/user/extracted".to_string())),
                enum_values: None,
            },
            SkillParameter {
                name: "overwrite".to_string(),
                param_type: "boolean".to_string(),
                description: "Whether to overwrite existing files".to_string(),
                required: false,
                default: Some(Value::Bool(false)),
                example: Some(Value::Bool(true)),
                enum_values: None,
            },
        ]
    }

    fn example_call(&self) -> Value {
        json!({
            "action": "archive_tar_extract",
            "parameters": {
                "archive": "/home/user/backup.tar.gz",
                "destination": "/home/user/extracted",
                "overwrite": true
            }
        })
    }

    fn example_output(&self) -> String {
        "Successfully extracted 15 files from /home/user/backup.tar.gz to /home/user/extracted"
            .to_string()
    }

    fn category(&self) -> &str {
        "archive"
    }

    async fn execute(&self, parameters: &HashMap<String, Value>) -> Result<String> {
        let archive = parameters
            .get("archive")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Missing 'archive' parameter"))?;
        let destination = parameters
            .get("destination")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Missing 'destination' parameter"))?;
        let overwrite = parameters
            .get("overwrite")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);
        let archive_path = validate_path(archive, None)?;
        let dest_path = validate_path(destination, None)?;
        if !file_exists(&archive_path.to_string_lossy()) {
            anyhow::bail!("Archive file not found: {}", archive);
        }
        fs::create_dir_all(&dest_path)?;
        let file = File::open(&archive_path)?;
        let archive_str = archive.to_lowercase();
        let reader: Box<dyn Read> =
            if archive_str.ends_with(".tar.gz") || archive_str.ends_with(".tgz") {
                Box::new(GzDecoder::new(file))
            } else if archive_str.ends_with(".tar.bz2") || archive_str.ends_with(".tbz2") {
                Box::new(BzDecoder::new(file))
            } else if archive_str.ends_with(".tar") {
                Box::new(file)
            } else {
                anyhow::bail!("Archive must have .tar, .tar.gz/.tgz, or .tar.bz2/.tbz2 extension");
            };
        let mut tar_archive = Archive::new(reader);
        let mut file_count = 0;
        let mut extracted_size = 0;
        for entry in tar_archive.entries()? {
            let mut entry = entry?;
            let entry_path = dest_path.join(entry.path()?);
            if entry.header().entry_type().is_dir() {
                fs::create_dir_all(&entry_path)?;
                continue;
            }
            if entry_path.exists() && !overwrite {
                continue;
            }
            if let Some(parent) = entry_path.parent() {
                fs::create_dir_all(parent)?;
            }
            let mut outfile = File::create(&entry_path)?;
            let size = std::io::copy(&mut entry, &mut outfile)?;
            extracted_size += size;
            file_count += 1;
        }
        Ok(format!(
            "Successfully extracted {} file(s) from {} to {} (total size: {} bytes)",
            file_count, archive, destination, extracted_size
        ))
    }

    fn validate(&self, parameters: &HashMap<String, Value>) -> Result<()> {
        if parameters.get("archive").and_then(|v| v.as_str()).is_none() {
            anyhow::bail!("Missing 'archive' parameter");
        }
        if parameters
            .get("destination")
            .and_then(|v| v.as_str())
            .is_none()
        {
            anyhow::bail!("Missing 'destination' parameter");
        }
        Ok(())
    }
}

/// Skill for compressing a single file using gzip or bzip2.
///
/// This skill compresses individual files using either gzip or bzip2 compression.
/// For multiple files or directories, use archive_tar_create or archive_zip_create instead.
///
/// # Parameters
///
/// * `source` (required): Path to the file to compress
/// * `destination` (optional): Output path (defaults to source + .gz or .bz2)
/// * `format` (optional): 'gzip' or 'bzip2', default 'gzip'
/// * `compression_level` (optional): 1-9, default 6
/// * `keep_original` (optional): Keep source file after compression, default false
///
/// # Example
///
/// ```json
/// {
///     "action": "archive_compress",
///     "parameters": {
///         "source": "/home/user/data.txt",
///         "format": "gzip",
///         "compression_level": 9,
///         "keep_original": true
///     }
/// }
/// ```
#[derive(Debug)]
pub struct ArchiveCompressSkill;

#[async_trait::async_trait]
impl Skill for ArchiveCompressSkill {
    fn name(&self) -> &str {
        "archive_compress"
    }

    fn description(&self) -> &str {
        "Compress a single file using gzip or bzip2"
    }

    fn usage_hint(&self) -> &str {
        "Use this skill when you need to compress a single file using gzip or bzip2 compression. For compressing multiple files or directories, use archive_tar_create or archive_zip_create instead."
    }

    fn parameters(&self) -> Vec<SkillParameter> {
        vec![
            SkillParameter {
                name: "source".to_string(),
                param_type: "string".to_string(),
                description: "Path to the file to compress".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("/home/user/document.txt".to_string())),
                enum_values: None,
            },
            SkillParameter {
                name: "destination".to_string(),
                param_type: "string".to_string(),
                description:
                    "Path where to create the compressed file (should end with .gz or .bz2)"
                        .to_string(),
                required: false,
                default: None,
                example: Some(Value::String("/home/user/document.txt.gz".to_string())),
                enum_values: None,
            },
            SkillParameter {
                name: "format".to_string(),
                param_type: "string".to_string(),
                description: "Compression format: 'gzip' or 'bzip2'".to_string(),
                required: false,
                default: Some(Value::String("gzip".to_string())),
                example: Some(Value::String("bzip2".to_string())),
                enum_values: Some(vec!["gzip".to_string(), "bzip2".to_string()]),
            },
            SkillParameter {
                name: "compression_level".to_string(),
                param_type: "integer".to_string(),
                description: "Compression level (1-9, where 1=fastest, 9=best compression)"
                    .to_string(),
                required: false,
                default: Some(Value::Number(6.into())),
                example: Some(Value::Number(9.into())),
                enum_values: None,
            },
            SkillParameter {
                name: "keep_original".to_string(),
                param_type: "boolean".to_string(),
                description: "Whether to keep the original file after compression".to_string(),
                required: false,
                default: Some(Value::Bool(false)),
                example: Some(Value::Bool(true)),
                enum_values: None,
            },
        ]
    }

    fn example_call(&self) -> Value {
        json!({
            "action": "archive_compress",
            "parameters": {
                "source": "/home/user/data.txt",
                "format": "gzip",
                "compression_level": 6,
                "keep_original": false
            }
        })
    }

    fn example_output(&self) -> String {
        "Successfully compressed /home/user/data.txt to /home/user/data.txt.gz (original size: 1024 bytes, compressed size: 512 bytes, ratio: 50.0%)".to_string()
    }

    fn category(&self) -> &str {
        "archive"
    }

    async fn execute(&self, parameters: &HashMap<String, Value>) -> Result<String> {
        let source = parameters
            .get("source")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Missing 'source' parameter"))?;
        let format = parameters
            .get("format")
            .and_then(|v| v.as_str())
            .unwrap_or("gzip");
        let compression_level = parameters
            .get("compression_level")
            .and_then(|v| v.as_u64())
            .unwrap_or(6)
            .min(9)
            .max(1) as u32;
        let keep_original = parameters
            .get("keep_original")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);
        let destination = parameters
            .get("destination")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string())
            .or_else(|| {
                let path = Path::new(source);
                let extension = match format {
                    "gzip" => ".gz",
                    "bzip2" => ".bz2",
                    _ => ".gz",
                };
                Some(format!("{}{}", path.display(), extension))
            })
            .ok_or_else(|| anyhow::anyhow!("Could not determine destination path"))?;
        let source_path = validate_path(source, None)?;
        let dest_path = validate_path(&destination, None)?;
        if !file_exists(&source_path.to_string_lossy()) {
            anyhow::bail!("Source file not found: {}", source);
        }
        if source_path.is_dir() {
            anyhow::bail!("Source must be a file, not a directory");
        }
        if let Some(parent) = dest_path.parent() {
            fs::create_dir_all(parent)?;
        }
        let original_size = fs::metadata(&source_path)?.len();
        let mut source_file = File::open(&source_path)?;
        let mut source_data = Vec::new();
        source_file.read_to_end(&mut source_data)?;
        let compressed_data = match format {
            "gzip" => {
                let mut encoder = GzEncoder::new(Vec::new(), Compression::new(compression_level));
                encoder.write_all(&source_data)?;
                encoder.finish()?
            }
            "bzip2" => {
                let mut encoder = BzEncoder::new(Vec::new(), BzCompression::new(compression_level));
                encoder.write_all(&source_data)?;
                encoder.finish()?
            }
            _ => anyhow::bail!(
                "Unsupported compression format: {}. Use 'gzip' or 'bzip2'",
                format
            ),
        };
        let mut dest_file = File::create(&dest_path)?;
        dest_file.write_all(&compressed_data)?;
        let compressed_size = compressed_data.len() as u64;
        let ratio = if original_size > 0 {
            (compressed_size as f64 / original_size as f64) * 100.0
        } else {
            0.0
        };
        if !keep_original {
            fs::remove_file(&source_path)?;
        }
        Ok(format!(
            "Successfully compressed {} to {} (original size: {} bytes, compressed size: {} bytes, ratio: {:.1}%)",
            source, destination, original_size, compressed_size, ratio
        ))
    }

    fn validate(&self, parameters: &HashMap<String, Value>) -> Result<()> {
        if parameters.get("source").and_then(|v| v.as_str()).is_none() {
            anyhow::bail!("Missing 'source' parameter");
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;
    use std::io::Write;
    use tempfile::tempdir;

    #[tokio::test]
    async fn test_zip_create_and_extract() {
        let temp_dir = tempdir().unwrap();
        let test_dir = temp_dir.path();
        let file1_path = test_dir.join("test1.txt");
        let file2_path = test_dir.join("test2.txt");
        File::create(&file1_path)
            .unwrap()
            .write_all(b"Hello World 1")
            .unwrap();
        File::create(&file2_path)
            .unwrap()
            .write_all(b"Hello World 2")
            .unwrap();
        let zip_path = test_dir.join("test.zip");
        let extract_dir = test_dir.join("extract");
        let create_skill = ArchiveZipCreateSkill;
        let mut params = HashMap::new();
        params.insert(
            "sources".to_string(),
            json!([file1_path.to_str().unwrap(), file2_path.to_str().unwrap()]),
        );
        params.insert("destination".to_string(), json!(zip_path.to_str().unwrap()));
        let result = create_skill.execute(&params).await.unwrap();
        assert!(result.contains("Successfully created ZIP archive"));
        assert!(zip_path.exists());
        let extract_skill = ArchiveZipExtractSkill;
        let mut extract_params = HashMap::new();
        extract_params.insert("archive".to_string(), json!(zip_path.to_str().unwrap()));
        extract_params.insert(
            "destination".to_string(),
            json!(extract_dir.to_str().unwrap()),
        );
        extract_params.insert("overwrite".to_string(), json!(true));
        let extract_result = extract_skill.execute(&extract_params).await.unwrap();
        assert!(extract_result.contains("Successfully extracted"));
        assert!(extract_dir.join("test1.txt").exists());
        assert!(extract_dir.join("test2.txt").exists());
    }

    #[tokio::test]
    async fn test_compress_gzip() {
        let temp_dir = tempdir().unwrap();
        let source_file = temp_dir.path().join("test.txt");
        let content = b"This is test content for compression";
        File::create(&source_file)
            .unwrap()
            .write_all(content)
            .unwrap();
        let compress_skill = ArchiveCompressSkill;
        let mut params = HashMap::new();
        params.insert("source".to_string(), json!(source_file.to_str().unwrap()));
        params.insert("format".to_string(), json!("gzip"));
        params.insert("keep_original".to_string(), json!(true));
        let result = compress_skill.execute(&params).await.unwrap();
        assert!(result.contains("Successfully compressed"));
        let gz_file = temp_dir.path().join("test.txt.gz");
        assert!(gz_file.exists());
        assert!(source_file.exists());
        let gz_reader = GzDecoder::new(File::open(&gz_file).unwrap());
        let mut decompressed = String::new();
        std::io::BufReader::new(gz_reader)
            .read_to_string(&mut decompressed)
            .unwrap();
        assert_eq!(decompressed, String::from_utf8_lossy(content));
    }

    /// Test TAR archive creation and extraction with gzip compression
    #[tokio::test]
    async fn test_tar_create_and_extract_gzip() {
        let temp_dir = tempdir().unwrap();
        let test_dir = temp_dir.path();
        let file1_path = test_dir.join("doc1.txt");
        let file2_path = test_dir.join("doc2.txt");
        let subdir = test_dir.join("subdir");
        fs::create_dir_all(&subdir).unwrap();
        let file3_path = subdir.join("doc3.txt");
        File::create(&file1_path)
            .unwrap()
            .write_all(b"Content of document 1")
            .unwrap();
        File::create(&file2_path)
            .unwrap()
            .write_all(b"Content of document 2")
            .unwrap();
        File::create(&file3_path)
            .unwrap()
            .write_all(b"Content of document 3 in subdirectory")
            .unwrap();
        let tar_path = test_dir.join("archive.tar.gz");
        let extract_dir = test_dir.join("extracted");
        let create_skill = ArchiveTarCreateSkill;
        let mut params = HashMap::new();
        params.insert(
            "sources".to_string(),
            json!([file1_path.to_str().unwrap(), subdir.to_str().unwrap()]),
        );
        params.insert("destination".to_string(), json!(tar_path.to_str().unwrap()));
        params.insert("preserve_paths".to_string(), json!(true));
        let create_result = create_skill.execute(&params).await.unwrap();
        assert!(create_result.contains("Successfully created"));
        assert!(tar_path.exists());
        let extract_skill = ArchiveTarExtractSkill;
        let mut extract_params = HashMap::new();
        extract_params.insert("archive".to_string(), json!(tar_path.to_str().unwrap()));
        extract_params.insert(
            "destination".to_string(),
            json!(extract_dir.to_str().unwrap()),
        );
        extract_params.insert("overwrite".to_string(), json!(true));
        let extract_result = extract_skill.execute(&extract_params).await.unwrap();
        assert!(extract_result.contains("Successfully extracted"));
        assert!(extract_dir.join("doc1.txt").exists());
        assert!(extract_dir.join("doc2.txt").exists());
        assert!(extract_dir.join("subdir/doc3.txt").exists());
        let extracted_content = fs::read_to_string(extract_dir.join("doc1.txt")).unwrap();
        assert_eq!(extracted_content, "Content of document 1");
    }

    /// Test bzip2 compression and decompression with compression ratio verification
    #[tokio::test]
    async fn test_compress_bzip2_with_ratio() {
        let temp_dir = tempdir().unwrap();
        let source_file = temp_dir.path().join("large.txt");
        let content = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".repeat(100);
        File::create(&source_file)
            .unwrap()
            .write_all(content.as_bytes())
            .unwrap();
        let original_size = fs::metadata(&source_file).unwrap().len();
        let compress_skill = ArchiveCompressSkill;
        let mut params = HashMap::new();
        params.insert("source".to_string(), json!(source_file.to_str().unwrap()));
        params.insert("format".to_string(), json!("bzip2"));
        params.insert("compression_level".to_string(), json!(9));
        params.insert("keep_original".to_string(), json!(false));
        let result = compress_skill.execute(&params).await.unwrap();
        assert!(result.contains("Successfully compressed"));
        assert!(result.contains("ratio:"));
        assert!(!source_file.exists());
        let bz2_file = temp_dir.path().join("large.txt.bz2");
        assert!(bz2_file.exists());
        let compressed_size = fs::metadata(&bz2_file).unwrap().len();
        assert!(compressed_size < original_size);
        let bz2_reader = BzDecoder::new(File::open(&bz2_file).unwrap());
        let mut decompressed = String::new();
        std::io::BufReader::new(bz2_reader)
            .read_to_string(&mut decompressed)
            .unwrap();
        assert_eq!(decompressed, content);
    }
}