AgentContext

Struct AgentContext 

Source
pub struct AgentContext {
    pub mesh_name: String,
    /* private fields */
}
Expand description

Runtime context provided to agents during lifecycle and message handling.

The context gives agents access to mesh information and utilities for interacting with the request/response system.

§Example

async fn on_message(&mut self, msg: Message, ctx: &mut AgentContext) -> Result<()> {
    println!("Running in mesh: {}", ctx.mesh_name);
    if let Some(id) = msg.correlation_id() {
        ctx.report_result(&id, "Done!".to_string());
    }
    Ok(())
}

Fields§

§mesh_name: String

The name of the mesh this agent is running in.

Implementations§

Source§

impl AgentContext

Source

pub fn new(mesh_name: String, request_queue: Option<Arc<RequestQueue>>) -> Self

Create a new agent context.

§Arguments
  • mesh_name - Name of the mesh
  • request_queue - Optional request queue for request/response pattern
Examples found in repository?
examples/llm_ollama.rs (line 39)
23async fn main() -> Result<()> {
24    println!("=== Ceylon Runtime - LLM Ollama Example ===\n");
25
26    // Create an LLM agent using Ollama with gemma3:latest model
27    // No API key is required for Ollama (local inference)
28    let mut agent = LlmAgent::builder("gemma_agent", "ollama::gemma3:latest")
29        .with_system_prompt(
30            "You are a helpful AI assistant. Be concise and informative in your responses.",
31        )
32        .with_temperature(0.7)
33        .with_max_tokens(1024)
34        .build()?;
35
36    println!("✓ LLM Agent created successfully with Ollama gemma3:latest\n");
37
38    // Create an agent context for the conversation
39    let mut ctx = AgentContext::new("gemma_demo_mesh".to_string(), None);
40
41    // Example 1: Simple greeting
42    println!("--- Example 1: Simple Greeting ---");
43    let prompt1 = "Hello! What are you capable of?";
44    println!("User: {}", prompt1);
45
46    match agent.send_message_and_get_response(prompt1, &mut ctx).await {
47        Ok(response) => {
48            println!("Assistant: {}\n", response);
49        }
50        Err(e) => {
51            eprintln!("Error: {}\n", e);
52            eprintln!("Make sure Ollama is running and gemma3:latest model is available.");
53            eprintln!("Pull the model with: ollama pull gemma3:latest");
54            return Err(e);
55        }
56    }
57
58    // Example 2: Technical question
59    println!("--- Example 2: Technical Question ---");
60    let prompt2 = "Explain what an AI agent is in 2-3 sentences.";
61    println!("User: {}", prompt2);
62
63    match agent.send_message_and_get_response(prompt2, &mut ctx).await {
64        Ok(response) => {
65            println!("Assistant: {}\n", response);
66        }
67        Err(e) => {
68            eprintln!("Error: {}\n", e);
69            return Err(e);
70        }
71    }
72
73    // Example 3: Creative task
74    println!("--- Example 3: Creative Task ---");
75    let prompt3 = "Write a haiku about programming.";
76    println!("User: {}", prompt3);
77
78    match agent.send_message_and_get_response(prompt3, &mut ctx).await {
79        Ok(response) => {
80            println!("Assistant: {}\n", response);
81        }
82        Err(e) => {
83            eprintln!("Error: {}\n", e);
84            return Err(e);
85        }
86    }
87
88    println!("=== Example completed successfully! ===");
89    Ok(())
90}
Source

pub fn report_result(&self, request_id: &str, response: String)

Report a result for a pending request.

Call this to send a response back to the requester when handling a message with a correlation ID.

Source

pub fn has_request_queue(&self) -> bool

Check if this context has a request queue available.

Auto Trait Implementations§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,