Trait Agent

Source
pub trait Agent {
    // Required methods
    fn get_agent_did(&self) -> &str;
    fn get_service_endpoint<'life0, 'life1, 'async_trait>(
        &'life0 self,
        to: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<Option<String>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn send_message<'life0, 'life1, 'life2, 'async_trait, T>(
        &'life0 self,
        message: &'life1 T,
        to: Vec<&'life2 str>,
        deliver: bool,
    ) -> Pin<Box<dyn Future<Output = Result<(String, Vec<DeliveryResult>)>> + Send + 'async_trait>>
       where T: 'async_trait + TapMessageBody + Serialize + Send + Sync + Debug + 'static,
             Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn receive_encrypted_message<'life0, 'life1, 'async_trait>(
        &'life0 self,
        jwe_value: &'life1 Value,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn receive_plain_message<'life0, 'async_trait>(
        &'life0 self,
        message: PlainMessage,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn receive_message<'life0, 'life1, 'async_trait>(
        &'life0 self,
        raw_message: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<PlainMessage>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided methods
    fn send_typed<'life0, 'async_trait, T>(
        &'life0 self,
        message: PlainMessage<T>,
        deliver: bool,
    ) -> Pin<Box<dyn Future<Output = Result<(String, Vec<DeliveryResult>)>> + Send + 'async_trait>>
       where T: 'async_trait + TapMessageBody + Send + Sync + Debug + 'static,
             Self: Sync + 'async_trait,
             'life0: 'async_trait { ... }
    fn send_with_context<'life0, 'life1, 'async_trait, T>(
        &'life0 self,
        message: &'life1 T,
        deliver: bool,
    ) -> Pin<Box<dyn Future<Output = Result<(String, Vec<DeliveryResult>)>> + Send + 'async_trait>>
       where T: TapMessageBody + MessageContext + Send + Sync + Debug + 'static + 'async_trait,
             Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn send_typed_with_context<'life0, 'async_trait, T>(
        &'life0 self,
        message: PlainMessage<T>,
        deliver: bool,
    ) -> Pin<Box<dyn Future<Output = Result<(String, Vec<DeliveryResult>)>> + Send + 'async_trait>>
       where T: TapMessageBody + MessageContext + Send + Sync + Debug + 'static + 'async_trait,
             Self: Sync + 'async_trait,
             'life0: 'async_trait { ... }
    fn receive_typed<'life0, 'life1, 'async_trait, T>(
        &'life0 self,
        raw_message: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<PlainMessage<T>>> + Send + 'async_trait>>
       where T: 'async_trait + TapMessageBody,
             Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

The Agent trait defines the interface for all TAP agents

This trait supports both standalone agent usage and integration with TAP Node. The different receive methods are designed for different usage patterns:

§Usage Patterns

§Node Integration

  • [receive_encrypted_message]: Called by TAP Node for encrypted messages
  • [receive_plain_message]: Called by TAP Node for verified/decrypted messages

§Standalone Usage

  • [receive_message]: Handles any message type (plain, signed, encrypted)

§Message Sending

  • [send_message]: Sends messages to recipients with optional delivery

§Examples

use tap_agent::{Agent, TapAgent};
use tap_msg::didcomm::PlainMessage;

async fn process_encrypted_message(agent: &TapAgent, jwe_json: &serde_json::Value) {
    // This would typically be called by TAP Node
    if let Err(e) = agent.receive_encrypted_message(jwe_json).await {
        eprintln!("Failed to process encrypted message: {}", e);
    }
}

Required Methods§

Source

fn get_agent_did(&self) -> &str

Gets the agent’s DID

Source

fn get_service_endpoint<'life0, 'life1, 'async_trait>( &'life0 self, to: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Option<String>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Gets the service endpoint URL for a recipient

This method resolves how to reach a given recipient, which could be:

  • A direct URL if to is already a URL
  • A DID resolution if to is a DID
Source

fn send_message<'life0, 'life1, 'life2, 'async_trait, T>( &'life0 self, message: &'life1 T, to: Vec<&'life2 str>, deliver: bool, ) -> Pin<Box<dyn Future<Output = Result<(String, Vec<DeliveryResult>)>> + Send + 'async_trait>>
where T: 'async_trait + TapMessageBody + Serialize + Send + Sync + Debug + 'static, Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Sends a message to one or more recipients

§Parameters
  • message: The message to send (must implement TapMessageBody)
  • to: List of recipient DIDs or URLs
  • deliver: Whether to actually deliver the message or just pack it
§Returns
  • Packed message string
  • Vector of delivery results (empty if deliver=false)
Source

fn receive_encrypted_message<'life0, 'life1, 'async_trait>( &'life0 self, jwe_value: &'life1 Value, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Receives an encrypted message (decrypt and process)

This method is typically called by TAP Node when routing encrypted messages to agents. The agent should:

  1. Parse the JWE from the JSON value
  2. Attempt to decrypt using its private keys
  3. Process the resulting PlainMessage
§Parameters
  • jwe_value: JSON representation of the encrypted message (JWE)
Source

fn receive_plain_message<'life0, 'async_trait>( &'life0 self, message: PlainMessage, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Receives a plain message (already verified/decrypted)

This method is called by TAP Node after signature verification or by other agents after decryption. The message is ready for business logic processing.

§Parameters
  • message: The verified/decrypted PlainMessage
Source

fn receive_message<'life0, 'life1, 'async_trait>( &'life0 self, raw_message: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<PlainMessage>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Receives a raw message (for standalone usage - handles any message type)

This method handles the complete message processing pipeline for standalone agent usage. It can process:

  • Plain messages (passed through)
  • Signed messages (signature verified)
  • Encrypted messages (decrypted)
§Parameters
  • raw_message: JSON string of any message type
§Returns
  • The processed PlainMessage

Provided Methods§

Source

fn send_typed<'life0, 'async_trait, T>( &'life0 self, message: PlainMessage<T>, deliver: bool, ) -> Pin<Box<dyn Future<Output = Result<(String, Vec<DeliveryResult>)>> + Send + 'async_trait>>
where T: 'async_trait + TapMessageBody + Send + Sync + Debug + 'static, Self: Sync + 'async_trait, 'life0: 'async_trait,

Send a strongly-typed message

§Parameters
  • message: The typed message to send
  • deliver: Whether to actually deliver the message
§Returns
  • Packed message string
  • Vector of delivery results (empty if deliver=false)
Source

fn send_with_context<'life0, 'life1, 'async_trait, T>( &'life0 self, message: &'life1 T, deliver: bool, ) -> Pin<Box<dyn Future<Output = Result<(String, Vec<DeliveryResult>)>> + Send + 'async_trait>>
where T: TapMessageBody + MessageContext + Send + Sync + Debug + 'static + 'async_trait, Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Send a message with MessageContext support for automatic routing

This method uses MessageContext to automatically extract participants and routing hints for improved message delivery.

§Parameters
  • message: The message body that implements both TapMessageBody and MessageContext
  • deliver: Whether to actually deliver the message
§Returns
  • Packed message string
  • Vector of delivery results (empty if deliver=false)
Source

fn send_typed_with_context<'life0, 'async_trait, T>( &'life0 self, message: PlainMessage<T>, deliver: bool, ) -> Pin<Box<dyn Future<Output = Result<(String, Vec<DeliveryResult>)>> + Send + 'async_trait>>
where T: TapMessageBody + MessageContext + Send + Sync + Debug + 'static + 'async_trait, Self: Sync + 'async_trait, 'life0: 'async_trait,

Send a typed message with automatic context routing

§Parameters
  • message: The typed message with MessageContext support
  • deliver: Whether to actually deliver the message
§Returns
  • Packed message string
  • Vector of delivery results (empty if deliver=false)
Source

fn receive_typed<'life0, 'life1, 'async_trait, T>( &'life0 self, raw_message: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<PlainMessage<T>>> + Send + 'async_trait>>
where T: 'async_trait + TapMessageBody, Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Receive and parse a typed message

§Parameters
  • raw_message: The raw message string
§Type Parameters
  • T: The expected message body type
§Returns
  • The typed message if parsing succeeds

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§