Skip to main content

TPCPNode

Struct TPCPNode 

Source
pub struct TPCPNode {
    pub identity: AgentIdentity,
    pub memory: Arc<RwLock<LWWMap>>,
    pub dlq: Arc<DLQ>,
    /* private fields */
}
Expand description

Async TPCP node backed by tokio and tokio-tungstenite.

Fields§

§identity: AgentIdentity§memory: Arc<RwLock<LWWMap>>§dlq: Arc<DLQ>

Implementations§

Source§

impl TPCPNode

Source

pub fn new(identity: AgentIdentity) -> Self

Creates a new TPCPNode with the given identity and no signing key.

Examples found in repository?
examples/hello.rs (line 16)
7async fn main() {
8    // Server
9    let server_identity = AgentIdentity {
10        agent_id: "server-001".into(),
11        framework: "hello-server".into(),
12        capabilities: vec![],
13        public_key: "".into(),
14        modality: vec!["text".into()],
15    };
16    let server = Arc::new(TPCPNode::new(server_identity));
17
18    server.register_handler(Intent::Handshake, |env| {
19        println!("Server received HANDSHAKE from {}", env.header.sender_id);
20        println!("Payload: {}", env.payload);
21    }).await;
22
23    server.listen("127.0.0.1:9001").await.expect("failed to listen");
24    sleep(Duration::from_millis(100)).await; // let server bind
25
26    // Client
27    let client_identity = AgentIdentity {
28        agent_id: "client-001".into(),
29        framework: "hello-client".into(),
30        capabilities: vec![],
31        public_key: "".into(),
32        modality: vec!["text".into()],
33    };
34    let client = TPCPNode::new(client_identity);
35
36    client.send_message(
37        "ws://127.0.0.1:9001",
38        "server-001",
39        Intent::Handshake,
40        serde_json::json!({"payload_type": "text", "content": "hello from Rust client"}),
41    ).await.expect("send failed");
42
43    sleep(Duration::from_millis(200)).await;
44    println!("TPCP Rust SDK hello example completed.");
45}
Source

pub fn with_signing_key(identity: AgentIdentity, key: SigningKey) -> Self

Creates a TPCPNode that signs all outbound messages.

Source

pub async fn register_peer_key(&self, agent_id: &str, public_key_b64: &str)

Registers a peer’s public key for inbound signature verification.

Source

pub async fn register_handler<F>(&self, intent: Intent, handler: F)
where F: Fn(TPCPEnvelope) + Send + Sync + 'static,

Registers a handler for a specific intent.

Examples found in repository?
examples/hello.rs (lines 18-21)
7async fn main() {
8    // Server
9    let server_identity = AgentIdentity {
10        agent_id: "server-001".into(),
11        framework: "hello-server".into(),
12        capabilities: vec![],
13        public_key: "".into(),
14        modality: vec!["text".into()],
15    };
16    let server = Arc::new(TPCPNode::new(server_identity));
17
18    server.register_handler(Intent::Handshake, |env| {
19        println!("Server received HANDSHAKE from {}", env.header.sender_id);
20        println!("Payload: {}", env.payload);
21    }).await;
22
23    server.listen("127.0.0.1:9001").await.expect("failed to listen");
24    sleep(Duration::from_millis(100)).await; // let server bind
25
26    // Client
27    let client_identity = AgentIdentity {
28        agent_id: "client-001".into(),
29        framework: "hello-client".into(),
30        capabilities: vec![],
31        public_key: "".into(),
32        modality: vec!["text".into()],
33    };
34    let client = TPCPNode::new(client_identity);
35
36    client.send_message(
37        "ws://127.0.0.1:9001",
38        "server-001",
39        Intent::Handshake,
40        serde_json::json!({"payload_type": "text", "content": "hello from Rust client"}),
41    ).await.expect("send failed");
42
43    sleep(Duration::from_millis(200)).await;
44    println!("TPCP Rust SDK hello example completed.");
45}
Source

pub async fn connect( &self, url: &str, ) -> Result<(), Box<dyn Error + Send + Sync>>

Connects as a WebSocket client to the given URL.

Source

pub async fn listen( &self, addr: &str, ) -> Result<(), Box<dyn Error + Send + Sync>>

Starts listening on the given address (e.g. “127.0.0.1:9001”).

Examples found in repository?
examples/hello.rs (line 23)
7async fn main() {
8    // Server
9    let server_identity = AgentIdentity {
10        agent_id: "server-001".into(),
11        framework: "hello-server".into(),
12        capabilities: vec![],
13        public_key: "".into(),
14        modality: vec!["text".into()],
15    };
16    let server = Arc::new(TPCPNode::new(server_identity));
17
18    server.register_handler(Intent::Handshake, |env| {
19        println!("Server received HANDSHAKE from {}", env.header.sender_id);
20        println!("Payload: {}", env.payload);
21    }).await;
22
23    server.listen("127.0.0.1:9001").await.expect("failed to listen");
24    sleep(Duration::from_millis(100)).await; // let server bind
25
26    // Client
27    let client_identity = AgentIdentity {
28        agent_id: "client-001".into(),
29        framework: "hello-client".into(),
30        capabilities: vec![],
31        public_key: "".into(),
32        modality: vec!["text".into()],
33    };
34    let client = TPCPNode::new(client_identity);
35
36    client.send_message(
37        "ws://127.0.0.1:9001",
38        "server-001",
39        Intent::Handshake,
40        serde_json::json!({"payload_type": "text", "content": "hello from Rust client"}),
41    ).await.expect("send failed");
42
43    sleep(Duration::from_millis(200)).await;
44    println!("TPCP Rust SDK hello example completed.");
45}
Source

pub async fn send_message( &self, url: &str, receiver_id: &str, intent: Intent, payload: Value, ) -> Result<(), Box<dyn Error + Send + Sync>>

Sends a message to the given WebSocket URL. receiver_id is the target agent’s agent_id (not the URL).

Examples found in repository?
examples/hello.rs (lines 36-41)
7async fn main() {
8    // Server
9    let server_identity = AgentIdentity {
10        agent_id: "server-001".into(),
11        framework: "hello-server".into(),
12        capabilities: vec![],
13        public_key: "".into(),
14        modality: vec!["text".into()],
15    };
16    let server = Arc::new(TPCPNode::new(server_identity));
17
18    server.register_handler(Intent::Handshake, |env| {
19        println!("Server received HANDSHAKE from {}", env.header.sender_id);
20        println!("Payload: {}", env.payload);
21    }).await;
22
23    server.listen("127.0.0.1:9001").await.expect("failed to listen");
24    sleep(Duration::from_millis(100)).await; // let server bind
25
26    // Client
27    let client_identity = AgentIdentity {
28        agent_id: "client-001".into(),
29        framework: "hello-client".into(),
30        capabilities: vec![],
31        public_key: "".into(),
32        modality: vec!["text".into()],
33    };
34    let client = TPCPNode::new(client_identity);
35
36    client.send_message(
37        "ws://127.0.0.1:9001",
38        "server-001",
39        Intent::Handshake,
40        serde_json::json!({"payload_type": "text", "content": "hello from Rust client"}),
41    ).await.expect("send failed");
42
43    sleep(Duration::from_millis(200)).await;
44    println!("TPCP Rust SDK hello example completed.");
45}

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, 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> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V