1use std::collections::{HashMap, HashSet, VecDeque};
66use std::sync::Arc;
67
68use serde::Deserialize;
69
70use crate::dsl::ErrorStrategy;
71use crate::event::Event;
72use crate::graph::{FAIL_BRANCH, Graph, NodeIx};
73use crate::node::NodeRole;
74use crate::pool::{Selector, VariablePool};
75use crate::snapshot::{EdgeStatus, FrameSnapshot, NodeStatus};
76use crate::template::{Part, Template};
77use crate::value::Value;
78
79#[derive(Clone, Debug, PartialEq, Eq)]
81pub struct ResponseChunk {
82 pub node_id: String,
85 pub text: String,
86 pub is_final: bool,
89}
90
91impl ResponseChunk {
92 fn text(node_id: &str, text: String) -> Self {
93 Self { node_id: node_id.to_owned(), text, is_final: false }
94 }
95
96 fn done(node_id: &str) -> Self {
97 Self { node_id: node_id.to_owned(), text: String::new(), is_final: true }
98 }
99}
100
101#[derive(Clone, Copy, Debug, PartialEq, Eq)]
102enum Status {
103 Unknown,
104 Started,
105 Succeeded,
106 Handled,
108 Failed,
110 Skipped,
111}
112
113impl Status {
114 fn finished(self) -> bool {
115 matches!(self, Status::Succeeded | Status::Handled | Status::Failed | Status::Skipped)
116 }
117}
118
119#[derive(Debug)]
120struct Response {
121 node: NodeIx,
122 id: Arc<str>,
123 parts: Vec<Part>,
124 queued: bool,
125 done: bool,
126}
127
128#[derive(Debug, Default)]
129struct StreamBuf {
130 text: String,
131}
132
133#[derive(Debug)]
134struct Active {
135 response: usize,
136 part: usize,
137 offset: usize,
139}
140
141#[derive(Deserialize)]
142struct EndOutput {
143 value_selector: Selector,
144}
145
146#[derive(Debug)]
148pub struct ResponseAssembler {
149 graph: Arc<Graph>,
150 responses: Vec<Response>,
151 status: Vec<Status>,
152 branch: Vec<Option<String>>,
153 deferred: Vec<bool>,
155 edge_state: Vec<Option<EdgeStatus>>,
157 rendered: Vec<bool>,
159 streams: HashMap<(Arc<str>, Arc<str>), StreamBuf>,
161 referenced: HashSet<(Arc<str>, Arc<str>)>,
162 pool: VariablePool,
163 queue: VecDeque<usize>,
164 active: Option<Active>,
165 emitted: String,
166 pass_through: bool,
167 include_end: bool,
168 finished: bool,
169 certain: Vec<bool>,
171 certain_valid: bool,
173 changed: Vec<NodeIx>,
175}
176
177impl ResponseAssembler {
178 pub fn new(graph: &Arc<Graph>) -> Self {
180 let mut this = Self {
181 graph: Arc::clone(graph),
182 responses: Vec::new(),
183 status: vec![Status::Unknown; graph.nodes().len()],
184 branch: vec![None; graph.nodes().len()],
185 deferred: graph
186 .nodes()
187 .iter()
188 .map(|n| n.policy.retry.max_retries > 0 || !matches!(n.policy.on_error, ErrorStrategy::Fail))
189 .collect(),
190 edge_state: vec![None; graph.edges().len()],
191 rendered: vec![false; graph.nodes().len()],
192 streams: HashMap::new(),
193 referenced: HashSet::new(),
194 pool: VariablePool::new(),
195 queue: VecDeque::new(),
196 active: None,
197 emitted: String::new(),
198 pass_through: false,
199 include_end: false,
200 finished: false,
201 certain: vec![false; graph.nodes().len()],
202 certain_valid: false,
203 changed: Vec::new(),
204 };
205 this.rebuild_responses();
206 this
207 }
208
209 pub fn pass_through_unreferenced(mut self, enabled: bool) -> Self {
211 self.pass_through = enabled;
212 self
213 }
214
215 pub fn include_end(mut self, enabled: bool) -> Self {
217 self.include_end = enabled;
218 self.rebuild_responses();
219 self
220 }
221
222 pub fn with_value(mut self, selector: &Selector, value: Value) -> Self {
224 self.pool.set(selector, value);
225 self
226 }
227
228 pub fn with_pool(mut self, pool: VariablePool) -> Self {
230 for (ns, name, value) in pool.iter() {
231 self.pool.insert(ns, name, value.clone());
232 }
233 self
234 }
235
236 pub fn resume_from(mut self, snapshot: &FrameSnapshot) -> Self {
246 let restored = VariablePool::from_snapshot(snapshot.pool.clone());
247 for (ns, name, value) in restored.iter() {
248 self.pool.insert(ns, name, value.clone());
249 }
250 for (ix, edge) in self.graph.edges().iter().enumerate() {
251 if let (Some(state), Some(slot)) = (snapshot.edges.get(edge.id.as_ref()), self.edge_state.get_mut(ix)) {
252 *slot = Some(*state);
253 }
254 }
255 for (node_id, node) in &snapshot.nodes {
256 let Some(ix) = self.root_node(node_id) else { continue };
257 let status = match node.status {
258 NodeStatus::Succeeded => Status::Succeeded,
259 NodeStatus::Skipped => Status::Skipped,
260 NodeStatus::Failed if self.has_taken_outgoing(ix) => Status::Handled,
261 NodeStatus::Failed => Status::Failed,
262 NodeStatus::Paused => Status::Started,
263 NodeStatus::Pending => Status::Unknown,
264 };
265 if let Some(slot) = self.status.get_mut(ix) {
266 *slot = status;
267 }
268 if let Some(slot) = self.rendered.get_mut(ix) {
269 *slot = status == Status::Succeeded;
270 }
271 }
272 self.certain_valid = false;
273 self.rebuild_responses();
274 self
275 }
276
277 fn has_taken_outgoing(&self, ix: NodeIx) -> bool {
278 self.graph.outgoing(ix).iter().any(|&ex| self.edge_state.get(ex).copied().flatten() == Some(EdgeStatus::Taken))
279 }
280
281 pub fn text_so_far(&self) -> &str {
283 &self.emitted
284 }
285
286 pub fn push(&mut self, event: &Event) -> Vec<ResponseChunk> {
288 let mut out = Vec::new();
289 if self.finished {
290 return out;
291 }
292 match event {
293 Event::NodeStarted { frame, node_id, .. } if frame.is_empty() => {
294 self.set_status(node_id, Status::Started);
295 }
296 Event::NodeChunk { frame, node_id, field, chunk, .. } => {
297 let key: (Arc<str>, Arc<str>) = (Arc::from(node_id.as_str()), Arc::from(field.as_str()));
298 if frame.is_empty() && self.referenced.contains(&key) {
299 self.streams.entry(key).or_default().text.push_str(chunk);
300 } else if self.pass_through && !chunk.is_empty() {
301 out.push(ResponseChunk::text(node_id, chunk.clone()));
302 }
303 }
304 Event::NodeSucceeded { frame, node_id, outputs, branch, .. } if frame.is_empty() => {
305 self.pool.set_outputs(node_id, outputs);
306 if let Some(ix) = self.root_node(node_id) {
307 if let Some(b) = self.branch.get_mut(ix) {
308 b.clone_from(branch);
309 }
310 }
311 self.set_status(node_id, Status::Succeeded);
312 }
313 Event::NodeFailed { frame, node_id, handled, outputs, .. } if frame.is_empty() => {
314 if *handled {
315 self.pool.set_outputs(node_id, outputs);
316 }
317 self.set_status(node_id, if *handled { Status::Handled } else { Status::Failed });
318 }
319 Event::NodeSkipped { frame, node_id } if frame.is_empty() => {
320 self.set_status(node_id, Status::Skipped);
321 }
322 Event::NodeRetry { frame, node_id, .. } if frame.is_empty() => {
323 self.streams.retain(|(node, _), _| node.as_ref() != node_id.as_str());
325 return out;
326 }
327 Event::RunSucceeded { .. } | Event::RunFailed { .. } | Event::RunAborted { .. } => {
328 out.extend(self.finish());
329 return out;
330 }
331 _ => return out,
332 }
333 self.refresh_certainty();
334 self.drain(false, &mut out);
335 out
336 }
337
338 pub fn finish(&mut self) -> Vec<ResponseChunk> {
341 let mut out = Vec::new();
342 if self.finished {
343 return out;
344 }
345 self.refresh_certainty();
346 self.drain(true, &mut out);
347 self.finished = true;
348 out
349 }
350
351 fn rebuild_responses(&mut self) {
352 let Some(scope) = self.graph.root_scope() else {
353 return;
354 };
355 let mut responses = Vec::new();
356 for &ix in &scope.nodes {
357 let Some(node) = self.graph.node(ix) else { continue };
358 if node.role != NodeRole::Response {
359 continue;
360 }
361 let parts = match node.kind.as_str() {
362 "answer" => node
363 .doc
364 .config
365 .get("answer")
366 .and_then(serde_json::Value::as_str)
367 .map(|s| Template::parse(s).parts().to_vec())
368 .unwrap_or_default(),
369 "end" if self.include_end => end_parts(&node.doc.config),
370 _ => continue,
371 };
372 let rendered = self.rendered.get(ix).copied().unwrap_or(false);
373 responses.push(Response { node: ix, id: node.id.clone(), parts, queued: rendered, done: rendered });
374 }
375 self.referenced = responses
376 .iter()
377 .flat_map(|r| r.parts.iter())
378 .filter_map(|p| match p {
379 Part::Var(sel) if sel.path().is_empty() => Some((Arc::from(sel.namespace()), Arc::from(sel.name()))),
380 _ => None,
381 })
382 .collect();
383 self.responses = responses;
384 }
385
386 fn root_node(&self, id: &str) -> Option<NodeIx> {
387 self.graph.index_of(id).filter(|&ix| self.graph.node(ix).is_some_and(|n| n.parent.is_none()))
388 }
389
390 fn set_status(&mut self, node_id: &str, status: Status) {
391 if let Some(ix) = self.root_node(node_id) {
392 if let Some(s) = self.status.get_mut(ix) {
393 if !(s.finished() && status == Status::Started) {
395 if *s != status {
396 self.changed.push(ix);
397 }
398 *s = status;
399 }
400 }
401 }
402 }
403
404 fn status_of(&self, ix: NodeIx) -> Status {
405 self.status.get(ix).copied().unwrap_or(Status::Unknown)
406 }
407
408 fn compute_certain(&self, ix: NodeIx, entry: NodeIx, certain: &[bool]) -> bool {
410 match self.status_of(ix) {
411 Status::Skipped => false,
412 Status::Unknown => ix == entry || self.has_certain_edge(ix, certain),
413 _ => true,
414 }
415 }
416
417 fn refresh_certainty(&mut self) {
424 let Some(scope) = self.graph.root_scope() else {
425 return;
426 };
427 let entry = scope.entry;
428 let mut certain = std::mem::take(&mut self.certain);
429 certain.resize(self.graph.nodes().len(), false);
430 let changed = std::mem::take(&mut self.changed);
431 let mut full = !self.certain_valid;
432 if !full {
433 let mut work: Vec<NodeIx> = changed;
434 while let Some(ix) = work.pop() {
435 let now = self.compute_certain(ix, entry, &certain);
436 let before = certain.get(ix).copied().unwrap_or(false);
437 if before && !now {
438 full = true;
439 break;
440 }
441 if now != before {
442 if let Some(c) = certain.get_mut(ix) {
443 *c = now;
444 }
445 }
446 if now {
449 for &ex in self.graph.outgoing(ix) {
450 if let Some(edge) = self.graph.edge(ex) {
451 if !certain.get(edge.target).copied().unwrap_or(false)
452 && self.compute_certain(edge.target, entry, &certain)
453 {
454 if let Some(c) = certain.get_mut(edge.target) {
455 *c = true;
456 }
457 work.push(edge.target);
458 }
459 }
460 }
461 }
462 }
463 }
464 if full {
465 certain.iter_mut().for_each(|c| *c = false);
466 for &ix in &scope.nodes {
467 let is_certain = self.compute_certain(ix, entry, &certain);
468 if let Some(c) = certain.get_mut(ix) {
469 *c = is_certain;
470 }
471 }
472 }
473 for (i, r) in self.responses.iter_mut().enumerate() {
474 if !r.queued && certain.get(r.node).copied().unwrap_or(false) {
475 r.queued = true;
476 self.queue.push_back(i);
477 }
478 }
479 self.certain = certain;
480 self.certain_valid = true;
481 }
482
483 fn has_certain_edge(&self, ix: NodeIx, certain: &[bool]) -> bool {
485 self.graph.incoming(ix).iter().filter_map(|&ex| self.graph.edge(ex).map(|e| (ex, e))).any(|(ex, edge)| {
486 if !certain.get(edge.source).copied().unwrap_or(false) {
487 return false;
488 }
489 match self.edge_state.get(ex).copied().flatten() {
491 Some(EdgeStatus::Taken) => return true,
492 Some(EdgeStatus::Skipped) => return false,
493 Some(EdgeStatus::Unknown) | None => {}
494 }
495 let Some(src) = self.graph.node(edge.source) else { return false };
496 let status = self.status_of(edge.source);
497 let fail_branch = matches!(src.policy.on_error, ErrorStrategy::FailBranch);
498 match status {
499 Status::Failed | Status::Skipped => false,
500 Status::Handled if fail_branch => edge.handle.as_ref() == FAIL_BRANCH,
501 Status::Handled if src.role == NodeRole::Branch => {
502 default_branch(&src.policy.on_error).as_deref() == Some(edge.handle.as_ref())
503 }
504 Status::Handled => edge.handle.as_ref() != FAIL_BRANCH,
505 Status::Succeeded if src.role == NodeRole::Branch => {
506 self.branch.get(edge.source).and_then(Option::as_deref) == Some(edge.handle.as_ref())
507 }
508 _ if src.role == NodeRole::Branch => false,
509 _ if fail_branch => false,
511 _ => edge.handle.as_ref() != FAIL_BRANCH,
512 }
513 })
514 }
515
516 fn drain(&mut self, flush: bool, out: &mut Vec<ResponseChunk>) {
518 loop {
519 if self.active.is_none() {
520 match self.queue.pop_front() {
521 Some(response) => self.active = Some(Active { response, part: 0, offset: 0 }),
522 None => return,
523 }
524 }
525 let Some(active) = self.active.as_mut() else { return };
526 let Some(response) = self.responses.get(active.response) else {
527 self.active = None;
528 continue;
529 };
530 if self.status.get(response.node).copied() == Some(Status::Skipped) {
532 self.finish_active(None, out);
533 continue;
534 }
535 let id = Arc::clone(&response.id);
536 match response.parts.get(active.part).cloned() {
537 None => self.finish_active(Some(&id), out),
538 Some(Part::Text(text)) => {
539 emit(&mut self.emitted, out, &id, text);
540 active.part += 1;
541 active.offset = 0;
542 }
543 Some(Part::Var(sel)) => {
544 let sources = Sources {
545 graph: &self.graph,
546 status: &self.status,
547 deferred: &self.deferred,
548 streams: &self.streams,
549 pool: &self.pool,
550 };
551 let step = resolve_var(&sources, &sel, active.offset, flush);
552 if !step.text.is_empty() {
553 emit(&mut self.emitted, out, &id, step.text);
554 }
555 active.offset = step.offset;
556 if step.complete {
557 active.part += 1;
558 active.offset = 0;
559 } else {
560 return;
561 }
562 }
563 }
564 }
565 }
566
567 fn finish_active(&mut self, id: Option<&Arc<str>>, out: &mut Vec<ResponseChunk>) {
568 if let Some(active) = self.active.take() {
569 if let Some(r) = self.responses.get_mut(active.response) {
570 r.done = true;
571 }
572 if let Some(id) = id {
573 out.push(ResponseChunk::done(id));
574 }
575 }
576 }
577}
578
579struct Step {
580 text: String,
581 offset: usize,
582 complete: bool,
583}
584
585struct Sources<'a> {
587 graph: &'a Graph,
588 status: &'a [Status],
589 deferred: &'a [bool],
590 streams: &'a HashMap<(Arc<str>, Arc<str>), StreamBuf>,
591 pool: &'a VariablePool,
592}
593
594fn resolve_var(sources: &Sources<'_>, sel: &Selector, offset: usize, flush: bool) -> Step {
595 let Sources { graph, status, deferred, streams, pool } = *sources;
596 let node_ix = graph.index_of(sel.namespace()).filter(|&ix| graph.node(ix).is_some_and(|n| n.parent.is_none()));
597 let Some(ix) = node_ix else {
598 return Step { text: pool.get(sel).map(|v| v.to_text()).unwrap_or_default(), offset: 0, complete: true };
600 };
601 let node_status = status.get(ix).copied().unwrap_or(Status::Unknown);
602
603 let is_deferred = deferred.get(ix).copied().unwrap_or(false);
604 let key = (Arc::from(sel.namespace()), Arc::from(sel.name()));
605 let buffered = if sel.path().is_empty() { streams.get(&key).filter(|b| !b.text.is_empty()) } else { None };
606
607 if is_deferred {
608 let text = match node_status {
610 Status::Succeeded => match buffered {
611 Some(buf) => buf.text.clone(),
612 None => pool.get(sel).map(|v| v.to_text()).unwrap_or_default(),
613 },
614 Status::Handled => pool.get(sel).map(|v| v.to_text()).unwrap_or_default(),
616 _ => String::new(),
617 };
618 return Step { text, offset: 0, complete: node_status.finished() || flush };
619 }
620
621 let mut text = String::new();
623 let mut new_offset = offset;
624 let streamed = buffered.is_some();
625 if let Some(buf) = buffered {
626 if let Some(rest) = buf.text.get(offset..) {
627 text.push_str(rest);
628 new_offset = buf.text.len();
629 }
630 }
631
632 let complete = match node_status {
633 Status::Skipped | Status::Failed => true,
634 Status::Succeeded | Status::Handled => {
635 if !streamed {
636 if let Some(v) = pool.get(sel) {
637 text.push_str(&v.to_text());
638 }
639 }
640 true
641 }
642 Status::Unknown | Status::Started => {
643 if flush && !streamed {
644 if let Some(v) = pool.get(sel) {
645 text.push_str(&v.to_text());
646 }
647 }
648 flush
649 }
650 };
651 Step { text, offset: new_offset, complete }
652}
653
654fn default_branch(strategy: &ErrorStrategy) -> Option<String> {
656 match strategy {
657 ErrorStrategy::DefaultValue { values } => values.get("branch").and_then(Value::as_str).map(str::to_owned),
658 _ => None,
659 }
660}
661
662fn emit(emitted: &mut String, out: &mut Vec<ResponseChunk>, id: &str, text: String) {
663 if text.is_empty() {
664 return;
665 }
666 emitted.push_str(&text);
667 out.push(ResponseChunk::text(id, text));
668}
669
670fn end_parts(config: &serde_json::Map<String, serde_json::Value>) -> Vec<Part> {
671 let outputs: Vec<EndOutput> = config
672 .get("outputs")
673 .cloned()
674 .and_then(|v| serde_json::from_value::<Vec<serde_json::Value>>(v).ok())
675 .unwrap_or_default()
676 .into_iter()
677 .filter_map(|v| serde_json::from_value(v).ok())
678 .collect();
679 let mut parts = Vec::with_capacity(outputs.len() * 2);
680 for (i, o) in outputs.into_iter().enumerate() {
681 if i > 0 {
682 parts.push(Part::Text("\n".to_owned()));
683 }
684 parts.push(Part::Var(o.value_selector));
685 }
686 parts
687}