greentic-flow 0.4.65

Generic YGTC flow schema/loader/IR for self-describing component nodes.
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
use crate::{
    component_schema::jsonschema_options_with_base,
    error::{FlowError, FlowErrorLocation, Result, SchemaErrorDetail},
    model::FlowDoc,
    path_safety::normalize_under_root,
};
use serde::Deserialize;
use serde_json::Value as JsonValue;
use serde_json::Value;
use serde_yaml_bw::Location as YamlLocation;
use std::{
    fs, io,
    path::{Path, PathBuf},
    sync::OnceLock,
};

const INLINE_SOURCE: &str = "<inline>";
const DEFAULT_SCHEMA_LABEL: &str = "https://raw.githubusercontent.com/greenticai/greentic-flow/refs/heads/master/schemas/ygtc.flow.schema.json";
const EMBEDDED_SCHEMA: &str = include_str!("../schemas/ygtc.flow.schema.json");

fn schema_file_valid(path: &Path) -> bool {
    let Ok(text) = fs::read_to_string(path) else {
        return false;
    };
    if text.trim().is_empty() {
        return false;
    }
    serde_json::from_str::<JsonValue>(&text).is_ok()
}

fn write_schema_atomically(path: &Path) -> io::Result<()> {
    let parent = path
        .parent()
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "schema path has no parent"))?;
    let unique = format!(
        "greentic-flow-config-schema-{}.tmp-{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_nanos())
            .unwrap_or(0)
    );
    let tmp = parent.join(unique);
    fs::write(&tmp, EMBEDDED_SCHEMA)?;
    match fs::rename(&tmp, path) {
        Ok(()) => Ok(()),
        Err(err) => {
            let _ = fs::remove_file(&tmp);
            if schema_file_valid(path) {
                Ok(())
            } else {
                Err(err)
            }
        }
    }
}

/// Ensure a temporary copy of the embedded flow schema exists and return its path.
pub fn ensure_config_schema_path() -> io::Result<PathBuf> {
    static CONFIG_SCHEMA_PATH: OnceLock<PathBuf> = OnceLock::new();
    if let Some(path) = CONFIG_SCHEMA_PATH.get() {
        if !schema_file_valid(path) {
            write_schema_atomically(path)?;
        }
        return Ok(path.clone());
    }
    let mut path = std::env::temp_dir();
    path.push(format!(
        "greentic-flow-config-schema-{}.json",
        env!("CARGO_PKG_VERSION")
    ));
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)?;
    }
    if !schema_file_valid(&path) {
        write_schema_atomically(&path)?;
    }
    if !schema_file_valid(&path) {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("schema file is invalid after write: {}", path.display()),
        ));
    }
    match CONFIG_SCHEMA_PATH.set(path.clone()) {
        Ok(()) => Ok(path),
        Err(_) => Ok(CONFIG_SCHEMA_PATH
            .get()
            .expect("config schema path set")
            .clone()),
    }
}

/// Load YGTC YAML from a string using the embedded schema.
pub fn load_ygtc_from_str(yaml: &str) -> Result<FlowDoc> {
    load_with_schema_text(
        yaml,
        EMBEDDED_SCHEMA,
        DEFAULT_SCHEMA_LABEL.to_string(),
        None,
        INLINE_SOURCE,
        None,
    )
}

/// Load YGTC YAML from a file path using the embedded schema.
pub fn load_ygtc_from_path(path: &Path) -> Result<FlowDoc> {
    let content = fs::read_to_string(path).map_err(|e| FlowError::Internal {
        message: format!("failed to read {}: {e}", path.display()),
        location: FlowErrorLocation::at_path(path.display().to_string())
            .with_source_path(Some(path)),
    })?;
    load_with_schema_text(
        &content,
        EMBEDDED_SCHEMA,
        DEFAULT_SCHEMA_LABEL.to_string(),
        None,
        path.display().to_string(),
        Some(path),
    )
}

/// Load YGTC YAML from a string using a schema file on disk.
pub fn load_ygtc_from_str_with_schema(yaml: &str, schema_path: &Path) -> Result<FlowDoc> {
    load_ygtc_from_str_with_source(yaml, schema_path, INLINE_SOURCE)
}

pub fn load_ygtc_from_str_with_source(
    yaml: &str,
    schema_path: &Path,
    source_label: impl Into<String>,
) -> Result<FlowDoc> {
    let schema_root = std::env::current_dir().map_err(|e| FlowError::Internal {
        message: format!("resolve schema root: {e}"),
        location: FlowErrorLocation::at_path(schema_path.display().to_string())
            .with_source_path(Some(schema_path)),
    })?;
    let safe_schema_path = if schema_path.is_absolute() {
        schema_path.to_path_buf()
    } else {
        normalize_under_root(&schema_root, schema_path).map_err(|e| FlowError::Internal {
            message: format!("schema path validation for {}: {e}", schema_path.display()),
            location: FlowErrorLocation::at_path(schema_path.display().to_string())
                .with_source_path(Some(schema_path)),
        })?
    };
    let schema_label = safe_schema_path.display().to_string();
    let schema_text = fs::read_to_string(&safe_schema_path).map_err(|e| FlowError::Internal {
        message: format!("schema read from {schema_label}: {e}"),
        location: FlowErrorLocation::at_path(schema_label.clone())
            .with_source_path(Some(&safe_schema_path)),
    })?;
    load_with_schema_text(
        yaml,
        &schema_text,
        schema_label,
        Some(&safe_schema_path),
        source_label,
        None,
    )
}

pub(crate) fn load_with_schema_text(
    yaml: &str,
    schema_text: &str,
    schema_label: impl Into<String>,
    schema_path: Option<&Path>,
    source_label: impl Into<String>,
    source_path: Option<&Path>,
) -> Result<FlowDoc> {
    let schema_label = schema_label.into();
    let source_label = source_label.into();
    let mut v_yaml: serde_yaml_bw::Value =
        serde_yaml_bw::from_str(yaml).map_err(|e| FlowError::Yaml {
            message: e.to_string(),
            location: yaml_error_location(&source_label, source_path, e.location()),
        })?;
    ensure_nodes_mapping(&mut v_yaml);
    let v_json: Value = serde_json::to_value(&v_yaml).map_err(|e| FlowError::Internal {
        message: format!("yaml->json: {e}"),
        location: FlowErrorLocation::at_path(source_label.clone()).with_source_path(source_path),
    })?;
    let schema_version = v_json
        .get("schema_version")
        .and_then(Value::as_u64)
        .unwrap_or(2);
    let nodes_empty = v_json
        .get("nodes")
        .and_then(Value::as_object)
        .map(|m| m.is_empty())
        .unwrap_or(false);
    let reserved_for_count = [
        "routing",
        "telemetry",
        "output",
        "retry",
        "timeout",
        "when",
        "annotations",
        "meta",
        "operation",
    ];
    let looks_legacy = v_json.get("nodes").and_then(Value::as_object).map(|nodes| {
        nodes.values().any(|n| {
            let (op_count, has_dot_key) = n
                .as_object()
                .map(|obj| {
                    let op_count = obj
                        .keys()
                        .filter(|k| !reserved_for_count.contains(&k.as_str()))
                        .count();
                    let has_dot_key = obj.keys().any(|k| k.contains('.'));
                    (op_count, has_dot_key)
                })
                .unwrap_or((0, false));
            op_count != 1
                || has_dot_key
                || n.get("component.exec").is_some()
                || n.get("operation").is_some()
                || n.get("pack_alias").is_some()
        })
    });
    if v_json.get("type").is_none() {
        return Err(FlowError::Schema {
            message: format!("{source_label}/type: missing required property 'type'"),
            details: vec![SchemaErrorDetail {
                message: "Missing required property 'type'".to_string(),
                location: FlowErrorLocation::at_path(format!("{source_label}/type"))
                    .with_source_path(source_path)
                    .with_json_pointer(Some("/type".to_string())),
            }],
            location: FlowErrorLocation::at_path(source_label.clone())
                .with_source_path(source_path),
        });
    }

    if let Some(nodes) = v_json.get("nodes").and_then(Value::as_object) {
        for id in nodes.keys() {
            let Some(node_val) = nodes.get(id) else {
                continue;
            };
            let Some(obj) = node_val.as_object() else {
                continue;
            };
            let op_count = obj
                .keys()
                .filter(|k| !reserved_for_count.contains(&k.as_str()))
                .count();
            let is_component_exec = obj.contains_key("component.exec");
            let component_combo = is_component_exec && op_count == 2;
            if op_count != 1 && !(component_combo || schema_version < 2) {
                return Err(FlowError::NodeComponentShape {
                    node_id: id.clone(),
                    location: node_location(&source_label, source_path, id),
                });
            }
        }
    }

    if !nodes_empty && schema_version >= 2 && !looks_legacy.unwrap_or(false) {
        validate_json(
            &v_json,
            schema_text,
            &schema_label,
            schema_path,
            &source_label,
            source_path,
        )?;
    }

    let mut flow: FlowDoc = match serde_yaml_bw::from_value(v_yaml.clone()) {
        Ok(doc) => doc,
        Err(e) => {
            validate_json(
                &v_json,
                schema_text,
                &schema_label,
                schema_path,
                &source_label,
                source_path,
            )?;
            return Err(FlowError::Yaml {
                message: e.to_string(),
                location: yaml_error_location(&source_label, source_path, None),
            });
        }
    };
    if flow.schema_version.is_none() {
        flow.schema_version = Some(2);
    }

    let node_ids: Vec<String> = flow.nodes.keys().cloned().collect();
    for id in &node_ids {
        let node = flow.nodes.get_mut(id).ok_or_else(|| FlowError::Internal {
            message: format!("node '{id}' missing after load"),
            location: node_location(&source_label, source_path, id),
        })?;
        let reserved = [
            "routing",
            "telemetry",
            "output",
            "retry",
            "timeout",
            "when",
            "annotations",
            "meta",
            "operation",
        ];
        let op_count = node
            .raw
            .keys()
            .filter(|k| !reserved.contains(&k.as_str()))
            .count();
        let is_component_exec = node.raw.contains_key("component.exec");
        let component_combo = is_component_exec && op_count == 2;
        if op_count != 1 && !(component_combo || flow.schema_version.unwrap_or(1) < 2) {
            return Err(FlowError::NodeComponentShape {
                node_id: id.clone(),
                location: node_location(&source_label, source_path, id),
            });
        }
    }

    for (from_id, node) in &flow.nodes {
        for route in parse_routes(&node.routing, from_id, &source_label, source_path)? {
            if let Some(to) = &route.to
                && to != "out"
                && !flow.nodes.contains_key(to)
            {
                return Err(FlowError::MissingNode {
                    target: to.clone(),
                    node_id: from_id.clone(),
                    location: routing_location(&source_label, source_path, from_id),
                });
            }
        }
    }

    if flow.start.is_none() && flow.nodes.contains_key("in") {
        flow.start = Some("in".to_string());
    }

    Ok(flow)
}

fn parse_routes(
    raw: &Value,
    node_id: &str,
    source_label: &str,
    source_path: Option<&Path>,
) -> Result<Vec<RouteDoc>> {
    if raw.is_null() {
        return Ok(Vec::new());
    }
    if let Some(shorthand) = raw.as_str() {
        return match shorthand {
            "out" => Ok(vec![RouteDoc {
                to: Some("out".to_string()),
                out: Some(true),
                status: None,
                reply: None,
            }]),
            "reply" => Ok(vec![RouteDoc {
                to: None,
                out: None,
                status: None,
                reply: Some(true),
            }]),
            other => Err(FlowError::Routing {
                node_id: node_id.to_string(),
                message: format!("invalid routing shorthand '{other}'"),
                location: routing_location(source_label, source_path, node_id),
            }),
        };
    }
    serde_json::from_value::<Vec<RouteDoc>>(raw.clone()).map_err(|e| FlowError::Routing {
        node_id: node_id.to_string(),
        message: e.to_string(),
        location: routing_location(source_label, source_path, node_id),
    })
}

#[derive(Debug, Clone, Deserialize)]
struct RouteDoc {
    #[serde(default)]
    pub to: Option<String>,
    #[allow(dead_code)]
    #[serde(default)]
    pub out: Option<bool>,
    #[allow(dead_code)]
    #[serde(default)]
    pub status: Option<String>,
    #[allow(dead_code)]
    #[serde(default)]
    pub reply: Option<bool>,
}

fn validate_json(
    doc: &Value,
    schema_text: &str,
    schema_label: &str,
    schema_path: Option<&Path>,
    source_label: &str,
    source_path: Option<&Path>,
) -> Result<()> {
    let schema: Value = serde_json::from_str(schema_text).map_err(|e| FlowError::Internal {
        message: format!("schema parse for {schema_label}: {e}"),
        location: FlowErrorLocation::at_path(schema_label.to_string())
            .with_source_path(schema_path),
    })?;
    let validator = jsonschema_options_with_base(schema_path)
        .build(&schema)
        .map_err(|e| FlowError::Internal {
            message: format!("schema compile for {schema_label}: {e}"),
            location: FlowErrorLocation::at_path(schema_label.to_string())
                .with_source_path(schema_path),
        })?;
    let details: Vec<SchemaErrorDetail> = validator
        .iter_errors(doc)
        .map(|e| {
            let pointer = e.instance_path().to_string();
            let pointer = if pointer.is_empty() {
                "/".to_string()
            } else {
                pointer
            };
            SchemaErrorDetail {
                message: e.to_string(),
                location: FlowErrorLocation::at_path(format!("{source_label}{pointer}"))
                    .with_source_path(source_path)
                    .with_json_pointer(Some(pointer.clone())),
            }
        })
        .collect();
    if !details.is_empty() {
        let message = details
            .iter()
            .map(|detail| {
                let where_str = detail
                    .location
                    .describe()
                    .unwrap_or_else(|| source_label.to_string());
                format!("{where_str}: {}", detail.message)
            })
            .collect::<Vec<_>>()
            .join("\n");
        return Err(FlowError::Schema {
            message,
            details,
            location: FlowErrorLocation::at_path(source_label.to_string())
                .with_source_path(source_path),
        });
    }
    Ok(())
}

fn ensure_nodes_mapping(doc: &mut serde_yaml_bw::Value) {
    let Some(mapping) = doc.as_mapping_mut() else {
        return;
    };
    let nodes_key = serde_yaml_bw::Value::String("nodes".to_string(), None);
    match mapping.get_mut(&nodes_key) {
        Some(existing) => {
            if existing.is_null() {
                *existing = serde_yaml_bw::Value::Mapping(serde_yaml_bw::Mapping::new());
            }
        }
        None => {
            mapping.insert(
                nodes_key,
                serde_yaml_bw::Value::Mapping(serde_yaml_bw::Mapping::new()),
            );
        }
    }
}

fn node_location(
    source_label: &str,
    source_path: Option<&Path>,
    node_id: &str,
) -> FlowErrorLocation {
    FlowErrorLocation::at_path(format!("{source_label}::nodes.{node_id}"))
        .with_source_path(source_path)
}

fn routing_location(
    source_label: &str,
    source_path: Option<&Path>,
    node_id: &str,
) -> FlowErrorLocation {
    FlowErrorLocation::at_path(format!("{source_label}::nodes.{node_id}.routing"))
        .with_source_path(source_path)
}

pub(crate) fn yaml_error_location(
    source_label: &str,
    source_path: Option<&Path>,
    loc: Option<YamlLocation>,
) -> FlowErrorLocation {
    if let Some(loc) = loc {
        FlowErrorLocation::at_path_with_position(
            source_label.to_string(),
            Some(loc.line()),
            Some(loc.column()),
        )
        .with_source_path(source_path)
    } else {
        FlowErrorLocation::at_path(source_label.to_string()).with_source_path(source_path)
    }
}