use adk_core::principal::{SubjectKind, SubjectRef};
use serde::{Deserialize, Serialize};
use crate::control::ControlTransport;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Presence {
#[default]
None,
PairedLocal,
Confirmed,
}
impl Presence {
pub fn is_present(self) -> bool {
!matches!(self, Self::None)
}
pub fn as_str(self) -> &'static str {
match self {
Self::None => "none",
Self::PairedLocal => "pairedLocal",
Self::Confirmed => "confirmed",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AgentCaller {
pub by: SubjectRef,
pub surface: String,
pub how: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub device: Option<String>,
#[serde(default)]
pub presence: Presence,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub transport: Option<ControlTransport>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub peer_pid: Option<u32>,
}
impl AgentCaller {
pub fn new(by: SubjectRef, surface: impl Into<String>, how: impl Into<String>) -> Self {
Self {
by,
surface: surface.into(),
how: how.into(),
device: None,
presence: Presence::None,
transport: None,
peer_pid: None,
}
}
#[must_use]
pub fn with_device(mut self, device: impl Into<String>) -> Self {
self.device = Some(device.into());
self
}
#[must_use]
pub fn with_presence(mut self, presence: Presence) -> Self {
self.presence = presence;
self
}
#[must_use]
pub fn with_transport(mut self, transport: ControlTransport) -> Self {
self.transport = Some(transport);
self
}
#[must_use]
pub fn with_peer_pid(mut self, pid: u32) -> Self {
self.peer_pid = Some(pid);
self
}
pub fn refusal(&self) -> Option<String> {
if self.by.kind != SubjectKind::Human {
return Some(format!(
"only a person adds, changes, pauses or removes an agent, and {} is not one \
(spec 3.3 S-I11)",
self.by.id
));
}
if self.device.is_some() {
return Some(
"a paired device answers suggested agents only; set up and change agents in \
the web home or the terminal (spec 3.3 decision 16)"
.to_string(),
);
}
None
}
}