use crate::types::{AttractorEdge, AttractorGraph, NodeOutcome, OutcomeStatus, RunContext};
use async_trait::async_trait;
use std::any::Any;
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use streamweave::node::{InputStreams, Node, NodeExecutionError, OutputStreams};
use tokio_stream::wrappers::ReceiverStream;
use tracing::instrument;
#[derive(Clone)]
pub struct SelectEdgeInput {
pub node_id: String,
pub outcome: NodeOutcome,
pub context: RunContext,
pub graph: AttractorGraph,
}
#[derive(Clone)]
#[allow(dead_code)] pub struct SelectEdgeOutput {
pub next_node_id: Option<String>,
pub done: bool,
}
#[instrument(level = "trace", skip(input))]
pub(crate) fn select_edge(input: &SelectEdgeInput) -> SelectEdgeOutput {
let edges = input.graph.outgoing_edges(&input.node_id);
if edges.is_empty() {
return SelectEdgeOutput {
next_node_id: None,
done: input.outcome.status == OutcomeStatus::Success,
};
}
let condition_matched: Vec<_> = edges
.iter()
.filter(|e| {
e.condition
.as_ref()
.is_some_and(|c| evaluate_condition(c, &input.outcome, &input.context))
})
.copied()
.collect();
if !condition_matched.is_empty() {
let best = best_by_weight_then_lexical(condition_matched);
return SelectEdgeOutput {
next_node_id: Some(best.to_node.clone()),
done: false,
};
}
if let Some(ref pref) = input.outcome.preferred_label {
let normalized_pref = normalize_label(pref);
for e in &edges {
if e
.label
.as_ref()
.is_some_and(|l| normalize_label(l) == normalized_pref)
{
return SelectEdgeOutput {
next_node_id: Some(e.to_node.clone()),
done: false,
};
}
}
}
for sid in &input.outcome.suggested_next_ids {
for e in &edges {
if e.to_node == *sid {
return SelectEdgeOutput {
next_node_id: Some(e.to_node.clone()),
done: false,
};
}
}
}
let unconditional: Vec<_> = edges
.iter()
.filter(|e| e.condition.is_none())
.copied()
.collect();
if !unconditional.is_empty() {
let best = best_by_weight_then_lexical(unconditional);
return SelectEdgeOutput {
next_node_id: Some(best.to_node.clone()),
done: false,
};
}
let best = best_by_weight_then_lexical(edges);
SelectEdgeOutput {
next_node_id: Some(best.to_node.clone()),
done: false,
}
}
#[instrument(level = "trace", skip(_outcome, context))]
pub(crate) fn evaluate_condition(cond: &str, _outcome: &NodeOutcome, context: &RunContext) -> bool {
let cond = cond.trim();
if let Some(stripped) = cond.strip_prefix("outcome=") {
let outcome_str = context.get("outcome").map(|s| s.as_str()).unwrap_or("");
return stripped.eq_ignore_ascii_case(outcome_str)
|| (stripped.eq_ignore_ascii_case("success") && outcome_str == "SUCCESS")
|| (stripped.eq_ignore_ascii_case("fail") && outcome_str == "FAIL");
}
if let Some(stripped) = cond.strip_prefix("outcome!=") {
let outcome_str = context.get("outcome").map(|s| s.as_str()).unwrap_or("");
return !stripped.eq_ignore_ascii_case(outcome_str);
}
if let Some((key, value)) = cond.split_once('=') {
let key = key.trim();
let value = value.trim();
if !key.is_empty() {
return context
.get(key)
.map(|v| v.as_str() == value || value.eq_ignore_ascii_case(v))
.unwrap_or(false);
}
}
false
}
#[instrument(level = "trace")]
pub(crate) fn normalize_label(l: &str) -> String {
l.to_lowercase()
.trim()
.trim_start_matches(|c: char| c == '[' || c.is_ascii_alphabetic())
.trim_start_matches([')', ' ', '-'])
.to_string()
}
#[instrument(level = "trace")]
pub(crate) fn best_by_weight_then_lexical(edges: Vec<&AttractorEdge>) -> &AttractorEdge {
let mut v: Vec<_> = edges.into_iter().collect();
v.sort_by(|a, b| {
b.weight
.cmp(&a.weight)
.then_with(|| a.to_node.cmp(&b.to_node))
});
v[0]
}
#[allow(dead_code)] pub struct SelectEdgeNode {
name: String,
input_ports: Vec<String>,
output_ports: Vec<String>,
}
impl SelectEdgeNode {
#[allow(dead_code)]
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
input_ports: vec!["in".to_string()],
output_ports: vec!["out".to_string()],
}
}
}
#[async_trait]
impl Node for SelectEdgeNode {
fn name(&self) -> &str {
&self.name
}
fn set_name(&mut self, name: &str) {
self.name = name.to_string();
}
fn input_port_names(&self) -> &[String] {
&self.input_ports
}
fn output_port_names(&self) -> &[String] {
&self.output_ports
}
fn has_input_port(&self, name: &str) -> bool {
name == "in"
}
fn has_output_port(&self, name: &str) -> bool {
name == "out"
}
fn execute(
&self,
mut inputs: InputStreams,
) -> Pin<
Box<dyn std::future::Future<Output = Result<OutputStreams, NodeExecutionError>> + Send + '_>,
> {
Box::pin(async move {
let in_stream = inputs.remove("in").ok_or("Missing 'in' input")?;
let (out_tx, out_rx) = tokio::sync::mpsc::channel(16);
tokio::spawn(async move {
use futures::StreamExt;
let mut s = in_stream;
while let Some(item) = s.next().await {
let input = match item.downcast::<SelectEdgeInput>() {
Ok(arc) => (*arc).clone(),
Err(_) => continue,
};
let output = select_edge(&input);
let _ = out_tx
.send(Arc::new(output) as Arc<dyn Any + Send + Sync>)
.await;
}
});
let mut outputs = HashMap::new();
outputs.insert(
"out".to_string(),
Box::pin(ReceiverStream::new(out_rx))
as Pin<Box<dyn futures::Stream<Item = Arc<dyn Any + Send + Sync>> + Send>>,
);
Ok(outputs)
})
}
}