refactor: 重构现代化命令管理为基于配置文件的安全方案

🎯 解决问题:
- 避免推荐用户系统中不存在的工具
- 防止因缺失工具导致的命令执行失败
- 提供更安全、更灵活的现代化命令管理

🔧 主要改进:
- 新增 ai_shell/modern_commands.toml 配置文件
- 智能检测系统中已安装的现代化工具
- 只推荐实际可用的工具,安全回退到原始命令
- 完整的工具描述、分类和安装提示

📦 配置文件特性:
- 28 个命令映射配置
- 20 个工具描述说明
- 8 个工具分类组织
- 6 个详细安装提示

🛠️ 新增管理工具:
- scripts/manage_modern_commands.py 配置管理脚本
- 支持验证、列表、安装建议等功能
- 完整的配置状态检查和报告

🔍 用户体验优化:
- ai --config 显示详细的工具状态
- 区分已启用、保持原样、未安装的工具
- 提供具体的安装命令和说明
- 支持环境变量和配置文件自定义

🛡️ 安全保障:
- 绝不推荐不存在的工具
- 优雅降级到原始命令
- 保持完全向后兼容性

📋 技术实现:
- 添加 tomli 依赖支持 TOML 解析
- 重构配置加载逻辑
- 智能工具检测和状态管理
- 完善的错误处理和回退机制
This commit is contained in:
2025-07-12 22:44:31 +08:00
parent c4d1510ce9
commit afbbb1fbb0
7 changed files with 614 additions and 87 deletions

View File

@ -81,7 +81,8 @@ def create_parser() -> argparse.ArgumentParser:
def show_config() -> None:
"""Show current configuration"""
from .config import (get_api_key, get_base_url, get_model, get_timeout,
get_max_retries, validate_config, get_available_modern_commands)
get_max_retries, validate_config, get_available_modern_commands,
get_missing_modern_commands, get_installation_hint)
print("AI Shell Configuration:")
print(f" Model: {get_model()}")
@ -115,26 +116,40 @@ def show_config() -> None:
# Show modern commands configuration
modern_commands = get_available_modern_commands()
if modern_commands:
print(f"\n现代化命令替代 ({len(modern_commands)} 个可用):")
missing_commands = get_missing_modern_commands()
print(f"\n现代化命令替代:")
if modern_commands:
# Group commands for better display
active_alternatives = {k: v for k, v in modern_commands.items() if k != v}
unchanged_commands = {k: v for k, v in modern_commands.items() if k == v}
if active_alternatives:
print(" 已启用的替代:")
print(f" 已启用的替代 ({len(active_alternatives)} 个):")
for old_cmd, new_cmd in sorted(active_alternatives.items()):
print(f" {old_cmd}{new_cmd}")
if unchanged_commands:
print(f" 保持原样: {', '.join(sorted(unchanged_commands.keys()))}")
print(f" ➡️ 保持原样: {', '.join(sorted(unchanged_commands.keys()))}")
print("\n 自定义配置格式:")
print(" export AI_SHELL_MODERN_COMMANDS=\"old1:new1,old2:new2\"")
print(" 例如: export AI_SHELL_MODERN_COMMANDS=\"ls:exa,cat:bat,find:fd\"")
else:
print("\n现代化命令替代: 未检测到可用的现代化工具")
if missing_commands:
print(f"\n ⚠️ 可配置但未安装的工具 ({len(missing_commands)} 个):")
for old_cmd, new_cmd in sorted(missing_commands.items()):
hint = get_installation_hint(new_cmd)
if hint:
print(f" {old_cmd}{new_cmd}")
print(f" 安装: {hint.split('#')[0].strip()}")
else:
print(f" {old_cmd}{new_cmd}")
if not modern_commands and not missing_commands:
print(" 未检测到可用的现代化工具配置")
print("\n 配置方法:")
print(" 1. 编辑配置文件: ai_shell/modern_commands.toml")
print(" 2. 环境变量: export AI_SHELL_MODERN_COMMANDS=\"ls:eza,cat:bat\"")
print(" 3. .env 文件: AI_SHELL_MODERN_COMMANDS=\"ls:eza,cat:bat\"")
def main() -> None:
"""Main entry point"""