Skip to main content

aios_cli/benchmark_next_app/
predictor.rs

1//! Adapt DiPECS decision backends into the `NextAppPredictor` trait.
2
3use aios_agent::{DecisionBackend, LocalEvaluatorBackend, RuleBasedBackend};
4use aios_spec::{Intent, IntentType, StructuredContext};
5
6use super::types::{NextAppPredictor, PredictionResult, ScoredPrediction};
7
8pub struct RuleBasedNextAppBackend;
9
10impl NextAppPredictor for RuleBasedNextAppBackend {
11    fn name(&self) -> &'static str {
12        "rule_based"
13    }
14
15    fn predict(
16        &self,
17        ctx: &StructuredContext,
18        _current_app: &str,
19        candidates: &[String],
20    ) -> PredictionResult {
21        let decision = RuleBasedBackend.evaluate(ctx);
22        PredictionResult {
23            ranked: extract_ranked_apps(&decision.intent_batch.intents, candidates),
24            latency_us: decision.latency_us,
25            rationale_present: decision
26                .intent_batch
27                .intents
28                .iter()
29                .any(|i| !i.rationale_tags.is_empty()),
30        }
31    }
32}
33
34pub struct LocalEvaluatorNextAppBackend;
35
36impl NextAppPredictor for LocalEvaluatorNextAppBackend {
37    fn name(&self) -> &'static str {
38        "local_evaluator"
39    }
40
41    fn predict(
42        &self,
43        ctx: &StructuredContext,
44        _current_app: &str,
45        candidates: &[String],
46    ) -> PredictionResult {
47        let decision = LocalEvaluatorBackend.evaluate(ctx);
48        PredictionResult {
49            ranked: extract_ranked_apps(&decision.intent_batch.intents, candidates),
50            latency_us: decision.latency_us,
51            rationale_present: decision
52                .intent_batch
53                .intents
54                .iter()
55                .any(|i| !i.rationale_tags.is_empty()),
56        }
57    }
58}
59
60fn extract_ranked_apps(intents: &[Intent], candidates: &[String]) -> Vec<ScoredPrediction> {
61    let mut best: std::collections::HashMap<String, f32> = std::collections::HashMap::new();
62
63    for intent in intents {
64        let pkg = match &intent.intent_type {
65            IntentType::OpenApp(p)
66            | IntentType::SwitchToApp(p)
67            | IntentType::CheckNotification(p) => p.clone(),
68            IntentType::HandleFile(_) | IntentType::EnterContext(_) | IntentType::Idle => continue,
69        };
70        best.entry(pkg)
71            .and_modify(|s| *s = s.max(intent.confidence))
72            .or_insert(intent.confidence);
73    }
74
75    let mut ranked: Vec<ScoredPrediction> = best
76        .into_iter()
77        .map(|(package, score)| ScoredPrediction { package, score })
78        .collect();
79    ranked.sort_by(|a, b| {
80        b.score
81            .partial_cmp(&a.score)
82            .unwrap_or(std::cmp::Ordering::Equal)
83            .then_with(|| a.package.cmp(&b.package))
84    });
85
86    ranked
87        .into_iter()
88        .filter(|p| candidates.contains(&p.package))
89        .collect()
90}