Skip to main content

aios_agent/backends/predictive/
mod.rs

1//! PredictiveLocalBackend - deterministic next-app prediction from exported artifacts.
2//!
3//! Training can happen offline, but runtime inference stays local and pure: the
4//! backend reads a JSON artifact containing Naive Bayes, Markov, and a log-lift
5//! feature ensemble, then emits low-risk app intent candidates.
6
7mod backend;
8mod ensemble;
9mod predictor;
10mod train;
11mod types;
12
13#[cfg(test)]
14mod tests;
15#[cfg(test)]
16mod validation_tests;
17
18pub use backend::PredictiveLocalBackend;
19pub use train::train_next_app_artifact;
20pub use types::{
21    AppScore, EnsembleCombiner, FeatureLiftModel, FeatureLiftTree, LogisticRerankerModel,
22    MarkovModel, NaiveBayesModel, NextAppAlgorithm, NextAppModelArtifact, NextAppModelConfig,
23    NextAppTrainingExample, PredictionFeatures, TrainingSummary,
24};
25
26pub struct NextAppPredictor {
27    artifact: NextAppModelArtifact,
28    app_index: std::collections::BTreeMap<String, usize>,
29}
30
31const SCHEMA_VERSION: &str = "dipecs.next_app_model.v1";
32const MODEL_NAME: &str = "predictive-local-v0.1";
33const MAX_BACKEND_INTENTS: usize = 5;
34const MAX_TRAINED_FEATURES: usize = 128;
35
36pub fn prediction_features_for_example(example: &NextAppTrainingExample) -> PredictionFeatures {
37    PredictionFeatures {
38        user_id: Some(example.user_id.clone()),
39        current_app: Some(example.current_app.clone()),
40        history: example.history.clone(),
41        hour_bucket: Some(example.hour_bucket),
42        weekday: Some(example.weekday),
43        event_type: Some(example.event_type.clone()),
44    }
45}
46
47fn score_order(a: f32, b: f32) -> std::cmp::Ordering {
48    b.partial_cmp(&a).unwrap_or(std::cmp::Ordering::Equal)
49}
50
51fn hour_bucket(timestamp_ms: i64) -> u8 {
52    let seconds = timestamp_ms.div_euclid(1000);
53    ((seconds.div_euclid(3600)).rem_euclid(24)) as u8
54}
55
56fn weekday(timestamp_ms: i64) -> u8 {
57    let days = timestamp_ms.div_euclid(86_400_000);
58    ((days + 4).rem_euclid(7)) as u8
59}