1use crate::agent::consumer::AgentMessage;
2use crate::agent::router::Router;
3use crate::error::LaserError;
4use crate::laser::Laser;
5use crate::provenance::{AgentTopic, Provenance};
6use crate::types::AgentId;
7use laser_wire::agent::{AgentCard, CapabilityDescriptor};
8use std::time::Duration;
9
10impl Laser {
11 pub fn agent(&self, id: AgentId) -> AgentScope {
16 AgentScope {
17 laser: self.clone(),
18 id,
19 }
20 }
21}
22
23#[derive(Clone)]
29pub struct AgentScope {
30 laser: Laser,
31 id: AgentId,
32}
33
34impl AgentScope {
35 pub async fn send(
39 &self,
40 topic: AgentTopic<'_>,
41 payload: impl Into<Vec<u8>>,
42 provenance: &Provenance,
43 ) -> Result<(), LaserError> {
44 let mut provenance = provenance.clone();
45 provenance.agent = Some(self.id.clone());
46 self.laser.send_agent(topic, payload, &provenance).await
47 }
48
49 pub async fn ask(
52 &self,
53 request_topic: AgentTopic<'_>,
54 reply_topic: AgentTopic<'_>,
55 payload: impl Into<Vec<u8>>,
56 provenance: &Provenance,
57 timeout: Duration,
58 ) -> Result<AgentMessage, LaserError> {
59 let mut provenance = provenance.clone();
60 provenance.agent = Some(self.id.clone());
61 self.laser
62 .request(request_topic, reply_topic, payload, &provenance, timeout)
63 .await
64 }
65
66 pub fn contract(&self, router: Router) -> crate::agent::contract::ContractBuilder<'_> {
69 self.laser.contract(router).from(self.id.clone())
70 }
71
72 pub async fn publish_card(&self, card: &AgentCard) -> Result<(), LaserError> {
75 self.laser.publish_card(self.id.clone(), card).await
76 }
77
78 pub async fn advertise(
83 &self,
84 listen_on: AgentTopic<'static>,
85 capabilities: Vec<CapabilityDescriptor>,
86 ) -> Result<(), LaserError> {
87 crate::agent::builder::advertise(&self.laser, &self.id, &listen_on, capabilities).await
88 }
89
90 pub fn id(&self) -> &AgentId {
92 &self.id
93 }
94}