Skip to main content

aios_agent/backends/cloud_llm/
summarizer.rs

1use std::time::Duration;
2
3use aios_spec::{RecentDecisionRecord, UserBehaviorProfile};
4use reqwest::blocking::Client;
5use serde::{Deserialize, Serialize};
6
7use super::config::{cloud_llm_enabled, CloudLlmConfig, CloudProvider};
8
9const ENV_PROFILE_SUMMARY_ENABLED: &str = "DIPECS_PROFILE_SUMMARY_ENABLED";
10const PROFILE_SUMMARY_SYSTEM_PROMPT: &str = r#"You summarize DiPECS user behavior memory.
11Return only compact plain text, no markdown, no JSON.
12Rules:
13- Summarize stable habits from sanitized counters and recent feedback only.
14- Mention action patterns that were likely correct, policy rejected, or failed.
15- Do not invent app names or private content.
16- Keep it under 80 words.
17"#;
18
19#[derive(Debug, Clone)]
20pub struct ProfileSummarizer {
21    config: CloudLlmConfig,
22    client: Client,
23}
24
25impl ProfileSummarizer {
26    pub fn from_env() -> Result<Option<Self>, String> {
27        if !cloud_llm_enabled() || !profile_summary_enabled() {
28            return Ok(None);
29        }
30        let config = CloudLlmConfig::from_env()?;
31        let client = Client::builder()
32            .timeout(Duration::from_secs(config.timeout_secs))
33            .build()
34            .map_err(|error| format!("building profile summarizer HTTP client failed: {error}"))?;
35        Ok(Some(Self { config, client }))
36    }
37
38    pub fn summarize(
39        &self,
40        profile: &UserBehaviorProfile,
41        recent_feedback: &[RecentDecisionRecord],
42    ) -> Result<String, String> {
43        let payload = ProfileSummaryInput {
44            behavior_profile: profile,
45            recent_feedback,
46        };
47        let user_json = serde_json::to_string(&payload)
48            .map_err(|error| format!("serializing profile summary input failed: {error}"))?;
49        let request = SummaryRequest {
50            model: self.config.model.clone(),
51            temperature: self.config.temperature,
52            messages: vec![
53                SummaryMessage {
54                    role: "system".into(),
55                    content: PROFILE_SUMMARY_SYSTEM_PROMPT.into(),
56                },
57                SummaryMessage {
58                    role: "user".into(),
59                    content: format!(
60                        "Summarize this DiPECS behavior memory.\nprofile_summary_input_json={user_json}"
61                    ),
62                },
63            ],
64            provider_options: SummaryProviderOptions::from_config(&self.config),
65        };
66
67        let mut http = self
68            .client
69            .post(&self.config.endpoint)
70            .json(&request)
71            .header("Accept", "application/json");
72        if let Some(api_key) = &self.config.api_key {
73            http = http.bearer_auth(api_key);
74        }
75        let response = http
76            .send()
77            .map_err(|error| format!("profile summary request failed: {error}"))?;
78        let status = response.status();
79        if !status.is_success() {
80            return Err(format!(
81                "HTTP {} from profile summary endpoint",
82                status.as_u16()
83            ));
84        }
85        let payload: SummaryResponse = response
86            .json()
87            .map_err(|error| format!("invalid profile summary response JSON: {error}"))?;
88        payload
89            .first_text()
90            .map(|value| value.trim().to_string())
91            .filter(|value| !value.is_empty())
92            .ok_or_else(|| "no profile summary content returned".to_string())
93    }
94}
95
96fn profile_summary_enabled() -> bool {
97    matches!(
98        std::env::var(ENV_PROFILE_SUMMARY_ENABLED).ok().as_deref(),
99        Some("1" | "true" | "TRUE" | "yes" | "YES" | "on" | "ON")
100    )
101}
102
103#[derive(Debug, Serialize)]
104struct ProfileSummaryInput<'a> {
105    behavior_profile: &'a UserBehaviorProfile,
106    recent_feedback: &'a [RecentDecisionRecord],
107}
108
109#[derive(Debug, Serialize)]
110struct SummaryRequest {
111    model: String,
112    temperature: f32,
113    messages: Vec<SummaryMessage>,
114    #[serde(flatten)]
115    provider_options: SummaryProviderOptions,
116}
117
118#[derive(Debug, Serialize)]
119struct SummaryMessage {
120    role: String,
121    content: String,
122}
123
124#[derive(Debug, Default, Serialize)]
125struct SummaryProviderOptions {
126    #[serde(skip_serializing_if = "Option::is_none")]
127    thinking: Option<SummaryThinkingConfig>,
128    #[serde(skip_serializing_if = "Option::is_none")]
129    enable_thinking: Option<bool>,
130}
131
132impl SummaryProviderOptions {
133    fn from_config(config: &CloudLlmConfig) -> Self {
134        match config.provider {
135            CloudProvider::DeepSeek => Self {
136                thinking: config.enable_thinking.map(|enabled| SummaryThinkingConfig {
137                    kind: if enabled { "enabled" } else { "disabled" }.to_string(),
138                }),
139                enable_thinking: None,
140            },
141            CloudProvider::Qwen => Self {
142                thinking: None,
143                enable_thinking: config.enable_thinking,
144            },
145            CloudProvider::GenericOpenAiCompatible => Self::default(),
146        }
147    }
148}
149
150#[derive(Debug, Serialize)]
151struct SummaryThinkingConfig {
152    #[serde(rename = "type")]
153    kind: String,
154}
155
156#[derive(Debug, Deserialize)]
157struct SummaryResponse {
158    choices: Vec<SummaryChoice>,
159}
160
161impl SummaryResponse {
162    fn first_text(&self) -> Option<String> {
163        self.choices
164            .first()
165            .and_then(|choice| choice.message.content.clone())
166    }
167}
168
169#[derive(Debug, Deserialize)]
170struct SummaryChoice {
171    message: SummaryMessageResponse,
172}
173
174#[derive(Debug, Deserialize)]
175struct SummaryMessageResponse {
176    content: Option<String>,
177}
178
179#[cfg(test)]
180mod tests {
181    use super::*;
182
183    #[test]
184    fn disabled_without_explicit_env() {
185        std::env::remove_var(ENV_PROFILE_SUMMARY_ENABLED);
186        assert!(!profile_summary_enabled());
187    }
188}