capns 0.108.50234

Core cap URN and definition system for FGND plugins
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
//! Formal cap definition
//!
//! This module defines the structure for formal cap definitions that include
//! the cap URN, versioning, and metadata. Caps are general-purpose
//! and do not assume any specific domain like files or documents.
//!
//! ## Cap Definition Format
//!
//! Caps use spec IDs in `media_spec` fields that reference definitions
//! in the `media_specs` table. Example:
//!
//! ```json
//! {
//!   "urn": { "tags": { "op": "conversation", "in": "media:string", "out": "my:output.v1" } },
//!   "media_specs": {
//!     "my:output.v1": {
//!       "media_type": "application/json",
//!       "profile_uri": "https://example.com/schema",
//!       "schema": { "type": "object", ... }
//!     }
//!   },
//!   "args": [
//!     { "media_urn": "media:string", "required": true, "sources": [{"cli_flag": "--input"}] }
//!   ],
//!   "output": { "media_urn": "my:output.v1", ... }
//! }
//! ```

use crate::cap_urn::CapUrn;
use crate::media_spec::{resolve_media_urn, MediaSpecDef, MediaSpecError, ResolvedMediaSpec};
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::HashMap;

/// Argument validation rules
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct ArgumentValidation {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min: Option<f64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub max: Option<f64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_length: Option<usize>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_length: Option<usize>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub pattern: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub allowed_values: Option<Vec<String>>,
}

impl ArgumentValidation {
    fn is_empty(&self) -> bool {
        self.min.is_none() &&
        self.max.is_none() &&
        self.min_length.is_none() &&
        self.max_length.is_none() &&
        self.pattern.is_none() &&
        self.allowed_values.is_none()
    }

    /// Create validation with min/max numeric constraints
    pub fn numeric_range(min: Option<f64>, max: Option<f64>) -> Self {
        Self {
            min,
            max,
            min_length: None,
            max_length: None,
            pattern: None,
            allowed_values: None,
        }
    }

    /// Create validation with string length constraints
    pub fn string_length(min_length: Option<usize>, max_length: Option<usize>) -> Self {
        Self {
            min: None,
            max: None,
            min_length,
            max_length,
            pattern: None,
            allowed_values: None,
        }
    }

    /// Create validation with pattern
    pub fn pattern(pattern: String) -> Self {
        Self {
            min: None,
            max: None,
            min_length: None,
            max_length: None,
            pattern: Some(pattern),
            allowed_values: None,
        }
    }

    /// Create validation with allowed values
    pub fn allowed_values(values: Vec<String>) -> Self {
        Self {
            min: None,
            max: None,
            min_length: None,
            max_length: None,
            pattern: None,
            allowed_values: Some(values),
        }
    }
}

/// Source specification for argument input
///
/// Each variant serializes to a distinct JSON object with a unique key:
/// - `{"stdin": "media:..."}`
/// - `{"position": 0}`
/// - `{"cli_flag": "--flag-name"}`
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged, deny_unknown_fields)]
pub enum ArgSource {
    /// Argument can be provided via stdin
    Stdin {
        /// Media URN for stdin input
        stdin: String,
    },
    /// Argument is positional
    Position {
        /// 0-based position in argument list
        position: usize,
    },
    /// Argument uses a CLI flag
    CliFlag {
        /// CLI flag (e.g., "--input" or "-i")
        cli_flag: String,
    },
}

impl ArgSource {
    pub fn get_type(&self) -> &'static str {
        match self {
            ArgSource::Stdin { .. } => "stdin",
            ArgSource::Position { .. } => "position",
            ArgSource::CliFlag { .. } => "cli_flag",
        }
    }

    pub fn stdin_media_urn(&self) -> Option<&str> {
        match self {
            ArgSource::Stdin { stdin } => Some(stdin),
            _ => None,
        }
    }

    pub fn position(&self) -> Option<usize> {
        match self {
            ArgSource::Position { position } => Some(*position),
            _ => None,
        }
    }

    pub fn cli_flag(&self) -> Option<&str> {
        match self {
            ArgSource::CliFlag { cli_flag } => Some(cli_flag),
            _ => None,
        }
    }
}

/// Cap argument definition - media_urn is the unique identifier
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CapArg {
    /// Unique media URN for this argument
    pub media_urn: String,

    /// Whether this argument is required
    pub required: bool,

    /// How this argument can be provided
    pub sources: Vec<ArgSource>,

    /// Human-readable description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub arg_description: Option<String>,

    /// Validation rules
    #[serde(skip_serializing_if = "ArgumentValidation::is_empty", default)]
    pub validation: ArgumentValidation,

    /// Default value for optional arguments
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_value: Option<serde_json::Value>,

    /// Arbitrary metadata
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<serde_json::Value>,
}

impl CapArg {
    /// Create a new cap argument
    pub fn new(media_urn: impl Into<String>, required: bool, sources: Vec<ArgSource>) -> Self {
        Self {
            media_urn: media_urn.into(),
            required,
            sources,
            arg_description: None,
            validation: ArgumentValidation::default(),
            default_value: None,
            metadata: None,
        }
    }

    /// Create a new cap argument with description
    pub fn with_description(
        media_urn: impl Into<String>,
        required: bool,
        sources: Vec<ArgSource>,
        description: impl Into<String>,
    ) -> Self {
        Self {
            media_urn: media_urn.into(),
            required,
            sources,
            arg_description: Some(description.into()),
            validation: ArgumentValidation::default(),
            default_value: None,
            metadata: None,
        }
    }

    /// Create a fully specified argument
    pub fn with_full_definition(
        media_urn: impl Into<String>,
        required: bool,
        sources: Vec<ArgSource>,
        description: Option<String>,
        validation: ArgumentValidation,
        default: Option<serde_json::Value>,
        metadata: Option<serde_json::Value>,
    ) -> Self {
        Self {
            media_urn: media_urn.into(),
            required,
            sources,
            arg_description: description,
            validation,
            default_value: default,
            metadata,
        }
    }

    /// Get the media URN
    pub fn get_media_urn(&self) -> &str {
        &self.media_urn
    }

    /// Resolve this argument's media spec using the provided media_specs table
    ///
    /// # Arguments
    /// * `media_specs` - The media_specs map from the cap definition
    ///
    /// # Errors
    /// Returns `MediaSpecError::UnresolvableMediaUrn` if the media URN cannot be resolved.
    pub fn resolve(&self, media_specs: &HashMap<String, MediaSpecDef>) -> Result<ResolvedMediaSpec, MediaSpecError> {
        resolve_media_urn(&self.media_urn, media_specs)
    }

    /// Check if argument is binary based on resolved media spec
    pub fn is_binary(&self, media_specs: &HashMap<String, MediaSpecDef>) -> Result<bool, MediaSpecError> {
        self.resolve(media_specs).map(|ms| ms.is_binary())
    }

    /// Check if argument is JSON based on resolved media spec
    pub fn is_json(&self, media_specs: &HashMap<String, MediaSpecDef>) -> Result<bool, MediaSpecError> {
        self.resolve(media_specs).map(|ms| ms.is_json())
    }

    /// Get the media type from resolved spec
    pub fn media_type(&self, media_specs: &HashMap<String, MediaSpecDef>) -> Result<String, MediaSpecError> {
        self.resolve(media_specs).map(|ms| ms.media_type)
    }

    /// Get the profile URI from resolved spec
    pub fn profile_uri(&self, media_specs: &HashMap<String, MediaSpecDef>) -> Result<Option<String>, MediaSpecError> {
        self.resolve(media_specs).map(|ms| ms.profile_uri)
    }

    /// Get the schema from resolved spec (if any)
    pub fn schema(&self, media_specs: &HashMap<String, MediaSpecDef>) -> Result<Option<serde_json::Value>, MediaSpecError> {
        self.resolve(media_specs).map(|ms| ms.schema)
    }

    /// Get metadata JSON
    pub fn get_metadata(&self) -> Option<&serde_json::Value> {
        self.metadata.as_ref()
    }

    /// Set metadata JSON
    pub fn set_metadata(&mut self, metadata: serde_json::Value) {
        self.metadata = Some(metadata);
    }

    /// Clear metadata JSON
    pub fn clear_metadata(&mut self) {
        self.metadata = None;
    }
}

/// Output definition
///
/// The `media_urn` field contains a media URN (e.g., "media:object") that
/// references a definition in the cap's `media_specs` table or a built-in primitive.
/// Any output schema should be defined in the media_specs entry, not inline here.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CapOutput {
    /// Media URN referencing a media spec definition
    /// e.g., "media:object" or a custom media URN like "media:my-output"
    pub media_urn: String,

    #[serde(skip_serializing_if = "ArgumentValidation::is_empty", default)]
    pub validation: ArgumentValidation,

    pub output_description: String,

    /// Arbitrary metadata as JSON object
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<serde_json::Value>,
}

impl CapOutput {
    /// Create a new output definition with media URN
    ///
    /// # Arguments
    /// * `media_urn` - Media URN referencing a media_specs entry (e.g., "media:object")
    /// * `description` - Human-readable description of the output
    pub fn new(media_urn: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            media_urn: media_urn.into(),
            output_description: description.into(),
            validation: ArgumentValidation::default(),
            metadata: None,
        }
    }

    /// Create output with validation
    pub fn with_validation(media_urn: impl Into<String>, description: impl Into<String>, validation: ArgumentValidation) -> Self {
        Self {
            media_urn: media_urn.into(),
            output_description: description.into(),
            validation,
            metadata: None,
        }
    }

    /// Create a fully specified output
    pub fn with_full_definition(
        media_urn: impl Into<String>,
        description: impl Into<String>,
        validation: ArgumentValidation,
        metadata: Option<serde_json::Value>,
    ) -> Self {
        Self {
            media_urn: media_urn.into(),
            output_description: description.into(),
            validation,
            metadata,
        }
    }

    /// Get the media URN
    pub fn get_media_urn(&self) -> &str {
        &self.media_urn
    }

    /// Resolve this output's media spec using the provided media_specs table
    ///
    /// # Arguments
    /// * `media_specs` - The media_specs map from the cap definition
    ///
    /// # Errors
    /// Returns `MediaSpecError::UnresolvableMediaUrn` if the media URN cannot be resolved.
    pub fn resolve(&self, media_specs: &HashMap<String, MediaSpecDef>) -> Result<ResolvedMediaSpec, MediaSpecError> {
        resolve_media_urn(&self.media_urn, media_specs)
    }

    /// Check if output is binary based on resolved media spec
    pub fn is_binary(&self, media_specs: &HashMap<String, MediaSpecDef>) -> Result<bool, MediaSpecError> {
        self.resolve(media_specs).map(|ms| ms.is_binary())
    }

    /// Check if output is JSON based on resolved media spec
    pub fn is_json(&self, media_specs: &HashMap<String, MediaSpecDef>) -> Result<bool, MediaSpecError> {
        self.resolve(media_specs).map(|ms| ms.is_json())
    }

    /// Get the media type from resolved spec
    pub fn media_type(&self, media_specs: &HashMap<String, MediaSpecDef>) -> Result<String, MediaSpecError> {
        self.resolve(media_specs).map(|ms| ms.media_type)
    }

    /// Get the profile URI from resolved spec
    pub fn profile_uri(&self, media_specs: &HashMap<String, MediaSpecDef>) -> Result<Option<String>, MediaSpecError> {
        self.resolve(media_specs).map(|ms| ms.profile_uri)
    }

    /// Get the schema from resolved spec (if any)
    pub fn schema(&self, media_specs: &HashMap<String, MediaSpecDef>) -> Result<Option<serde_json::Value>, MediaSpecError> {
        self.resolve(media_specs).map(|ms| ms.schema)
    }

    /// Get metadata JSON
    pub fn get_metadata(&self) -> Option<&serde_json::Value> {
        self.metadata.as_ref()
    }

    /// Set metadata JSON
    pub fn set_metadata(&mut self, metadata: serde_json::Value) {
        self.metadata = Some(metadata);
    }

    /// Clear metadata JSON
    pub fn clear_metadata(&mut self) {
        self.metadata = None;
    }
}

/// Registration attribution - who registered this capability and when
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RegisteredBy {
    /// Username of the user who registered this capability
    pub username: String,

    /// ISO 8601 timestamp of when the capability was registered
    pub registered_at: String,
}

impl RegisteredBy {
    /// Create a new registration attribution
    pub fn new(username: impl Into<String>, registered_at: impl Into<String>) -> Self {
        Self {
            username: username.into(),
            registered_at: registered_at.into(),
        }
    }
}

/// Formal cap definition
///
/// A cap definition includes:
/// - URN with tags (including `op`, `in`, `out` which use spec IDs)
/// - `media_specs` table mapping spec IDs to definitions
/// - Arguments with spec ID references
/// - Output with spec ID reference
#[derive(Debug, Clone, PartialEq)]
pub struct Cap {
    /// Formal cap URN with hierarchical naming
    /// Tags can include `op`, `in`, `out` (which should be spec IDs)
    pub urn: CapUrn,

    /// Human-readable title of the capability (required)
    pub title: String,

    /// Optional description
    pub cap_description: Option<String>,

    /// Optional metadata as key-value pairs
    pub metadata: HashMap<String, String>,

    /// Command string for CLI execution
    pub command: String,

    /// Media spec definitions table
    /// Maps spec IDs to their definitions (string or object form)
    /// Arguments and output `media_spec` fields reference entries here
    pub media_specs: HashMap<String, MediaSpecDef>,

    /// Cap arguments
    pub args: Vec<CapArg>,

    /// Output definition
    pub output: Option<CapOutput>,

    /// Arbitrary metadata as JSON object
    pub metadata_json: Option<serde_json::Value>,

    /// Registration attribution - who registered this capability and when
    pub registered_by: Option<RegisteredBy>,
}

impl Serialize for Cap {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::ser::SerializeStruct;
        let mut state = serializer.serialize_struct("Cap", 10)?;

        // Serialize urn as tags object (including in/out direction specs)
        let mut all_tags = serde_json::Map::new();
        all_tags.insert("in".to_string(), serde_json::Value::String(self.urn.in_spec().to_string()));
        all_tags.insert("out".to_string(), serde_json::Value::String(self.urn.out_spec().to_string()));
        for (k, v) in &self.urn.tags {
            all_tags.insert(k.clone(), serde_json::Value::String(v.clone()));
        }
        state.serialize_field("urn", &serde_json::json!({
            "tags": all_tags
        }))?;

        state.serialize_field("title", &self.title)?;
        state.serialize_field("command", &self.command)?;

        if self.cap_description.is_some() {
            state.serialize_field("cap_description", &self.cap_description)?;
        }

        if !self.metadata.is_empty() {
            state.serialize_field("metadata", &self.metadata)?;
        }

        if !self.media_specs.is_empty() {
            state.serialize_field("media_specs", &self.media_specs)?;
        }

        if !self.args.is_empty() {
            state.serialize_field("args", &self.args)?;
        }

        if self.output.is_some() {
            state.serialize_field("output", &self.output)?;
        }

        if self.metadata_json.is_some() {
            state.serialize_field("metadata_json", &self.metadata_json)?;
        }

        if self.registered_by.is_some() {
            state.serialize_field("registered_by", &self.registered_by)?;
        }

        state.end()
    }
}

impl<'de> Deserialize<'de> for Cap {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct CapRegistry {
            urn: serde_json::Value,
            title: String,
            cap_description: Option<String>,
            #[serde(default)]
            metadata: HashMap<String, String>,
            command: String,
            #[serde(default)]
            media_specs: HashMap<String, MediaSpecDef>,
            #[serde(default)]
            args: Vec<CapArg>,
            output: Option<CapOutput>,
            metadata_json: Option<serde_json::Value>,
            registered_by: Option<RegisteredBy>,
        }

        let registry_cap = CapRegistry::deserialize(deserializer)?;

        // Handle urn field - can be string or object with tags
        let urn = match registry_cap.urn {
            serde_json::Value::String(urn_str) => {
                CapUrn::from_string(&urn_str).map_err(serde::de::Error::custom)?
            },
            serde_json::Value::Object(urn_obj) => {
                if let Some(tags) = urn_obj.get("tags").and_then(|t| t.as_object()) {
                    // Extract required in/out specs first
                    let in_spec = tags.get("in")
                        .and_then(|v| v.as_str())
                        .ok_or_else(|| serde::de::Error::custom("Missing required 'in' tag in urn - caps must declare their input type (use media:void for no input)"))?
                        .to_string();
                    let out_spec = tags.get("out")
                        .and_then(|v| v.as_str())
                        .ok_or_else(|| serde::de::Error::custom("Missing required 'out' tag in urn - caps must declare their output type"))?
                        .to_string();

                    // Build CapUrn with direction specs and other tags
                    let mut urn_builder = crate::cap_urn::CapUrnBuilder::new()
                        .in_spec(&in_spec)
                        .out_spec(&out_spec);
                    for (key, value) in tags {
                        // Skip in/out as they're handled via in_spec/out_spec
                        if key == "in" || key == "out" {
                            continue;
                        }
                        if let Some(val_str) = value.as_str() {
                            urn_builder = urn_builder.tag(key, val_str);
                        }
                    }
                    urn_builder.build().map_err(serde::de::Error::custom)?
                } else {
                    return Err(serde::de::Error::custom("Invalid urn object format"));
                }
            },
            _ => return Err(serde::de::Error::custom("urn must be string or object")),
        };

        Ok(Cap {
            urn,
            title: registry_cap.title,
            cap_description: registry_cap.cap_description,
            metadata: registry_cap.metadata,
            command: registry_cap.command,
            media_specs: registry_cap.media_specs,
            args: registry_cap.args,
            output: registry_cap.output,
            metadata_json: registry_cap.metadata_json,
            registered_by: registry_cap.registered_by,
        })
    }
}

impl Cap {
    /// Create a new cap
    pub fn new(urn: CapUrn, title: String, command: String) -> Self {
        Self {
            urn,
            title,
            cap_description: None,
            metadata: HashMap::new(),
            command,
            media_specs: HashMap::new(),
            args: Vec::new(),
            output: None,
            metadata_json: None,
            registered_by: None,
        }
    }

    /// Create a new cap with description
    pub fn with_description(urn: CapUrn, title: String, command: String, description: String) -> Self {
        Self {
            urn,
            title,
            cap_description: Some(description),
            metadata: HashMap::new(),
            command,
            media_specs: HashMap::new(),
            args: Vec::new(),
            output: None,
            metadata_json: None,
            registered_by: None,
        }
    }

    /// Create a new cap with metadata
    pub fn with_metadata(
        urn: CapUrn,
        title: String,
        command: String,
        metadata: HashMap<String, String>
    ) -> Self {
        Self {
            urn,
            title,
            cap_description: None,
            metadata,
            command,
            media_specs: HashMap::new(),
            args: Vec::new(),
            output: None,
            metadata_json: None,
            registered_by: None,
        }
    }

    /// Create a new cap with description and metadata
    pub fn with_description_and_metadata(
        urn: CapUrn,
        title: String,
        command: String,
        description: String,
        metadata: HashMap<String, String>,
    ) -> Self {
        Self {
            urn,
            title,
            cap_description: Some(description),
            metadata,
            command,
            media_specs: HashMap::new(),
            args: Vec::new(),
            output: None,
            metadata_json: None,
            registered_by: None,
        }
    }

    /// Create a new cap with args
    pub fn with_args(
        urn: CapUrn,
        title: String,
        command: String,
        args: Vec<CapArg>,
    ) -> Self {
        Self {
            urn,
            title,
            cap_description: None,
            metadata: HashMap::new(),
            command,
            media_specs: HashMap::new(),
            args,
            output: None,
            metadata_json: None,
            registered_by: None,
        }
    }

    /// Create a fully specified cap
    pub fn with_full_definition(
        urn: CapUrn,
        title: String,
        description: Option<String>,
        metadata: HashMap<String, String>,
        command: String,
        media_specs: HashMap<String, MediaSpecDef>,
        args: Vec<CapArg>,
        output: Option<CapOutput>,
        metadata_json: Option<serde_json::Value>,
    ) -> Self {
        Self {
            urn,
            title,
            cap_description: description,
            metadata,
            command,
            media_specs,
            args,
            output,
            metadata_json,
            registered_by: None,
        }
    }

    /// Get the stdin media URN from args (first stdin source found)
    pub fn get_stdin_media_urn(&self) -> Option<&str> {
        for arg in &self.args {
            for source in &arg.sources {
                if let ArgSource::Stdin { stdin } = source {
                    return Some(stdin);
                }
            }
        }
        None
    }

    /// Check if this cap accepts stdin
    pub fn accepts_stdin(&self) -> bool {
        self.get_stdin_media_urn().is_some()
    }

    /// Get the media_specs table
    pub fn get_media_specs(&self) -> &HashMap<String, MediaSpecDef> {
        &self.media_specs
    }

    /// Set media_specs
    pub fn set_media_specs(&mut self, media_specs: HashMap<String, MediaSpecDef>) {
        self.media_specs = media_specs;
    }

    /// Add a media spec definition
    pub fn add_media_spec(&mut self, spec_id: impl Into<String>, def: MediaSpecDef) {
        self.media_specs.insert(spec_id.into(), def);
    }

    /// Resolve a spec ID using this cap's media_specs
    pub fn resolve_media_urn(&self, spec_id: &str) -> Result<ResolvedMediaSpec, MediaSpecError> {
        resolve_media_urn(spec_id, &self.media_specs)
    }

    /// Check if this cap matches a request string
    pub fn matches_request(&self, request: &str) -> bool {
        let request_urn = CapUrn::from_string(request).expect("Invalid cap URN in request");
        self.urn.can_handle(&request_urn)
    }

    /// Get the cap URN as a string
    pub fn urn_string(&self) -> String {
        self.urn.to_string()
    }

    /// Check if this cap is more specific than another for the same request
    pub fn is_more_specific_than(&self, other: &Cap, request: &str) -> bool {
        if !self.matches_request(request) || !other.matches_request(request) {
            return false;
        }
        self.urn.is_more_specific_than(&other.urn)
    }

    /// Get a metadata value by key
    pub fn get_metadata(&self, key: &str) -> Option<&String> {
        self.metadata.get(key)
    }

    /// Set a metadata value
    pub fn set_metadata(&mut self, key: String, value: String) {
        self.metadata.insert(key, value);
    }

    /// Remove a metadata value
    pub fn remove_metadata(&mut self, key: &str) -> Option<String> {
        self.metadata.remove(key)
    }

    /// Check if this cap has specific metadata
    pub fn has_metadata(&self, key: &str) -> bool {
        self.metadata.contains_key(key)
    }

    /// Get the registration attribution
    pub fn get_registered_by(&self) -> Option<&RegisteredBy> {
        self.registered_by.as_ref()
    }

    /// Set the registration attribution
    pub fn set_registered_by(&mut self, registered_by: RegisteredBy) {
        self.registered_by = Some(registered_by);
    }

    /// Clear the registration attribution
    pub fn clear_registered_by(&mut self) {
        self.registered_by = None;
    }

    /// Get the command
    pub fn get_command(&self) -> &String {
        &self.command
    }

    /// Set the command
    pub fn set_command(&mut self, command: String) {
        self.command = command;
    }

    /// Get the title
    pub fn get_title(&self) -> &String {
        &self.title
    }

    /// Set the title
    pub fn set_title(&mut self, title: String) {
        self.title = title;
    }

    /// Get the args
    pub fn get_args(&self) -> &Vec<CapArg> {
        &self.args
    }

    /// Set the args
    pub fn set_args(&mut self, args: Vec<CapArg>) {
        self.args = args;
    }

    /// Add an argument
    pub fn add_arg(&mut self, arg: CapArg) {
        self.args.push(arg);
    }

    /// Get the output definition if defined
    pub fn get_output(&self) -> Option<&CapOutput> {
        self.output.as_ref()
    }

    /// Set the output definition
    pub fn set_output(&mut self, output: CapOutput) {
        self.output = Some(output);
    }

    /// Get metadata JSON
    pub fn get_metadata_json(&self) -> Option<&serde_json::Value> {
        self.metadata_json.as_ref()
    }

    /// Set metadata JSON
    pub fn set_metadata_json(&mut self, metadata: serde_json::Value) {
        self.metadata_json = Some(metadata);
    }

    /// Clear metadata JSON
    pub fn clear_metadata_json(&mut self) {
        self.metadata_json = None;
    }
}


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

    // Helper to create test URN with required in/out specs
    fn test_urn(tags: &str) -> String {
        format!("cap:in=media:void;out=media:object;{}", tags)
    }

    #[test]
    fn test_cap_creation() {
        let urn = CapUrn::from_string(&test_urn("op=transform;format=json;data_processing")).unwrap();
        let cap = Cap::new(urn, "Transform JSON Data".to_string(), "test-command".to_string());

        assert!(cap.urn_string().contains("op=transform"));
        assert!(cap.urn_string().contains("in=media:void"));
        assert!(cap.urn_string().contains("out=media:object"));
        assert_eq!(cap.title, "Transform JSON Data");
        assert!(cap.metadata.is_empty());
    }

    #[test]
    fn test_cap_with_metadata() {
        let urn = CapUrn::from_string(&test_urn("op=arithmetic;compute;subtype=math")).unwrap();
        let mut metadata = HashMap::new();
        metadata.insert("precision".to_string(), "double".to_string());
        metadata.insert("operations".to_string(), "add,subtract,multiply,divide".to_string());

        let cap = Cap::with_metadata(urn, "Perform Mathematical Operations".to_string(), "test-command".to_string(), metadata);

        assert_eq!(cap.title, "Perform Mathematical Operations");
        assert_eq!(cap.get_metadata("precision"), Some(&"double".to_string()));
        assert_eq!(cap.get_metadata("operations"), Some(&"add,subtract,multiply,divide".to_string()));
        assert!(cap.has_metadata("precision"));
        assert!(!cap.has_metadata("nonexistent"));
    }

    #[test]
    fn test_cap_matching() {
        // Use type=data_processing key-value instead of flag for proper matching
        let urn = CapUrn::from_string(&test_urn("op=transform;format=json;type=data_processing")).unwrap();
        let cap = Cap::new(urn, "Transform JSON Data".to_string(), "test-command".to_string());

        assert!(cap.matches_request(&test_urn("op=transform;format=json;type=data_processing")));
        assert!(cap.matches_request(&test_urn("op=transform;format=*;type=data_processing")));
        assert!(cap.matches_request(&test_urn("type=data_processing")));
        assert!(!cap.matches_request(&test_urn("type=compute")));
    }

    #[test]
    fn test_cap_title() {
        let urn = CapUrn::from_string(&test_urn("op=extract;target=metadata")).unwrap();
        let mut cap = Cap::new(urn, "Extract Document Metadata".to_string(), "extract-metadata".to_string());

        assert_eq!(cap.get_title(), &"Extract Document Metadata".to_string());
        assert_eq!(cap.title, "Extract Document Metadata");

        cap.set_title("Extract File Metadata".to_string());
        assert_eq!(cap.get_title(), &"Extract File Metadata".to_string());
        assert_eq!(cap.title, "Extract File Metadata");
    }

    #[test]
    fn test_cap_definition_equality() {
        let urn1 = CapUrn::from_string(&test_urn("op=transform;format=json")).unwrap();
        let urn2 = CapUrn::from_string(&test_urn("op=transform;format=json")).unwrap();

        let cap1 = Cap::new(urn1, "Transform JSON Data".to_string(), "transform".to_string());
        let cap2 = Cap::new(urn2.clone(), "Transform JSON Data".to_string(), "transform".to_string());
        let cap3 = Cap::new(urn2, "Convert JSON Format".to_string(), "transform".to_string());

        assert_eq!(cap1, cap2);
        assert_ne!(cap1, cap3);
        assert_ne!(cap2, cap3);
    }

    #[test]
    fn test_cap_stdin() {
        let urn = CapUrn::from_string(&test_urn("op=generate;target=embeddings")).unwrap();
        let mut cap = Cap::new(urn, "Generate Embeddings".to_string(), "generate".to_string());

        // By default, caps should not accept stdin
        assert!(!cap.accepts_stdin());
        assert!(cap.get_stdin_media_urn().is_none());

        // Enable stdin support by adding an arg with a stdin source
        let stdin_arg = CapArg {
            media_urn: "media:text;textable".to_string(),
            required: true,
            sources: vec![ArgSource::Stdin { stdin: "media:text;textable".to_string() }],
            arg_description: Some("Input text".to_string()),
            validation: ArgumentValidation::default(),
            default_value: None,
            metadata: None,
        };
        cap.add_arg(stdin_arg);

        assert!(cap.accepts_stdin());
        assert_eq!(cap.get_stdin_media_urn(), Some("media:text;textable"));

        // Test serialization/deserialization preserves the args
        let serialized = serde_json::to_string(&cap).unwrap();
        assert!(serialized.contains("\"args\""));
        assert!(serialized.contains("\"stdin\""));
        let deserialized: Cap = serde_json::from_str(&serialized).unwrap();
        assert!(deserialized.accepts_stdin());
        assert_eq!(deserialized.get_stdin_media_urn(), Some("media:text;textable"));
    }

    #[test]
    fn test_arg_source_types() {
        // Test stdin source
        let stdin_source = ArgSource::Stdin { stdin: "media:text".to_string() };
        assert_eq!(stdin_source.get_type(), "stdin");
        assert_eq!(stdin_source.stdin_media_urn(), Some("media:text"));
        assert_eq!(stdin_source.position(), None);
        assert_eq!(stdin_source.cli_flag(), None);

        // Test position source
        let position_source = ArgSource::Position { position: 0 };
        assert_eq!(position_source.get_type(), "position");
        assert_eq!(position_source.stdin_media_urn(), None);
        assert_eq!(position_source.position(), Some(0));
        assert_eq!(position_source.cli_flag(), None);

        // Test cli_flag source
        let cli_flag_source = ArgSource::CliFlag { cli_flag: "--input".to_string() };
        assert_eq!(cli_flag_source.get_type(), "cli_flag");
        assert_eq!(cli_flag_source.stdin_media_urn(), None);
        assert_eq!(cli_flag_source.position(), None);
        assert_eq!(cli_flag_source.cli_flag(), Some("--input"));
    }

    #[test]
    fn test_cap_arg_serialization() {
        let arg = CapArg {
            media_urn: "media:string".to_string(),
            required: true,
            sources: vec![
                ArgSource::CliFlag { cli_flag: "--name".to_string() },
                ArgSource::Position { position: 0 },
            ],
            arg_description: Some("The name argument".to_string()),
            validation: ArgumentValidation::default(),
            default_value: None,
            metadata: None,
        };

        let serialized = serde_json::to_string(&arg).unwrap();
        assert!(serialized.contains("\"media_urn\":\"media:string\""));
        assert!(serialized.contains("\"required\":true"));
        assert!(serialized.contains("\"cli_flag\":\"--name\""));
        assert!(serialized.contains("\"position\":0"));

        let deserialized: CapArg = serde_json::from_str(&serialized).unwrap();
        assert_eq!(arg, deserialized);
    }

    #[test]
    fn test_cap_arg_constructors() {
        // Test basic constructor
        let arg = CapArg::new(
            "media:string",
            true,
            vec![ArgSource::CliFlag { cli_flag: "--name".to_string() }],
        );
        assert_eq!(arg.media_urn, "media:string");
        assert!(arg.required);
        assert_eq!(arg.sources.len(), 1);
        assert!(arg.arg_description.is_none());

        // Test with description
        let arg = CapArg::with_description(
            "media:integer",
            false,
            vec![ArgSource::Position { position: 0 }],
            "The count argument",
        );
        assert_eq!(arg.media_urn, "media:integer");
        assert!(!arg.required);
        assert_eq!(arg.arg_description, Some("The count argument".to_string()));
    }
}