use crate::agent::consumer::AgentMessage;
use crate::agent::router::Router;
use crate::error::LaserError;
use crate::laser::Laser;
use crate::provenance::{AgentTopic, Provenance};
use crate::types::AgentId;
use laser_wire::agent::{AgentCard, CapabilityDescriptor};
use std::time::Duration;
impl Laser {
pub fn agent(&self, id: AgentId) -> AgentScope {
AgentScope {
laser: self.clone(),
id,
}
}
}
#[derive(Clone)]
pub struct AgentScope {
laser: Laser,
id: AgentId,
}
impl AgentScope {
pub async fn send(
&self,
topic: AgentTopic<'_>,
payload: impl Into<Vec<u8>>,
provenance: &Provenance,
) -> Result<(), LaserError> {
let mut provenance = provenance.clone();
provenance.agent = Some(self.id.clone());
self.laser.send_agent(topic, payload, &provenance).await
}
pub async fn ask(
&self,
request_topic: AgentTopic<'_>,
reply_topic: AgentTopic<'_>,
payload: impl Into<Vec<u8>>,
provenance: &Provenance,
timeout: Duration,
) -> Result<AgentMessage, LaserError> {
let mut provenance = provenance.clone();
provenance.agent = Some(self.id.clone());
self.laser
.request(request_topic, reply_topic, payload, &provenance, timeout)
.await
}
pub fn contract(&self, router: Router) -> crate::agent::contract::ContractBuilder<'_> {
self.laser.contract(router).from(self.id.clone())
}
pub async fn publish_card(&self, card: &AgentCard) -> Result<(), LaserError> {
self.laser.publish_card(self.id.clone(), card).await
}
pub async fn advertise(
&self,
listen_on: AgentTopic<'static>,
capabilities: Vec<CapabilityDescriptor>,
) -> Result<(), LaserError> {
crate::agent::builder::advertise(&self.laser, &self.id, &listen_on, capabilities).await
}
pub fn id(&self) -> &AgentId {
&self.id
}
}