Skip to main content

scouter_types/genai/
traits.rs

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