use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::Arc;
use serde::Deserialize;
use crate::dsl::ErrorStrategy;
use crate::event::Event;
use crate::graph::{FAIL_BRANCH, Graph, NodeIx};
use crate::node::NodeRole;
use crate::pool::{Selector, VariablePool};
use crate::snapshot::{EdgeStatus, FrameSnapshot, NodeStatus};
use crate::template::{Part, Template};
use crate::value::Value;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ResponseChunk {
pub node_id: String,
pub text: String,
pub is_final: bool,
}
impl ResponseChunk {
fn text(node_id: &str, text: String) -> Self {
Self { node_id: node_id.to_owned(), text, is_final: false }
}
fn done(node_id: &str) -> Self {
Self { node_id: node_id.to_owned(), text: String::new(), is_final: true }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Status {
Unknown,
Started,
Succeeded,
Handled,
Failed,
Skipped,
}
impl Status {
fn finished(self) -> bool {
matches!(self, Status::Succeeded | Status::Handled | Status::Failed | Status::Skipped)
}
}
#[derive(Debug)]
struct Response {
node: NodeIx,
id: Arc<str>,
parts: Vec<Part>,
queued: bool,
done: bool,
}
#[derive(Debug, Default)]
struct StreamBuf {
text: String,
}
#[derive(Debug)]
struct Active {
response: usize,
part: usize,
offset: usize,
}
#[derive(Deserialize)]
struct EndOutput {
value_selector: Selector,
}
#[derive(Debug)]
pub struct ResponseAssembler {
graph: Arc<Graph>,
responses: Vec<Response>,
status: Vec<Status>,
branch: Vec<Option<String>>,
deferred: Vec<bool>,
edge_state: Vec<Option<EdgeStatus>>,
rendered: Vec<bool>,
streams: HashMap<(Arc<str>, Arc<str>), StreamBuf>,
referenced: HashSet<(Arc<str>, Arc<str>)>,
pool: VariablePool,
queue: VecDeque<usize>,
active: Option<Active>,
emitted: String,
pass_through: bool,
include_end: bool,
finished: bool,
certain: Vec<bool>,
certain_valid: bool,
changed: Vec<NodeIx>,
}
impl ResponseAssembler {
pub fn new(graph: &Arc<Graph>) -> Self {
let mut this = Self {
graph: Arc::clone(graph),
responses: Vec::new(),
status: vec![Status::Unknown; graph.nodes().len()],
branch: vec![None; graph.nodes().len()],
deferred: graph
.nodes()
.iter()
.map(|n| n.policy.retry.max_retries > 0 || !matches!(n.policy.on_error, ErrorStrategy::Fail))
.collect(),
edge_state: vec![None; graph.edges().len()],
rendered: vec![false; graph.nodes().len()],
streams: HashMap::new(),
referenced: HashSet::new(),
pool: VariablePool::new(),
queue: VecDeque::new(),
active: None,
emitted: String::new(),
pass_through: false,
include_end: false,
finished: false,
certain: vec![false; graph.nodes().len()],
certain_valid: false,
changed: Vec::new(),
};
this.rebuild_responses();
this
}
pub fn pass_through_unreferenced(mut self, enabled: bool) -> Self {
self.pass_through = enabled;
self
}
pub fn include_end(mut self, enabled: bool) -> Self {
self.include_end = enabled;
self.rebuild_responses();
self
}
pub fn with_value(mut self, selector: &Selector, value: Value) -> Self {
self.pool.set(selector, value);
self
}
pub fn with_pool(mut self, pool: VariablePool) -> Self {
for (ns, name, value) in pool.iter() {
self.pool.insert(ns, name, value.clone());
}
self
}
pub fn resume_from(mut self, snapshot: &FrameSnapshot) -> Self {
let restored = VariablePool::from_snapshot(snapshot.pool.clone());
for (ns, name, value) in restored.iter() {
self.pool.insert(ns, name, value.clone());
}
for (ix, edge) in self.graph.edges().iter().enumerate() {
if let (Some(state), Some(slot)) = (snapshot.edges.get(edge.id.as_ref()), self.edge_state.get_mut(ix)) {
*slot = Some(*state);
}
}
for (node_id, node) in &snapshot.nodes {
let Some(ix) = self.root_node(node_id) else { continue };
let status = match node.status {
NodeStatus::Succeeded => Status::Succeeded,
NodeStatus::Skipped => Status::Skipped,
NodeStatus::Failed if self.has_taken_outgoing(ix) => Status::Handled,
NodeStatus::Failed => Status::Failed,
NodeStatus::Paused => Status::Started,
NodeStatus::Pending => Status::Unknown,
};
if let Some(slot) = self.status.get_mut(ix) {
*slot = status;
}
if let Some(slot) = self.rendered.get_mut(ix) {
*slot = status == Status::Succeeded;
}
}
self.certain_valid = false;
self.rebuild_responses();
self
}
fn has_taken_outgoing(&self, ix: NodeIx) -> bool {
self.graph.outgoing(ix).iter().any(|&ex| self.edge_state.get(ex).copied().flatten() == Some(EdgeStatus::Taken))
}
pub fn text_so_far(&self) -> &str {
&self.emitted
}
pub fn push(&mut self, event: &Event) -> Vec<ResponseChunk> {
let mut out = Vec::new();
if self.finished {
return out;
}
match event {
Event::NodeStarted { frame, node_id, .. } if frame.is_empty() => {
self.set_status(node_id, Status::Started);
}
Event::NodeChunk { frame, node_id, field, chunk, .. } => {
let key: (Arc<str>, Arc<str>) = (Arc::from(node_id.as_str()), Arc::from(field.as_str()));
if frame.is_empty() && self.referenced.contains(&key) {
self.streams.entry(key).or_default().text.push_str(chunk);
} else if self.pass_through && !chunk.is_empty() {
out.push(ResponseChunk::text(node_id, chunk.clone()));
}
}
Event::NodeSucceeded { frame, node_id, outputs, branch, .. } if frame.is_empty() => {
self.pool.set_outputs(node_id, outputs);
if let Some(ix) = self.root_node(node_id) {
if let Some(b) = self.branch.get_mut(ix) {
b.clone_from(branch);
}
}
self.set_status(node_id, Status::Succeeded);
}
Event::NodeFailed { frame, node_id, handled, outputs, .. } if frame.is_empty() => {
if *handled {
self.pool.set_outputs(node_id, outputs);
}
self.set_status(node_id, if *handled { Status::Handled } else { Status::Failed });
}
Event::NodeSkipped { frame, node_id } if frame.is_empty() => {
self.set_status(node_id, Status::Skipped);
}
Event::NodeRetry { frame, node_id, .. } if frame.is_empty() => {
self.streams.retain(|(node, _), _| node.as_ref() != node_id.as_str());
return out;
}
Event::RunSucceeded { .. } | Event::RunFailed { .. } | Event::RunAborted { .. } => {
out.extend(self.finish());
return out;
}
_ => return out,
}
self.refresh_certainty();
self.drain(false, &mut out);
out
}
pub fn finish(&mut self) -> Vec<ResponseChunk> {
let mut out = Vec::new();
if self.finished {
return out;
}
self.refresh_certainty();
self.drain(true, &mut out);
self.finished = true;
out
}
fn rebuild_responses(&mut self) {
let Some(scope) = self.graph.root_scope() else {
return;
};
let mut responses = Vec::new();
for &ix in &scope.nodes {
let Some(node) = self.graph.node(ix) else { continue };
if node.role != NodeRole::Response {
continue;
}
let parts = match node.kind.as_str() {
"answer" => node
.doc
.config
.get("answer")
.and_then(serde_json::Value::as_str)
.map(|s| Template::parse(s).parts().to_vec())
.unwrap_or_default(),
"end" if self.include_end => end_parts(&node.doc.config),
_ => continue,
};
let rendered = self.rendered.get(ix).copied().unwrap_or(false);
responses.push(Response { node: ix, id: node.id.clone(), parts, queued: rendered, done: rendered });
}
self.referenced = responses
.iter()
.flat_map(|r| r.parts.iter())
.filter_map(|p| match p {
Part::Var(sel) if sel.path().is_empty() => Some((Arc::from(sel.namespace()), Arc::from(sel.name()))),
_ => None,
})
.collect();
self.responses = responses;
}
fn root_node(&self, id: &str) -> Option<NodeIx> {
self.graph.index_of(id).filter(|&ix| self.graph.node(ix).is_some_and(|n| n.parent.is_none()))
}
fn set_status(&mut self, node_id: &str, status: Status) {
if let Some(ix) = self.root_node(node_id) {
if let Some(s) = self.status.get_mut(ix) {
if !(s.finished() && status == Status::Started) {
if *s != status {
self.changed.push(ix);
}
*s = status;
}
}
}
}
fn status_of(&self, ix: NodeIx) -> Status {
self.status.get(ix).copied().unwrap_or(Status::Unknown)
}
fn compute_certain(&self, ix: NodeIx, entry: NodeIx, certain: &[bool]) -> bool {
match self.status_of(ix) {
Status::Skipped => false,
Status::Unknown => ix == entry || self.has_certain_edge(ix, certain),
_ => true,
}
}
fn refresh_certainty(&mut self) {
let Some(scope) = self.graph.root_scope() else {
return;
};
let entry = scope.entry;
let mut certain = std::mem::take(&mut self.certain);
certain.resize(self.graph.nodes().len(), false);
let changed = std::mem::take(&mut self.changed);
let mut full = !self.certain_valid;
if !full {
let mut work: Vec<NodeIx> = changed;
while let Some(ix) = work.pop() {
let now = self.compute_certain(ix, entry, &certain);
let before = certain.get(ix).copied().unwrap_or(false);
if before && !now {
full = true;
break;
}
if now != before {
if let Some(c) = certain.get_mut(ix) {
*c = now;
}
}
if now {
for &ex in self.graph.outgoing(ix) {
if let Some(edge) = self.graph.edge(ex) {
if !certain.get(edge.target).copied().unwrap_or(false)
&& self.compute_certain(edge.target, entry, &certain)
{
if let Some(c) = certain.get_mut(edge.target) {
*c = true;
}
work.push(edge.target);
}
}
}
}
}
}
if full {
certain.iter_mut().for_each(|c| *c = false);
for &ix in &scope.nodes {
let is_certain = self.compute_certain(ix, entry, &certain);
if let Some(c) = certain.get_mut(ix) {
*c = is_certain;
}
}
}
for (i, r) in self.responses.iter_mut().enumerate() {
if !r.queued && certain.get(r.node).copied().unwrap_or(false) {
r.queued = true;
self.queue.push_back(i);
}
}
self.certain = certain;
self.certain_valid = true;
}
fn has_certain_edge(&self, ix: NodeIx, certain: &[bool]) -> bool {
self.graph.incoming(ix).iter().filter_map(|&ex| self.graph.edge(ex).map(|e| (ex, e))).any(|(ex, edge)| {
if !certain.get(edge.source).copied().unwrap_or(false) {
return false;
}
match self.edge_state.get(ex).copied().flatten() {
Some(EdgeStatus::Taken) => return true,
Some(EdgeStatus::Skipped) => return false,
Some(EdgeStatus::Unknown) | None => {}
}
let Some(src) = self.graph.node(edge.source) else { return false };
let status = self.status_of(edge.source);
let fail_branch = matches!(src.policy.on_error, ErrorStrategy::FailBranch);
match status {
Status::Failed | Status::Skipped => false,
Status::Handled if fail_branch => edge.handle.as_ref() == FAIL_BRANCH,
Status::Handled if src.role == NodeRole::Branch => {
default_branch(&src.policy.on_error).as_deref() == Some(edge.handle.as_ref())
}
Status::Handled => edge.handle.as_ref() != FAIL_BRANCH,
Status::Succeeded if src.role == NodeRole::Branch => {
self.branch.get(edge.source).and_then(Option::as_deref) == Some(edge.handle.as_ref())
}
_ if src.role == NodeRole::Branch => false,
_ if fail_branch => false,
_ => edge.handle.as_ref() != FAIL_BRANCH,
}
})
}
fn drain(&mut self, flush: bool, out: &mut Vec<ResponseChunk>) {
loop {
if self.active.is_none() {
match self.queue.pop_front() {
Some(response) => self.active = Some(Active { response, part: 0, offset: 0 }),
None => return,
}
}
let Some(active) = self.active.as_mut() else { return };
let Some(response) = self.responses.get(active.response) else {
self.active = None;
continue;
};
if self.status.get(response.node).copied() == Some(Status::Skipped) {
self.finish_active(None, out);
continue;
}
let id = Arc::clone(&response.id);
match response.parts.get(active.part).cloned() {
None => self.finish_active(Some(&id), out),
Some(Part::Text(text)) => {
emit(&mut self.emitted, out, &id, text);
active.part += 1;
active.offset = 0;
}
Some(Part::Var(sel)) => {
let sources = Sources {
graph: &self.graph,
status: &self.status,
deferred: &self.deferred,
streams: &self.streams,
pool: &self.pool,
};
let step = resolve_var(&sources, &sel, active.offset, flush);
if !step.text.is_empty() {
emit(&mut self.emitted, out, &id, step.text);
}
active.offset = step.offset;
if step.complete {
active.part += 1;
active.offset = 0;
} else {
return;
}
}
}
}
}
fn finish_active(&mut self, id: Option<&Arc<str>>, out: &mut Vec<ResponseChunk>) {
if let Some(active) = self.active.take() {
if let Some(r) = self.responses.get_mut(active.response) {
r.done = true;
}
if let Some(id) = id {
out.push(ResponseChunk::done(id));
}
}
}
}
struct Step {
text: String,
offset: usize,
complete: bool,
}
struct Sources<'a> {
graph: &'a Graph,
status: &'a [Status],
deferred: &'a [bool],
streams: &'a HashMap<(Arc<str>, Arc<str>), StreamBuf>,
pool: &'a VariablePool,
}
fn resolve_var(sources: &Sources<'_>, sel: &Selector, offset: usize, flush: bool) -> Step {
let Sources { graph, status, deferred, streams, pool } = *sources;
let node_ix = graph.index_of(sel.namespace()).filter(|&ix| graph.node(ix).is_some_and(|n| n.parent.is_none()));
let Some(ix) = node_ix else {
return Step { text: pool.get(sel).map(|v| v.to_text()).unwrap_or_default(), offset: 0, complete: true };
};
let node_status = status.get(ix).copied().unwrap_or(Status::Unknown);
let is_deferred = deferred.get(ix).copied().unwrap_or(false);
let key = (Arc::from(sel.namespace()), Arc::from(sel.name()));
let buffered = if sel.path().is_empty() { streams.get(&key).filter(|b| !b.text.is_empty()) } else { None };
if is_deferred {
let text = match node_status {
Status::Succeeded => match buffered {
Some(buf) => buf.text.clone(),
None => pool.get(sel).map(|v| v.to_text()).unwrap_or_default(),
},
Status::Handled => pool.get(sel).map(|v| v.to_text()).unwrap_or_default(),
_ => String::new(),
};
return Step { text, offset: 0, complete: node_status.finished() || flush };
}
let mut text = String::new();
let mut new_offset = offset;
let streamed = buffered.is_some();
if let Some(buf) = buffered {
if let Some(rest) = buf.text.get(offset..) {
text.push_str(rest);
new_offset = buf.text.len();
}
}
let complete = match node_status {
Status::Skipped | Status::Failed => true,
Status::Succeeded | Status::Handled => {
if !streamed {
if let Some(v) = pool.get(sel) {
text.push_str(&v.to_text());
}
}
true
}
Status::Unknown | Status::Started => {
if flush && !streamed {
if let Some(v) = pool.get(sel) {
text.push_str(&v.to_text());
}
}
flush
}
};
Step { text, offset: new_offset, complete }
}
fn default_branch(strategy: &ErrorStrategy) -> Option<String> {
match strategy {
ErrorStrategy::DefaultValue { values } => values.get("branch").and_then(Value::as_str).map(str::to_owned),
_ => None,
}
}
fn emit(emitted: &mut String, out: &mut Vec<ResponseChunk>, id: &str, text: String) {
if text.is_empty() {
return;
}
emitted.push_str(&text);
out.push(ResponseChunk::text(id, text));
}
fn end_parts(config: &serde_json::Map<String, serde_json::Value>) -> Vec<Part> {
let outputs: Vec<EndOutput> = config
.get("outputs")
.cloned()
.and_then(|v| serde_json::from_value::<Vec<serde_json::Value>>(v).ok())
.unwrap_or_default()
.into_iter()
.filter_map(|v| serde_json::from_value(v).ok())
.collect();
let mut parts = Vec::with_capacity(outputs.len() * 2);
for (i, o) in outputs.into_iter().enumerate() {
if i > 0 {
parts.push(Part::Text("\n".to_owned()));
}
parts.push(Part::Var(o.value_selector));
}
parts
}