Skip to main content

scouter_types/genai/
traits.rs

1use crate::genai::{
2    AssertionResult, AssertionTask, ComparisonOperator, EvaluationTask, EvaluationTaskType,
3    LLMJudgeTask,
4};
5use serde_json::Value;
6use std::fmt::Debug;
7
8pub trait TaskAccessor {
9    /// Returns optional field path - avoids `&Option<String>` pattern
10    fn field_path(&self) -> Option<&str>;
11
12    /// Returns assertion ID as string slice
13    fn id(&self) -> &str;
14
15    fn task_type(&self) -> &EvaluationTaskType;
16
17    /// Returns reference to comparison operator
18    fn operator(&self) -> &ComparisonOperator;
19
20    /// Returns reference to expected value
21    fn expected_value(&self) -> &Value;
22
23    /// Returns slice of dependency IDs - more efficient than `&Vec<String>`
24    fn depends_on(&self) -> &[String];
25
26    fn add_result(&mut self, result: AssertionResult);
27}
28
29pub fn separate_tasks(tasks: Vec<EvaluationTask>) -> (Vec<AssertionTask>, Vec<LLMJudgeTask>) {
30    let mut llm_judges = Vec::new();
31    let mut assertions = Vec::new();
32
33    for task in tasks {
34        match task {
35            EvaluationTask::Assertion(a) => assertions.push(*a),
36            EvaluationTask::LLMJudge(j) => llm_judges.push(*j),
37        }
38    }
39
40    (assertions, llm_judges)
41}
42
43#[derive(Debug)]
44pub enum TaskRef<'a> {
45    Assertion(&'a mut AssertionTask),
46    LLMJudge(&'a mut LLMJudgeTask),
47}
48
49impl<'a> TaskRef<'a> {
50    pub fn depends_on(&self) -> &[String] {
51        match self {
52            TaskRef::Assertion(t) => t.depends_on(),
53            TaskRef::LLMJudge(t) => t.depends_on(),
54        }
55    }
56}
57
58/// Extension trait for evaluation profiles
59/// Provides unified access to assertions and LLM judge tasks
60pub trait ProfileExt {
61    fn id(&self) -> &str;
62    fn get_assertion_by_id(&self, id: &str) -> Option<&AssertionTask>;
63    fn get_llm_judge_by_id(&self, id: &str) -> Option<&LLMJudgeTask>;
64    fn get_task_by_id(&self, id: &str) -> Option<&dyn TaskAccessor>;
65    fn has_llm_tasks(&self) -> bool;
66}