use serde::{Deserialize, Serialize};
pub use crate::language::span::Span;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", content = "value", rename_all = "snake_case")]
pub enum Literal {
Str(String),
Num(f64),
Ident(String),
}
impl Literal {
pub fn as_display(&self) -> String {
match self {
Literal::Str(s) | Literal::Ident(s) => s.clone(),
Literal::Num(n) => {
if n.fract() == 0.0 {
format!("{}", *n as i64)
} else {
format!("{n}")
}
}
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Program {
pub graphs: Vec<GraphDecl>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct GraphDecl {
pub name: String,
pub span: Span,
pub start: Option<String>,
pub defaults: Vec<(String, Literal)>,
pub input: Vec<IoFieldDecl>,
pub output: Vec<IoFieldDecl>,
pub checkpoint: Option<String>,
pub interrupt: Option<String>,
pub channels: Vec<ChannelDecl>,
pub nodes: Vec<NodeDecl>,
pub edges: Vec<EdgeDecl>,
pub joins: Vec<JoinDecl>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct IoFieldDecl {
pub name: String,
pub ty: String,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ChannelDecl {
pub name: String,
pub reducer: String,
pub args: Vec<Literal>,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct NodeDecl {
pub name: String,
pub kind: Option<String>,
pub model: Option<String>,
pub prompt: Option<String>,
pub tools: Vec<String>,
pub next: Option<String>,
pub routes: Vec<RouteDecl>,
pub agent: Option<String>,
pub graph: Option<String>,
pub script: Option<String>,
pub input: Option<String>,
pub command: Option<CommandDecl>,
pub sends: Vec<SendDecl>,
pub sources: Vec<String>,
pub options: Vec<String>,
pub checkpoint: Option<String>,
pub timeout: Option<Literal>,
pub retry: Vec<(String, Literal)>,
pub metadata: Vec<(String, Literal)>,
pub span: Span,
}
impl NodeDecl {
pub fn empty(name: String, span: Span) -> Self {
Self {
name,
kind: None,
model: None,
prompt: None,
tools: Vec::new(),
next: None,
routes: Vec::new(),
agent: None,
graph: None,
script: None,
input: None,
command: None,
sends: Vec::new(),
sources: Vec::new(),
options: Vec::new(),
checkpoint: None,
timeout: None,
retry: Vec::new(),
metadata: Vec::new(),
span,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct RouteDecl {
pub label: String,
pub target: String,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct EdgeDecl {
pub from: String,
pub to: String,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CommandDecl {
pub goto: Option<String>,
pub update: Vec<(String, Literal)>,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct SendDecl {
pub target: String,
pub input: Option<String>,
pub span: Span,
}
#[derive(Clone, Debug, PartialEq)]
pub struct JoinDecl {
pub sources: Vec<String>,
pub target: String,
pub span: Span,
}