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,
pub max_total_tokens: Option<u64>,
pub max_cost_micros: Option<u64>,
}
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,
max_total_tokens: None,
max_cost_micros: None,
}
}
}
#[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>,
pub max_total_tokens: Option<u64>,
pub max_cost_micros: Option<u64>,
}
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,
#[cfg(feature = "subagents")]
linked: Arc<IndexMap<String, LinkedWorkflow>>,
#[cfg(feature = "subagents")]
linked_fingerprint: Option<Arc<str>>,
}
#[cfg(feature = "subagents")]
#[derive(Clone, Debug)]
pub struct LinkedWorkflow {
graph: Arc<Graph>,
env: IndexMap<String, Value>,
services: Option<Arc<Services>>,
}
#[cfg(feature = "subagents")]
impl LinkedWorkflow {
pub fn new(graph: Arc<Graph>) -> Self {
Self { graph, env: IndexMap::new(), services: None }
}
pub fn with_env(mut self, env: IndexMap<String, Value>) -> Self {
self.env = env;
self
}
pub fn with_services(mut self, services: Arc<Services>) -> Self {
self.services = Some(services);
self
}
pub fn graph(&self) -> &Arc<Graph> {
&self.graph
}
pub fn env(&self) -> &IndexMap<String, Value> {
&self.env
}
pub fn services(&self) -> Option<&Arc<Services>> {
self.services.as_ref()
}
pub fn check(&self) -> Result<(), String> {
self.resolved_env()?;
frame::start_schema(&self.graph).map(|_| ())
}
pub(crate) fn resolved_env(&self) -> Result<Vec<(String, Value)>, String> {
root::resolve_decls(&self.graph.workflow().env, &self.env, true).map_err(|why| format!("environment: {why}"))
}
}
impl Engine {
pub fn new(graph: Arc<Graph>) -> Self {
Self {
graph,
services: Arc::new(Services::new()),
clock: default_clock(),
limits: Limits::default(),
#[cfg(feature = "subagents")]
linked: Arc::new(IndexMap::new()),
#[cfg(feature = "subagents")]
linked_fingerprint: None,
}
}
#[cfg(feature = "subagents")]
pub fn with_linked(mut self, linked: impl IntoIterator<Item = (String, LinkedWorkflow)>) -> Self {
self.linked = Arc::new(linked.into_iter().collect());
self.linked_fingerprint = (!self.linked.is_empty()).then(|| {
let mut linked: Vec<String> = self
.linked
.iter()
.map(|(name, link)| format!("{}:{name}={}", name.len(), link.graph.fingerprint()))
.collect();
linked.sort();
Arc::from(format!("{}+{}", self.graph.fingerprint(), linked.join(",")))
});
self
}
pub fn fingerprint(&self) -> &str {
#[cfg(feature = "subagents")]
if let Some(linked) = &self.linked_fingerprint {
return linked;
}
self.graph.fingerprint()
}
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 services(&self) -> &Arc<Services> {
&self.services
}
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,
})
}
}
fn external_reason(reason: String) -> PauseReason {
PauseReason { frame: Arc::from(""), node_id: None, kind: "external".to_owned(), data: Value::from(reason) }
}
#[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()
&& 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.pause_with(external_reason(reason.into()));
}
pub fn pause_with(&self, reason: PauseReason) {
self.control.request_pause(Some(reason));
}
pub fn preempt(&self, reason: impl Into<String>) {
self.preempt_with(Some(external_reason(reason.into())));
}
pub fn preempt_with(&self, reason: Option<PauseReason>) {
self.control.request_preempt(reason);
}
pub fn stop_requested(&self) -> bool {
self.control.is_stopped() || self.control.pause_requested()
}
pub fn update_variable(&self, selector: Selector, value: Value) {
self.control.push_update(selector, value);
}
}