Skip to main content

YoAgentRuntime

Struct YoAgentRuntime 

Source
pub struct YoAgentRuntime<S: EventStore> { /* private fields */ }

Implementations§

Source§

impl<S: EventStore> YoAgentRuntime<S>

Source

pub fn new(state: YoAgentState<S>) -> Self

Examples found in repository?
examples/policy_approval.rs (line 10)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let state = YoAgentState::load(MemoryEventStore::new()).await?;
10    let mut runtime = YoAgentRuntime::new(state.clone());
11    runtime.register_policy(Policy::require_approval(
12        PolicyId::new("policy_create_node_review"),
13        "Creating graph nodes requires review",
14        PolicyAction::CreateNode,
15    ));
16
17    let result = runtime
18        .create_typed_node(
19            ActorRef::agent("demo"),
20            NodeId::new("sensitive_node"),
21            "memory",
22            json!({ "title": "Potentially sensitive memory" }),
23        )
24        .await;
25
26    match result {
27        Err(StateError::PolicyDenied(message)) => println!("blocked: {message}"),
28        other => println!("unexpected: {other:?}"),
29    }
30
31    let approvals = state
32        .graph()
33        .await
34        .nodes
35        .values()
36        .filter(|node| node.kind == "approval_request")
37        .count();
38    println!("approval_requests={approvals}");
39    Ok(())
40}
More examples
Hide additional examples
examples/typed_pack.rs (line 10)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let state = YoAgentState::load(MemoryEventStore::new()).await?;
10    let mut runtime = YoAgentRuntime::new(state.clone());
11    runtime.register_pack(
12        Pack::new(PackId::new("pack_lineage"), "lineage", "0.1.0")
13            .add_object_type(ObjectType::new("goal").require("title"))
14            .add_object_type(ObjectType::new("task").require("title"))
15            .add_relation_type(
16                RelationType::new("serves")
17                    .from_kind("task")
18                    .to_kind("goal"),
19            ),
20    );
21
22    runtime
23        .create_typed_node(
24            ActorRef::agent("demo"),
25            NodeId::new("goal_1"),
26            "goal",
27            json!({ "title": "Improve retry reliability" }),
28        )
29        .await?;
30    runtime
31        .create_typed_node(
32            ActorRef::agent("demo"),
33            NodeId::new("task_1"),
34            "task",
35            json!({ "title": "Investigate timeout" }),
36        )
37        .await?;
38    runtime
39        .create_typed_relation(
40            ActorRef::agent("demo"),
41            NodeId::new("task_1"),
42            "serves",
43            NodeId::new("goal_1"),
44            json!({}),
45        )
46        .await?;
47
48    println!("{}", serde_json::to_string_pretty(&state.graph().await)?);
49    Ok(())
50}
examples/behavior_subscription.rs (line 10)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let state = YoAgentState::load(MemoryEventStore::new()).await?;
10    let mut runtime = YoAgentRuntime::new(state.clone());
11
12    runtime.register_behavior(FnBehavior::new(
13        BehaviorId::new("behavior_failure_to_task"),
14        EventPattern::Kind("failure.observed".to_string()),
15        |_ctx: BehaviorContext, event: Event| async move {
16            let title = event
17                .payload
18                .get("title")
19                .and_then(|value| value.as_str())
20                .unwrap_or("Investigate failure");
21            Ok(vec![StateOp::CreateNode {
22                id: NodeId::new("task_from_failure"),
23                kind: "task".to_string(),
24                props: json!({
25                    "title": format!("Investigate: {title}"),
26                    "status": "Open",
27                    "created_by_behavior": "behavior_failure_to_task",
28                }),
29            }])
30        },
31    ));
32
33    runtime
34        .emit_event(Event::new(
35            ActorRef::agent("demo"),
36            "failure.observed",
37            json!({ "title": "retry timeout loses state" }),
38        ))
39        .await?;
40
41    print!(
42        "{}",
43        state
44            .lineage(NodeId::new("task_from_failure"))
45            .await
46            .to_markdown()
47    );
48    Ok(())
49}
Source

pub fn state(&self) -> YoAgentState<S>

Source

pub fn register_pack(&mut self, pack: Pack)

Examples found in repository?
examples/typed_pack.rs (lines 11-20)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let state = YoAgentState::load(MemoryEventStore::new()).await?;
10    let mut runtime = YoAgentRuntime::new(state.clone());
11    runtime.register_pack(
12        Pack::new(PackId::new("pack_lineage"), "lineage", "0.1.0")
13            .add_object_type(ObjectType::new("goal").require("title"))
14            .add_object_type(ObjectType::new("task").require("title"))
15            .add_relation_type(
16                RelationType::new("serves")
17                    .from_kind("task")
18                    .to_kind("goal"),
19            ),
20    );
21
22    runtime
23        .create_typed_node(
24            ActorRef::agent("demo"),
25            NodeId::new("goal_1"),
26            "goal",
27            json!({ "title": "Improve retry reliability" }),
28        )
29        .await?;
30    runtime
31        .create_typed_node(
32            ActorRef::agent("demo"),
33            NodeId::new("task_1"),
34            "task",
35            json!({ "title": "Investigate timeout" }),
36        )
37        .await?;
38    runtime
39        .create_typed_relation(
40            ActorRef::agent("demo"),
41            NodeId::new("task_1"),
42            "serves",
43            NodeId::new("goal_1"),
44            json!({}),
45        )
46        .await?;
47
48    println!("{}", serde_json::to_string_pretty(&state.graph().await)?);
49    Ok(())
50}
Source

pub fn register_behavior<B>(&mut self, behavior: B)
where B: Behavior + 'static,

Examples found in repository?
examples/behavior_subscription.rs (lines 12-31)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let state = YoAgentState::load(MemoryEventStore::new()).await?;
10    let mut runtime = YoAgentRuntime::new(state.clone());
11
12    runtime.register_behavior(FnBehavior::new(
13        BehaviorId::new("behavior_failure_to_task"),
14        EventPattern::Kind("failure.observed".to_string()),
15        |_ctx: BehaviorContext, event: Event| async move {
16            let title = event
17                .payload
18                .get("title")
19                .and_then(|value| value.as_str())
20                .unwrap_or("Investigate failure");
21            Ok(vec![StateOp::CreateNode {
22                id: NodeId::new("task_from_failure"),
23                kind: "task".to_string(),
24                props: json!({
25                    "title": format!("Investigate: {title}"),
26                    "status": "Open",
27                    "created_by_behavior": "behavior_failure_to_task",
28                }),
29            }])
30        },
31    ));
32
33    runtime
34        .emit_event(Event::new(
35            ActorRef::agent("demo"),
36            "failure.observed",
37            json!({ "title": "retry timeout loses state" }),
38        ))
39        .await?;
40
41    print!(
42        "{}",
43        state
44            .lineage(NodeId::new("task_from_failure"))
45            .await
46            .to_markdown()
47    );
48    Ok(())
49}
Source

pub fn register_policy(&mut self, policy: Policy)

Examples found in repository?
examples/policy_approval.rs (lines 11-15)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let state = YoAgentState::load(MemoryEventStore::new()).await?;
10    let mut runtime = YoAgentRuntime::new(state.clone());
11    runtime.register_policy(Policy::require_approval(
12        PolicyId::new("policy_create_node_review"),
13        "Creating graph nodes requires review",
14        PolicyAction::CreateNode,
15    ));
16
17    let result = runtime
18        .create_typed_node(
19            ActorRef::agent("demo"),
20            NodeId::new("sensitive_node"),
21            "memory",
22            json!({ "title": "Potentially sensitive memory" }),
23        )
24        .await;
25
26    match result {
27        Err(StateError::PolicyDenied(message)) => println!("blocked: {message}"),
28        other => println!("unexpected: {other:?}"),
29    }
30
31    let approvals = state
32        .graph()
33        .await
34        .nodes
35        .values()
36        .filter(|node| node.kind == "approval_request")
37        .count();
38    println!("approval_requests={approvals}");
39    Ok(())
40}
Source

pub async fn emit_event(&self, event: Event) -> Result<EventId, StateError>

Examples found in repository?
examples/behavior_subscription.rs (lines 34-38)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let state = YoAgentState::load(MemoryEventStore::new()).await?;
10    let mut runtime = YoAgentRuntime::new(state.clone());
11
12    runtime.register_behavior(FnBehavior::new(
13        BehaviorId::new("behavior_failure_to_task"),
14        EventPattern::Kind("failure.observed".to_string()),
15        |_ctx: BehaviorContext, event: Event| async move {
16            let title = event
17                .payload
18                .get("title")
19                .and_then(|value| value.as_str())
20                .unwrap_or("Investigate failure");
21            Ok(vec![StateOp::CreateNode {
22                id: NodeId::new("task_from_failure"),
23                kind: "task".to_string(),
24                props: json!({
25                    "title": format!("Investigate: {title}"),
26                    "status": "Open",
27                    "created_by_behavior": "behavior_failure_to_task",
28                }),
29            }])
30        },
31    ));
32
33    runtime
34        .emit_event(Event::new(
35            ActorRef::agent("demo"),
36            "failure.observed",
37            json!({ "title": "retry timeout loses state" }),
38        ))
39        .await?;
40
41    print!(
42        "{}",
43        state
44            .lineage(NodeId::new("task_from_failure"))
45            .await
46            .to_markdown()
47    );
48    Ok(())
49}
Source

pub async fn create_typed_node( &self, actor: ActorRef, id: NodeId, kind: impl Into<String>, props: Value, ) -> Result<EventId, StateError>

Examples found in repository?
examples/policy_approval.rs (lines 18-23)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let state = YoAgentState::load(MemoryEventStore::new()).await?;
10    let mut runtime = YoAgentRuntime::new(state.clone());
11    runtime.register_policy(Policy::require_approval(
12        PolicyId::new("policy_create_node_review"),
13        "Creating graph nodes requires review",
14        PolicyAction::CreateNode,
15    ));
16
17    let result = runtime
18        .create_typed_node(
19            ActorRef::agent("demo"),
20            NodeId::new("sensitive_node"),
21            "memory",
22            json!({ "title": "Potentially sensitive memory" }),
23        )
24        .await;
25
26    match result {
27        Err(StateError::PolicyDenied(message)) => println!("blocked: {message}"),
28        other => println!("unexpected: {other:?}"),
29    }
30
31    let approvals = state
32        .graph()
33        .await
34        .nodes
35        .values()
36        .filter(|node| node.kind == "approval_request")
37        .count();
38    println!("approval_requests={approvals}");
39    Ok(())
40}
More examples
Hide additional examples
examples/typed_pack.rs (lines 23-28)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let state = YoAgentState::load(MemoryEventStore::new()).await?;
10    let mut runtime = YoAgentRuntime::new(state.clone());
11    runtime.register_pack(
12        Pack::new(PackId::new("pack_lineage"), "lineage", "0.1.0")
13            .add_object_type(ObjectType::new("goal").require("title"))
14            .add_object_type(ObjectType::new("task").require("title"))
15            .add_relation_type(
16                RelationType::new("serves")
17                    .from_kind("task")
18                    .to_kind("goal"),
19            ),
20    );
21
22    runtime
23        .create_typed_node(
24            ActorRef::agent("demo"),
25            NodeId::new("goal_1"),
26            "goal",
27            json!({ "title": "Improve retry reliability" }),
28        )
29        .await?;
30    runtime
31        .create_typed_node(
32            ActorRef::agent("demo"),
33            NodeId::new("task_1"),
34            "task",
35            json!({ "title": "Investigate timeout" }),
36        )
37        .await?;
38    runtime
39        .create_typed_relation(
40            ActorRef::agent("demo"),
41            NodeId::new("task_1"),
42            "serves",
43            NodeId::new("goal_1"),
44            json!({}),
45        )
46        .await?;
47
48    println!("{}", serde_json::to_string_pretty(&state.graph().await)?);
49    Ok(())
50}
Source

pub async fn create_typed_relation( &self, actor: ActorRef, from: NodeId, rel: impl Into<String>, to: NodeId, props: Value, ) -> Result<EventId, StateError>

Examples found in repository?
examples/typed_pack.rs (lines 39-45)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let state = YoAgentState::load(MemoryEventStore::new()).await?;
10    let mut runtime = YoAgentRuntime::new(state.clone());
11    runtime.register_pack(
12        Pack::new(PackId::new("pack_lineage"), "lineage", "0.1.0")
13            .add_object_type(ObjectType::new("goal").require("title"))
14            .add_object_type(ObjectType::new("task").require("title"))
15            .add_relation_type(
16                RelationType::new("serves")
17                    .from_kind("task")
18                    .to_kind("goal"),
19            ),
20    );
21
22    runtime
23        .create_typed_node(
24            ActorRef::agent("demo"),
25            NodeId::new("goal_1"),
26            "goal",
27            json!({ "title": "Improve retry reliability" }),
28        )
29        .await?;
30    runtime
31        .create_typed_node(
32            ActorRef::agent("demo"),
33            NodeId::new("task_1"),
34            "task",
35            json!({ "title": "Investigate timeout" }),
36        )
37        .await?;
38    runtime
39        .create_typed_relation(
40            ActorRef::agent("demo"),
41            NodeId::new("task_1"),
42            "serves",
43            NodeId::new("goal_1"),
44            json!({}),
45        )
46        .await?;
47
48    println!("{}", serde_json::to_string_pretty(&state.graph().await)?);
49    Ok(())
50}
Source

pub async fn request_approval( &self, policy_id: PolicyId, action: PolicyAction, target: Option<NodeId>, requested_by: ActorRef, reason: Option<String>, ) -> Result<EventId, StateError>

Source

pub async fn approve_request( &self, actor: ActorRef, request_id: NodeId, reason: Option<String>, ) -> Result<EventId, StateError>

Source

pub async fn reject_request( &self, actor: ActorRef, request_id: NodeId, reason: Option<String>, ) -> Result<EventId, StateError>

Source

pub async fn graph(&self) -> GraphSnapshot

Auto Trait Implementations§

§

impl<S> !RefUnwindSafe for YoAgentRuntime<S>

§

impl<S> !UnwindSafe for YoAgentRuntime<S>

§

impl<S> Freeze for YoAgentRuntime<S>

§

impl<S> Send for YoAgentRuntime<S>

§

impl<S> Sync for YoAgentRuntime<S>

§

impl<S> Unpin for YoAgentRuntime<S>

§

impl<S> UnsafeUnpin for YoAgentRuntime<S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.