diamond-in commited on
Commit
7ec5bef
·
verified ·
1 Parent(s): fb60fec

Update src/agent/mod.rs

Browse files
Files changed (1) hide show
  1. src/agent/mod.rs +20 -6
src/agent/mod.rs CHANGED
@@ -7,10 +7,24 @@ pub async fn chat_handler(
7
  State(state): State<Arc<AppState>>,
8
  Json(payload): Json<serde_json::Value>
9
  ) -> Json<serde_json::Value> {
10
- let _ = state.tx.send(json!({"type": "log", "data": "Thinking..."}).to_string());
11
-
12
- // Gemini API Call Logic here using reqwest
13
- // Use model ID: gemini-2.0-flash-exp (current flash preview ID)
14
-
15
- Json(json!({"status": "ok"}))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  }
 
7
  State(state): State<Arc<AppState>>,
8
  Json(payload): Json<serde_json::Value>
9
  ) -> Json<serde_json::Value> {
10
+ let user_msg = payload["message"].as_str().unwrap_or("");
11
+ let api_key = std::env::var("GEMINI_API_KEY").expect("Missing GEMINI_API_KEY");
12
+
13
+ let client = reqwest::Client::new();
14
+ let response = client.post(format!("https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent?key={}", api_key))
15
+ .json(&json!({
16
+ "contents": [{ "parts": [{ "text": user_msg }] }]
17
+ }))
18
+ .send()
19
+ .await;
20
+
21
+ if let Ok(res) = response {
22
+ let body: serde_json::Value = res.json().await.unwrap();
23
+ let ai_text = body["candidates"][0]["content"]["parts"][0]["text"].as_str().unwrap_or("Error");
24
+
25
+ // Send log to UI via SSE
26
+ let _ = state.tx.send(json!({"type": "log", "data": ai_text}).to_string());
27
+ }
28
+
29
+ Json(json!({"status": "sent"}))
30
  }