use std::fmt;
use adk_core::ApprovalRisk;
use serde::{Deserialize, Serialize};
use crate::agents::record::{AgentDefinition, AgentRecord};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "action", rename_all = "camelCase", rename_all_fields = "camelCase")]
#[non_exhaustive]
pub enum AgentAction {
Create {
definition: AgentDefinition,
#[serde(default, skip_serializing_if = "Option::is_none")]
replaces: Option<String>,
},
Change {
id: String,
expected_version: u64,
definition: AgentDefinition,
},
Pause {
id: String,
},
Resume {
id: String,
},
Remove {
id: String,
expected_version: u64,
},
}
impl AgentAction {
pub fn id(&self) -> Option<&str> {
match self {
Self::Create { .. } => None,
Self::Change { id, .. }
| Self::Pause { id }
| Self::Resume { id }
| Self::Remove { id, .. } => Some(id),
}
}
pub fn name(&self) -> &'static str {
match self {
Self::Create { .. } => "create",
Self::Change { .. } => "change",
Self::Pause { .. } => "pause",
Self::Resume { .. } => "resume",
Self::Remove { .. } => "remove",
}
}
pub fn digest(&self) -> String {
use sha2::Digest as _;
let canonical = match self {
Self::Create { definition, replaces } => Self::Create {
definition: definition.clone().normalized(),
replaces: replaces.clone(),
},
Self::Change { id, expected_version, definition } => Self::Change {
id: id.clone(),
expected_version: *expected_version,
definition: definition.clone().normalized(),
},
other => other.clone(),
};
let bytes = serde_json::to_vec(&canonical).unwrap_or_default();
sha2::Sha256::digest(&bytes).iter().map(|byte| format!("{byte:02x}")).collect()
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct EndedWork {
#[serde(default)]
pub work: Vec<String>,
#[serde(default)]
pub routines: Vec<String>,
#[serde(default)]
pub decisions: Vec<String>,
#[serde(default)]
pub follow_ups: Vec<String>,
#[serde(default)]
pub rules: u64,
}
impl EndedWork {
pub fn is_empty(&self) -> bool {
self.work.is_empty()
&& self.routines.is_empty()
&& self.decisions.is_empty()
&& self.follow_ups.is_empty()
&& self.rules == 0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AgentPreview {
pub id: String,
pub action: String,
#[serde(default)]
pub abilities: Vec<String>,
pub risk: ApprovalRisk,
#[serde(default)]
pub money: bool,
#[serde(default)]
pub widens: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub fields: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub was: Option<AgentDefinition>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub now: Option<AgentDefinition>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ended: Option<EndedWork>,
pub digest: String,
}
impl AgentPreview {
pub fn new(
id: impl Into<String>,
action: impl Into<String>,
digest: impl Into<String>,
assessment: crate::agents::AgentAssessment,
) -> Self {
Self {
id: id.into(),
action: action.into(),
abilities: assessment.abilities,
risk: assessment.risk,
money: assessment.money,
widens: false,
fields: Vec::new(),
was: None,
now: None,
ended: None,
digest: digest.into(),
}
}
pub fn code(&self) -> &str {
&self.digest[..self.digest.len().min(6)]
}
pub fn needs_presence(&self) -> bool {
self.widens && (self.risk >= ApprovalRisk::High || self.money)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum AgentChange {
Proposed,
Created,
Changed,
Paused,
Resumed,
Removing,
Removed,
Refused,
CantStart,
Hosted,
}
impl AgentChange {
pub fn as_str(self) -> &'static str {
match self {
Self::Proposed => "proposed",
Self::Created => "created",
Self::Changed => "changed",
Self::Paused => "paused",
Self::Resumed => "resumed",
Self::Removing => "removing",
Self::Removed => "removed",
Self::Refused => "refused",
Self::CantStart => "cantStart",
Self::Hosted => "hosted",
}
}
}
impl fmt::Display for AgentChange {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AgentView {
pub agent: AgentRecord,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cant_start: Option<String>,
}
impl AgentView {
pub fn new(agent: AgentRecord) -> Self {
Self { agent, cant_start: None }
}
#[must_use]
pub fn with_cant_start(mut self, reason: impl Into<String>) -> Self {
self.cant_start = Some(reason.into());
self
}
}