Skip to main content

aios_agent/backends/cloud_llm/
client.rs

1use std::time::{Duration, Instant};
2
3use aios_spec::{DecisionBackendResult, DecisionRoute, IntentBatch, ModelInput, StructuredContext};
4use reqwest::blocking::Client;
5use serde::{Deserialize, Serialize};
6
7use super::config::{CloudLlmConfig, CloudProvider};
8use super::translate::{idle_batch, parse_model_output, translate_intents};
9use crate::DecisionBackend;
10
11#[derive(Debug, Clone)]
12pub struct CloudLlmBackend {
13    config: CloudLlmConfig,
14    client: Client,
15}
16
17impl CloudLlmBackend {
18    pub fn try_new(config: CloudLlmConfig) -> Result<Self, String> {
19        let client = Client::builder()
20            .timeout(Duration::from_secs(config.timeout_secs))
21            .build()
22            .map_err(|error| format!("building HTTP client failed: {error}"))?;
23        Ok(Self { config, client })
24    }
25
26    fn request_intents(&self, input: &ModelInput) -> Result<IntentBatch, String> {
27        let context = &input.current_context;
28        let request = self.build_request_body(input)?;
29
30        let mut http = self
31            .client
32            .post(&self.config.endpoint)
33            .json(&request)
34            .header("Accept", "application/json");
35        if let Some(api_key) = &self.config.api_key {
36            http = http.bearer_auth(api_key);
37        }
38
39        let response = http
40            .send()
41            .map_err(|error| format!("request failed: {error}"))?;
42        let status = response.status();
43        if !status.is_success() {
44            return Err(format!("HTTP {} from cloud LLM endpoint", status.as_u16()));
45        }
46
47        let payload: ChatCompletionResponse = response
48            .json()
49            .map_err(|error| format!("invalid response JSON: {error}"))?;
50        let content = payload
51            .first_text()
52            .ok_or_else(|| "no completion content returned".to_string())?;
53        let model_output = parse_model_output(&content)?;
54
55        Ok(IntentBatch {
56            window_id: context.window_id.clone(),
57            intents: translate_intents(model_output.intents)?,
58            generated_at_ms: context.window_end_ms,
59            model: payload.model.unwrap_or_else(|| self.config.model.clone()),
60        })
61    }
62
63    fn render_user_prompt(&self, input: &ModelInput) -> Result<String, String> {
64        render_model_input_prompt(input)
65    }
66
67    fn build_request_body(&self, input: &ModelInput) -> Result<ChatCompletionRequest, String> {
68        let messages = vec![
69            ChatMessage {
70                role: "system".to_string(),
71                content: self.config.system_prompt.clone(),
72            },
73            ChatMessage {
74                role: "user".to_string(),
75                content: self.render_user_prompt(input)?,
76            },
77        ];
78        Ok(ChatCompletionRequest {
79            model: self.config.model.clone(),
80            temperature: self.config.temperature,
81            response_format: ChatResponseFormat {
82                kind: "json_object".to_string(),
83            },
84            messages,
85            reasoning_effort: self.config.reasoning_effort.clone(),
86            provider_options: ProviderRequestOptions::from_config(&self.config),
87        })
88    }
89}
90
91impl DecisionBackend for CloudLlmBackend {
92    fn evaluate(&self, context: &StructuredContext) -> DecisionBackendResult {
93        let input = ModelInput::current_only(context.clone());
94        self.evaluate_model_input(&input)
95    }
96
97    fn evaluate_model_input(&self, input: &ModelInput) -> DecisionBackendResult {
98        let context = &input.current_context;
99        let start = Instant::now();
100        match self.request_intents(input) {
101            Ok(intent_batch) => {
102                let rationale_tags = intent_batch
103                    .intents
104                    .iter()
105                    .flat_map(|intent| intent.rationale_tags.iter().cloned())
106                    .collect();
107                DecisionBackendResult {
108                    route: DecisionRoute::CloudLlm,
109                    intent_batch,
110                    rationale_tags,
111                    latency_us: start.elapsed().as_micros() as u64,
112                    error: None,
113                }
114            },
115            Err(error) => DecisionBackendResult {
116                route: DecisionRoute::CloudLlm,
117                intent_batch: idle_batch(context, "cloud-llm-error".to_string()),
118                rationale_tags: vec!["cloud_llm_error".into()],
119                latency_us: start.elapsed().as_micros() as u64,
120                error: Some(error),
121            },
122        }
123    }
124}
125
126pub(super) fn render_model_input_prompt(input: &ModelInput) -> Result<String, String> {
127    let json = serde_json::to_string(input)
128        .map_err(|error| format!("serializing ModelInput failed: {error}"))?;
129    Ok(format!(
130        "Generate DiPECS intents for this sanitized context plus behavior memory.\nwindow_id={}\nmodel_input_json={json}",
131        input.current_context.window_id
132    ))
133}
134
135#[derive(Debug, Serialize)]
136struct ChatCompletionRequest {
137    model: String,
138    temperature: f32,
139    response_format: ChatResponseFormat,
140    messages: Vec<ChatMessage>,
141    #[serde(skip_serializing_if = "Option::is_none")]
142    reasoning_effort: Option<String>,
143    #[serde(flatten)]
144    provider_options: ProviderRequestOptions,
145}
146
147#[derive(Debug, Default, Serialize)]
148struct ProviderRequestOptions {
149    #[serde(skip_serializing_if = "Option::is_none")]
150    thinking: Option<ThinkingConfig>,
151    #[serde(skip_serializing_if = "Option::is_none")]
152    enable_thinking: Option<bool>,
153}
154
155impl ProviderRequestOptions {
156    fn from_config(config: &CloudLlmConfig) -> Self {
157        match config.provider {
158            CloudProvider::DeepSeek => Self {
159                thinking: config.enable_thinking.map(|enabled| ThinkingConfig {
160                    kind: if enabled { "enabled" } else { "disabled" }.to_string(),
161                }),
162                enable_thinking: None,
163            },
164            CloudProvider::Qwen => Self {
165                thinking: None,
166                enable_thinking: config.enable_thinking,
167            },
168            CloudProvider::GenericOpenAiCompatible => Self::default(),
169        }
170    }
171}
172
173#[derive(Debug, Serialize)]
174struct ThinkingConfig {
175    #[serde(rename = "type")]
176    kind: String,
177}
178
179#[derive(Debug, Serialize)]
180struct ChatResponseFormat {
181    #[serde(rename = "type")]
182    kind: String,
183}
184
185#[derive(Debug, Serialize)]
186struct ChatMessage {
187    role: String,
188    content: String,
189}
190
191#[derive(Debug, Deserialize)]
192struct ChatCompletionResponse {
193    model: Option<String>,
194    choices: Vec<ChatChoice>,
195}
196
197impl ChatCompletionResponse {
198    fn first_text(&self) -> Option<String> {
199        self.choices.first().and_then(|choice| {
200            choice
201                .message
202                .content
203                .clone()
204                .filter(|content| !content.trim().is_empty())
205                .or_else(|| choice.message.reasoning_content.clone())
206        })
207    }
208}
209
210#[derive(Debug, Deserialize)]
211struct ChatChoice {
212    message: ChatMessageResponse,
213}
214
215#[derive(Debug, Deserialize)]
216struct ChatMessageResponse {
217    content: Option<String>,
218    reasoning_content: Option<String>,
219}
220
221#[cfg(test)]
222mod tests {
223    use super::render_model_input_prompt;
224    use aios_spec::{
225        ContextSummary, ModelInput, RecentDecisionRecord, SanitizedEvent, SanitizedEventType,
226        ScriptHint, SemanticHint, SourceTier, StructuredContext, TextHint, UserBehaviorProfile,
227    };
228
229    #[test]
230    fn prompt_contains_model_memory_sections_without_raw_text() {
231        let input = ModelInput {
232            current_context: StructuredContext {
233                window_id: "w-memory".into(),
234                window_start_ms: 0,
235                window_end_ms: 1000,
236                duration_secs: 1,
237                events: vec![SanitizedEvent {
238                    event_id: "n1".into(),
239                    timestamp_ms: 1,
240                    event_type: SanitizedEventType::Notification {
241                        source_package: "com.chat".into(),
242                        category: None,
243                        channel_id: None,
244                        title_hint: TextHint {
245                            length_chars: 12,
246                            script: ScriptHint::Latin,
247                            is_emoji_only: false,
248                        },
249                        text_hint: TextHint {
250                            length_chars: 20,
251                            script: ScriptHint::Latin,
252                            is_emoji_only: false,
253                        },
254                        semantic_hints: vec![SemanticHint::FileMention],
255                        is_ongoing: false,
256                        group_key: None,
257                    },
258                    source_tier: SourceTier::PublicApi,
259                    app_package: Some("com.chat".into()),
260                    uid: None,
261                }],
262                summary: ContextSummary {
263                    foreground_apps: vec!["com.chat".into()],
264                    notified_apps: vec!["com.chat".into()],
265                    all_semantic_hints: vec![SemanticHint::FileMention],
266                    file_activity: vec![],
267                    latest_system_status: None,
268                    source_tier: SourceTier::PublicApi,
269                },
270            },
271            behavior_profile: UserBehaviorProfile {
272                user_id: None,
273                summary: "usually opens docs after chat notifications".into(),
274                observation_windows: 3,
275                frequent_foreground_apps: vec![],
276                frequent_notifying_apps: vec![],
277                frequent_semantic_hints: vec![],
278                action_successes: vec![],
279                action_denials: vec![],
280                action_failures: vec![],
281                last_updated_window_id: Some("w-prev".into()),
282            },
283            recent_feedback: vec![RecentDecisionRecord {
284                window_id: "w-prev".into(),
285                window_start_ms: 0,
286                window_end_ms: 1,
287                foreground_apps: vec!["com.chat".into()],
288                notified_apps: vec!["com.chat".into()],
289                semantic_hints: vec![SemanticHint::FileMention],
290                route: "CloudLlm".into(),
291                model: "test".into(),
292                intent_count: 1,
293                rationale_tags: vec!["attachment".into()],
294                backend_error: None,
295                action_outcomes: vec![],
296            }],
297        };
298
299        let prompt = render_model_input_prompt(&input).unwrap();
300        assert!(prompt.contains("model_input_json="));
301        assert!(prompt.contains("current_context"));
302        assert!(prompt.contains("behavior_profile"));
303        assert!(prompt.contains("recent_feedback"));
304        assert!(prompt.contains("usually opens docs"));
305        assert!(!prompt.contains("private raw notification body"));
306    }
307}