use adk_core::{UsageUnit, UsageVector};
use serde::{Deserialize, Serialize};
use crate::backend::{BackendId, Locality};
use crate::digest::Digest;
use crate::egress::DataClass;
use crate::geometry::{BBox, EscalationReason};
use crate::manifest::VisionTask;
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", rename_all_fields = "camelCase", tag = "kind")]
pub enum Trigger {
LowConfidence {
threshold: f32,
},
NoResult,
Disagreement {
with: BackendId,
min_iou: f32,
},
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Escalation {
pub backend: BackendId,
pub triggers: Vec<Trigger>,
pub margin: f32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub data_class: Option<DataClass>,
}
impl Escalation {
pub fn to(backend: impl Into<String>) -> Self {
Self {
backend: BackendId::new(backend),
triggers: Vec::new(),
margin: 16.0,
data_class: None,
}
}
pub fn on(mut self, trigger: Trigger) -> Self {
self.triggers.push(trigger);
self
}
pub fn with_margin(mut self, margin: f32) -> Self {
self.margin = margin.max(0.0);
self
}
pub fn with_data_class(mut self, class: impl Into<String>) -> Self {
self.data_class = Some(DataClass::new(class));
self
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Route {
pub primary: BackendId,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub escalation: Option<Escalation>,
}
impl Route {
pub fn to(backend: impl Into<String>) -> Self {
Self { primary: BackendId::new(backend), escalation: None }
}
pub fn escalate(mut self, escalation: Escalation) -> Self {
self.escalation = Some(escalation);
self
}
}
#[derive(Clone, Debug, Default, PartialEq)]
#[non_exhaustive]
pub struct CallOptions {
pub route: Option<Route>,
pub data_class: Option<DataClass>,
pub approved: Vec<Digest>,
pub marked: Vec<BBox>,
pub region: Option<BBox>,
pub frames: bool,
}
impl CallOptions {
pub fn new() -> Self {
Self::default()
}
pub fn with_route(mut self, route: Route) -> Self {
self.route = Some(route);
self
}
pub fn with_data_class(mut self, class: impl Into<String>) -> Self {
self.data_class = Some(DataClass::new(class));
self
}
pub fn approving(mut self, digest: Digest) -> Self {
self.approved.push(digest);
self
}
pub fn with_marked(mut self, regions: Vec<BBox>) -> Self {
self.marked = regions;
self
}
pub fn with_region(mut self, region: BBox) -> Self {
self.region = Some(region);
self
}
pub fn as_frames(mut self) -> Self {
self.frames = true;
self
}
pub fn image_unit(&self) -> UsageUnit {
if self.frames { UsageUnit::Frames } else { UsageUnit::Images }
}
pub(crate) fn data_class(&self) -> DataClass {
self.data_class.clone().unwrap_or_else(|| DataClass::new("image"))
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CallRecord {
pub task: VisionTask,
pub backend: BackendId,
pub locality: Locality,
pub images: Vec<Digest>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub egress: Vec<Digest>,
pub units: UsageVector,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cost_micro_usd: Option<u64>,
pub regions: usize,
pub duration_ms: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub escalation: Option<EscalationReason>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct VisionOutput<T> {
pub value: T,
pub calls: Vec<CallRecord>,
}