use super::{ArtifactName, MutexTag, Profile, ProfileKind, identifier};
use crate::scope::LogicalTarget;
use serde::{Deserialize, Serialize};
use std::ops::Range;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EvalPayload {
pub instruction: String,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(transparent)]
pub struct LocalEvalId(String);
impl<'de> Deserialize<'de> for LocalEvalId {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
String::deserialize(deserializer)?
.parse()
.map_err(serde::de::Error::custom)
}
}
impl std::str::FromStr for LocalEvalId {
type Err = String;
fn from_str(value: &str) -> Result<Self, Self::Err> {
identifier(value, "Eval id")?;
Ok(Self(value.to_owned()))
}
}
impl LocalEvalId {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl PartialEq<&str> for LocalEvalId {
fn eq(&self, other: &&str) -> bool {
self.0 == *other
}
}
impl std::ops::Deref for LocalEvalId {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
impl std::fmt::Display for LocalEvalId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
pub use artifactize_harness::verdict::Priority;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Audience {
Agent,
Human,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum Slot {
Artf {
artifact: ArtifactName,
},
Path {
artifact: ArtifactName,
path: LogicalTarget,
},
}
impl Slot {
pub fn artifact(&self) -> &ArtifactName {
match self {
Self::Artf { artifact } | Self::Path { artifact, .. } => artifact,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct InstructionSlot {
#[serde(serialize_with = "range")]
pub range: Range<usize>,
#[serde(flatten)]
pub slot: Slot,
}
fn range<S: serde::Serializer>(range: &Range<usize>, serializer: S) -> Result<S::Ok, S::Error> {
[range.start, range.end].serialize(serializer)
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EvalDeclaration {
pub id: LocalEvalId,
pub title: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub mutex: Vec<MutexTag>,
#[serde(flatten)]
body: EvalBody,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct EvalBody {
profile: Profile,
#[serde(skip_serializing_if = "Option::is_none")]
profile_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
payload: Option<EvalPayload>,
#[serde(skip_serializing_if = "Vec::is_empty")]
slots: Vec<InstructionSlot>,
#[serde(skip_serializing_if = "Vec::is_empty")]
inputs: Vec<ArtifactName>,
#[serde(skip_serializing_if = "Option::is_none")]
red_at: Option<Priority>,
}
pub(super) struct Declared {
pub id: LocalEvalId,
pub title: String,
pub mutex: Vec<MutexTag>,
pub profile: Profile,
pub profile_name: Option<String>,
pub instruction: Option<(String, Vec<InstructionSlot>)>,
pub inputs: Vec<ArtifactName>,
pub red_at: Option<Priority>,
}
impl EvalDeclaration {
pub(super) fn new(declared: Declared) -> Result<Self, String> {
let kind = declared.profile.kind();
if !declared.mutex.is_empty() && kind == ProfileKind::Human {
return Err("mutex is supported only on runtime and Agent evals.".into());
}
if kind != ProfileKind::Agent && declared.red_at.is_some() {
return Err("red_at is supported only on Agent evals.".into());
}
if kind != ProfileKind::Runtime && !declared.inputs.is_empty() {
return Err("inputs are supported only on runtime evals.".into());
}
super::validation::text(&declared.title, "Eval title")?;
let (payload, slots) = match declared.instruction {
Some((instruction, slots)) => {
super::validation::text(&instruction, "Eval instruction")?;
(Some(EvalPayload { instruction }), slots)
}
None if kind == ProfileKind::Runtime => (None, Vec::new()),
None => return Err("Agent and Human evals need an instruction.".into()),
};
if kind == ProfileKind::Runtime && !slots.is_empty() {
return Err("A runtime eval's instruction names no Artifact.".into());
}
Ok(Self {
id: declared.id,
title: declared.title,
mutex: declared.mutex,
body: EvalBody {
profile: declared.profile,
profile_name: declared.profile_name,
payload,
slots,
inputs: declared.inputs,
red_at: declared.red_at,
},
})
}
pub fn profile(&self) -> &Profile {
&self.body.profile
}
pub fn profile_name(&self) -> Option<&str> {
self.body.profile_name.as_deref()
}
pub fn payload(&self) -> Option<&EvalPayload> {
self.body.payload.as_ref()
}
pub fn instruction(&self) -> Option<&str> {
self.payload().map(|payload| payload.instruction.as_str())
}
pub fn slots(&self) -> &[InstructionSlot] {
&self.body.slots
}
pub fn inputs(&self) -> &[ArtifactName] {
&self.body.inputs
}
pub fn red_at(&self) -> Option<Priority> {
self.body.red_at
}
}
#[cfg(test)]
mod tests;