pub struct AgentContext<T> {
pub llm: LlmClient,
/* private fields */
}Expand description
Context provided to agent handlers.
This gives agents access to LLM inference and the ability to emit results.
Fields§
§llm: LlmClientLLM client for inference calls.
Implementations§
Source§impl<T> AgentContext<T>
impl<T> AgentContext<T>
Sourcepub fn set_role(&mut self, role: impl Into<String>)
pub fn set_role(&mut self, role: impl Into<String>)
Set the role this agent plays in protocols.
Sourcepub fn session_registry(&self) -> &SharedSessionRegistry
pub fn session_registry(&self) -> &SharedSessionRegistry
Get the session registry.
Sourcepub fn emit(&mut self, value: T) -> SageResult<T>where
T: Clone,
pub fn emit(&mut self, value: T) -> SageResult<T>where
T: Clone,
Emit a value to the awaiter.
This should be called once at the end of the agent’s execution. Calling emit multiple times is a no-op after the first call.
Sourcepub async fn infer<R>(&self, prompt: &str) -> SageResult<R>where
R: DeserializeOwned,
pub async fn infer<R>(&self, prompt: &str) -> SageResult<R>where
R: DeserializeOwned,
Call the LLM with a prompt and parse the response.
Sourcepub async fn infer_string(&self, prompt: &str) -> SageResult<String>
pub async fn infer_string(&self, prompt: &str) -> SageResult<String>
Call the LLM with a prompt and return the raw string response.
Sourcepub async fn receive<M>(&mut self) -> SageResult<M>where
M: DeserializeOwned,
pub async fn receive<M>(&mut self) -> SageResult<M>where
M: DeserializeOwned,
Receive a message from the agent’s mailbox.
This blocks until a message is available. The message is deserialized into the specified type.
Sourcepub async fn receive_timeout<M>(
&mut self,
timeout: Duration,
) -> SageResult<Option<M>>where
M: DeserializeOwned,
pub async fn receive_timeout<M>(
&mut self,
timeout: Duration,
) -> SageResult<Option<M>>where
M: DeserializeOwned,
Receive a message with a timeout.
Returns None if the timeout expires before a message arrives.
Sourcepub async fn receive_raw(&mut self) -> SageResult<Message>
pub async fn receive_raw(&mut self) -> SageResult<Message>
Receive the raw message from the agent’s mailbox.
This blocks until a message is available. Returns the full Message including session context.
Sourcepub fn set_current_message(&mut self, msg: Message)
pub fn set_current_message(&mut self, msg: Message)
Set the current message context (for use in message handlers).
This is called by generated code when entering a message handler.
Sourcepub fn clear_current_message(&mut self)
pub fn clear_current_message(&mut self)
Clear the current message context (for use after message handlers).
Sourcepub async fn reply<M: Serialize>(&mut self, msg: M) -> SageResult<()>
pub async fn reply<M: Serialize>(&mut self, msg: M) -> SageResult<()>
Phase 3: Reply to the current message.
This sends a response back to the sender of the current message. Can only be called inside a message handler.
§Errors
Returns an error if called outside a message handler or if the current message has no sender handle.
Sourcepub async fn reply_with_protocol<M: Serialize>(
&mut self,
msg: M,
msg_type: &str,
role: &str,
) -> SageResult<()>
pub async fn reply_with_protocol<M: Serialize>( &mut self, msg: M, msg_type: &str, role: &str, ) -> SageResult<()>
Phase 3: Reply to the current message with protocol state validation.
This validates that the reply is allowed by the protocol state machine, transitions the state, and then sends the reply.
§Arguments
msg- The message to send backmsg_type- The type name of the message for protocol validationrole- The role this agent plays in the protocol
§Errors
Returns an error if:
- Called outside a message handler
- The current message has no sender handle
- The protocol state doesn’t allow this reply
Sourcepub async fn validate_protocol_receive(
&mut self,
msg_type: &str,
role: &str,
) -> SageResult<()>
pub async fn validate_protocol_receive( &mut self, msg_type: &str, role: &str, ) -> SageResult<()>
Phase 3: Validate incoming message against protocol state.
Call this after receiving a message to validate it against the protocol state machine and transition to the next state.
§Arguments
msg_type- The type name of the received messagerole- The role this agent plays in the protocol
§Errors
Returns an error if the message violates the protocol.
Sourcepub async fn start_session(
&self,
protocol: String,
role: String,
state: Box<dyn ProtocolStateMachine>,
partner: SenderHandle,
) -> SessionId
pub async fn start_session( &self, protocol: String, role: String, state: Box<dyn ProtocolStateMachine>, partner: SenderHandle, ) -> SessionId
Phase 3: Start a new protocol session.
Call this when initiating a protocol exchange with another agent.
§Arguments
protocol- The protocol namerole- The role this agent playsstate- The initial state machine for this protocolpartner- Handle to send messages to the partner
§Returns
The session ID for tracking this protocol session.
Sourcepub fn current_message(&self) -> Option<&Message>
pub fn current_message(&self) -> Option<&Message>
Get the current message being handled (if any).