mod control;
mod frame;
mod ids;
mod root;
use std::collections::VecDeque;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, PoisonError};
use std::task::{Context, Poll};
use futures::future::BoxFuture;
use futures::task::AtomicWaker;
use futures::{FutureExt, Stream, StreamExt};
use indexmap::IndexMap;
use self::control::{Control, Stop};
use crate::event::{Event, PauseReason};
use crate::graph::Graph;
use crate::node::{Clock, EventSink, Services, default_clock};
use crate::pool::Selector;
use crate::snapshot::Snapshot;
use crate::value::Value;
#[cfg(doc)]
use crate::node::{NodeError, NodeOutput};
#[derive(Clone, Debug, PartialEq)]
pub struct Limits {
pub max_steps: u64,
pub max_duration_ms: Option<u64>,
pub max_depth: u32,
pub default_node_timeout_ms: Option<u64>,
pub max_concurrency: usize,
}
impl Default for Limits {
fn default() -> Self {
Self {
max_steps: 10_000,
max_duration_ms: None,
max_depth: 8,
default_node_timeout_ms: None,
max_concurrency: 64,
}
}
}
#[derive(Clone, Debug, Default)]
pub struct RunInput {
pub inputs: IndexMap<String, Value>,
pub sys: IndexMap<String, Value>,
pub env: IndexMap<String, Value>,
pub conversation: IndexMap<String, Value>,
pub run_id: Option<String>,
pub resume: Option<ResumeFrom>,
}
impl RunInput {
pub fn new() -> Self {
Self::default()
}
pub fn input(mut self, name: impl Into<String>, value: impl Into<Value>) -> Self {
self.inputs.insert(name.into(), value.into());
self
}
pub fn sys(mut self, name: impl Into<String>, value: impl Into<Value>) -> Self {
self.sys.insert(name.into(), value.into());
self
}
pub fn env(mut self, name: impl Into<String>, value: impl Into<Value>) -> Self {
self.env.insert(name.into(), value.into());
self
}
pub fn resume(mut self, snapshot: Snapshot, inputs: IndexMap<String, Value>) -> Self {
self.resume = Some(ResumeFrom::new(snapshot, inputs));
self
}
}
#[derive(Clone, Debug)]
pub struct ResumeFrom {
pub snapshot: Snapshot,
pub inputs: IndexMap<String, Value>,
pub secrets: IndexMap<String, Value>,
}
impl ResumeFrom {
pub fn new(snapshot: Snapshot, inputs: IndexMap<String, Value>) -> Self {
Self { snapshot, inputs, secrets: IndexMap::new() }
}
pub fn secret(mut self, selector: impl Into<String>, value: impl Into<String>) -> Self {
self.secrets.insert(selector.into(), Value::Secret(Arc::from(value.into())));
self
}
}
#[derive(Clone, Debug)]
pub struct Engine {
graph: Arc<Graph>,
services: Arc<Services>,
clock: Arc<dyn Clock>,
limits: Limits,
}
impl Engine {
pub fn new(graph: Arc<Graph>) -> Self {
Self { graph, services: Arc::new(Services::new()), clock: default_clock(), limits: Limits::default() }
}
pub fn with_services(mut self, services: Services) -> Self {
self.services = Arc::new(services);
self
}
pub fn with_shared_services(mut self, services: Arc<Services>) -> Self {
self.services = services;
self
}
pub fn with_clock(mut self, clock: Arc<dyn Clock>) -> Self {
self.clock = clock;
self
}
pub fn with_limits(mut self, limits: Limits) -> Self {
self.limits = limits;
self
}
pub fn graph(&self) -> &Arc<Graph> {
&self.graph
}
pub fn limits(&self) -> &Limits {
&self.limits
}
pub fn run(&self, input: RunInput) -> RunStream {
let queue = Arc::new(EventQueue::default());
let control = Arc::new(Control::default());
let sink_queue = Arc::clone(&queue);
let sink: EventSink = Arc::new(move |event| sink_queue.push(event));
let driver = root::run_root(self.clone(), input, control.clone(), sink).boxed();
RunStream { driver: Some(driver), queue, finished: false, handle: RunHandle { control } }
}
pub async fn run_to_end(&self, input: RunInput) -> Event {
let mut stream = self.run(input);
let mut last = None;
while let Some(event) = stream.next().await {
last = Some(event);
}
last.unwrap_or(Event::RunFailed {
error: "run produced no events".to_owned(),
error_type: "internal".to_owned(),
usage: Default::default(),
elapsed_ms: 0,
})
}
}
#[derive(Default)]
struct EventQueue {
events: Mutex<VecDeque<Event>>,
waker: AtomicWaker,
closed: AtomicBool,
}
impl EventQueue {
fn push(&self, event: Event) {
if self.closed.load(Ordering::Acquire) {
return;
}
self.events.lock().unwrap_or_else(PoisonError::into_inner).push_back(event);
self.waker.wake();
}
fn pop(&self) -> Option<Event> {
self.events.lock().unwrap_or_else(PoisonError::into_inner).pop_front()
}
}
pub struct RunStream {
driver: Option<BoxFuture<'static, ()>>,
queue: Arc<EventQueue>,
finished: bool,
handle: RunHandle,
}
impl Drop for RunStream {
fn drop(&mut self) {
self.queue.closed.store(true, Ordering::Release);
}
}
impl std::fmt::Debug for RunStream {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RunStream").field("finished", &self.finished).finish_non_exhaustive()
}
}
impl RunStream {
pub fn handle(&self) -> RunHandle {
self.handle.clone()
}
}
impl Stream for RunStream {
type Item = Event;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Event>> {
if self.finished {
return Poll::Ready(None);
}
if let Some(driver) = self.driver.as_mut() {
if driver.as_mut().poll(cx).is_ready() {
self.driver = None;
}
}
let event = match self.queue.pop() {
Some(event) => Some(event),
None if self.driver.is_none() => None,
None => {
self.queue.waker.register(cx.waker());
match self.queue.pop() {
Some(event) => Some(event),
None => return Poll::Pending,
}
}
};
match event {
Some(event) => {
if event.is_terminal() {
self.finished = true;
}
Poll::Ready(Some(event))
}
None => {
self.finished = true;
Poll::Ready(None)
}
}
}
}
#[derive(Clone, Debug)]
pub struct RunHandle {
control: Arc<Control>,
}
impl RunHandle {
pub fn abort(&self, reason: impl Into<String>) {
self.control.stop(Stop::Abort(reason.into()));
}
pub fn pause(&self, reason: impl Into<String>) {
self.control.request_pause(Some(PauseReason {
frame: Arc::from(""),
node_id: None,
kind: "external".to_owned(),
data: Value::from(reason.into()),
}));
}
pub fn update_variable(&self, selector: Selector, value: Value) {
self.control.push_update(selector, value);
}
}