greentic-types 0.5.2

Shared primitives for Greentic: TenantCtx, InvocationEnvelope, NodeError, ids.
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
//! Component manifest structures with generic capability declarations.

use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;

use semver::Version;

use crate::flow::FlowKind;
use crate::{ComponentId, FlowId, SecretRequirement};

#[cfg(feature = "schemars")]
use schemars::JsonSchema;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Development-time flow embedded directly in a component manifest.
///
/// These flows are consumed by tooling such as `greentic-dev` during authoring. They are not
/// required for deployment or runtime execution and may be safely ignored by hosts and runners.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct ComponentDevFlow {
    /// Flow representation format. Currently only `flow-ir-json` is supported.
    #[cfg_attr(feature = "serde", serde(default = "dev_flow_default_format"))]
    pub format: String,
    /// FlowIR JSON graph for this flow.
    pub graph: serde_json::Value,
}

fn dev_flow_default_format() -> String {
    "flow-ir-json".to_owned()
}

/// Component metadata describing capabilities and supported flows.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct ComponentManifest {
    /// Logical component identifier (opaque string).
    pub id: ComponentId,
    /// Semantic component version.
    #[cfg_attr(
        feature = "schemars",
        schemars(with = "String", description = "SemVer version")
    )]
    pub version: Version,
    /// Flow kinds this component can participate in.
    #[cfg_attr(feature = "serde", serde(default))]
    pub supports: Vec<FlowKind>,
    /// Referenced WIT world binding.
    pub world: String,
    /// Profile metadata for the component.
    pub profiles: ComponentProfiles,
    /// Capability contract required by the component.
    pub capabilities: ComponentCapabilities,
    /// Optional configurator flows.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub configurators: Option<ComponentConfigurators>,
    /// Operation-level descriptions.
    #[cfg_attr(feature = "serde", serde(default))]
    pub operations: Vec<ComponentOperation>,
    /// Optional configuration schema.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub config_schema: Option<serde_json::Value>,
    /// Resource usage hints for deployers/schedulers.
    #[cfg_attr(feature = "serde", serde(default))]
    pub resources: ResourceHints,
    /// Development-time flows used for authoring only. This field is optional and ignored by
    /// runtime systems. Tools may store FlowIR-as-JSON values here to allow editing flows without
    /// sidecar files.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "BTreeMap::is_empty")
    )]
    pub dev_flows: BTreeMap<FlowId, ComponentDevFlow>,
}

impl ComponentManifest {
    /// Returns `true` when the component supports the specified flow kind.
    pub fn supports_kind(&self, kind: FlowKind) -> bool {
        self.supports.iter().copied().any(|entry| entry == kind)
    }

    /// Resolves the effective profile name, returning the requested profile when supported or
    /// falling back to the manifest default.
    pub fn select_profile<'a>(
        &'a self,
        requested: Option<&str>,
    ) -> Result<Option<&'a str>, ComponentProfileError> {
        if let Some(name) = requested {
            let matched = self
                .profiles
                .supported
                .iter()
                .find(|candidate| candidate.as_str() == name)
                .ok_or_else(|| ComponentProfileError::UnsupportedProfile {
                    requested: name.to_owned(),
                    supported: self.profiles.supported.clone(),
                })?;
            Ok(Some(matched.as_str()))
        } else {
            Ok(self.profiles.default.as_deref())
        }
    }

    /// Returns the optional basic configurator flow identifier.
    pub fn basic_configurator(&self) -> Option<&FlowId> {
        self.configurators
            .as_ref()
            .and_then(|cfg| cfg.basic.as_ref())
    }

    /// Returns the optional full configurator flow identifier.
    pub fn full_configurator(&self) -> Option<&FlowId> {
        self.configurators
            .as_ref()
            .and_then(|cfg| cfg.full.as_ref())
    }
}

/// Component profile declaration.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct ComponentProfiles {
    /// Default profile applied when a node does not specify one.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub default: Option<String>,
    /// Supported profile identifiers.
    #[cfg_attr(feature = "serde", serde(default))]
    pub supported: Vec<String>,
}

/// Flow configurators linked from a component manifest.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct ComponentConfigurators {
    /// Basic configurator flow identifier.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub basic: Option<FlowId>,
    /// Full configurator flow identifier.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub full: Option<FlowId>,
}

/// Operation descriptor for a component.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct ComponentOperation {
    /// Operation name (for example `handle_message`).
    pub name: String,
    /// Input schema for the operation.
    pub input_schema: serde_json::Value,
    /// Output schema for the operation.
    pub output_schema: serde_json::Value,
}

/// Resource usage hints for a component.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct ResourceHints {
    /// Suggested CPU in millis.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub cpu_millis: Option<u32>,
    /// Suggested memory in MiB.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub memory_mb: Option<u32>,
    /// Expected average latency in milliseconds.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub average_latency_ms: Option<u32>,
}

/// Host + WASI capabilities required by a component.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct ComponentCapabilities {
    /// WASI Preview 2 surfaces.
    pub wasi: WasiCapabilities,
    /// Host capability surfaces.
    pub host: HostCapabilities,
}

/// WASI capability declarations.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct WasiCapabilities {
    /// Filesystem configuration.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub filesystem: Option<FilesystemCapabilities>,
    /// Environment variable allow list.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub env: Option<EnvCapabilities>,
    /// Whether random number generation is required.
    #[cfg_attr(feature = "serde", serde(default))]
    pub random: bool,
    /// Whether clock access is required.
    #[cfg_attr(feature = "serde", serde(default))]
    pub clocks: bool,
}

/// Filesystem sandbox configuration.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct FilesystemCapabilities {
    /// Filesystem exposure mode.
    pub mode: FilesystemMode,
    /// Declared mounts.
    #[cfg_attr(feature = "serde", serde(default))]
    pub mounts: Vec<FilesystemMount>,
}

impl Default for FilesystemCapabilities {
    fn default() -> Self {
        Self {
            mode: FilesystemMode::None,
            mounts: Vec::new(),
        }
    }
}

/// Filesystem exposure mode.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum FilesystemMode {
    /// No filesystem access.
    #[default]
    None,
    /// Read-only view with predefined mounts.
    ReadOnly,
    /// Isolated sandbox with write access.
    Sandbox,
}

/// Single mount definition.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct FilesystemMount {
    /// Logical mount identifier.
    pub name: String,
    /// Host-provided storage class (scratch/cache/config/etc.).
    pub host_class: String,
    /// Guest-visible mount path.
    pub guest_path: String,
}

/// Environment variable allow list.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct EnvCapabilities {
    /// Environment variable names components may read.
    #[cfg_attr(feature = "serde", serde(default))]
    pub allow: Vec<String>,
}

/// Host capability declaration.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct HostCapabilities {
    /// Secret resolution requirements.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub secrets: Option<SecretsCapabilities>,
    /// Durable state access requirements.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub state: Option<StateCapabilities>,
    /// Messaging ingress/egress needs.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub messaging: Option<MessagingCapabilities>,
    /// Event ingress/egress needs.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub events: Option<EventsCapabilities>,
    /// HTTP client/server needs.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub http: Option<HttpCapabilities>,
    /// Telemetry emission settings.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub telemetry: Option<TelemetryCapabilities>,
    /// Infrastructure-as-code artifact permissions.
    #[cfg_attr(
        feature = "serde",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub iac: Option<IaCCapabilities>,
}

/// Secret requirements.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct SecretsCapabilities {
    /// Secret identifiers required at runtime.
    #[cfg_attr(feature = "serde", serde(default))]
    pub required: Vec<SecretRequirement>,
}

/// State surface declaration.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct StateCapabilities {
    /// Whether read access is required.
    #[cfg_attr(feature = "serde", serde(default))]
    pub read: bool,
    /// Whether write access is required.
    #[cfg_attr(feature = "serde", serde(default))]
    pub write: bool,
}

/// Messaging capability declaration.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct MessagingCapabilities {
    /// Whether the component receives inbound messages.
    #[cfg_attr(feature = "serde", serde(default))]
    pub inbound: bool,
    /// Whether the component emits outbound messages.
    #[cfg_attr(feature = "serde", serde(default))]
    pub outbound: bool,
}

/// Events capability declaration.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct EventsCapabilities {
    /// Whether inbound events are handled.
    #[cfg_attr(feature = "serde", serde(default))]
    pub inbound: bool,
    /// Whether outbound events are emitted.
    #[cfg_attr(feature = "serde", serde(default))]
    pub outbound: bool,
}

/// HTTP capability declaration.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct HttpCapabilities {
    /// Outbound HTTP client usage.
    #[cfg_attr(feature = "serde", serde(default))]
    pub client: bool,
    /// Inbound HTTP server usage.
    #[cfg_attr(feature = "serde", serde(default))]
    pub server: bool,
}

/// Telemetry scoping modes.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub enum TelemetryScope {
    /// Emitted telemetry is scoped to the tenant.
    Tenant,
    /// Scoped to the pack.
    Pack,
    /// Scoped per-node invocation.
    Node,
}

/// Telemetry capability declaration.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct TelemetryCapabilities {
    /// Maximum telemetry scope granted to the component.
    pub scope: TelemetryScope,
}

/// Infrastructure-as-code host permissions.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
pub struct IaCCapabilities {
    /// Whether templates/manifests may be written to a preopened path.
    pub write_templates: bool,
    /// Whether the component may trigger IaC plan execution via the host.
    #[cfg_attr(feature = "serde", serde(default))]
    pub execute_plans: bool,
}

/// Profile resolution errors.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ComponentProfileError {
    /// Requested profile is not advertised by the component.
    UnsupportedProfile {
        /// Profile requested by the flow.
        requested: String,
        /// Known supported profiles for troubleshooting.
        supported: Vec<String>,
    },
}

impl core::fmt::Display for ComponentProfileError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            ComponentProfileError::UnsupportedProfile {
                requested,
                supported,
            } => {
                write!(
                    f,
                    "profile `{requested}` is not supported; known profiles: {supported:?}"
                )
            }
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for ComponentProfileError {}