Groq and OR switch without save
All checks were successful
Android Build Final Fixed / build-android (push) Successful in 7m8s

This commit is contained in:
2026-01-27 06:29:46 +01:00
parent 20f6191385
commit 4b4331cd0c
4 changed files with 190 additions and 77 deletions

View File

@@ -12,6 +12,7 @@ pub struct RssConfig {
#[derive(Default)]
pub struct NewsState {
pub openrouter_key: Mutex<String>,
pub groq_key: Mutex<String>,
pub rss_config: Mutex<RssConfig>,
}
@@ -37,40 +38,38 @@ pub async fn save_openrouter_key(key: String, state: State<'_, NewsState>) -> Re
Ok(())
}
#[tauri::command]
pub async fn save_groq_key(key: String, state: State<'_, NewsState>) -> Result<(), String> {
let mut lock = state.groq_key.lock().map_err(|_| "Lock failed")?;
*lock = key.trim().to_string();
Ok(())
}
#[tauri::command]
pub async fn load_rss_config(
app: AppHandle,
state: State<'_, NewsState>,
) -> Result<Vec<String>, String> {
let path = get_config_path(&app);
// Initialisierung mit Standard-Feeds, falls Datei nicht existiert
if !path.exists() {
let default_urls = vec![
"https://www.nasa.gov/news-release/feed/".to_string(),
"https://www.heise.de/rss/heise-atom.xml".to_string(),
];
let config = RssConfig {
urls: default_urls.clone(),
};
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).map_err(|e| e.to_string())?;
}
let ron_str = ron::to_string(&config).map_err(|e| e.to_string())?;
fs::write(&path, ron_str).map_err(|e| e.to_string())?;
let mut lock = state.rss_config.lock().unwrap();
lock.urls = default_urls.clone();
return Ok(default_urls);
}
let content = fs::read_to_string(path).map_err(|e| e.to_string())?;
let config: RssConfig = ron::from_str(&content).map_err(|e| e.to_string())?;
let mut lock = state.rss_config.lock().unwrap();
*lock = config.clone();
Ok(config.urls)
@@ -84,40 +83,59 @@ pub async fn save_rss_urls(
) -> Result<(), String> {
let config = RssConfig { urls };
let path = get_config_path(&app);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).map_err(|e| e.to_string())?;
}
let ron_str = ron::to_string(&config).map_err(|e| e.to_string())?;
fs::write(path, ron_str).map_err(|e| e.to_string())?;
let mut lock = state.rss_config.lock().unwrap();
*lock = config;
Ok(())
}
#[tauri::command]
pub async fn fetch_ai_news(state: State<'_, NewsState>) -> Result<Vec<NewsArticle>, String> {
let key = state
.openrouter_key
.lock()
.map_err(|_| "Lock failed")?
.clone();
pub async fn fetch_ai_news(
provider: String,
state: State<'_, NewsState>,
) -> Result<Vec<NewsArticle>, String> {
let (key, url, model, author) = match provider.as_str() {
"groq" => {
let k = state.groq_key.lock().map_err(|_| "Lock failed")?.clone();
(
k,
"https://api.groq.com/openai/v1/chat/completions",
"llama-3.1-8b-instant",
"Groq AI",
)
}
_ => {
let k = state
.openrouter_key
.lock()
.map_err(|_| "Lock failed")?
.clone();
(
k,
"https://openrouter.ai/api/v1/chat/completions",
"openai/gpt-oss-20b:free:online",
"OpenRouter AI",
)
}
};
if key.is_empty() {
return Err("API Key fehlt.".into());
return Err(format!("API Key für {} fehlt.", author));
}
let client = reqwest::Client::new();
let response = client
.post("https://openrouter.ai/api/v1/chat/completions")
.post(url)
.header("Authorization", format!("Bearer {}", key))
.json(&serde_json::json!({
"model": "minimax/minimax-m2.1",
"model": model,
"messages": [
{"role": "system", "content": "Kurze News, 1-2 Sätze, Deutsch."},
{"role": "user", "content": "Tech-News."}
{"role": "system", "content": "Kurze Tech-News, 1-2 Sätze, Deutsch. Nutze Markdown."},
{"role": "user", "content": "Was gibt es neues in der Tech-Welt?"}
]
}))
.send()
@@ -127,12 +145,12 @@ pub async fn fetch_ai_news(state: State<'_, NewsState>) -> Result<Vec<NewsArticl
let json: serde_json::Value = response.json().await.map_err(|e| e.to_string())?;
let content = json["choices"][0]["message"]["content"]
.as_str()
.unwrap_or("Fehler")
.unwrap_or("Fehler beim Abrufen der Inhalte")
.to_string();
Ok(vec![NewsArticle {
content,
author: "OpenRouter AI".into(),
author: author.into(),
created_at: "Gerade eben".into(),
}])
}
@@ -151,7 +169,6 @@ pub async fn fetch_rss_news(state: State<'_, NewsState>) -> Result<Vec<NewsArtic
.title
.map(|t| t.content)
.unwrap_or_else(|| "RSS".into());
for entry in feed.entries.into_iter().take(3) {
let date = entry
.published