Skip to main content

aios_agent/backends/cloud_llm/
config.rs

1use std::env;
2
3const ENV_ENABLED: &str = "DIPECS_CLOUD_LLM_ENABLED";
4const ENV_PROVIDER: &str = "DIPECS_CLOUD_LLM_PROVIDER";
5const ENV_ENDPOINT: &str = "DIPECS_CLOUD_LLM_ENDPOINT";
6const ENV_MODEL: &str = "DIPECS_CLOUD_LLM_MODEL";
7const ENV_API_KEY: &str = "DIPECS_CLOUD_LLM_API_KEY";
8const ENV_TIMEOUT_SECS: &str = "DIPECS_CLOUD_LLM_TIMEOUT_SECS";
9const ENV_TEMPERATURE: &str = "DIPECS_CLOUD_LLM_TEMPERATURE";
10const ENV_SYSTEM_PROMPT: &str = "DIPECS_CLOUD_LLM_SYSTEM_PROMPT";
11const ENV_REASONING_EFFORT: &str = "DIPECS_CLOUD_LLM_REASONING_EFFORT";
12const ENV_ENABLE_THINKING: &str = "DIPECS_CLOUD_LLM_ENABLE_THINKING";
13const ENV_DEEPSEEK_API_KEY: &str = "DEEPSEEK_API_KEY";
14const ENV_DASHSCOPE_API_KEY: &str = "DASHSCOPE_API_KEY";
15
16const DEFAULT_TIMEOUT_SECS: u64 = 15;
17const DEFAULT_TEMPERATURE: f32 = 0.1;
18const DEFAULT_DEEPSEEK_ENDPOINT: &str = "https://api.deepseek.com/chat/completions";
19const DEFAULT_QWEN_ENDPOINT: &str =
20    "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";
21pub const DEFAULT_SYSTEM_PROMPT: &str = r#"You are the decision backend for DiPECS.
22Return only valid JSON with this shape:
23{
24  "intents": [
25    {
26      "intent_type": "OpenApp|SwitchToApp|CheckNotification|HandleFile|EnterContext|Idle",
27      "target": "optional string",
28      "extension_category": "Document|Image|Video|Audio|Archive|Code|Other|Unknown",
29      "confidence": 0.0,
30      "risk_level": "Low|Medium|High",
31      "actions": [
32        {
33          "action_type": "PreWarmProcess|PrefetchFile|KeepAlive|ReleaseMemory|NoOp",
34          "target": "optional string",
35          "urgency": "Immediate|IdleTime|Deferred"
36        }
37      ],
38      "rationale_tags": ["short_tag"]
39    }
40  ]
41}
42
43Rules:
44- Return JSON only, no markdown fences.
45- Use at most 3 intents.
46- If uncertain, return one Idle intent with one NoOp action.
47- Intent type selection:
48  * HandleFile: when file_activity is present or notification references a file.
49  * CheckNotification: for notifications (especially with semantic_hints like
50    LinkAttachment, FinancialContext, VerificationCode, FileMention).
51  * OpenApp/SwitchToApp: for foreground app transitions.
52  * Idle: when no actionable signal exists.
53- Action selection based on semantic hints and context:
54  * PreWarmProcess when notified_apps have semantic_hints (LinkAttachment,
55    FinancialContext, VerificationCode, FileMention) — user will likely
56    switch to that app soon.
57  * PrefetchFile when file_activity is present with a specific extension.
58  * ReleaseMemory when battery is low and not charging.
59  * KeepAlive when charging or when system needs sustained background work.
60  * NoOp only when there is genuinely no actionable signal.
61- For PrefetchFile targets, use the app package name (e.g. `com.example.chat`)
62  or a concrete Android bridge target (`url:https://...`, `uri:content://...`).
63- For PreWarmProcess, use `own:resources` or `pkg:<observed.package>`.
64- For KeepAlive, use `work:collector_heartbeat`.
65- For ReleaseMemory, use `cache:prefetch`.
66- Use short snake_case rationale tags.
67- The user message contains `model_input_json` with:
68  - `current_context`: the current sanitized window (events, summary).
69  - `behavior_profile`: long-running privacy-preserving habit summary.
70  - `recent_feedback`: recent decisions plus local policy/execution outcomes.
71- Prefer current_context for immediate facts, use behavior_profile for stable
72  tendencies, and use recent_feedback to avoid repeating recently denied,
73  failed, or low-value actions.
74"#;
75
76pub(super) fn cloud_llm_enabled() -> bool {
77    read_bool_var(ENV_ENABLED).unwrap_or(false)
78}
79
80#[derive(Debug, Clone)]
81pub struct CloudLlmConfig {
82    pub provider: CloudProvider,
83    pub endpoint: String,
84    pub model: String,
85    pub api_key: Option<String>,
86    pub timeout_secs: u64,
87    pub temperature: f32,
88    pub system_prompt: String,
89    pub reasoning_effort: Option<String>,
90    pub enable_thinking: Option<bool>,
91}
92
93impl CloudLlmConfig {
94    pub fn from_env() -> Result<Self, String> {
95        Self::from_env_inner()
96    }
97
98    fn from_env_inner() -> Result<Self, String> {
99        let provider = read_var(ENV_PROVIDER)
100            .as_deref()
101            .map(CloudProvider::parse)
102            .transpose()?
103            .unwrap_or(CloudProvider::DeepSeek);
104
105        let endpoint =
106            read_var(ENV_ENDPOINT).unwrap_or_else(|| provider.default_endpoint().to_string());
107        if endpoint.is_empty() {
108            return Err(format!(
109                "{ENV_ENDPOINT} is required when cloud LLM is enabled"
110            ));
111        }
112
113        let model = read_var(ENV_MODEL).unwrap_or_else(|| provider.default_model().to_string());
114        if model.is_empty() {
115            return Err(format!("{ENV_MODEL} is required when cloud LLM is enabled"));
116        }
117
118        Ok(Self {
119            provider,
120            endpoint,
121            model,
122            api_key: provider
123                .api_key_candidates()
124                .iter()
125                .find_map(|key| read_var(key)),
126            timeout_secs: read_u64_var(ENV_TIMEOUT_SECS).unwrap_or(DEFAULT_TIMEOUT_SECS),
127            temperature: read_f32_var(ENV_TEMPERATURE).unwrap_or(DEFAULT_TEMPERATURE),
128            system_prompt: read_var(ENV_SYSTEM_PROMPT)
129                .unwrap_or_else(|| DEFAULT_SYSTEM_PROMPT.to_string()),
130            reasoning_effort: read_var(ENV_REASONING_EFFORT),
131            enable_thinking: read_bool_var(ENV_ENABLE_THINKING),
132        })
133    }
134
135    /// Test/baseline-only constructor for latency/benchmark tests against a real provider.
136    #[cfg(test)]
137    pub(crate) fn new_for_test(
138        provider: CloudProvider,
139        endpoint: impl Into<String>,
140        model: impl Into<String>,
141        api_key: impl Into<String>,
142    ) -> Self {
143        Self {
144            provider,
145            endpoint: endpoint.into(),
146            model: model.into(),
147            api_key: Some(api_key.into()),
148            timeout_secs: DEFAULT_TIMEOUT_SECS,
149            temperature: DEFAULT_TEMPERATURE,
150            system_prompt: DEFAULT_SYSTEM_PROMPT.to_string(),
151            reasoning_effort: None,
152            enable_thinking: None,
153        }
154    }
155}
156
157#[derive(Debug, Clone, Copy, PartialEq, Eq)]
158pub enum CloudProvider {
159    GenericOpenAiCompatible,
160    DeepSeek,
161    Qwen,
162}
163
164impl CloudProvider {
165    pub(super) fn parse(raw: &str) -> Result<Self, String> {
166        match raw.trim().to_ascii_lowercase().as_str() {
167            "generic" | "openai-compatible" | "openai_compatible" | "openai" => {
168                Ok(Self::GenericOpenAiCompatible)
169            },
170            "deepseek" => Ok(Self::DeepSeek),
171            "qwen" | "dashscope" => Ok(Self::Qwen),
172            _ => Err(format!(
173                "unsupported DIPECS_CLOUD_LLM_PROVIDER: {raw} (expected generic, deepseek, or qwen)"
174            )),
175        }
176    }
177
178    fn default_endpoint(self) -> &'static str {
179        match self {
180            Self::GenericOpenAiCompatible => "",
181            Self::DeepSeek => DEFAULT_DEEPSEEK_ENDPOINT,
182            Self::Qwen => DEFAULT_QWEN_ENDPOINT,
183        }
184    }
185
186    fn default_model(self) -> &'static str {
187        match self {
188            Self::GenericOpenAiCompatible => "",
189            Self::DeepSeek => "deepseek-v4-flash",
190            Self::Qwen => "qwen-plus",
191        }
192    }
193
194    fn api_key_candidates(self) -> &'static [&'static str] {
195        match self {
196            Self::GenericOpenAiCompatible => &[ENV_API_KEY],
197            Self::DeepSeek => &[ENV_API_KEY, ENV_DEEPSEEK_API_KEY],
198            Self::Qwen => &[ENV_API_KEY, ENV_DASHSCOPE_API_KEY],
199        }
200    }
201}
202
203fn read_var(key: &str) -> Option<String> {
204    env::var(key).ok().filter(|value| !value.is_empty())
205}
206
207fn read_bool_var(key: &str) -> Option<bool> {
208    read_var(key).and_then(|value| parse_bool(&value))
209}
210
211fn read_u64_var(key: &str) -> Option<u64> {
212    read_var(key).and_then(|value| value.parse().ok())
213}
214
215fn read_f32_var(key: &str) -> Option<f32> {
216    read_var(key).and_then(|value| value.parse().ok())
217}
218
219fn parse_bool(value: &str) -> Option<bool> {
220    match value.trim().to_ascii_lowercase().as_str() {
221        "1" | "true" | "yes" | "on" => Some(true),
222        "0" | "false" | "no" | "off" => Some(false),
223        _ => None,
224    }
225}
226
227#[cfg(test)]
228mod tests {
229    use super::{parse_bool, CloudProvider};
230
231    #[test]
232    fn provider_parser_accepts_known_values() {
233        assert_eq!(
234            CloudProvider::parse("deepseek").unwrap(),
235            CloudProvider::DeepSeek
236        );
237        assert_eq!(CloudProvider::parse("qwen").unwrap(), CloudProvider::Qwen);
238        assert_eq!(
239            CloudProvider::parse("openai-compatible").unwrap(),
240            CloudProvider::GenericOpenAiCompatible
241        );
242    }
243
244    #[test]
245    fn bool_parser_accepts_common_values() {
246        assert_eq!(parse_bool("true"), Some(true));
247        assert_eq!(parse_bool("1"), Some(true));
248        assert_eq!(parse_bool("false"), Some(false));
249        assert_eq!(parse_bool("0"), Some(false));
250    }
251}