use serde::{Deserialize, Serialize};
use crate::value::ValueType;
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub enum NodeCategory {
Io,
Logic,
Transform,
Model,
Knowledge,
Integration,
Human,
#[default]
Other,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(tag = "kind", rename_all = "snake_case")]
#[non_exhaustive]
pub enum Handles {
#[default]
Single,
Fixed { handles: Vec<String> },
Dynamic {
#[serde(default, skip_serializing_if = "Option::is_none")]
config_path: Option<String>,
},
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OutputDescriptor {
pub name: String,
#[serde(rename = "type")]
pub ty: ValueType,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub description: String,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub streaming: bool,
}
impl OutputDescriptor {
pub fn new(name: impl Into<String>, ty: ValueType) -> Self {
Self { name: name.into(), ty, description: String::new(), streaming: false }
}
pub fn describe(mut self, description: impl Into<String>) -> Self {
self.description = description.into();
self
}
pub fn streaming(mut self) -> Self {
self.streaming = true;
self
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct NodeDescriptor {
pub kind: String,
pub title: String,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub description: String,
#[serde(default)]
pub category: NodeCategory,
#[serde(default = "empty_schema")]
pub config_schema: serde_json::Value,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub outputs: Vec<OutputDescriptor>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub dynamic_outputs: bool,
#[serde(default)]
pub handles: Handles,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub container: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub icon: Option<String>,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub source: String,
}
fn empty_schema() -> serde_json::Value {
serde_json::json!({})
}
impl NodeDescriptor {
pub fn new(kind: impl Into<String>, title: impl Into<String>, category: NodeCategory) -> Self {
Self {
kind: kind.into(),
title: title.into(),
description: String::new(),
category,
config_schema: empty_schema(),
outputs: Vec::new(),
dynamic_outputs: false,
handles: Handles::Single,
container: false,
icon: None,
source: String::new(),
}
}
pub fn describe(mut self, description: impl Into<String>) -> Self {
self.description = description.into();
self
}
pub fn schema(mut self, schema: serde_json::Value) -> Self {
self.config_schema = schema;
self
}
pub fn output(mut self, output: OutputDescriptor) -> Self {
self.outputs.push(output);
self
}
pub fn dynamic_outputs(mut self) -> Self {
self.dynamic_outputs = true;
self
}
pub fn handles(mut self, handles: Handles) -> Self {
self.handles = handles;
self
}
pub fn container(mut self) -> Self {
self.container = true;
self
}
pub fn icon(mut self, icon: impl Into<String>) -> Self {
self.icon = Some(icon.into());
self
}
pub fn source(mut self, source: impl Into<String>) -> Self {
self.source = source.into();
self
}
pub fn bare(kind: &str) -> Self {
Self::new(kind, kind, NodeCategory::Other)
}
pub fn is_bare(&self) -> bool {
self.title == self.kind && self.description.is_empty() && self.source.is_empty()
}
}
pub mod schema {
use serde_json::{Value, json};
pub const DRAFT: &str = "https://json-schema.org/draft/2020-12/schema";
pub fn object(properties: Value, required: &[&str]) -> Value {
json!({
"$schema": DRAFT,
"type": "object",
"properties": properties,
"required": required,
"additionalProperties": false
})
}
pub fn open_object(properties: Value, required: &[&str]) -> Value {
json!({ "$schema": DRAFT, "type": "object", "properties": properties, "required": required })
}
pub fn deprecated_alias(schema: Value, replacement: &str) -> Value {
let mut schema = schema;
if let Value::Object(map) = &mut schema {
map.insert("deprecated".into(), Value::Bool(true));
map.insert("description".into(), Value::String(format!("Deprecated alias of `{replacement}`")));
}
schema
}
pub fn nested(properties: Value, required: &[&str]) -> Value {
json!({ "type": "object", "properties": properties, "required": required })
}
pub fn selector() -> Value {
json!({
"description": "Variable selector: [namespace, name, path...] or \"namespace.name\"",
"oneOf": [
{ "type": "array", "items": { "type": "string", "minLength": 1 }, "minItems": 2 },
{ "type": "string", "pattern": "^[^.\\s]+(\\.[^.\\s]+)+$" }
]
})
}
pub fn template(description: &str) -> Value {
json!({ "type": "string", "description": description })
}
pub fn value_type() -> Value {
json!({
"type": "string",
"enum": [
"any", "none", "string", "secret", "number", "integer", "float", "boolean", "object", "file",
"array[any]", "array[string]", "array[number]", "array[boolean]", "array[object]", "array[file]",
"array", "file-list", "json", "text-input", "paragraph", "select", "checkbox"
]
})
}
pub fn logical_operator() -> Value {
json!({ "type": "string", "enum": ["and", "or"], "default": "and" })
}
pub fn comparison_operator() -> Value {
json!({
"type": "string",
"enum": [
"contains", "not contains", "start with", "starts with", "start_with", "starts_with",
"end with", "ends with", "end_with", "ends_with", "is", "is not", "is_not",
"empty", "not empty", "not_empty", "in", "not in", "not_in", "all of", "all_of",
"=", "==", "eq", "≠", "!=", "ne", ">", "gt", "<", "lt", "≥", ">=", "ge", "≤", "<=", "le",
"null", "is null", "not null", "is not null", "not_null", "exists", "not exists", "not_exists"
]
})
}
pub fn key_condition() -> Value {
nested(
json!({
"key": { "type": "string" },
"comparison_operator": comparison_operator(),
"value": {}
}),
&["comparison_operator"],
)
}
pub fn condition() -> Value {
nested(
json!({
"variable_selector": selector(),
"comparison_operator": comparison_operator(),
"value": { "description": "Right-hand side; strings may contain placeholders" },
"sub_variable_condition": nested(
json!({
"logical_operator": logical_operator(),
"conditions": { "type": "array", "items": key_condition() }
}),
&[]
)
}),
&["variable_selector", "comparison_operator"],
)
}
pub fn conditions() -> Value {
json!({ "type": "array", "items": condition() })
}
pub fn variable_bindings() -> Value {
json!({
"type": "array",
"items": closed(nested(
json!({ "variable": { "type": "string", "minLength": 1 }, "value_selector": selector() }),
&["variable", "value_selector"]
))
})
}
pub fn closed(schema: Value) -> Value {
let mut schema = schema;
if let Value::Object(map) = &mut schema {
map.insert("additionalProperties".into(), Value::Bool(false));
}
schema
}
pub fn model() -> Value {
nested(
json!({
"provider": { "type": "string", "minLength": 1 },
"name": { "type": "string", "minLength": 1 },
"mode": { "type": "string", "enum": ["chat", "completion"] },
"completion_params": { "type": "object" }
}),
&["provider", "name"],
)
}
}