Ver código fonte

【新增】实现配置文件与请求数据的深度合并

- 新增处理请求数据的逻辑,与配置文件进行深度合并
- 优化了配置加载和处理的代码结构
- 增加了错误处理和日志输出,提高了系统的健壮性和可调试性
PJ568 1 ano atrás
pai
commit
38e42b4a1a
3 arquivos alterados com 92 adições e 29 exclusões
  1. 1 1
      Cargo.lock
  2. 1 1
      Cargo.toml
  3. 90 27
      src/main.rs

+ 1 - 1
Cargo.lock

@@ -2433,7 +2433,7 @@ checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51"
 
 [[package]]
 name = "yaa"
-version = "2.0.2"
+version = "2.1.0"
 dependencies = [
  "actix-web",
  "anyhow",

+ 1 - 1
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "yaa"
-version = "2.0.2"
+version = "2.1.0"
 edition = "2024"
 
 [dependencies]

+ 90 - 27
src/main.rs

@@ -1,17 +1,17 @@
 mod agent;
 mod cli;
 mod core;
-use actix_web::{web, App, HttpServer};
-use agent::{process_session, api::OpenAIClient};
-use clap::Parser;
-use core::{session::SessionData, tool::ToolRegistry};
+use actix_web::{App, HttpServer, web};
 use agent::tools::finish::FinishTool;
 use agent::tools::rethink::RethinkTool;
+use agent::{api::OpenAIClient, process_session};
+use clap::Parser;
 use cli::commands::{Commands, handle_command};
+use core::{session::{SessionData, Config}, tool::ToolRegistry};
 use tokio;
 
-use std::fs;
 use anyhow::{Context, Result};
+use std::fs;
 
 #[derive(Parser, Debug)]
 #[command(version, about)]
@@ -35,28 +35,28 @@ struct Args {
 fn load_config(path: Option<&str>) -> Result<SessionData> {
     // 创建默认配置
     let mut session_data = SessionData::default();
-    
+
     // 如果指定了配置文件,则加载并深度合并用户配置
     if let Some(config_path) = path {
         let config_content = fs::read_to_string(config_path)
             .with_context(|| format!("Failed to read config file: {}", config_path))?;
-        
+
         // 反序列化用户配置
         let user_config: serde_json::Value = serde_json::from_str(&config_content)
             .with_context(|| format!("Failed to parse config file: {}", config_path))?;
-        
+
         // 将默认配置转换为Value以便合并
         let mut default_config = serde_json::to_value(&session_data.config)?;
-        
+
         // 深度合并配置
         if let Some(user_config) = user_config.get("config") {
             merge_json(&mut default_config, user_config);
         }
-        
+
         // 转换回Config结构体
         session_data.config = serde_json::from_value(default_config)?;
     }
-    
+
     Ok(session_data)
 }
 
@@ -77,29 +77,90 @@ fn merge_json(target: &mut serde_json::Value, source: &serde_json::Value) {
     }
 }
 
-async fn handle_request(session_data: web::Json<SessionData>) -> actix_web::HttpResponse {
+async fn handle_request(
+    app_data: web::Data<SessionData>,
+    request_data: web::Json<serde_json::Value>
+) -> actix_web::HttpResponse {
+    println!("[DEBUG] 接收到的原始数据:{:?}", request_data);
+
     let mut tool_registry = ToolRegistry::new();
     tool_registry.register(FinishTool);
     tool_registry.register(RethinkTool);
 
+    // 使用从 load_config 加载的配置作为基础
+    let mut merged_data = (*app_data.into_inner()).clone();
+
+    // 获取传入数据并检查是否有 config 字段
+    let mut user_data = request_data.into_inner();
+    if !user_data.as_object().unwrap().contains_key("config") {
+        // 如果请求中没有 config 字段,使用 load_config 加载的配置
+        user_data["config"] = serde_json::to_value(&merged_data.config).unwrap();
+    }
+
+    // 将基础配置转换为 JSON Value
+    let mut base_data = match serde_json::to_value(&merged_data) {
+        Ok(v) => v,
+        Err(e) => {
+            return actix_web::HttpResponse::InternalServerError()
+                .content_type("application/json")
+                .body(serde_json::json!({
+                    "messages": [{
+                        "role": "system",
+                        "content": format!("Failed to serialize base config: {}", e)
+                    }]
+                }).to_string());
+        }
+    };
+
+    // 深度合并配置
+    merge_json(&mut base_data, &user_data);
+
+    // 转换回 SessionData 结构体
+    match serde_json::from_value::<SessionData>(base_data) {
+        Ok(v) => merged_data = v,
+        Err(e) => {
+            return actix_web::HttpResponse::BadRequest()
+                .content_type("application/json")
+                .body(serde_json::json!({
+                    "messages": [{
+                        "role": "system",
+                        "content": format!("Invalid request data: {}", e)
+                    }]
+                }).to_string());
+        }
+    };
+
+    println!("[DEBUG] 合并后的数据:{:?}", merged_data);
+
     let client = OpenAIClient::new(
-        session_data.config.llm_api.provider.api_key.to_string(),
-        Some(session_data.config.llm_api.provider.api_url.to_string()),
+        merged_data.config.llm_api.provider.api_key.to_string(),
+        Some(merged_data.config.llm_api.provider.api_url.to_string()),
     );
 
-    match process_session(session_data.into_inner(), &tool_registry, &client).await {
-        Ok(response) => {
-            match serde_json::to_string(&response) {
-                Ok(json) => actix_web::HttpResponse::Ok()
-                    .content_type("application/json")
-                    .body(json),
-                Err(e) => actix_web::HttpResponse::InternalServerError()
-                    .body(format!("Failed to serialize response: {}", e))
-            }
+    match process_session(merged_data, &tool_registry, &client).await {
+        Ok(response) => match serde_json::to_string(&response) {
+            Ok(json) => actix_web::HttpResponse::Ok()
+                .content_type("application/json")
+                .body(json),
+            Err(e) => actix_web::HttpResponse::InternalServerError()
+                .content_type("application/json")
+                .body(serde_json::json!({
+                    "messages": [{
+                        "role": "system",
+                        "content": format!("Failed to serialize response: {}", e)
+                    }]
+                }).to_string()),
         },
         Err(e) => {
             eprintln!("Agent Error: {}", e);
-            actix_web::HttpResponse::InternalServerError().body(e.to_string())
+            actix_web::HttpResponse::InternalServerError()
+                .content_type("application/json")
+                .body(serde_json::json!({
+                    "messages": [{
+                        "role": "system",
+                        "content": e.to_string()
+                    }]
+                }).to_string())
         }
     }
 }
@@ -129,12 +190,14 @@ async fn main() -> std::io::Result<()> {
             session_data.config.llm_api.provider.api_key.to_string(),
             Some(session_data.config.llm_api.provider.api_url.to_string()),
         );
-        
+
         if let Some(cmd) = args.command {
             handle_command(cmd).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
         } else {
-            cli::run_interactive(&tool_registry, &client, &session_data).await.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
+            cli::run_interactive(&tool_registry, &client, &session_data)
+                .await
+                .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
         }
         Ok(())
     }
-}
+}