浏览代码

【新增,配置】重构配置管理逻辑

- 新增 ConfigTools 类,实现通用的深度合并功能
- 重构 Config 和 ServerConfig 类的 merge_config 方法
- 更新 BaseServer 类的初始化逻辑,支持配置优先级
- 修改 yaa.py,支持单独的服务配置文件
PJ568 1 年之前
父节点
当前提交
3643e37675
共有 7 个文件被更改,包括 78 次插入 和 53 次删除
  1. 1 1
      VERSION
  2. 10 24
      yaa/Config/Config.py
  3. 20 0
      yaa/Config/ConfigTools.py
  4. 36 26
      yaa/Config/ServerConfig.py
  5. 3 1
      yaa/Config/__init__.py
  6. 5 1
      yaa/Server/BaseServer.py
  7. 3 0
      yaa/yaa.py

+ 1 - 1
VERSION

@@ -1 +1 @@
-1.0.3
+1.1.0

+ 10 - 24
yaa/Config/Config.py

@@ -5,6 +5,8 @@
 2. 管理配置加载优先级(默认配置 < 运行时配置 < 用户配置)
 """
 
+from yaa.Config.ConfigTools import ConfigTools
+
 class Config:
     """系统默认配置类"""
     
@@ -69,7 +71,7 @@ class Config:
         }
 
     @classmethod
-    def merge_config(cls, session_data=None):
+    def merge_config(cls, session_data=None, lower_priority_config=None):
         """合并用户配置和默认配置
         
         参数:
@@ -81,10 +83,14 @@ class Config:
         if session_data is None:
             raise ValueError('未提供会话数据')
 
+        # 新增:处理默认参数
+        if lower_priority_config is None:
+            lower_priority_config = cls.get_default_config()
+
         # 从 session_data 中取出用户配置
         user_config = session_data.get('config', {})
         
-        session_data['config'] = cls._deep_merge(cls.get_default_config(), user_config)
+        session_data['config'] = ConfigTools.deep_merge(lower_priority_config, user_config)
 
         return session_data
     
@@ -105,7 +111,7 @@ class Config:
         if runtime_config['config'] is None:
             raise ValueError('未提供运行时配置')
         
-        merged_config = cls._deep_merge(cls.get_default_config(), runtime_config['config'])
+        merged_config = ConfigTools.deep_merge(cls.get_default_config(), runtime_config['config'])
 
         # 更新 YAA 配置
         cls.YAA_CONFIG = {**cls.YAA_CONFIG, **merged_config['yaa']}
@@ -117,24 +123,4 @@ class Config:
         cls.PROMPT_CONFIG = {**cls.PROMPT_CONFIG, **merged_config['prompt']}
         
         # 更新工具调用配置
-        cls.TOOL_CONFIG = {**cls.TOOL_CONFIG, **merged_config['tool']}
-
-    @classmethod
-    def _deep_merge(cls, default_dict, target_dict):
-            """深度合并两个字典,用户字典优先
-            
-            参数:
-                default_dict (dict): 默认配置字典
-                target_dict (dict): 目标配置字典
-                
-            返回:
-                dict: 合并后的字典
-            """
-            merged = default_dict.copy()
-            for key, value in target_dict.items():
-                if (key in merged and isinstance(merged[key], dict)
-                    and isinstance(value, dict)):
-                    merged[key] = cls._deep_merge(merged[key], value)
-                else:
-                    merged[key] = value
-            return merged
+        cls.TOOL_CONFIG = {**cls.TOOL_CONFIG, **merged_config['tool']}

+ 20 - 0
yaa/Config/ConfigTools.py

@@ -0,0 +1,20 @@
+class ConfigTools:
+    @classmethod
+    def deep_merge(cls, lower_priority_config, higher_priority_config):
+            """深度合并两个字典
+            
+            参数:
+                lower_priority_config (dict): 基底配置字典
+                higher_priority_config (dict): 目标配置字典
+                
+            返回:
+                dict: 合并后的字典
+            """
+            merged = lower_priority_config.copy()
+            for key, value in higher_priority_config.items():
+                if (key in merged and isinstance(merged[key], dict)
+                    and isinstance(value, dict)):
+                    merged[key] = cls.deep_merge(merged[key], value)
+                else:
+                    merged[key] = value
+            return merged

+ 36 - 26
yaa/Config/ServerConfig.py

@@ -4,6 +4,8 @@
 1. 提供服务端相关的默认配置项
 """
 
+from yaa.Config.ConfigTools import ConfigTools
+
 class ServerConfig:
     """服务端默认配置类"""
     
@@ -36,40 +38,48 @@ class ServerConfig:
             'yaa_server': cls.YAA_SERVER_CONFIG,
             'security': cls.SECURITY_CONFIG
         }
-    
-    @classmethod
-    def _deep_merge(cls, default_dict, target_dict):
-            """深度合并两个字典,用户字典优先
-            
-            参数:
-                default_dict (dict): 默认配置字典
-                target_dict (dict): 目标配置字典
-                
-            返回:
-                dict: 合并后的字典
-            """
-            merged = default_dict.copy()
-            for key, value in target_dict.items():
-                if (key in merged and isinstance(merged[key], dict)
-                    and isinstance(value, dict)):
-                    merged[key] = cls._deep_merge(merged[key], value)
-                else:
-                    merged[key] = value
-            return merged
 
     @classmethod
-    def merge_config(cls, args_config=None):
+    def merge_config(cls, higher_priority_config=None, lower_priority_config=None):
         """合并用户配置和默认配置
         
         参数:
-            args_config (dict): 服务器运行时参数传入编译的配置字典
+            higher_priority_config (dict): 服务器运行时参数传入编译的配置字典
             
         返回:
             dict: 合并后的配置字典,参数配置优先于默认配置
         """
-        if args_config is None:
-            args_config = {}
+        if lower_priority_config is None:
+            lower_priority_config = cls.get_default_config()
+
+        if higher_priority_config is None:
+            higher_priority_config = {}
             
-        merged_config = cls._deep_merge(cls.get_default_config(), args_config)
+        merged_config = ConfigTools.deep_merge(lower_priority_config, higher_priority_config)
+        
+        return merged_config
+    
+    @classmethod
+    def update_config(cls, runtime_config_path=None):
+        """更新默认配置(运行时配置优先)
+        
+        参数:
+            runtime_config_path (src): 运行时提供的服务设置 json 的路径
+        """
+        if runtime_config_path is None:
+            raise ValueError('未提供运行时配置路径')
+
+        import json
+
+        runtime_config = json.load(open(runtime_config_path, 'r'))
+
+        if runtime_config is None:
+            raise ValueError('未提供运行时配置')
+        
+        merged_config = ConfigTools.deep_merge(cls.get_default_config(), runtime_config)
+
+        # 更新 YAA 服务配置
+        cls.YAA_SERVER_CONFIG = {**cls.YAA_SERVER_CONFIG, **merged_config['yaa_server']}
         
-        return merged_config
+        # 更新安全相关配置
+        cls.SECURITY_CONFIG = {**cls.SECURITY_CONFIG, **merged_config['security']}

+ 3 - 1
yaa/Config/__init__.py

@@ -1,8 +1,10 @@
 """yaa 配置模块"""
 from yaa.Config.Config import Config
 from yaa.Config.ServerConfig import ServerConfig
+from yaa.Config.ConfigTools import ConfigTools
 
 __all__ = [
     "Config",
-    "ServerConfig"
+    "ServerConfig",
+    "ConfigTools"
 ]

+ 5 - 1
yaa/Server/BaseServer.py

@@ -8,8 +8,10 @@
 
 import socket
 import json
+
 from yaa.Agent.BaseAgent import Agent
 from yaa.Auth.KeyAuth import Auth
+from yaa.Config.ServerConfig import ServerConfig
 
 class BaseServer:
     def __init__(self, config=None):
@@ -21,7 +23,9 @@ class BaseServer:
                 - security: 安全配置
         """
         if config is None:
-            config = {}
+            config = ServerConfig.get_default_config()
+        else:
+            config = ServerConfig.merge_config(config)
             
         server_config = config.get('yaa_server', {})
         self.host = server_config.get('ip', 'localhost')

+ 3 - 0
yaa/yaa.py

@@ -22,6 +22,7 @@ def main():
     group.add_argument('--run', nargs='?', const='', help='传入命令到智能体')
     group.add_argument('--serve', action='store_true', help='启动服务模式')
     parser.add_argument('--config', type=str, help='配置文件路径')
+    parser.add_argument('--server-config', type=str, help='服务配置文件路径')
 
     # serve 模式的可选参数
     parser.add_argument('--port', type=int, default=12345, help='服务端口号,默认 12345')
@@ -31,6 +32,8 @@ def main():
     # 如果指定了配置文件,则加载配置文件
     if args.config:
         Config.update_config(args.config)
+    if args.server_config:
+        ServerConfig.update_config(args.server_config)
     if args.serve:
         try:
             # 构建运行时配置