use crate::runtime_core::MapleRuntime;
use crate::types::*;
#[derive(Clone)]
pub struct ResonatorHandle {
pub id: ResonatorId,
runtime: MapleRuntime,
}
impl ResonatorHandle {
pub(crate) fn new(id: ResonatorId, runtime: MapleRuntime) -> Self {
Self { id, runtime }
}
pub async fn signal_presence(&self, state: PresenceState) -> Result<(), PresenceError> {
self.runtime
.presence_fabric()
.signal_presence(self.id, state)
.await
}
pub async fn enable_silent_mode(&self) {
self.runtime
.presence_fabric()
.enable_silent_presence(self.id)
.await
}
pub fn get_presence(&self) -> Option<PresenceState> {
self.runtime.presence_fabric().get_presence(&self.id)
}
pub async fn couple_with(
&self,
_target: ResonatorId,
params: CouplingParams,
) -> Result<CouplingHandle, CouplingError> {
let (coupling_id, attention_token) = self
.runtime
.coupling_fabric()
.establish_coupling(params)
.await?;
Ok(CouplingHandle::new(
coupling_id,
attention_token,
self.runtime.clone(),
))
}
pub async fn attention_status(&self) -> Option<AttentionBudget> {
self.runtime
.attention_allocator()
.get_budget(&self.id)
.await
}
}
#[derive(Clone)]
pub struct CouplingHandle {
pub id: CouplingId,
_attention_token: AllocationToken,
runtime: MapleRuntime,
}
impl CouplingHandle {
pub(crate) fn new(
id: CouplingId,
attention_token: AllocationToken,
runtime: MapleRuntime,
) -> Self {
Self {
id,
_attention_token: attention_token,
runtime,
}
}
pub async fn strengthen(&self, delta: f64) -> Result<(), CouplingError> {
self.runtime
.coupling_fabric()
.strengthen(self.id, delta)
.await
}
pub async fn weaken(&self, factor: f64) -> Result<(), CouplingError> {
self.runtime.coupling_fabric().weaken(self.id, factor).await
}
pub async fn decouple(self) -> Result<DecouplingResult, CouplingError> {
self.runtime
.coupling_fabric()
.decouple_safely(self.id)
.await
}
pub fn get_coupling(&self) -> Option<Coupling> {
self.runtime.coupling_fabric().get_coupling(&self.id)
}
}
#[derive(Debug, Clone)]
pub enum DecouplingResult {
Success,
PartialSuccess { reason: String },
}
#[derive(Debug, Clone)]
pub struct ScheduleHandle {
pub task_id: Option<TaskId>,
pub status: ScheduleStatus,
}
impl ScheduleHandle {
pub(crate) fn scheduled(task_id: TaskId) -> Self {
Self {
task_id: Some(task_id),
status: ScheduleStatus::Scheduled,
}
}
pub(crate) fn rejected(reason: RejectionReason) -> Self {
Self {
task_id: None,
status: ScheduleStatus::Rejected(reason),
}
}
pub(crate) fn deferred(reason: DeferralReason) -> Self {
Self {
task_id: None,
status: ScheduleStatus::Deferred(reason),
}
}
}
#[derive(Debug, Clone)]
pub enum ScheduleStatus {
Scheduled,
Rejected(RejectionReason),
Deferred(DeferralReason),
}
#[derive(Debug, Clone)]
pub enum RejectionReason {
CircuitOpen,
AttentionUnavailable,
QueueFull,
}
#[derive(Debug, Clone)]
pub enum DeferralReason {
AttentionUnavailable,
BackPressure,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TaskId(uuid::Uuid);
impl TaskId {
pub fn generate() -> Self {
Self(uuid::Uuid::new_v4())
}
}
impl std::fmt::Display for TaskId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "task:{}", self.0)
}
}