""" 完整工具系统使用示例 演示基础工具和高级工具的使用。 """ import asyncio from agent.tools.builtin import ( read_file, edit_file, write_file, bash_command, glob_files, grep_content ) async def demo_basic_tools(): """演示基础工具(Python 实现)""" print("=" * 60) print("基础工具演示") print("=" * 60) # 1. 读取文件 print("\n1. 读取文件") result = await read_file(file_path="README.md", limit=20) print(f"✅ {result.title}") print(f" 前 5 行: {result.output[:200]}...") # 2. 搜索文件 print("\n2. Glob 搜索") result = await glob_files(pattern="**/*.py", path="agent/tools") print(f"✅ {result.title}") print(f" 找到 {result.metadata['count']} 个文件") # 3. 内容搜索 print("\n3. Grep 搜索") result = await grep_content( pattern="async def", path="agent/tools/builtin", include="*.py" ) print(f"✅ {result.title}") print(f" 找到 {result.metadata['matches']} 个匹配") # 4. 执行命令 print("\n4. Bash 命令") result = await bash_command( command="git status --short", timeout=10 ) print(f"✅ {result.title}") print(f" 退出码: {result.metadata['exit_code']}") # 5. 编辑文件(演示智能匹配) print("\n5. 智能编辑(9 种策略)") # 创建测试文件 test_content = """ def hello(): print("Hello") def world(): print("World") """ await write_file(file_path="/tmp/test_edit.py", content=test_content) # 编辑:忽略缩进(会使用 IndentationFlexibleReplacer) result = await edit_file( file_path="/tmp/test_edit.py", old_string='def hello():\nprint("Hello")', # 缩进不同 new_string='def hello():\n print("Hello, World!")' ) print(f"✅ {result.title}") print(f" Diff:\n{result.metadata['diff'][:200]}...") async def demo_advanced_tools(): """演示高级工具(Bun 适配器)""" print("\n" + "=" * 60) print("高级工具演示(需要 Bun)") print("=" * 60) try: from agent.tools.advanced import webfetch, lsp_diagnostics # 1. 网页抓取 print("\n1. 网页抓取 (HTML -> Markdown)") result = await webfetch( url="https://example.com", format="markdown" ) print(f"✅ {result.title}") print(f" 内容长度: {len(result.output)} 字符") # 2. LSP 诊断 print("\n2. LSP 诊断") result = await lsp_diagnostics( file_path="agent/tools/builtin/edit.py" ) print(f"✅ {result.title}") print(f" 诊断结果: {result.output[:200]}...") except Exception as e: print(f"⚠️ 高级工具需要 Bun 运行时: {e}") print(" 安装: curl -fsSL https://bun.sh/install | bash") async def demo_edit_strategies(): """演示 edit_file 的 9 种匹配策略""" print("\n" + "=" * 60) print("edit_file 策略演示") print("=" * 60) test_cases = [ { "name": "策略 1: 精确匹配", "content": "DEBUG = True\nVERBOSE = False", "old": "DEBUG = True", "new": "DEBUG = False" }, { "name": "策略 2: 忽略行首尾空白", "content": " DEBUG = True \nVERBOSE = False", "old": "DEBUG = True", # 无空白 "new": "DEBUG = False" }, { "name": "策略 4: 空白归一化", "content": "DEBUG = True", "old": "DEBUG = True", # 单空格 "new": "DEBUG = False" }, { "name": "策略 5: 灵活缩进", "content": """ def foo(): if True: print("hello") """, "old": "if True:\nprint(\"hello\")", # 无缩进 "new": "if True:\n print(\"world\")" } ] for i, test in enumerate(test_cases, 1): print(f"\n{i}. {test['name']}") # 创建测试文件 test_file = f"/tmp/test_strategy_{i}.py" await write_file(file_path=test_file, content=test["content"]) # 执行编辑 try: result = await edit_file( file_path=test_file, old_string=test["old"], new_string=test["new"] ) print(f" ✅ 成功匹配") except Exception as e: print(f" ❌ 失败: {e}") async def main(): """运行所有演示""" print("\n🚀 工具系统完整演示\n") # 基础工具 await demo_basic_tools() # 编辑策略 await demo_edit_strategies() # 高级工具 await demo_advanced_tools() print("\n" + "=" * 60) print("演示完成!") print("=" * 60) if __name__ == "__main__": asyncio.run(main())