Skip to main content

graphrt_core/
response.rs

1//! Ordered response streaming.
2//!
3//! The engine emits raw events as soon as they happen: tokens from parallel
4//! LLM nodes interleave, and nodes finish in any order. A chat-style consumer
5//! wants something different: the text of each response node (`answer`, and
6//! optionally `end`) rendered in template order, with model tokens streamed
7//! through as early as it is safe to do so.
8//!
9//! [`ResponseAssembler`] is a consumer-side transducer that does exactly that:
10//!
11//! ```ignore
12//! let mut assembler = ResponseAssembler::new(engine.graph());
13//! while let Some(event) = run.next().await {
14//!     for chunk in assembler.push(&event) {
15//!         print!("{}", chunk.text);
16//!     }
17//! }
18//! ```
19//!
20//! ## Rules
21//!
22//! - Only response nodes of the **root scope** are assembled. Events from child
23//!   frames (iteration / loop bodies) are ignored, unless
24//!   [`ResponseAssembler::pass_through_unreferenced`] is enabled, in which case
25//!   their chunks are forwarded verbatim.
26//! - At most one response is *active* at a time. Responses are activated in the
27//!   order they become **certain** to execute (ties broken by topological
28//!   order). Certainty is a linear-time fixpoint over the graph — never a path
29//!   enumeration:
30//!   - a node that started or finished is certain, and so is the root entry;
31//!   - a node is certain when one incoming edge comes from a certain source and
32//!     that edge will be taken if the source succeeds: every edge of a
33//!     non-branching node (except `fail-branch` edges), or the edge matching the
34//!     handle a finished branch node selected. Node failures are not anticipated,
35//!     except that an unfinished node with the `fail-branch` strategy makes none
36//!     of its edges certain (either side may be taken).
37//! - While a response is active, its template is drained left to right:
38//!   literal text is emitted immediately; a variable produced by a graph node
39//!   emits that node's buffered stream chunks for the variable's field as they
40//!   arrive; once the node finishes, a variable that never streamed is emitted
41//!   whole (`Value::to_text`). Variables of skipped or failed nodes render as
42//!   nothing. Variables outside graph nodes (`sys.*`, `conversation.*`, ...)
43//!   are resolved from values given via [`ResponseAssembler::with_value`] /
44//!   [`ResponseAssembler::with_pool`], or render as nothing.
45//! - `end` nodes do not stream by default (their outputs are structured data,
46//!   delivered in `RunSucceeded`). Enable [`ResponseAssembler::include_end`] to
47//!   render them as their output values joined by newlines.
48//! - **Retries and handled failures never mix attempts.** A source node whose
49//!   policy allows retries (`retry.max_retries > 0`) or handles errors
50//!   (`on_error` other than `fail`) is *not* streamed live: its chunks are
51//!   buffered and emitted only once the node finishes. A `node_retry` event
52//!   discards the buffered chunks of the failed attempt. On success the final
53//!   attempt's text is emitted; after a handled `default-value` failure the
54//!   default output is emitted instead of any partial text; after a
55//!   `fail-branch` failure (or an unhandled one) nothing is emitted for the
56//!   variable. The trade-off: such nodes lose token-by-token streaming in
57//!   exchange for an answer that always matches the node's final output.
58//! - Ordering after handled failures follows the engine: a non-branch node with
59//!   `default-value` continues along its regular edges, a `fail-branch` node
60//!   along its `fail-branch` edges, and a branch node with `default-value`
61//!   along the handle named by its `branch` default value (no edge if absent).
62//! - A terminal run event (`RunSucceeded`, `RunFailed`, `RunAborted`) flushes
63//!   everything that can still be rendered, as does [`ResponseAssembler::finish`].
64
65use 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/// A piece of ordered response text.
80#[derive(Clone, Debug, PartialEq, Eq)]
81pub struct ResponseChunk {
82    /// Response node the text belongs to (or, for passed-through chunks, the
83    /// node that produced them).
84    pub node_id: String,
85    pub text: String,
86    /// `true` exactly once per response node, on an empty chunk marking that
87    /// its template is fully rendered.
88    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    /// Failed but kept going through its error strategy.
107    Handled,
108    /// Failed without an error strategy: outputs never appear.
109    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    /// Byte offset already emitted from the current variable's stream buffer.
138    offset: usize,
139}
140
141#[derive(Deserialize)]
142struct EndOutput {
143    value_selector: Selector,
144}
145
146/// Turns raw run events into ordered response text. See the module docs.
147#[derive(Debug)]
148pub struct ResponseAssembler {
149    graph: Arc<Graph>,
150    responses: Vec<Response>,
151    status: Vec<Status>,
152    branch: Vec<Option<String>>,
153    /// Nodes whose streams are buffered until they finish (retry / handled-error policy).
154    deferred: Vec<bool>,
155    /// Edge states restored from a snapshot (indexed like `Graph::edges`).
156    edge_state: Vec<Option<EdgeStatus>>,
157    /// Response nodes fully rendered before a pause (restored via `resume_from`).
158    rendered: Vec<bool>,
159    /// Stream buffers keyed by `(node id, field)`, only for referenced selectors.
160    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    /// Certainty of each node, maintained incrementally between events.
170    certain: Vec<bool>,
171    /// Whether `certain` reflects the current state (false after construction / resume).
172    certain_valid: bool,
173    /// Root nodes whose status changed since the last certainty refresh.
174    changed: Vec<NodeIx>,
175}
176
177impl ResponseAssembler {
178    /// Build an assembler for runs of `graph`.
179    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    /// Forward chunks that no template references (and child-frame chunks) verbatim.
210    pub fn pass_through_unreferenced(mut self, enabled: bool) -> Self {
211        self.pass_through = enabled;
212        self
213    }
214
215    /// Also render `end` nodes (their outputs joined by newlines).
216    pub fn include_end(mut self, enabled: bool) -> Self {
217        self.include_end = enabled;
218        self.rebuild_responses();
219        self
220    }
221
222    /// Provide a value for a non-node variable such as `sys.query`.
223    pub fn with_value(mut self, selector: &Selector, value: Value) -> Self {
224        self.pool.set(selector, value);
225        self
226    }
227
228    /// Provide non-node variables in bulk (e.g. the run's `sys` / `conversation` values).
229    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    /// Continue assembling a resumed run from the root frame snapshot carried by
237    /// `RunPaused` (`snapshot.root`).
238    ///
239    /// Restores every variable value of the snapshot (node outputs included, so
240    /// templates can reference nodes that will not run again), node and edge
241    /// states (for correct ordering), and marks response nodes that already
242    /// succeeded before the pause as rendered, so they are not emitted twice.
243    /// A response that was only partially rendered when the run paused is
244    /// rendered again from the start.
245    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    /// All response text emitted so far, concatenated.
282    pub fn text_so_far(&self) -> &str {
283        &self.emitted
284    }
285
286    /// Feed one event; returns the response chunks it unlocks.
287    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                // The failed attempt's partial output must never reach the answer.
324                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    /// Flush: treat every pending variable as resolved and render what is
339    /// available. Idempotent.
340    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                // Never downgrade a finished node back to "started" (retries reuse the id).
394                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    /// Whether `ix` is certain to run, given the certainty of its predecessors.
409    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    /// Bring the certain set up to date and enqueue newly certain responses.
418    ///
419    /// Certainty only grows as a run progresses, so after one full pass each
420    /// status change is propagated forward from the changed node: O(affected
421    /// nodes) per event instead of O(graph). A node losing certainty (not
422    /// expected in practice) falls back to a full recomputation.
423    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                // Even when `ix` itself was already certain, its outgoing edges may
447                // have become certain (e.g. a branch picked its handle).
448                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    /// Whether an incoming edge of `ix` will be taken given its (certain) source.
484    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            // States restored from a snapshot are authoritative.
490            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                // Unfinished fail-branch node: which side it takes is not known yet.
510                _ if fail_branch => false,
511                _ => edge.handle.as_ref() != FAIL_BRANCH,
512            }
513        })
514    }
515
516    /// Render as much as possible. With `flush`, pending variables are treated as resolved.
517    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            // A response node that ended up skipped renders nothing.
531            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
585/// Read-only view of the state a template variable is resolved from.
586struct 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        // Not produced by a root graph node: host-provided value (or nothing).
599        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        // Emitted only once the node has finished, from its final attempt.
609        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            // Default-value outputs (or nothing on fail-branch, whose outputs are only error fields).
615            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    // Live streaming: emit whatever was buffered beyond `offset`.
622    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
654/// Handle named by a `branch` entry of a `default-value` strategy.
655fn 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}