use std::sync::Arc;
use adk_core::{AgentId, ApprovalRisk};
use adk_runner::Runner;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use crate::agents::record::{AgentDefinition, AgentRecord, AgentUses, ModelChoice};
use crate::error::Result;
use crate::partition::{PartitionId, PartitionMode};
use crate::process::ProgressSource;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AgentAssessment {
pub risk: ApprovalRisk,
#[serde(default)]
pub money: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub abilities: Vec<String>,
}
impl AgentAssessment {
pub fn new(risk: ApprovalRisk) -> Self {
Self { risk, money: false, abilities: Vec::new() }
}
#[must_use]
pub fn with_money(mut self) -> Self {
self.money = true;
self.risk = self.risk.max(ApprovalRisk::High);
self
}
#[must_use]
pub fn with_ability(mut self, ability: impl Into<String>) -> Self {
self.abilities.push(ability.into());
self
}
#[must_use]
pub fn merge(mut self, other: Self) -> Self {
self.risk = self.risk.max(other.risk);
self.money |= other.money;
for ability in other.abilities {
if !self.abilities.contains(&ability) {
self.abilities.push(ability);
}
}
if self.money {
self.risk = self.risk.max(ApprovalRisk::High);
}
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AgentCeiling {
pub mode: PartitionMode,
pub uses: AgentUses,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub daily_limit_micro_usd: Option<u64>,
}
impl AgentCeiling {
pub fn new(mode: PartitionMode, uses: AgentUses) -> Self {
Self { mode, uses, daily_limit_micro_usd: None }
}
#[must_use]
pub fn with_daily_limit(mut self, micro_usd: u64) -> Self {
self.daily_limit_micro_usd = Some(micro_usd);
self
}
#[must_use]
pub(crate) fn within_daily_limit(mut self, cap: Option<u64>) -> Self {
self.daily_limit_micro_usd = match (self.daily_limit_micro_usd, cap) {
(Some(own), Some(cap)) => Some(own.min(cap)),
(own, cap) => own.or(cap),
};
self
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AgentChoices {
#[serde(default)]
pub models: Vec<ModelChoice>,
#[serde(default)]
pub packs: Vec<String>,
}
impl AgentChoices {
#[must_use]
pub fn with_model(mut self, model: ModelChoice) -> Self {
self.models.push(model);
self
}
#[must_use]
pub fn with_pack(mut self, pack: impl Into<String>) -> Self {
self.packs.push(pack.into());
self
}
}
#[async_trait]
pub trait HostedClose: Send + Sync {
async fn close(&self);
}
#[derive(Clone)]
#[non_exhaustive]
pub struct HostedAgent {
pub runner: Arc<Runner>,
pub progress: Option<Arc<dyn ProgressSource>>,
pub close: Option<Arc<dyn HostedClose>>,
}
impl HostedAgent {
pub fn new(runner: Arc<Runner>) -> Self {
Self { runner, progress: None, close: None }
}
#[must_use]
pub fn with_progress(mut self, progress: Arc<dyn ProgressSource>) -> Self {
self.progress = Some(progress);
self
}
#[must_use]
pub fn with_close(mut self, close: Arc<dyn HostedClose>) -> Self {
self.close = Some(close);
self
}
}
impl std::fmt::Debug for HostedAgent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HostedAgent")
.field("root", &self.runner.root_agent_name())
.field("progress", &self.progress.is_some())
.field("close", &self.close.is_some())
.finish()
}
}
#[async_trait]
pub trait AgentFactory: Send + Sync {
fn assess(&self, record: &AgentRecord) -> AgentAssessment;
async fn ceiling(&self, agent: &AgentId, session: &str) -> Result<Option<AgentCeiling>>;
async fn ceiling_in(
&self,
agent: &AgentId,
session: &str,
area: &PartitionId,
) -> Result<Option<AgentCeiling>> {
let _ = area;
self.ceiling(agent, session).await
}
async fn screen(&self, definition: AgentDefinition) -> AgentDefinition {
definition
}
async fn choices(&self) -> Result<AgentChoices>;
async fn build(&self, record: &AgentRecord) -> Result<HostedAgent>;
async fn retired(&self, record: &AgentRecord) -> Result<u64>;
fn reserved_ids(&self) -> Vec<String> {
Vec::new()
}
fn reserved_names(&self) -> Vec<String> {
Vec::new()
}
}