use ag_protocol::{ProtocolRequestProfile, TurnPrompt};
use serde::{Deserialize, Serialize};
use super::GraphContractError;
use crate::{AgentDefinition, AgentSelection, EnvId, PermissionMode};
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct GraphNode {
#[serde(with = "super::selection")]
pub agent: AgentSelection,
pub definition: AgentDefinition,
pub dependencies: Vec<GraphDependency>,
pub env_id: EnvId,
pub execution: GraphExecutionProfile,
pub id: String,
pub inputs: Vec<GraphInputBinding>,
pub objective: String,
pub outputs: Vec<GraphOutput>,
pub request: TurnPrompt,
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct GraphExecutionProfile {
pub context: GraphContext,
pub post_processing: GraphPostProcessing,
pub response_profile: ProtocolRequestProfile,
pub shared_budget_id: Option<String>,
pub workspace_access: GraphWorkspaceAccess,
}
impl GraphExecutionProfile {
pub fn permission_mode(
&self,
definition: &AgentDefinition,
session_mode: PermissionMode,
) -> PermissionMode {
if self.workspace_access.is_read() {
return PermissionMode::ReadOnly;
}
definition.permission_mode(session_mode)
}
pub fn validate(&self) -> Result<(), GraphContractError> {
match &self.context {
GraphContext::Fresh => {
if self.response_profile == ProtocolRequestProfile::SessionTurn
|| self.post_processing != GraphPostProcessing::None
{
return Err(GraphContractError::InvalidProfile(
"fresh utilities require a utility schema and no session effects",
));
}
}
GraphContext::TaskConversation { task_id } => {
GraphContractError::require(task_id, "task_id")?;
if self.response_profile != ProtocolRequestProfile::SessionTurn {
return Err(GraphContractError::InvalidProfile(
"task conversations require the session-turn schema",
));
}
}
}
if self.workspace_access.is_read() && self.post_processing != GraphPostProcessing::None {
return Err(GraphContractError::InvalidProfile(
"shared readers cannot perform host mutation effects",
));
}
if self.workspace_access == GraphWorkspaceAccess::BackgroundRead
&& self.context != GraphContext::Fresh
{
return Err(GraphContractError::InvalidProfile(
"background readers require a fresh utility context",
));
}
if let Some(budget_id) = &self.shared_budget_id {
GraphContractError::require(budget_id, "shared_budget_id")?;
}
Ok(())
}
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum GraphContext {
Fresh,
TaskConversation {
task_id: String,
},
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum GraphPostProcessing {
None,
SessionWorkflow,
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum GraphWorkspaceAccess {
SharedRead,
BackgroundRead,
ExclusiveWrite,
}
impl GraphWorkspaceAccess {
#[must_use]
pub fn is_read(self) -> bool {
matches!(self, Self::SharedRead | Self::BackgroundRead)
}
}
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct GraphDependency {
pub gate: GraphCompletionGate,
pub node_id: String,
}
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum GraphCompletionGate {
ExecutionSucceeded,
ResultValidated {
output_name: String,
},
ResultAccepted {
output_name: String,
},
}
impl GraphCompletionGate {
pub(super) fn output_name(&self) -> Option<&str> {
match self {
Self::ExecutionSucceeded => None,
Self::ResultValidated { output_name } | Self::ResultAccepted { output_name } => {
Some(output_name)
}
}
}
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct GraphOutput {
pub name: String,
pub schema: ProtocolRequestProfile,
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct GraphInputBinding {
pub name: String,
pub output_name: String,
pub producer_node_id: String,
}
#[cfg(test)]
#[path = "node_test.rs"]
mod tests;