| #!/usr/bin/env python3 |
| """ |
| Claude Agent 增强版主入口文件 |
| 支持行编辑、历史翻查和SSHOUT集成 |
| """ |
| |
| import sys |
| import os |
| import asyncio |
| import click |
| from typing import Optional |
| |
| # 添加项目根目录到Python路径 |
| project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| sys.path.insert(0, os.path.join(project_root, 'src')) |
| |
| from claude_agent.cli.enhanced_interface import EnhancedCLIInterface |
| from claude_agent.core.agent import ThinkingMode |
| |
| |
| @click.command() |
| @click.option('--api-key', '-k', help='Claude API密钥') |
| @click.option('--interactive/--no-interactive', default=True, help='是否启动交互模式') |
| @click.option('--mode', type=click.Choice(['interactive', 'yolo']), default='interactive', help='思考模式') |
| @click.option('--sshout', is_flag=True, help='启动时自动连接SSHOUT') |
| @click.argument('message', required=False) |
| def main(api_key: Optional[str], interactive: bool, mode: str, sshout: bool, message: Optional[str]): |
| """Claude Agent 增强版命令行工具 |
| |
| 新功能: |
| - 行编辑和历史翻查 (↑/↓ 键) |
| - 命令自动补全 (Tab 键) |
| - SSHOUT 聊天室集成 |
| - @Claude 自动响应 |
| """ |
| |
| async def run(): |
| cli = EnhancedCLIInterface() |
| |
| try: |
| # 从环境变量获取API密钥 |
| api_key_final = api_key or os.getenv('CLAUDE_API_KEY') or os.getenv('ANTHROPIC_API_KEY') |
| if not api_key_final: |
| click.echo("警告: 未提供Claude API密钥,将使用默认配置") |
| |
| # 初始化 |
| await cli.initialize(api_key_final) |
| |
| # 设置模式 |
| thinking_mode = ThinkingMode.YOLO if mode == 'yolo' else ThinkingMode.INTERACTIVE |
| cli.agent.set_thinking_mode(thinking_mode) |
| |
| # 自动连接SSHOUT(如果指定) |
| if sshout: |
| click.echo("自动连接SSHOUT...") |
| await cli._sshout_connect() |
| |
| # 如果提供了消息且不是交互模式,直接处理 |
| if message and not interactive: |
| await cli.process_user_input(message) |
| else: |
| # 启动交互循环 |
| await cli.run_interactive_loop() |
| |
| except Exception as e: |
| click.echo(f"错误: {str(e)}") |
| finally: |
| await cli.shutdown() |
| |
| # 运行异步主函数 |
| asyncio.run(run()) |
| |
| |
| if __name__ == "__main__": |
| main() |