greentic-bundle 0.5.9

Greentic bundle authoring CLI scaffold with embedded i18n and answer-document contracts.
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
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

use crate::setup::SetupSpecInput;

pub const BUNDLED_PROVIDER_REGISTRY_SOURCE: &str = "registries/providers.json";

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CatalogEntry {
    pub id: String,
    #[serde(default)]
    pub category: Option<String>,
    #[serde(default)]
    pub category_label: Option<String>,
    #[serde(default)]
    pub category_description: Option<String>,
    #[serde(default)]
    pub label: Option<String>,
    #[serde(alias = "ref")]
    pub reference: String,
    #[serde(default)]
    pub setup: Option<SetupSpecInput>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CatalogSummary {
    pub item_count: usize,
    pub item_ids: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedCatalog {
    pub entries: Vec<CatalogEntry>,
    pub summary: CatalogSummary,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
enum ArrayCatalogFormat {
    Categorized(Vec<ProviderRegistryCategory>),
    Flat(Vec<CatalogEntry>),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
enum ObjectCatalogFormat {
    Oci(OciProviderRegistryFile),
    Categorized(CategorizedProviderRegistryFile),
    Flat(ProviderRegistryFile),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct ProviderRegistryFile {
    #[serde(default)]
    items: Vec<ProviderRegistryItem>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct CategorizedProviderRegistryFile {
    #[serde(default)]
    categories: Vec<ProviderRegistryCategory>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct ProviderRegistryCategory {
    category: String,
    #[serde(default)]
    description: Option<String>,
    #[serde(default)]
    items: Vec<ProviderRegistryItem>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct ProviderRegistryItem {
    id: String,
    label: ProviderRegistryLabel,
    #[serde(alias = "ref")]
    reference: String,
    #[serde(default)]
    setup: Option<SetupSpecInput>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct ProviderRegistryLabel {
    #[serde(default)]
    fallback: String,
}

/// OCI registry format with separate categories and items arrays at root level.
/// Format: `{ "categories": [...], "items": [...] }`
#[derive(Debug, Clone, Serialize, Deserialize)]
struct OciProviderRegistryFile {
    #[serde(default)]
    registry_version: Option<String>,
    #[serde(default)]
    categories: Vec<OciProviderRegistryCategory>,
    #[serde(default)]
    items: Vec<OciProviderRegistryItem>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct OciProviderRegistryCategory {
    id: String,
    #[serde(default)]
    label: ProviderRegistryLabel,
    #[serde(default)]
    description: Option<ProviderRegistryLabel>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct OciProviderRegistryItem {
    id: String,
    #[serde(default)]
    category: Option<String>,
    label: ProviderRegistryLabel,
    #[serde(alias = "ref")]
    reference: String,
    #[serde(default)]
    setup: Option<SetupSpecInput>,
}

pub fn parse_catalog_bytes(bytes: &[u8], source: &str) -> Result<CatalogSummary> {
    let entries = parse_catalog_entries(bytes, source)?;
    Ok(summary_from_entries(&entries))
}

pub fn bundled_provider_registry_entries() -> Result<Vec<CatalogEntry>> {
    load_catalog_entries(
        include_bytes!("../../registries/providers.json"),
        BUNDLED_PROVIDER_REGISTRY_SOURCE,
    )
}

pub fn load_catalog_entries(bytes: &[u8], source: &str) -> Result<Vec<CatalogEntry>> {
    parse_catalog_entries(bytes, source)
}

pub fn parse_catalog(bytes: &[u8], source: &str) -> Result<ParsedCatalog> {
    let entries = parse_catalog_entries(bytes, source)?;
    Ok(ParsedCatalog {
        summary: summary_from_entries(&entries),
        entries,
    })
}

fn parse_catalog_entries(bytes: &[u8], source: &str) -> Result<Vec<CatalogEntry>> {
    let raw = std::str::from_utf8(bytes)
        .with_context(|| format!("catalog {source} must be valid UTF-8 JSON"))?;
    let trimmed = raw.trim_start();

    if trimmed.starts_with('[') {
        return match serde_json::from_str::<ArrayCatalogFormat>(trimmed)
            .with_context(|| format!("parse catalog/provider registry file {source}"))?
        {
            ArrayCatalogFormat::Categorized(categories) => Ok(categories
                .into_iter()
                .flat_map(|category| {
                    let category_name = category.category;
                    let category_description = category.description;
                    category.items.into_iter().map(move |item| {
                        CatalogEntry::from_categorized_item(
                            item,
                            &category_name,
                            None,
                            category_description.as_deref(),
                        )
                    })
                })
                .collect()),
            ArrayCatalogFormat::Flat(entries) => Ok(entries),
        };
    }

    if trimmed.starts_with('{') {
        return match serde_json::from_str::<ObjectCatalogFormat>(trimmed)
            .with_context(|| format!("parse catalog/provider registry file {source}"))?
        {
            ObjectCatalogFormat::Oci(registry) => {
                // Build category lookup for labels and descriptions once per catalog parse.
                let category_map: std::collections::HashMap<String, &OciProviderRegistryCategory> =
                    registry
                        .categories
                        .iter()
                        .map(|c| (c.id.clone(), c))
                        .collect();
                Ok(registry
                    .items
                    .into_iter()
                    .map(|item| {
                        let category_meta = item
                            .category
                            .as_ref()
                            .and_then(|cat_id| category_map.get(cat_id));
                        let category_label = category_meta
                            .filter(|c| !c.label.fallback.is_empty())
                            .map(|c| c.label.fallback.clone());
                        let category_description = category_meta
                            .and_then(|c| c.description.as_ref())
                            .map(|d| d.fallback.clone());
                        CatalogEntry {
                            id: item.id,
                            category: item.category,
                            category_label,
                            category_description,
                            label: (!item.label.fallback.is_empty()).then_some(item.label.fallback),
                            reference: item.reference,
                            setup: item.setup,
                        }
                    })
                    .collect())
            }
            ObjectCatalogFormat::Categorized(registry) => Ok(registry
                .categories
                .into_iter()
                .flat_map(|category| {
                    let category_name = category.category;
                    let category_description = category.description;
                    category.items.into_iter().map(move |item| {
                        CatalogEntry::from_categorized_item(
                            item,
                            &category_name,
                            None,
                            category_description.as_deref(),
                        )
                    })
                })
                .collect()),
            ObjectCatalogFormat::Flat(registry) => {
                Ok(registry.items.into_iter().map(CatalogEntry::from).collect())
            }
        };
    }

    Err(anyhow::anyhow!(
        "parse catalog/provider registry file {source}: expected JSON object or array"
    ))
}

fn summary_from_entries(entries: &[CatalogEntry]) -> CatalogSummary {
    let mut item_ids = entries
        .iter()
        .map(|entry| entry.id.clone())
        .collect::<Vec<_>>();
    item_ids.sort();
    item_ids.dedup();
    let item_count = item_ids.len();
    CatalogSummary {
        item_count,
        item_ids,
    }
}

impl From<ProviderRegistryItem> for CatalogEntry {
    fn from(item: ProviderRegistryItem) -> Self {
        Self {
            id: item.id,
            category: None,
            category_label: None,
            category_description: None,
            label: (!item.label.fallback.is_empty()).then_some(item.label.fallback),
            reference: item.reference,
            setup: item.setup,
        }
    }
}

impl CatalogEntry {
    fn from_categorized_item(
        item: ProviderRegistryItem,
        category: &str,
        category_label: Option<&str>,
        category_description: Option<&str>,
    ) -> Self {
        Self {
            category: Some(category.to_string()),
            // For legacy format, use category name as label if no explicit label provided
            category_label: Some(category_label.unwrap_or(category).to_string()),
            category_description: category_description.map(ToString::to_string),
            ..Self::from(item)
        }
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::{bundled_provider_registry_entries, load_catalog_entries, parse_catalog_bytes};

    #[test]
    fn parses_inline_setup_metadata_from_array_catalog() {
        let entries = load_catalog_entries(
            br#"[
  {
    "id":"provider-a",
    "reference":"repo://providers/provider-a@1",
    "setup":{
      "type":"legacy",
      "spec":{
        "title":"Provider A Setup",
        "questions":[{"name":"enabled","kind":"boolean","required":true}]
      }
    }
  }
]"#,
            "inline",
        )
        .expect("entries");

        assert_eq!(entries.len(), 1);
        let setup = entries[0].setup.as_ref().expect("setup metadata");
        assert_eq!(
            serde_json::to_value(setup).expect("setup json"),
            json!({
              "type":"legacy",
              "spec":{
                "title":"Provider A Setup",
                "questions":[{"name":"enabled","kind":"boolean","required":true}]
              }
            })
        );
    }

    #[test]
    fn parses_categorized_registry_file() {
        let entries = load_catalog_entries(
            br#"[
    {
      "category": "deployer",
      "description": "deployment helpers for rollout targets",
      "items": [
        {
          "id":"provider-a",
          "label":{"fallback":"Provider A"},
          "reference":"repo://providers/provider-a@1"
        }
      ]
    },
    {
      "category": "oauth",
      "description": "OAuth provider helpers and identity integrations",
      "items": [
        {
          "id":"provider-b",
          "label":{"fallback":"Provider B"},
          "reference":"repo://providers/provider-b@1"
        }
      ]
    }
]"#,
            "inline",
        )
        .expect("entries");

        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].id, "provider-a");
        assert_eq!(entries[1].id, "provider-b");
        assert_eq!(entries[0].category.as_deref(), Some("deployer"));
        assert_eq!(entries[1].category.as_deref(), Some("oauth"));
        assert_eq!(
            entries[0].category_description.as_deref(),
            Some("deployment helpers for rollout targets")
        );
        assert_eq!(
            entries[1].category_description.as_deref(),
            Some("OAuth provider helpers and identity integrations")
        );
    }

    #[test]
    fn parses_oci_registry_format_with_root_items() {
        let entries = load_catalog_entries(
            br#"{
  "registry_version": "providers@1",
  "categories": [
    {
      "id": "messaging",
      "label": { "fallback": "Messaging" },
      "description": { "fallback": "Bridges to external messaging services" }
    },
    {
      "id": "events",
      "label": { "fallback": "Events" },
      "description": { "fallback": "Event sources and delivery helpers" }
    }
  ],
  "items": [
    {
      "id": "messaging-teams",
      "category": "messaging",
      "label": { "fallback": "MS-Teams" },
      "ref": "oci://ghcr.io/greenticai/packs/messaging/messaging-teams:stable"
    },
    {
      "id": "messaging-telegram",
      "category": "messaging",
      "label": { "fallback": "Telegram" },
      "ref": "oci://ghcr.io/greenticai/packs/messaging/messaging-telegram:stable"
    },
    {
      "id": "events-webhook",
      "category": "events",
      "label": { "fallback": "Webhook" },
      "ref": "oci://ghcr.io/greenticai/packs/events/events-webhook:stable"
    }
  ]
}"#,
            "oci-registry",
        )
        .expect("entries");

        assert_eq!(entries.len(), 3);
        // Check first item
        assert_eq!(entries[0].id, "messaging-teams");
        assert_eq!(entries[0].category.as_deref(), Some("messaging"));
        assert_eq!(entries[0].category_label.as_deref(), Some("Messaging"));
        assert_eq!(entries[0].label.as_deref(), Some("MS-Teams"));
        assert_eq!(
            entries[0].reference,
            "oci://ghcr.io/greenticai/packs/messaging/messaging-teams:stable"
        );
        assert_eq!(
            entries[0].category_description.as_deref(),
            Some("Bridges to external messaging services")
        );
        // Check second item
        assert_eq!(entries[1].id, "messaging-telegram");
        assert_eq!(entries[1].category_label.as_deref(), Some("Messaging"));
        assert_eq!(entries[1].label.as_deref(), Some("Telegram"));
        // Check third item (different category)
        assert_eq!(entries[2].id, "events-webhook");
        assert_eq!(entries[2].category.as_deref(), Some("events"));
        assert_eq!(entries[2].category_label.as_deref(), Some("Events"));
        assert_eq!(entries[2].label.as_deref(), Some("Webhook"));
        assert_eq!(
            entries[2].category_description.as_deref(),
            Some("Event sources and delivery helpers")
        );
    }

    #[test]
    fn parses_checked_in_provider_registry_fixture() {
        let entries = bundled_provider_registry_entries().expect("catalog fixture");
        assert!(!entries.is_empty());
        assert_eq!(entries[0].id, "messaging-teams");
        assert_eq!(
            entries[0].reference,
            "oci://ghcr.io/greenticai/packs/messaging/messaging-teams:stable"
        );
    }

    #[test]
    fn parses_flat_registry_object_items() {
        let entries = load_catalog_entries(
            br#"{
  "items": [
    {
      "id":"provider-a",
      "label":{"fallback":"Provider A"},
      "ref":"repo://providers/provider-a@1"
    }
  ]
}"#,
            "flat-object",
        )
        .expect("entries");

        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].id, "provider-a");
        assert_eq!(entries[0].label.as_deref(), Some("Provider A"));
        assert_eq!(entries[0].reference, "repo://providers/provider-a@1");
    }

    #[test]
    fn catalog_summary_deduplicates_item_ids() {
        let summary = parse_catalog_bytes(
            br#"[
  {"id":"provider-b","reference":"repo://providers/provider-b@1"},
  {"id":"provider-a","reference":"repo://providers/provider-a@1"},
  {"id":"provider-a","reference":"repo://providers/provider-a@2"}
]"#,
            "summary",
        )
        .expect("summary");

        assert_eq!(summary.item_count, 2);
        assert_eq!(summary.item_ids, vec!["provider-a", "provider-b"]);
    }
}