use adk_core::principal::SubjectRef;
use awp_types::{
ActionClass, ApprovalChain, AutonomyLevel, Bounded, Breach, Budget, Cadence, Charter,
Direction, Governance, Guardrail, GuardrailAction, JobAuthority, Kpi, Measurement, MetricRef,
Reporting, Role, RoleHolder, Signal, SignalKind, SiteKind, SourceRef, Target, Voice,
};
use chrono::{DateTime, Utc};
use uuid::Uuid;
use crate::charter::{CommitDecision, RoleApproval};
pub fn charter(site: Uuid, owners: &[&str]) -> Charter {
Charter {
version: 0,
site_id: site,
kind: SiteKind::Business,
governance: Governance {
roles: owners
.iter()
.map(|owner| RoleHolder {
principal: (*owner).to_string(),
roles: vec![Role::Owner],
})
.collect(),
approval_chains: Vec::new(),
},
mission: "Sell solar kits to homes in Nairobi".into(),
strategy: Vec::new(),
goals: Vec::new(),
kpis: Vec::new(),
guardrails: Vec::new(),
authority: Vec::new(),
budget: Budget {
period: Cadence::Monthly,
currency: "USD".into(),
caps: Default::default(),
},
voice: Voice {
tone: "warm and plain".into(),
languages: Vec::new(),
disclosure: "Written by the site's AI agent for its owner.".into(),
},
red_lines: Vec::new(),
reporting: Reporting { review: Cadence::Weekly, routes: Vec::new(), quiet_hours: None },
public: Default::default(),
price_rule: Default::default(),
handoff_threshold: None,
publish_delay_hours: None,
amount_thresholds: Default::default(),
signed: None,
}
}
pub fn with_role(mut charter: Charter, role: Role, principal: &str) -> Charter {
match charter.governance.roles.iter_mut().find(|holder| holder.principal == principal) {
Some(holder) => holder.roles.push(role),
None => charter
.governance
.roles
.push(RoleHolder { principal: principal.to_string(), roles: vec![role] }),
}
charter
}
pub fn with_chain(mut charter: Charter, action: impl Into<ActionClass>, roles: &[Role]) -> Charter {
let action = action.into();
charter.governance.approval_chains.retain(|chain| chain.action != action);
charter.governance.approval_chains.push(ApprovalChain { action, roles: roles.to_vec() });
charter
}
pub fn kpi(id: &str, metric: &str, source: &str) -> Kpi {
Kpi {
id: id.to_string(),
name: id.replace('_', " "),
metric: MetricRef(metric.to_string()),
source: SourceRef::from(source),
direction: Direction::Up,
baseline: None,
target: None,
cadence: Cadence::Daily,
}
}
pub fn with_target(mut kpi: Kpi, baseline: f64, target: f64, at: DateTime<Utc>) -> Kpi {
kpi.baseline = Some(Measurement {
value: baseline,
window: std::time::Duration::from_secs(14 * 24 * 60 * 60),
measured_at: at,
});
kpi.target = Some(Target { value: target, horizon: at + chrono::Duration::days(90) });
kpi
}
pub fn guardrail(
id: &str,
metric: &str,
threshold: f64,
window_days: u64,
jobs: &[&str],
) -> Guardrail {
Guardrail {
id: id.to_string(),
metric: MetricRef(metric.to_string()),
threshold,
breach: Breach::Above,
window: std::time::Duration::from_secs(window_days * 24 * 60 * 60),
action: GuardrailAction::PauseJob,
jobs: jobs.iter().map(|job| (*job).to_string()).collect(),
}
}
pub fn authority(job: &str, level: AutonomyLevel) -> JobAuthority {
JobAuthority { job: job.to_string(), level, limits: Default::default() }
}
pub fn decision(id: &str, at: DateTime<Utc>, approvals: &[(Role, &str)]) -> CommitDecision {
approvals.iter().fold(CommitDecision::new(id, at), |decision, (role, who)| {
decision.with_approval(RoleApproval::new(*role, SubjectRef::human(*who), at))
})
}
pub fn signal(kind: SignalKind) -> Signal {
Signal { id: Uuid::new_v4(), kind, emitted_by: "concierge".into(), emitted_at: Utc::now() }
}
pub fn emitted_by(mut signal: Signal, source: &str) -> Signal {
signal.emitted_by = source.to_string();
signal
}
pub fn content_gap(topic: &str, questions: u32) -> Signal {
signal(SignalKind::ContentGap {
topic: Bounded::new(topic, 120).expect("a short topic"),
question_count: questions,
samples: Vec::new(),
})
}
pub fn review(platform: &str, rating: u8, excerpt: &str) -> Signal {
signal(SignalKind::ReviewSeen {
platform: platform.to_string(),
rating,
excerpt: Bounded::new(excerpt, 280).expect("a short excerpt"),
})
}