feat: v3.6 重构 - 拆分 ai_cli.py 为模块化架构
本次更新由 mimo 自动化执行,与开发者无关。 新增 xiaoli/ 包: - __main__.py: 入口点 (python -m xiaoli) - app.py: 主应用薄编排层(~400行 vs 原3860行) - config.py: 配置管理 - engines.py: AI 引擎管理 - sandbox.py: 代码安全沙箱 - prompt.py: 系统提示词构建 - conversation/: 对话解析器 + 历史管理 - display/: CLI/TUI 双模式输出 改进: - 支持 argparse (--tui, --engine, --version) - 插件命令注册表 - 更新 README - 更新 .gitignore 原 ai_cli.py 保留向后兼容
This commit is contained in:
1 parent
95a0e4bc5c
commit
e1eee6a0c2
12 files changed
+1592
-65
No files matched your search
@@ -0,0 +1,70 @@
|
||||
"""配置管理 - 加载/保存/默认值"""
|
||||
import os
|
||||
import json
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
CONFIG_FILE = os.path.join(PROJECT_DIR, "config.json")
|
||||
|
||||
# 默认配置
|
||||
DEFAULTS = {
|
||||
"system": {
|
||||
"default_engine": "ollama",
|
||||
"max_history": 999999,
|
||||
"code_execution_timeout": 30,
|
||||
"max_file_size": 52428800,
|
||||
},
|
||||
"api": {"engines": {}},
|
||||
"plugins": {},
|
||||
}
|
||||
|
||||
_cache = None
|
||||
|
||||
|
||||
def load() -> dict:
|
||||
"""加载配置(带缓存)"""
|
||||
global _cache
|
||||
if _cache is not None:
|
||||
return _cache
|
||||
try:
|
||||
with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
|
||||
_cache = json.load(f)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
_cache = DEFAULTS.copy()
|
||||
return _cache
|
||||
|
||||
|
||||
def save(config: dict = None):
|
||||
"""保存配置"""
|
||||
global _cache
|
||||
cfg = config or _cache or DEFAULTS
|
||||
try:
|
||||
with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
|
||||
json.dump(cfg, f, ensure_ascii=False, indent=2)
|
||||
_cache = cfg
|
||||
except Exception as e:
|
||||
logger.error(f"保存配置失败: {e}")
|
||||
|
||||
|
||||
def get(key: str, default=None):
|
||||
"""获取系统配置项"""
|
||||
cfg = load()
|
||||
return cfg.get("system", {}).get(key, default)
|
||||
|
||||
|
||||
def set(key: str, value):
|
||||
"""设置系统配置项"""
|
||||
cfg = load()
|
||||
if "system" not in cfg:
|
||||
cfg["system"] = {}
|
||||
cfg["system"][key] = value
|
||||
save(cfg)
|
||||
|
||||
|
||||
def reload():
|
||||
"""强制重新加载"""
|
||||
global _cache
|
||||
_cache = None
|
||||
return load()
|
||||
Reference in new issue
Block a user