use serde_json::Value;
use aidaemon_events::{ExecutorExpectationItem, ExecutorExpectationsDeclaredData};
use aidaemon_events::dialogue::RequestObservationTarget;
use crate::traits::ToolMutationEffects;
pub const UNBOUND_TARGETS_KEY: &str = "_unbound_targets";
pub fn unbound_targets_of(item: &Value) -> Vec<String> {
item.get(UNBOUND_TARGETS_KEY)
.and_then(Value::as_array)
.map(|values| {
values
.iter()
.filter_map(Value::as_str)
.map(str::to_string)
.collect()
})
.unwrap_or_default()
}
pub fn executor_expectations_from_checklist_arguments(
task_id: &str,
arguments: &str,
) -> Option<ExecutorExpectationsDeclaredData> {
let value: Value = serde_json::from_str(arguments).ok()?;
let raw_items = value.get("items")?.as_array()?;
let mut items = Vec::with_capacity(raw_items.len());
for (index, item) in raw_items.iter().enumerate() {
let description = item.get("text")?.as_str()?.to_string();
let mut mutation_effects = ToolMutationEffects::NONE;
if let Some(effects) = item.get("mutation_effects").and_then(Value::as_array) {
for effect in effects.iter().filter_map(Value::as_str) {
if let Some(effect) = ToolMutationEffects::from_protocol_name(effect) {
mutation_effects = mutation_effects.union(effect);
}
}
}
let (targets, observation_targets) = parse_expectation_targets(item.get("targets")).ok()?;
items.push(ExecutorExpectationItem {
index,
description,
requires_observation: item
.get("requires_observation")
.and_then(Value::as_bool)
.unwrap_or(false),
mutation_effects,
targets,
observation_targets,
unbound_targets: unbound_targets_of(item),
depends_on: indices(item.get("depends_on")),
owner_units: indices(item.get("owner_units")),
status: item
.get("status")
.and_then(Value::as_str)
.unwrap_or("pending")
.to_string(),
});
}
(!items.is_empty()).then(|| ExecutorExpectationsDeclaredData {
schema_version: ExecutorExpectationsDeclaredData::SCHEMA_VERSION,
task_id: task_id.to_string(),
items,
outcome: value
.get("outcome")
.and_then(Value::as_str)
.and_then(aidaemon_events::ExecutorClaimedOutcome::from_protocol_name),
short_of: value
.get("short_of")
.and_then(Value::as_str)
.and_then(aidaemon_events::ShortResource::from_protocol_name),
})
}
fn indices(value: Option<&Value>) -> Vec<usize> {
value
.and_then(Value::as_array)
.map(|values| {
values
.iter()
.filter_map(|value| value.as_u64().and_then(|n| usize::try_from(n).ok()))
.collect()
})
.unwrap_or_default()
}
pub fn parse_expectation_targets(
raw_targets: Option<&Value>,
) -> Result<(Vec<String>, Vec<RequestObservationTarget>), String> {
let Some(raw_targets) = raw_targets else {
return Ok((Vec::new(), Vec::new()));
};
let values = raw_targets
.as_array()
.ok_or_else(|| "targets must be an array".to_string())?;
let mut legacy_targets = Vec::with_capacity(values.len());
let mut observation_targets = Vec::with_capacity(values.len());
for value in values {
let target = parse_expectation_target(value)?;
legacy_targets.push(target.subject.value.clone());
observation_targets.push(target);
}
Ok((legacy_targets, observation_targets))
}
pub fn parse_expectation_target(value: &Value) -> Result<RequestObservationTarget, String> {
let target = if let Some(value) = value.as_str() {
RequestObservationTarget::from_legacy_exact(value)
.ok_or_else(|| "target strings must not be empty".to_string())?
} else {
serde_json::from_value::<RequestObservationTarget>(value.clone())
.map_err(|error| format!("invalid structured target: {error}"))?
};
if target.subject.value.trim().is_empty() {
return Err("target subject must not be empty".to_string());
}
if let Some(coverage) = target.collection_coverage.as_ref() {
if coverage.collection.value.trim().is_empty() {
return Err("collection target must not be empty".to_string());
}
if target.facets.is_empty() {
return Err(
"collection-backed targets must declare at least one semantic facet".to_string(),
);
}
}
Ok(target)
}