فهرست منبع

feat: 添加YYHDBL毕业设计项目HelloCodeAgentCli

yyhdbl 6 ماه پیش
والد
کامیت
b37c3f9aca
98فایلهای تغییر یافته به همراه18583 افزوده شده و 0 حذف شده
  1. 72 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.gitignore
  2. 9 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/backups/20251218_192253/testDemo/hello.html.bak
  3. 9 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/backups/20251218_200446/testDemo/hello.html.bak
  4. 9 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/backups/20251218_200920/testDemo/hello.html.bak
  5. 112 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/backups/20251218_201216/testDemo/hello.html.bak
  6. 112 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/backups/20251218_201409/testDemo/hello.html.bak
  7. 1 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/backups/20251219_192206/testDemo/我讨厌java.txt.bak
  8. 33 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251217_155009_0.md
  9. 26 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251217_155543_1.md
  10. 38 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251217_160215_2.md
  11. 32 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251217_160218_3.md
  12. 32 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_190919_4.md
  13. 34 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_191113_5.md
  14. 32 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_191343_6.md
  15. 32 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_191554_7.md
  16. 33 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_192121_8.md
  17. 85 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_192253_9.md
  18. 95 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_192612_10.md
  19. 96 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_192912_11.md
  20. 147 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_200446_12.md
  21. 157 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_200920_13.md
  22. 157 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_201216_14.md
  23. 24 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_201409_15.md
  24. 384 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_202749_16.md
  25. 21 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251219_150917_17.md
  26. 253 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251219_153949_18.md
  27. 161 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251219_161406_19.md
  28. 226 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251219_164354_20.md
  29. 374 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251219_171820_21.md
  30. 25 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251219_191656_22.md
  31. 24 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251219_192206_23.md
  32. 20 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/todos/todos.json.bak
  33. 376 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/README.md
  34. 66 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/__init__.py
  35. 7 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/agents/__init__.py
  36. 201 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/agents/plan_solve_agent.py
  37. 428 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/agents/react_agent.py
  38. 180 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/agents/reflection_agent.py
  39. 407 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/agents/simple_agent.py
  40. 57 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/README.md
  41. 6 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/__init__.py
  42. 5 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/agentic/__init__.py
  43. 346 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/agentic/code_agent.py
  44. 2 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/executors/__init__.py
  45. 512 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/executors/apply_patch_executor.py
  46. 220 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/hello_code_cli.py
  47. 38 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/prompts/README.md
  48. 18 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/prompts/plan.md
  49. 122 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/prompts/react.md
  50. 19 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/prompts/summarize_observation.md
  51. 78 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/prompts/system.md
  52. 50 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/prompts/tools.md
  53. 10 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/context/__init__.py
  54. 510 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/context/builder.py
  55. 19 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/core/__init__.py
  56. 46 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/core/agent.py
  57. 134 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/core/config.py
  58. 195 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/core/database_config.py
  59. 21 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/core/exceptions.py
  60. 321 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/core/llm.py
  61. 33 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/core/message.py
  62. 19 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/__init__.py
  63. 174 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/base.py
  64. 315 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/embedding.py
  65. 342 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/manager.py
  66. 65 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/rag/__init__.py
  67. 258 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/rag/document.py
  68. 1207 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/rag/pipeline.py
  69. 18 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/storage/__init__.py
  70. 462 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/storage/document_store.py
  71. 455 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/storage/neo4j_store.py
  72. 541 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/storage/qdrant_store.py
  73. 27 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/types/__init__.py
  74. 661 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/types/episodic.py
  75. 709 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/types/perceptual.py
  76. 1181 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/types/semantic.py
  77. 411 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/types/working.py
  78. 5 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/requirement.txt
  79. 10 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/__init__.py
  80. 184 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/async_executor.py
  81. 49 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/base.py
  82. 22 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/__init__.py
  83. 300 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/context_fetch_tool.py
  84. 119 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/mcp_wrapper_tool.py
  85. 453 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/memory_tool.py
  86. 518 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/note_tool.py
  87. 76 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/plan_tool.py
  88. 851 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/protocol_tools.py
  89. 264 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/search.py
  90. 724 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/terminal_tool.py
  91. 213 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/todo_tool.py
  92. 166 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/chain.py
  93. 220 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/registry.py
  94. 11 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/__init__.py
  95. 86 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/cli_ui.py
  96. 75 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/helpers.py
  97. 39 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/logging.py
  98. 61 0
      Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/serialization.py

+ 72 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.gitignore

@@ -0,0 +1,72 @@
+# Python
+__pycache__/
+*.py[cod]
+*$py.class
+*.so
+.Python
+venv/
+env/
+ENV/
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+wheels/
+*.egg-info/
+.installed.cfg
+*.egg
+
+# Environment
+.env
+.env.local
+.venv
+
+# IDE
+.vscode/
+.idea/
+*.swp
+*.swo
+*~
+
+# Logs
+logs/
+*.log
+
+# Database
+*.db
+*.sqlite
+*.sqlite3
+
+# Data
+data/
+*.csv
+*.json
+
+# OS
+.DS_Store
+Thumbs.db
+
+# Testing
+.pytest_cache/
+.coverage
+htmlcov/
+
+# Jupyter
+.ipynb_checkpoints/
+
+# Model files
+*.pth
+*.pt
+*.ckpt
+models/checkpoints/
+
+# Submission helper files (for reference only, not to be committed to hello-agents)
+PR.md
+提交文件说明.md

+ 9 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/backups/20251218_192253/testDemo/hello.html.bak

@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Hello</title>
+</head>
+<body>
+    hello
+</body>
+</html>

+ 9 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/backups/20251218_200446/testDemo/hello.html.bak

@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Hello</title>
+</head>
+<body>
+    hello
+</body>
+</html>

+ 9 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/backups/20251218_200920/testDemo/hello.html.bak

@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Hello</title>
+</head>
+<body>
+    hello
+</body>
+</html>

+ 112 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/backups/20251218_201216/testDemo/hello.html.bak

@@ -0,0 +1,112 @@
+   <title>Hello Website</title>
+   <style>
+       body {
+           margin: 0;
+           padding: 0;
+           font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
+           background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
+           min-height: 100vh;
+           display: flex;
+           flex-direction: column;
+           align-items: center;
+           justify-content: center;
+           color: white;
+           text-align: center;
+       }
+       .container {
+           max-width: 800px;
+           padding: 40px;
+           background-color: rgba(255, 255, 255, 0.1);
+           backdrop-filter: blur(10px);
+           border-radius: 20px;
+           box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
+           border: 1px solid rgba(255, 255, 255, 0.2);
+       }
+       h1 {
+           font-size: 3.5rem;
+           margin-bottom: 20px;
+           text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
+           background: linear-gradient(45deg, #ff9a9e, #fad0c4);
+           -webkit-background-clip: text;
+           -webkit-text-fill-color: transparent;
+           background-clip: text;
+       }
+       p {
+           font-size: 1.2rem;
+           line-height: 1.6;
+           margin-bottom: 30px;
+           opacity: 0.9;
+       }
+       .features {
+           display: flex;
+           flex-wrap: wrap;
+           justify-content: center;
+           gap: 20px;
+           margin: 30px 0;
+       }
+       .feature {
+           background: rgba(255, 255, 255, 0.15);
+           padding: 20px;
+           border-radius: 12px;
+           flex: 1;
+           min-width: 200px;
+           max-width: 250px;
+           transition: transform 0.3s ease, background 0.3s ease;
+       }
+       .feature:hover {
+           transform: translateY(-5px);
+           background: rgba(255, 255, 255, 0.25);
+       }
+       .feature h3 {
+           margin-top: 0;
+           color: #ffd166;
+       }
+       .cta-button {
+           display: inline-block;
+           padding: 15px 40px;
+           background: linear-gradient(45deg, #ff9a9e, #fad0c4);
+           color: #333;
+           text-decoration: none;
+           border-radius: 50px;
+           font-weight: bold;
+           font-size: 1.1rem;
+           margin-top: 20px;
+           transition: all 0.3s ease;
+           box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
+       }
+       .cta-button:hover {
+           transform: scale(1.05);
+           box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
+       }
+       footer {
+           margin-top: 40px;
+           font-size: 0.9rem;
+           opacity: 0.7;
+           border-top: 1px solid rgba(255, 255, 255, 0.2);
+           padding-top: 20px;
+           width: 100%;
+       }
+   </style>
+   <div class="container">
+       <h1>Hello World!</h1>
+       <p>Welcome to our simple yet beautiful website. This is a demonstration of how inline CSS can transform a basic HTML page into an attractive web experience.</p>
+       <div class="features">
+           <div class="feature">
+               <h3>Modern Design</h3>
+               <p>Clean layout with gradient backgrounds and smooth animations.</p>
+           </div>
+           <div class="feature">
+               <h3>Fully Responsive</h3>
+               <p>Adapts to different screen sizes for optimal viewing.</p>
+           </div>
+           <div class="feature">
+               <h3>Fast Loading</h3>
+               <p>All styles are inline for immediate rendering.</p>
+           </div>
+       </div>
+       <p>This page demonstrates the power of CSS to create engaging user interfaces without external dependencies.</p>
+       <a href="#" class="cta-button">Get Started</a>
+       <footer>
+           <p>© 2023 Simple Website Demo | All inline CSS | No external files required</p>
+       </footer>
+   </div>

+ 112 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/backups/20251218_201409/testDemo/hello.html.bak

@@ -0,0 +1,112 @@
+   <title>Hello Website</title>
+   <style>
+       body {
+           margin: 0;
+           padding: 0;
+           font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
+           background: linear-gradient(135deg, #4CAF50 0%, #2E7D32 100%);
+           min-height: 100vh;
+           display: flex;
+           flex-direction: column;
+           align-items: center;
+           justify-content: center;
+           color: white;
+           text-align: center;
+       }
+       .container {
+           max-width: 800px;
+           padding: 40px;
+           background-color: rgba(255, 255, 255, 0.1);
+           backdrop-filter: blur(10px);
+           border-radius: 20px;
+           box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
+           border: 1px solid rgba(255, 255, 255, 0.2);
+       }
+       h1 {
+           font-size: 3.5rem;
+           margin-bottom: 20px;
+           text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
+           background: linear-gradient(45deg, #ff9a9e, #fad0c4);
+           -webkit-background-clip: text;
+           -webkit-text-fill-color: transparent;
+           background-clip: text;
+       }
+       p {
+           font-size: 1.2rem;
+           line-height: 1.6;
+           margin-bottom: 30px;
+           opacity: 0.9;
+       }
+       .features {
+           display: flex;
+           flex-wrap: wrap;
+           justify-content: center;
+           gap: 20px;
+           margin: 30px 0;
+       }
+       .feature {
+           background: rgba(255, 255, 255, 0.15);
+           padding: 20px;
+           border-radius: 12px;
+           flex: 1;
+           min-width: 200px;
+           max-width: 250px;
+           transition: transform 0.3s ease, background 0.3s ease;
+       }
+       .feature:hover {
+           transform: translateY(-5px);
+           background: rgba(255, 255, 255, 0.25);
+       }
+       .feature h3 {
+           margin-top: 0;
+           color: #ffd166;
+       }
+       .cta-button {
+           display: inline-block;
+           padding: 15px 40px;
+           background: linear-gradient(45deg, #ff9a9e, #fad0c4);
+           color: #333;
+           text-decoration: none;
+           border-radius: 50px;
+           font-weight: bold;
+           font-size: 1.1rem;
+           margin-top: 20px;
+           transition: all 0.3s ease;
+           box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
+       }
+       .cta-button:hover {
+           transform: scale(1.05);
+           box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
+       }
+       footer {
+           margin-top: 40px;
+           font-size: 0.9rem;
+           opacity: 0.7;
+           border-top: 1px solid rgba(255, 255, 255, 0.2);
+           padding-top: 20px;
+           width: 100%;
+       }
+   </style>
+   <div class="container">
+       <h1>Hello World!</h1>
+       <p>Welcome to our simple yet beautiful website. This is a demonstration of how inline CSS can transform a basic HTML page into an attractive web experience.</p>
+       <div class="features">
+           <div class="feature">
+               <h3>Modern Design</h3>
+               <p>Clean layout with gradient backgrounds and smooth animations.</p>
+           </div>
+           <div class="feature">
+               <h3>Fully Responsive</h3>
+               <p>Adapts to different screen sizes for optimal viewing.</p>
+           </div>
+           <div class="feature">
+               <h3>Fast Loading</h3>
+               <p>All styles are inline for immediate rendering.</p>
+           </div>
+       </div>
+       <p>This page demonstrates the power of CSS to create engaging user interfaces without external dependencies.</p>
+       <a href="#" class="cta-button">Get Started</a>
+       <footer>
+           <p>© 2023 Simple Website Demo | All inline CSS | No external files required</p>
+       </footer>
+   </div>

+ 1 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/backups/20251219_192206/testDemo/我讨厌java.txt.bak

@@ -0,0 +1 @@
+我讨厌java

+ 33 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251217_155009_0.md

@@ -0,0 +1,33 @@
+---
+id: note_20251217_155009_0
+title: 发现问题: 你好...
+type: blocker
+tags: ["hello_agents_forStudy", "auto_detected", "session_20251217_154935"]
+created_at: 2025-12-17T15:50:09.222222
+updated_at: 2025-12-17T15:50:09.222235
+---
+
+# 发现问题: 你好...
+
+## 用户输入
+你好
+
+## 问题分析
+基于对项目结构的探索,我现在对这个代码库有了清晰的了解。这是一个名为 **HelloAgents** 的多智能体系统框架,专门用于构建代码助手和开发工具。
+
+## 项目概览
+
+### 1. 项目结构
+这是一个Python包,包含以下核心模块:
+- **`agents/`** - 智能体实现(SimpleAgent, ReActAgent, ReflectionAgent, PlanSolveAgent)
+- **`code_agent/`** - 代码智能体相关实现,包括 `CodebaseMaintainer.py` 和 `hello_code_cli.py`
+- **`core/`** - 核心基类和配置
+- **`tools/`** - 工具系统
+- **`memory/`** - 记忆存储和RAG工具
+- **`context/`** - 上下文构建管道
+- **`utils/`** - 工具函数
+
+### 2. 主要功能
+从代码结构看,这个项目支持:
+- **多种智能体策略**:简单对话、ReAct(推理-行动)、反思、规划求解
+- **代码维护功能**:`CodebaseM...

+ 26 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251217_155543_1.md

@@ -0,0 +1,26 @@
+---
+id: note_20251217_155543_1
+title: 发现问题: 你好...
+type: blocker
+tags: ["hello_agents_forStudy", "auto_detected", "session_20251217_155536"]
+created_at: 2025-12-17T15:55:43.736200
+updated_at: 2025-12-17T15:55:43.736210
+---
+
+# 发现问题: 你好...
+
+## 用户输入
+你好
+
+## 问题分析
+你好!欢迎使用 Code Agent CLI。
+
+我看到你之前已经探索过这个仓库了。这是一个名为 HelloAgents 的多智能体系统框架项目。
+
+有什么需要我帮助的吗?比如:
+- 查看项目结构或特定文件
+- 分析代码逻辑
+- 修复问题或添加功能
+- 了解项目的某个部分
+
+请告诉我你想要做什么。...

+ 38 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251217_160215_2.md

@@ -0,0 +1,38 @@
+---
+id: note_20251217_160215_2
+title: 项目结构分析
+type: general
+created_at: 2025-12-17T16:02:15.369475
+updated_at: 2025-12-17T16:02:15.369491
+---
+
+# 项目结构分析
+
+## 项目结构概览
+
+**项目类型**: Python AI Agent 框架/工具包
+**主要特征**:
+1. 模块化设计 - 清晰的目录结构
+2. 虚拟环境已配置 (.venv)
+3. 版本控制 (.gitignore)
+4. 环境变量配置 (.env)
+5. 依赖管理 (requirements-*.txt)
+
+**目录结构**:
+- `agents/` - Agent 实现
+- `code_agent/` - 代码相关 Agent
+- `context/` - 上下文管理
+- `core/` - 核心框架
+- `memory/` - 记忆管理
+- `tools/` - 工具库
+- `utils/` - 工具函数
+- `docs/` - 文档
+
+**依赖文件**:
+- requirements-mvp.txt - 最小可行产品依赖
+- requirements-optional.txt - 可选依赖
+
+**配置**:
+- .env - 环境变量
+- .helloagents - 项目特定配置
+- version.py - 版本信息

+ 32 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251217_160218_3.md

@@ -0,0 +1,32 @@
+---
+id: note_20251217_160218_3
+title: 发现问题: 分析项目结构...
+type: blocker
+tags: ["hello_agents_forStudy", "auto_detected", "session_20251217_160130"]
+created_at: 2025-12-17T16:02:18.293437
+updated_at: 2025-12-17T16:02:18.293442
+---
+
+# 发现问题: 分析项目结构...
+
+## 用户输入
+分析项目结构
+
+## 问题分析
+基于对项目结构的深入分析,我来更新笔记并总结发现的问题。
+
+
+
+现在让我检查一些关键文件的内容,看看是否有代码质量问题:
+
+
+
+
+
+
+
+
+
+
+
+...

+ 32 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_190919_4.md

@@ -0,0 +1,32 @@
+---
+id: note_20251218_190919_4
+title: Patch failed
+type: blocker
+tags: ["hello_agents_forStudy", "patch_failed"]
+created_at: 2025-12-18T19:09:19.769824
+updated_at: 2025-12-18T19:09:19.769829
+---
+
+# Patch failed
+
+Error: Add File content lines must start with '+'
+
+User input:
+在testDemo文件夹下创建一个简单的HTML文件显示"helloworld"。
+
+Patch:
+
+```text
+*** Begin Patch
+*** Add File: testDemo/helloworld.html
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Hello World</title>
+</head>
+<body>
+    <h1>helloworld</h1>
+</body>
+</html>
+*** End Patch
+```

+ 34 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_191113_5.md

@@ -0,0 +1,34 @@
+---
+id: note_20251218_191113_5
+title: Patch failed
+type: blocker
+tags: ["hello_agents_forStudy", "patch_failed"]
+created_at: 2025-12-18T19:11:13.741895
+updated_at: 2025-12-18T19:11:13.741900
+---
+
+# Patch failed
+
+Error: Patch must start with '*** Begin Patch'
+
+User input:
+testDemo文件夹下创建一个简单的HTML文件显示"helloworld"
+
+Patch:
+
+```text
+*** Begin Patch ***
+*** Add File: testDemo/helloworld.html ***
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Hello World</title>
+</head>
+<body>
+    <h1>helloworld</h1>
+</body>
+</html>
+*** End Patch
+```

+ 32 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_191343_6.md

@@ -0,0 +1,32 @@
+---
+id: note_20251218_191343_6
+title: Patch failed
+type: blocker
+tags: ["hello_agents_forStudy", "patch_failed"]
+created_at: 2025-12-18T19:13:43.965183
+updated_at: 2025-12-18T19:13:43.965189
+---
+
+# Patch failed
+
+Error: Disallowed file suffix for write: .html
+
+User input:
+创建一个简单的HTML文件显示"helloworld" 在testDemo文件夹下
+
+Patch:
+
+```text
+*** Begin Patch
+*** Add File: testDemo/helloworld.html
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Hello World</title>
+</head>
+<body>
+    <h1>helloworld</h1>
+</body>
+</html>
+*** End Patch
+```

+ 32 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_191554_7.md

@@ -0,0 +1,32 @@
+---
+id: note_20251218_191554_7
+title: Patch failed
+type: blocker
+tags: ["hello_agents_forStudy", "patch_failed"]
+created_at: 2025-12-18T19:15:54.805891
+updated_at: 2025-12-18T19:15:54.805896
+---
+
+# Patch failed
+
+Error: Patch must start with '*** Begin Patch'
+
+User input:
+建一个简单的HTML文件显示"helloworld"在testDemo文件夹
+
+Patch:
+
+```text
+*** Begin Patch ***
+*** Add File: testDemo/helloworld.html
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Hello World</title>
+</head>
+<body>
+    <h1>helloworld</h1>
+</body>
+</html>
+*** End Patch
+```

+ 33 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_192121_8.md

@@ -0,0 +1,33 @@
+---
+id: note_20251218_192121_8
+title: Patch applied
+type: action
+tags: ["hello_agents_forStudy", "patch_applied"]
+created_at: 2025-12-18T19:21:21.897610
+updated_at: 2025-12-18T19:21:21.897614
+---
+
+# Patch applied
+
+User input:
+活 帮我在testDemo文件夹下建一个html文件 写上hello
+
+Patch:
+
+```text
+*** Begin Patch
+*** Add File: testDemo/hello.html
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Hello</title>
+</head>
+<body>
+    hello
+</body>
+</html>
+*** End Patch
+```
+
+Files:
+- testDemo/hello.html

+ 85 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_192253_9.md

@@ -0,0 +1,85 @@
+---
+id: note_20251218_192253_9
+title: Patch failed
+type: blocker
+tags: ["hello_agents_forStudy", "patch_failed"]
+created_at: 2025-12-18T19:22:53.964366
+updated_at: 2025-12-18T19:22:53.964371
+---
+
+# Patch failed
+
+Error: Patch hunk context not found; file changed?
+
+User input:
+给这个html文件增加一些css样式 使其好简易的网站的样子
+
+Patch:
+
+```text
+*** Begin Patch
+*** Update File: testDemo/hello.html
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Hello</title>
+    <style>
+        body {
+            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
+            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
+            margin: 0;
+            padding: 0;
+            display: flex;
+            justify-content: center;
+            align-items: center;
+            min-height: 100vh;
+            color: #333;
+        }
+        .container {
+            background-color: white;
+            border-radius: 20px;
+            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
+            padding: 40px 50px;
+            text-align: center;
+            max-width: 600px;
+            width: 90%;
+            border: 1px solid #e0e0e0;
+        }
+        h1 {
+            font-size: 3.5rem;
+            margin-bottom: 20px;
+            color: #2c3e50;
+            letter-spacing: 1px;
+        }
+        p {
+            font-size: 1.4rem;
+            line-height: 1.6;
+            color: #555;
+            margin-bottom: 30px;
+        }
+        .highlight {
+            color: #3498db;
+            font-weight: bold;
+        }
+        .footer {
+            margin-top: 40px;
+            font-size: 0.9rem;
+            color: #7f8c8d;
+            border-top: 1px dashed #ddd;
+            padding-top: 20px;
+        }
+    </style>
+</head>
+<body>
+    <div class="container">
+        <h1>Hello, World!</h1>
+        <p>This is a simple website created with HTML and CSS.</p>
+        <p>You can easily customize this page by editing the <span class="highlight">style</span> tag in the head section.</p>
+        <div class="footer">
+            <p>Built with ❤️ using Code Agent</p>
+        </div>
+    </div>
+</body>
+</html>
+*** End Patch
+```

+ 95 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_192612_10.md

@@ -0,0 +1,95 @@
+---
+id: note_20251218_192612_10
+title: Patch failed
+type: blocker
+tags: ["hello_agents_forStudy", "patch_failed"]
+created_at: 2025-12-18T19:26:12.653652
+updated_at: 2025-12-18T19:26:12.653654
+---
+
+# Patch failed
+
+Error: Patch must start with '*** Begin Patch'
+
+User input:
+为 `testDemo/hello.html` 添加内联 CSS 样式,使其看起来更像一个简易网站。样式将包括:居中布局、背景渐变、字体美化、边框和阴影效果。
+
+Patch:
+
+```text
+*** Begin Patch ***
+*** Update File: testDemo/hello.html
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Hello</title>
+    <style>
+        body {
+            margin: 0;
+            padding: 0;
+            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
+            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
+            min-height: 100vh;
+            display: flex;
+            justify-content: center;
+            align-items: center;
+            color: #333;
+        }
+        .container {
+            background: rgba(255, 255, 255, 0.95);
+            padding: 3rem 4rem;
+            border-radius: 20px;
+            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
+            text-align: center;
+            max-width: 600px;
+            border: 1px solid rgba(255, 255, 255, 0.2);
+            backdrop-filter: blur(10px);
+        }
+        h1 {
+            font-size: 3.5rem;
+            margin-bottom: 1rem;
+            color: #2d3748;
+            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
+            letter-spacing: 1px;
+        }
+        p {
+            font-size: 1.2rem;
+            line-height: 1.6;
+            color: #4a5568;
+            margin-bottom: 2rem;
+        }
+        .highlight {
+            color: #667eea;
+            font-weight: bold;
+        }
+        .button {
+            display: inline-block;
+            background: linear-gradient(to right, #667eea, #764ba2);
+            color: white;
+            padding: 0.8rem 2rem;
+            border-radius: 50px;
+            text-decoration: none;
+            font-weight: 600;
+            letter-spacing: 0.5px;
+            box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
+            transition: all 0.3s ease;
+            border: none;
+            cursor: pointer;
+        }
+        .button:hover {
+            transform: translateY(-3px);
+            box-shadow: 0 8px 25px rgba(102, 126, 234, 0.6);
+        }
+    </style>
+</head>
+<body>
+    <div class="container">
+        <h1>Hello, World!</h1>
+        <p>Welcome to this <span class="highlight">beautiful</span> and <span class="highlight">modern</span> webpage.</p>
+        <p>This page features a gradient background, centered layout, smooth shadows, and elegant typography.</p>
+        <a href="#" class="button">Get Started</a>
+    </div>
+</body>
+</html>
+*** End Patch
+```

+ 96 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_192912_11.md

@@ -0,0 +1,96 @@
+---
+id: note_20251218_192912_11
+title: Patch failed
+type: blocker
+tags: ["hello_agents_forStudy", "patch_failed"]
+created_at: 2025-12-18T19:29:12.383775
+updated_at: 2025-12-18T19:29:12.383776
+---
+
+# Patch failed
+
+Error: Patch must start with '*** Begin Patch'
+
+User input:
+继续啊 怎么停了
+
+Patch:
+
+```text
+*** Begin Patch ***
+*** Update File: testDemo/hello.html
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Hello</title>
+    <style>
+        body {
+            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
+            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
+            margin: 0;
+            padding: 0;
+            display: flex;
+            justify-content: center;
+            align-items: center;
+            min-height: 100vh;
+            color: #333;
+        }
+        .container {
+            background: white;
+            padding: 40px;
+            border-radius: 20px;
+            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
+            text-align: center;
+            max-width: 600px;
+            width: 90%;
+            border: 2px solid #fff;
+            transition: transform 0.3s ease;
+        }
+        .container:hover {
+            transform: translateY(-10px);
+        }
+        h1 {
+            color: #764ba2;
+            font-size: 3rem;
+            margin-bottom: 20px;
+            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
+        }
+        p {
+            font-size: 1.2rem;
+            line-height: 1.6;
+            color: #555;
+            margin-bottom: 30px;
+        }
+        .highlight {
+            color: #667eea;
+            font-weight: bold;
+        }
+        .button {
+            display: inline-block;
+            padding: 15px 30px;
+            background: linear-gradient(to right, #667eea, #764ba2);
+            color: white;
+            text-decoration: none;
+            border-radius: 50px;
+            font-weight: bold;
+            font-size: 1.1rem;
+            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.4);
+            transition: all 0.3s ease;
+        }
+        .button:hover {
+            box-shadow: 0 15px 30px rgba(102, 126, 234, 0.6);
+            transform: scale(1.05);
+        }
+    </style>
+</head>
+<body>
+    <div class="container">
+        <h1>Hello, World!</h1>
+        <p>Welcome to this <span class="highlight">simple yet elegant</span> webpage. This demo showcases inline CSS styling with modern effects.</p>
+        <p>Features include: centered layout, gradient background, font beautification, rounded borders, and smooth shadow effects.</p>
+        <a href="#" class="button">Explore More</a>
+    </div>
+</body>
+</html>
+*** End Patch
+```

+ 147 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_200446_12.md

@@ -0,0 +1,147 @@
+---
+id: note_20251218_200446_12
+title: Patch failed
+type: blocker
+tags: ["hello_agents_forStudy", "patch_failed"]
+created_at: 2025-12-18T20:04:46.733862
+updated_at: 2025-12-18T20:04:46.733865
+---
+
+# Patch failed
+
+Error: Patch hunk context not found; file changed?
+
+User input:
+testDemo/hello.html 添加内联 CSS 样式,使其看起来更像一个简易网站
+
+Patch:
+
+```text
+*** Begin Patch
+*** Update File: testDemo/hello.html
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Hello World Website</title>
+    <style>
+        body {
+            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
+            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
+            margin: 0;
+            padding: 0;
+            min-height: 100vh;
+            display: flex;
+            flex-direction: column;
+            align-items: center;
+            justify-content: center;
+            color: white;
+            text-align: center;
+        }
+        
+        .container {
+            max-width: 800px;
+            padding: 40px;
+            background-color: rgba(255, 255, 255, 0.1);
+            backdrop-filter: blur(10px);
+            border-radius: 20px;
+            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
+            border: 1px solid rgba(255, 255, 255, 0.2);
+        }
+        
+        h1 {
+            font-size: 3.5rem;
+            margin-bottom: 20px;
+            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
+            background: linear-gradient(45deg, #ff9a9e, #fad0c4);
+            -webkit-background-clip: text;
+            -webkit-text-fill-color: transparent;
+        }
+        
+        p {
+            font-size: 1.2rem;
+            line-height: 1.6;
+            margin-bottom: 30px;
+            opacity: 0.9;
+        }
+        
+        .features {
+            display: flex;
+            justify-content: center;
+            gap: 20px;
+            flex-wrap: wrap;
+            margin: 30px 0;
+        }
+        
+        .feature {
+            background: rgba(255, 255, 255, 0.15);
+            padding: 20px;
+            border-radius: 12px;
+            width: 180px;
+            transition: transform 0.3s ease;
+        }
+        
+        .feature:hover {
+            transform: translateY(-5px);
+            background: rgba(255, 255, 255, 0.25);
+        }
+        
+        .feature h3 {
+            margin-top: 0;
+            color: #ffd166;
+        }
+        
+        button {
+            background: linear-gradient(45deg, #ff9a9e, #fad0c4);
+            border: none;
+            padding: 15px 40px;
+            font-size: 1.1rem;
+            color: #333;
+            border-radius: 50px;
+            cursor: pointer;
+            font-weight: bold;
+            transition: all 0.3s ease;
+            margin-top: 20px;
+        }
+        
+        button:hover {
+            transform: scale(1.05);
+            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
+        }
+        
+        footer {
+            margin-top: 40px;
+            font-size: 0.9rem;
+            opacity: 0.7;
+        }
+    </style>
+</head>
+<body>
+    <div class="container">
+        <h1>Hello World!</h1>
+        <p>Welcome to this simple yet beautiful website demo. This page demonstrates how a few lines of CSS can transform basic HTML into an attractive web page.</p>
+        
+        <div class="features">
+            <div class="feature">
+                <h3>Responsive</h3>
+                <p>Adapts to different screen sizes</p>
+            </div>
+            <div class="feature">
+                <h3>Modern</h3>
+                <p>Uses CSS gradients and effects</p>
+            </div>
+            <div class="feature">
+                <h3>Interactive</h3>
+                <p>Hover effects and transitions</p>
+            </div>
+        </div>
+        
+        <button onclick="alert('Welcome to our website!')">Click Me</button>
+        
+        <footer>
+            <p>Created with ❤️ | Simple HTML & CSS Demo</p>
+        </footer>
+    </div>
+</body>
+</html>
+*** End Patch
+```

+ 157 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_200920_13.md

@@ -0,0 +1,157 @@
+---
+id: note_20251218_200920_13
+title: Patch applied
+type: action
+tags: ["hello_agents_forStudy", "patch_applied"]
+created_at: 2025-12-18T20:09:20.964843
+updated_at: 2025-12-18T20:09:20.964847
+---
+
+# Patch applied
+
+User input:
+为 testDemo/hello.html 添加内联 CSS 样式,使其看起来更像一个简易网站
+
+Patch:
+
+```text
+*** Begin Patch
+*** Update File: testDemo/hello.html
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Hello Website</title>
+    <style>
+        body {
+            margin: 0;
+            padding: 0;
+            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
+            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
+            min-height: 100vh;
+            display: flex;
+            flex-direction: column;
+            align-items: center;
+            justify-content: center;
+            color: white;
+            text-align: center;
+        }
+        
+        .container {
+            max-width: 800px;
+            padding: 40px;
+            background-color: rgba(255, 255, 255, 0.1);
+            backdrop-filter: blur(10px);
+            border-radius: 20px;
+            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
+            border: 1px solid rgba(255, 255, 255, 0.2);
+        }
+        
+        h1 {
+            font-size: 3.5rem;
+            margin-bottom: 20px;
+            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
+            background: linear-gradient(45deg, #ff9a9e, #fad0c4);
+            -webkit-background-clip: text;
+            -webkit-text-fill-color: transparent;
+            background-clip: text;
+        }
+        
+        p {
+            font-size: 1.2rem;
+            line-height: 1.6;
+            margin-bottom: 30px;
+            opacity: 0.9;
+        }
+        
+        .features {
+            display: flex;
+            flex-wrap: wrap;
+            justify-content: center;
+            gap: 20px;
+            margin: 30px 0;
+        }
+        
+        .feature {
+            background: rgba(255, 255, 255, 0.15);
+            padding: 20px;
+            border-radius: 12px;
+            flex: 1;
+            min-width: 200px;
+            max-width: 250px;
+            transition: transform 0.3s ease, background 0.3s ease;
+        }
+        
+        .feature:hover {
+            transform: translateY(-5px);
+            background: rgba(255, 255, 255, 0.25);
+        }
+        
+        .feature h3 {
+            margin-top: 0;
+            color: #ffd166;
+        }
+        
+        .cta-button {
+            display: inline-block;
+            padding: 15px 40px;
+            background: linear-gradient(45deg, #ff9a9e, #fad0c4);
+            color: #333;
+            text-decoration: none;
+            border-radius: 50px;
+            font-weight: bold;
+            font-size: 1.1rem;
+            margin-top: 20px;
+            transition: all 0.3s ease;
+            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
+        }
+        
+        .cta-button:hover {
+            transform: scale(1.05);
+            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
+        }
+        
+        footer {
+            margin-top: 40px;
+            font-size: 0.9rem;
+            opacity: 0.7;
+            border-top: 1px solid rgba(255, 255, 255, 0.2);
+            padding-top: 20px;
+            width: 100%;
+        }
+    </style>
+</head>
+<body>
+    <div class="container">
+        <h1>Hello World!</h1>
+        <p>Welcome to our simple yet beautiful website. This is a demonstration of how inline CSS can transform a basic HTML page into an attractive web experience.</p>
+        
+        <div class="features">
+            <div class="feature">
+                <h3>Modern Design</h3>
+                <p>Clean layout with gradient backgrounds and smooth animations.</p>
+            </div>
+            <div class="feature">
+                <h3>Fully Responsive</h3>
+                <p>Adapts to different screen sizes for optimal viewing.</p>
+            </div>
+            <div class="feature">
+                <h3>Fast Loading</h3>
+                <p>All styles are inline for immediate rendering.</p>
+            </div>
+        </div>
+        
+        <p>This page demonstrates the power of CSS to create engaging user interfaces without external dependencies.</p>
+        
+        <a href="#" class="cta-button">Get Started</a>
+        
+        <footer>
+            <p>© 2023 Simple Website Demo | All inline CSS | No external files required</p>
+        </footer>
+    </div>
+</body>
+</html>
+*** End Patch
+```
+
+Files:
+- testDemo/hello.html

+ 157 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_201216_14.md

@@ -0,0 +1,157 @@
+---
+id: note_20251218_201216_14
+title: Patch applied
+type: action
+tags: ["hello_agents_forStudy", "patch_applied"]
+created_at: 2025-12-18T20:12:16.689200
+updated_at: 2025-12-18T20:12:16.689205
+---
+
+# Patch applied
+
+User input:
+把网页的背景颜色改成绿色
+
+Patch:
+
+```text
+*** Begin Patch
+*** Update File: testDemo/hello.html
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Hello Website</title>
+    <style>
+        body {
+            margin: 0;
+            padding: 0;
+            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
+            background: linear-gradient(135deg, #4CAF50 0%, #2E7D32 100%);
+            min-height: 100vh;
+            display: flex;
+            flex-direction: column;
+            align-items: center;
+            justify-content: center;
+            color: white;
+            text-align: center;
+        }
+        
+        .container {
+            max-width: 800px;
+            padding: 40px;
+            background-color: rgba(255, 255, 255, 0.1);
+            backdrop-filter: blur(10px);
+            border-radius: 20px;
+            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
+            border: 1px solid rgba(255, 255, 255, 0.2);
+        }
+        
+        h1 {
+            font-size: 3.5rem;
+            margin-bottom: 20px;
+            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
+            background: linear-gradient(45deg, #ff9a9e, #fad0c4);
+            -webkit-background-clip: text;
+            -webkit-text-fill-color: transparent;
+            background-clip: text;
+        }
+        
+        p {
+            font-size: 1.2rem;
+            line-height: 1.6;
+            margin-bottom: 30px;
+            opacity: 0.9;
+        }
+        
+        .features {
+            display: flex;
+            flex-wrap: wrap;
+            justify-content: center;
+            gap: 20px;
+            margin: 30px 0;
+        }
+        
+        .feature {
+            background: rgba(255, 255, 255, 0.15);
+            padding: 20px;
+            border-radius: 12px;
+            flex: 1;
+            min-width: 200px;
+            max-width: 250px;
+            transition: transform 0.3s ease, background 0.3s ease;
+        }
+        
+        .feature:hover {
+            transform: translateY(-5px);
+            background: rgba(255, 255, 255, 0.25);
+        }
+        
+        .feature h3 {
+            margin-top: 0;
+            color: #ffd166;
+        }
+        
+        .cta-button {
+            display: inline-block;
+            padding: 15px 40px;
+            background: linear-gradient(45deg, #ff9a9e, #fad0c4);
+            color: #333;
+            text-decoration: none;
+            border-radius: 50px;
+            font-weight: bold;
+            font-size: 1.1rem;
+            margin-top: 20px;
+            transition: all 0.3s ease;
+            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
+        }
+        
+        .cta-button:hover {
+            transform: scale(1.05);
+            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
+        }
+        
+        footer {
+            margin-top: 40px;
+            font-size: 0.9rem;
+            opacity: 0.7;
+            border-top: 1px solid rgba(255, 255, 255, 0.2);
+            padding-top: 20px;
+            width: 100%;
+        }
+    </style>
+</head>
+<body>
+    <div class="container">
+        <h1>Hello World!</h1>
+        <p>Welcome to our simple yet beautiful website. This is a demonstration of how inline CSS can transform a basic HTML page into an attractive web experience.</p>
+        
+        <div class="features">
+            <div class="feature">
+                <h3>Modern Design</h3>
+                <p>Clean layout with gradient backgrounds and smooth animations.</p>
+            </div>
+            <div class="feature">
+                <h3>Fully Responsive</h3>
+                <p>Adapts to different screen sizes for optimal viewing.</p>
+            </div>
+            <div class="feature">
+                <h3>Fast Loading</h3>
+                <p>All styles are inline for immediate rendering.</p>
+            </div>
+        </div>
+        
+        <p>This page demonstrates the power of CSS to create engaging user interfaces without external dependencies.</p>
+        
+        <a href="#" class="cta-button">Get Started</a>
+        
+        <footer>
+            <p>© 2023 Simple Website Demo | All inline CSS | No external files required</p>
+        </footer>
+    </div>
+</body>
+</html>
+*** End Patch
+```
+
+Files:
+- testDemo/hello.html

+ 24 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_201409_15.md

@@ -0,0 +1,24 @@
+---
+id: note_20251218_201409_15
+title: Patch applied
+type: action
+tags: ["hello_agents_forStudy", "patch_applied"]
+created_at: 2025-12-18T20:14:09.925143
+updated_at: 2025-12-18T20:14:09.925151
+---
+
+# Patch applied
+
+User input:
+是的 删除吧
+
+Patch:
+
+```text
+*** Begin Patch
+*** Delete File: testDemo/hello.html
+*** End Patch
+```
+
+Files:
+- testDemo/hello.html

+ 384 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251218_202749_16.md

@@ -0,0 +1,384 @@
+---
+id: note_20251218_202749_16
+title: Patch applied
+type: action
+tags: ["hello_agents_forStudy", "patch_applied"]
+created_at: 2025-12-18T20:27:49.256280
+updated_at: 2025-12-18T20:27:49.256286
+---
+
+# Patch applied
+
+User input:
+plan 我想要设介绍你自己的一个简易网页
+
+Patch:
+
+```text
+*** Begin Patch
+*** Add File: testDemo/agent_intro.html
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Code Agent - Your AI Coding Assistant</title>
+    <style>
+        /* Reset and base styles */
+        * {
+            margin: 0;
+            padding: 0;
+            box-sizing: border-box;
+        }
+        
+        body {
+            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
+            line-height: 1.6;
+            color: #333;
+            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
+            min-height: 100vh;
+            padding: 20px;
+        }
+        
+        .container {
+            max-width: 1000px;
+            margin: 0 auto;
+            background: white;
+            border-radius: 20px;
+            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.1);
+            overflow: hidden;
+        }
+        
+        /* Header */
+        header {
+            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
+            color: white;
+            padding: 60px 40px;
+            text-align: center;
+        }
+        
+        .logo {
+            font-size: 3.5rem;
+            margin-bottom: 20px;
+        }
+        
+        h1 {
+            font-size: 3rem;
+            margin-bottom: 15px;
+            font-weight: 700;
+        }
+        
+        .tagline {
+            font-size: 1.3rem;
+            opacity: 0.9;
+            max-width: 700px;
+            margin: 0 auto;
+        }
+        
+        /* Main content */
+        main {
+            padding: 50px 40px;
+        }
+        
+        section {
+            margin-bottom: 50px;
+        }
+        
+        h2 {
+            color: #667eea;
+            font-size: 2.2rem;
+            margin-bottom: 25px;
+            padding-bottom: 15px;
+            border-bottom: 3px solid #f0f0f0;
+        }
+        
+        h3 {
+            color: #764ba2;
+            font-size: 1.5rem;
+            margin: 25px 0 15px;
+        }
+        
+        p {
+            font-size: 1.1rem;
+            margin-bottom: 20px;
+            color: #555;
+        }
+        
+        /* Features */
+        .features-grid {
+            display: grid;
+            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
+            gap: 25px;
+            margin-top: 30px;
+        }
+        
+        .feature-card {
+            background: #f8f9ff;
+            border-radius: 15px;
+            padding: 25px;
+            border: 2px solid #e6e9ff;
+            transition: transform 0.3s ease, box-shadow 0.3s ease;
+        }
+        
+        .feature-card:hover {
+            transform: translateY(-5px);
+            box-shadow: 0 15px 30px rgba(102, 126, 234, 0.15);
+        }
+        
+        .feature-icon {
+            font-size: 2.5rem;
+            margin-bottom: 15px;
+            color: #667eea;
+        }
+        
+        /* Code example */
+        .code-example {
+            background: #2d2d2d;
+            color: #f8f8f2;
+            border-radius: 10px;
+            padding: 25px;
+            font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
+            overflow-x: auto;
+            margin: 25px 0;
+        }
+        
+        .code-comment {
+            color: #6a9955;
+        }
+        
+        .code-keyword {
+            color: #569cd6;
+        }
+        
+        .code-string {
+            color: #ce9178;
+        }
+        
+        /* How it works */
+        .workflow {
+            display: flex;
+            flex-direction: column;
+            gap: 30px;
+            margin-top: 30px;
+        }
+        
+        .workflow-step {
+            display: flex;
+            align-items: flex-start;
+            gap: 20px;
+        }
+        
+        .step-number {
+            background: #667eea;
+            color: white;
+            width: 40px;
+            height: 40px;
+            border-radius: 50%;
+            display: flex;
+            align-items: center;
+            justify-content: center;
+            font-weight: bold;
+            flex-shrink: 0;
+        }
+        
+        /* Footer */
+        footer {
+            background: #f8f9ff;
+            padding: 30px 40px;
+            text-align: center;
+            border-top: 2px solid #e6e9ff;
+            color: #666;
+        }
+        
+        .footer-links {
+            display: flex;
+            justify-content: center;
+            gap: 30px;
+            margin-top: 20px;
+            flex-wrap: wrap;
+        }
+        
+        .footer-link {
+            color: #667eea;
+            text-decoration: none;
+            font-weight: 500;
+        }
+        
+        .footer-link:hover {
+            text-decoration: underline;
+        }
+        
+        /* Responsive */
+        @media (max-width: 768px) {
+            header {
+                padding: 40px 20px;
+            }
+            
+            h1 {
+                font-size: 2.2rem;
+            }
+            
+            .logo {
+                font-size: 2.5rem;
+            }
+            
+            main {
+                padding: 30px 20px;
+            }
+            
+            h2 {
+                font-size: 1.8rem;
+            }
+            
+            .features-grid {
+                grid-template-columns: 1fr;
+            }
+            
+            .workflow-step {
+                flex-direction: column;
+                align-items: center;
+                text-align: center;
+            }
+        }
+        
+        @media (max-width: 480px) {
+            body {
+                padding: 10px;
+            }
+            
+            .container {
+                border-radius: 15px;
+            }
+            
+            header {
+                padding: 30px 15px;
+            }
+            
+            h1 {
+                font-size: 1.8rem;
+            }
+        }
+    </style>
+</head>
+<body>
+    <div class="container">
+        <header>
+            <div class="logo">🤖</div>
+            <h1>Code Agent</h1>
+            <p class="tagline">Your intelligent AI coding assistant that understands, reasons, and helps you build better software.</p>
+        </header>
+        
+        <main>
+            <section id="introduction">
+                <h2>What is Code Agent?</h2>
+                <p>Code Agent is an AI-powered coding assistant designed to help developers write, debug, and understand code more efficiently. It combines natural language understanding with code analysis capabilities to provide contextual assistance.</p>
+                <p>Unlike simple code completion tools, Code Agent can understand your project context, reason about code structure, and provide intelligent suggestions based on your specific needs.</p>
+            </section>
+            
+            <section id="features">
+                <h2>Key Features</h2>
+                <div class="features-grid">
+                    <div class="feature-card">
+                        <div class="feature-icon">💡</div>
+                        <h3>Intelligent Code Understanding</h3>
+                        <p>Analyzes your codebase to understand context, dependencies, and patterns before making suggestions.</p>
+                    </div>
+                    
+                    <div class="feature-card">
+                        <div class="feature-icon">🔍</div>
+                        <h3>Context-Aware Debugging</h3>
+                        <p>Helps identify bugs by understanding error messages, stack traces, and your specific code context.</p>
+                    </div>
+                    
+                    <div class="feature-card">
+                        <div class="feature-icon">⚡</div>
+                        <h3>Rapid Prototyping</h3>
+                        <p>Generates boilerplate code, implements common patterns, and helps you build features faster.</p>
+                    </div>
+                    
+                    <div class="feature-card">
+                        <div class="feature-icon">📚</div>
+                        <h3>Documentation Assistant</h3>
+                        <p>Helps create and maintain documentation, comments, and code explanations automatically.</p>
+                    </div>
+                </div>
+            </section>
+            
+            <section id="example">
+                <h2>Example Usage</h2>
+                <p>Here's how Code Agent can help you with a simple task:</p>
+                
+                <div class="code-example">
+                    <span class="code-comment"># User: "Create a function to calculate factorial"</span><br>
+                    <br>
+                    <span class="code-keyword">def</span> factorial(n):<br>
+                    &nbsp;&nbsp;&nbsp;&nbsp;<span class="code-keyword">if</span> n &lt; <span class="code-number">0</span>:<br>
+                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="code-keyword">raise</span> ValueError(<span class="code-string">"Factorial is not defined for negative numbers"</span>)<br>
+                    &nbsp;&nbsp;&nbsp;&nbsp;<span class="code-keyword">if</span> n == <span class="code-number">0</span> <span class="code-keyword">or</span> n == <span class="code-number">1</span>:<br>
+                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="code-keyword">return</span> <span class="code-number">1</span><br>
+                    &nbsp;&nbsp;&nbsp;&nbsp;<span class="code-keyword">return</span> n * factorial(n - <span class="code-number">1</span>)<br>
+                    <br>
+                    <span class="code-comment"># Code Agent also adds:</span><br>
+                    <span class="code-comment"># - Docstring with examples</span><br>
+                    <span class="code-comment"># - Type hints</span><br>
+                    <span class="code-comment"># - Edge case handling</span><br>
+                    <span class="code-comment"># - Test cases</span>
+                </div>
+            </section>
+            
+            <section id="how-it-works">
+                <h2>How It Works</h2>
+                <div class="workflow">
+                    <div class="workflow-step">
+                        <div class="step-number">1</div>
+                        <div>
+                            <h3>Understand Your Request</h3>
+                            <p>Code Agent analyzes your natural language request and understands the context of your project.</p>
+                        </div>
+                    </div>
+                    
+                    <div class="workflow-step">
+                        <div class="step-number">2</div>
+                        <div>
+                            <h3>Analyze Codebase</h3>
+                            <p>It examines relevant files, dependencies, and patterns to provide context-aware suggestions.</p>
+                        </div>
+                    </div>
+                    
+                    <div class="workflow-step">
+                        <div class="step-number">3</div>
+                        <div>
+                            <h3>Generate Solution</h3>
+                            <p>Based on best practices and your specific needs, it creates code, explanations, or fixes.</p>
+                        </div>
+                    </div>
+                    
+                    <div class="workflow-step">
+                        <div class="step-number">4</div>
+                        <div>
+                            <h3>Iterate & Refine</h3>
+                            <p>You can provide feedback, and Code Agent will refine its suggestions until you're satisfied.</p>
+                        </div>
+                    </div>
+                </div>
+            </section>
+        </main>
+        
+        <footer>
+            <p>Code Agent - Making development faster, smarter, and more enjoyable</p>
+            <div class="footer-links">
+                <a href="#" class="footer-link">Documentation</a>
+                <a href="#" class="footer-link">Examples</a>
+                <a href="#" class="footer-link">GitHub</a>
+                <a href="#" class="footer-link">Contact</a>
+            </div>
+            <p style="margin-top: 20px; font-size: 0.9rem;">© 2023 Code Agent Project. All rights reserved.</p>
+        </footer>
+    </div>
+</body>
+</html>
+*** End Patch
+```
+
+Files:
+- testDemo/agent_intro.html

+ 21 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251219_150917_17.md

@@ -0,0 +1,21 @@
+---
+id: note_20251219_150917_17
+title: Patch failed
+type: blocker
+tags: ["hello_agents_forStudy", "patch_failed"]
+created_at: 2025-12-19T15:09:17.654597
+updated_at: 2025-12-19T15:09:17.654601
+---
+
+# Patch failed
+
+Error: Patch must start with '*** Begin Patch'
+
+User input:
+将testDemo文件夹下的html文件内容改成中 文的 并且   用js实现一些简易的效果 让页面开起来更生  动
+
+Patch:
+
+```text
+*** Begin Patch` ... `*** End Patch
+```

+ 253 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251219_153949_18.md

@@ -0,0 +1,253 @@
+---
+id: note_20251219_153949_18
+title: Patch applied
+type: action
+tags: ["hello_agents_forStudy", "patch_applied"]
+created_at: 2025-12-19T15:39:49.117819
+updated_at: 2025-12-19T15:39:49.117824
+---
+
+# Patch applied
+
+User input:
+请在 testDemo 目录下把 hello.html 改造成一个简洁的个人简介页,分三步进行:
+
+Patch:
+
+```text
+*** Begin Patch
+*** Add File: testDemo/hello.html
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Personal Profile</title>
+    <style>
+        * {
+            margin: 0;
+            padding: 0;
+            box-sizing: border-box;
+        }
+        
+        body {
+            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
+            line-height: 1.6;
+            color: #333;
+            background-color: #f8f9fa;
+            padding: 20px;
+        }
+        
+        .container {
+            max-width: 800px;
+            margin: 0 auto;
+            background: white;
+            border-radius: 12px;
+            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
+            overflow: hidden;
+        }
+        
+        header {
+            background: linear-gradient(135deg, #4f6df5 0%, #3a56d5 100%);
+            color: white;
+            padding: 40px 30px;
+            text-align: center;
+        }
+        
+        h1 {
+            font-size: 2.5rem;
+            margin-bottom: 8px;
+            font-weight: 700;
+        }
+        
+        .tagline {
+            font-size: 1.2rem;
+            opacity: 0.9;
+            margin-bottom: 20px;
+        }
+        
+        .avatar {
+            width: 120px;
+            height: 120px;
+            border-radius: 50%;
+            border: 4px solid white;
+            margin: 0 auto 20px;
+            background: #e9ecef;
+            display: flex;
+            align-items: center;
+            justify-content: center;
+            font-size: 2.5rem;
+            color: #4f6df5;
+        }
+        
+        section {
+            padding: 30px;
+            border-bottom: 1px solid #e9ecef;
+        }
+        
+        section:last-child {
+            border-bottom: none;
+        }
+        
+        h2 {
+            color: #4f6df5;
+            margin-bottom: 15px;
+            font-size: 1.5rem;
+            display: flex;
+            align-items: center;
+            gap: 10px;
+        }
+        
+        h2::before {
+            content: '';
+            display: inline-block;
+            width: 6px;
+            height: 24px;
+            background: #4f6df5;
+            border-radius: 3px;
+        }
+        
+        p {
+            margin-bottom: 15px;
+            color: #555;
+        }
+        
+        .skills {
+            display: flex;
+            flex-wrap: wrap;
+            gap: 10px;
+            margin-top: 15px;
+        }
+        
+        .skill {
+            background: #eef2ff;
+            color: #4f6df5;
+            padding: 8px 16px;
+            border-radius: 20px;
+            font-size: 0.9rem;
+            font-weight: 500;
+        }
+        
+        .contact-list {
+            list-style: none;
+            margin-top: 15px;
+        }
+        
+        .contact-list li {
+            margin-bottom: 12px;
+            display: flex;
+            align-items: center;
+            gap: 12px;
+        }
+        
+        .contact-icon {
+            width: 36px;
+            height: 36px;
+            background: #eef2ff;
+            border-radius: 50%;
+            display: flex;
+            align-items: center;
+            justify-content: center;
+            color: #4f6df5;
+            font-weight: bold;
+        }
+        
+        footer {
+            text-align: center;
+            padding: 20px;
+            color: #6c757d;
+            font-size: 0.9rem;
+            border-top: 1px solid #e9ecef;
+        }
+        
+        @media (max-width: 600px) {
+            body {
+                padding: 10px;
+            }
+            
+            h1 {
+                font-size: 2rem;
+            }
+            
+            section {
+                padding: 20px;
+            }
+        }
+    </style>
+</head>
+<body>
+    <div class="container">
+        <header>
+            <div class="avatar">👤</div>
+            <h1>Alex Chen</h1>
+            <p class="tagline">Full Stack Developer & UI/UX Enthusiast</p>
+        </header>
+        
+        <section id="about">
+            <h2>About Me</h2>
+            <p>Hello! I'm a passionate developer with 5+ years of experience building web applications. I enjoy creating clean, efficient, and user-friendly solutions that solve real-world problems.</p>
+            <p>My approach combines technical expertise with design thinking, ensuring that every project is both functional and delightful to use.</p>
+        </section>
+        
+        <section id="skills">
+            <h2>Skills</h2>
+            <div class="skills">
+                <span class="skill">JavaScript</span>
+                <span class="skill">React</span>
+                <span class="skill">TypeScript</span>
+                <span class="skill">Node.js</span>
+                <span class="skill">Python</span>
+                <span class="skill">Django</span>
+                <span class="skill">PostgreSQL</span>
+                <span class="skill">Docker</span>
+                <span class="skill">AWS</span>
+                <span class="skill">Figma</span>
+            </div>
+        </section>
+        
+        <section id="contact">
+            <h2>Contact</h2>
+            <ul class="contact-list">
+                <li>
+                    <div class="contact-icon">📧</div>
+                    <div>
+                        <strong>Email</strong><br>
+                        alex.chen@example.com
+                    </div>
+                </li>
+                <li>
+                    <div class="contact-icon">💼</div>
+                    <div>
+                        <strong>LinkedIn</strong><br>
+                        linkedin.com/in/alexchen
+                    </div>
+                </li>
+                <li>
+                    <div class="contact-icon">🐙</div>
+                    <div>
+                        <strong>GitHub</strong><br>
+                        github.com/alexchen
+                    </div>
+                </li>
+                <li>
+                    <div class="contact-icon">🌐</div>
+                    <div>
+                        <strong>Portfolio</strong><br>
+                        alexchen.dev
+                    </div>
+                </li>
+            </ul>
+        </section>
+        
+        <footer>
+            <p>© 2023 Alex Chen. All rights reserved.</p>
+            <p>Last updated: December 2023</p>
+        </footer>
+    </div>
+</body>
+</html>
+*** End Patch
+```
+
+Files:
+- testDemo/hello.html

+ 161 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251219_161406_19.md

@@ -0,0 +1,161 @@
+---
+id: note_20251219_161406_19
+title: Patch applied
+type: action
+tags: ["hello_agents_forStudy", "patch_applied"]
+created_at: 2025-12-19T16:14:06.058947
+updated_at: 2025-12-19T16:14:06.058951
+---
+
+# Patch applied
+
+User input:
+请在 testDemo 目录下把 hello.html 改造成一个简洁的个人简介页,设计布局并添加头部、简介段落、技能列表(三项即可) 添加一段内联 CSS,包含背景色、卡片样式和按钮悬停效果。添加一个‘联系我’按钮,点击跳转 mailto 链接。
+
+Patch:
+
+```text
+*** Begin Patch
+*** Add File: testDemo/hello.html
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Personal Profile</title>
+    <style>
+        * {
+            margin: 0;
+            padding: 0;
+            box-sizing: border-box;
+            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
+        }
+        body {
+            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
+            min-height: 100vh;
+            display: flex;
+            justify-content: center;
+            align-items: center;
+            padding: 20px;
+        }
+        .card {
+            background: white;
+            border-radius: 20px;
+            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
+            width: 100%;
+            max-width: 800px;
+            padding: 40px;
+            text-align: center;
+        }
+        .header {
+            margin-bottom: 30px;
+        }
+        .header h1 {
+            color: #333;
+            font-size: 2.8rem;
+            margin-bottom: 10px;
+        }
+        .header p {
+            color: #666;
+            font-size: 1.2rem;
+            font-weight: 300;
+        }
+        .intro {
+            font-size: 1.1rem;
+            line-height: 1.6;
+            color: #444;
+            margin-bottom: 30px;
+            text-align: left;
+            padding: 0 10px;
+        }
+        .skills {
+            margin-bottom: 40px;
+            text-align: left;
+        }
+        .skills h2 {
+            color: #333;
+            margin-bottom: 15px;
+            font-size: 1.8rem;
+        }
+        .skills ul {
+            list-style: none;
+            padding-left: 0;
+        }
+        .skills li {
+            background: #f8f9fa;
+            margin: 10px 0;
+            padding: 15px 20px;
+            border-radius: 10px;
+            border-left: 5px solid #667eea;
+            font-size: 1.1rem;
+            color: #333;
+            transition: transform 0.3s ease, box-shadow 0.3s ease;
+        }
+        .skills li:hover {
+            transform: translateX(5px);
+            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.2);
+        }
+        .contact-btn {
+            display: inline-block;
+            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
+            color: white;
+            padding: 16px 40px;
+            font-size: 1.2rem;
+            font-weight: 600;
+            text-decoration: none;
+            border-radius: 50px;
+            border: none;
+            cursor: pointer;
+            transition: all 0.3s ease;
+            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
+        }
+        .contact-btn:hover {
+            transform: translateY(-5px);
+            box-shadow: 0 10px 25px rgba(102, 126, 234, 0.6);
+            background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
+        }
+        .footer {
+            margin-top: 30px;
+            color: #888;
+            font-size: 0.9rem;
+        }
+        @media (max-width: 768px) {
+            .card {
+                padding: 30px 20px;
+            }
+            .header h1 {
+                font-size: 2.2rem;
+            }
+        }
+    </style>
+</head>
+<body>
+    <div class="card">
+        <div class="header">
+            <h1>Alex Chen</h1>
+            <p>Full‑Stack Developer & UI/UX Enthusiast</p>
+        </div>
+        <div class="intro">
+            <p>Hello! I'm a passionate developer with over 5 years of experience building web applications. I love creating clean, efficient, and user‑friendly interfaces. My approach combines technical expertise with a keen eye for design, ensuring every project is both functional and beautiful.</p>
+            <p>Currently, I'm exploring the intersection of AI and front‑end development, working on tools that make coding more accessible and enjoyable.</p>
+        </div>
+        <div class="skills">
+            <h2>Core Skills</h2>
+            <ul>
+                <li>JavaScript / TypeScript & Modern Frameworks (React, Vue, Svelte)</li>
+                <li>Back‑end Development with Node.js & Python</li>
+                <li>UI/UX Design & Responsive Web Development</li>
+            </ul>
+        </div>
+        <a href="mailto:alex.chen@example.com" class="contact-btn">Contact Me</a>
+        <div class="footer">
+            <p>© 2023 Personal Profile. All rights reserved.</p>
+        </div>
+    </div>
+</body>
+</html>
+*** End Patch
+```
+
+Files:
+- testDemo/hello.html

+ 226 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251219_164354_20.md

@@ -0,0 +1,226 @@
+---
+id: note_20251219_164354_20
+title: Patch applied
+type: action
+tags: ["hello_agents_forStudy", "patch_applied"]
+created_at: 2025-12-19T16:43:54.544921
+updated_at: 2025-12-19T16:43:54.544926
+---
+
+# Patch applied
+
+User input:
+请在 testDemo 目录下把 hello.html 改造成一个简洁的个人简介页,设计布局并添加头部、简介段落、技能列表(三项即可) 添加一段内联 CSS,包含背景色、卡片样式和按钮悬停效果。添加一个‘联系我’按钮,点击跳转 mailto 链接。
+
+Patch:
+
+```text
+*** Begin Patch
+*** Add File: testDemo/hello.html
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Personal Profile</title>
+    <style>
+        /* Base styles */
+        * {
+            margin: 0;
+            padding: 0;
+            box-sizing: border-box;
+        }
+        
+        body {
+            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
+            line-height: 1.6;
+            color: #333;
+            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
+            min-height: 100vh;
+            padding: 20px;
+            display: flex;
+            justify-content: center;
+            align-items: center;
+        }
+        
+        .container {
+            max-width: 800px;
+            width: 100%;
+            margin: 0 auto;
+        }
+        
+        /* Card style */
+        .card {
+            background: white;
+            border-radius: 16px;
+            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
+            padding: 40px;
+            margin: 20px;
+        }
+        
+        /* Header */
+        header {
+            text-align: center;
+            margin-bottom: 30px;
+        }
+        
+        h1 {
+            font-size: 2.5rem;
+            color: #2d3748;
+            margin-bottom: 10px;
+        }
+        
+        .tagline {
+            font-size: 1.2rem;
+            color: #718096;
+            font-weight: 300;
+        }
+        
+        /* Intro section */
+        .intro {
+            margin-bottom: 30px;
+        }
+        
+        .intro p {
+            font-size: 1.1rem;
+            color: #4a5568;
+            margin-bottom: 15px;
+        }
+        
+        /* Skills section */
+        .skills {
+            margin-bottom: 40px;
+        }
+        
+        h2 {
+            font-size: 1.8rem;
+            color: #2d3748;
+            margin-bottom: 20px;
+            border-bottom: 2px solid #e2e8f0;
+            padding-bottom: 10px;
+        }
+        
+        .skills-list {
+            list-style: none;
+            display: grid;
+            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
+            gap: 20px;
+        }
+        
+        .skill-item {
+            background: #f7fafc;
+            border-radius: 10px;
+            padding: 20px;
+            border-left: 4px solid #667eea;
+        }
+        
+        .skill-item h3 {
+            font-size: 1.3rem;
+            color: #2d3748;
+            margin-bottom: 8px;
+        }
+        
+        .skill-item p {
+            color: #718096;
+            font-size: 0.95rem;
+        }
+        
+        /* Contact button */
+        .contact {
+            text-align: center;
+        }
+        
+        .btn-contact {
+            display: inline-block;
+            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
+            color: white;
+            padding: 15px 40px;
+            font-size: 1.1rem;
+            font-weight: 600;
+            text-decoration: none;
+            border-radius: 50px;
+            border: none;
+            cursor: pointer;
+            transition: all 0.3s ease;
+            box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
+        }
+        
+        /* Button hover effect */
+        .btn-contact:hover {
+            transform: translateY(-3px);
+            box-shadow: 0 8px 25px rgba(102, 126, 234, 0.6);
+            background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
+        }
+        
+        /* Footer */
+        footer {
+            text-align: center;
+            margin-top: 40px;
+            color: #a0aec0;
+            font-size: 0.9rem;
+        }
+        
+        /* Responsive */
+        @media (max-width: 768px) {
+            .card {
+                padding: 25px;
+                margin: 10px;
+            }
+            
+            h1 {
+                font-size: 2rem;
+            }
+            
+            .skills-list {
+                grid-template-columns: 1fr;
+            }
+        }
+    </style>
+</head>
+<body>
+    <div class="container">
+        <div class="card">
+            <header>
+                <h1>Alex Chen</h1>
+                <p class="tagline">Full Stack Developer & UI/UX Enthusiast</p>
+            </header>
+            
+            <section class="intro">
+                <p>Hello! I'm a passionate developer with over 5 years of experience building web applications. I love creating clean, efficient, and user-friendly solutions that solve real-world problems.</p>
+                <p>My approach combines technical expertise with creative design thinking, ensuring that every project is both functional and visually appealing.</p>
+            </section>
+            
+            <section class="skills">
+                <h2>Core Skills</h2>
+                <ul class="skills-list">
+                    <li class="skill-item">
+                        <h3>Frontend Development</h3>
+                        <p>React, Vue.js, TypeScript, HTML5, CSS3, responsive design</p>
+                    </li>
+                    <li class="skill-item">
+                        <h3>Backend Development</h3>
+                        <p>Node.js, Python, REST APIs, database design, cloud deployment</p>
+                    </li>
+                    <li class="skill-item">
+                        <h3>UI/UX Design</h3>
+                        <p>Figma, user research, prototyping, design systems, accessibility</p>
+                    </li>
+                </ul>
+            </section>
+            
+            <section class="contact">
+                <a href="mailto:alex.chen@example.com?subject=Hello%20from%20your%20profile" class="btn-contact">Contact Me</a>
+            </section>
+            
+            <footer>
+                <p>© 2023 Alex Chen. All rights reserved.</p>
+            </footer>
+        </div>
+    </div>
+</body>
+</html>
+*** End Patch
+```
+
+Files:
+- testDemo/hello.html

+ 374 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251219_171820_21.md

@@ -0,0 +1,374 @@
+---
+id: note_20251219_171820_21
+title: Patch applied
+type: action
+tags: ["hello_agents_forStudy", "patch_applied"]
+created_at: 2025-12-19T17:18:20.676780
+updated_at: 2025-12-19T17:18:20.676784
+---
+
+# Patch applied
+
+User input:
+用户想创建一个贪吃蛇游戏的 MVP 版本,用 HTML、CSS、JS
+
+Patch:
+
+```text
+*** Begin Patch
+*** Add File: testDemo/snake.js
+// Snake Game MVP - JavaScript Implementation
+document.addEventListener('DOMContentLoaded', function() {
+    // Game variables
+    const canvas = document.getElementById('gameCanvas');
+    const ctx = canvas.getContext('2d');
+    const scoreElement = document.getElementById('score');
+    const highScoreElement = document.getElementById('highScore');
+    const speedElement = document.getElementById('speed');
+    const startBtn = document.getElementById('startBtn');
+    const pauseBtn = document.getElementById('pauseBtn');
+    const resetBtn = document.getElementById('resetBtn');
+    
+    // Game constants
+    const GRID_SIZE = 20;
+    const GRID_WIDTH = canvas.width / GRID_SIZE;
+    const GRID_HEIGHT = canvas.height / GRID_SIZE;
+    
+    // Game state
+    let snake = [];
+    let food = {};
+    let direction = 'right';
+    let nextDirection = 'right';
+    let gameSpeed = 150; // ms
+    let score = 0;
+    let highScore = localStorage.getItem('snakeHighScore') || 0;
+    let gameRunning = false;
+    let gameLoop;
+    let foodsEaten = 0;
+    
+    // Initialize game
+    function init() {
+        // Initialize snake
+        snake = [
+            {x: 5, y: 10},
+            {x: 4, y: 10},
+            {x: 3, y: 10}
+        ];
+        
+        // Generate first food
+        generateFood();
+        
+        // Reset score and speed
+        score = 0;
+        foodsEaten = 0;
+        updateScore();
+        updateSpeedDisplay();
+        
+        // Draw initial state
+        draw();
+    }
+    
+    // Generate food at random position
+    function generateFood() {
+        let newFood;
+        let foodOnSnake;
+        
+        do {
+            foodOnSnake = false;
+            newFood = {
+                x: Math.floor(Math.random() * GRID_WIDTH),
+                y: Math.floor(Math.random() * GRID_HEIGHT)
+            };
+            
+            // Check if food is on snake
+            for (let segment of snake) {
+                if (segment.x === newFood.x && segment.y === newFood.y) {
+                    foodOnSnake = true;
+                    break;
+                }
+            }
+        } while (foodOnSnake);
+        
+        food = newFood;
+    }
+    
+    // Draw game elements
+    function draw() {
+        // Clear canvas
+        ctx.fillStyle = '#000';
+        ctx.fillRect(0, 0, canvas.width, canvas.height);
+        
+        // Draw snake
+        ctx.fillStyle = '#00ff88';
+        for (let i = 0; i < snake.length; i++) {
+            const segment = snake[i];
+            // Head is slightly brighter
+            if (i === 0) {
+                ctx.fillStyle = '#00ff88';
+            } else {
+                ctx.fillStyle = '#00cc6a';
+            }
+            ctx.fillRect(
+                segment.x * GRID_SIZE, 
+                segment.y * GRID_SIZE, 
+                GRID_SIZE - 1, 
+                GRID_SIZE - 1
+            );
+        }
+        
+        // Draw food
+        ctx.fillStyle = '#ff5555';
+        ctx.beginPath();
+        ctx.arc(
+            food.x * GRID_SIZE + GRID_SIZE / 2,
+            food.y * GRID_SIZE + GRID_SIZE / 2,
+            GRID_SIZE / 2 - 1,
+            0,
+            Math.PI * 2
+        );
+        ctx.fill();
+        
+        // Draw grid (optional, for debugging)
+        // drawGrid();
+    }
+    
+    // Draw grid lines (for debugging)
+    function drawGrid() {
+        ctx.strokeStyle = '#333';
+        ctx.lineWidth = 0.5;
+        
+        // Vertical lines
+        for (let x = 0; x <= canvas.width; x += GRID_SIZE) {
+            ctx.beginPath();
+            ctx.moveTo(x, 0);
+            ctx.lineTo(x, canvas.height);
+            ctx.stroke();
+        }
+        
+        // Horizontal lines
+        for (let y = 0; y <= canvas.height; y += GRID_SIZE) {
+            ctx.beginPath();
+            ctx.moveTo(0, y);
+            ctx.lineTo(canvas.width, y);
+            ctx.stroke();
+        }
+    }
+    
+    // Update game state
+    function update() {
+        // Update direction
+        direction = nextDirection;
+        
+        // Calculate new head position
+        const head = {...snake[0]};
+        
+        switch (direction) {
+            case 'up': head.y--; break;
+            case 'down': head.y++; break;
+            case 'left': head.x--; break;
+            case 'right': head.x++; break;
+        }
+        
+        // Check wall collision
+        if (
+            head.x < 0 || 
+            head.x >= GRID_WIDTH || 
+            head.y < 0 || 
+            head.y >= GRID_HEIGHT
+        ) {
+            gameOver();
+            return;
+        }
+        
+        // Check self collision
+        for (let segment of snake) {
+            if (head.x === segment.x && head.y === segment.y) {
+                gameOver();
+                return;
+            }
+        }
+        
+        // Add new head
+        snake.unshift(head);
+        
+        // Check food collision
+        if (head.x === food.x && head.y === food.y) {
+            // Increase score
+            score += 10;
+            foodsEaten++;
+            
+            // Update high score if needed
+            if (score > highScore) {
+                highScore = score;
+                localStorage.setItem('snakeHighScore', highScore);
+            }
+            
+            // Increase speed every 5 foods
+            if (foodsEaten % 5 === 0 && gameSpeed > 50) {
+                gameSpeed -= 20;
+                updateSpeedDisplay();
+                clearInterval(gameLoop);
+                if (gameRunning) {
+                    gameLoop = setInterval(gameStep, gameSpeed);
+                }
+            }
+            
+            // Generate new food
+            generateFood();
+            updateScore();
+        } else {
+            // Remove tail if no food eaten
+            snake.pop();
+        }
+        
+        // Draw updated state
+        draw();
+    }
+    
+    // Game step (called by interval)
+    function gameStep() {
+        if (gameRunning) {
+            update();
+        }
+    }
+    
+    // Update score display
+    function updateScore() {
+        scoreElement.textContent = score;
+        highScoreElement.textContent = highScore;
+    }
+    
+    // Update speed display
+    function updateSpeedDisplay() {
+        if (gameSpeed >= 130) {
+            speedElement.textContent = 'Slow';
+        } else if (gameSpeed >= 90) {
+            speedElement.textContent = 'Normal';
+        } else if (gameSpeed >= 70) {
+            speedElement.textContent = 'Fast';
+        } else if (gameSpeed >= 50) {
+            speedElement.textContent = 'Very Fast';
+        } else {
+            speedElement.textContent = 'Extreme';
+        }
+    }
+    
+    // Game over
+    function gameOver() {
+        gameRunning = false;
+        clearInterval(gameLoop);
+        
+        // Show game over overlay
+        const gameOverDiv = document.createElement('div');
+        gameOverDiv.className = 'game-over';
+        gameOverDiv.innerHTML = `
+            <h2>Game Over!</h2>
+            <p>Final Score: ${score}</p>
+            <p>High Score: ${highScore}</p>
+            <button class="btn btn-primary" onclick="this.parentElement.remove();">OK</button>
+        `;
+        
+        // Add to game board
+        const gameBoard = document.querySelector('.game-board');
+        gameBoard.appendChild(gameOverDiv);
+        
+        // Update button states
+        startBtn.disabled = false;
+        pauseBtn.disabled = true;
+    }
+    
+    // Start game
+    function startGame() {
+        if (!gameRunning) {
+            gameRunning = true;
+            gameLoop = setInterval(gameStep, gameSpeed);
+            
+            // Update button states
+            startBtn.disabled = true;
+            pauseBtn.disabled = false;
+        }
+    }
+    
+    // Pause game
+    function pauseGame() {
+        if (gameRunning) {
+            gameRunning = false;
+            clearInterval(gameLoop);
+            
+            // Update button states
+            startBtn.disabled = false;
+            pauseBtn.disabled = true;
+        }
+    }
+    
+    // Reset game
+    function resetGame() {
+        // Clear any game over overlay
+        const gameOverDiv = document.querySelector('.game-over');
+        if (gameOverDiv) {
+            gameOverDiv.remove();
+        }
+        
+        // Stop game if running
+        if (gameRunning) {
+            clearInterval(gameLoop);
+            gameRunning = false;
+        }
+        
+        // Reinitialize
+        init();
+        
+        // Update button states
+        startBtn.disabled = false;
+        pauseBtn.disabled = true;
+    }
+    
+    // Keyboard controls
+    document.addEventListener('keydown', function(e) {
+        switch (e.key) {
+            case 'ArrowUp':
+            case 'w':
+            case 'W':
+                if (direction !== 'down') nextDirection = 'up';
+                break;
+            case 'ArrowDown':
+            case 's':
+            case 'S':
+                if (direction !== 'up') nextDirection = 'down';
+                break;
+            case 'ArrowLeft':
+            case 'a':
+            case 'A':
+                if (direction !== 'right') nextDirection = 'left';
+                break;
+            case 'ArrowRight':
+            case 'd':
+            case 'D':
+                if (direction !== 'left') nextDirection = 'right';
+                break;
+            case ' ':
+                if (gameRunning) {
+                    pauseGame();
+                } else {
+                    startGame();
+                }
+                break;
+        }
+    });
+    
+    // Button event listeners
+    startBtn.addEventListener('click', startGame);
+    pauseBtn.addEventListener('click', pauseGame);
+    resetBtn.addEventListener('click', resetGame);
+    
+    // Initialize game on load
+    init();
+    
+    // Update button states
+    pauseBtn.disabled = true;
+});
+*** End Patch
+```
+
+Files:
+- testDemo/snake.js

+ 25 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251219_191656_22.md

@@ -0,0 +1,25 @@
+---
+id: note_20251219_191656_22
+title: Patch applied
+type: action
+tags: ["hello_agents_forStudy", "patch_applied"]
+created_at: 2025-12-19T19:16:56.242524
+updated_at: 2025-12-19T19:16:56.242529
+---
+
+# Patch applied
+
+User input:
+在testDem新建一个一个文档 写上 我讨厌java
+
+Patch:
+
+```text
+*** Begin Patch
+*** Add File: testDemo/我讨厌java.txt
+我讨厌java
+*** End Patch
+```
+
+Files:
+- testDemo/我讨厌java.txt

+ 24 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/notes/note_20251219_192206_23.md

@@ -0,0 +1,24 @@
+---
+id: note_20251219_192206_23
+title: Patch applied
+type: action
+tags: ["hello_agents_forStudy", "patch_applied"]
+created_at: 2025-12-19T19:22:06.995412
+updated_at: 2025-12-19T19:22:06.995418
+---
+
+# Patch applied
+
+User input:
+确认
+
+Patch:
+
+```text
+*** Begin Patch
+*** Delete File: testDemo/我讨厌java.txt
+*** End Patch
+```
+
+Files:
+- testDemo/我讨厌java.txt

+ 20 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/.helloagents/todos/todos.json.bak

@@ -0,0 +1,20 @@
+{
+  "items": [
+    {
+      "id": 1,
+      "title": "改造 hello.html 为个人简介页",
+      "desc": "分三步:1. 确认文件存在或创建 2. 设计简洁布局 3. 添加样式和内容",
+      "status": "pending",
+      "created_at": "2025-12-19T15:19:35",
+      "updated_at": "2025-12-19T17:01:00"
+    },
+    {
+      "id": 2,
+      "title": "在 testDemo 下创建贪吃蛇游戏 MVP",
+      "desc": "创建 HTML、CSS、JS 文件实现简单贪吃蛇游戏",
+      "status": "completed",
+      "created_at": "2025-12-19T17:01:08",
+      "updated_at": "2025-12-19T17:09:57"
+    }
+  ]
+}

+ 376 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/README.md

@@ -0,0 +1,376 @@
+# HelloAgents Code Agent CLI
+
+<div align="center">
+
+**面向本地代码仓库的智能 Code Agent 命令行工具**
+
+[![Python Version](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
+[![License](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE)
+
+[特性](#-核心特性) • [架构](#-系统架构) • [快速开始](#-快速开始) • [使用指南](#-使用指南) • [开发](#-开发指南)
+
+</div>
+
+---
+
+## 📝 项目简介
+
+**HelloAgents Code Agent CLI** 是一个基于 HelloAgents 框架开发的智能代码助手,提供类似 Claude Code/Codex 的交互体验,专注于本地代码仓库的安全智能操作。
+
+### 核心价值
+
+- **🎯 精准检索**:按需探索代码库,先证据后结论,避免全库扫描
+- **🛡️ 安全可控**:补丁式修改 + 原子写入 + 自动备份,危险修改需人工确认
+- **🧠 智能推理**:基于 ReAct 范式,支持多步推理与行动
+- **📊 任务管理**:内置 Todo 系统,可视化追踪多步骤任务进度
+- **🔧 工具丰富**:集成终端、上下文获取、Note管理
+
+### 适用场景
+
+- ✅ 代码库探索与分析
+- ✅ 智能代码修改与重构
+- ✅ 局部功能修复与优化
+- ✅ 项目结构理解
+- ✅ 代码审查辅助
+- ✅ 技术演示与教学
+
+---
+
+## ✨ 核心特性
+
+### 1. 智能推理引擎
+
+- **ReAct Agent**:结合推理(Reasoning)与行动(Acting),支持多步骤复杂任务
+- **多步跟踪**:Todo Board 实时展示任务进度(pending → in_progress → completed)
+
+### 2. 安全补丁系统
+
+```
+*** Begin Patch
+Update File: src/example.py
+```python
+# 修改后的代码
+```
+*** End Patch
+```
+
+- ✅ 标准化补丁格式
+- ✅ 原子化文件操作
+- ✅ 自动备份(.backup 后缀)
+- ✅ 白名单文件类型控制
+- ✅ 人工确认机制
+
+### 3. 多源上下文构建
+
+**GSSC 流水线**(Gather-Select-Structure-Compress):
+
+```
+用户查询 → 收集信息 → 相关性筛选 → 结构化组织 → Token 压缩 → 生成回复
+```
+
+
+### 4. 工具生态
+
+| 工具类型 | 功能描述 | 主要用途 |
+|---------|---------|---------|
+| **Terminal Tool** | 安全终端执行(白名单命令) | 文件浏览、搜索、文本处理 |
+| **Context Fetch Tool** | 按需代码检索 | 读取特定文件/目录内容 |
+| **Note Tool** | 笔记增删改查 | 记录重要信息、决策点 |
+| **Todo Tool** | 任务管理 | 多步任务跟踪、进度可视化 |
+| **Plan Tool** | 规划生成 | 复杂任务分解与执行计划 |
+| **Memory Tool** | 记忆管理 | 长期知识存储与检索 |
+
+
+
+## 🏗️ 系统架构
+
+### 整体架构图
+┌─────────────────────────────────────────────────────────────┐
+│                     用户交互层 (CLI)                          │
+│                   hello_code_cli.py                          │
+└────────────────────────┬────────────────────────────────────┘
+                         │
+┌────────────────────────▼────────────────────────────────────┐
+│                   智能体层 (Agents)                           │
+│  ┌─────────────┐  ┌─────────────┐                           │
+│  │ ReActAgent  │  │ PlanAgent   │                           │
+│  └─────────────┘  └─────────────┘                           │
+└────────────────────────┬────────────────────────────────────┘
+                         │
+┌────────────────────────▼────────────────────────────────────┐
+│                    核心层 (Core)                              │
+│  ┌──────────┐  ┌──────────┐  ┌──────────┐  ┌──────────┐   │
+│  │   LLM    │  │ Message  │  │  Config  │  │ Exception│   │
+│  └──────────┘  └──────────┘  └──────────┘  └──────────┘   │
+└────────────────────────┬────────────────────────────────────┘
+                         │
+┌────────────────────────▼────────────────────────────────────┐
+│                   能力层 (Capabilities)                       │
+│  ┌─────────────────┐  ┌─────────────────┐                  │
+│  │  Context Builder │  │  Memory Manager │                  │
+│  │   (GSSC流水线)   │  │   (多层记忆)    │                  │
+│  └─────────────────┘  └─────────────────┘                  │
+└────────────────────────┬────────────────────────────────────┘
+                         │
+┌────────────────────────▼────────────────────────────────────┐
+│                    工具层 (Tools)                             │
+│  Terminal │ ContextFetch │ Note │ Todo │ Plan │ Memory │... │
+└────────────────────────┬────────────────────────────────────┘
+                         │
+┌────────────────────────▼────────────────────────────────────┐
+│                   执行器层 (Executors)                        │
+│  ApplyPatchExecutor - 安全补丁应用与文件操作                  │
+└─────────────────────────────────────────────────────────────┘
+```
+
+
+```
+
+### 核心模块说明
+
+#### 1. Agents 层
+- **ReActAgent**:主引擎,循环执行"思考→行动→观察"
+- **PlanSolveAgent**:规划式任务分解
+- **ReflectionAgent**:自我反思与优化
+- **SimpleAgent**:基础对话型 Agent
+
+#### 2. Core 层
+- **HelloAgentsLLM**:统一 LLM 接口,支持 OpenAI/DeepSeek/Qwen 等
+- **Message**:消息抽象与序列化
+- **Config**:配置管理
+- **Exceptions**:异常体系
+
+#### 3. Context 层
+- **ContextBuilder**:GSSC 流水线实现
+- **ContextConfig**:上下文构建配置
+- **ContextPacket**:信息包抽象
+
+#### 4. Memory 层
+- **MemoryManager**:统一记忆管理接口
+- **Types**:WorkingMemory, EpisodicMemory, SemanticMemory, PerceptualMemory
+
+
+#### 5. Tools 层
+- **ToolRegistry**:工具注册与管理
+- **Builtin Tools**:内置工具集
+- **Base**:工具基类与参数定义
+
+#### 6. Executors 层
+- **ApplyPatchExecutor**:补丁解析与安全应用
+
+---
+
+## 🚀 快速开始
+
+### 环境要求
+
+- **Python**:3.10 或更高版本
+- **操作系统**:macOS / Linux / Windows
+
+
+### 安装步骤
+
+#### 1. 克隆项目
+
+```bash
+git clone <repository-url>
+cd YYHDBL-HelloCodeAgentCli
+```
+
+#### 2. 创建虚拟环境
+
+```bash
+python -m venv .venv
+source .venv/bin/activate  # Windows: .venv\Scripts\activate
+```
+
+#### 3. 安装依赖
+
+```bash
+pip install -r requirements.txt
+```
+
+#### 4. 配置环境变量
+
+创建 `.env` 文件:
+
+```bash
+# LLM 配置(必需)
+LLM_BASE_URL=https://api.deepseek.com
+LLM_MODEL=deepseek-chat
+DEEPSEEK_API_KEY=sk-xxxxxxxxxxxx
+```
+
+#### 5. 运行 CLI
+
+```bash
+# 在当前目录启动
+python -m code_agent.hello_code_cli --repo .
+
+# 指定其他代码库
+python -m code_agent.hello_code_cli --repo /path/to/your/project
+```
+
+---
+
+## 📖 使用指南
+
+### 基础用法
+
+启动后进入交互式命令行:
+
+```
+🚀 HelloAgents Code Agent CLI
+📂 Repo: /path/to/project
+💡 输入 'help' 查看帮助,'exit' 退出
+
+👤 > 帮我分析 src/main.py 的入口函数
+
+🤖 Thought: 需要先获取文件内容
+   Action: context_fetch[path=src/main.py]
+   Observation: [文件内容]
+   
+   Thought: 已获取内容,开始分析
+   Action: Finish[分析结果...]
+```
+
+### 项目演示
+
+视频演示:https://www.bilibili.com/video/BV1UzBpBBE75/?vd_source=b0893117eda5f3e931a05d426f9789ed
+
+图片演示:
+
+![image-20251222163957228](/Users/yyhdbl/Library/Application Support/typora-user-images/image-20251222163957228.png)
+
+
+
+### 命令行参数
+
+```bash
+python -m code_agent.hello_code_cli [OPTIONS]
+
+选项:
+  --repo PATH              代码库路径(默认:当前目录)
+  --model TEXT             LLM 模型名称
+  --api-key TEXT           API 密钥
+  --base-url TEXT          API 基础 URL
+  --max-steps INTEGER      最大推理步数(默认:15)
+  --enable-memory          启用记忆系统
+  --enable-rag             启用 RAG 检索
+  --debug                  调试模式
+  --help                   显示帮助信息
+```
+
+---
+
+## 📂 项目结构
+
+```
+YYHDBL-HelloCodeAgentCli/
+├── agents/                      # 智能体实现
+│   ├── react_agent.py          # ReAct 范式
+│   ├── plan_solve_agent.py     # 规划式 Agent
+│   ├── reflection_agent.py     # 反思式 Agent
+│   └── simple_agent.py         # 简单对话 Agent
+│
+├── code_agent/                  # 主应用
+│   ├── hello_code_cli.py       # CLI 入口
+│   ├── agentic/                # Code Agent 实现
+│   │   └── code_agent.py
+│   ├── executors/              # 执行器
+│   │   └── apply_patch_executor.py
+│   └── prompts/                # 提示词模板
+│       ├── system.md
+│       ├── react.md
+│       ├── plan.md
+│       └── tools.md
+│
+├── core/                        # 核心模块
+│   ├── agent.py                # Agent 基类
+│   ├── llm.py                  # LLM 接口
+│   ├── message.py              # 消息抽象
+│   ├── config.py               # 配置管理
+│   └── exceptions.py           # 异常定义
+│
+├── context/                     # 上下文构建
+│   └── builder.py              # GSSC 流水线
+│
+├── memory/                      # 记忆系统
+│   ├── manager.py              # 记忆管理器
+│   ├── base.py                 # 基础定义
+│   ├── embedding.py            # 嵌入模型
+│   ├── types/                  # 记忆类型
+│   │   ├── working.py
+│   │   ├── episodic.py
+│   │   ├── semantic.py
+│   │   └── perceptual.py
+│   ├── storage/                # 存储后端
+│   │   ├── document_store.py
+│   │   ├── qdrant_store.py
+│   │   └── neo4j_store.py
+│   └── rag/                    # RAG 系统
+│       ├── document.py
+│       └── pipeline.py
+│
+├── tools/                       # 工具系统
+│   ├── base.py                 # 工具基类
+│   ├── registry.py             # 工具注册表
+│   ├── chain.py                # 工具链
+│   ├── async_executor.py      # 异步执行
+│   └── builtin/                # 内置工具
+│       ├── terminal_tool.py
+│       ├── context_fetch_tool.py
+│       ├── note_tool.py
+│       ├── todo_tool.py
+│       ├── plan_tool.py
+│       ├── memory_tool.py
+│       └── ...
+│
+├── utils/                       # 工具函数
+│   ├── cli_ui.py               # CLI 界面
+│   ├── helpers.py              # 辅助函数
+│   ├── logging.py              # 日志配置
+│   └── serialization.py        # 序列化工具
+---
+
+```
+
+## 🔮 未来规划
+
+### 近期计划
+
+- [ ] **会话恢复**:支持断点续传,自动恢复摘要
+- [ ] **细分终端命令工具**:将Terminal Tool拆分为原子性的命令工具
+- [ ] **改写Note Tool**:
+- [ ] **改写记忆系统**:
+
+
+## 📄 许可证
+
+本项目采用 [MIT License](LICENSE) 开源协议。
+
+---
+
+## 👥 作者与致谢
+
+### 核心贡献者
+
+- **YYHDBL** - *项目维护者*
+
+### 特别鸣谢
+
+- **Datawhale 社区**:提供学习资源与支持
+- **Hello-Agents 项目**:提供框架基础
+- **OpenAI & DeepSeek**:LLM 技术支持
+
+---
+
+
+
+<div align="center">
+
+**如果这个项目对你有帮助,请给一个 ⭐ Star!**
+
+Made with ❤️ by HelloAgents Community
+
+</div>

+ 66 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/__init__.py

@@ -0,0 +1,66 @@
+"""
+HelloAgents - 灵活、可扩展的多智能体框架
+
+基于OpenAI原生API构建,提供简洁高效的智能体开发体验。
+"""
+
+# 配置第三方库的日志级别,减少噪音
+import logging
+logging.getLogger("httpx").setLevel(logging.WARNING)
+logging.getLogger("qdrant_client").setLevel(logging.WARNING)
+logging.getLogger("urllib3").setLevel(logging.WARNING)
+logging.getLogger("neo4j").setLevel(logging.WARNING)
+logging.getLogger("neo4j.notifications").setLevel(logging.WARNING)
+
+from .version import __version__, __author__, __email__, __description__
+
+# 核心组件
+from .core.llm import HelloAgentsLLM
+from .core.config import Config
+from .core.message import Message
+from .core.exceptions import HelloAgentsException
+
+# Agent实现
+from .agents.simple_agent import SimpleAgent
+from .agents.react_agent import ReActAgent
+from .agents.reflection_agent import ReflectionAgent
+from .agents.plan_solve_agent import PlanAndSolveAgent
+
+# 工具系统
+from .tools.registry import ToolRegistry, global_registry
+from .tools.builtin.search import SearchTool, search
+from .tools.builtin.calculator import CalculatorTool, calculate
+from .tools.chain import ToolChain, ToolChainManager
+from .tools.async_executor import AsyncToolExecutor
+
+__all__ = [
+    # 版本信息
+    "__version__",
+    "__author__",
+    "__email__",
+    "__description__",
+
+    # 核心组件
+    "HelloAgentsLLM",
+    "Config",
+    "Message",
+    "HelloAgentsException",
+
+    # Agent范式
+    "SimpleAgent",
+    "ReActAgent", 
+    "ReflectionAgent",
+    "PlanAndSolveAgent",
+
+    # 工具系统
+    "ToolRegistry",
+    "global_registry",
+    "SearchTool",
+    "search",
+    "CalculatorTool",
+    "calculate",
+    "ToolChain",
+    "ToolChainManager",
+    "AsyncToolExecutor",
+]
+

+ 7 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/agents/__init__.py

@@ -0,0 +1,7 @@
+"""Agent implementations.
+
+Keep this module lightweight: avoid importing optional dependencies at import time.
+Import concrete agents directly, e.g. `from agents.simple_agent import SimpleAgent`.
+"""
+
+__all__ = ["SimpleAgent", "ReActAgent", "ReflectionAgent", "PlanAndSolveAgent"]

+ 201 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/agents/plan_solve_agent.py

@@ -0,0 +1,201 @@
+"""Plan and Solve Agent实现 - 分解规划与逐步执行的智能体"""
+
+import ast
+from typing import Optional, List, Dict
+from core.agent import Agent
+from core.llm import HelloAgentsLLM
+from core.config import Config
+from core.message import Message
+
+# 默认规划器提示词模板
+DEFAULT_PLANNER_PROMPT = """
+你是一个顶级的AI规划专家。你的任务是将用户提出的复杂问题分解成一个由多个简单步骤组成的行动计划。
+请确保计划中的每个步骤都是一个独立的、可执行的子任务,并且严格按照逻辑顺序排列。
+你的输出必须是一个Python列表,其中每个元素都是一个描述子任务的字符串。
+
+问题: {question}
+
+请严格按照以下格式输出你的计划:
+```python
+["步骤1", "步骤2", "步骤3", ...]
+```
+"""
+
+# 默认执行器提示词模板
+DEFAULT_EXECUTOR_PROMPT = """
+你是一位顶级的AI执行专家。你的任务是严格按照给定的计划,一步步地解决问题。
+你将收到原始问题、完整的计划、以及到目前为止已经完成的步骤和结果。
+请你专注于解决"当前步骤",并仅输出该步骤的最终答案,不要输出任何额外的解释或对话。
+
+# 原始问题:
+{question}
+
+# 完整计划:
+{plan}
+
+# 历史步骤与结果:
+{history}
+
+# 当前步骤:
+{current_step}
+
+请仅输出针对"当前步骤"的回答:
+"""
+
+class Planner:
+    """规划器 - 负责将复杂问题分解为简单步骤"""
+
+    def __init__(self, llm_client: HelloAgentsLLM, prompt_template: Optional[str] = None):
+        self.llm_client = llm_client
+        self.prompt_template = prompt_template if prompt_template else DEFAULT_PLANNER_PROMPT
+
+    def plan(self, question: str, **kwargs) -> List[str]:
+        """
+        生成执行计划
+
+        Args:
+            question: 要解决的问题
+            **kwargs: LLM调用参数
+
+        Returns:
+            步骤列表
+        """
+        prompt = self.prompt_template.format(question=question)
+        messages = [{"role": "user", "content": prompt}]
+
+        print("--- 正在生成计划 ---")
+        response_text = self.llm_client.invoke(messages, **kwargs) or ""
+        print(f"✅ 计划已生成:\n{response_text}")
+
+        try:
+            # 提取Python代码块中的列表
+            plan_str = response_text.split("```python")[1].split("```")[0].strip()
+            plan = ast.literal_eval(plan_str)
+            return plan if isinstance(plan, list) else []
+        except (ValueError, SyntaxError, IndexError) as e:
+            print(f"❌ 解析计划时出错: {e}")
+            print(f"原始响应: {response_text}")
+            return []
+        except Exception as e:
+            print(f"❌ 解析计划时发生未知错误: {e}")
+            return []
+
+class Executor:
+    """执行器 - 负责按计划逐步执行"""
+
+    def __init__(self, llm_client: HelloAgentsLLM, prompt_template: Optional[str] = None):
+        self.llm_client = llm_client
+        self.prompt_template = prompt_template if prompt_template else DEFAULT_EXECUTOR_PROMPT
+
+    def execute(self, question: str, plan: List[str], **kwargs) -> str:
+        """
+        按计划执行任务
+
+        Args:
+            question: 原始问题
+            plan: 执行计划
+            **kwargs: LLM调用参数
+
+        Returns:
+            最终答案
+        """
+        history = ""
+        final_answer = ""
+
+        print("\n--- 正在执行计划 ---")
+        for i, step in enumerate(plan, 1):
+            print(f"\n-> 正在执行步骤 {i}/{len(plan)}: {step}")
+            prompt = self.prompt_template.format(
+                question=question,
+                plan=plan,
+                history=history if history else "无",
+                current_step=step
+            )
+            messages = [{"role": "user", "content": prompt}]
+
+            response_text = self.llm_client.invoke(messages, **kwargs) or ""
+
+            history += f"步骤 {i}: {step}\n结果: {response_text}\n\n"
+            final_answer = response_text
+            print(f"✅ 步骤 {i} 已完成,结果: {final_answer}")
+
+        return final_answer
+
+class PlanAndSolveAgent(Agent):
+    """
+    Plan and Solve Agent - 分解规划与逐步执行的智能体
+    
+    这个Agent能够:
+    1. 将复杂问题分解为简单步骤
+    2. 按照计划逐步执行
+    3. 维护执行历史和上下文
+    4. 得出最终答案
+    
+    特别适合多步骤推理、数学问题、复杂分析等任务。
+    """
+    
+    def __init__(
+        self,
+        name: str,
+        llm: HelloAgentsLLM,
+        system_prompt: Optional[str] = None,
+        config: Optional[Config] = None,
+        custom_prompts: Optional[Dict[str, str]] = None
+    ):
+        """
+        初始化PlanAndSolveAgent
+
+        Args:
+            name: Agent名称
+            llm: LLM实例
+            system_prompt: 系统提示词
+            config: 配置对象
+            custom_prompts: 自定义提示词模板 {"planner": "", "executor": ""}
+        """
+        super().__init__(name, llm, system_prompt, config)
+
+        # 设置提示词模板:用户自定义优先,否则使用默认模板
+        if custom_prompts:
+            planner_prompt = custom_prompts.get("planner")
+            executor_prompt = custom_prompts.get("executor")
+        else:
+            planner_prompt = None
+            executor_prompt = None
+
+        self.planner = Planner(self.llm, planner_prompt)
+        self.executor = Executor(self.llm, executor_prompt)
+    
+    def run(self, input_text: str, **kwargs) -> str:
+        """
+        运行Plan and Solve Agent
+        
+        Args:
+            input_text: 要解决的问题
+            **kwargs: 其他参数
+            
+        Returns:
+            最终答案
+        """
+        print(f"\n🤖 {self.name} 开始处理问题: {input_text}")
+        
+        # 1. 生成计划
+        plan = self.planner.plan(input_text, **kwargs)
+        if not plan:
+            final_answer = "无法生成有效的行动计划,任务终止。"
+            print(f"\n--- 任务终止 ---\n{final_answer}")
+            
+            # 保存到历史记录
+            self.add_message(Message(input_text, "user"))
+            self.add_message(Message(final_answer, "assistant"))
+            
+            return final_answer
+        
+        # 2. 执行计划
+        final_answer = self.executor.execute(input_text, plan, **kwargs)
+        print(f"\n--- 任务完成 ---\n最终答案: {final_answer}")
+        
+        # 保存到历史记录
+        self.add_message(Message(input_text, "user"))
+        self.add_message(Message(final_answer, "assistant"))
+        
+        return final_answer

+ 428 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/agents/react_agent.py

@@ -0,0 +1,428 @@
+"""ReAct Agent实现 - 推理与行动结合的智能体"""
+
+import re
+from typing import Optional, List, Tuple, Callable, Dict, Any
+from core.agent import Agent
+from core.llm import HelloAgentsLLM
+from core.config import Config
+from core.message import Message
+from tools.registry import ToolRegistry
+from utils.cli_ui import Spinner, c, PRIMARY, ACCENT, INFO, hr, log_tool_event, clamp_text
+
+# 默认ReAct提示词模板
+DEFAULT_REACT_PROMPT = """你是一个具备推理和行动能力的AI助手。你可以通过思考分析问题,然后调用合适的工具来获取信息,最终给出准确的答案。
+
+## 可用工具
+{tools}
+
+## 工作流程
+请严格按照以下格式进行回应,每次只能执行一个步骤:
+
+**Thought:** 分析当前问题,思考需要什么信息或采取什么行动。
+**Action:** 选择一个行动,格式必须是以下之一:
+- `{{tool_name}}[{{tool_input}}]` - 调用指定工具
+- `Finish[最终答案]` - 当你有足够信息给出最终答案时
+
+## 重要提醒
+1. 每次回应必须包含Thought和Action两部分
+2. 工具调用的格式必须严格遵循:工具名[参数]
+3. 只有当你确信有足够信息回答问题时,才使用Finish
+4. 如果工具返回的信息不够,继续使用其他工具或相同工具的不同参数
+
+## 当前任务
+**Question:** {question}
+
+## 执行历史
+{history}
+
+现在开始你的推理和行动:"""
+
+class ReActAgent(Agent):
+    """
+    ReAct (Reasoning and Acting) Agent
+    
+    结合推理和行动的智能体,能够:
+    1. 分析问题并制定行动计划
+    2. 调用外部工具获取信息
+    3. 基于观察结果进行推理
+    4. 迭代执行直到得出最终答案
+    
+    这是一个经典的Agent范式,特别适合需要外部信息的任务。
+    """
+    
+    def __init__(
+        self,
+        name: str,
+        llm: HelloAgentsLLM,
+        tool_registry: Optional[ToolRegistry] = None,
+        system_prompt: Optional[str] = None,
+        config: Optional[Config] = None,
+        max_steps: int = 5,
+        custom_prompt: Optional[str] = None,
+        observation_summarizer: Optional[Callable[[str, str, str], str]] = None,
+        summarize_threshold_chars: int = 2000,
+        finalize_on_max_steps: bool = True,
+        early_stop_on_repeat: bool = True,
+        repeat_action_threshold: int = 2,
+    ):
+        """
+        初始化ReActAgent
+
+        Args:
+            name: Agent名称
+            llm: LLM实例
+            tool_registry: 工具注册表(可选,如果不提供则创建空的工具注册表)
+            system_prompt: 系统提示词
+            config: 配置对象
+            max_steps: 最大执行步数
+            custom_prompt: 自定义提示词模板
+        """
+        super().__init__(name, llm, system_prompt, config)
+
+        # 如果没有提供tool_registry,创建一个空的
+        if tool_registry is None:
+            self.tool_registry = ToolRegistry()
+        else:
+            self.tool_registry = tool_registry
+
+        self.max_steps = max_steps
+        self.current_history: List[str] = []
+        self.last_trace: List[Dict[str, Any]] = []
+        self.observation_summarizer = observation_summarizer
+        self.summarize_threshold_chars = summarize_threshold_chars
+        self.finalize_on_max_steps = finalize_on_max_steps
+        self.early_stop_on_repeat = early_stop_on_repeat
+        self.repeat_action_threshold = repeat_action_threshold
+
+        # 设置提示词模板:用户自定义优先,否则使用默认模板
+        self.prompt_template = custom_prompt if custom_prompt else DEFAULT_REACT_PROMPT
+
+    def add_tool(self, tool):
+        """
+        添加工具到工具注册表
+        支持MCP工具的自动展开
+
+        Args:
+            tool: 工具实例(可以是普通Tool或MCPTool)
+        """
+        # 检查是否是MCP工具
+        if hasattr(tool, 'auto_expand') and tool.auto_expand:
+            # MCP工具会自动展开为多个工具
+            if hasattr(tool, '_available_tools') and tool._available_tools:
+                for mcp_tool in tool._available_tools:
+                    # 创建包装工具
+                    from tools.base import Tool
+                    wrapped_tool = Tool(
+                        name=f"{tool.name}_{mcp_tool['name']}",
+                        description=mcp_tool.get('description', ''),
+                        func=lambda input_text, t=tool, tn=mcp_tool['name']: t.run({
+                            "action": "call_tool",
+                            "tool_name": tn,
+                            "arguments": {"input": input_text}
+                        })
+                    )
+                    self.tool_registry.register_tool(wrapped_tool)
+                print(f"✅ MCP工具 '{tool.name}' 已展开为 {len(tool._available_tools)} 个独立工具")
+            else:
+                self.tool_registry.register_tool(tool)
+        else:
+            self.tool_registry.register_tool(tool)
+
+    def run(self, input_text: str, **kwargs) -> str:
+        """
+        运行ReAct Agent
+        
+        Args:
+            input_text: 用户问题
+            **kwargs: 其他参数
+            
+        Returns:
+            最终答案
+        """
+        self.current_history = []
+        self.last_trace = []
+        current_step = 0
+        
+        # Avoid dumping huge stitched prompts to console (CLI UX)
+        preview = input_text.replace("\n", " ")
+        if len(preview) > 160:
+            preview = preview[:160] + "..."
+        print("\n" + hr("=", 80))
+        print(c(f"🤖 {self.name}", PRIMARY) + " " + c(f"{preview}", INFO))
+        print(hr("=", 80))
+        
+        repeat_count = 0
+        last_action_sig: Optional[str] = None
+
+        while current_step < self.max_steps:
+            current_step += 1
+            print(c(f"\n--- Step {current_step}/{self.max_steps} ---", ACCENT))
+            
+            # 构建提示词
+            tools_desc = self.tool_registry.get_tools_description()
+            history_str = "\n".join(self.current_history)
+            prompt = self.prompt_template.format(
+                tools=tools_desc,
+                question=input_text,
+                history=history_str
+            )
+            
+            # 调用LLM
+            messages = [{"role": "user", "content": prompt}]
+            spinner = Spinner("Thinking…")
+            spinner.start()
+            response_text = self.llm.invoke(messages, **kwargs)
+            spinner.stop()
+            
+            if not response_text:
+                print("❌ 错误:LLM未能返回有效响应。")
+                break
+            
+            # 解析输出
+            thought, action = self._parse_output(response_text)
+            
+            if thought:
+                print(c("Thought:", INFO), thought)
+            
+            if not action:
+                # One forced retry: ask model to rewrite in strict format (helps for greetings / bilingual models)
+                try:
+                    repair_sys = (
+                        "You MUST output exactly two lines:\n"
+                        "Thought: ...\n"
+                        "Action: tool_name[tool_input] OR Finish[final answer]\n"
+                        "No extra text. No markdown headers."
+                    )
+                    repair_user = f"Rewrite the following into the required two-line format:\n\n{response_text}"
+                    spinner = Spinner("Repairing format…")
+                    spinner.start()
+                    repaired = self.llm.invoke(
+                        [{"role": "system", "content": repair_sys}, {"role": "user", "content": repair_user}],
+                        max_tokens=200,
+                    )
+                    spinner.stop()
+                    thought, action = self._parse_output(repaired or "")
+                except Exception:
+                    pass
+
+                if not action:
+                    print("⚠️ 警告:未能解析出有效的Action,流程终止。")
+                    break
+            
+            # 检查是否完成
+            if action.startswith("Finish"):
+                final_answer = self._parse_action_input(action)
+                print(c("Finish:", PRIMARY))
+                print(final_answer)
+                
+                # 保存到历史记录
+                self.add_message(Message(input_text, "user"))
+                self.add_message(Message(final_answer, "assistant"))
+                
+                return final_answer
+            
+            # 执行工具调用
+            tool_name, tool_input = self._parse_action(action)
+            if not tool_name or tool_input is None:
+                self.current_history.append("Observation: 无效的Action格式,请检查。")
+                continue
+            
+            log_tool_event(tool_name, tool_input)
+            
+            # 调用工具
+            observation = self.tool_registry.execute_tool(tool_name, tool_input)
+            observation_full = observation
+            observation_summary = None
+            if (
+                self.observation_summarizer is not None
+                and isinstance(observation, str)
+                and len(observation) > self.summarize_threshold_chars
+            ):
+                try:
+                    observation_summary = self.observation_summarizer(tool_name, tool_input, observation)
+                    if observation_summary and isinstance(observation_summary, str):
+                        observation = observation_summary.strip() + "\n...truncated...\n"
+                except Exception:
+                    # fall back to raw observation
+                    pass
+
+            log_tool_event(f"{tool_name} result", clamp_text(str(observation), limit=6000))
+
+            # 提前终止:重复相同 action 且无明显进展
+            action_sig = f"{tool_name}|{tool_input}".strip()
+            if self.early_stop_on_repeat:
+                if last_action_sig == action_sig:
+                    repeat_count += 1
+                else:
+                    repeat_count = 0
+                last_action_sig = action_sig
+
+                if repeat_count >= self.repeat_action_threshold:
+                    self.current_history.append("Observation: 已检测到重复行动,建议停止继续工具调用并给出当前能提供的结论/下一步。")
+                    break
+            
+            # 更新历史
+            self.current_history.append(f"Action: {action}")
+            self.current_history.append(f"Observation: {observation}")
+            self.last_trace.append(
+                {
+                    "action": action,
+                    "tool_name": tool_name,
+                    "tool_input": tool_input,
+                    "observation_full_len": len(observation_full) if isinstance(observation_full, str) else None,
+                    "observation_summary": observation_summary,
+                }
+            )
+        
+        # 未在循环内 Finish:进行兜底收敛
+        if self.finalize_on_max_steps:
+            try:
+                tools_desc = self.tool_registry.get_tools_description()
+                history_str = "\n".join(self.current_history[-24:])
+                finalize_prompt = (
+                    "你是一个 ReAct 代理的最终收敛器。现在工具调用阶段结束了。"
+                    "请基于已有的 Thought/Action/Observation 历史,给出一个尽可能有用的最终回答。"
+                    "要求:\n"
+                    "1) 不要再调用工具\n"
+                    "2) 明确已完成的证据/发现\n"
+                    "3) 如果信息不足,说清楚缺少什么,并给出下一步最小化建议(1-3条)\n"
+                )
+                messages = [
+                    {"role": "system", "content": finalize_prompt},
+                    {"role": "user", "content": f"Question:\n{input_text}\n\nTools:\n{tools_desc}\n\nTrace:\n{history_str}"},
+                ]
+                final_answer = self.llm.invoke(messages, max_tokens=600)
+                if final_answer:
+                    self.add_message(Message(input_text, "user"))
+                    self.add_message(Message(final_answer, "assistant"))
+                    return final_answer
+            except Exception:
+                pass
+
+        print("⏰ 已达到最大步数,流程终止。")
+        final_answer = "抱歉,我无法在限定步数内完成这个任务。你可以缩小范围或指定目标文件/模块。"
+        
+        # 保存到历史记录
+        self.add_message(Message(input_text, "user"))
+        self.add_message(Message(final_answer, "assistant"))
+        
+        return final_answer
+    
+    def _parse_output(self, text: str) -> Tuple[Optional[str], Optional[str]]:
+        """解析LLM输出,提取思考和行动。
+
+        兼容常见变体:
+        - Thought/Action 的全角冒号(:)
+        - 中文标签:思考/行动
+        - Markdown 强调:**Thought:** / **Action:**
+        """
+        # Normalize to make regex easier
+        t = (text or "").strip()
+
+        # Primary: strict 2-line format, allow markdown markers and fullwidth colon
+        m = re.search(
+            r"(?:\*\*)?(Thought|思考)(?:\*\*)?\s*[::]\s*(.*?)\n(?:\*\*)?(Action|行动)(?:\*\*)?\s*[::]\s*(.*)\s*$",
+            t,
+            flags=re.DOTALL,
+        )
+        if m:
+            thought = m.group(2).strip()
+            action = m.group(4).strip()
+            return thought or None, action or None
+
+        # Fallback: find first Thought-like line and first Action-like line anywhere
+        thought_match = re.search(r"(?:\*\*)?(Thought|思考)(?:\*\*)?\s*[::]\s*(.*)", t)
+        action_match = re.search(r"(?:\*\*)?(Action|行动)(?:\*\*)?\s*[::]\s*(.*)", t)
+        thought = thought_match.group(2).strip() if thought_match else None
+        action_raw = action_match.group(2).strip() if action_match else None
+        
+        # 关键修复:如果 action 中包含另一个 Thought/Action/Observation,截断到该位置
+        # 防止模型一次输出多个 Thought/Action 循环时,把后续内容都当作第一个 Action 的输入
+        if action_raw:
+            stop_patterns = [
+                r"\nThought:", r"\n思考:", r"\nAction:", r"\n行动:",
+                r"\nObservation:", r"\n观察:", r"\n\*\*Thought", r"\n\*\*Action",
+            ]
+            earliest_stop = len(action_raw)
+            for pat in stop_patterns:
+                m = re.search(pat, action_raw, re.IGNORECASE)
+                if m and m.start() < earliest_stop:
+                    earliest_stop = m.start()
+            action_raw = action_raw[:earliest_stop].strip()
+        
+        return thought, action_raw
+    
+    def _parse_action(self, action_text: str) -> Tuple[Optional[str], Optional[str]]:
+        """解析行动文本,提取工具名称和输入
+        
+        使用括号匹配算法而非贪婪正则,正确处理嵌套 JSON。
+        """
+        # 先找工具名
+        name_match = re.match(r"(\w+)\[", action_text)
+        if not name_match:
+            return None, None
+        
+        tool_name = name_match.group(1)
+        start = name_match.end() - 1  # '[' 的位置
+        
+        # 使用括号匹配找到对应的 ']'
+        depth = 0
+        in_string = False
+        escape = False
+        end_pos = None
+        
+        for i, c in enumerate(action_text[start:], start):
+            if escape:
+                escape = False
+                continue
+            if c == '\\' and in_string:
+                escape = True
+                continue
+            if c == '"' and not escape:
+                in_string = not in_string
+                continue
+            if in_string:
+                continue
+            if c == '[':
+                depth += 1
+            elif c == ']':
+                depth -= 1
+                if depth == 0:
+                    end_pos = i
+                    break
+        
+        if end_pos is not None:
+            tool_input = action_text[start + 1:end_pos]
+            return tool_name, tool_input
+        
+        # fallback: 如果括号不匹配,尝试简单正则(不跨行)
+        # 注意:不使用 re.DOTALL,这样 . 不会匹配换行符
+        match = re.match(r"(\w+)\[([^\n]*)\]", action_text)
+        if match:
+            return match.group(1), match.group(2)
+        
+        return None, None
+    
+    def _parse_action_input(self, action_text: str) -> str:
+        """解析行动输入
+
+        兼容多种 Finish 书写:
+        - Finish[...]
+        - Finish:... / Finish: ...(无方括号)
+        - Finish\n<content>(换行后直接给内容/补丁)
+        """
+        # 规范格式:Finish[...]
+        match = re.match(r"\w+\[(.*)\]\s*$", action_text, flags=re.DOTALL)
+        if match:
+            return match.group(1)
+
+        # 宽松格式:Finish: ... 或 Finish:...
+        m2 = re.match(r"finish\s*[::]\s*(.*)", action_text, flags=re.IGNORECASE | re.DOTALL)
+        if m2:
+            return m2.group(1)
+
+        # 再宽松:去掉前缀 "Finish" 后的剩余内容
+        if action_text.lower().startswith("finish"):
+            return action_text[len("finish"):].strip()
+
+        return ""

+ 180 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/agents/reflection_agent.py

@@ -0,0 +1,180 @@
+"""Reflection Agent实现 - 自我反思与迭代优化的智能体"""
+
+from typing import Optional, List, Dict, Any
+from core.agent import Agent
+from core.llm import HelloAgentsLLM
+from core.config import Config
+from core.message import Message
+
+# 默认提示词模板
+DEFAULT_PROMPTS = {
+    "initial": """
+请根据以下要求完成任务:
+
+任务: {task}
+
+请提供一个完整、准确的回答。
+""",
+    "reflect": """
+请仔细审查以下回答,并找出可能的问题或改进空间:
+
+# 原始任务:
+{task}
+
+# 当前回答:
+{content}
+
+请分析这个回答的质量,指出不足之处,并提出具体的改进建议。
+如果回答已经很好,请回答"无需改进"。
+""",
+    "refine": """
+请根据反馈意见改进你的回答:
+
+# 原始任务:
+{task}
+
+# 上一轮回答:
+{last_attempt}
+
+# 反馈意见:
+{feedback}
+
+请提供一个改进后的回答。
+"""
+}
+
+class Memory:
+    """
+    简单的短期记忆模块,用于存储智能体的行动与反思轨迹。
+    """
+    def __init__(self):
+        self.records: List[Dict[str, Any]] = []
+
+    def add_record(self, record_type: str, content: str):
+        """向记忆中添加一条新记录"""
+        self.records.append({"type": record_type, "content": content})
+        print(f"📝 记忆已更新,新增一条 '{record_type}' 记录。")
+
+    def get_trajectory(self) -> str:
+        """将所有记忆记录格式化为一个连贯的字符串文本"""
+        trajectory = ""
+        for record in self.records:
+            if record['type'] == 'execution':
+                trajectory += f"--- 上一轮尝试 (代码) ---\n{record['content']}\n\n"
+            elif record['type'] == 'reflection':
+                trajectory += f"--- 评审员反馈 ---\n{record['content']}\n\n"
+        return trajectory.strip()
+
+    def get_last_execution(self) -> str:
+        """获取最近一次的执行结果"""
+        for record in reversed(self.records):
+            if record['type'] == 'execution':
+                return record['content']
+        return ""
+
+class ReflectionAgent(Agent):
+    """
+    Reflection Agent - 自我反思与迭代优化的智能体
+
+    这个Agent能够:
+    1. 执行初始任务
+    2. 对结果进行自我反思
+    3. 根据反思结果进行优化
+    4. 迭代改进直到满意
+
+    特别适合代码生成、文档写作、分析报告等需要迭代优化的任务。
+
+    支持多种专业领域的提示词模板,用户可以自定义或使用内置模板。
+    """
+
+    def __init__(
+        self,
+        name: str,
+        llm: HelloAgentsLLM,
+        system_prompt: Optional[str] = None,
+        config: Optional[Config] = None,
+        max_iterations: int = 3,
+        custom_prompts: Optional[Dict[str, str]] = None
+    ):
+        """
+        初始化ReflectionAgent
+
+        Args:
+            name: Agent名称
+            llm: LLM实例
+            system_prompt: 系统提示词
+            config: 配置对象
+            max_iterations: 最大迭代次数
+            custom_prompts: 自定义提示词模板 {"initial": "", "reflect": "", "refine": ""}
+        """
+        super().__init__(name, llm, system_prompt, config)
+        self.max_iterations = max_iterations
+        self.memory = Memory()
+
+        # 设置提示词模板:用户自定义优先,否则使用默认模板
+        self.prompts = custom_prompts if custom_prompts else DEFAULT_PROMPTS
+    
+    def run(self, input_text: str, **kwargs) -> str:
+        """
+        运行Reflection Agent
+
+        Args:
+            input_text: 任务描述
+            **kwargs: 其他参数
+
+        Returns:
+            最终优化后的结果
+        """
+        print(f"\n🤖 {self.name} 开始处理任务: {input_text}")
+
+        # 重置记忆
+        self.memory = Memory()
+
+        # 1. 初始执行
+        print("\n--- 正在进行初始尝试 ---")
+        initial_prompt = self.prompts["initial"].format(task=input_text)
+        initial_result = self._get_llm_response(initial_prompt, **kwargs)
+        self.memory.add_record("execution", initial_result)
+
+        # 2. 迭代循环:反思与优化
+        for i in range(self.max_iterations):
+            print(f"\n--- 第 {i+1}/{self.max_iterations} 轮迭代 ---")
+
+            # a. 反思
+            print("\n-> 正在进行反思...")
+            last_result = self.memory.get_last_execution()
+            reflect_prompt = self.prompts["reflect"].format(
+                task=input_text,
+                content=last_result
+            )
+            feedback = self._get_llm_response(reflect_prompt, **kwargs)
+            self.memory.add_record("reflection", feedback)
+
+            # b. 检查是否需要停止
+            if "无需改进" in feedback or "no need for improvement" in feedback.lower():
+                print("\n✅ 反思认为结果已无需改进,任务完成。")
+                break
+
+            # c. 优化
+            print("\n-> 正在进行优化...")
+            refine_prompt = self.prompts["refine"].format(
+                task=input_text,
+                last_attempt=last_result,
+                feedback=feedback
+            )
+            refined_result = self._get_llm_response(refine_prompt, **kwargs)
+            self.memory.add_record("execution", refined_result)
+
+        final_result = self.memory.get_last_execution()
+        print(f"\n--- 任务完成 ---\n最终结果:\n{final_result}")
+
+        # 保存到历史记录
+        self.add_message(Message(input_text, "user"))
+        self.add_message(Message(final_result, "assistant"))
+
+        return final_result
+    
+    def _get_llm_response(self, prompt: str, **kwargs) -> str:
+        """调用LLM并获取完整响应"""
+        messages = [{"role": "user", "content": prompt}]
+        return self.llm.invoke(messages, **kwargs) or ""

+ 407 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/agents/simple_agent.py

@@ -0,0 +1,407 @@
+"""简单Agent实现 - 基于OpenAI原生API"""
+
+from typing import Optional, Iterator, TYPE_CHECKING, Callable
+import re
+
+from core.agent import Agent
+from core.llm import HelloAgentsLLM
+from core.config import Config
+from core.message import Message
+
+if TYPE_CHECKING:
+    from tools.registry import ToolRegistry
+
+class SimpleAgent(Agent):
+    """简单的对话Agent,支持可选的工具调用"""
+    
+    def __init__(
+        self,
+        name: str,
+        llm: HelloAgentsLLM,
+        system_prompt: Optional[str] = None,
+        config: Optional[Config] = None,
+        tool_registry: Optional['ToolRegistry'] = None,
+        enable_tool_calling: bool = True,
+        tool_confirm_callback: Optional[Callable[[str, dict], bool]] = None,
+    ):
+        """
+        初始化SimpleAgent
+        
+        Args:
+            name: Agent名称
+            llm: LLM实例
+            system_prompt: 系统提示词
+            config: 配置对象
+            tool_registry: 工具注册表(可选,如果提供则启用工具调用)
+            enable_tool_calling: 是否启用工具调用(只有在提供tool_registry时生效)
+        """
+        super().__init__(name, llm, system_prompt, config)
+        self.tool_registry = tool_registry
+        self.enable_tool_calling = enable_tool_calling and tool_registry is not None
+        self.tool_confirm_callback = tool_confirm_callback
+    
+    def _get_enhanced_system_prompt(self) -> str:
+        """构建增强的系统提示词,包含工具信息"""
+        base_prompt = self.system_prompt or "你是一个有用的AI助手。"
+        
+        if not self.enable_tool_calling or not self.tool_registry:
+            return base_prompt
+        
+        # 获取工具描述
+        tools_description = self.tool_registry.get_tools_description()
+        if not tools_description or tools_description == "暂无可用工具":
+            return base_prompt
+        
+        tools_section = "\n\n## 可用工具\n"
+        tools_section += "你可以使用以下工具来帮助回答问题:\n"
+        tools_section += tools_description + "\n"
+
+        tools_section += "\n## 工具调用格式\n"
+        tools_section += "当需要使用工具时,请使用以下格式:\n"
+        tools_section += "`[TOOL_CALL:{tool_name}:{parameters}]`\n\n"
+
+        tools_section += "### 参数格式说明\n"
+        tools_section += "1. **多个参数**:使用 `key=value` 格式,用逗号分隔\n"
+        tools_section += "   示例:`[TOOL_CALL:calculator_multiply:a=12,b=8]`\n"
+        tools_section += "   示例:`[TOOL_CALL:filesystem_read_file:path=README.md]`\n\n"
+        tools_section += "2. **单个参数**:直接使用 `key=value`\n"
+        tools_section += "   示例:`[TOOL_CALL:search:query=Python编程]`\n\n"
+        tools_section += "3. **简单查询**:可以直接传入文本\n"
+        tools_section += "   示例:`[TOOL_CALL:search:Python编程]`\n\n"
+
+        tools_section += "### 重要提示\n"
+        tools_section += "- 参数名必须与工具定义的参数名完全匹配\n"
+        tools_section += "- 数字参数直接写数字,不需要引号:`a=12` 而不是 `a=\"12\"`\n"
+        tools_section += "- 文件路径等字符串参数直接写:`path=README.md`\n"
+        tools_section += "- 工具调用结果会自动插入到对话中,然后你可以基于结果继续回答\n"
+
+        return base_prompt + tools_section
+    
+    def _parse_tool_calls(self, text: str) -> list:
+        """解析文本中的工具调用"""
+        pattern = r'\[TOOL_CALL:([^:]+):([^\]]+)\]'
+        matches = re.findall(pattern, text)
+        
+        tool_calls = []
+        for tool_name, parameters in matches:
+            tool_calls.append({
+                'tool_name': tool_name.strip(),
+                'parameters': parameters.strip(),
+                'original': f'[TOOL_CALL:{tool_name}:{parameters}]'
+            })
+        
+        return tool_calls
+    
+    def _execute_tool_call(self, tool_name: str, parameters: str) -> str:
+        """执行工具调用"""
+        if not self.tool_registry:
+            return f"❌ 错误:未配置工具注册表"
+
+        try:
+            # 获取Tool对象
+            tool = self.tool_registry.get_tool(tool_name)
+            if not tool:
+                return f"❌ 错误:未找到工具 '{tool_name}'"
+
+            # 智能参数解析
+            param_dict = self._parse_tool_parameters(tool_name, parameters)
+
+            # 交互式确认门(由上层执行器裁决是否允许执行)
+            if self.tool_confirm_callback is not None:
+                try:
+                    allowed = bool(self.tool_confirm_callback(tool_name, param_dict))
+                except Exception as e:
+                    return f"❌ 工具调用确认失败:{str(e)}"
+                if not allowed:
+                    return "⛔️ 已取消本次工具调用(需要用户确认)。"
+
+            # 调用工具
+            result = tool.run(param_dict)
+            return f"🔧 工具 {tool_name} 执行结果:\n{result}"
+
+        except Exception as e:
+            return f"❌ 工具调用失败:{str(e)}"
+
+    def _parse_tool_parameters(self, tool_name: str, parameters: str) -> dict:
+        """智能解析工具参数"""
+        import json
+        param_dict = {}
+
+        # 尝试解析JSON格式
+        if parameters.strip().startswith('{'):
+            try:
+                param_dict = json.loads(parameters)
+                # JSON解析成功,进行类型转换
+                param_dict = self._convert_parameter_types(tool_name, param_dict)
+                return param_dict
+            except json.JSONDecodeError:
+                # JSON解析失败,继续使用其他方式
+                pass
+
+        if '=' in parameters:
+            # 格式: key=value 或 action=search,query=Python
+            if ',' in parameters:
+                # 多个参数:action=search,query=Python,limit=3
+                pairs = parameters.split(',')
+                for pair in pairs:
+                    if '=' in pair:
+                        key, value = pair.split('=', 1)
+                        param_dict[key.strip()] = value.strip()
+            else:
+                # 单个参数:key=value
+                key, value = parameters.split('=', 1)
+                param_dict[key.strip()] = value.strip()
+
+            # 类型转换
+            param_dict = self._convert_parameter_types(tool_name, param_dict)
+
+            # 智能推断action(如果没有指定)
+            if 'action' not in param_dict:
+                param_dict = self._infer_action(tool_name, param_dict)
+        else:
+            # 直接传入参数,根据工具类型智能推断
+            param_dict = self._infer_simple_parameters(tool_name, parameters)
+
+        return param_dict
+
+    def _convert_parameter_types(self, tool_name: str, param_dict: dict) -> dict:
+        """
+        根据工具的参数定义转换参数类型
+
+        Args:
+            tool_name: 工具名称
+            param_dict: 参数字典
+
+        Returns:
+            类型转换后的参数字典
+        """
+        if not self.tool_registry:
+            return param_dict
+
+        tool = self.tool_registry.get_tool(tool_name)
+        if not tool:
+            return param_dict
+
+        # 获取工具的参数定义
+        try:
+            tool_params = tool.get_parameters()
+        except:
+            return param_dict
+
+        # 创建参数类型映射
+        param_types = {}
+        for param in tool_params:
+            param_types[param.name] = param.type
+
+        # 转换参数类型
+        converted_dict = {}
+        for key, value in param_dict.items():
+            if key in param_types:
+                param_type = param_types[key]
+                try:
+                    if param_type == 'number' or param_type == 'integer':
+                        # 转换为数字
+                        if isinstance(value, str):
+                            converted_dict[key] = float(value) if param_type == 'number' else int(value)
+                        else:
+                            converted_dict[key] = value
+                    elif param_type == 'boolean':
+                        # 转换为布尔值
+                        if isinstance(value, str):
+                            converted_dict[key] = value.lower() in ('true', '1', 'yes')
+                        else:
+                            converted_dict[key] = bool(value)
+                    else:
+                        converted_dict[key] = value
+                except (ValueError, TypeError):
+                    # 转换失败,保持原值
+                    converted_dict[key] = value
+            else:
+                converted_dict[key] = value
+
+        return converted_dict
+
+    def _infer_action(self, tool_name: str, param_dict: dict) -> dict:
+        """根据工具类型和参数推断action"""
+        if tool_name == 'memory':
+            if 'recall' in param_dict:
+                param_dict['action'] = 'search'
+                param_dict['query'] = param_dict.pop('recall')
+            elif 'store' in param_dict:
+                param_dict['action'] = 'add'
+                param_dict['content'] = param_dict.pop('store')
+            elif 'query' in param_dict:
+                param_dict['action'] = 'search'
+            elif 'content' in param_dict:
+                param_dict['action'] = 'add'
+        elif tool_name == 'rag':
+            if 'search' in param_dict:
+                param_dict['action'] = 'search'
+                param_dict['query'] = param_dict.pop('search')
+            elif 'query' in param_dict:
+                param_dict['action'] = 'search'
+            elif 'text' in param_dict:
+                param_dict['action'] = 'add_text'
+
+        return param_dict
+
+    def _infer_simple_parameters(self, tool_name: str, parameters: str) -> dict:
+        """为简单参数推断完整的参数字典"""
+        if tool_name == 'rag':
+            return {'action': 'search', 'query': parameters}
+        elif tool_name == 'memory':
+            return {'action': 'search', 'query': parameters}
+        else:
+            return {'input': parameters}
+
+    def run(self, input_text: str, max_tool_iterations: int = 3, **kwargs) -> str:
+        """
+        运行SimpleAgent,支持可选的工具调用
+        
+        Args:
+            input_text: 用户输入
+            max_tool_iterations: 最大工具调用迭代次数(仅在启用工具时有效)
+            **kwargs: 其他参数
+            
+        Returns:
+            Agent响应
+        """
+        # 构建消息列表
+        messages = []
+        
+        # 添加系统消息(可能包含工具信息)
+        enhanced_system_prompt = self._get_enhanced_system_prompt()
+        messages.append({"role": "system", "content": enhanced_system_prompt})
+        
+        # 添加历史消息
+        for msg in self._history:
+            messages.append({"role": msg.role, "content": msg.content})
+        
+        # 添加当前用户消息
+        messages.append({"role": "user", "content": input_text})
+        
+        # 如果没有启用工具调用,使用原有逻辑
+        if not self.enable_tool_calling:
+            response = self.llm.invoke(messages, **kwargs)
+            self.add_message(Message(input_text, "user"))
+            self.add_message(Message(response, "assistant"))
+            return response
+        
+        # 迭代处理,支持多轮工具调用
+        current_iteration = 0
+        final_response = ""
+
+        while current_iteration < max_tool_iterations:
+            # 调用LLM
+            response = self.llm.invoke(messages, **kwargs)
+
+            # 检查是否有工具调用
+            tool_calls = self._parse_tool_calls(response)
+
+            if tool_calls:
+                # 执行所有工具调用并收集结果
+                tool_results = []
+                clean_response = response
+
+                for call in tool_calls:
+                    result = self._execute_tool_call(call['tool_name'], call['parameters'])
+                    tool_results.append(result)
+                    # 从响应中移除工具调用标记
+                    clean_response = clean_response.replace(call['original'], "")
+
+                # 构建包含工具结果的消息
+                messages.append({"role": "assistant", "content": clean_response})
+
+                # 添加工具结果
+                tool_results_text = "\n\n".join(tool_results)
+                messages.append({"role": "user", "content": f"工具执行结果:\n{tool_results_text}\n\n请基于这些结果给出完整的回答。"})
+
+                current_iteration += 1
+                continue
+
+            # 没有工具调用,这是最终回答
+            final_response = response
+            break
+
+        # 如果超过最大迭代次数,获取最后一次回答
+        if current_iteration >= max_tool_iterations and not final_response:
+            final_response = self.llm.invoke(messages, **kwargs)
+        
+        # 保存到历史记录
+        self.add_message(Message(input_text, "user"))
+        self.add_message(Message(final_response, "assistant"))
+
+        return final_response
+
+    def add_tool(self, tool) -> None:
+        """
+        添加工具到Agent(便利方法)
+
+        如果是MCP工具且启用了auto_expand,会自动展开为多个独立工具
+        """
+        if not self.tool_registry:
+            from tools.registry import ToolRegistry
+            self.tool_registry = ToolRegistry()
+            self.enable_tool_calling = True
+
+        # 检查是否是MCP工具且需要展开
+        if hasattr(tool, 'auto_expand') and tool.auto_expand:
+            # 获取展开的工具列表
+            expanded_tools = tool.get_expanded_tools()
+            if expanded_tools:
+                # 注册所有展开的工具
+                for expanded_tool in expanded_tools:
+                    self.tool_registry.register_tool(expanded_tool)
+                print(f"✅ MCP工具 '{tool.name}' 已展开为 {len(expanded_tools)} 个独立工具")
+                return
+
+        # 普通工具或不展开的MCP工具
+        self.tool_registry.register_tool(tool)
+
+    def remove_tool(self, tool_name: str) -> bool:
+        """移除工具(便利方法)"""
+        if self.tool_registry:
+            return self.tool_registry.unregister_tool(tool_name)
+        return False
+
+    def list_tools(self) -> list:
+        """列出所有可用工具"""
+        if self.tool_registry:
+            return self.tool_registry.list_tools()
+        return []
+
+    def has_tools(self) -> bool:
+        """检查是否有可用工具"""
+        return self.enable_tool_calling and self.tool_registry is not None
+
+    def stream_run(self, input_text: str, **kwargs) -> Iterator[str]:
+        """
+        流式运行Agent
+        
+        Args:
+            input_text: 用户输入
+            **kwargs: 其他参数
+            
+        Yields:
+            Agent响应片段
+        """
+        # 构建消息列表
+        messages = []
+        
+        if self.system_prompt:
+            messages.append({"role": "system", "content": self.system_prompt})
+        
+        for msg in self._history:
+            messages.append({"role": msg.role, "content": msg.content})
+        
+        messages.append({"role": "user", "content": input_text})
+        
+        # 流式调用LLM
+        full_response = ""
+        for chunk in self.llm.stream_invoke(messages, **kwargs):
+            full_response += chunk
+            yield chunk
+        
+        # 保存完整对话到历史记录
+        self.add_message(Message(input_text, "user"))
+        self.add_message(Message(full_response, "assistant"))

+ 57 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/README.md

@@ -0,0 +1,57 @@
+# Code Agent(HelloAgents CLI)
+
+这是一个基于 **HelloAgents** 组件(`HelloAgentsLLM` / `ContextBuilder` / `ReActAgent` / `TerminalTool` / `NoteTool` / `MemoryTool`)搭建的简易 Code Agent CLI,目标体验类似 Claude Code/Codex:支持多轮对话、按需探索代码库、生成补丁并在确认后落盘。
+
+## 快速开始
+
+1) 准备依赖与环境变量(建议用 `.env`,不要提交):
+
+- 依赖:安装根目录的 `requirements-mvp.txt`
+- 在仓库根目录创建 `.env`(参考 `.env.example`),至少包含:
+  - `DEEPSEEK_API_KEY=...`(或其他 OpenAI 兼容 provider 的 key)
+  - 可选:`LLM_MODEL_ID=deepseek-chat`、`LLM_BASE_URL=https://api.deepseek.com`
+
+2) 启动 CLI(工作区默认 `.`):
+
+```bash
+python3 -m code_agent.hello_code_cli --repo .
+```
+
+命令:
+- `:quit` 退出
+- `:plan <目标>` 强制生成计划(平时由模型按需调用 `plan[...]` 工具)
+
+## 已实现功能(当前版本)
+
+- 多轮对话:基于 `SimpleAgent`,保留近期对话历史。
+- 智能体范式:核心使用 `ReActAgent`(推理 + 工具调用),规划能力作为可选工具 `plan[...]`。
+- 上下文工程:使用 `ContextBuilder`(GSSC)融合系统指令、对话历史、笔记检索结果、情景记忆(episodic)等,控制上下文预算。
+- 按需探索(更像 Claude Code):
+  - 默认不做全仓扫描;模型只有在需要证据时才会调用 `TerminalTool`(优先 `ls/rg/sed/cat` 小范围命令)。
+- 工具能力:
+  - `TerminalTool`:用于查看/检索(`rg/grep/find/cat/head/tail/sed` 等),并强化了安全限制:
+    - 默认允许 shell 语义(管道等,体验更像 Claude Code)
+    - 重定向/子命令替换/危险命令会要求用户确认(`allow_dangerous=true` + 交互确认)
+    - `git` 默认仅允许 `status/diff`;`git reset --hard` 需要显式放行
+    - `rm/chmod` 等高风险命令默认拒绝(需显式确认后放行)
+  - `NoteTool`:在 `<repo>/.helloagents/notes/` 下写入结构化笔记(用于记录决策/阻塞/行动)。
+- `MemoryTool`:仅启用 `episodic`(SQLite 持久化),默认存储在 `<repo>/.helloagents/memory/`。
+- 补丁落盘(B 路线):
+  - 模型可在回复中输出 `*** Begin Patch ... *** End Patch` 格式补丁。
+  - CLI 检测到补丁后会执行应用流程;对于高风险补丁(如 `*** Delete File:` 或大规模变更)会二次询问 `y/n`。
+  - 落盘通过 `code_agent/executors/apply_patch_executor.py`:原子写、备份、冲突检测、规模限制。
+
+## 约束与已知限制
+
+- 目前“敏感操作确认”优先覆盖:`Delete File`、`git reset --hard`、`rm/chmod`、大规模变更;更细粒度策略后续可扩展。
+- `TerminalTool` 仍是“字符串命令”入口,但执行为 argv-only,并在工具内拦截 shell 语义;更严格的参数级白名单可继续收紧。
+- 存储布局默认使用 `<repo>/.helloagents/`(notes/memory/sessions/logs),可用环境变量覆盖:
+  - `HELLOAGENTS_DIR=.helloagents`
+  - `CODE_AGENT_MAX_STEPS=8`
+
+## 相关文件
+
+- CLI 入口:`code_agent/hello_code_cli.py`
+- 主逻辑/上下文工程:`code_agent/agentic/code_agent.py`
+- 补丁执行器:`code_agent/executors/apply_patch_executor.py`
+- 上下文工程文档:`docs/第九章 上下文工程.md`

+ 6 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/__init__.py

@@ -0,0 +1,6 @@
+"""Code Agent implementations and CLI entrypoints.
+
+Note: `code_agent/langchain_agent.py` is kept as an optional demo/comparison implementation.
+The MVP CLI lives under `code_agent/cli/` and uses a HelloAgents-style hand-rolled executor + state machine.
+"""
+

+ 5 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/agentic/__init__.py

@@ -0,0 +1,5 @@
+"""Agentic Code Agent (ReAct + tools)"""
+
+from .code_agent import CodeAgent
+
+__all__ = ["CodeAgent"]

+ 346 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/agentic/code_agent.py

@@ -0,0 +1,346 @@
+from __future__ import annotations
+
+import json
+from dataclasses import dataclass
+from datetime import datetime
+from pathlib import Path
+from typing import List, Optional
+
+from agents.react_agent import ReActAgent
+from core.config import Config
+from core.llm import HelloAgentsLLM
+from core.message import Message
+from context.builder import ContextBuilder, ContextConfig, ContextPacket
+from tools.registry import ToolRegistry
+from tools.builtin.note_tool import NoteTool
+from tools.builtin.terminal_tool import TerminalTool
+from tools.builtin.plan_tool import PlanTool
+from tools.builtin.todo_tool import TodoTool
+from tools.builtin.context_fetch_tool import ContextFetchTool
+
+
+
+@dataclass
+class CodeAgentPaths:
+    """CodeAgent 路径配置类,集中管理所有相关目录路径"""
+    repo_root: Path
+    notes_dir: Path
+    memory_dir: Path
+    sessions_dir: Path
+    logs_dir: Path
+
+    @property
+    def helloagents_dir(self) -> Path:
+        """返回 .helloagents 目录路径"""
+        return self.repo_root / ".helloagents"
+
+    @property
+    def prompts_dir(self) -> Path:
+        """返回 prompts 目录路径"""
+        return self.repo_root / "code_agent" / "prompts"
+
+
+class CodeAgent:
+    """
+    类似 Claude Code/Codex 的 CLI 智能体:
+    - 核心循环使用 ReActAgent。
+    - ContextBuilder 负责拼接:系统提示词 + 最近对话 + 相关笔记 + 情景记忆。
+    - 规划能力作为可选工具 (`plan`) 暴露给模型,模型可按需调用。
+    """
+
+    def __init__(self, repo_root: Path, llm: Optional[HelloAgentsLLM] = None, config: Optional[Config] = None):
+        """
+        初始化 CodeAgent
+
+        Args:
+            repo_root: 代码仓库根目录
+            llm: LLM 实例
+            config: 配置对象
+        """
+        repo_root = repo_root.resolve()
+        self.config = config or Config.from_env()
+
+        # 初始化目录结构
+        helloagents_dir = Path(self.config.helloagents_dir)
+        state_root = helloagents_dir if helloagents_dir.is_absolute() else (repo_root / helloagents_dir)
+        self.paths = CodeAgentPaths(
+            repo_root=repo_root,
+            notes_dir=state_root / "notes",
+            memory_dir=state_root / "memory",
+            sessions_dir=state_root / "sessions",
+            logs_dir=state_root / "logs",
+        )
+        # 确保所有必要目录存在
+        self.paths.helloagents_dir.mkdir(parents=True, exist_ok=True)
+        self.paths.notes_dir.mkdir(parents=True, exist_ok=True)
+        self.paths.sessions_dir.mkdir(parents=True, exist_ok=True)
+        # memory / logs 仅在需要时创建,这里不再预建
+
+        self.session_id = f"session_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
+        self.llm = llm or HelloAgentsLLM()
+
+        # 初始化工具 (真实实现)
+        self.note_tool = NoteTool(workspace=str(self.paths.notes_dir))
+        # 类似 Claude Code:默认允许 Shell 语法 (管道等),但危险操作需确认
+        self.terminal_tool = TerminalTool(
+            workspace=str(self.paths.repo_root),
+            timeout=60,
+            confirm_dangerous=True,
+            default_shell_mode=True,
+        )
+        self.todo_tool = TodoTool(workspace=str(self.paths.helloagents_dir / "todos"))
+
+        # ReActAgent 的工具注册表
+        # 核心工具:terminal, note, memory, plan
+        # 扩展上下文工具:context_fetch(让模型按需获取更多证据)
+        self.registry = ToolRegistry()
+        self.registry.register_tool(self.terminal_tool)
+        self.registry.register_tool(self.note_tool)
+        self.registry.register_tool(PlanTool(self.llm, prompt_path=str(self.paths.prompts_dir / "plan.md")))
+        self.registry.register_tool(self.todo_tool)
+        
+        # 注册上下文获取工具(让模型按需探索)
+        self.context_fetch_tool = ContextFetchTool(
+            workspace=str(self.paths.repo_root),
+            note_tool=self.note_tool,
+            memory_tool=None,
+            max_tokens_per_source=800,
+            context_lines=5,
+        )
+        self.registry.register_tool(self.context_fetch_tool)
+
+        # 初始化上下文构建器(lazy_fetch=True:只构建保底上下文)
+        self.context_builder = ContextBuilder(
+            memory_tool=None,
+            rag_tool=None,
+            config=ContextConfig(
+                max_tokens=8000,
+                reserve_ratio=0.15,
+                max_history_turns=10,
+                enable_compression=True,
+                include_output_format=False,
+                lazy_fetch=True,  # 按需探索模式
+            ),
+            llm=self.llm,
+        )
+
+        # 加载自定义 Prompt 并初始化 ReActAgent
+        react_prompt = (self.paths.prompts_dir / "react.md").read_text(encoding="utf-8")
+        summarize_prompt = (self.paths.prompts_dir / "summarize_observation.md").read_text(encoding="utf-8")
+
+        def _summarize_observation(tool_name: str, tool_input: str, observation: str) -> str:
+            """
+            使用 LLM 压缩工具输出 (避免将巨大的原始输出放入 Prompt)
+            """
+            truncated = observation
+            if len(truncated) > 8000:
+                truncated = truncated[:8000] + "\n...truncated...\n"
+            user_msg = (
+                f"Tool: {tool_name}\n"
+                f"Input: {tool_input}\n\n"
+                f"Output:\n{truncated}"
+            )
+            return self.llm.invoke(
+                [
+                    {"role": "system", "content": summarize_prompt},
+                    {"role": "user", "content": user_msg},
+                ],
+                max_tokens=400,
+            ) or ""
+
+        self.react = ReActAgent(
+            name="code_agent",
+            llm=self.llm,
+            tool_registry=self.registry,
+            max_steps=20,
+            custom_prompt=react_prompt,
+            observation_summarizer=_summarize_observation,
+            summarize_threshold_chars=1800,
+        )
+
+        base_system = (self.paths.prompts_dir / "system.md").read_text(encoding="utf-8")
+        self.tools_reference_path = self.paths.prompts_dir / "tools.md"
+        self.system_prompt = base_system
+        self.history: List[Message] = []
+        self.recent_tool_packets: List[ContextPacket] = []
+        self.last_direct_reply: bool = False
+
+    def _is_chitchat(self, text: str) -> bool:
+        """判断是否为闲聊,避免不必要的工具调用"""
+        t = (text or "").strip().lower()
+        return t in {"hi", "hello", "hey", "yo", "你好", "您好", "在吗", "嗨", "哈喽"}
+
+    def _is_history_query(self, text: str) -> bool:
+        """判断是否为'回顾刚才说了什么'的元请求"""
+        t = (text or "").strip().lower()
+        patterns = [
+            "说了什么",
+            "刚才说了什么",
+            "之前说了什么",
+            "what did i say",
+            "what did we say",
+            "recap",
+            "summary of conversation",
+        ]
+        return any(p in t for p in patterns)
+
+    def _reply_with_recent_history(self, limit: int = 6) -> str:
+        """生成最近对话的简要回顾"""
+        # 只取用户/助手消息(跳过系统等)
+        items = [m for m in self.history if m.role in {"user", "assistant"}][-limit * 2 :]
+        if not items:
+            return "目前还没有可回顾的对话历史。"
+        lines = []
+        for m in items:
+            role = "你" if m.role == "user" else "助手"
+            lines.append(f"- {role}: {m.content}")
+        return "下面是最近的对话回顾:\n" + "\n".join(lines)
+
+    # 以下两个方法在 lazy_fetch 模式下不再主动调用,
+    # 扩展上下文改由模型通过 context_fetch 工具按需获取。
+    # 保留这些方法以支持 lazy_fetch=False 的传统模式。
+    
+    def _note_packets(self, query: str) -> List[ContextPacket]:
+        """检索相关笔记并封装为 ContextPacket"""
+        packets: List[ContextPacket] = []
+        if self._is_chitchat(query):
+            return packets
+        try:
+            # 获取最近的阻碍 (Blocker)
+            blockers = self.note_tool.run({"action": "list", "note_type": "blocker", "limit": 2})
+            if blockers and isinstance(blockers, str) and "暂无" not in blockers:
+                packets.append(ContextPacket(content=f"[Notes:blocker]\n{blockers}", metadata={"source": "note"}))
+            # 搜索相关笔记
+            hits = self.note_tool.run({"action": "search", "query": query, "limit": 3})
+            if hits and isinstance(hits, str) and "未找到" not in hits:
+                packets.append(ContextPacket(content=f"[Notes:search]\n{hits}", metadata={"source": "note"}))
+        except Exception:
+            pass
+        return packets
+
+    def _memory_packets(self, query: str) -> List[ContextPacket]:
+        """检索相关记忆并封装为 ContextPacket"""
+        packets: List[ContextPacket] = []
+        if self._is_chitchat(query):
+            return packets
+        try:
+            hits = self.memory_tool.run(
+                {"action": "search", "query": query, "memory_types": self.memory_tool.memory_types, "limit": 5, "min_importance": 0.0}
+            )
+            if hits and isinstance(hits, str) and "未找到" not in hits:
+                packets.append(ContextPacket(content=f"[Memory]\n{hits}", metadata={"source": "memory"}))
+        except Exception:
+            pass
+        return packets
+
+    def _persist_session(self) -> None:
+        """持久化当前会话到 JSON 文件"""
+        p = self.paths.sessions_dir / f"{self.session_id}.json"
+        data = {
+            "session_id": self.session_id,
+            "updated_at": datetime.now().isoformat(),
+            "history": [
+                {"role": m.role, "content": m.content, "timestamp": m.timestamp.isoformat()} for m in self.history[-50:]
+            ],
+        }
+        p.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
+
+    def run_turn(self, user_input: str) -> str:
+        """
+        执行一轮对话:
+        1. 收集上下文 (笔记、记忆、最近工具输出)
+        2. 构建完整 Prompt
+        3. 运行 ReAct 循环
+        4. 更新历史并持久化
+        """
+        # 空输入:提示而不进入 ReAct
+        if not user_input.strip():
+            return "请提供具体指令或问题。"
+
+        # 闲聊/问候:直接回复,避免 ReAct 的严格格式解析失败,也避免无谓的工具调用。
+        if self._is_chitchat(user_input):
+            self.last_direct_reply = True
+            reply = "你好!我是 Code Agent,可以帮你按需探索代码仓库、生成补丁并在确认后落盘。你想做什么?(例如:分析项目结构 / 搜索某个类 / 修复一个报错)"
+            self.history.append(Message(content=user_input, role="user", timestamp=datetime.now()))
+            self.history.append(Message(content=reply, role="assistant", timestamp=datetime.now()))
+            if len(self.history) > 50:
+                self.history = self.history[-50:]
+            self._persist_session()
+            return reply
+        self.last_direct_reply = False
+
+        # 元请求:回顾最近对话
+        if self._is_history_query(user_input):
+            self.last_direct_reply = True
+            reply = self._reply_with_recent_history(limit=6)
+            self.history.append(Message(content=user_input, role="user", timestamp=datetime.now()))
+            self.history.append(Message(content=reply, role="assistant", timestamp=datetime.now()))
+            if len(self.history) > 50:
+                self.history = self.history[-50:]
+            self._persist_session()
+            return reply
+
+        # 若检测到明显多步骤词汇,向模型追加轻量提示(不强制,只提高倾向)
+        multistep_hint = ""
+        multi_patterns = ["分步", "步骤", "三步", "计划", "改造", "完成后", "多步", "多步骤"]
+        if any(p in user_input for p in multi_patterns):
+            multistep_hint = "提示:本任务包含多个步骤,先用 todo 记录/更新,再执行;收尾用 todo list 汇总。"
+
+        # 构建保底上下文(系统提示 + 对话历史 + 上次工具摘要 + 可选 hint)
+        # 扩展上下文由模型通过 context_fetch 工具按需获取
+        tool_summaries = []
+        for packet in self.recent_tool_packets[-3:]:
+            tool_summaries.append(packet.content)
+        
+        context_text = self.context_builder.build_base(
+            user_query=user_input,
+            conversation_history=self.history,
+            system_instructions=self.system_prompt + ("\n" + multistep_hint if multistep_hint else ""),
+            tool_summaries=tool_summaries if tool_summaries else None,
+        )
+        
+        # 将拼接好的上下文作为"问题"输入给 ReAct
+        response = self.react.run(context_text, max_tokens=8000)
+
+        # 收集本轮的工具执行证据 (已在 ReActAgent 内部摘要)
+        try:
+            tool_summaries: List[str] = []
+            todo_used = False
+            todo_listed = False
+            for item in getattr(self.react, "last_trace", [])[-6:]:
+                summary = item.get("observation_summary")
+                tname = item.get("tool_name")
+                if tname == "todo":
+                    todo_used = True
+                    if "list" in str(item.get("tool_input", "")):
+                        todo_listed = True
+                if summary:
+                    tool_summaries.append(
+                        f"[{item.get('tool_name')}] {item.get('tool_input')}\n{summary}"
+                    )
+            if tool_summaries:
+                self.recent_tool_packets.append(
+                    ContextPacket(
+                        content="[Tool Evidence]\n" + "\n\n".join(tool_summaries),
+                        metadata={"type": "tool_result", "source": "react"},
+                    )
+                )
+                # 保持缓冲区较小
+                if len(self.recent_tool_packets) > 8:
+                    self.recent_tool_packets = self.recent_tool_packets[-8:]
+        except Exception:
+            pass
+
+        # 更新历史记录 (保留最近 50 条)
+        self.history.append(Message(content=user_input, role="user", timestamp=datetime.now()))
+        self.history.append(Message(content=response, role="assistant", timestamp=datetime.now()))
+        if len(self.history) > 50:
+            self.history = self.history[-50:]
+        self._persist_session()
+        try:
+            if todo_used and not todo_listed:
+                todo_snapshot = self.registry.execute_tool("todo", {"action": "list"})
+                response = f"{response}\n\nTodo board:\n{todo_snapshot}"
+        except Exception:
+            pass
+        return response

+ 2 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/executors/__init__.py

@@ -0,0 +1,2 @@
+"""Executors are the only place with side effects (writes)."""
+

+ 512 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/executors/apply_patch_executor.py

@@ -0,0 +1,512 @@
+from __future__ import annotations
+
+import os
+import tempfile
+from dataclasses import dataclass
+from datetime import datetime
+from pathlib import Path
+from typing import List, Optional, Tuple
+
+
+class PatchApplyError(RuntimeError):
+    """
+    补丁应用过程中发生的异常类。
+    用于封装补丁应用失败的原因,并提供额外的检查目标信息。
+    
+    参数:
+        message: 错误消息,描述补丁应用失败的原因
+        recheck_targets: 可选的重新检查目标列表,用于辅助调试和修复补丁问题
+    """
+    def __init__(self, message: str, recheck_targets: Optional[List[str]] = None):
+        super().__init__(message)
+        self.recheck_targets = recheck_targets or []
+
+
+@dataclass
+class ApplyResult:
+    """
+    补丁应用结果的数据类。
+    用于返回补丁应用过程中产生的变更信息和备份信息。
+    
+    字段:
+        files_changed: 被修改的文件路径列表(相对路径)
+        backups: 创建的备份文件路径列表(绝对路径)
+    """
+    files_changed: List[str]
+    backups: List[str]
+
+
+class ApplyPatchExecutor:
+    """
+    应用 Codex 风格的 *** Begin Patch 格式补丁。
+
+    安全特性 (MVP):
+    - repo_root 路径限制 (防止路径逃逸)
+    - 通过临时文件 + os.replace 实现原子写入
+    - 备份到 <repo_root>/.helloagents/backups/<timestamp>/
+    - 大小限制 (最大文件数, 最大总变更行数)
+    - Update File 块的冲突检测 (精确匹配)
+    """
+
+    def __init__(
+        self,
+        repo_root: Path,
+        max_files: int = 10,
+        max_total_changed_lines: int = 800,
+        allowed_write_suffixes: Optional[List[str]] = None,
+    ):
+        """
+        初始化补丁应用执行器。
+        
+        参数:
+            repo_root: 代码仓库根目录路径,所有补丁操作都限制在此目录内
+            max_files: 单个补丁允许修改的最大文件数量,默认10个
+            max_total_changed_lines: 单个补丁允许修改的最大总行数,默认800行
+            allowed_write_suffixes: 允许修改的文件后缀列表,默认只允许常见文本文件
+        """
+        self.repo_root = repo_root
+        self.max_files = max_files
+        self.max_total_changed_lines = max_total_changed_lines
+        
+        # 默认允许写入的文件后缀,防止意外修改二进制文件或敏感文件
+        self.allowed_write_suffixes = allowed_write_suffixes or [
+            ".py",
+            ".md",
+            ".toml",
+            ".json",
+            ".yml",
+            ".yaml",
+            ".txt",
+            ".html",
+            ".htm",
+            ".css",
+            ".js",
+        ]
+
+        # 初始化工作目录和备份目录
+        self.root_dir = repo_root / ".helloagents"
+        self.backups_dir = self.root_dir / "backups"
+        self.backups_dir.mkdir(parents=True, exist_ok=True)
+
+    def apply(self, patch_text: str) -> ApplyResult:
+        """
+        解析并应用补丁文本。
+        
+        执行流程:
+        1. 解析补丁操作 (Add/Update/Delete)
+        2. 检查安全限制 (文件数量, 变更行数)
+        3. 创建备份目录
+        4. 逐个执行操作 (先备份再修改)
+        
+        参数:
+            patch_text: 符合 Codex 风格的补丁文本,以 *** Begin Patch 开始,*** End Patch 结束
+            
+        返回:
+            ApplyResult: 包含被修改文件和备份文件信息的结果对象
+            
+        异常:
+            PatchApplyError: 当补丁不符合格式、超出限制或应用失败时抛出
+        """
+        # 解析补丁文本,提取操作列表
+        ops = self._parse_patch(patch_text)
+        
+        # 统计受影响的文件数量,检查是否超过限制
+        touched_files = [op[1] for op in ops if op[0] in {"add", "update", "delete"}]
+        if len(set(touched_files)) > self.max_files:
+            raise PatchApplyError(f"Too many files in patch: {len(set(touched_files))} > {self.max_files}")
+
+        # 估算补丁修改的总行数,检查是否超过限制
+        total_changed = self._estimate_changed_lines(ops)
+        if total_changed > self.max_total_changed_lines:
+            raise PatchApplyError(f"Patch too large: {total_changed} changed lines > {self.max_total_changed_lines}")
+
+        # 创建本次补丁应用的专属备份目录(时间戳命名)
+        backup_run_dir = self.backups_dir / datetime.now().strftime("%Y%m%d_%H%M%S")
+        backup_run_dir.mkdir(parents=True, exist_ok=True)
+
+        # 初始化结果收集变量
+        files_changed: List[str] = []  # 记录被修改的文件路径
+        backups: List[str] = []  # 记录创建的备份文件路径
+
+        # 遍历所有解析出的操作,逐个执行
+        for kind, rel_path, payload in ops:
+            # 安全检查:确保路径在仓库内,防止路径遍历攻击
+            target = self._safe_path(rel_path)
+            
+            # 安全检查:确保文件后缀在允许的列表中
+            self._enforce_suffix(target)
+
+            if kind == "add":
+                # 添加新文件操作
+                if target.exists():
+                    raise PatchApplyError(f"Add File target already exists: {rel_path}")
+                # 创建父目录(如果不存在)
+                target.parent.mkdir(parents=True, exist_ok=True)
+                # 原子写入新文件内容
+                self._atomic_write(target, payload)
+                # 记录变更
+                files_changed.append(rel_path)
+                
+            elif kind == "delete":
+                # 删除文件操作
+                if not target.exists():
+                    raise PatchApplyError(f"Delete File target missing: {rel_path}")
+                # 删除前先备份文件
+                b = self._backup_file(target, backup_run_dir)
+                backups.append(str(b))
+                # 删除文件
+                target.unlink()
+                # 记录变更
+                files_changed.append(rel_path)
+                
+            elif kind == "update":
+                # 更新文件操作
+                if not target.exists():
+                    raise PatchApplyError(f"Update File target missing: {rel_path}")
+                # 读取原始文件内容(保留换行符)
+                original = target.read_text(encoding="utf-8").splitlines(keepends=True)
+                # 修改前先备份文件
+                b = self._backup_file(target, backup_run_dir)
+                backups.append(str(b))
+                # 应用更新补丁内容
+                updated = self._apply_update_payload(original, payload, rel_path)
+                # 原子写入更新后的内容
+                self._atomic_write(target, "".join(updated))
+                # 记录变更
+                files_changed.append(rel_path)
+                
+            else:
+                # 未知操作类型
+                raise PatchApplyError(f"Unknown op kind: {kind}")
+
+        # 返回最终的应用结果
+        return ApplyResult(files_changed=files_changed, backups=backups)
+
+    def _safe_path(self, rel_path: str) -> Path:
+        """
+        验证路径安全性,防止路径遍历攻击 (Path Traversal)。
+        确保目标路径在 repo_root 目录下,防止访问仓库外的文件。
+        
+        参数:
+            rel_path: 相对路径字符串
+            
+        返回:
+            Path: 安全的绝对路径对象
+            
+        异常:
+            PatchApplyError: 当路径是绝对路径、包含特殊字符或试图访问仓库外时抛出
+        """
+        if rel_path.startswith("/") or rel_path.startswith("~"):
+            raise PatchApplyError(f"Absolute paths are not allowed: {rel_path}")
+        target = (self.repo_root / rel_path).resolve()
+        # 检查解析后的路径是否以 repo_root 开头
+        if not str(target).startswith(str(self.repo_root.resolve()) + os.sep) and target != self.repo_root.resolve():
+            raise PatchApplyError(f"Path escapes repo_root: {rel_path}")
+        if target.exists() and target.is_symlink():
+            raise PatchApplyError(f"Refusing to modify symlink: {rel_path}")
+        return target
+
+    def _enforce_suffix(self, target: Path) -> None:
+        """
+        检查目标文件的后缀是否在允许的列表中。
+        防止意外修改二进制文件、配置文件或其他敏感文件。
+        
+        参数:
+            target: 目标文件路径对象
+            
+        异常:
+            PatchApplyError: 当文件后缀不在允许列表中时抛出
+        """
+        if target.suffix and target.suffix not in self.allowed_write_suffixes:
+            raise PatchApplyError(f"Disallowed file suffix for write: {target.suffix}")
+
+    def _backup_file(self, target: Path, backup_run_dir: Path) -> Path:
+        """
+        备份目标文件到指定的备份目录。
+        备份文件保持与原文件相同的相对路径结构,后缀添加 .bak。
+        
+        参数:
+            target: 要备份的目标文件路径
+            backup_run_dir: 本次运行的备份目录
+            
+        返回:
+            Path: 创建的备份文件路径
+        """
+        # 获取文件相对于仓库根目录的路径
+        rel = target.relative_to(self.repo_root)
+        # 构建备份文件路径
+        backup_path = backup_run_dir / (str(rel) + ".bak")
+        # 创建备份文件的父目录(如果不存在)
+        backup_path.parent.mkdir(parents=True, exist_ok=True)
+        # 复制文件内容到备份文件
+        backup_path.write_bytes(target.read_bytes())
+        return backup_path
+
+    def _atomic_write(self, target: Path, content: str) -> None:
+        """
+        原子写入文件内容。
+        先写入临时文件,然后使用 os.replace 原子性替换目标文件,确保写入过程不会因为中断而导致文件损坏。
+        
+        参数:
+            target: 目标文件路径
+            content: 要写入的文件内容
+        """
+        target.parent.mkdir(parents=True, exist_ok=True)
+        with tempfile.NamedTemporaryFile("w", delete=False, dir=str(target.parent), encoding="utf-8") as tf:
+            tf.write(content)
+            tf.flush()
+            os.fsync(tf.fileno())
+            tmp_name = tf.name
+        os.replace(tmp_name, target)
+
+    def _parse_patch(self, text: str) -> List[Tuple[str, str, str]]:
+        """
+        解析补丁文本,提取操作列表。
+        支持的操作:
+        - *** Add File: <path> - 添加新文件
+        - *** Delete File: <path> - 删除文件
+        - *** Update File: <path> - 更新文件内容
+        
+        参数:
+            text: 补丁文本字符串
+            
+        返回:
+            List[Tuple[str, str, str]]: 操作列表,每个操作包含(操作类型, 路径, 内容)
+            
+        异常:
+            PatchApplyError: 当补丁格式不符合要求时抛出
+        """
+        lines = text.splitlines()
+        # 宽容处理:跳过前置空行/代码块围栏,找到真正的开头
+        while lines and lines[0].strip() in {"", "```", "```patch", "```diff", "```text"}:
+            lines = lines[1:]
+        # 如果仍未以标头开头,尝试向下寻找标头并截取
+        if lines and lines[0].strip() != "*** Begin Patch":
+            for idx, l in enumerate(lines):
+                if l.strip() == "*** Begin Patch":
+                    lines = lines[idx:]
+                    break
+        if not lines or lines[0].strip() != "*** Begin Patch":
+            raise PatchApplyError("Patch must start with '*** Begin Patch'")
+        # 同样跳过结尾的围栏/空行
+        while lines and lines[-1].strip() in {"", "```"}:
+            lines = lines[:-1]
+        if not lines or lines[-1].strip() != "*** End Patch":
+            # 如果末尾未对齐,尝试在中间找到最后一个 End 标记
+            for idx in range(len(lines) - 1, -1, -1):
+                if lines[idx].strip() == "*** End Patch":
+                    lines = lines[: idx + 1]
+                    break
+        if not lines or lines[-1].strip() != "*** End Patch":
+            raise PatchApplyError("Patch must end with '*** End Patch'")
+
+        ops: List[Tuple[str, str, str]] = []
+        i = 1
+        while i < len(lines) - 1:
+            line = lines[i]
+            if line.startswith("*** Add File: "):
+                path = line[len("*** Add File: ") :].strip()
+                i += 1
+                buf: List[str] = []
+                while i < len(lines) - 1 and not lines[i].startswith("*** "):
+                    # 兼容两种格式:
+                    # 1) 规范形式:以 '+' 开头
+                    # 2) 宽松形式:直接给出正文(模型有时会省略 '+')
+                    if lines[i].startswith("+"):
+                        buf.append(lines[i][1:] + "\n")
+                    else:
+                        buf.append(lines[i] + "\n")
+                    i += 1
+                ops.append(("add", path, "".join(buf)))
+                continue
+            if line.startswith("*** Delete File: "):
+                path = line[len("*** Delete File: ") :].strip()
+                ops.append(("delete", path, ""))
+                i += 1
+                continue
+            if line.startswith("*** Update File: "):
+                path = line[len("*** Update File: ") :].strip()
+                i += 1
+                buf: List[str] = []
+                while i < len(lines) - 1 and not lines[i].startswith("*** "):
+                    buf.append(lines[i])
+                    i += 1
+                ops.append(("update", path, "\n".join(buf)))
+                continue
+            if line.strip() == "":
+                i += 1
+                continue
+            raise PatchApplyError(f"Unexpected patch line: {line}")
+
+        return ops
+
+    def _estimate_changed_lines(self, ops: List[Tuple[str, str, str]]) -> int:
+        """
+        估算补丁操作的总变更行数。
+        用于检查补丁大小是否超过限制。
+        
+        参数:
+            ops: 补丁操作列表
+            
+        返回:
+            int: 估算的总变更行数
+        """
+        changed = 0
+        for kind, _, payload in ops:
+            if kind == "add":
+                # 添加文件:按行数计算
+                changed += payload.count("\n")
+            elif kind == "delete":
+                # 删除文件:按1行计算
+                changed += 1
+            elif kind == "update":
+                # 更新文件:只计算+/-开头的变更行
+                for l in payload.splitlines():
+                    if l.startswith("+") or l.startswith("-"):
+                        changed += 1
+        return changed
+
+    def _apply_update_payload(self, original: List[str], payload: str, rel_path: str) -> List[str]:
+        """
+        应用 Update File 的内容。
+        将 payload 分割成多个 hunk (代码块),然后逐个应用。
+        """
+        # 兼容宽松格式:如果 payload 没有任何 + / - / 前导空格行,视为“整文件替换”
+        raw_lines = payload.splitlines(keepends=True)
+        if raw_lines and all(not l.startswith(("+", "-", " ")) for l in raw_lines):
+            return raw_lines
+
+        hunks = self._split_hunks(payload)
+        current = original
+        try:
+            for hunk in hunks:
+                current = self._apply_hunk(current, hunk, rel_path)
+            return current
+        except PatchApplyError as e:
+            # 宽松兜底:当上下文匹配失败时,尝试将 payload 视作“新的完整文件”生成 after 版本
+            if "context not found" not in str(e).lower():
+                raise
+            fallback = self._hunks_to_after(hunks)
+            if fallback:
+                return fallback
+            raise
+
+    def _split_hunks(self, payload: str) -> List[List[str]]:
+        """
+        将 Update File 的 payload 分割成多个 hunk(代码块)。
+        Hunk 通常由 @@ ... @@ 分隔符分隔,或者由空行分隔。
+        每个 hunk 代表文件的一个修改区域。
+        
+        参数:
+            payload: Update File 操作的内容
+            
+        返回:
+            List[List[str]]: hunk 列表,每个 hunk 是多行字符串的列表
+        """
+        lines = payload.splitlines()
+        hunks: List[List[str]] = []
+        buf: List[str] = []
+        for l in lines:
+            if l.startswith("@@"):
+                if buf:
+                    hunks.append(buf)
+                    buf = []
+                continue
+            if l.strip() == "" and buf:
+                hunks.append(buf)
+                buf = []
+                continue
+            buf.append(l)
+        if buf:
+            hunks.append(buf)
+        return [h for h in hunks if any(x.startswith((" ", "+", "-")) for x in h)]
+
+    def _apply_hunk(self, current: List[str], hunk_lines: List[str], rel_path: str) -> List[str]:
+        """
+        应用单个 hunk(代码块)到当前文件内容。
+        
+        原理:
+        1. 解析 hunk,分离出 'before' (上下文 + 删除行) 和 'after' (上下文 + 新增行)
+        2. 在当前文件中查找 'before' 块的精确位置
+        3. 如果找到匹配的上下文,用 'after' 块替换 'before' 块
+        4. 如果找不到匹配的上下文,抛出异常
+        
+        参数:
+            current: 当前文件的内容行列表
+            hunk_lines: hunk 的内容行列表
+            rel_path: 文件的相对路径(用于错误提示)
+            
+        返回:
+            List[str]: 应用 hunk 后的文件内容行列表
+            
+        异常:
+            PatchApplyError: 当 hunk 格式错误或找不到匹配的上下文时抛出
+        """
+        before: List[str] = []
+        after: List[str] = []
+        for l in hunk_lines:
+            if not l:
+                continue
+            tag = l[0]
+            text = l[1:] + "\n"
+            if tag == " ":
+                before.append(text)
+                after.append(text)
+            elif tag == "-":
+                before.append(text)
+            elif tag == "+":
+                after.append(text)
+
+        if not before:
+            raise PatchApplyError("Update hunk has no context/removals; refusing to apply")
+
+        idx = self._find_subsequence(current, before)
+        if idx is None:
+            context_line = next((b.strip() for b in before if b.strip()), "")
+            hint = f"{rel_path}:search:'{context_line[:80]}'"
+            raise PatchApplyError("Patch hunk context not found; file changed?", recheck_targets=[hint])
+
+        return current[:idx] + after + current[idx + len(before) :]
+
+    def _find_subsequence(self, haystack: List[str], needle: List[str]) -> Optional[int]:
+        """
+        在文件内容中查找代码块的起始位置。
+        使用简单的 O(N*M) 字符串匹配算法,在 haystack 中查找 needle 的精确匹配。
+        
+        参数:
+            haystack: 文件内容行列表
+            needle: 要查找的代码块行列表
+            
+        返回:
+            Optional[int]: 匹配的起始行索引,如果未找到则返回 None
+        """
+        if len(needle) > len(haystack):
+            return None
+        for i in range(0, len(haystack) - len(needle) + 1):
+            if haystack[i : i + len(needle)] == needle:
+                return i
+        # 宽松匹配:忽略行尾空白再尝试一次,缓解缩进/换行轻微偏差
+        norm_hay = [h.rstrip() + "\n" for h in haystack]
+        norm_need = [n.rstrip() + "\n" for n in needle]
+        for i in range(0, len(norm_hay) - len(norm_need) + 1):
+            if norm_hay[i : i + len(norm_need)] == norm_need:
+                return i
+        return None
+
+    def _hunks_to_after(self, hunks: List[List[str]]) -> List[str]:
+        """
+        将多个 hunk 的“after”部分合成为一份完整文件内容。
+        用于上下文匹配失败时的宽松回退:保留 + 和空格行,忽略 - 行。
+        """
+        out: List[str] = []
+        for hunk in hunks:
+            for l in hunk:
+                if not l:
+                    continue
+                tag = l[0]
+                text = l[1:] + "\n" if len(l) > 1 else "\n"
+                if tag == "-" or tag == "@":
+                    continue
+                if tag in (" ", "+"):
+                    out.append(text)
+        return out

+ 220 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/hello_code_cli.py

@@ -0,0 +1,220 @@
+from __future__ import annotations
+
+import argparse
+import os
+import re
+import logging
+from pathlib import Path
+
+try:
+    from dotenv import load_dotenv  # type: ignore
+except Exception:  # pragma: no cover
+    def load_dotenv(*args, **kwargs):  # type: ignore
+        return False
+
+from core.llm import HelloAgentsLLM
+from core.exceptions import HelloAgentsException
+from core.config import Config
+from code_agent.agentic import CodeAgent
+from code_agent.executors.apply_patch_executor import ApplyPatchExecutor, PatchApplyError
+from utils.cli_ui import c, hr, PRIMARY, ACCENT, INFO, WARN, ERROR
+
+
+# 匹配 Codex 风格补丁块(宽松,跨行,允许前导空白或代码围栏)
+PATCH_RE = re.compile(r"\s*\*\*\* Begin Patch[\s\S]*?\*\*\* End Patch", re.MULTILINE)
+# 备用:从 ```patch/```diff 围栏中提取补丁主体
+PATCH_FENCE_RE = re.compile(
+    r"```(?:patch|diff|text)?\s*(\*\*\* Begin Patch[\s\S]*?\*\*\* End Patch)\s*```",
+    re.MULTILINE,
+)
+
+
+def _extract_patch(text: str) -> str | None:
+    """
+    从 LLM 响应文本中提取补丁块。
+    补丁块通常由 *** Begin Patch 和 *** End Patch 包围。
+    """
+    # 优先匹配代码围栏内的补丁
+    m = PATCH_FENCE_RE.search(text)
+    if m:
+        return m.group(1)
+    # 退回普通匹配(允许前导空白)
+    m = PATCH_RE.search(text)
+    return m.group(0).strip() if m else None
+
+
+def _normalize_patch(patch_text: str) -> str:
+    """
+    规范化补丁文本,以宽容处理模型的一些格式错误。
+    - 接受 'Delete File:' / 'Update File:' / 'Add File:' (即使缺少前导 '*** ')
+    - 保持执行器所需的标准 Codex 风格格式。
+    """
+    lines = patch_text.splitlines()
+    out: list[str] = []
+    for line in lines:
+        stripped = line.strip()
+        if stripped.startswith(("Add File:", "Update File:", "Delete File:")) and not stripped.startswith("*** "):
+            out.append("*** " + stripped)
+            continue
+        out.append(line)
+    return "\n".join(out)
+
+
+def _patch_requires_confirmation(patch_text: str) -> bool:
+    """
+    判断补丁是否需要用户确认。
+    策略:
+    - 包含文件删除操作
+    - 涉及文件数量过多 (>= 6)
+    - 变更行数过多 (>= 400)
+    """
+    # MVP: Delete File / too many files / too big => confirm
+    if "*** Delete File:" in patch_text:
+        return True
+    file_ops = patch_text.count("*** Add File:") + patch_text.count("*** Update File:") + patch_text.count("*** Delete File:")
+    if file_ops >= 6:
+        return True
+    changed_lines = 0
+    for line in patch_text.splitlines():
+        if line.startswith("+") or line.startswith("-"):
+            changed_lines += 1
+    return changed_lines >= 400
+
+
+def main(argv: list[str] | None = None) -> int:
+    """
+    CLI 入口点。
+    初始化 LLM、CodebaseMaintainer 和 PatchExecutor,并进入交互式循环。
+    """
+    # 1. 解析命令行参数
+    parser = argparse.ArgumentParser(description="HelloAgents-style Code Agent CLI (Codex/Claude-like)")
+    parser.add_argument("--repo", type=str, default=".", help="Repository root (workspace). Default: .")
+    parser.add_argument("--project", type=str, default=None, help="Project name (default: repo folder name)")
+    args = parser.parse_args(argv)
+
+    # 2. 初始化环境和 LLM
+    repo_root = Path(args.repo).resolve()
+    load_dotenv(dotenv_path=repo_root / ".env", override=False)
+
+    project = args.project or repo_root.name
+    config = Config.from_env()
+    llm = HelloAgentsLLM()  # auto-detect provider from env
+    # reduce noisy HTTP client logs in the CLI
+    logging.getLogger("httpx").setLevel(logging.WARNING)
+    logging.getLogger("openai").setLevel(logging.WARNING)
+    logging.getLogger("openai._base_client").setLevel(logging.WARNING)
+    logging.getLogger("memory").setLevel(logging.WARNING)
+
+    print(c(hr("=", 80), INFO))
+    print(c("HelloAgents Code Agent CLI", PRIMARY))
+    print(c(f"workspace: {repo_root}", INFO))
+    print(c(f"LLM: provider={llm.provider} model={llm.model} base_url={llm.base_url}", INFO))
+    print(c(f"state: {Path(config.helloagents_dir).as_posix()}", INFO))
+    print(c(hr("=", 80), INFO))
+
+    # Optional preflight to surface auth issues early.
+    try:
+        _ = llm.invoke([{"role": "user", "content": "ping"}], max_tokens=1)
+    except HelloAgentsException as e:
+        print(c("LLM 预检失败(通常是 API key/base_url/model 配置问题)。", ERROR))
+        print(c(f"error: {e}", ERROR))
+        print(c("请检查 .env 中的 DEEPSEEK_API_KEY / LLM_* 配置是否正确。", WARN))
+        return 2
+
+    # 3. 初始化核心组件(ReAct + tools)
+    agent = CodeAgent(repo_root=repo_root, llm=llm, config=config)
+    patch_executor = ApplyPatchExecutor(repo_root=repo_root)
+
+    # 4. 进入交互循环
+    print(c("输入自然语言需求开始;命令:", INFO))
+    print(c("  :quit", ACCENT) + c(" 退出", INFO))
+    print(c("  :plan <目标>", ACCENT) + c(" 强制生成计划", INFO))
+    while True:
+        try:
+            user_in = input(c("👤 > ", PRIMARY))
+        except (EOFError, KeyboardInterrupt):
+            print("\n" + c("bye", INFO))
+            return 0
+
+        if user_in is None:
+            continue
+        user_in = user_in.strip()
+        if not user_in:
+            print(c("请提供具体指令或问题。", WARN))
+            continue
+        if user_in in {":q", ":quit", "quit", "exit"}:
+            print(c("bye", INFO))
+            return 0
+        if user_in.startswith(":plan"):
+            goal = user_in[len(":plan") :].strip() or "请为当前任务生成一个可执行计划"
+            response = agent.registry.execute_tool("plan", goal)
+            print("\n" + c("🤖 plan", PRIMARY))
+            print(response + "\n")
+            continue
+
+        # 5. 运行一轮对话(ReAct 可能按需调用终端/笔记/记忆)
+        try:
+            response = agent.run_turn(user_in)
+        except HelloAgentsException as e:
+            print(c(f"LLM 调用失败: {e}", ERROR))
+            continue
+
+        # 对于 direct reply(未经过 ReAct 的控制台打印),在 CLI 里补打一份输出
+        if getattr(agent, "last_direct_reply", False):
+            print(c("🤖 assistant", PRIMARY))
+            print(response)
+        
+        # 7. 提取并应用补丁
+        patch_text = _extract_patch(response)
+        if not patch_text:
+            continue
+        patch_text = _normalize_patch(patch_text)
+        # Ignore empty patch blocks
+        if patch_text.strip() == "*** Begin Patch\n*** End Patch":
+            continue
+
+        needs_confirm = _patch_requires_confirmation(patch_text)
+        if needs_confirm:
+            # If user just answered y/n as the *current* input, treat it as confirmation for this patch.
+            if user_in.strip().lower() in {"n", "no"}:
+                print("已取消补丁应用。")
+                continue
+            if user_in.strip().lower() not in {"y", "yes"}:
+                print("\n⚠️ 检测到高风险补丁(删除/大规模变更)。是否应用?(y/n)")
+                ans = input("confirm> ").strip().lower()
+                if ans not in {"y", "yes"}:
+                    print("已取消补丁应用。")
+                    continue
+
+        try:
+            res = patch_executor.apply(patch_text)
+            print("\n" + c("✅ Patch applied", PRIMARY))
+            print(c(f"files: {', '.join(res.files_changed) if res.files_changed else '(none)'}", INFO))
+            if res.backups:
+                print(c(f"backups: {len(res.backups)} (in .helloagents/backups/...)", INFO))
+
+            # 记录到 NoteTool(action)
+            agent.note_tool.run({
+                "action": "create",
+                "title": "Patch applied",
+                "content": f"User input:\n{user_in}\n\nPatch:\n\n```text\n{patch_text}\n```\n\nFiles:\n"
+                + "\n".join([f"- {p}" for p in res.files_changed]),
+                "note_type": "action",
+                "tags": [project, "patch_applied"],
+            })
+        except PatchApplyError as e:
+            print("\n" + c(f"❌ Patch failed: {e}", ERROR))
+            agent.note_tool.run({
+                "action": "create",
+                "title": "Patch failed",
+                "content": f"Error: {e}\n\nUser input:\n{user_in}\n\nPatch:\n\n```text\n{patch_text}\n```\n",
+                "note_type": "blocker",
+                "tags": [project, "patch_failed"],
+            })
+            continue
+
+    return 0
+
+
+if __name__ == "__main__":
+    raise SystemExit(main())

+ 38 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/prompts/README.md

@@ -0,0 +1,38 @@
+# Prompts
+
+本目录用于集中管理 Code Agent 的提示词,方便独立迭代与对比。
+
+## 文件说明
+- `system.md`:全局行为与安全边界(按需探索/敏感操作确认/补丁格式)
+- `react.md`:ReAct 回合格式与工具输入约定
+- `plan.md`:规划工具(`plan[...]`)专用提示词
+- `summarize_observation.md`:工具输出摘要提示词
+
+## 核心设计理念(类似 Claude Code)
+
+### 按需探索
+- **保底上下文**(自动注入):系统提示 + 对话历史[-10:] + 上次工具摘要[-3:]
+- **扩展上下文**(按需获取):通过 `context_fetch` 工具由模型主动调用
+
+### context_fetch 工具使用指南
+
+**何时使用:**
+- ✅ 需要搜索代码中的类/函数定义
+- ✅ 用户问"有没有关于 X 的笔记/记忆"
+- ✅ 提到错误栈/报错信息,需要找相关代码
+- ❌ 用户问"我们刚才说了什么"(直接用对话历史)
+- ❌ 已经通过 terminal 拿到足够证据
+
+**参数说明:**
+```json
+{
+  "sources": ["files", "notes", "memory", "tests"],  // 可多选
+  "query": "ContextBuilder",                         // 关键词
+  "paths": "context/**/*.py"                         // 可选,限定范围
+}
+```
+
+**预算控制:** 每个数据源返回最多 ~800 tokens,自动截断,支持缓存
+
+**调用策略:** 先用保底上下文推理,证据不足再调用;避免盲目全局扫描
+

+ 18 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/prompts/plan.md

@@ -0,0 +1,18 @@
+你是一个“可选规划工具”。只有当需要多步执行、或用户强制要求计划时才会调用你。
+
+请输出一个可执行计划,要求:
+- 5~12 条步骤为宜,按可运行顺序排列
+- 每步尽量具体到“要看哪些文件/用什么命令/产出什么”
+- 若涉及修改代码:明确“先检索证据 -> 再给 patch -> 需要用户确认才落盘”
+
+输出格式(Markdown):
+## Plan
+1. ...
+2. ...
+
+## Risks
+- ...
+
+## Validation
+- ...
+

+ 122 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/prompts/react.md

@@ -0,0 +1,122 @@
+你是一个具备推理与行动能力的 Code Agent。你可以思考,然后调用工具获取证据,最终给出结论或补丁。
+
+## 可用工具
+{tools}
+
+## 工作流程(严格遵守)
+**每次回复必须包含 Thought 和 Action 两部分,缺一不可:**
+
+Thought: <你的思考(简短)>
+Action: <以下二选一>
+- tool_name[tool_input]
+- Finish[最终回答(必要时包含 *** Begin Patch...*** End Patch)]
+
+**关键规则:**
+1. **永远不要只有 Thought 没有 Action** - 这会导致解析失败!
+2. 如果已有足够信息回答,必须用 `Finish[答案]` 结束
+3. 每次只执行一个工具调用,等待结果后再决定下一步
+4. 不要连续写多个 Action
+## 证据与任务管理策略(重要)
+**优先使用保底上下文(对话历史 + 上次工具结果)推理,证据不足时再调用工具:**
+
+1. **先评估已有信息**:检查对话历史和上次工具输出是否已包含答案
+2. **需要新证据时**:
+   - 涉及"之前说了什么/记得吗" → 直接查看对话历史,不需要调用工具
+   - 需要查看代码/文件 → 优先 `terminal` (快速定位)
+   - 需要搜索代码/笔记/记忆 → 使用 `context_fetch` (聚合搜索,单次上限 ~800 tokens/源)
+   - 需要执行命令/写笔记 → 使用对应工具
+3. **多步骤/需持续跟踪的任务**:如果任务有≥2个子步骤、需用户确认、或跨回合继续,请先/及时用 `todo` 记录或更新;确保同时最多 1 个 `in_progress`。若用户表达“分步/步骤/三步/改造/计划/完成后”等,多数情况下先 `todo add` 再行动,结尾 `todo list` 汇总。
+4. **避免过度收集**:不要为了"更全面"而反复调用工具
+
+## context_fetch 使用指南
+何时使用:
+- ✅ 用户问"有没有关于 X 的笔记/记忆"
+- ✅ 需要搜索代码中的类/函数定义
+- ✅ 提到错误栈/报错信息,需要找相关代码
+- ❌ 用户问"我们刚才说了什么" (直接用对话历史)
+- ❌ 已经通过 terminal 拿到足够证据
+
+参数说明:
+- `sources`: 可选 ["notes", "memory", "files", "tests"],可多选
+- `query`: 关键词(类名/函数名/错误关键字)
+- `paths`: 限定搜索范围(如 "src/**/*.py"),避免全仓库扫描
+- `budget_tokens`: 单个源的返回上限,默认 800(已内置控制,不需指定)
+## 停止条件(非常重要)
+- 一旦你已经拿到了足够的证据(例如:rg 命中、关键文件片段、错误栈、配置项),**必须**使用 `Finish[...]` 结束,不要为了"更全面"继续调用更多工具。
+- 如果你发现自己准备重复执行同一个工具调用(相同命令/相同文件范围),通常说明没有新信息:**立即**改用 `Finish[...]` 给出当前结论 + 下一步最小化建议。
+- **记住:有答案就 Finish,永远不要只写 Thought 而不写 Action!**
+
+## 工具输入约定
+- terminal:推荐 JSON,例如 `terminal[{{"command":"rg -n \\"ContextBuilder\\" -S .","allow_dangerous":false}}]`
+  - 支持管道等 shell 写法(例如 `rg ... | head`)
+  - 包含重定向(`>`/`>>`)、子命令替换(`$()`/反引号)或危险命令时需确认
+- context_fetch:**聚合搜索工具(优先推荐)**,例如 `context_fetch[{{"sources":["files","notes"],"query":"ContextBuilder","paths":"context/**/*.py"}}]`
+  - 一次调用可搜索多个源(notes/memory/files/tests)
+  - 返回结构化结果,自动控制 token 预算(~800/源)
+  - **优于直接用 note/memory search:避免多次工具调用**
+- note:必须 JSON,例如 `note[{{"action":"create","title":"...","content":"...","note_type":"task_state","tags":["..."]}}]`
+- memory:推荐 JSON,例如 `memory[{{"action":"add","memory_type":"episodic","content":"...","importance":0.6}}]`
+- plan:可用纯文本目标,或 JSON(见工具说明)
+- todo:JSON 调用管理待办,适用于多步骤任务跟踪;示例
+  - `todo[{{"action":"add","title":"修复 hello 页面样式","desc":"补充内联 CSS","status":"pending"}}]`
+  - `todo[{{"action":"update","id":3,"status":"in_progress"}}]`(同时仅允许 1 个 in_progress)
+  - `todo[{{"action":"list"}}]`(输出按 in_progress/pending/completed 分组的要点列表)
+## 补丁格式(产出代码修改时)
+当需要修改代码时,在 `Finish[...]` 中输出补丁。**补丁必须单独成段,`*** Begin Patch` 必须独占一行(前面不能有任何文字)**:
+
+**正确格式:**
+```
+Finish[
+已为 testDemo/hello.html 添加样式。
+
+*** Begin Patch
+*** Update File: testDemo/hello.html
+<!DOCTYPE html>
+<html>
+<head>
+  <style>
+    body {{ background: #f0f0f0; }}
+  </style>
+</head>
+<body>
+  <h1>Hello World</h1>
+</body>
+</html>
+*** End Patch
+]
+```
+
+**关键要点:**
+1. 说明文字和补丁之间要有**空行**分隔
+2. `*** Begin Patch` **独占一行**(不要在同一行前面加任何字符)
+3. `*** End Patch` **独占一行**(不要在同一行后面加任何字符)
+4. 不要用 markdown 代码块包裹补丁(不要用 ``` )
+
+**常见错误对比:**
+```
+❌ 错误1:补丁前有冒号
+Finish[补丁如下:*** Begin Patch...]
+
+❌ 错误2:补丁前有文字在同一行
+Finish[这是补丁 *** Begin Patch...]
+
+❌ 错误3:没有空行分隔
+Finish[已添加样式
+*** Begin Patch...]
+
+✅ 正确:说明和补丁分段
+Finish[已添加样式
+
+*** Begin Patch...]
+```
+## 关键行为准则
+- 先证据后结论:回答“项目结构/模块职责”等问题前,先用 terminal 取到目录/文件列表/关键入口文件证据
+- 不要擅自做代码质量评审:除非用户明确要求“代码质量/重构/修 bug”
+- 不要在没有明确需求时输出补丁;需要澄清就问
+- 删除文件/大改动:先解释风险并征求确认;确认后再在 Finish 里给出补丁
+
+## 当前任务
+Question: {question}
+
+## 执行历史
+{history}

+ 19 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/prompts/summarize_observation.md

@@ -0,0 +1,19 @@
+你是一个“工具输出摘要器”。你的任务是把一段工具输出压缩成短摘要,供后续上下文使用。
+
+要求:
+- 保留对当前任务最重要的信息(文件路径、函数/类名、报错、关键数字、结论)
+- 如果是终端命令输出:优先保留“命中行/关键信息”,不要堆满无关行
+- 不要编造不存在的内容;不确定就写“不确定/需要进一步检索”
+- 输出尽量短:目标 120~200 中文字或 10~15 行以内
+- 最后给出 1~3 条“下一步建议”(如果有)
+
+输出格式(Markdown):
+## Summary
+- ...
+
+## Key Details
+- ...
+
+## Next
+- ...
+

+ 78 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/prompts/system.md

@@ -0,0 +1,78 @@
+你是一个“在仓库内工作的 CLI 编程助手”(类似 Claude Code/Codex),不是闲聊机器人。
+
+工作区固定为仓库根目录 `.`,核心准则:
+- 边界:所有路径必须在 repo_root 内,resolve 后校验前缀,拒绝逃逸。
+- 按需探索:只有确实需要证据时才调用终端;优先小范围命令(`ls` / `rg --files` / `rg <pat> <path>` / `sed -n <range>p <file>` / `cat <file>`);避免无端全库扫描。
+- 写盘唯一通道:补丁 + apply_patch。**严禁** `cat >` / `tee` / Here-Doc / 重定向等终端写法。
+- 高风险(删除/覆盖大量/危险命令 rm/chmod/git reset --hard)必须说明风险并征求确认;最终执行由 CLI 裁决。
+- 复杂任务遵循“计划 → 取证 → 补丁 → 确认 → 落盘 → 验证”节奏,最小改动满足需求。
+
+**关于对话历史的重要说明**:
+- 本段 [Role & Policies] 是你的**系统角色定义和工作规则**,不是用户对话内容
+- 当用户询问"我们之前聊了什么 / 说了什么 / 总结对话"时,请**只总结 [Context] 区块中的对话历史**(格式为 `[user]` 和 `[assistant]` 的交互记录)
+- **不要把系统规则、工具定义、角色描述当作"对话内容"来总结**
+- 总结对话时直接根据 [Context] 回答,不需要调用 memory 或 note 工具
+
+可用工具(ReAct Action 用):
+
+**优先使用的聚合搜索工具:**
+- **context_fetch[...]**:按需获取扩展上下文(单次可查多源,自动控制预算 ~800 tokens/源)
+  - JSON 格式:`{"sources": ["files","notes","memory","tests"], "query": "关键词", "paths": "src/**/*.py"}`
+  - **使用策略:先用保底上下文(对话历史+上次工具结果)推理,证据不足再调用**
+  - 优于单独调用 note/memory search:一次调用可搜索多个数据源,避免多次工具调用
+  - 自动预算控制:每源返回 ~800 tokens,避免上下文爆炸
+
+**其他工具:**
+- terminal[...]:只读检索;支持管道等,但重定向/子命令替换/危险命令需确认。写文件用补丁。
+- note[...]:记录关键结论/阻塞/行动。
+- memory[...]:跨会话情景记忆,需显式 add。
+- plan[...]:多步/模糊任务时生成计划;或用户要求时调用。
+- todo[...]:多步骤任务跟踪,状态 pending/in_progress/completed(仅 1 个 in_progress)。
+
+详尽用法参考 tools.md(按需自行查阅)。
+## 补丁格式(重要)
+
+产出补丁时,必须严格遵守以下格式:
+
+```
+*** Begin Patch
+*** Add File: path/to/new_file.py
+文件内容...
+可以多行...
+*** Update File: path/to/existing_file.py
+更新后的完整文件内容...
+*** Delete File: path/to/old_file.py
+*** End Patch
+```
+
+**关键规则:**
+1. 第一行必须是 `*** Begin Patch`(前面不要有任何文字)
+2. 最后一行必须是 `*** End Patch`
+3. 操作行格式:`*** Add File: <path>` / `*** Update File: <path>` / `*** Delete File: <path>`
+4. Add/Update 后面跟完整文件内容,Delete 后面不需要内容
+5. 不要在补丁外包裹 markdown 代码块(不要用 ```)
+6. 路径相对于仓库根目录
+
+**错误示例:**
+```
+这是一个补丁:
+*** Begin Patch
+...
+```
+❌ 问题:`*** Begin Patch` 前面有文字
+
+**正确示例:**
+```
+*** Begin Patch
+*** Add File: testDemo/style.css
+body {
+  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
+  font-family: Arial, sans-serif;
+}
+*** End Patch
+```
+✅ 第一行就是 `*** Begin Patch`
+
+用户如果只是问候/闲聊,直接自然回复,不要调用工具,不要输出补丁。
+
+**输出风格**:非代码/非工具回复尽量 ≤4 行,直接给结论,避免“Here is...”等冗余开场;除非用户要求,不使用 emoji;事实性问题直接给结果。

+ 50 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/code_agent/prompts/tools.md

@@ -0,0 +1,50 @@
+# 工具使用指南(Claude Code 风格精简版)
+
+针对当前内置工具:`terminal`、`context_fetch`、`todo`、`note`、`memory`、`plan`。遵循“明确何时用 / 何时不用 / 如何用”,避免盲目调用。
+
+## terminal
+用途:只读检索与快速查看(ls/rg/cat/sed/head/tail/grep/git status/diff)。
+- 何时用:定位文件/符号/报错;小范围查看片段;确认目录结构。
+- 何时不用:写文件(用补丁);大范围全库扫描(除非用户要求);危险命令(rm/chmod/git reset --hard)。
+- 调用示例:`terminal[{"command":"rg -n \"foo\" context/**/*.py","allow_dangerous":false}]`
+
+## context_fetch
+用途:聚合搜索(files/notes/memory/tests),自动摘要,控制预算。
+- 何时用:需要“更多证据”时;搜类名/函数名/错误栈;需要相关笔记/记忆;比单独 note/memory 搜索更省步数。
+- 何时不用:已经有足够证据;用户仅问对话历史。
+- 调用示例:`context_fetch[{"sources":["files","notes"],"query":"ContextBuilder","paths":"context/**/*.py"}]`
+
+## todo
+用途:多步骤任务跟踪。状态:pending | in_progress(仅 1 个)| completed。
+- 何时用:3 步以上或多文件/多特性;用户列出多项需求;跨回合/需确认的任务;开始工作前先标记 in_progress,完成后立即 completed。
+- 何时不用:单一步、琐碎或纯问答。
+- 示例:
+  - `todo[{"action":"add","title":"设计简介页布局","desc":"头部/简介/技能","status":"pending"}]`
+  - `todo[{"action":"update","id":1,"status":"in_progress"}]`
+  - `todo[{"action":"update","id":1,"status":"completed"}]`
+  - `todo[{"action":"list"}]`
+
+## note
+用途:结构化笔记(action/decision/blocker/task_state 等),Markdown 持久化。
+- 何时用:记录关键结论/风险/阻塞;补丁成功/失败总结;阶段小结。
+- 何时不用:临时想法可先留在对话,不必频繁写笔记。
+- 示例:`note[{"action":"create","title":"Patch applied","content":"...","note_type":"action","tags":["patch"]}]`
+
+## memory
+用途:情景记忆(SQLite);跨会话回忆“发生过什么”。默认不开自动写,需要显式添加。
+- 何时用:需要在未来回忆本次决策/阻塞/结论;会话结束前写一条小结;复用过往经验时可先 search。
+- 何时不用:即时对话短期内容已有 history;信息尚不确定。
+- 示例:
+  - `memory[{"action":"add","memory_type":"episodic","content":"完成 hello.html 样式改造,见补丁...","importance":0.7}]`
+  - `memory[{"action":"search","query":"hello.html 样式","memory_types":["episodic"],"limit":5}]`
+
+## plan
+用途:显式规划工具,生成分步计划。
+- 何时用:任务模糊或明显多步骤;用户要求出计划;执行前需要拆解。
+- 何时不用:非常简单的一步任务。
+- 示例:`:plan 添加 dark mode 开关` 或 `plan[{"goal":"优化渲染性能,先梳理瓶颈再改"}]`
+
+## 重要提醒
+- 写/改文件必须用补丁(*** Begin Patch ...),禁止 `cat > file` / Here-Doc / tee / 重定向写盘。
+- 先用已有上下文推理,不足再调用工具;避免无端多次搜索。
+- todo 只保持 1 个 in_progress;完成立刻标记完成;阻塞则新增一条说明阻塞。

+ 10 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/context/__init__.py

@@ -0,0 +1,10 @@
+"""上下文工程模块
+
+为HelloAgents框架提供上下文工程能力:
+- ContextBuilder: GSSC流水线(Gather-Select-Structure-Compress)
+"""
+
+from .builder import ContextBuilder, ContextConfig, ContextPacket
+
+__all__ = ["ContextBuilder", "ContextConfig", "ContextPacket"]
+

+ 510 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/context/builder.py

@@ -0,0 +1,510 @@
+"""ContextBuilder - GSSC流水线实现
+
+实现 Gather-Select-Structure-Compress 上下文构建流程:
+1. Gather: 从多源收集候选信息(历史、记忆、RAG、工具结果)
+2. Select: 基于优先级、相关性、多样性筛选
+3. Structure: 组织成结构化上下文模板
+4. Compress: 在预算内压缩与规范化
+"""
+
+from typing import Dict, Any, List, Optional, Tuple, TYPE_CHECKING, Any as TypingAny
+from dataclasses import dataclass, field
+from datetime import datetime
+import tiktoken
+import math
+
+from core.message import Message
+from core.llm import HelloAgentsLLM
+
+if TYPE_CHECKING:
+    # Optional, only for type checking. Importing tools at runtime may pull in heavy optional deps.
+    from tools import MemoryTool, RAGTool
+else:
+    MemoryTool = TypingAny  # type: ignore[assignment,misc]
+    RAGTool = TypingAny  # type: ignore[assignment,misc]
+
+
+@dataclass
+class ContextPacket:
+    """上下文信息包"""
+    content: str
+    timestamp: datetime = field(default_factory=datetime.now)
+    metadata: Dict[str, Any] = field(default_factory=dict)
+    token_count: int = 0
+    relevance_score: float = 0.0  # 0.0-1.0
+    
+    def __post_init__(self):
+        """自动计算token数"""
+        if self.token_count == 0:
+            self.token_count = count_tokens(self.content)
+
+
+@dataclass
+class ContextConfig:
+    """上下文构建配置"""
+    max_tokens: int = 8000  # 总预算
+    reserve_ratio: float = 0.15  # 生成余量(10-20%)
+    min_relevance: float = 0.3  # 最小相关性阈值(仅对扩展上下文生效)
+    max_history_turns: int = 10  # 最大保留对话轮数
+    enable_mmr: bool = True  # 启用最大边际相关性(多样性)
+    mmr_lambda: float = 0.7  # MMR平衡参数(0=纯多样性, 1=纯相关性)
+    system_prompt_template: str = ""  # 系统提示模板
+    enable_compression: bool = True  # 启用压缩
+    include_output_format: bool = True  # 是否附加固定输出格式约束
+    # 按需探索模式:不主动查询 memory/rag,由模型通过工具按需获取
+    lazy_fetch: bool = True
+    
+    def get_available_tokens(self) -> int:
+        """获取可用token预算(扣除余量)"""
+        return int(self.max_tokens * (1 - self.reserve_ratio))
+
+
+class ContextBuilder:
+    """上下文构建器 - GSSC流水线
+    
+    设计理念(借鉴 Claude Code):
+    - 保底上下文:系统提示 + 对话历史 + 上次工具摘要(自动注入,不可或缺)
+    - 扩展上下文:memory/rag/notes(通过工具按需获取,模型自行决定)
+    
+    用法示例:
+    ```python
+    # 方式1:只构建保底上下文(推荐,让模型按需获取扩展上下文)
+    builder = ContextBuilder(config=ContextConfig(lazy_fetch=True))
+    context = builder.build_base(
+        user_query="用户问题",
+        conversation_history=[...],
+        system_instructions="系统指令",
+        tool_summaries=[...]  # 上次工具调用摘要
+    )
+    
+    # 方式2:传统模式,主动收集所有上下文
+    builder = ContextBuilder(
+        memory_tool=memory_tool,
+        rag_tool=rag_tool,
+        config=ContextConfig(lazy_fetch=False)
+    )
+    context = builder.build(user_query="用户问题", ...)
+    ```
+    """
+    
+    def __init__(
+        self,
+        memory_tool: Optional[MemoryTool] = None,
+        rag_tool: Optional[RAGTool] = None,
+        config: Optional[ContextConfig] = None,
+        llm: Optional[HelloAgentsLLM] = None,
+    ):
+        self.memory_tool = memory_tool
+        self.rag_tool = rag_tool
+        self.config = config or ContextConfig()
+        self.llm = llm
+        self._encoding = tiktoken.get_encoding("cl100k_base")
+    
+    def build_base(
+        self,
+        user_query: str,
+        conversation_history: Optional[List[Message]] = None,
+        system_instructions: Optional[str] = None,
+        tool_summaries: Optional[List[str]] = None,
+        pending_state: Optional[str] = None,
+    ) -> str:
+        """构建保底上下文(推荐使用)
+        
+        只包含必需的基础上下文,不主动查询 memory/rag。
+        扩展上下文由模型通过 context_fetch 工具按需获取。
+        
+        保底上下文包括:
+        - 系统指令/安全约束
+        - 最近 N 轮对话历史
+        - 上次工具调用摘要
+        - 待确认的补丁/计划状态
+        
+        Args:
+            user_query: 用户查询
+            conversation_history: 对话历史
+            system_instructions: 系统指令
+            tool_summaries: 上次工具调用摘要列表
+            pending_state: 待确认的状态(补丁/计划等)
+            
+        Returns:
+            结构化上下文字符串
+        """
+        packets = []
+        
+        # P0: 系统指令(必须)
+        if system_instructions:
+            packets.append(ContextPacket(
+                content=system_instructions,
+                metadata={"type": "instructions"}
+            ))
+        
+        # P1: 对话历史(必须)
+        if conversation_history:
+            recent_history = conversation_history[-self.config.max_history_turns:]
+            history_text = "\n".join([
+                f"[{msg.role}] {msg.content}"
+                for msg in recent_history
+            ])
+            packets.append(ContextPacket(
+                content=history_text,
+                metadata={"type": "history", "count": len(recent_history)}
+            ))
+        
+        # P2: 上次工具调用摘要(如有)
+        if tool_summaries:
+            summary_text = "\n".join(tool_summaries[-3:])  # 最多保留最近 3 条
+            packets.append(ContextPacket(
+                content=f"[上次工具结果摘要]\n{summary_text}",
+                metadata={"type": "tool_summary"}
+            ))
+        
+        # P3: 待确认状态(如有)
+        if pending_state:
+            packets.append(ContextPacket(
+                content=f"[待确认状态]\n{pending_state}",
+                metadata={"type": "pending_state"}
+            ))
+        
+        # 直接结构化,不做相关性过滤(保底上下文全部保留)
+        structured_context = self._structure_base(packets, user_query)
+        
+        # 压缩(如果超预算)
+        return self._compress(structured_context)
+    
+    def _structure_base(
+        self,
+        packets: List[ContextPacket],
+        user_query: str,
+    ) -> str:
+        """为保底上下文构建结构化模板"""
+        sections = []
+        
+        # [Role & Policies]
+        instructions = [p for p in packets if p.metadata.get("type") == "instructions"]
+        if instructions:
+            sections.append("[Role & Policies]\n" + "\n".join([p.content for p in instructions]))
+        
+        # [Context] - 对话历史
+        history = [p for p in packets if p.metadata.get("type") == "history"]
+        if history:
+            sections.append("[Context]\n以下是最近的对话记录:\n" + "\n".join([p.content for p in history]))
+        
+        # [Evidence] - 工具摘要
+        tool_summary = [p for p in packets if p.metadata.get("type") == "tool_summary"]
+        if tool_summary:
+            sections.append("[Evidence]\n" + "\n".join([p.content for p in tool_summary]))
+        
+        # [State] - 待确认状态
+        pending = [p for p in packets if p.metadata.get("type") == "pending_state"]
+        if pending:
+            sections.append("[State]\n" + "\n".join([p.content for p in pending]))
+        
+        # [Task]
+        sections.append(f"[Task]\n{user_query}")
+        
+        return "\n\n".join(sections)
+    
+    def build(
+        self,
+        user_query: str,
+        conversation_history: Optional[List[Message]] = None,
+        system_instructions: Optional[str] = None,
+        additional_packets: Optional[List[ContextPacket]] = None
+    ) -> str:
+        """构建完整上下文
+        
+        Args:
+            user_query: 用户查询
+            conversation_history: 对话历史
+            system_instructions: 系统指令
+            additional_packets: 额外的上下文包
+            
+        Returns:
+            结构化上下文字符串
+        """
+        # 1. Gather: 收集候选信息
+        packets = self._gather(
+            user_query=user_query,
+            conversation_history=conversation_history or [],
+            system_instructions=system_instructions,
+            additional_packets=additional_packets or []
+        )
+        
+        # 2. Select: 筛选与排序
+        selected_packets = self._select(packets, user_query)
+        
+        # 3. Structure: 组织成结构化模板
+        structured_context = self._structure(
+            selected_packets=selected_packets,
+            user_query=user_query,
+            system_instructions=system_instructions
+        )
+        
+        # 4. Compress: 压缩与规范化(如果超预算)
+        final_context = self._compress(structured_context)
+        
+        return final_context
+    
+    def _gather(
+        self,
+        user_query: str,
+        conversation_history: List[Message],
+        system_instructions: Optional[str],
+        additional_packets: List[ContextPacket]
+    ) -> List[ContextPacket]:
+        """Gather: 收集候选信息
+        
+        当 lazy_fetch=True 时,只收集保底上下文(系统指令+对话历史+额外包)。
+        当 lazy_fetch=False 时,主动查询 memory/rag(传统模式)。
+        """
+        packets = []
+        
+        # P0: 系统指令(强约束,总是保留)
+        if system_instructions:
+            packets.append(ContextPacket(
+                content=system_instructions,
+                metadata={"type": "instructions"}
+            ))
+        
+        # P1: 对话历史(基础上下文,总是保留)
+        if conversation_history:
+            recent_history = conversation_history[-self.config.max_history_turns:]
+            history_text = "\n".join([
+                f"[{msg.role}] {msg.content}"
+                for msg in recent_history
+            ])
+            packets.append(ContextPacket(
+                content=history_text,
+                metadata={"type": "history", "count": len(recent_history)}
+            ))
+        
+        # 以下为扩展上下文,仅在 lazy_fetch=False 时主动收集
+        if not self.config.lazy_fetch:
+            # P2: 从记忆中获取任务状态与关键结论
+            if self.memory_tool:
+                try:
+                    # 搜索任务状态相关记忆
+                    state_results = self.memory_tool.execute(
+                        "search",
+                        query="(任务状态 OR 子目标 OR 结论 OR 阻塞)",
+                        min_importance=0.7,
+                        limit=5
+                    )
+                    if state_results and "未找到" not in state_results:
+                        packets.append(ContextPacket(
+                            content=state_results,
+                            metadata={"type": "task_state", "importance": "high"}
+                        ))
+                    
+                    # 搜索与当前查询相关的记忆
+                    related_results = self.memory_tool.execute(
+                        "search",
+                        query=user_query,
+                        limit=5
+                    )
+                    if related_results and "未找到" not in related_results:
+                        packets.append(ContextPacket(
+                            content=related_results,
+                            metadata={"type": "related_memory"}
+                        ))
+                except Exception as e:
+                    print(f"⚠️ 记忆检索失败: {e}")
+            
+            # P3: 从RAG中获取事实证据
+            if self.rag_tool:
+                try:
+                    rag_results = self.rag_tool.run({
+                        "action": "search",
+                        "query": user_query,
+                        "top_k": 5
+                    })
+                    if rag_results and "未找到" not in rag_results and "错误" not in rag_results:
+                        packets.append(ContextPacket(
+                            content=rag_results,
+                            metadata={"type": "knowledge_base"}
+                        ))
+                except Exception as e:
+                    print(f"⚠️ RAG检索失败: {e}")
+        
+        # 添加额外包(如上次工具结果摘要)
+        packets.extend(additional_packets)
+        
+        return packets
+    
+    def _select(
+        self,
+        packets: List[ContextPacket],
+        user_query: str
+    ) -> List[ContextPacket]:
+        """Select: 基于分数与预算的筛选"""
+        # 1) 计算相关性(关键词重叠)
+        query_tokens = set(user_query.lower().split())
+        for packet in packets:
+            content_tokens = set(packet.content.lower().split())
+            if len(query_tokens) > 0:
+                overlap = len(query_tokens & content_tokens)
+                packet.relevance_score = overlap / len(query_tokens)
+            else:
+                packet.relevance_score = 0.0
+        
+        # 2) 计算新近性(指数衰减)
+        def recency_score(ts: datetime) -> float:
+            delta = max((datetime.now() - ts).total_seconds(), 0)
+            tau = 3600  # 1小时时间尺度,可暴露到配置
+            return math.exp(-delta / tau)
+        
+        # 3) 计算复合分:0.7*相关性 + 0.3*新近性
+        scored_packets: List[Tuple[float, ContextPacket]] = []
+        for p in packets:
+            rec = recency_score(p.timestamp)
+            score = 0.7 * p.relevance_score + 0.3 * rec
+            scored_packets.append((score, p))
+        
+        # 4) 系统指令和对话历史单独拿出,固定纳入(这两者是基础上下文,不应被过滤)
+        must_keep_types = {"instructions", "history"}
+        must_keep_packets = [p for (_, p) in scored_packets if p.metadata.get("type") in must_keep_types]
+        remaining = [p for (s, p) in sorted(scored_packets, key=lambda x: x[0], reverse=True)
+                     if p.metadata.get("type") not in must_keep_types]
+        
+        # 5) 依据 min_relevance 过滤(仅对扩展上下文:memory、RAG、notes 等)
+        filtered = [p for p in remaining if p.relevance_score >= self.config.min_relevance]
+        
+        # 6) 按预算填充
+        available_tokens = self.config.get_available_tokens()
+        selected: List[ContextPacket] = []
+        used_tokens = 0
+        
+        # 先放入必须保留的上下文(系统指令 + 对话历史)
+        for p in must_keep_packets:
+            if used_tokens + p.token_count <= available_tokens:
+                selected.append(p)
+                used_tokens += p.token_count
+        
+        # 再按分数加入其余
+        for p in filtered:
+            if used_tokens + p.token_count > available_tokens:
+                continue
+            selected.append(p)
+            used_tokens += p.token_count
+        
+        return selected
+    
+    def _structure(
+        self,
+        selected_packets: List[ContextPacket],
+        user_query: str,
+        system_instructions: Optional[str]
+    ) -> str:
+        """Structure: 组织成结构化上下文模板"""
+        sections = []
+        
+        # [Role & Policies] - 系统指令
+        p0_packets = [p for p in selected_packets if p.metadata.get("type") == "instructions"]
+        if p0_packets:
+            role_section = "[Role & Policies]\n"
+            role_section += "\n".join([p.content for p in p0_packets])
+            sections.append(role_section)
+        
+        # [Task] - 当前任务
+        sections.append(f"[Task]\n用户问题:{user_query}")
+        
+        # [State] - 任务状态
+        p1_packets = [p for p in selected_packets if p.metadata.get("type") == "task_state"]
+        if p1_packets:
+            state_section = "[State]\n关键进展与未决问题:\n"
+            state_section += "\n".join([p.content for p in p1_packets])
+            sections.append(state_section)
+        
+        # [Evidence] - 事实证据
+        p2_packets = [
+            p for p in selected_packets
+            if p.metadata.get("type") in {"related_memory", "knowledge_base", "retrieval", "tool_result"}
+        ]
+        if p2_packets:
+            evidence_section = "[Evidence]\n事实与引用:\n"
+            for p in p2_packets:
+                evidence_section += f"\n{p.content}\n"
+            sections.append(evidence_section)
+        
+        # [Recent Conversation] - 对话历史(明确标识)
+        p3_packets = [p for p in selected_packets if p.metadata.get("type") == "history"]
+        if p3_packets:
+            context_section = "[Recent Conversation]\n以下是最近的对话记录。当用户询问之前的对话内容时,请参考此部分:\n"
+            context_section += "\n".join([p.content for p in p3_packets])
+            sections.append(context_section)
+        
+        # [Output] - 输出约束(可选)
+        if self.config.include_output_format:
+            output_section = """[Output]
+请按以下格式回答:
+1. 结论(简洁明确)
+2. 依据(列出支撑证据及来源)
+3. 风险与假设(如有)
+4. 下一步行动建议(如适用)"""
+            sections.append(output_section)
+        
+        return "\n\n".join(sections)
+    
+    def _compress(self, context: str) -> str:
+        """Compress: 压缩与规范化"""
+        if not self.config.enable_compression:
+            return context
+        
+        current_tokens = count_tokens(context)
+        available_tokens = self.config.get_available_tokens()
+        
+        if current_tokens <= available_tokens:
+            return context
+
+        # LLM 压缩(更保真):仅在提供 llm 时启用
+        if self.llm is not None:
+            try:
+                target = available_tokens
+                # 尽量保留结构:Role/Task 原样,压缩 Evidence/Context
+                sys = (
+                    "你是一个上下文压缩器。将输入的多段上下文压缩到目标 token 预算内,"
+                    "尽量保留:用户目标、关键约束、关键证据(文件路径/命令/错误/结论)。"
+                    "不要编造。保持原有分段标题([Role & Policies]/[Task]/[State]/[Evidence]/[Context] 等),"
+                    "但可以大幅精简 [Evidence]/[Context] 内容。输出应尽量短。"
+                )
+                user = f"目标预算(约): {target} tokens\n\n原始上下文:\n{context}"
+                compressed = self.llm.invoke(
+                    [
+                        {"role": "system", "content": sys},
+                        {"role": "user", "content": user},
+                    ],
+                    max_tokens=min(1200, int(self.config.max_tokens * 0.4)),
+                )
+                if compressed and isinstance(compressed, str) and count_tokens(compressed) <= target:
+                    return compressed
+            except Exception:
+                pass
+        
+        # 简单截断策略(保留前N个token)
+        # 实际应用中可用LLM做高保真摘要
+        print(f"⚠️ 上下文超预算 ({current_tokens} > {available_tokens}),执行截断")
+        
+        # 按段落截断,保留结构
+        lines = context.split("\n")
+        compressed_lines = []
+        used_tokens = 0
+        
+        for line in lines:
+            line_tokens = count_tokens(line)
+            if used_tokens + line_tokens > available_tokens:
+                break
+            compressed_lines.append(line)
+            used_tokens += line_tokens
+        
+        return "\n".join(compressed_lines)
+
+
+def count_tokens(text: str) -> int:
+    """计算文本token数(使用tiktoken)"""
+    try:
+        encoding = tiktoken.get_encoding("cl100k_base")
+        return len(encoding.encode(text))
+    except Exception:
+        # 降级方案:粗略估算(1 token ≈ 4 字符)
+        return len(text) // 4
+

+ 19 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/core/__init__.py

@@ -0,0 +1,19 @@
+"""核心框架模块
+
+说明:本仓库的核心模块包含可选依赖(如 pydantic)。为便于在最小环境下
+复用部分能力(例如仅使用 LLM 客户端),这里对可选依赖做惰性/容错导入。
+"""
+
+from .exceptions import HelloAgentsException
+from .llm import HelloAgentsLLM
+
+try:
+    from .agent import Agent
+    from .config import Config
+    from .message import Message
+except Exception:  # optional deps may be missing in minimal environments
+    Agent = None  # type: ignore[assignment]
+    Config = None  # type: ignore[assignment]
+    Message = None  # type: ignore[assignment]
+
+__all__ = ["HelloAgentsLLM", "HelloAgentsException", "Agent", "Config", "Message"]

+ 46 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/core/agent.py

@@ -0,0 +1,46 @@
+"""Agent基类"""
+
+from abc import ABC, abstractmethod
+from typing import Optional
+from .message import Message
+from .llm import HelloAgentsLLM
+from .config import Config
+
+class Agent(ABC):
+    """Agent基类"""
+    
+    def __init__(
+        self,
+        name: str,
+        llm: HelloAgentsLLM,
+        system_prompt: Optional[str] = None,
+        config: Optional[Config] = None
+    ):
+        self.name = name
+        self.llm = llm
+        self.system_prompt = system_prompt
+        self.config = config or Config()
+        self._history: list[Message] = []
+    
+    @abstractmethod
+    def run(self, input_text: str, **kwargs) -> str:
+        """运行Agent"""
+        pass
+    
+    def add_message(self, message: Message):
+        """添加消息到历史记录"""
+        self._history.append(message)
+    
+    def clear_history(self):
+        """清空历史记录"""
+        self._history.clear()
+    
+    def get_history(self) -> list[Message]:
+        """获取历史记录"""
+        return self._history.copy()
+    
+    def __str__(self) -> str:
+        return f"Agent(name={self.name}, provider={self.llm.provider})"
+    
+    def __repr__(self) -> str:
+        return self.__str__()

+ 134 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/core/config.py

@@ -0,0 +1,134 @@
+"""配置管理 - Code Agent CLI 统一配置"""
+
+import os
+from typing import Optional, Dict, Any, List
+from pathlib import Path
+from pydantic import BaseModel, Field
+
+
+class Config(BaseModel):
+    """Code Agent CLI 统一配置类
+    
+    集中管理所有配置项,支持:
+    - 环境变量加载
+    - 默认值设置
+    - 类型验证
+    """
+    
+    # ==================== 基础配置 ====================
+    project_name: str = Field(default="code_agent", description="项目名称")
+    debug: bool = Field(default=False, description="调试模式")
+    log_level: str = Field(default="INFO", description="日志级别")
+    
+    # ==================== LLM 配置 ====================
+    default_model: str = Field(default="gpt-3.5-turbo", description="默认模型")
+    default_provider: str = Field(default="openai", description="默认提供商")
+    temperature: float = Field(default=0.7, ge=0.0, le=2.0, description="温度参数")
+    max_tokens: Optional[int] = Field(default=None, description="最大 token 数")
+    llm_timeout: int = Field(default=60, gt=0, description="LLM 请求超时(秒)")
+    
+    # ==================== Agent 配置 ====================
+    max_react_steps: int = Field(default=20, gt=0, le=50, description="ReAct 最大步数")
+    max_history_turns: int = Field(default=50, gt=0, description="最大历史对话轮数")
+    observation_summary_threshold: int = Field(default=2000, gt=0, description="工具输出摘要阈值")
+    
+    # ==================== 上下文配置 ====================
+    context_max_tokens: int = Field(default=8000, gt=0, description="上下文最大 token 数")
+    context_reserve_ratio: float = Field(default=0.15, ge=0.0, le=0.5, description="生成预留比例")
+    context_enable_compression: bool = Field(default=True, description="启用上下文压缩")
+    context_lazy_fetch: bool = Field(default=True, description="按需获取上下文")
+    
+    # ==================== 工具配置 ====================
+    terminal_timeout: int = Field(default=60, gt=0, description="终端命令超时(秒)")
+    terminal_max_output_size: int = Field(default=10 * 1024 * 1024, gt=0, description="终端输出最大大小")
+    terminal_confirm_dangerous: bool = Field(default=True, description="危险命令需要确认")
+    terminal_allow_shell_mode: bool = Field(default=True, description="允许 Shell 模式")
+    context_fetch_max_tokens: int = Field(default=800, gt=0, description="单个数据源最大 token")
+    context_fetch_context_lines: int = Field(default=5, ge=0, description="代码上下文行数")
+    
+    # ==================== 补丁执行器配置 ====================
+    patch_max_files: int = Field(default=10, gt=0, description="单个补丁最大文件数")
+    patch_max_total_lines: int = Field(default=800, gt=0, description="单个补丁最大总行数")
+    patch_allowed_suffixes: List[str] = Field(
+        default=[".py", ".md", ".toml", ".json", ".yml", ".yaml", ".txt", ".html", ".css", ".js", ".ts"],
+        description="允许修改的文件后缀"
+    )
+    
+    # ==================== 存储配置 ====================
+    helloagents_dir: str = Field(default=".helloagents", description="状态存储目录")
+    
+    # ==================== 安全配置 ====================
+    confirm_delete_files: bool = Field(default=True, description="删除文件需要确认")
+    confirm_large_changes: bool = Field(default=True, description="大规模变更需要确认")
+    large_change_threshold_files: int = Field(default=6, gt=0, description="大规模变更文件数阈值")
+    large_change_threshold_lines: int = Field(default=400, gt=0, description="大规模变更行数阈值")
+    
+    @classmethod
+    def from_env(cls, **overrides) -> "Config":
+        """从环境变量创建配置
+        
+        环境变量命名规则:
+        - CODE_AGENT_<配置项大写> 或传统命名
+        
+        Args:
+            **overrides: 手动覆盖的配置项
+        """
+        env_config = {
+            "debug": os.getenv("DEBUG", "false").lower() == "true" or os.getenv("CODE_AGENT_DEBUG", "false").lower() == "true",
+            "log_level": os.getenv("LOG_LEVEL", "INFO"),
+            "temperature": float(os.getenv("TEMPERATURE", "0.7")),
+            "helloagents_dir": os.getenv("HELLOAGENTS_DIR", os.getenv("CODE_AGENT_STATE_DIR", ".helloagents")),
+            "max_react_steps": int(os.getenv("CODE_AGENT_MAX_REACT_STEPS", os.getenv("CODE_AGENT_MAX_STEPS", "20"))),
+            "llm_timeout": int(os.getenv("LLM_TIMEOUT", "60")),
+            "terminal_timeout": int(os.getenv("CODE_AGENT_TERMINAL_TIMEOUT", "60")),
+            "patch_max_files": int(os.getenv("CODE_AGENT_PATCH_MAX_FILES", "10")),
+            "patch_max_total_lines": int(os.getenv("CODE_AGENT_PATCH_MAX_LINES", "800")),
+        }
+        
+        if os.getenv("MAX_TOKENS"):
+            env_config["max_tokens"] = int(os.getenv("MAX_TOKENS"))
+        
+        # 合并覆盖配置
+        env_config.update(overrides)
+        
+        return cls(**env_config)
+    
+    def get_state_dir(self, repo_root: Path) -> Path:
+        """获取状态存储目录的绝对路径"""
+        state_path = Path(self.helloagents_dir)
+        if state_path.is_absolute():
+            return state_path
+        return repo_root / state_path
+    
+    def get_notes_dir(self, repo_root: Path) -> Path:
+        """获取笔记目录"""
+        return self.get_state_dir(repo_root) / "notes"
+    
+    def get_sessions_dir(self, repo_root: Path) -> Path:
+        """获取会话目录"""
+        return self.get_state_dir(repo_root) / "sessions"
+    
+    def get_backups_dir(self, repo_root: Path) -> Path:
+        """获取备份目录"""
+        return self.get_state_dir(repo_root) / "backups"
+    
+    def get_todos_dir(self, repo_root: Path) -> Path:
+        """获取待办目录"""
+        return self.get_state_dir(repo_root) / "todos"
+    
+    def to_dict(self) -> Dict[str, Any]:
+        """转换为字典"""
+        return self.dict()
+    
+    def print_summary(self):
+        """打印配置摘要"""
+        print("=" * 50)
+        print("Code Agent CLI 配置")
+        print("=" * 50)
+        print(f"调试模式: {self.debug}")
+        print(f"ReAct 步数: {self.max_react_steps}")
+        print(f"历史轮数: {self.max_history_turns}")
+        print(f"终端超时: {self.terminal_timeout}s")
+        print(f"补丁限制: {self.patch_max_files} 文件, {self.patch_max_total_lines} 行")
+        print(f"状态目录: {self.helloagents_dir}")
+        print("=" * 50)

+ 195 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/core/database_config.py

@@ -0,0 +1,195 @@
+"""
+数据库配置管理
+支持Qdrant向量数据库和Neo4j图数据库的配置
+"""
+
+import os
+from dotenv import load_dotenv
+from typing import Dict, Any, Optional
+from pydantic import BaseModel, Field
+import logging
+
+logger = logging.getLogger(__name__)
+
+# Load environment variables early so DB configs pick them up
+load_dotenv()
+
+
+class QdrantConfig(BaseModel):
+    """Qdrant向量数据库配置"""
+    
+    # 连接配置
+    url: Optional[str] = Field(
+        default=None,
+        description="Qdrant服务URL (云服务或自定义URL)"
+    )
+    api_key: Optional[str] = Field(
+        default=None,
+        description="Qdrant API密钥 (云服务需要)"
+    )
+    
+    # 集合配置
+    collection_name: str = Field(
+        default="hello_agents_vectors",
+        description="向量集合名称"
+    )
+    vector_size: int = Field(
+        default=384,
+        description="向量维度"
+    )
+    distance: str = Field(
+        default="cosine",
+        description="距离度量方式 (cosine, dot, euclidean)"
+    )
+    
+    # 连接配置
+    timeout: int = Field(
+        default=30,
+        description="连接超时时间(秒)"
+    )
+    
+    @classmethod
+    def from_env(cls) -> "QdrantConfig":
+        """从环境变量创建配置"""
+        return cls(
+            url=os.getenv("QDRANT_URL"),
+            api_key=os.getenv("QDRANT_API_KEY"),
+            collection_name=os.getenv("QDRANT_COLLECTION", "hello_agents_vectors"),
+            vector_size=int(os.getenv("QDRANT_VECTOR_SIZE", "384")),
+            distance=os.getenv("QDRANT_DISTANCE", "cosine"),
+            timeout=int(os.getenv("QDRANT_TIMEOUT", "30"))
+        )
+    
+    def to_dict(self) -> Dict[str, Any]:
+        """转换为字典"""
+        return self.model_dump(exclude_none=True)
+
+
+class Neo4jConfig(BaseModel):
+    """Neo4j图数据库配置"""
+    
+    # 连接配置
+    uri: str = Field(
+        default="bolt://localhost:7687",
+        description="Neo4j连接URI"
+    )
+    username: str = Field(
+        default="neo4j",
+        description="用户名"
+    )
+    password: str = Field(
+        default="hello-agents-password",
+        description="密码"
+    )
+    database: str = Field(
+        default="neo4j",
+        description="数据库名称"
+    )
+    
+    # 连接池配置
+    max_connection_lifetime: int = Field(
+        default=3600,
+        description="最大连接生命周期(秒)"
+    )
+    max_connection_pool_size: int = Field(
+        default=50,
+        description="最大连接池大小"
+    )
+    connection_acquisition_timeout: int = Field(
+        default=60,
+        description="连接获取超时(秒)"
+    )
+    
+    @classmethod
+    def from_env(cls) -> "Neo4jConfig":
+        """从环境变量创建配置"""
+        return cls(
+            uri=os.getenv("NEO4J_URI", "bolt://localhost:7687"),
+            username=os.getenv("NEO4J_USERNAME", "neo4j"),
+            password=os.getenv("NEO4J_PASSWORD", "hello-agents-password"),
+            database=os.getenv("NEO4J_DATABASE", "neo4j"),
+            max_connection_lifetime=int(os.getenv("NEO4J_MAX_CONNECTION_LIFETIME", "3600")),
+            max_connection_pool_size=int(os.getenv("NEO4J_MAX_CONNECTION_POOL_SIZE", "50")),
+            connection_acquisition_timeout=int(os.getenv("NEO4J_CONNECTION_TIMEOUT", "60"))
+        )
+    
+    def to_dict(self) -> Dict[str, Any]:
+        """转换为字典"""
+        return self.model_dump()
+
+
+class DatabaseConfig(BaseModel):
+    """数据库配置管理器"""
+    
+    qdrant: QdrantConfig = Field(
+        default_factory=QdrantConfig,
+        description="Qdrant向量数据库配置"
+    )
+    neo4j: Neo4jConfig = Field(
+        default_factory=Neo4jConfig,
+        description="Neo4j图数据库配置"
+    )
+    
+    @classmethod
+    def from_env(cls) -> "DatabaseConfig":
+        """从环境变量创建配置"""
+        return cls(
+            qdrant=QdrantConfig.from_env(),
+            neo4j=Neo4jConfig.from_env()
+        )
+    
+    def get_qdrant_config(self) -> Dict[str, Any]:
+        """获取Qdrant配置字典"""
+        return self.qdrant.to_dict()
+    
+    def get_neo4j_config(self) -> Dict[str, Any]:
+        """获取Neo4j配置字典"""
+        return self.neo4j.to_dict()
+    
+    def validate_connections(self) -> Dict[str, bool]:
+        """验证数据库连接配置"""
+        results = {}
+        
+        # 验证Qdrant配置
+        try:
+            from ..memory.storage.qdrant_store import QdrantVectorStore
+            qdrant_store = QdrantVectorStore(**self.get_qdrant_config())
+            results["qdrant"] = qdrant_store.health_check()
+            logger.info(f"✅ Qdrant连接验证: {'成功' if results['qdrant'] else '失败'}")
+        except Exception as e:
+            results["qdrant"] = False
+            logger.error(f"❌ Qdrant连接验证失败: {e}")
+        
+        # 验证Neo4j配置
+        try:
+            from ..memory.storage.neo4j_store import Neo4jGraphStore
+            neo4j_store = Neo4jGraphStore(**self.get_neo4j_config())
+            results["neo4j"] = neo4j_store.health_check()
+            logger.info(f"✅ Neo4j连接验证: {'成功' if results['neo4j'] else '失败'}")
+        except Exception as e:
+            results["neo4j"] = False
+            logger.error(f"❌ Neo4j连接验证失败: {e}")
+        
+        return results
+
+
+# 全局配置实例
+db_config = DatabaseConfig.from_env()
+
+
+def get_database_config() -> DatabaseConfig:
+    """获取数据库配置"""
+    return db_config
+
+
+def update_database_config(**kwargs) -> None:
+    """更新数据库配置"""
+    global db_config
+    
+    if "qdrant" in kwargs:
+        db_config.qdrant = QdrantConfig(**kwargs["qdrant"])
+    
+    if "neo4j" in kwargs:
+        db_config.neo4j = Neo4jConfig(**kwargs["neo4j"])
+    
+    logger.info("✅ 数据库配置已更新")

+ 21 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/core/exceptions.py

@@ -0,0 +1,21 @@
+"""异常体系"""
+
+class HelloAgentsException(Exception):
+    """HelloAgents基础异常类"""
+    pass
+
+class LLMException(HelloAgentsException):
+    """LLM相关异常"""
+    pass
+
+class AgentException(HelloAgentsException):
+    """Agent相关异常"""
+    pass
+
+class ConfigException(HelloAgentsException):
+    """配置相关异常"""
+    pass
+
+class ToolException(HelloAgentsException):
+    """工具相关异常"""
+    pass

+ 321 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/core/llm.py

@@ -0,0 +1,321 @@
+"""HelloAgents统一LLM接口 - 基于OpenAI原生API"""
+
+import os
+from typing import Literal, Optional, Iterator
+from openai import OpenAI
+
+from .exceptions import HelloAgentsException
+
+# 支持的LLM提供商
+SUPPORTED_PROVIDERS = Literal[
+    "openai", "deepseek", "qwen", "modelscope",
+    "kimi", "zhipu", "ollama", "vllm", "local", "auto"
+]
+
+class HelloAgentsLLM:
+    """
+    为HelloAgents定制的LLM客户端。
+    它用于调用任何兼容OpenAI接口的服务,并默认使用流式响应。
+
+    设计理念:
+    - 参数优先,环境变量兜底
+    - 流式响应为默认,提供更好的用户体验
+    - 支持多种LLM提供商
+    - 统一的调用接口
+    """
+
+    def __init__(
+        self,
+        model: Optional[str] = None,
+        api_key: Optional[str] = None,
+        base_url: Optional[str] = None,
+        provider: Optional[SUPPORTED_PROVIDERS] = None,
+        temperature: float = 0.7,
+        max_tokens: Optional[int] = None,
+        timeout: Optional[int] = None,
+        **kwargs
+    ):
+        """
+        初始化客户端。优先使用传入参数,如果未提供,则从环境变量加载。
+        支持自动检测provider或使用统一的LLM_*环境变量配置。
+
+        Args:
+            model: 模型名称,如果未提供则从环境变量LLM_MODEL_ID读取
+            api_key: API密钥,如果未提供则从环境变量读取
+            base_url: 服务地址,如果未提供则从环境变量LLM_BASE_URL读取
+            provider: LLM提供商,如果未提供则自动检测
+            temperature: 温度参数
+            max_tokens: 最大token数
+            timeout: 超时时间,从环境变量LLM_TIMEOUT读取,默认60秒
+        """
+        # 优先使用传入参数,如果未提供,则从环境变量加载
+        self.model = model or os.getenv("LLM_MODEL_ID")
+        self.temperature = temperature
+        self.max_tokens = max_tokens
+        self.timeout = timeout or int(os.getenv("LLM_TIMEOUT", "60"))
+        self.kwargs = kwargs
+
+        # 自动检测provider或使用指定的provider
+        self.provider = provider or self._auto_detect_provider(api_key, base_url)
+
+        # 根据provider确定API密钥和base_url
+        self.api_key, self.base_url = self._resolve_credentials(api_key, base_url)
+
+        # 验证必要参数
+        if not self.model:
+            self.model = self._get_default_model()
+        if not all([self.api_key, self.base_url]):
+            raise HelloAgentsException("API密钥和服务地址必须被提供或在.env文件中定义。")
+
+        # 创建OpenAI客户端
+        self._client = self._create_client()
+
+    def _auto_detect_provider(self, api_key: Optional[str], base_url: Optional[str]) -> str:
+        """
+        自动检测LLM提供商
+
+        检测逻辑:
+        1. 优先检查特定提供商的环境变量
+        2. 根据API密钥格式判断
+        3. 根据base_url判断
+        4. 默认返回通用配置
+        """
+        # 1. 检查特定提供商的环境变量
+        if os.getenv("OPENAI_API_KEY"):
+            return "openai"
+        if os.getenv("DEEPSEEK_API_KEY"):
+            return "deepseek"
+        if os.getenv("DASHSCOPE_API_KEY"):
+            return "qwen"
+        if os.getenv("MODELSCOPE_API_KEY"):
+            return "modelscope"
+        if os.getenv("KIMI_API_KEY") or os.getenv("MOONSHOT_API_KEY"):
+            return "kimi"
+        if os.getenv("ZHIPU_API_KEY") or os.getenv("GLM_API_KEY"):
+            return "zhipu"
+        if os.getenv("OLLAMA_API_KEY") or os.getenv("OLLAMA_HOST"):
+            return "ollama"
+        if os.getenv("VLLM_API_KEY") or os.getenv("VLLM_HOST"):
+            return "vllm"
+
+        # 2. 根据API密钥格式判断
+        actual_api_key = api_key or os.getenv("LLM_API_KEY")
+        if actual_api_key:
+            actual_key_lower = actual_api_key.lower()
+            if actual_api_key.startswith("ms-"):
+                return "modelscope"
+            elif actual_key_lower == "ollama":
+                return "ollama"
+            elif actual_key_lower == "vllm":
+                return "vllm"
+            elif actual_key_lower == "local":
+                return "local"
+            elif actual_api_key.startswith("sk-") and len(actual_api_key) > 50:
+                # 可能是OpenAI、DeepSeek或Kimi,需要进一步判断
+                pass
+            elif actual_api_key.endswith(".") or "." in actual_api_key[-20:]:
+                # 智谱AI的API密钥格式通常包含点号
+                return "zhipu"
+
+        # 3. 根据base_url判断
+        actual_base_url = base_url or os.getenv("LLM_BASE_URL")
+        if actual_base_url:
+            base_url_lower = actual_base_url.lower()
+            if "api.openai.com" in base_url_lower:
+                return "openai"
+            elif "api.deepseek.com" in base_url_lower:
+                return "deepseek"
+            elif "dashscope.aliyuncs.com" in base_url_lower:
+                return "qwen"
+            elif "api-inference.modelscope.cn" in base_url_lower:
+                return "modelscope"
+            elif "api.moonshot.cn" in base_url_lower:
+                return "kimi"
+            elif "open.bigmodel.cn" in base_url_lower:
+                return "zhipu"
+            elif "localhost" in base_url_lower or "127.0.0.1" in base_url_lower:
+                # 本地部署检测 - 优先检查特定服务
+                if ":11434" in base_url_lower or "ollama" in base_url_lower:
+                    return "ollama"
+                elif ":8000" in base_url_lower and "vllm" in base_url_lower:
+                    return "vllm"
+                elif ":8080" in base_url_lower or ":7860" in base_url_lower:
+                    return "local"
+                else:
+                    # 根据API密钥进一步判断
+                    if actual_api_key and actual_api_key.lower() == "ollama":
+                        return "ollama"
+                    elif actual_api_key and actual_api_key.lower() == "vllm":
+                        return "vllm"
+                    else:
+                        return "local"
+            elif any(port in base_url_lower for port in [":8080", ":7860", ":5000"]):
+                # 常见的本地部署端口
+                return "local"
+
+        # 4. 默认返回auto,使用通用配置
+        return "auto"
+
+    def _resolve_credentials(self, api_key: Optional[str], base_url: Optional[str]) -> tuple[str, str]:
+        """根据provider解析API密钥和base_url"""
+        if self.provider == "openai":
+            resolved_api_key = api_key or os.getenv("OPENAI_API_KEY") or os.getenv("LLM_API_KEY")
+            resolved_base_url = base_url or os.getenv("LLM_BASE_URL") or "https://api.openai.com/v1"
+            return resolved_api_key, resolved_base_url
+
+        elif self.provider == "deepseek":
+            resolved_api_key = api_key or os.getenv("DEEPSEEK_API_KEY") or os.getenv("LLM_API_KEY")
+            resolved_base_url = base_url or os.getenv("LLM_BASE_URL") or "https://api.deepseek.com"
+            return resolved_api_key, resolved_base_url
+
+        elif self.provider == "qwen":
+            resolved_api_key = api_key or os.getenv("DASHSCOPE_API_KEY") or os.getenv("LLM_API_KEY")
+            resolved_base_url = base_url or os.getenv("LLM_BASE_URL") or "https://dashscope.aliyuncs.com/compatible-mode/v1"
+            return resolved_api_key, resolved_base_url
+
+        elif self.provider == "modelscope":
+            resolved_api_key = api_key or os.getenv("MODELSCOPE_API_KEY") or os.getenv("LLM_API_KEY")
+            resolved_base_url = base_url or os.getenv("LLM_BASE_URL") or "https://api-inference.modelscope.cn/v1/"
+            return resolved_api_key, resolved_base_url
+
+        elif self.provider == "kimi":
+            resolved_api_key = api_key or os.getenv("KIMI_API_KEY") or os.getenv("MOONSHOT_API_KEY") or os.getenv("LLM_API_KEY")
+            resolved_base_url = base_url or os.getenv("LLM_BASE_URL") or "https://api.moonshot.cn/v1"
+            return resolved_api_key, resolved_base_url
+
+        elif self.provider == "zhipu":
+            resolved_api_key = api_key or os.getenv("ZHIPU_API_KEY") or os.getenv("GLM_API_KEY") or os.getenv("LLM_API_KEY")
+            resolved_base_url = base_url or os.getenv("LLM_BASE_URL") or "https://open.bigmodel.cn/api/paas/v4"
+            return resolved_api_key, resolved_base_url
+
+        elif self.provider == "ollama":
+            resolved_api_key = api_key or os.getenv("OLLAMA_API_KEY") or os.getenv("LLM_API_KEY") or "ollama"
+            resolved_base_url = base_url or os.getenv("OLLAMA_HOST") or os.getenv("LLM_BASE_URL") or "http://localhost:11434/v1"
+            return resolved_api_key, resolved_base_url
+
+        elif self.provider == "vllm":
+            resolved_api_key = api_key or os.getenv("VLLM_API_KEY") or os.getenv("LLM_API_KEY") or "vllm"
+            resolved_base_url = base_url or os.getenv("VLLM_HOST") or os.getenv("LLM_BASE_URL") or "http://localhost:8000/v1"
+            return resolved_api_key, resolved_base_url
+
+        elif self.provider == "local":
+            resolved_api_key = api_key or os.getenv("LLM_API_KEY") or "local"
+            resolved_base_url = base_url or os.getenv("LLM_BASE_URL") or "http://localhost:8000/v1"
+            return resolved_api_key, resolved_base_url
+
+        else:
+            # auto或其他情况:使用通用配置,支持任何OpenAI兼容的服务
+            resolved_api_key = api_key or os.getenv("LLM_API_KEY")
+            resolved_base_url = base_url or os.getenv("LLM_BASE_URL")
+            return resolved_api_key, resolved_base_url
+
+    def _create_client(self) -> OpenAI:
+        """创建OpenAI客户端"""
+        return OpenAI(
+            api_key=self.api_key,
+            base_url=self.base_url,
+            timeout=self.timeout
+        )
+    
+    def _get_default_model(self) -> str:
+        """获取默认模型"""
+        if self.provider == "openai":
+            return "gpt-3.5-turbo"
+        elif self.provider == "deepseek":
+            return "deepseek-chat"
+        elif self.provider == "qwen":
+            return "qwen-plus"
+        elif self.provider == "modelscope":
+            return "Qwen/Qwen2.5-72B-Instruct"
+        elif self.provider == "kimi":
+            return "moonshot-v1-8k"
+        elif self.provider == "zhipu":
+            return "glm-4"
+        elif self.provider == "ollama":
+            return "llama3.2"  # Ollama常用模型
+        elif self.provider == "vllm":
+            return "meta-llama/Llama-2-7b-chat-hf"  # vLLM常用模型
+        elif self.provider == "local":
+            return "local-model"  # 本地模型占位符
+        else:
+            # auto或其他情况:根据base_url智能推断默认模型
+            base_url = os.getenv("LLM_BASE_URL", "")
+            base_url_lower = base_url.lower()
+            if "modelscope" in base_url_lower:
+                return "Qwen/Qwen2.5-72B-Instruct"
+            elif "deepseek" in base_url_lower:
+                return "deepseek-chat"
+            elif "dashscope" in base_url_lower:
+                return "qwen-plus"
+            elif "moonshot" in base_url_lower:
+                return "moonshot-v1-8k"
+            elif "bigmodel" in base_url_lower:
+                return "glm-4"
+            elif "ollama" in base_url_lower or ":11434" in base_url_lower:
+                return "llama3.2"
+            elif ":8000" in base_url_lower or "vllm" in base_url_lower:
+                return "meta-llama/Llama-2-7b-chat-hf"
+            elif "localhost" in base_url_lower or "127.0.0.1" in base_url_lower:
+                return "local-model"
+            else:
+                return "gpt-3.5-turbo"
+
+    def think(self, messages: list[dict[str, str]], temperature: Optional[float] = None) -> Iterator[str]:
+        """
+        调用大语言模型进行思考,并返回流式响应。
+        这是主要的调用方法,默认使用流式响应以获得更好的用户体验。
+
+        Args:
+            messages: 消息列表
+            temperature: 温度参数,如果未提供则使用初始化时的值
+
+        Yields:
+            str: 流式响应的文本片段
+        """
+        print(f"🧠 正在调用 {self.model} 模型...")
+        try:
+            response = self._client.chat.completions.create(
+                model=self.model,
+                messages=messages,
+                temperature=temperature if temperature is not None else self.temperature,
+                max_tokens=self.max_tokens,
+                stream=True,
+            )
+
+            # 处理流式响应
+            print("✅ 大语言模型响应成功:")
+            for chunk in response:
+                content = chunk.choices[0].delta.content or ""
+                if content:
+                    print(content, end="", flush=True)
+                    yield content
+            print()  # 在流式输出结束后换行
+
+        except Exception as e:
+            print(f"❌ 调用LLM API时发生错误: {e}")
+            raise HelloAgentsException(f"LLM调用失败: {str(e)}")
+
+    def invoke(self, messages: list[dict[str, str]], **kwargs) -> str:
+        """
+        非流式调用LLM,返回完整响应。
+        适用于不需要流式输出的场景。
+        """
+        try:
+            response = self._client.chat.completions.create(
+                model=self.model,
+                messages=messages,
+                temperature=kwargs.get('temperature', self.temperature),
+                max_tokens=kwargs.get('max_tokens', self.max_tokens),
+                **{k: v for k, v in kwargs.items() if k not in ['temperature', 'max_tokens']}
+            )
+            return response.choices[0].message.content
+        except Exception as e:
+            raise HelloAgentsException(f"LLM调用失败: {str(e)}")
+
+    def stream_invoke(self, messages: list[dict[str, str]], **kwargs) -> Iterator[str]:
+        """
+        流式调用LLM的别名方法,与think方法功能相同。
+        保持向后兼容性。
+        """
+        temperature = kwargs.get('temperature')
+        yield from self.think(messages, temperature)

+ 33 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/core/message.py

@@ -0,0 +1,33 @@
+"""消息系统"""
+
+from typing import Optional, Dict, Any, Literal
+from datetime import datetime
+from pydantic import BaseModel
+
+MessageRole = Literal["user", "assistant", "system", "tool"]
+
+class Message(BaseModel):
+    """消息类"""
+    
+    content: str
+    role: MessageRole
+    timestamp: datetime = None
+    metadata: Optional[Dict[str, Any]] = None
+    
+    def __init__(self, content: str, role: MessageRole, **kwargs):
+        super().__init__(
+            content=content,
+            role=role,
+            timestamp=kwargs.get('timestamp', datetime.now()),
+            metadata=kwargs.get('metadata', {})
+        )
+    
+    def to_dict(self) -> Dict[str, Any]:
+        """转换为字典格式(OpenAI API格式)"""
+        return {
+            "role": self.role,
+            "content": self.content
+        }
+    
+    def __str__(self) -> str:
+        return f"[{self.role}] {self.content}"

+ 19 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/__init__.py

@@ -0,0 +1,19 @@
+"""HelloAgents 记忆系统模块(轻量导出)
+
+注意:不要在此处 eager-import 所有记忆类型实现(semantic/perceptual 可能依赖额外第三方库)。
+需要某个记忆类型时,由 MemoryManager 在运行时按 enable_* 选项惰性加载。
+"""
+
+from .base import MemoryItem, MemoryConfig, BaseMemory
+from .manager import MemoryManager
+from .storage.document_store import DocumentStore, SQLiteDocumentStore
+
+__all__ = [
+    "MemoryManager",
+    "MemoryItem",
+    "MemoryConfig",
+    "BaseMemory",
+    "DocumentStore",
+    "SQLiteDocumentStore",
+]
+

+ 174 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/base.py

@@ -0,0 +1,174 @@
+"""记忆系统基础类和配置
+
+按照第8章架构设计的基础组件:
+- MemoryItem: 记忆项数据结构
+- MemoryConfig: 记忆系统配置
+- BaseMemory: 记忆基类
+"""
+
+from abc import ABC, abstractmethod
+from typing import List, Dict, Any
+from datetime import datetime
+from pydantic import BaseModel
+
+class MemoryItem(BaseModel):
+    """记忆项数据结构"""
+    id: str
+    content: str
+    memory_type: str
+    user_id: str
+    timestamp: datetime
+    importance: float = 0.5
+    metadata: Dict[str, Any] = {}
+
+    class Config:
+        arbitrary_types_allowed = True
+
+class MemoryConfig(BaseModel):
+    """记忆系统配置"""
+    
+    # 存储路径
+    storage_path: str = "./memory_data"
+
+    # 向量/嵌入相关(可选关闭,适用于只用 SQLite 的轻量 episodic)
+    disable_vector_store: bool = False
+    disable_embeddings: bool = False
+    
+    # 统计显示用的基础配置(仅用于展示)
+    max_capacity: int = 100
+    importance_threshold: float = 0.1
+    decay_factor: float = 0.95
+
+    # 工作记忆特定配置
+    working_memory_capacity: int = 10
+    working_memory_tokens: int = 2000
+    working_memory_ttl_minutes: int = 120
+
+    # 感知记忆特定配置
+    perceptual_memory_modalities: List[str] = ["text", "image", "audio", "video"]
+
+class BaseMemory(ABC):
+    """记忆基类
+
+    定义所有记忆类型的通用接口和行为
+    """
+
+    def __init__(self, config: MemoryConfig, storage_backend=None):
+        self.config = config
+        self.storage = storage_backend
+        self.memory_type = self.__class__.__name__.lower().replace("memory", "")
+
+    @abstractmethod
+    def add(self, memory_item: MemoryItem) -> str:
+        """添加记忆项
+
+        Args:
+            memory_item: 记忆项对象
+
+        Returns:
+            记忆ID
+        """
+        pass
+
+    @abstractmethod
+    def retrieve(self, query: str, limit: int = 5, **kwargs) -> List[MemoryItem]:
+        """检索相关记忆
+
+        Args:
+            query: 查询内容
+            limit: 返回数量限制
+            **kwargs: 其他检索参数
+
+        Returns:
+            相关记忆列表
+        """
+        pass
+
+    @abstractmethod
+    def update(self, memory_id: str, content: str = None,
+               importance: float = None, metadata: Dict[str, Any] = None) -> bool:
+        """更新记忆
+
+        Args:
+            memory_id: 记忆ID
+            content: 新内容
+            importance: 新重要性
+            metadata: 新元数据
+
+        Returns:
+            是否更新成功
+        """
+        pass
+
+    @abstractmethod
+    def remove(self, memory_id: str) -> bool:
+        """删除记忆
+
+        Args:
+            memory_id: 记忆ID
+
+        Returns:
+            是否删除成功
+        """
+        pass
+
+    @abstractmethod
+    def has_memory(self, memory_id: str) -> bool:
+        """检查记忆是否存在
+
+        Args:
+            memory_id: 记忆ID
+
+        Returns:
+            是否存在
+        """
+        pass
+
+    @abstractmethod
+    def clear(self):
+        """清空所有记忆"""
+        pass
+
+    @abstractmethod
+    def get_stats(self) -> Dict[str, Any]:
+        """获取记忆统计信息
+
+        Returns:
+            统计信息字典
+        """
+        pass
+
+    def _generate_id(self) -> str:
+        """生成记忆ID"""
+        import uuid
+        return str(uuid.uuid4())
+
+    def _calculate_importance(self, content: str, base_importance: float = 0.5) -> float:
+        """计算记忆重要性
+
+        Args:
+            content: 记忆内容
+            base_importance: 基础重要性
+
+        Returns:
+            计算后的重要性分数
+        """
+        importance = base_importance
+
+        # 基于内容长度
+        if len(content) > 100:
+            importance += 0.1
+
+        # 基于关键词
+        important_keywords = ["重要", "关键", "必须", "注意", "警告", "错误"]
+        if any(keyword in content for keyword in important_keywords):
+            importance += 0.2
+
+        return max(0.0, min(1.0, importance))
+
+    def __str__(self) -> str:
+        stats = self.get_stats()
+        return f"{self.__class__.__name__}(count={stats.get('count', 0)})"
+
+    def __repr__(self) -> str:
+        return self.__str__()

+ 315 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/embedding.py

@@ -0,0 +1,315 @@
+"""统一嵌入模块(实现 + 提供器)
+
+说明(中文):
+- 提供统一的文本嵌入接口与多实现:本地Transformer、DashScope(通义千问)、TF-IDF兜底。
+- 暴露 get_text_embedder()/get_dimension()/refresh_embedder() 供各记忆类型统一使用。
+- 通过环境变量优先级:dashscope > local > tfidf。
+
+环境变量:
+- EMBED_MODEL_TYPE: "dashscope" | "local" | "tfidf"(默认 dashscope)
+- EMBED_MODEL_NAME: 模型名称(dashscope默认 text-embedding-v3;local默认 sentence-transformers/all-MiniLM-L6-v2)
+- EMBED_API_KEY: Embedding API Key(统一命名)
+- EMBED_BASE_URL: Embedding Base URL(统一命名,可选)
+"""
+
+from typing import List, Union, Optional
+import threading
+import os
+import numpy as np
+
+
+# ==============
+# 抽象与实现
+# ==============
+
+class EmbeddingModel:
+    """嵌入模型基类(最小接口)"""
+
+    def encode(self, texts: Union[str, List[str]]):
+        raise NotImplementedError
+
+    @property
+    def dimension(self) -> int:
+        raise NotImplementedError
+
+
+class LocalTransformerEmbedding(EmbeddingModel):
+    """本地Transformer嵌入(优先 sentence-transformers,缺失回退 transformers+torch)"""
+
+    def __init__(self, model_name: str = "sentence-transformers/all-MiniLM-L6-v2"):
+        self.model_name = model_name
+        self._backend = None  # "st" 或 "hf"
+        self._st_model = None
+        self._hf_tokenizer = None
+        self._hf_model = None
+        self._dimension = None
+        self._load_backend()
+
+    def _load_backend(self):
+        # 优先 sentence-transformers
+        try:
+            from sentence_transformers import SentenceTransformer
+            self._st_model = SentenceTransformer(self.model_name)
+            test_vec = self._st_model.encode("test_text")
+            self._dimension = len(test_vec)
+            self._backend = "st"
+            return
+        except Exception:
+            self._st_model = None
+
+        # 回退 transformers
+        try:
+            from transformers import AutoTokenizer, AutoModel
+            import torch
+            self._hf_tokenizer = AutoTokenizer.from_pretrained(self.model_name)
+            self._hf_model = AutoModel.from_pretrained(self.model_name)
+            with torch.no_grad():
+                inputs = self._hf_tokenizer("test_text", return_tensors="pt", padding=True, truncation=True)
+                outputs = self._hf_model(**inputs)
+                test_embedding = outputs.last_hidden_state.mean(dim=1)
+                self._dimension = int(test_embedding.shape[1])
+            self._backend = "hf"
+            return
+        except Exception:
+            self._hf_tokenizer = None
+            self._hf_model = None
+
+        raise ImportError("未找到可用的本地嵌入后端,请安装 sentence-transformers 或 transformers+torch")
+
+    def encode(self, texts: Union[str, List[str]]):
+        if isinstance(texts, str):
+            inputs = [texts]
+            single = True
+        else:
+            inputs = list(texts)
+            single = False
+
+        if self._backend == "st":
+            vecs = self._st_model.encode(inputs)
+            if hasattr(vecs, "tolist"):
+                vecs = [v for v in vecs]
+        else:
+            import torch
+            tokenized = self._hf_tokenizer(inputs, return_tensors="pt", padding=True, truncation=True, max_length=512)
+            with torch.no_grad():
+                outputs = self._hf_model(**tokenized)
+                embeddings = outputs.last_hidden_state.mean(dim=1).cpu().numpy()
+            vecs = [v for v in embeddings]
+
+        if single:
+            return vecs[0]
+        return vecs
+
+    @property
+    def dimension(self) -> int:
+        return int(self._dimension or 0)
+
+
+class TFIDFEmbedding(EmbeddingModel):
+    """TF-IDF 简易兜底(在无深度模型时保证可用)"""
+
+    def __init__(self, max_features: int = 1000):
+        self.max_features = max_features
+        self._vectorizer = None
+        self._is_fitted = False
+        self._dimension = max_features
+        self._init_vectorizer()
+
+    def _init_vectorizer(self):
+        try:
+            from sklearn.feature_extraction.text import TfidfVectorizer
+            self._vectorizer = TfidfVectorizer(max_features=self.max_features, stop_words='english')
+        except ImportError:
+            raise ImportError("请安装 scikit-learn: pip install scikit-learn")
+
+    def fit(self, texts: List[str]):
+        self._vectorizer.fit(texts)
+        self._is_fitted = True
+        self._dimension = len(self._vectorizer.get_feature_names_out())
+
+    def encode(self, texts: Union[str, List[str]]):
+        if not self._is_fitted:
+            raise ValueError("TF-IDF模型未训练,请先调用fit()方法")
+        if isinstance(texts, str):
+            texts = [texts]
+            single = True
+        else:
+            single = False
+        tfidf_matrix = self._vectorizer.transform(texts)
+        embeddings = tfidf_matrix.toarray()
+        if single:
+            return embeddings[0]
+        return [e for e in embeddings]
+
+    @property
+    def dimension(self) -> int:
+        return self._dimension
+
+
+class DashScopeEmbedding(EmbeddingModel):
+    """阿里云 DashScope(通义千问)Embedding / OpenAI兼容REST 模式
+
+    行为:
+    - 如提供 base_url,则优先使用 OpenAI 兼容的 REST 接口(POST {base_url}/embeddings)。
+    - 否则使用官方 dashscope SDK 的 TextEmbedding.call。
+    """
+
+    def __init__(self, model_name: str = "text-embedding-v3", api_key: Optional[str] = None, base_url: Optional[str] = None):
+        self.model_name = model_name
+        self.api_key = api_key
+        self.base_url = base_url
+        self._dimension = None
+        # 仅在非REST情况下初始化SDK
+        if not self.base_url:
+            self._init_client()
+        # 探测维度
+        test = self.encode("health_check")
+        self._dimension = len(test)
+
+    def _init_client(self):
+        try:
+            if self.api_key:
+                # 将统一命名的 API Key 注入到 SDK 期望的位置
+                os.environ["DASHSCOPE_API_KEY"] = self.api_key
+            import dashscope  # noqa: F401
+        except ImportError:
+            raise ImportError("请安装 dashscope: pip install dashscope")
+
+    def encode(self, texts: Union[str, List[str]]):
+        if isinstance(texts, str):
+            inputs = [texts]
+            single = True
+        else:
+            inputs = list(texts)
+            single = False
+
+        # REST 模式(OpenAI兼容)
+        if self.base_url:
+            import requests
+            url = self.base_url.rstrip("/") + "/embeddings"
+            headers = {
+                "Authorization": f"Bearer {self.api_key}" if self.api_key else "",
+                "Content-Type": "application/json",
+            }
+            payload = {"model": self.model_name, "input": inputs}
+            resp = requests.post(url, headers=headers, json=payload, timeout=30)
+            if resp.status_code >= 400:
+                raise RuntimeError(f"Embedding REST 调用失败: {resp.status_code} {resp.text}")
+            data = resp.json()
+            # 期望结构:{"data": [{"embedding": [...]}]}
+            items = data.get("data") or []
+            vecs = [np.array(item.get("embedding")) for item in items]
+            if single:
+                return vecs[0]
+            return vecs
+
+        # SDK 模式
+        from dashscope import TextEmbedding
+        rsp = TextEmbedding.call(model=self.model_name, input=inputs)
+        embeddings_obj = None
+        if isinstance(rsp, dict):
+            embeddings_obj = (rsp.get("output") or {}).get("embeddings")
+        else:
+            embeddings_obj = getattr(getattr(rsp, "output", None), "embeddings", None)
+        if not embeddings_obj:
+            raise RuntimeError("DashScope 返回为空或格式不匹配")
+        vecs = [np.array(item.get("embedding") or item.get("vector")) for item in embeddings_obj]
+        if single:
+            return vecs[0]
+        return vecs
+
+    @property
+    def dimension(self) -> int:
+        return int(self._dimension or 0)
+
+
+# ==============
+# 工厂与回退
+# ==============
+
+def create_embedding_model(model_type: str = "local", **kwargs) -> EmbeddingModel:
+    """创建嵌入模型实例
+
+    model_type: "dashscope" | "local" | "tfidf"
+    kwargs: model_name, api_key
+    """
+    if model_type in ("local", "sentence_transformer", "huggingface"):
+        return LocalTransformerEmbedding(**kwargs)
+    elif model_type == "dashscope":
+        return DashScopeEmbedding(**kwargs)
+    elif model_type == "tfidf":
+        return TFIDFEmbedding(**kwargs)
+    else:
+        raise ValueError(f"不支持的模型类型: {model_type}")
+
+
+def create_embedding_model_with_fallback(preferred_type: str = "dashscope", **kwargs) -> EmbeddingModel:
+    """带回退的创建:dashscope -> local -> tfidf"""
+    if preferred_type in ("sentence_transformer", "huggingface"):
+        preferred_type = "local"
+    fallback = ["dashscope", "local", "tfidf"]
+    # 将首选放最前
+    if preferred_type in fallback:
+        fallback.remove(preferred_type)
+        fallback.insert(0, preferred_type)
+    for t in fallback:
+        try:
+            return create_embedding_model(t, **kwargs)
+        except Exception:
+            continue
+    raise RuntimeError("所有嵌入模型都不可用,请安装依赖或检查配置")
+
+
+# ==================
+# Provider(单例)
+# ==================
+
+_lock = threading.RLock()
+_embedder: Optional[EmbeddingModel] = None
+
+
+def _build_embedder() -> EmbeddingModel:
+    preferred = os.getenv("EMBED_MODEL_TYPE", "dashscope").strip()
+    # 根据提供商选择默认模型
+    default_model = "text-embedding-v3" if preferred == "dashscope" else "sentence-transformers/all-MiniLM-L6-v2"
+    model_name = os.getenv("EMBED_MODEL_NAME", default_model).strip()
+    kwargs = {}
+    if model_name:
+        kwargs["model_name"] = model_name
+    # 仅使用统一命名
+    api_key = os.getenv("EMBED_API_KEY")
+    if api_key:
+        kwargs["api_key"] = api_key
+    base_url = os.getenv("EMBED_BASE_URL")
+    if base_url:
+        kwargs["base_url"] = base_url
+    return create_embedding_model_with_fallback(preferred_type=preferred, **kwargs)
+
+
+def get_text_embedder() -> EmbeddingModel:
+    """获取全局共享的文本嵌入实例(线程安全单例)"""
+    global _embedder
+    if _embedder is not None:
+        return _embedder
+    with _lock:
+        if _embedder is None:
+            _embedder = _build_embedder()
+        return _embedder
+
+
+def get_dimension(default: int = 384) -> int:
+    """获取统一向量维度(失败回退默认值)"""
+    try:
+        return int(getattr(get_text_embedder(), "dimension", default))
+    except Exception:
+        return int(default)
+
+
+def refresh_embedder() -> EmbeddingModel:
+    """强制重建嵌入实例(可用于动态切换环境变量)"""
+    global _embedder
+    with _lock:
+        _embedder = _build_embedder()
+        return _embedder
+
+

+ 342 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/manager.py

@@ -0,0 +1,342 @@
+"""记忆管理器 - 记忆核心层的统一管理接口"""
+
+from typing import List, Dict, Any, Optional, Union
+from datetime import datetime
+import uuid
+import logging
+
+from .base import MemoryItem, MemoryConfig
+# 存储和检索功能已被各记忆类型内部实现替代
+
+logger = logging.getLogger(__name__)
+
+class MemoryManager:
+    """记忆管理器 - 统一的记忆操作接口
+    
+    负责:
+    - 记忆生命周期管理
+    - 记忆优先级和重要性评估
+    - 记忆遗忘和清理机制
+    - 多类型记忆的协调管理
+    """
+    
+    def __init__(
+        self,
+        config: Optional[MemoryConfig] = None,
+        user_id: str = "default_user",
+        enable_working: bool = True,
+        enable_episodic: bool = True,
+        enable_semantic: bool = True,
+        enable_perceptual: bool = False
+    ):
+        self.config = config or MemoryConfig()
+        self.user_id = user_id
+        
+        # 存储和检索功能已移至各记忆类型内部实现
+        
+        # 初始化各类型记忆
+        self.memory_types = {}
+        
+        if enable_working:
+            from .types.working import WorkingMemory
+            self.memory_types['working'] = WorkingMemory(self.config)
+        
+        if enable_episodic:
+            from .types.episodic import EpisodicMemory
+            self.memory_types['episodic'] = EpisodicMemory(self.config)
+            
+        if enable_semantic:
+            from .types.semantic import SemanticMemory
+            self.memory_types['semantic'] = SemanticMemory(self.config)
+            
+        if enable_perceptual:
+            from .types.perceptual import PerceptualMemory
+            self.memory_types['perceptual'] = PerceptualMemory(self.config)
+        
+        logger.info(f"MemoryManager初始化完成,启用记忆类型: {list(self.memory_types.keys())}")
+    
+    def add_memory(
+        self,
+        content: str,
+        memory_type: str = "working",
+        importance: Optional[float] = None,
+        metadata: Optional[Dict[str, Any]] = None,
+        auto_classify: bool = True
+    ) -> str:
+        """添加记忆
+        
+        Args:
+            content: 记忆内容
+            memory_type: 记忆类型
+            importance: 重要性分数 (0-1)
+            metadata: 元数据
+            auto_classify: 是否自动分类到合适的记忆类型
+            
+        Returns:
+            记忆ID
+        """
+        # 自动分类记忆类型
+        if auto_classify:
+            memory_type = self._classify_memory_type(content, metadata)
+        
+        # 计算重要性
+        if importance is None:
+            importance = self._calculate_importance(content, metadata)
+        
+        # 创建记忆项
+        memory_item = MemoryItem(
+            id=str(uuid.uuid4()),
+            content=content,
+            memory_type=memory_type,
+            user_id=self.user_id,
+            timestamp=datetime.now(),
+            importance=importance,
+            metadata=metadata or {}
+        )
+        
+        # 添加到对应的记忆类型
+        if memory_type in self.memory_types:
+            memory_id = self.memory_types[memory_type].add(memory_item)
+            logger.debug(f"添加记忆到 {memory_type}: {memory_id}")
+            return memory_id
+        else:
+            raise ValueError(f"不支持的记忆类型: {memory_type}")
+    
+    def retrieve_memories(
+        self,
+        query: str,
+        memory_types: Optional[List[str]] = None,
+        limit: int = 10,
+        min_importance: float = 0.0,
+        time_range: Optional[tuple] = None
+    ) -> List[MemoryItem]:
+        """检索记忆
+        
+        Args:
+            query: 查询内容
+            memory_types: 要检索的记忆类型列表
+            limit: 返回数量限制
+            min_importance: 最小重要性阈值
+            time_range: 时间范围 (start_time, end_time)
+            
+        Returns:
+            检索到的记忆列表
+        """
+        if memory_types is None:
+            memory_types = list(self.memory_types.keys())
+        
+        # 从各个记忆类型中检索
+        all_results = []
+        per_type_limit = max(1, limit // len(memory_types))
+
+        for memory_type in memory_types:
+            if memory_type in self.memory_types:
+                memory_instance = self.memory_types[memory_type]
+                try:
+                    # 使用各个记忆类型自己的检索方法
+                    type_results = memory_instance.retrieve(
+                        query=query,
+                        limit=per_type_limit,
+                        min_importance=min_importance,
+                        user_id=self.user_id
+                    )
+                    all_results.extend(type_results)
+                except Exception as e:
+                    logger.warning(f"检索 {memory_type} 记忆时出错: {e}")
+                    continue
+
+        # 按重要性和相关性排序
+        all_results.sort(key=lambda x: x.importance, reverse=True)
+        return all_results[:limit]
+    
+    def update_memory(
+        self,
+        memory_id: str,
+        content: Optional[str] = None,
+        importance: Optional[float] = None,
+        metadata: Optional[Dict[str, Any]] = None
+    ) -> bool:
+        """更新记忆
+        
+        Args:
+            memory_id: 记忆ID
+            content: 新内容
+            importance: 新重要性
+            metadata: 新元数据
+            
+        Returns:
+            是否更新成功
+        """
+        # 查找记忆所在的类型
+        for memory_type, memory_instance in self.memory_types.items():
+            if memory_instance.has_memory(memory_id):
+                return memory_instance.update(memory_id, content, importance, metadata)
+        
+        logger.warning(f"未找到记忆: {memory_id}")
+        return False
+    
+    def remove_memory(self, memory_id: str) -> bool:
+        """删除记忆
+        
+        Args:
+            memory_id: 记忆ID
+            
+        Returns:
+            是否删除成功
+        """
+        for memory_type, memory_instance in self.memory_types.items():
+            if memory_instance.has_memory(memory_id):
+                return memory_instance.remove(memory_id)
+        
+        logger.warning(f"未找到记忆: {memory_id}")
+        return False
+    
+    def forget_memories(
+        self,
+        strategy: str = "importance_based",
+        threshold: float = 0.1,
+        max_age_days: int = 30
+    ) -> int:
+        """记忆遗忘机制
+        
+        Args:
+            strategy: 遗忘策略 ("importance_based", "time_based", "capacity_based")
+            threshold: 遗忘阈值
+            max_age_days: 最大保存天数
+            
+        Returns:
+            遗忘的记忆数量
+        """
+        total_forgotten = 0
+        
+        for memory_type, memory_instance in self.memory_types.items():
+            if hasattr(memory_instance, 'forget'):
+                forgotten = memory_instance.forget(strategy, threshold, max_age_days)
+                total_forgotten += forgotten
+
+        logger.info(f"记忆遗忘完成: {total_forgotten} 条记忆")
+        return total_forgotten
+
+    def consolidate_memories(
+        self,
+        from_type: str = "working",
+        to_type: str = "episodic",
+        importance_threshold: float = 0.7
+    ) -> int:
+        """记忆整合 - 将重要的短期记忆转换为长期记忆
+
+        Args:
+            from_type: 源记忆类型
+            to_type: 目标记忆类型
+            importance_threshold: 重要性阈值
+
+        Returns:
+            整合的记忆数量
+        """
+        if from_type not in self.memory_types or to_type not in self.memory_types:
+            logger.warning(f"记忆类型不存在: {from_type} -> {to_type}")
+            return 0
+
+        # 获取高重要性的源记忆
+        source_memory = self.memory_types[from_type]
+        target_memory = self.memory_types[to_type]
+
+        # 获取需要整合的记忆
+        all_memories = source_memory.get_all()
+        candidates = [
+            m for m in all_memories
+            if m.importance >= importance_threshold
+        ]
+
+        consolidated_count = 0
+        for memory in candidates:
+            # 移动到目标记忆类型
+            if source_memory.remove(memory.id):
+                memory.memory_type = to_type
+                memory.importance *= 1.1  # 提升重要性
+                target_memory.add(memory)
+                consolidated_count += 1
+
+        logger.info(f"记忆整合完成: {consolidated_count} 条记忆从 {from_type} 转移到 {to_type}")
+        return consolidated_count
+
+    def get_memory_stats(self) -> Dict[str, Any]:
+        """获取记忆统计信息"""
+        stats = {
+            "user_id": self.user_id,
+            "enabled_types": list(self.memory_types.keys()),
+            "total_memories": 0,
+            "memories_by_type": {},
+            "config": {
+                "max_capacity": self.config.max_capacity,
+                "importance_threshold": self.config.importance_threshold,
+                "decay_factor": self.config.decay_factor
+            }
+        }
+
+        for memory_type, memory_instance in self.memory_types.items():
+            type_stats = memory_instance.get_stats()
+            stats["memories_by_type"][memory_type] = type_stats
+            # 使用count字段(活跃记忆数),而不是total_count(包含已遗忘的)
+            stats["total_memories"] += type_stats.get("count", 0)
+
+        return stats
+
+    def clear_all_memories(self):
+        """清空所有记忆"""
+        for memory_type, memory_instance in self.memory_types.items():
+            memory_instance.clear()
+        logger.info("所有记忆已清空")
+
+
+
+
+    def _classify_memory_type(self, content: str, metadata: Optional[Dict[str, Any]]) -> str:
+        """自动分类记忆类型"""
+        if metadata and metadata.get("type"):
+            return metadata["type"]
+        
+        # 简单的分类逻辑,可以扩展为更复杂的分类器
+        if self._is_episodic_content(content):
+            return "episodic"
+        elif self._is_semantic_content(content):
+            return "semantic"
+        else:
+            return "working"
+    
+    def _is_episodic_content(self, content: str) -> bool:
+        """判断是否为情景记忆内容"""
+        episodic_keywords = ["昨天", "今天", "明天", "上次", "记得", "发生", "经历"]
+        return any(keyword in content for keyword in episodic_keywords)
+    
+    def _is_semantic_content(self, content: str) -> bool:
+        """判断是否为语义记忆内容"""
+        semantic_keywords = ["定义", "概念", "规则", "知识", "原理", "方法"]
+        return any(keyword in content for keyword in semantic_keywords)
+    
+    def _calculate_importance(self, content: str, metadata: Optional[Dict[str, Any]]) -> float:
+        """计算记忆重要性"""
+        importance = 0.5  # 基础重要性
+        
+        # 基于内容长度
+        if len(content) > 100:
+            importance += 0.1
+        
+        # 基于关键词
+        important_keywords = ["重要", "关键", "必须", "注意", "警告", "错误"]
+        if any(keyword in content for keyword in important_keywords):
+            importance += 0.2
+        
+        # 基于元数据
+        if metadata:
+            if metadata.get("priority") == "high":
+                importance += 0.3
+            elif metadata.get("priority") == "low":
+                importance -= 0.2
+        
+        return max(0.0, min(1.0, importance))
+    
+
+    def __str__(self) -> str:
+        stats = self.get_memory_stats()
+        return f"MemoryManager(user={self.user_id}, total={stats['total_memories']})"

+ 65 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/rag/__init__.py

@@ -0,0 +1,65 @@
+"""RAG (检索增强生成) 模块
+
+合并了 GraphRAG 能力:
+- loader:文件加载/分块(含PDF、语言标注、去重)
+- embedding/cache:嵌入与SQLite缓存,默认哈希回退
+- vector search:Qdrant召回
+- rank/merge:融合排序与片段合并
+"""
+
+# 说明:原先的 .embeddings 已合并到上级目录的 memory/embedding.py
+# 这里做兼容导出,避免历史引用报错。
+from ..embedding import (
+    EmbeddingModel,
+    LocalTransformerEmbedding,
+    TFIDFEmbedding,
+    create_embedding_model,
+    create_embedding_model_with_fallback,
+)
+from .document import Document, DocumentProcessor
+from .pipeline import (
+    load_and_chunk_texts,
+    build_graph_from_chunks,
+    index_chunks,
+    embed_query,
+    search_vectors,
+    rank,
+    merge_snippets,
+    rerank_with_cross_encoder,
+    expand_neighbors_from_pool,
+    compute_graph_signals_from_pool,
+    merge_snippets_grouped,
+    search_vectors_expanded,
+    compress_ranked_items,
+    tldr_summarize,
+)
+
+# 兼容旧类名(历史代码中可能从此处导入)
+SentenceTransformerEmbedding = LocalTransformerEmbedding
+HuggingFaceEmbedding = LocalTransformerEmbedding
+
+__all__ = [
+    "EmbeddingModel",
+    "LocalTransformerEmbedding",
+    "SentenceTransformerEmbedding",  # 兼容别名
+    "HuggingFaceEmbedding",          # 兼容别名
+    "TFIDFEmbedding",
+    "create_embedding_model",
+    "create_embedding_model_with_fallback",
+    "Document",
+    "DocumentProcessor",
+    "load_and_chunk_texts",
+    "build_graph_from_chunks",
+    "index_chunks",
+    "embed_query",
+    "search_vectors",
+    "rank",
+    "merge_snippets",
+    "rerank_with_cross_encoder",
+    "expand_neighbors_from_pool",
+    "compute_graph_signals_from_pool",
+    "merge_snippets_grouped",
+    "search_vectors_expanded",
+    "compress_ranked_items",
+    "tldr_summarize",
+]

+ 258 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/rag/document.py

@@ -0,0 +1,258 @@
+"""文档处理模块"""
+
+from typing import List, Dict, Any, Optional
+from dataclasses import dataclass
+from datetime import datetime
+import hashlib
+
+@dataclass
+class Document:
+    """文档类"""
+    content: str
+    metadata: Dict[str, Any]
+    doc_id: Optional[str] = None
+    
+    def __post_init__(self):
+        if self.doc_id is None:
+            # 基于内容生成ID
+            self.doc_id = hashlib.md5(self.content.encode()).hexdigest()
+
+@dataclass 
+class DocumentChunk:
+    """文档块类"""
+    content: str
+    metadata: Dict[str, Any]
+    chunk_id: Optional[str] = None
+    doc_id: Optional[str] = None
+    chunk_index: int = 0
+    
+    def __post_init__(self):
+        if self.chunk_id is None:
+            # 基于文档ID和块索引生成ID
+            chunk_content = f"{self.doc_id}_{self.chunk_index}_{self.content[:50]}"
+            self.chunk_id = hashlib.md5(chunk_content.encode()).hexdigest()
+
+class DocumentProcessor:
+    """文档处理器"""
+    
+    def __init__(
+        self,
+        chunk_size: int = 1000,
+        chunk_overlap: int = 200,
+        separators: Optional[List[str]] = None
+    ):
+        self.chunk_size = chunk_size
+        self.chunk_overlap = chunk_overlap
+        self.separators = separators or ["\n\n", "\n", "。", ".", " "]
+    
+    def process_document(self, document: Document) -> List[DocumentChunk]:
+        """
+        处理文档,分割成块
+        
+        Args:
+            document: 输入文档
+            
+        Returns:
+            文档块列表
+        """
+        chunks = self._split_text(document.content)
+        
+        document_chunks = []
+        for i, chunk_content in enumerate(chunks):
+            # 创建块的元数据
+            chunk_metadata = document.metadata.copy()
+            chunk_metadata.update({
+                "doc_id": document.doc_id,
+                "chunk_index": i,
+                "total_chunks": len(chunks),
+                "processed_at": datetime.now().isoformat()
+            })
+            
+            chunk = DocumentChunk(
+                content=chunk_content,
+                metadata=chunk_metadata,
+                doc_id=document.doc_id,
+                chunk_index=i
+            )
+            document_chunks.append(chunk)
+        
+        return document_chunks
+    
+    def process_documents(self, documents: List[Document]) -> List[DocumentChunk]:
+        """
+        批量处理文档
+        
+        Args:
+            documents: 文档列表
+            
+        Returns:
+            所有文档块列表
+        """
+        all_chunks = []
+        for document in documents:
+            chunks = self.process_document(document)
+            all_chunks.extend(chunks)
+        
+        return all_chunks
+    
+    def _split_text(self, text: str) -> List[str]:
+        """
+        分割文本为块
+        
+        Args:
+            text: 输入文本
+            
+        Returns:
+            文本块列表
+        """
+        if len(text) <= self.chunk_size:
+            return [text]
+        
+        chunks = []
+        start = 0
+        
+        while start < len(text):
+            # 确定块的结束位置
+            end = start + self.chunk_size
+            
+            if end >= len(text):
+                # 最后一块
+                chunks.append(text[start:])
+                break
+            
+            # 寻找合适的分割点
+            split_point = self._find_split_point(text, start, end)
+            
+            if split_point == -1:
+                # 没找到合适的分割点,强制分割
+                split_point = end
+            
+            chunks.append(text[start:split_point])
+            
+            # 计算下一块的开始位置(考虑重叠)
+            start = max(start + 1, split_point - self.chunk_overlap)
+        
+        return chunks
+    
+    def _find_split_point(self, text: str, start: int, end: int) -> int:
+        """
+        在指定范围内寻找最佳分割点
+        
+        Args:
+            text: 文本
+            start: 开始位置
+            end: 结束位置
+            
+        Returns:
+            分割点位置,-1表示未找到
+        """
+        # 从后往前寻找分隔符
+        for separator in self.separators:
+            # 在end附近寻找分隔符
+            search_start = max(start, end - 100)  # 在最后100个字符中寻找
+            
+            for i in range(end - len(separator), search_start - 1, -1):
+                if text[i:i + len(separator)] == separator:
+                    return i + len(separator)
+        
+        return -1
+    
+    def merge_chunks(self, chunks: List[DocumentChunk], max_length: int = 2000) -> List[DocumentChunk]:
+        """
+        合并小的文档块
+        
+        Args:
+            chunks: 文档块列表
+            max_length: 合并后的最大长度
+            
+        Returns:
+            合并后的文档块列表
+        """
+        if not chunks:
+            return []
+        
+        merged_chunks = []
+        current_chunk = chunks[0]
+        
+        for next_chunk in chunks[1:]:
+            # 检查是否可以合并
+            combined_length = len(current_chunk.content) + len(next_chunk.content)
+            
+            if (combined_length <= max_length and 
+                current_chunk.doc_id == next_chunk.doc_id):
+                # 合并块
+                current_chunk.content += "\n" + next_chunk.content
+                current_chunk.metadata["total_chunks"] = current_chunk.metadata.get("total_chunks", 1) + 1
+            else:
+                # 不能合并,保存当前块
+                merged_chunks.append(current_chunk)
+                current_chunk = next_chunk
+        
+        # 添加最后一个块
+        merged_chunks.append(current_chunk)
+        
+        return merged_chunks
+    
+    def filter_chunks(self, chunks: List[DocumentChunk], min_length: int = 50) -> List[DocumentChunk]:
+        """
+        过滤太短的文档块
+        
+        Args:
+            chunks: 文档块列表
+            min_length: 最小长度
+            
+        Returns:
+            过滤后的文档块列表
+        """
+        return [chunk for chunk in chunks if len(chunk.content.strip()) >= min_length]
+    
+    def add_chunk_metadata(self, chunks: List[DocumentChunk], metadata: Dict[str, Any]) -> List[DocumentChunk]:
+        """
+        为文档块添加额外元数据
+        
+        Args:
+            chunks: 文档块列表
+            metadata: 要添加的元数据
+            
+        Returns:
+            更新后的文档块列表
+        """
+        for chunk in chunks:
+            chunk.metadata.update(metadata)
+        
+        return chunks
+
+def load_text_file(file_path: str, encoding: str = "utf-8") -> Document:
+    """
+    加载文本文件为文档
+    
+    Args:
+        file_path: 文件路径
+        encoding: 文件编码
+        
+    Returns:
+        文档对象
+    """
+    with open(file_path, 'r', encoding=encoding) as f:
+        content = f.read()
+    
+    metadata = {
+        "source": file_path,
+        "type": "text_file",
+        "loaded_at": datetime.now().isoformat()
+    }
+    
+    return Document(content=content, metadata=metadata)
+
+def create_document(content: str, **metadata) -> Document:
+    """
+    创建文档的便捷函数
+    
+    Args:
+        content: 文档内容
+        **metadata: 元数据
+        
+    Returns:
+        文档对象
+    """
+    return Document(content=content, metadata=metadata)

+ 1207 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/rag/pipeline.py

@@ -0,0 +1,1207 @@
+from typing import List, Dict, Optional, Any
+import os
+import hashlib
+import sqlite3
+import time
+import json
+from ..embedding import get_text_embedder, get_dimension
+from ..storage.qdrant_store import QdrantVectorStore
+
+
+def _get_markitdown_instance():
+    """
+    Get a configured MarkItDown instance for document conversion.
+    """
+    try:
+        from markitdown import MarkItDown
+        return MarkItDown()
+    except ImportError:
+        print("[WARNING] MarkItDown not available. Install with: pip install markitdown")
+        return None
+
+
+def _is_markitdown_supported_format(path: str) -> bool:
+    """
+    Check if the file format is supported by MarkItDown.
+    Supports: PDF, Office docs (docx, xlsx, pptx), images (jpg, png, gif, bmp, tiff), 
+    audio (mp3, wav, m4a), HTML, text formats (txt, md, csv, json, xml), ZIP files, etc.
+    """
+    ext = (os.path.splitext(path)[1] or '').lower()
+    supported_formats = {
+        # Documents
+        '.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx',
+        # Text formats
+        '.txt', '.md', '.csv', '.json', '.xml', '.html', '.htm',
+        # Images (OCR + metadata)
+        '.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.tif', '.webp',
+        # Audio (transcription + metadata) 
+        '.mp3', '.wav', '.m4a', '.aac', '.flac', '.ogg',
+        # Archives
+        '.zip', '.tar', '.gz', '.rar',
+        # Code files
+        '.py', '.js', '.ts', '.java', '.cpp', '.c', '.h', '.css', '.scss',
+        # Other text
+        '.log', '.conf', '.ini', '.cfg', '.yaml', '.yml', '.toml'
+    }
+    return ext in supported_formats
+
+
+def _convert_to_markdown(path: str) -> str:
+    """
+    Universal document reader using MarkItDown with enhanced PDF processing.
+    Converts any supported file format to markdown text.
+    """
+    if not os.path.exists(path):
+        return ""
+    
+    # 对PDF文件使用增强处理
+    ext = (os.path.splitext(path)[1] or '').lower()
+    if ext == '.pdf':
+        return _enhanced_pdf_processing(path)
+    
+    # 其他格式使用原有MarkItDown
+    md_instance = _get_markitdown_instance()
+    if md_instance is None:
+        return _fallback_text_reader(path)
+    
+    try:
+        result = md_instance.convert(path)
+        text = getattr(result, "text_content", None)
+        if isinstance(text, str) and text.strip():
+            return text
+        return ""
+    except Exception as e:
+        print(f"[WARNING] MarkItDown failed for {path}: {e}")
+        return _fallback_text_reader(path)
+
+def _enhanced_pdf_processing(path: str) -> str:
+    """
+    Enhanced PDF processing with post-processing cleanup.
+    """
+    print(f"[RAG] Using enhanced PDF processing for: {path}")
+    
+    # 使用原有MarkItDown提取
+    md_instance = _get_markitdown_instance()
+    if md_instance is None:
+        return _fallback_text_reader(path)
+    
+    try:
+        result = md_instance.convert(path)
+        raw_text = getattr(result, "text_content", None)
+        if not raw_text or not raw_text.strip():
+            return ""
+        
+        # 后处理:清理和重组文本
+        cleaned_text = _post_process_pdf_text(raw_text)
+        print(f"[RAG] PDF post-processing completed: {len(raw_text)} -> {len(cleaned_text)} chars")
+        return cleaned_text
+        
+    except Exception as e:
+        print(f"[WARNING] Enhanced PDF processing failed for {path}: {e}")
+        return _fallback_text_reader(path)
+
+def _post_process_pdf_text(text: str) -> str:
+    """
+    Post-process PDF text to improve quality.
+    """
+    import re
+    
+    # 1. 按行分割并清理
+    lines = text.splitlines()
+    cleaned_lines = []
+    
+    for line in lines:
+        line = line.strip()
+        if not line:
+            continue
+            
+        # 移除单个字符的行(通常是噪音)
+        if len(line) <= 2 and not line.isdigit():
+            continue
+            
+        # 移除明显的页眉页脚噪音
+        if re.match(r'^\d+$', line):  # 纯数字行(页码)
+            continue
+        if line.lower() in ['github', 'project', 'forks', 'stars', 'language']:
+            continue
+            
+        cleaned_lines.append(line)
+    
+    # 2. 智能合并短行
+    merged_lines = []
+    i = 0
+    
+    while i < len(cleaned_lines):
+        current_line = cleaned_lines[i]
+        
+        # 如果当前行很短,尝试与下一行合并
+        if len(current_line) < 60 and i + 1 < len(cleaned_lines):
+            next_line = cleaned_lines[i + 1]
+            
+            # 合并条件:都是内容,不是标题
+            if (not current_line.endswith(':') and 
+                not current_line.endswith(':') and
+                not current_line.startswith('#') and
+                not next_line.startswith('#') and
+                len(next_line) < 120):
+                
+                merged_line = current_line + " " + next_line
+                merged_lines.append(merged_line)
+                i += 2  # 跳过下一行
+                continue
+        
+        merged_lines.append(current_line)
+        i += 1
+    
+    # 3. 重新组织段落
+    paragraphs = []
+    current_paragraph = []
+    
+    for line in merged_lines:
+        # 检查是否是新段落的开始
+        if (line.startswith('#') or  # 标题
+            line.endswith(':') or   # 中文冒号结尾
+            line.endswith(':') or    # 英文冒号结尾
+            len(line) > 150 or       # 长句通常是段落开始
+            not current_paragraph):  # 第一行
+            
+            # 保存当前段落
+            if current_paragraph:
+                paragraphs.append(' '.join(current_paragraph))
+                current_paragraph = []
+            
+            paragraphs.append(line)
+        else:
+            current_paragraph.append(line)
+    
+    # 添加最后一个段落
+    if current_paragraph:
+        paragraphs.append(' '.join(current_paragraph))
+    
+    return '\n\n'.join(paragraphs)
+
+
+def _fallback_text_reader(path: str) -> str:
+    """
+    Simple fallback reader for basic text files when MarkItDown is unavailable.
+    """
+    try:
+        with open(path, 'r', encoding='utf-8', errors='ignore') as f:
+            return f.read()
+    except Exception:
+        try:
+            with open(path, 'r', encoding='latin-1', errors='ignore') as f:
+                return f.read()
+        except Exception:
+            return ""
+
+
+def _detect_lang(sample: str) -> str:
+    try:
+        from langdetect import detect
+        return detect(sample[:1000]) if sample else "unknown"
+    except Exception:
+        return "unknown"
+
+
+def _is_cjk(ch: str) -> bool:
+    code = ord(ch)
+    return (
+        0x4E00 <= code <= 0x9FFF or
+        0x3400 <= code <= 0x4DBF or
+        0x20000 <= code <= 0x2A6DF or
+        0x2A700 <= code <= 0x2B73F or
+        0x2B740 <= code <= 0x2B81F or
+        0x2B820 <= code <= 0x2CEAF or
+        0xF900 <= code <= 0xFAFF
+    )
+
+
+def _approx_token_len(text: str) -> int:
+    # 近似估计:CJK字符按1 token,其他按空白分词
+    cjk = sum(1 for ch in text if _is_cjk(ch))
+    non_cjk_tokens = len([t for t in text.split() if t])
+    return cjk + non_cjk_tokens
+
+
+def _split_paragraphs_with_headings(text: str) -> List[Dict]:
+    lines = text.splitlines()
+    heading_stack: List[str] = []
+    paragraphs: List[Dict] = []
+    buf: List[str] = []
+    char_pos = 0
+    def flush_buf(end_pos: int):
+        if not buf:
+            return
+        content = "\n".join(buf).strip()
+        if not content:
+            return
+        paragraphs.append({
+            "content": content,
+            "heading_path": " > ".join(heading_stack) if heading_stack else None,
+            "start": max(0, end_pos - len(content)),
+            "end": end_pos,
+        })
+    for ln in lines:
+        raw = ln
+        if raw.strip().startswith("#"):
+            # heading line
+            flush_buf(char_pos)
+            level = len(raw) - len(raw.lstrip('#'))
+            title = raw.lstrip('#').strip()
+            if level <= 0:
+                level = 1
+            if level <= len(heading_stack):
+                heading_stack = heading_stack[:level-1]
+            heading_stack.append(title)
+            char_pos += len(raw) + 1
+            continue
+        # paragraph accumulation
+        if raw.strip() == "":
+            flush_buf(char_pos)
+            buf = []
+        else:
+            buf.append(raw)
+        char_pos += len(raw) + 1
+    flush_buf(char_pos)
+    if not paragraphs:
+        paragraphs = [{"content": text, "heading_path": None, "start": 0, "end": len(text)}]
+    return paragraphs
+
+
+def _chunk_paragraphs(paragraphs: List[Dict], chunk_tokens: int, overlap_tokens: int) -> List[Dict]:
+    chunks: List[Dict] = []
+    cur: List[Dict] = []
+    cur_tokens = 0
+    i = 0
+    while i < len(paragraphs):
+        p = paragraphs[i]
+        p_tokens = _approx_token_len(p["content"]) or 1
+        if cur_tokens + p_tokens <= chunk_tokens or not cur:
+            cur.append(p)
+            cur_tokens += p_tokens
+            i += 1
+        else:
+            # emit current chunk
+            content = "\n\n".join(x["content"] for x in cur)
+            start = cur[0]["start"]
+            end = cur[-1]["end"]
+            heading_path = next((x["heading_path"] for x in reversed(cur) if x.get("heading_path")), None)
+            chunks.append({
+                "content": content,
+                "start": start,
+                "end": end,
+                "heading_path": heading_path,
+            })
+            # build overlap by keeping tail tokens
+            if overlap_tokens > 0 and cur:
+                kept: List[Dict] = []
+                kept_tokens = 0
+                for x in reversed(cur):
+                    t = _approx_token_len(x["content"]) or 1
+                    if kept_tokens + t > overlap_tokens:
+                        break
+                    kept.append(x)
+                    kept_tokens += t
+                cur = list(reversed(kept))
+                cur_tokens = kept_tokens
+            else:
+                cur = []
+                cur_tokens = 0
+    if cur:
+        content = "\n\n".join(x["content"] for x in cur)
+        start = cur[0]["start"]
+        end = cur[-1]["end"]
+        heading_path = next((x["heading_path"] for x in reversed(cur) if x.get("heading_path")), None)
+        chunks.append({
+            "content": content,
+            "start": start,
+            "end": end,
+            "heading_path": heading_path,
+        })
+    return chunks
+
+
+def load_and_chunk_texts(paths: List[str], chunk_size: int = 800, chunk_overlap: int = 100, namespace: Optional[str] = None, source_label: str = "rag") -> List[Dict]:
+    """
+    Universal document loader and chunker using MarkItDown.
+    Converts all supported formats to markdown, then chunks intelligently.
+    """
+    print(f"[RAG] Universal loader start: files={len(paths)} chunk_size={chunk_size} overlap={chunk_overlap} ns={namespace or 'default'}")
+    chunks: List[Dict] = []
+    seen_hashes = set()
+    
+    for path in paths:
+        if not os.path.exists(path):
+            print(f"[WARNING] File not found: {path}")
+            continue
+            
+        print(f"[RAG] Processing: {path}")
+        ext = (os.path.splitext(path)[1] or '').lower()
+        
+        # Convert to markdown using MarkItDown
+        markdown_text = _convert_to_markdown(path)
+        if not markdown_text.strip():
+            print(f"[WARNING] No content extracted from: {path}")
+            continue
+        
+        lang = _detect_lang(markdown_text)
+        doc_id = hashlib.md5(f"{path}|{len(markdown_text)}".encode('utf-8')).hexdigest()
+        
+        # Always use markdown-aware chunking for better structure preservation
+        para = _split_paragraphs_with_headings(markdown_text)
+        token_chunks = _chunk_paragraphs(para, chunk_tokens=max(1, chunk_size), overlap_tokens=max(0, chunk_overlap))
+        
+        for ch in token_chunks:
+            content = ch["content"]
+            start = ch.get("start", 0)
+            end = ch.get("end", start + len(content))
+            norm = content.strip()
+            if not norm:
+                continue
+                
+            content_hash = hashlib.md5(norm.encode('utf-8')).hexdigest()
+            if content_hash in seen_hashes:
+                continue
+            seen_hashes.add(content_hash)
+            
+            chunk_id = hashlib.md5(f"{doc_id}|{start}|{end}|{content_hash}".encode('utf-8')).hexdigest()
+            chunks.append({
+                "id": chunk_id,
+                "content": content,
+                "metadata": {
+                    "source_path": path,
+                    "file_ext": ext,
+                    "doc_id": doc_id,
+                    "lang": lang,
+                    "start": start,
+                    "end": end,
+                    "content_hash": content_hash,
+                    "namespace": namespace or "default",
+                    "source": source_label,
+                    "external": True,
+                    "heading_path": ch.get("heading_path"),
+                    "format": "markdown",  # Mark all content as markdown-processed
+                },
+            })
+            
+    print(f"[RAG] Universal loader done: total_chunks={len(chunks)}")
+    return chunks
+
+
+def build_graph_from_chunks(neo4j, chunks: List[Dict]) -> None:
+    created_docs = set()
+    for ch in chunks:
+        mem_id = ch["id"]
+        meta = ch.get("metadata", {})
+        source_path = meta.get("source_path")
+        doc_id = meta.get("doc_id")
+        if doc_id and doc_id not in created_docs:
+            created_docs.add(doc_id)
+            try:
+                neo4j.add_entity(
+                    entity_id=doc_id,
+                    name=os.path.basename(source_path or doc_id),
+                    entity_type="Document",
+                    properties={"source_path": source_path, "lang": meta.get("lang")}
+                )
+            except Exception:
+                pass
+        try:
+            neo4j.add_entity(entity_id=mem_id, name=mem_id, entity_type="Memory", properties={
+                "source_path": source_path,
+                "doc_id": doc_id,
+                "start": meta.get("start"),
+                "end": meta.get("end"),
+            })
+        except Exception:
+            pass
+        if doc_id:
+            try:
+                neo4j.add_relationship(from_id=doc_id, to_id=mem_id, rel_type="HAS_CHUNK", properties={})
+            except Exception:
+                pass
+
+
+def _preprocess_markdown_for_embedding(text: str) -> str:
+    """
+    Preprocess markdown text for better embedding quality.
+    Removes excessive markup while preserving semantic content.
+    """
+    import re
+    
+    # Remove markdown headers symbols but keep the text
+    text = re.sub(r'^#{1,6}\s+', '', text, flags=re.MULTILINE)
+    
+    # Remove markdown links but keep the text
+    text = re.sub(r'\[([^\]]+)\]\([^)]+\)', r'\1', text)
+    
+    # Remove markdown emphasis markers
+    text = re.sub(r'\*\*([^*]+)\*\*', r'\1', text)  # bold
+    text = re.sub(r'\*([^*]+)\*', r'\1', text)      # italic
+    text = re.sub(r'`([^`]+)`', r'\1', text)        # inline code
+    
+    # Remove markdown code blocks but keep content
+    text = re.sub(r'```[^\n]*\n([\s\S]*?)```', r'\1', text)
+    
+    # Remove excessive whitespace
+    text = re.sub(r'\n\s*\n', '\n\n', text)
+    text = re.sub(r'[ \t]+', ' ', text)
+    
+    return text.strip()
+
+
+def _create_default_vector_store(dimension: int = None) -> QdrantVectorStore:
+    """
+    Create default Qdrant vector store with RAG-optimized settings.
+    使用连接管理器避免重复连接。
+    """
+    if dimension is None:
+        dimension = get_dimension(384)
+    
+    # Check for Qdrant configuration
+    qdrant_url = os.getenv("QDRANT_URL")
+    qdrant_api_key = os.getenv("QDRANT_API_KEY")
+    
+    # 使用连接管理器
+    from ..storage.qdrant_store import QdrantConnectionManager
+    return QdrantConnectionManager.get_instance(
+        url=qdrant_url,
+        api_key=qdrant_api_key,
+        collection_name="hello_agents_rag_vectors",
+        vector_size=dimension,
+        distance="cosine"
+    )
+
+
+# Cache functions removed - using unified embedder with internal caching
+
+
+def index_chunks(
+    store = None, 
+    chunks: List[Dict] = None, 
+    cache_db: Optional[str] = None, 
+    batch_size: int = 64,
+    rag_namespace: str = "default"
+) -> None:
+    """
+    Index markdown chunks with unified embedding and Qdrant storage.
+    Uses百炼 API with fallback to sentence-transformers.
+    """
+    if not chunks:
+        print("[RAG] No chunks to index")
+        return
+    
+    # Use unified embedding from embedding module
+    embedder = get_text_embedder()
+    dimension = get_dimension(384)
+    
+    # Create default Qdrant store if not provided
+    if store is None:
+        store = _create_default_vector_store(dimension)
+        print(f"[RAG] Created default Qdrant store with dimension {dimension}")
+    
+    # Preprocess markdown texts for better embeddings
+    processed_texts = []
+    for c in chunks:
+        raw_content = c["content"]
+        processed_content = _preprocess_markdown_for_embedding(raw_content)
+        processed_texts.append(processed_content)
+    
+    print(f"[RAG] Embedding start: total_texts={len(processed_texts)} batch_size={batch_size}")
+    
+    # Batch encoding with unified embedder
+    vecs: List[List[float]] = []
+    for i in range(0, len(processed_texts), batch_size):
+        part = processed_texts[i:i+batch_size]
+        try:
+            # Use unified embedder directly (handles caching internally)
+            part_vecs = embedder.encode(part)
+            
+            # Normalize to List[List[float]]
+            if not isinstance(part_vecs, list):
+                # 单个numpy数组转为列表中的列表
+                if hasattr(part_vecs, "tolist"):
+                    part_vecs = [part_vecs.tolist()]
+                else:
+                    part_vecs = [list(part_vecs)]
+            else:
+                # 检查是否是嵌套列表
+                if part_vecs and not isinstance(part_vecs[0], (list, tuple)) and hasattr(part_vecs[0], "__len__"):
+                    # numpy数组列表 -> 转换每个数组
+                    normalized_vecs = []
+                    for v in part_vecs:
+                        if hasattr(v, "tolist"):
+                            normalized_vecs.append(v.tolist())
+                        else:
+                            normalized_vecs.append(list(v))
+                    part_vecs = normalized_vecs
+                elif part_vecs and not isinstance(part_vecs[0], (list, tuple)):
+                    # 单个向量被误判为列表,实际应该包装成[[...]]
+                    if hasattr(part_vecs, "tolist"):
+                        part_vecs = [part_vecs.tolist()]
+                    else:
+                        part_vecs = [list(part_vecs)]
+            
+            for v in part_vecs:
+                try:
+                    # 确保向量是float列表
+                    if hasattr(v, "tolist"):
+                        v = v.tolist()
+                    v_norm = [float(x) for x in v]
+                    if len(v_norm) != dimension:
+                        print(f"[WARNING] 向量维度异常: 期望{dimension}, 实际{len(v_norm)}")
+                        # 用零向量填充或截断
+                        if len(v_norm) < dimension:
+                            v_norm.extend([0.0] * (dimension - len(v_norm)))
+                        else:
+                            v_norm = v_norm[:dimension]
+                    vecs.append(v_norm)
+                except Exception as e:
+                    print(f"[WARNING] 向量转换失败: {e}, 使用零向量")
+                    vecs.append([0.0] * dimension)
+                
+        except Exception as e:
+            print(f"[WARNING] Batch {i} encoding failed: {e}")
+            print(f"[RAG] Retrying batch {i} with smaller chunks...")
+            
+            # 尝试重试:将批次分解为更小的块
+            success = False
+            for j in range(0, len(part), 8):  # 更小的批次
+                small_part = part[j:j+8]
+                try:
+                    import time
+                    time.sleep(2)  # 等待2秒避免频率限制
+                    
+                    small_vecs = embedder.encode(small_part)
+                    # Normalize to List[List[float]]
+                    if isinstance(small_vecs, list) and small_vecs and not isinstance(small_vecs[0], list):
+                        small_vecs = [small_vecs]
+                    
+                    for v in small_vecs:
+                        if hasattr(v, "tolist"):
+                            v = v.tolist()
+                        try:
+                            v_norm = [float(x) for x in v]
+                            if len(v_norm) != dimension:
+                                print(f"[WARNING] 向量维度异常: 期望{dimension}, 实际{len(v_norm)}")
+                                if len(v_norm) < dimension:
+                                    v_norm.extend([0.0] * (dimension - len(v_norm)))
+                                else:
+                                    v_norm = v_norm[:dimension]
+                            vecs.append(v_norm)
+                            success = True
+                        except Exception as e2:
+                            print(f"[WARNING] 小批次向量转换失败: {e2}")
+                            vecs.append([0.0] * dimension)
+                except Exception as e2:
+                    print(f"[WARNING] 小批次 {j//8} 仍然失败: {e2}")
+                    # 为这个小批次创建零向量
+                    for _ in range(len(small_part)):
+                        vecs.append([0.0] * dimension)
+            
+            if not success:
+                print(f"[ERROR] 批次 {i} 完全失败,使用零向量")
+        
+        print(f"[RAG] Embedding progress: {min(i+batch_size, len(processed_texts))}/{len(processed_texts)}")
+    
+    # Prepare metadata with RAG tags
+    metas: List[Dict] = []
+    ids: List[str] = []
+    for ch in chunks:
+        meta = {
+            "memory_id": ch["id"],
+            "user_id": "rag_user",
+            "memory_type": "rag_chunk",
+            "content": ch["content"],  # Keep original markdown content
+            "data_source": "rag_pipeline",  # RAG identification tag
+            "rag_namespace": rag_namespace,
+            "is_rag_data": True,  # Clear RAG data marker
+        }
+        # Merge chunk metadata
+        meta.update(ch.get("metadata", {}))
+        metas.append(meta)
+        ids.append(ch["id"])
+    
+    print(f"[RAG] Qdrant upsert start: n={len(vecs)}")
+    success = store.add_vectors(vectors=vecs, metadata=metas, ids=ids)
+    if success:
+        print(f"[RAG] Qdrant upsert done: {len(vecs)} vectors indexed")
+    else:
+        print(f"[RAG] Qdrant upsert failed")
+        raise RuntimeError("Failed to index vectors to Qdrant")
+
+
+def embed_query(query: str) -> List[float]:
+    """
+    Embed query using unified embedding (百炼 with fallback).
+    """
+    embedder = get_text_embedder()
+    dimension = get_dimension(384)
+    try:
+        vec = embedder.encode(query)
+        
+        # Normalize to List[float]
+        if hasattr(vec, "tolist"):
+            vec = vec.tolist()
+        
+        # 处理嵌套列表情况
+        if isinstance(vec, list) and vec and isinstance(vec[0], (list, tuple)):
+            vec = vec[0]  # Extract first vector if nested
+        
+        # 转换为float列表
+        result = [float(x) for x in vec]
+        
+        # 检查维度
+        if len(result) != dimension:
+            print(f"[WARNING] Query向量维度异常: 期望{dimension}, 实际{len(result)}")
+            # 用零向量填充或截断
+            if len(result) < dimension:
+                result.extend([0.0] * (dimension - len(result)))
+            else:
+                result = result[:dimension]
+        
+        return result
+    except Exception as e:
+        print(f"[WARNING] Query embedding failed: {e}")
+        # Return zero vector as fallback
+        return [0.0] * dimension
+
+
+def search_vectors(
+    store = None, 
+    query: str = "", 
+    top_k: int = 8, 
+    rag_namespace: Optional[str] = None, 
+    only_rag_data: bool = True, 
+    score_threshold: Optional[float] = None
+) -> List[Dict]:
+    """
+    Search RAG vectors using unified embedding and Qdrant.
+    """
+    if not query:
+        return []
+    
+    # Create default store if not provided
+    if store is None:
+        store = _create_default_vector_store()
+    
+    # Embed query with unified embedder
+    qv = embed_query(query)
+    
+    # Build filter for RAG data
+    where = {"memory_type": "rag_chunk"}
+    if only_rag_data:
+        where["is_rag_data"] = True
+        where["data_source"] = "rag_pipeline"
+    if rag_namespace:
+        where["rag_namespace"] = rag_namespace
+    
+    try:
+        return store.search_similar(
+            query_vector=qv, 
+            limit=top_k, 
+            score_threshold=score_threshold, 
+            where=where
+        )
+    except Exception as e:
+        print(f"[WARNING] RAG search failed: {e}")
+        return []
+
+
+def _prompt_mqe(query: str, n: int) -> List[str]:
+    try:
+        from core.llm import HelloAgentsLLM
+        llm = HelloAgentsLLM()
+        prompt = [
+            {"role": "system", "content": "你是检索查询扩展助手。生成语义等价或互补的多样化查询。使用中文,简短,避免标点。"},
+            {"role": "user", "content": f"原始查询:{query}\n请给出{n}个不同表述的查询,每行一个。"}
+        ]
+        text = llm.invoke(prompt)
+        lines = [ln.strip("- \t") for ln in (text or "").splitlines()]
+        outs = [ln for ln in lines if ln]
+        return outs[:n] or [query]
+    except Exception:
+        return [query]
+
+
+def _prompt_hyde(query: str) -> Optional[str]:
+    try:
+        from core.llm import HelloAgentsLLM
+        llm = HelloAgentsLLM()
+        prompt = [
+            {"role": "system", "content": "根据用户问题,先写一段可能的答案性段落,用于向量检索的查询文档(不要分析过程)。"},
+            {"role": "user", "content": f"问题:{query}\n请直接写一段中等长度、客观、包含关键术语的段落。"}
+        ]
+        return llm.invoke(prompt)
+    except Exception:
+        return None
+
+
+def search_vectors_expanded(
+    store = None,
+    query: str = "",
+    top_k: int = 8,
+    rag_namespace: Optional[str] = None,
+    only_rag_data: bool = True,
+    score_threshold: Optional[float] = None,
+    enable_mqe: bool = False,
+    mqe_expansions: int = 2,
+    enable_hyde: bool = False,
+    candidate_pool_multiplier: int = 4,
+) -> List[Dict]:
+    """
+    Search with query expansion using unified embedding and Qdrant.
+    """
+    if not query:
+        return []
+    
+    # Create default store if not provided
+    if store is None:
+        store = _create_default_vector_store()
+    
+    # expansions
+    expansions: List[str] = [query]
+    
+    if enable_mqe and mqe_expansions > 0:
+        expansions.extend(_prompt_mqe(query, mqe_expansions))
+    if enable_hyde:
+        hyde_text = _prompt_hyde(query)
+        if hyde_text:
+            expansions.append(hyde_text)
+
+    # unique and trim
+    uniq: List[str] = []
+    for e in expansions:
+        if e and e not in uniq:
+            uniq.append(e)
+    expansions = uniq[: max(1, len(uniq))]
+
+    # distribute pool per expansion
+    pool = max(top_k * candidate_pool_multiplier, 20)
+    per = max(1, pool // max(1, len(expansions)))
+
+    # Build filter for RAG data
+    where = {"memory_type": "rag_chunk"}
+    if only_rag_data:
+        where["is_rag_data"] = True
+        where["data_source"] = "rag_pipeline"
+    if rag_namespace:
+        where["rag_namespace"] = rag_namespace
+
+    # collect hits across expansions
+    agg: Dict[str, Dict] = {}
+    for q in expansions:
+        qv = embed_query(q)
+        hits = store.search_similar(query_vector=qv, limit=per, score_threshold=score_threshold, where=where)
+        for h in hits:
+            mid = h.get("metadata", {}).get("memory_id", h.get("id"))
+            s = float(h.get("score", 0.0))
+            if mid not in agg or s > float(agg[mid].get("score", 0.0)):
+                agg[mid] = h
+    # return top by score
+    merged = list(agg.values())
+    merged.sort(key=lambda x: float(x.get("score", 0.0)), reverse=True)
+    return merged[:top_k]
+
+
+def _try_load_cross_encoder(model_name: str = "cross-encoder/ms-marco-MiniLM-L-6-v2"):
+    try:
+        from sentence_transformers import CrossEncoder
+        return CrossEncoder(model_name)
+    except Exception:
+        return None
+
+
+def rerank_with_cross_encoder(query: str, items: List[Dict], model_name: str = "cross-encoder/ms-marco-MiniLM-L-6-v2", top_k: int = 10) -> List[Dict]:
+    ce = _try_load_cross_encoder(model_name)
+    if ce is None or not items:
+        return items[:top_k]
+    pairs = [[query, it.get("content", "")] for it in items]
+    try:
+        scores = ce.predict(pairs)
+        for it, s in zip(items, scores):
+            it["rerank_score"] = float(s)
+        items.sort(key=lambda x: x.get("rerank_score", x.get("score", 0.0)), reverse=True)
+        return items[:top_k]
+    except Exception:
+        return items[:top_k]
+
+
+def compute_graph_signals_from_pool(vector_hits: List[Dict], same_doc_weight: float = 1.0, proximity_weight: float = 1.0, proximity_window_chars: int = 1600) -> Dict[str, float]:
+    """
+    Compute graph signals with direct parameters instead of environment variables.
+    """
+
+    # group by doc
+    by_doc: Dict[str, List[Dict]] = {}
+    for h in vector_hits:
+        meta = h.get("metadata", {})
+        did = meta.get("doc_id")
+        if not did:
+            # fall back to memory_id grouping if doc missing
+            did = meta.get("memory_id") or h.get("id")
+        by_doc.setdefault(did, []).append(h)
+
+    # same-doc density score
+    doc_counts = {d: len(arr) for d, arr in by_doc.items()}
+    max_count = max(doc_counts.values()) if doc_counts else 1
+
+    # proximity score per hit within same doc
+    graph_signal: Dict[str, float] = {}
+    for did, arr in by_doc.items():
+        arr.sort(key=lambda x: x.get("metadata", {}).get("start", 0))
+        # precompute density
+        density = doc_counts.get(did, 1) / max_count
+        # proximity accumulation
+        for i, h in enumerate(arr):
+            mid = h.get("metadata", {}).get("memory_id", h.get("id"))
+            pos_i = h.get("metadata", {}).get("start", 0)
+            prox_acc = 0.0
+            # look around neighbors within window
+            # two-pointer expansion
+            # left
+            j = i - 1
+            while j >= 0:
+                pos_j = arr[j].get("metadata", {}).get("start", 0)
+                dist = abs(pos_i - pos_j)
+                if dist > proximity_window_chars:
+                    break
+                prox_acc += max(0.0, 1.0 - (dist / max(1.0, float(proximity_window_chars))))
+                j -= 1
+            # right
+            j = i + 1
+            while j < len(arr):
+                pos_j = arr[j].get("metadata", {}).get("start", 0)
+                dist = abs(pos_i - pos_j)
+                if dist > proximity_window_chars:
+                    break
+                prox_acc += max(0.0, 1.0 - (dist / max(1.0, float(proximity_window_chars))))
+                j += 1
+            # combine
+            score = same_doc_weight * density + proximity_weight * prox_acc
+            graph_signal[mid] = graph_signal.get(mid, 0.0) + score
+
+    # normalize to [0,1]
+    if graph_signal:
+        max_v = max(graph_signal.values())
+        if max_v > 0:
+            for k in list(graph_signal.keys()):
+                graph_signal[k] = graph_signal[k] / max_v
+    return graph_signal
+
+
+def rank(vector_hits: List[Dict], graph_signals: Optional[Dict[str, float]] = None, w_vector: float = 0.7, w_graph: float = 0.3) -> List[Dict]:
+    """
+    Rank results with direct weight parameters instead of environment variables.
+    """
+    items: List[Dict] = []
+    graph_signals = graph_signals or {}
+    for h in vector_hits:
+        mid = h.get("metadata", {}).get("memory_id", h.get("id"))
+        g = float(graph_signals.get(mid, 0.0))
+        v = float(h.get("score", 0.0))
+        score = w_vector * v + w_graph * g
+        items.append({
+            "memory_id": mid,
+            "score": score,
+            "vector_score": v,
+            "graph_score": g,
+            "content": h.get("metadata", {}).get("content", ""),
+            "metadata": h.get("metadata", {}),
+        })
+    items.sort(key=lambda x: x["score"], reverse=True)
+    return items
+
+
+def merge_snippets(ranked_items: List[Dict], max_chars: int = 1200) -> str:
+    out: List[str] = []
+    total = 0
+    for it in ranked_items:
+        text = it.get("content", "").strip()
+        if not text:
+            continue
+        if total + len(text) > max_chars:
+            remain = max_chars - total
+            if remain <= 0:
+                break
+            out.append(text[:remain])
+            total += remain
+            break
+        out.append(text)
+        total += len(text)
+    return "\n\n".join(out)
+
+
+def expand_neighbors_from_pool(selected: List[Dict], pool: List[Dict], neighbors: int = 1, max_additions: int = 5) -> List[Dict]:
+    if not selected or not pool or neighbors <= 0:
+        return selected
+    # index pool by doc_id and sort by start
+    by_doc: Dict[str, List[Dict]] = {}
+    for it in pool:
+        meta = it.get("metadata", {})
+        did = meta.get("doc_id")
+        if not did:
+            continue
+        by_doc.setdefault(did, []).append(it)
+    for did, arr in by_doc.items():
+        arr.sort(key=lambda x: (x.get("metadata", {}).get("start", 0)))
+    selected_ids = set(it.get("memory_id") for it in selected)
+    additions: List[Dict] = []
+    for it in selected:
+        meta = it.get("metadata", {})
+        did = meta.get("doc_id")
+        if not did or did not in by_doc:
+            continue
+        arr = by_doc[did]
+        # find index
+        try:
+            idx = next(i for i, x in enumerate(arr) if x.get("memory_id") == it.get("memory_id"))
+        except StopIteration:
+            continue
+        for offset in range(1, neighbors + 1):
+            for j in (idx - offset, idx + offset):
+                if 0 <= j < len(arr):
+                    cand = arr[j]
+                    mid = cand.get("memory_id")
+                    if mid not in selected_ids:
+                        additions.append(cand)
+                        selected_ids.add(mid)
+                        if len(additions) >= max_additions:
+                            break
+            if len(additions) >= max_additions:
+                break
+        if len(additions) >= max_additions:
+            break
+    # keep relative order by score
+    extended = list(selected) + additions
+    extended.sort(key=lambda x: (x.get("rerank_score", x.get("score", 0.0))), reverse=True)
+    return extended
+
+
+def merge_snippets_grouped(ranked_items: List[Dict], max_chars: int = 1200, include_citations: bool = True) -> str:
+    # Group by doc_id and aggregate doc score
+    by_doc: Dict[str, List[Dict]] = {}
+    doc_score: Dict[str, float] = {}
+    for it in ranked_items:
+        meta = it.get("metadata", {})
+        did = meta.get("doc_id") or meta.get("source_path") or "unknown"
+        by_doc.setdefault(did, []).append(it)
+        doc_score[did] = doc_score.get(did, 0.0) + float(it.get("score", 0.0))
+    # Sort docs by aggregate score
+    ordered_docs = sorted(by_doc.keys(), key=lambda d: doc_score.get(d, 0.0), reverse=True)
+    # Within doc, order by start offset to preserve context
+    for d in ordered_docs:
+        by_doc[d].sort(key=lambda x: (x.get("metadata", {}).get("start", 0)))
+    out: List[str] = []
+    citations: List[Dict] = []
+    total = 0
+    cite_index = 1
+    for did in ordered_docs:
+        parts = by_doc[did]
+        for it in parts:
+            text = (it.get("content", "") or "").strip()
+            if not text:
+                continue
+            # add citation marker if enabled
+            suffix = ""
+            if include_citations:
+                suffix = f" [{cite_index}]"
+            need = len(text) + (len(suffix) if suffix else 0)
+            if total + need > max_chars:
+                remain = max_chars - total
+                if remain <= 0:
+                    break
+                clipped = text[: max(0, remain - len(suffix))]
+                if clipped:
+                    out.append(clipped + suffix)
+                    total += len(clipped) + len(suffix)
+                    if include_citations:
+                        m = it.get("metadata", {})
+                        citations.append({
+                            "index": cite_index,
+                            "source_path": m.get("source_path"),
+                            "doc_id": m.get("doc_id"),
+                            "start": m.get("start"),
+                            "end": m.get("end"),
+                            "heading_path": m.get("heading_path"),
+                        })
+                        cite_index += 1
+                break
+            out.append(text + suffix)
+            total += need
+            if include_citations:
+                m = it.get("metadata", {})
+                citations.append({
+                    "index": cite_index,
+                    "source_path": m.get("source_path"),
+                    "doc_id": m.get("doc_id"),
+                    "start": m.get("start"),
+                    "end": m.get("end"),
+                    "heading_path": m.get("heading_path"),
+                })
+                cite_index += 1
+        if total >= max_chars:
+            break
+    merged = "\n\n".join(out)
+    if include_citations and citations:
+        lines: List[str] = [merged, "", "References:"]
+        for c in citations:
+            loc = ""
+            if c.get("start") is not None and c.get("end") is not None:
+                loc = f" ({c['start']}-{c['end']})"
+            hp = f" – {c['heading_path']}" if c.get("heading_path") else ""
+            sp = c.get("source_path") or c.get("doc_id") or "source"
+            lines.append(f"[{c['index']}] {sp}{loc}{hp}")
+        return "\n".join(lines)
+    return merged
+
+
+def compress_ranked_items(ranked_items: List[Dict], enable_compression: bool = True, max_per_doc: int = 2, join_gap: int = 200) -> List[Dict]:
+    """
+    Compress ranked items with direct parameters instead of environment variables.
+    """
+    if not enable_compression:
+        return ranked_items
+    by_doc_count: Dict[str, int] = {}
+    last_by_doc: Dict[str, Dict] = {}
+    new_items: List[Dict] = []
+    for it in ranked_items:
+        meta = it.get("metadata", {})
+        did = meta.get("doc_id") or meta.get("source_path") or "unknown"
+        start = int(meta.get("start") or 0)
+        end = int(meta.get("end") or (start + len(it.get("content", "") or "")))
+        if did not in last_by_doc:
+            last_by_doc[did] = it
+            by_doc_count[did] = 1
+            new_items.append(it)
+            continue
+        last = last_by_doc[did]
+        lmeta = last.get("metadata", {})
+        lstart = int(lmeta.get("start") or 0)
+        lend = int(lmeta.get("end") or (lstart + len(last.get("content", "") or "")))
+        if start - lend <= join_gap and start >= lstart:
+            # merge into last
+            merged_text = (last.get("content", "") or "").strip()
+            add_text = (it.get("content", "") or "").strip()
+            if add_text:
+                if merged_text:
+                    merged_text = merged_text + "\n\n" + add_text
+                else:
+                    merged_text = add_text
+                last["content"] = merged_text
+                lmeta["end"] = max(lend, end)
+                # keep the higher score
+                try:
+                    last["score"] = max(float(last.get("score", 0.0)), float(it.get("score", 0.0)))
+                except Exception:
+                    pass
+            last_by_doc[did] = last
+        else:
+            cnt = by_doc_count.get(did, 0)
+            if cnt >= max_per_doc:
+                continue
+            new_items.append(it)
+            last_by_doc[did] = it
+            by_doc_count[did] = cnt + 1
+    return new_items
+
+
+def tldr_summarize(text: str, bullets: int = 3) -> Optional[str]:
+    try:
+        if not text or len(text.strip()) == 0:
+            return None
+        from core.llm import HelloAgentsLLM
+        llm = HelloAgentsLLM()
+        prompt = [
+            {"role": "system", "content": "请将以下内容概括为简洁的要点列表(最多3-5条),用中文,避免重复,突出关键信息。"},
+            {"role": "user", "content": f"请用 {max(1, min(5, int(bullets)))} 条要点总结:\n\n{text}"},
+        ]
+        out = llm.invoke(prompt)
+        return out
+    except Exception:
+        return None
+
+
+# ==================
+# High-level RAG Pipeline API
+# ==================
+
+def create_rag_pipeline(
+    qdrant_url: Optional[str] = None,
+    qdrant_api_key: Optional[str] = None,
+    collection_name: str = "hello_agents_rag_vectors",
+    rag_namespace: str = "default"
+) -> Dict[str, Any]:
+    """
+    Create a complete RAG pipeline with Qdrant and unified embedding.
+    
+    Returns:
+        Dict containing store, namespace, and helper functions
+    """
+    dimension = get_dimension(384)
+    
+    store = QdrantVectorStore(
+        url=qdrant_url,
+        api_key=qdrant_api_key,
+        collection_name=collection_name,
+        vector_size=dimension,
+        distance="cosine"
+    )
+    
+    def add_documents(file_paths: List[str], chunk_size: int = 800, chunk_overlap: int = 100):
+        """Add documents to RAG pipeline"""
+        chunks = load_and_chunk_texts(
+            paths=file_paths,
+            chunk_size=chunk_size,
+            chunk_overlap=chunk_overlap,
+            namespace=rag_namespace,
+            source_label="rag"
+        )
+        index_chunks(
+            store=store,
+            chunks=chunks,
+            rag_namespace=rag_namespace
+        )
+        return len(chunks)
+    
+    def search(query: str, top_k: int = 8, score_threshold: Optional[float] = None):
+        """Search RAG knowledge base"""
+        return search_vectors(
+            store=store,
+            query=query,
+            top_k=top_k,
+            rag_namespace=rag_namespace,
+            score_threshold=score_threshold
+        )
+    
+    def search_advanced(
+        query: str, 
+        top_k: int = 8, 
+        enable_mqe: bool = False,
+        enable_hyde: bool = False,
+        score_threshold: Optional[float] = None
+    ):
+        """Advanced search with query expansion"""
+        return search_vectors_expanded(
+            store=store,
+            query=query,
+            top_k=top_k,
+            rag_namespace=rag_namespace,
+            enable_mqe=enable_mqe,
+            enable_hyde=enable_hyde,
+            score_threshold=score_threshold
+        )
+    
+    def get_stats():
+        """Get pipeline statistics"""
+        return store.get_collection_stats()
+    
+    return {
+        "store": store,
+        "namespace": rag_namespace,
+        "add_documents": add_documents,
+        "search": search,
+        "search_advanced": search_advanced,
+        "get_stats": get_stats
+    }

+ 18 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/storage/__init__.py

@@ -0,0 +1,18 @@
+"""存储层模块
+
+按照第8章架构设计的存储层:
+- DocumentStore: 文档存储
+- QdrantVectorStore: Qdrant向量存储
+- Neo4jGraphStore: Neo4j图存储
+"""
+
+from .qdrant_store import QdrantVectorStore, QdrantConnectionManager
+from .neo4j_store import Neo4jGraphStore
+from .document_store import DocumentStore, SQLiteDocumentStore
+__all__ = [
+    "QdrantVectorStore",
+    "QdrantConnectionManager",
+    "Neo4jGraphStore",
+    "DocumentStore",
+    "SQLiteDocumentStore"
+]

+ 462 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/storage/document_store.py

@@ -0,0 +1,462 @@
+"""文档存储实现
+
+支持多种文档数据库后端:
+- SQLite: 轻量级关系型数据库
+- PostgreSQL: 企业级关系型数据库(可扩展)
+"""
+
+from abc import ABC, abstractmethod
+from typing import List, Dict, Any, Optional
+import sqlite3
+import json
+import os
+import threading
+
+
+class DocumentStore(ABC):
+    """文档存储基类"""
+    
+    @abstractmethod
+    def add_memory(
+        self,
+        memory_id: str,
+        user_id: str,
+        content: str,
+        memory_type: str,
+        timestamp: int,
+        importance: float,
+        properties: Dict[str, Any] = None
+    ) -> str:
+        """添加记忆"""
+        pass
+    
+    @abstractmethod
+    def get_memory(self, memory_id: str) -> Optional[Dict[str, Any]]:
+        """获取单个记忆"""
+        pass
+    
+    @abstractmethod
+    def search_memories(
+        self,
+        user_id: Optional[str] = None,
+        memory_type: Optional[str] = None,
+        text_query: Optional[str] = None,
+        start_time: Optional[int] = None,
+        end_time: Optional[int] = None,
+        importance_threshold: Optional[float] = None,
+        limit: int = 10
+    ) -> List[Dict[str, Any]]:
+        """搜索记忆"""
+        pass
+    
+    @abstractmethod
+    def update_memory(
+        self,
+        memory_id: str,
+        content: str = None,
+        importance: float = None,
+        properties: Dict[str, Any] = None
+    ) -> bool:
+        """更新记忆"""
+        pass
+    
+    @abstractmethod
+    def delete_memory(self, memory_id: str) -> bool:
+        """删除记忆"""
+        pass
+    
+    @abstractmethod
+    def get_database_stats(self) -> Dict[str, Any]:
+        """获取数据库统计信息"""
+        pass
+    
+    @abstractmethod
+    def add_document(self, content: str, metadata: Dict[str, Any] = None) -> str:
+        """添加文档"""
+        pass
+    
+    @abstractmethod
+    def get_document(self, document_id: str) -> Optional[Dict[str, Any]]:
+        """获取文档"""
+        pass
+
+class SQLiteDocumentStore(DocumentStore):
+    """SQLite文档存储实现"""
+    
+    _instances = {}  # 存储已创建的实例
+    _initialized_dbs = set()  # 存储已初始化的数据库路径
+    
+    def __new__(cls, db_path: str = "./memory.db"):
+        """单例模式,同一路径只创建一个实例"""
+        abs_path = os.path.abspath(db_path)
+        if abs_path not in cls._instances:
+            instance = super(SQLiteDocumentStore, cls).__new__(cls)
+            cls._instances[abs_path] = instance
+        return cls._instances[abs_path]
+    
+    def __init__(self, db_path: str = "./memory.db"):
+        # 避免重复初始化
+        if hasattr(self, '_initialized'):
+            return
+            
+        self.db_path = db_path
+        self.local = threading.local()
+        
+        # 确保目录存在
+        os.makedirs(os.path.dirname(os.path.abspath(db_path)), exist_ok=True)
+        
+        # 初始化数据库(只初始化一次)
+        abs_path = os.path.abspath(db_path)
+        if abs_path not in self._initialized_dbs:
+            self._init_database()
+            self._initialized_dbs.add(abs_path)
+            print(f"[OK] SQLite 文档存储初始化完成: {db_path}")
+        
+        self._initialized = True
+    
+    def _get_connection(self):
+        """获取线程本地连接"""
+        if not hasattr(self.local, 'connection'):
+            self.local.connection = sqlite3.connect(self.db_path)
+            self.local.connection.row_factory = sqlite3.Row  # 使结果可以按列名访问
+        return self.local.connection
+    
+    def _init_database(self):
+        """初始化数据库表"""
+        conn = self._get_connection()
+        cursor = conn.cursor()
+        
+        # 创建用户表
+        cursor.execute("""
+            CREATE TABLE IF NOT EXISTS users (
+                id TEXT PRIMARY KEY,
+                name TEXT,
+                properties TEXT,
+                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+            )
+        """)
+        
+        # 创建记忆表
+        cursor.execute("""
+            CREATE TABLE IF NOT EXISTS memories (
+                id TEXT PRIMARY KEY,
+                user_id TEXT NOT NULL,
+                content TEXT NOT NULL,
+                memory_type TEXT NOT NULL,
+                timestamp INTEGER NOT NULL,
+                importance REAL NOT NULL,
+                properties TEXT,
+                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+                updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+                FOREIGN KEY (user_id) REFERENCES users (id)
+            )
+        """)
+        
+        # 创建概念表
+        cursor.execute("""
+            CREATE TABLE IF NOT EXISTS concepts (
+                id TEXT PRIMARY KEY,
+                name TEXT NOT NULL,
+                description TEXT,
+                properties TEXT,
+                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+            )
+        """)
+        
+        # 创建记忆-概念关联表
+        cursor.execute("""
+            CREATE TABLE IF NOT EXISTS memory_concepts (
+                memory_id TEXT NOT NULL,
+                concept_id TEXT NOT NULL,
+                relevance_score REAL DEFAULT 1.0,
+                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+                PRIMARY KEY (memory_id, concept_id),
+                FOREIGN KEY (memory_id) REFERENCES memories (id) ON DELETE CASCADE,
+                FOREIGN KEY (concept_id) REFERENCES concepts (id) ON DELETE CASCADE
+            )
+        """)
+        
+        # 创建概念关系表
+        cursor.execute("""
+            CREATE TABLE IF NOT EXISTS concept_relationships (
+                from_concept_id TEXT NOT NULL,
+                to_concept_id TEXT NOT NULL,
+                relationship_type TEXT NOT NULL,
+                strength REAL DEFAULT 1.0,
+                properties TEXT,
+                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+                PRIMARY KEY (from_concept_id, to_concept_id, relationship_type),
+                FOREIGN KEY (from_concept_id) REFERENCES concepts (id) ON DELETE CASCADE,
+                FOREIGN KEY (to_concept_id) REFERENCES concepts (id) ON DELETE CASCADE
+            )
+        """)
+        
+        # 创建索引
+        indexes = [
+            "CREATE INDEX IF NOT EXISTS idx_memories_user_id ON memories (user_id)",
+            "CREATE INDEX IF NOT EXISTS idx_memories_type ON memories (memory_type)",
+            "CREATE INDEX IF NOT EXISTS idx_memories_timestamp ON memories (timestamp)",
+            "CREATE INDEX IF NOT EXISTS idx_memories_importance ON memories (importance)",
+            "CREATE INDEX IF NOT EXISTS idx_memory_concepts_memory ON memory_concepts (memory_id)",
+            "CREATE INDEX IF NOT EXISTS idx_memory_concepts_concept ON memory_concepts (concept_id)"
+        ]
+        
+        for index_sql in indexes:
+            cursor.execute(index_sql)
+        
+        conn.commit()
+        print("[OK] SQLite 数据库表和索引创建完成")
+    
+    def add_memory(
+        self,
+        memory_id: str,
+        user_id: str,
+        content: str,
+        memory_type: str,
+        timestamp: int,
+        importance: float,
+        properties: Dict[str, Any] = None
+    ) -> str:
+        """添加记忆"""
+        conn = self._get_connection()
+        cursor = conn.cursor()
+        
+        # 确保用户存在
+        cursor.execute("INSERT OR IGNORE INTO users (id, name) VALUES (?, ?)", (user_id, user_id))
+        
+        # 插入记忆
+        cursor.execute("""
+            INSERT OR REPLACE INTO memories 
+            (id, user_id, content, memory_type, timestamp, importance, properties, updated_at)
+            VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
+        """, (
+            memory_id,
+            user_id,
+            content,
+            memory_type,
+            timestamp,
+            importance,
+            json.dumps(properties) if properties else None
+        ))
+        
+        conn.commit()
+        return memory_id
+    
+    def get_memory(self, memory_id: str) -> Optional[Dict[str, Any]]:
+        """获取单个记忆"""
+        conn = self._get_connection()
+        cursor = conn.cursor()
+        
+        cursor.execute("""
+            SELECT id, user_id, content, memory_type, timestamp, importance, properties, created_at
+            FROM memories
+            WHERE id = ?
+        """, (memory_id,))
+        
+        row = cursor.fetchone()
+        if not row:
+            return None
+        
+        return {
+            "memory_id": row["id"],
+            "user_id": row["user_id"],
+            "content": row["content"],
+            "memory_type": row["memory_type"],
+            "timestamp": row["timestamp"],
+            "importance": row["importance"],
+            "properties": json.loads(row["properties"]) if row["properties"] else {},
+            "created_at": row["created_at"]
+        }
+    
+    def search_memories(
+        self,
+        user_id: Optional[str] = None,
+        memory_type: Optional[str] = None,
+        text_query: Optional[str] = None,
+        start_time: Optional[int] = None,
+        end_time: Optional[int] = None,
+        importance_threshold: Optional[float] = None,
+        limit: int = 10
+    ) -> List[Dict[str, Any]]:
+        """搜索记忆"""
+        conn = self._get_connection()
+        cursor = conn.cursor()
+        
+        # 构建查询条件
+        where_conditions = []
+        params = []
+        
+        if user_id:
+            where_conditions.append("user_id = ?")
+            params.append(user_id)
+        
+        if memory_type:
+            where_conditions.append("memory_type = ?")
+            params.append(memory_type)
+
+        if text_query:
+            where_conditions.append("content LIKE ?")
+            params.append(f"%{text_query}%")
+        
+        if start_time:
+            where_conditions.append("timestamp >= ?")
+            params.append(start_time)
+        
+        if end_time:
+            where_conditions.append("timestamp <= ?")
+            params.append(end_time)
+        
+        if importance_threshold:
+            where_conditions.append("importance >= ?")
+            params.append(importance_threshold)
+        
+        where_clause = ""
+        if where_conditions:
+            where_clause = "WHERE " + " AND ".join(where_conditions)
+        
+        cursor.execute(f"""
+            SELECT id, user_id, content, memory_type, timestamp, importance, properties, created_at
+            FROM memories
+            {where_clause}
+            ORDER BY importance DESC, timestamp DESC
+            LIMIT ?
+        """, params + [limit])
+        
+        memories = []
+        for row in cursor.fetchall():
+            memories.append({
+                "memory_id": row["id"],
+                "user_id": row["user_id"],
+                "content": row["content"],
+                "memory_type": row["memory_type"],
+                "timestamp": row["timestamp"],
+                "importance": row["importance"],
+                "properties": json.loads(row["properties"]) if row["properties"] else {},
+                "created_at": row["created_at"]
+            })
+        
+        return memories
+    
+    def update_memory(
+        self,
+        memory_id: str,
+        content: str = None,
+        importance: float = None,
+        properties: Dict[str, Any] = None
+    ) -> bool:
+        """更新记忆"""
+        conn = self._get_connection()
+        cursor = conn.cursor()
+        
+        # 构建更新字段
+        update_fields = []
+        params = []
+        
+        if content is not None:
+            update_fields.append("content = ?")
+            params.append(content)
+        
+        if importance is not None:
+            update_fields.append("importance = ?")
+            params.append(importance)
+        
+        if properties is not None:
+            update_fields.append("properties = ?")
+            params.append(json.dumps(properties))
+        
+        if not update_fields:
+            return False
+        
+        update_fields.append("updated_at = CURRENT_TIMESTAMP")
+        params.append(memory_id)
+        
+        cursor.execute(f"""
+            UPDATE memories
+            SET {', '.join(update_fields)}
+            WHERE id = ?
+        """, params)
+        
+        conn.commit()
+        return cursor.rowcount > 0
+    
+    def delete_memory(self, memory_id: str) -> bool:
+        """删除记忆"""
+        conn = self._get_connection()
+        cursor = conn.cursor()
+        
+        cursor.execute("DELETE FROM memories WHERE id = ?", (memory_id,))
+        deleted_count = cursor.rowcount
+        
+        conn.commit()
+        return deleted_count > 0
+    
+    def get_database_stats(self) -> Dict[str, Any]:
+        """获取数据库统计信息"""
+        conn = self._get_connection()
+        cursor = conn.cursor()
+        
+        stats = {}
+        
+        # 统计各表的记录数
+        tables = ["users", "memories", "concepts", "memory_concepts", "concept_relationships"]
+        for table in tables:
+            cursor.execute(f"SELECT COUNT(*) as count FROM {table}")
+            stats[f"{table}_count"] = cursor.fetchone()["count"]
+        
+        # 统计记忆类型分布
+        cursor.execute("""
+            SELECT memory_type, COUNT(*) as count
+            FROM memories
+            GROUP BY memory_type
+        """)
+        memory_types = {}
+        for row in cursor.fetchall():
+            memory_types[row["memory_type"]] = row["count"]
+        stats["memory_types"] = memory_types
+        
+        # 统计用户分布
+        cursor.execute("""
+            SELECT user_id, COUNT(*) as count
+            FROM memories
+            GROUP BY user_id
+            ORDER BY count DESC
+            LIMIT 10
+        """)
+        top_users = {}
+        for row in cursor.fetchall():
+            top_users[row["user_id"]] = row["count"]
+        stats["top_users"] = top_users
+        
+        stats["store_type"] = "sqlite"
+        stats["db_path"] = self.db_path
+        
+        return stats
+    
+    def add_document(self, content: str, metadata: Dict[str, Any] = None) -> str:
+        """添加文档"""
+        import uuid
+        import time
+        
+        doc_id = str(uuid.uuid4())
+        user_id = metadata.get("user_id", "system") if metadata else "system"
+        
+        return self.add_memory(
+            memory_id=doc_id,
+            user_id=user_id,
+            content=content,
+            memory_type="document",
+            timestamp=int(time.time()),
+            importance=0.5,
+            properties=metadata or {}
+        )
+    
+    def get_document(self, document_id: str) -> Optional[Dict[str, Any]]:
+        """获取文档"""
+        return self.get_memory(document_id)
+
+    def close(self):
+        """关闭数据库连接"""
+        if hasattr(self.local, 'connection'):
+            self.local.connection.close()
+            delattr(self.local, 'connection')
+            print("[OK] SQLite 连接已关闭")

+ 455 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/storage/neo4j_store.py

@@ -0,0 +1,455 @@
+"""
+Neo4j图数据库存储实现
+"""
+
+import logging
+from typing import Dict, List, Optional, Any, Tuple
+from datetime import datetime
+
+try:
+    from neo4j import GraphDatabase
+    from neo4j.exceptions import ServiceUnavailable, AuthError
+    NEO4J_AVAILABLE = True
+except ImportError:
+    NEO4J_AVAILABLE = False
+    GraphDatabase = None
+
+logger = logging.getLogger(__name__)
+
+class Neo4jGraphStore:
+    """Neo4j图数据库存储实现"""
+    
+    def __init__(
+        self,
+        uri: str = "bolt://localhost:7687",
+        username: str = "neo4j", 
+        password: str = "hello-agents-password",
+        database: str = "neo4j",
+        max_connection_lifetime: int = 3600,
+        max_connection_pool_size: int = 50,
+        connection_acquisition_timeout: int = 60,
+        **kwargs
+    ):
+        """
+        初始化Neo4j图存储 (支持云API)
+        
+        Args:
+            uri: Neo4j连接URI (本地: bolt://localhost:7687, 云: neo4j+s://xxx.databases.neo4j.io)
+            username: 用户名
+            password: 密码
+            database: 数据库名称
+            max_connection_lifetime: 最大连接生命周期(秒)
+            max_connection_pool_size: 最大连接池大小
+            connection_acquisition_timeout: 连接获取超时(秒)
+        """
+        if not NEO4J_AVAILABLE:
+            raise ImportError(
+                "neo4j未安装。请运行: pip install neo4j>=5.0.0"
+            )
+        
+        self.uri = uri
+        self.username = username
+        self.password = password
+        self.database = database
+        
+        # 初始化驱动
+        self.driver = None
+        self._initialize_driver(
+            max_connection_lifetime=max_connection_lifetime,
+            max_connection_pool_size=max_connection_pool_size,
+            connection_acquisition_timeout=connection_acquisition_timeout
+        )
+        
+        # 创建索引
+        self._create_indexes()
+    
+    def _initialize_driver(self, **config):
+        """初始化Neo4j驱动"""
+        try:
+            self.driver = GraphDatabase.driver(
+                self.uri,
+                auth=(self.username, self.password),
+                **config
+            )
+            
+            # 验证连接
+            self.driver.verify_connectivity()
+            
+            # 检查是否是云服务
+            if "neo4j.io" in self.uri or "aura" in self.uri.lower():
+                logger.info(f"✅ 成功连接到Neo4j云服务: {self.uri}")
+            else:
+                logger.info(f"✅ 成功连接到Neo4j服务: {self.uri}")
+                
+        except AuthError as e:
+            logger.error(f"❌ Neo4j认证失败: {e}")
+            logger.info("💡 请检查用户名和密码是否正确")
+            raise
+        except ServiceUnavailable as e:
+            logger.error(f"❌ Neo4j服务不可用: {e}")
+            if "localhost" in self.uri:
+                logger.info("💡 本地连接失败,可以考虑使用Neo4j Aura云服务")
+                logger.info("💡 或启动本地服务: docker run -p 7474:7474 -p 7687:7687 neo4j:5.14")
+            else:
+                logger.info("💡 请检查URL和网络连接")
+            raise
+        except Exception as e:
+            logger.error(f"❌ Neo4j连接失败: {e}")
+            raise
+    
+    def _create_indexes(self):
+        """创建必要的索引以提高查询性能"""
+        indexes = [
+            # 实体索引
+            "CREATE INDEX entity_id_index IF NOT EXISTS FOR (e:Entity) ON (e.id)",
+            "CREATE INDEX entity_name_index IF NOT EXISTS FOR (e:Entity) ON (e.name)",
+            "CREATE INDEX entity_type_index IF NOT EXISTS FOR (e:Entity) ON (e.type)",
+            
+            # 记忆索引
+            "CREATE INDEX memory_id_index IF NOT EXISTS FOR (m:Memory) ON (m.id)",
+            "CREATE INDEX memory_type_index IF NOT EXISTS FOR (m:Memory) ON (m.memory_type)",
+            "CREATE INDEX memory_timestamp_index IF NOT EXISTS FOR (m:Memory) ON (m.timestamp)",
+        ]
+        
+        with self.driver.session(database=self.database) as session:
+            for index_query in indexes:
+                try:
+                    session.run(index_query)
+                except Exception as e:
+                    logger.debug(f"索引创建跳过 (可能已存在): {e}")
+        
+        logger.info("✅ Neo4j索引创建完成")
+    
+    def add_entity(self, entity_id: str, name: str, entity_type: str, properties: Dict[str, Any] = None) -> bool:
+        """
+        添加实体节点
+        
+        Args:
+            entity_id: 实体ID
+            name: 实体名称
+            entity_type: 实体类型
+            properties: 附加属性
+        
+        Returns:
+            bool: 是否成功
+        """
+        try:
+            props = properties or {}
+            props.update({
+                "id": entity_id,
+                "name": name,
+                "type": entity_type,
+                "created_at": datetime.now().isoformat(),
+                "updated_at": datetime.now().isoformat()
+            })
+            
+            query = """
+            MERGE (e:Entity {id: $entity_id})
+            SET e += $properties
+            RETURN e
+            """
+            
+            with self.driver.session(database=self.database) as session:
+                result = session.run(query, entity_id=entity_id, properties=props)
+                record = result.single()
+                
+                if record:
+                    logger.debug(f"✅ 添加实体: {name} ({entity_type})")
+                    return True
+                return False
+                
+        except Exception as e:
+            logger.error(f"❌ 添加实体失败: {e}")
+            return False
+    
+    def add_relationship(
+        self, 
+        from_entity_id: str, 
+        to_entity_id: str, 
+        relationship_type: str,
+        properties: Dict[str, Any] = None
+    ) -> bool:
+        """
+        添加实体间关系
+        
+        Args:
+            from_entity_id: 源实体ID
+            to_entity_id: 目标实体ID  
+            relationship_type: 关系类型
+            properties: 关系属性
+        
+        Returns:
+            bool: 是否成功
+        """
+        try:
+            props = properties or {}
+            props.update({
+                "type": relationship_type,
+                "created_at": datetime.now().isoformat(),
+                "updated_at": datetime.now().isoformat()
+            })
+            
+            query = f"""
+            MATCH (from:Entity {{id: $from_id}})
+            MATCH (to:Entity {{id: $to_id}})
+            MERGE (from)-[r:{relationship_type}]->(to)
+            SET r += $properties
+            RETURN r
+            """
+            
+            with self.driver.session(database=self.database) as session:
+                result = session.run(
+                    query,
+                    from_id=from_entity_id,
+                    to_id=to_entity_id,
+                    properties=props
+                )
+                record = result.single()
+                
+                if record:
+                    logger.debug(f"✅ 添加关系: {from_entity_id} -{relationship_type}-> {to_entity_id}")
+                    return True
+                return False
+                
+        except Exception as e:
+            logger.error(f"❌ 添加关系失败: {e}")
+            return False
+    
+    def find_related_entities(
+        self, 
+        entity_id: str, 
+        relationship_types: List[str] = None,
+        max_depth: int = 2,
+        limit: int = 50
+    ) -> List[Dict[str, Any]]:
+        """
+        查找相关实体
+        
+        Args:
+            entity_id: 起始实体ID
+            relationship_types: 关系类型过滤
+            max_depth: 最大搜索深度
+            limit: 结果限制
+        
+        Returns:
+            List[Dict]: 相关实体列表
+        """
+        try:
+            # 构建关系类型过滤
+            rel_filter = ""
+            if relationship_types:
+                rel_types = "|".join(relationship_types)
+                rel_filter = f":{rel_types}"
+            
+            query = f"""
+            MATCH path = (start:Entity {{id: $entity_id}})-[r{rel_filter}*1..{max_depth}]-(related:Entity)
+            WHERE start.id <> related.id
+            RETURN DISTINCT related, 
+                   length(path) as distance,
+                   [rel in relationships(path) | type(rel)] as relationship_path
+            ORDER BY distance, related.name
+            LIMIT $limit
+            """
+            
+            with self.driver.session(database=self.database) as session:
+                result = session.run(query, entity_id=entity_id, limit=limit)
+                
+                entities = []
+                for record in result:
+                    entity_data = dict(record["related"])
+                    entity_data["distance"] = record["distance"]
+                    entity_data["relationship_path"] = record["relationship_path"]
+                    entities.append(entity_data)
+                
+                logger.debug(f"🔍 找到 {len(entities)} 个相关实体")
+                return entities
+                
+        except Exception as e:
+            logger.error(f"❌ 查找相关实体失败: {e}")
+            return []
+    
+    def search_entities_by_name(self, name_pattern: str, entity_types: List[str] = None, limit: int = 20) -> List[Dict[str, Any]]:
+        """
+        按名称搜索实体
+        
+        Args:
+            name_pattern: 名称模式 (支持部分匹配)
+            entity_types: 实体类型过滤
+            limit: 结果限制
+        
+        Returns:
+            List[Dict]: 匹配的实体列表
+        """
+        try:
+            # 构建类型过滤
+            type_filter = ""
+            params = {"pattern": f".*{name_pattern}.*", "limit": limit}
+            
+            if entity_types:
+                type_filter = "AND e.type IN $types"
+                params["types"] = entity_types
+            
+            query = f"""
+            MATCH (e:Entity)
+            WHERE e.name =~ $pattern {type_filter}
+            RETURN e
+            ORDER BY e.name
+            LIMIT $limit
+            """
+            
+            with self.driver.session(database=self.database) as session:
+                result = session.run(query, **params)
+                
+                entities = []
+                for record in result:
+                    entity_data = dict(record["e"])
+                    entities.append(entity_data)
+                
+                logger.debug(f"🔍 按名称搜索到 {len(entities)} 个实体")
+                return entities
+                
+        except Exception as e:
+            logger.error(f"❌ 按名称搜索实体失败: {e}")
+            return []
+    
+    def get_entity_relationships(self, entity_id: str) -> List[Dict[str, Any]]:
+        """
+        获取实体的所有关系
+        
+        Args:
+            entity_id: 实体ID
+        
+        Returns:
+            List[Dict]: 关系列表
+        """
+        try:
+            query = """
+            MATCH (e:Entity {id: $entity_id})-[r]-(other:Entity)
+            RETURN r, other, 
+                   CASE WHEN startNode(r).id = $entity_id THEN 'outgoing' ELSE 'incoming' END as direction
+            """
+            
+            with self.driver.session(database=self.database) as session:
+                result = session.run(query, entity_id=entity_id)
+                
+                relationships = []
+                for record in result:
+                    rel_data = dict(record["r"])
+                    other_data = dict(record["other"])
+                    
+                    relationship = {
+                        "relationship": rel_data,
+                        "other_entity": other_data,
+                        "direction": record["direction"]
+                    }
+                    relationships.append(relationship)
+                
+                return relationships
+                
+        except Exception as e:
+            logger.error(f"❌ 获取实体关系失败: {e}")
+            return []
+    
+    def delete_entity(self, entity_id: str) -> bool:
+        """
+        删除实体及其所有关系
+        
+        Args:
+            entity_id: 实体ID
+        
+        Returns:
+            bool: 是否成功
+        """
+        try:
+            query = """
+            MATCH (e:Entity {id: $entity_id})
+            DETACH DELETE e
+            """
+            
+            with self.driver.session(database=self.database) as session:
+                result = session.run(query, entity_id=entity_id)
+                summary = result.consume()
+                
+                deleted_count = summary.counters.nodes_deleted
+                logger.info(f"✅ 删除实体: {entity_id} (删除 {deleted_count} 个节点)")
+                return deleted_count > 0
+                
+        except Exception as e:
+            logger.error(f"❌ 删除实体失败: {e}")
+            return False
+    
+    def clear_all(self) -> bool:
+        """
+        清空所有数据
+        
+        Returns:
+            bool: 是否成功
+        """
+        try:
+            query = "MATCH (n) DETACH DELETE n"
+            
+            with self.driver.session(database=self.database) as session:
+                result = session.run(query)
+                summary = result.consume()
+                
+                deleted_nodes = summary.counters.nodes_deleted
+                deleted_relationships = summary.counters.relationships_deleted
+                
+                logger.info(f"✅ 清空Neo4j数据库: 删除 {deleted_nodes} 个节点, {deleted_relationships} 个关系")
+                return True
+                
+        except Exception as e:
+            logger.error(f"❌ 清空数据库失败: {e}")
+            return False
+    
+    def get_stats(self) -> Dict[str, Any]:
+        """
+        获取图数据库统计信息
+        
+        Returns:
+            Dict: 统计信息
+        """
+        try:
+            queries = {
+                "total_nodes": "MATCH (n) RETURN count(n) as count",
+                "total_relationships": "MATCH ()-[r]->() RETURN count(r) as count",
+                "entity_nodes": "MATCH (n:Entity) RETURN count(n) as count",
+                "memory_nodes": "MATCH (n:Memory) RETURN count(n) as count",
+            }
+            
+            stats = {}
+            with self.driver.session(database=self.database) as session:
+                for key, query in queries.items():
+                    result = session.run(query)
+                    record = result.single()
+                    stats[key] = record["count"] if record else 0
+            
+            return stats
+            
+        except Exception as e:
+            logger.error(f"❌ 获取统计信息失败: {e}")
+            return {}
+    
+    def health_check(self) -> bool:
+        """
+        健康检查
+        
+        Returns:
+            bool: 服务是否健康
+        """
+        try:
+            with self.driver.session(database=self.database) as session:
+                result = session.run("RETURN 1 as health")
+                record = result.single()
+                return record["health"] == 1
+        except Exception as e:
+            logger.error(f"❌ Neo4j健康检查失败: {e}")
+            return False
+    
+    def __del__(self):
+        """析构函数,清理资源"""
+        if hasattr(self, 'driver') and self.driver:
+            try:
+                self.driver.close()
+            except:
+                pass

+ 541 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/storage/qdrant_store.py

@@ -0,0 +1,541 @@
+"""
+Qdrant向量数据库存储实现
+使用专业的Qdrant向量数据库替代ChromaDB
+"""
+
+import logging
+import os
+import uuid
+import threading
+from typing import Dict, List, Optional, Any, Union
+import numpy as np
+from datetime import datetime
+
+try:
+    from qdrant_client import QdrantClient
+    from qdrant_client.http import models
+    from qdrant_client.http.models import (
+        Distance, VectorParams, PointStruct, 
+        Filter, FieldCondition, MatchValue, SearchRequest
+    )
+    QDRANT_AVAILABLE = True
+except ImportError:
+    QDRANT_AVAILABLE = False
+    QdrantClient = None
+    models = None
+
+logger = logging.getLogger(__name__)
+
+class QdrantConnectionManager:
+    """Qdrant连接管理器 - 防止重复连接和初始化"""
+    _instances = {}  # key: (url, collection_name) -> QdrantVectorStore instance
+    _lock = threading.Lock()
+    
+    @classmethod
+    def get_instance(
+        cls, 
+        url: Optional[str] = None,
+        api_key: Optional[str] = None,
+        collection_name: str = "hello_agents_vectors",
+        vector_size: int = 384,
+        distance: str = "cosine",
+        timeout: int = 30,
+        **kwargs
+    ) -> 'QdrantVectorStore':
+        """获取或创建Qdrant实例(单例模式)"""
+        # 创建唯一键
+        key = (url or "local", collection_name)
+        
+        if key not in cls._instances:
+            with cls._lock:
+                # 双重检查锁定
+                if key not in cls._instances:
+                    logger.debug(f"🔄 创建新的Qdrant连接: {collection_name}")
+                    cls._instances[key] = QdrantVectorStore(
+                        url=url,
+                        api_key=api_key,
+                        collection_name=collection_name,
+                        vector_size=vector_size,
+                        distance=distance,
+                        timeout=timeout,
+                        **kwargs
+                    )
+                else:
+                    logger.debug(f"♻️ 复用现有Qdrant连接: {collection_name}")
+        else:
+            logger.debug(f"♻️ 复用现有Qdrant连接: {collection_name}")
+            
+        return cls._instances[key]
+
+class QdrantVectorStore:
+    """Qdrant向量数据库存储实现"""
+    
+    def __init__(
+        self, 
+        url: Optional[str] = None,
+        api_key: Optional[str] = None,
+        collection_name: str = "hello_agents_vectors",
+        vector_size: int = 384,
+        distance: str = "cosine",
+        timeout: int = 30,
+        **kwargs
+    ):
+        """
+        初始化Qdrant向量存储 (支持云API)
+        
+        Args:
+            url: Qdrant云服务URL (如果为None则使用本地)
+            api_key: Qdrant云服务API密钥
+            collection_name: 集合名称
+            vector_size: 向量维度
+            distance: 距离度量方式 (cosine, dot, euclidean)
+            timeout: 连接超时时间
+        """
+        if not QDRANT_AVAILABLE:
+            raise ImportError(
+                "qdrant-client未安装。请运行: pip install qdrant-client>=1.6.0"
+            )
+        
+        self.url = url
+        self.api_key = api_key
+        self.collection_name = collection_name
+        self.vector_size = vector_size
+        self.timeout = timeout
+        # HNSW/Query params via env
+        try:
+            self.hnsw_m = int(os.getenv("QDRANT_HNSW_M", "32"))
+        except Exception:
+            self.hnsw_m = 32
+        try:
+            self.hnsw_ef_construct = int(os.getenv("QDRANT_HNSW_EF_CONSTRUCT", "256"))
+        except Exception:
+            self.hnsw_ef_construct = 256
+        try:
+            self.search_ef = int(os.getenv("QDRANT_SEARCH_EF", "128"))
+        except Exception:
+            self.search_ef = 128
+        self.search_exact = os.getenv("QDRANT_SEARCH_EXACT", "0") == "1"
+        
+        # 距离度量映射
+        distance_map = {
+            "cosine": Distance.COSINE,
+            "dot": Distance.DOT,
+            "euclidean": Distance.EUCLID,
+        }
+        self.distance = distance_map.get(distance.lower(), Distance.COSINE)
+        
+        # 初始化客户端
+        self.client = None
+        self._initialize_client()
+        
+    def _initialize_client(self):
+        """初始化Qdrant客户端和集合"""
+        try:
+            # 根据配置创建客户端连接
+            if self.url and self.api_key:
+                # 使用云服务API
+                self.client = QdrantClient(
+                    url=self.url,
+                    api_key=self.api_key,
+                    timeout=self.timeout
+                )
+                logger.info(f"✅ 成功连接到Qdrant云服务: {self.url}")
+            elif self.url:
+                # 使用自定义URL(无API密钥)
+                self.client = QdrantClient(
+                    url=self.url,
+                    timeout=self.timeout
+                )
+                logger.info(f"✅ 成功连接到Qdrant服务: {self.url}")
+            else:
+                # 使用本地服务(默认)
+                self.client = QdrantClient(
+                    host="localhost",
+                    port=6333,
+                    timeout=self.timeout
+                )
+                logger.info("✅ 成功连接到本地Qdrant服务: localhost:6333")
+            
+            # 检查连接
+            collections = self.client.get_collections()
+            
+            # 创建或获取集合
+            self._ensure_collection()
+            
+        except Exception as e:
+            logger.error(f"❌ Qdrant连接失败: {e}")
+            if not self.url:
+                logger.info("💡 本地连接失败,可以考虑使用Qdrant云服务")
+                logger.info("💡 或启动本地服务: docker run -p 6333:6333 qdrant/qdrant")
+            else:
+                logger.info("💡 请检查URL和API密钥是否正确")
+            raise
+    
+    def _ensure_collection(self):
+        """确保集合存在,不存在则创建"""
+        try:
+            # 检查集合是否存在
+            collections = self.client.get_collections().collections
+            collection_names = [c.name for c in collections]
+            
+            if self.collection_name not in collection_names:
+                # 创建新集合
+                hnsw_cfg = None
+                try:
+                    hnsw_cfg = models.HnswConfigDiff(m=self.hnsw_m, ef_construct=self.hnsw_ef_construct)
+                except Exception:
+                    hnsw_cfg = None
+                self.client.create_collection(
+                    collection_name=self.collection_name,
+                    vectors_config=VectorParams(
+                        size=self.vector_size,
+                        distance=self.distance
+                    ),
+                    hnsw_config=hnsw_cfg
+                )
+                logger.info(f"✅ 创建Qdrant集合: {self.collection_name}")
+            else:
+                logger.info(f"✅ 使用现有Qdrant集合: {self.collection_name}")
+                # 尝试更新 HNSW 配置
+                try:
+                    self.client.update_collection(
+                        collection_name=self.collection_name,
+                        hnsw_config=models.HnswConfigDiff(m=self.hnsw_m, ef_construct=self.hnsw_ef_construct)
+                    )
+                except Exception as ie:
+                    logger.debug(f"跳过更新HNSW配置: {ie}")
+            # 确保必要的payload索引
+            self._ensure_payload_indexes()
+                
+        except Exception as e:
+            logger.error(f"❌ 集合初始化失败: {e}")
+            raise
+
+    def _ensure_payload_indexes(self):
+        """为常用过滤字段创建payload索引"""
+        try:
+            index_fields = [
+                ("memory_type", models.PayloadSchemaType.KEYWORD),
+                ("user_id", models.PayloadSchemaType.KEYWORD),
+                ("memory_id", models.PayloadSchemaType.KEYWORD),
+                ("timestamp", models.PayloadSchemaType.INTEGER),
+                ("modality", models.PayloadSchemaType.KEYWORD),  # 感知记忆模态筛选
+                ("source", models.PayloadSchemaType.KEYWORD),
+                ("external", models.PayloadSchemaType.BOOL),
+                ("namespace", models.PayloadSchemaType.KEYWORD),
+                # RAG相关字段索引
+                ("is_rag_data", models.PayloadSchemaType.BOOL),
+                ("rag_namespace", models.PayloadSchemaType.KEYWORD),
+                ("data_source", models.PayloadSchemaType.KEYWORD),
+            ]
+            for field_name, schema_type in index_fields:
+                try:
+                    self.client.create_payload_index(
+                        collection_name=self.collection_name,
+                        field_name=field_name,
+                        field_schema=schema_type,
+                    )
+                except Exception as ie:
+                    # 索引已存在会报错,忽略
+                    logger.debug(f"索引 {field_name} 已存在或创建失败: {ie}")
+        except Exception as e:
+            logger.debug(f"创建payload索引时出错: {e}")
+    
+    def add_vectors(
+        self, 
+        vectors: List[List[float]], 
+        metadata: List[Dict[str, Any]], 
+        ids: Optional[List[str]] = None
+    ) -> bool:
+        """
+        添加向量到Qdrant
+        
+        Args:
+            vectors: 向量列表
+            metadata: 元数据列表
+            ids: 可选的ID列表
+        
+        Returns:
+            bool: 是否成功
+        """
+        try:
+            if not vectors:
+                logger.warning("⚠️ 向量列表为空")
+                return False
+                
+            # 生成ID(如果未提供)
+            if ids is None:
+                ids = [f"vec_{i}_{int(datetime.now().timestamp() * 1000000)}" 
+                       for i in range(len(vectors))]
+            
+            # 构建点数据
+            logger.info(f"[Qdrant] add_vectors start: n_vectors={len(vectors)} n_meta={len(metadata)} collection={self.collection_name}")
+            points = []
+            for i, (vector, meta, point_id) in enumerate(zip(vectors, metadata, ids)):
+                # 确保向量是正确的维度
+                try:
+                    vlen = len(vector)
+                except Exception:
+                    logger.error(f"[Qdrant] 非法向量类型: index={i} type={type(vector)} value={vector}")
+                    continue
+                if vlen != self.vector_size:
+                    logger.warning(f"⚠️ 向量维度不匹配: 期望{self.vector_size}, 实际{len(vector)}")
+                    continue
+                    
+                # 添加时间戳到元数据
+                meta_with_timestamp = meta.copy()
+                meta_with_timestamp["timestamp"] = int(datetime.now().timestamp())
+                meta_with_timestamp["added_at"] = int(datetime.now().timestamp())
+                if "external" in meta_with_timestamp and not isinstance(meta_with_timestamp.get("external"), bool):
+                    # normalize to bool
+                    val = meta_with_timestamp.get("external")
+                    meta_with_timestamp["external"] = True if str(val).lower() in ("1", "true", "yes") else False
+                # 确保点ID是Qdrant接受的类型(无符号整数或UUID字符串)
+                safe_id: Any
+                if isinstance(point_id, int):
+                    safe_id = point_id
+                elif isinstance(point_id, str):
+                    try:
+                        uuid.UUID(point_id)
+                        safe_id = point_id
+                    except Exception:
+                        safe_id = str(uuid.uuid4())
+                else:
+                    safe_id = str(uuid.uuid4())
+
+                point = PointStruct(
+                    id=safe_id,
+                    vector=vector,
+                    payload=meta_with_timestamp
+                )
+                points.append(point)
+            
+            if not points:
+                logger.warning("⚠️ 没有有效的向量点")
+                return False
+            
+            # 批量插入
+            logger.info(f"[Qdrant] upsert begin: points={len(points)}")
+            operation_info = self.client.upsert(
+                collection_name=self.collection_name,
+                points=points,
+                wait=True
+            )
+            logger.info("[Qdrant] upsert done")
+            
+            logger.info(f"✅ 成功添加 {len(points)} 个向量到Qdrant")
+            return True
+            
+        except Exception as e:
+            logger.error(f"❌ 添加向量失败: {e}")
+            return False
+    
+    def search_similar(
+        self, 
+        query_vector: List[float], 
+        limit: int = 10, 
+        score_threshold: Optional[float] = None,
+        where: Optional[Dict[str, Any]] = None
+    ) -> List[Dict[str, Any]]:
+        """
+        搜索相似向量
+        
+        Args:
+            query_vector: 查询向量
+            limit: 返回结果数量限制
+            score_threshold: 相似度阈值
+            where: 过滤条件
+        
+        Returns:
+            List[Dict]: 搜索结果
+        """
+        try:
+            if len(query_vector) != self.vector_size:
+                logger.error(f"❌ 查询向量维度错误: 期望{self.vector_size}, 实际{len(query_vector)}")
+                return []
+            
+            # 构建过滤器
+            query_filter = None
+            if where:
+                conditions = []
+                for key, value in where.items():
+                    if isinstance(value, (str, int, float, bool)):
+                        conditions.append(
+                            FieldCondition(
+                                key=key,
+                                match=MatchValue(value=value)
+                            )
+                        )
+                
+                if conditions:
+                    query_filter = Filter(must=conditions)
+            
+            # 执行搜索
+            # 搜索参数
+            search_params = None
+            try:
+                search_params = models.SearchParams(hnsw_ef=self.search_ef, exact=self.search_exact)
+            except Exception:
+                search_params = None
+            search_result = self.client.search(
+                collection_name=self.collection_name,
+                query_vector=query_vector,
+                query_filter=query_filter,
+                limit=limit,
+                score_threshold=score_threshold,
+                with_payload=True,
+                with_vectors=False,
+                search_params=search_params
+            )
+            
+            # 转换结果格式
+            results = []
+            for hit in search_result:
+                result = {
+                    "id": hit.id,
+                    "score": hit.score,
+                    "metadata": hit.payload or {}
+                }
+                results.append(result)
+            
+            logger.debug(f"🔍 Qdrant搜索返回 {len(results)} 个结果")
+            return results
+            
+        except Exception as e:
+            logger.error(f"❌ 向量搜索失败: {e}")
+            return []
+    
+    def delete_vectors(self, ids: List[str]) -> bool:
+        """
+        删除向量
+        
+        Args:
+            ids: 要删除的向量ID列表
+        
+        Returns:
+            bool: 是否成功
+        """
+        try:
+            if not ids:
+                return True
+                
+            operation_info = self.client.delete(
+                collection_name=self.collection_name,
+                points_selector=models.PointIdsList(
+                    points=ids
+                ),
+                wait=True
+            )
+            
+            logger.info(f"✅ 成功删除 {len(ids)} 个向量")
+            return True
+            
+        except Exception as e:
+            logger.error(f"❌ 删除向量失败: {e}")
+            return False
+    
+    def clear_collection(self) -> bool:
+        """
+        清空集合
+        
+        Returns:
+            bool: 是否成功
+        """
+        try:
+            # 删除并重新创建集合
+            self.client.delete_collection(collection_name=self.collection_name)
+            self._ensure_collection()
+            
+            logger.info(f"✅ 成功清空Qdrant集合: {self.collection_name}")
+            return True
+            
+        except Exception as e:
+            logger.error(f"❌ 清空集合失败: {e}")
+            return False
+    
+    def delete_memories(self, memory_ids: List[str]):
+        """
+        删除指定记忆(通过payload中的 memory_id 过滤删除)
+        
+        注意:由于写入时可能将非UUID的点ID转换为UUID,这里不再依赖点ID,
+        而是通过payload中的memory_id来匹配删除,确保一致性。
+        """
+        try:
+            if not memory_ids:
+                return
+            # 构建 should 过滤条件:memory_id 等于任一给定值
+            conditions = [
+                FieldCondition(key="memory_id", match=MatchValue(value=mid))
+                for mid in memory_ids
+            ]
+            query_filter = Filter(should=conditions)
+            self.client.delete(
+                collection_name=self.collection_name,
+                points_selector=models.FilterSelector(filter=query_filter),
+                wait=True,
+            )
+            logger.info(f"✅ 成功按memory_id删除 {len(memory_ids)} 个Qdrant向量")
+        except Exception as e:
+            logger.error(f"❌ 删除记忆失败: {e}")
+            raise
+    
+    def get_collection_info(self) -> Dict[str, Any]:
+        """
+        获取集合信息
+        
+        Returns:
+            Dict: 集合信息
+        """
+        try:
+            collection_info = self.client.get_collection(self.collection_name)
+            
+            info = {
+                "name": self.collection_name,
+                "vectors_count": collection_info.vectors_count,
+                "indexed_vectors_count": collection_info.indexed_vectors_count,
+                "points_count": collection_info.points_count,
+                "segments_count": collection_info.segments_count,
+                "config": {
+                    "vector_size": self.vector_size,
+                    "distance": self.distance.value,
+                }
+            }
+            
+            return info
+            
+        except Exception as e:
+            logger.error(f"❌ 获取集合信息失败: {e}")
+            return {}
+    
+    def get_collection_stats(self) -> Dict[str, Any]:
+        """
+        获取集合统计信息(兼容抽象接口)
+        """
+        info = self.get_collection_info()
+        if not info:
+            return {"store_type": "qdrant", "name": self.collection_name}
+        info["store_type"] = "qdrant"
+        return info
+    
+    def health_check(self) -> bool:
+        """
+        健康检查
+        
+        Returns:
+            bool: 服务是否健康
+        """
+        try:
+            # 尝试获取集合列表
+            collections = self.client.get_collections()
+            return True
+        except Exception as e:
+            logger.error(f"❌ Qdrant健康检查失败: {e}")
+            return False
+    
+    def __del__(self):
+        """析构函数,清理资源"""
+        if hasattr(self, 'client') and self.client:
+            try:
+                self.client.close()
+            except:
+                pass

+ 27 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/types/__init__.py

@@ -0,0 +1,27 @@
+"""记忆类型层模块
+
+按照第8章架构设计的记忆类型层:
+- WorkingMemory: 工作记忆 - 短期上下文管理
+- EpisodicMemory: 情景记忆 - 具体交互事件存储
+- SemanticMemory: 语义记忆 - 抽象知识和概念存储
+- PerceptualMemory: 感知记忆 - 多模态数据存储
+"""
+
+from .working import WorkingMemory
+from .episodic import EpisodicMemory, Episode
+from .semantic import SemanticMemory, Entity, Relation
+from .perceptual import PerceptualMemory, Perception
+
+__all__ = [
+    # 记忆类型
+    "WorkingMemory",
+    "EpisodicMemory",
+    "SemanticMemory",
+    "PerceptualMemory",
+
+    # 辅助类
+    "Episode",
+    "Entity",
+    "Relation",
+    "Perception"
+]

+ 661 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/types/episodic.py

@@ -0,0 +1,661 @@
+"""情景记忆实现
+
+按照第8章架构设计的情景记忆,提供:
+- 具体交互事件存储
+- 时间序列组织
+- 上下文丰富的记忆
+- 模式识别能力
+"""
+
+from typing import List, Dict, Any, Optional, Tuple
+from datetime import datetime, timedelta
+import os
+import math
+import json
+import logging
+
+logger = logging.getLogger(__name__)
+
+from ..base import BaseMemory, MemoryItem, MemoryConfig
+from ..storage import SQLiteDocumentStore, QdrantVectorStore
+from ..embedding import get_text_embedder, get_dimension
+
+class Episode:
+    """情景记忆中的单个情景"""
+    
+    def __init__(
+        self,
+        episode_id: str,
+        user_id: str,
+        session_id: str,
+        timestamp: datetime,
+        content: str,
+        context: Dict[str, Any],
+        outcome: Optional[str] = None,
+        importance: float = 0.5
+    ):
+        self.episode_id = episode_id
+        self.user_id = user_id
+        self.session_id = session_id
+        self.timestamp = timestamp
+        self.content = content
+        self.context = context
+        self.outcome = outcome
+        self.importance = importance
+
+class EpisodicMemory(BaseMemory):
+    """情景记忆实现
+    
+    特点:
+    - 存储具体的交互事件
+    - 包含丰富的上下文信息
+    - 按时间序列组织
+    - 支持模式识别和回溯
+    """
+    
+    def __init__(self, config: MemoryConfig, storage_backend=None):
+        super().__init__(config, storage_backend)
+        
+        # 本地缓存(内存)
+        self.episodes: List[Episode] = []
+        self.sessions: Dict[str, List[str]] = {}  # session_id -> episode_ids
+        
+        # 模式识别缓存
+        self.patterns_cache = {}
+        self.last_pattern_analysis = None
+
+        # 权威文档存储(SQLite)
+        db_dir = self.config.storage_path if hasattr(self.config, 'storage_path') else "./memory_data"
+        os.makedirs(db_dir, exist_ok=True)
+        db_path = os.path.join(db_dir, "memory.db")
+        self.doc_store = SQLiteDocumentStore(db_path=db_path)
+
+        self.embedder = None
+        self.vector_store = None
+
+        disable_vec = bool(getattr(self.config, "disable_vector_store", False))
+        disable_emb = bool(getattr(self.config, "disable_embeddings", False))
+
+        # 统一嵌入模型 / 向量存储(可关闭:只用 SQLite + 文本检索)
+        if not disable_emb:
+            try:
+                self.embedder = get_text_embedder()
+            except Exception:
+                self.embedder = None
+
+        if not disable_vec and self.embedder is not None:
+            try:
+                from ..storage.qdrant_store import QdrantConnectionManager
+
+                qdrant_url = os.getenv("QDRANT_URL")
+                qdrant_api_key = os.getenv("QDRANT_API_KEY")
+                self.vector_store = QdrantConnectionManager.get_instance(
+                    url=qdrant_url,
+                    api_key=qdrant_api_key,
+                    collection_name=os.getenv("QDRANT_COLLECTION", "hello_agents_vectors"),
+                    vector_size=get_dimension(getattr(self.embedder, "dimension", 384)),
+                    distance=os.getenv("QDRANT_DISTANCE", "cosine"),
+                )
+            except Exception:
+                self.vector_store = None
+    
+    def add(self, memory_item: MemoryItem) -> str:
+        """添加情景记忆"""
+        # 从元数据中提取情景信息
+        session_id = memory_item.metadata.get("session_id", "default_session")
+        context = memory_item.metadata.get("context", {})
+        outcome = memory_item.metadata.get("outcome")
+        participants = memory_item.metadata.get("participants", [])
+        tags = memory_item.metadata.get("tags", [])
+        
+        # 创建情景(内存缓存)
+        episode = Episode(
+            episode_id=memory_item.id,
+            user_id=memory_item.user_id,
+            session_id=session_id,
+            timestamp=memory_item.timestamp,
+            content=memory_item.content,
+            context=context,
+            outcome=outcome,
+            importance=memory_item.importance
+        )
+        self.episodes.append(episode)
+        if session_id not in self.sessions:
+            self.sessions[session_id] = []
+        self.sessions[session_id].append(episode.episode_id)
+
+        # 1) 权威存储(SQLite)
+        ts_int = int(memory_item.timestamp.timestamp())
+        self.doc_store.add_memory(
+            memory_id=memory_item.id,
+            user_id=memory_item.user_id,
+            content=memory_item.content,
+            memory_type="episodic",
+            timestamp=ts_int,
+            importance=memory_item.importance,
+            properties={
+                "session_id": session_id,
+                "context": context,
+                "outcome": outcome,
+                "participants": participants,
+                "tags": tags
+            }
+        )
+
+        # 2) 向量索引(Qdrant)
+        try:
+            embedding = self.embedder.encode(memory_item.content)
+            if hasattr(embedding, "tolist"):
+                embedding = embedding.tolist()
+            self.vector_store.add_vectors(
+                vectors=[embedding],
+                metadata=[{
+                    "memory_id": memory_item.id,
+                    "user_id": memory_item.user_id,
+                    "memory_type": "episodic",
+                    "importance": memory_item.importance,
+                    "session_id": session_id,
+                    "content": memory_item.content
+                }],
+                ids=[memory_item.id]
+            )
+        except Exception:
+            # 向量入库失败不影响权威存储
+            pass
+
+        return memory_item.id
+    
+    def retrieve(self, query: str, limit: int = 5, **kwargs) -> List[MemoryItem]:
+        """检索情景记忆(SQLite 结构化检索 + 可选向量检索)"""
+        user_id = kwargs.get("user_id")
+        session_id = kwargs.get("session_id")
+        time_range: Optional[Tuple[datetime, datetime]] = kwargs.get("time_range")
+        importance_threshold: Optional[float] = kwargs.get("importance_threshold")
+
+        # 结构化过滤候选(来自权威库)
+        candidate_ids: Optional[set] = None
+        if time_range is not None or importance_threshold is not None:
+            start_ts = int(time_range[0].timestamp()) if time_range else None
+            end_ts = int(time_range[1].timestamp()) if time_range else None
+            docs = self.doc_store.search_memories(
+                user_id=user_id,
+                memory_type="episodic",
+                start_time=start_ts,
+                end_time=end_ts,
+                importance_threshold=importance_threshold,
+                limit=1000
+            )
+            candidate_ids = {d["memory_id"] for d in docs}
+
+        # 若禁用向量检索(或不可用),直接走 SQLite 文本检索(持久化可用)
+        if self.embedder is None or self.vector_store is None:
+            docs = self.doc_store.search_memories(
+                user_id=user_id,
+                memory_type="episodic",
+                text_query=query,
+                start_time=int(time_range[0].timestamp()) if time_range else None,
+                end_time=int(time_range[1].timestamp()) if time_range else None,
+                importance_threshold=importance_threshold,
+                limit=max(limit * 5, 20),
+            )
+            out: List[Tuple[float, MemoryItem]] = []
+            now_ts = int(datetime.now().timestamp())
+            for doc in docs:
+                if candidate_ids is not None and doc["memory_id"] not in candidate_ids:
+                    continue
+                if session_id and (doc.get("properties", {}) or {}).get("session_id") != session_id:
+                    continue
+                age_days = max(0.0, (now_ts - int(doc["timestamp"])) / 86400.0)
+                recency_score = 1.0 / (1.0 + age_days)
+                imp = float(doc.get("importance", 0.5))
+                # 文本检索回退:相似度弱,主要依赖重要性与近因
+                base_relevance = 0.4 * 0.8 + recency_score * 0.2
+                importance_weight = 0.8 + (imp * 0.4)
+                combined = base_relevance * importance_weight
+                out.append(
+                    (
+                        combined,
+                        MemoryItem(
+                            id=doc["memory_id"],
+                            content=doc["content"],
+                            memory_type=doc["memory_type"],
+                            user_id=doc["user_id"],
+                            timestamp=datetime.fromtimestamp(doc["timestamp"]),
+                            importance=doc.get("importance", 0.5),
+                            metadata={
+                                **(doc.get("properties", {}) or {}),
+                                "relevance_score": combined,
+                                "recency_score": recency_score,
+                                "retrieval": "sqlite_like",
+                            },
+                        ),
+                    )
+                )
+            out.sort(key=lambda x: x[0], reverse=True)
+            return [it for _, it in out[:limit]]
+
+        hits = []
+        # 向量检索(Qdrant):可选
+        if self.embedder is not None and self.vector_store is not None:
+            try:
+                query_vec = self.embedder.encode(query)
+                if hasattr(query_vec, "tolist"):
+                    query_vec = query_vec.tolist()
+                where = {"memory_type": "episodic"}
+                if user_id:
+                    where["user_id"] = user_id
+                hits = self.vector_store.search_similar(
+                    query_vector=query_vec,
+                    limit=max(limit * 5, 20),
+                    where=where,
+                )
+            except Exception:
+                hits = []
+
+        # 过滤与重排
+        now_ts = int(datetime.now().timestamp())
+        results: List[Tuple[float, MemoryItem]] = []
+        seen = set()
+        for hit in hits:
+            meta = hit.get("metadata", {})
+            mem_id = meta.get("memory_id")
+            if not mem_id or mem_id in seen:
+                continue
+            
+            # 检查是否已遗忘
+            episode = next((e for e in self.episodes if e.episode_id == mem_id), None)
+            if episode and episode.context.get("forgotten", False):
+                continue  # 跳过已遗忘的记忆
+                
+            if candidate_ids is not None and mem_id not in candidate_ids:
+                continue
+            if session_id and meta.get("session_id") != session_id:
+                continue
+
+            # 从权威库读取完整记录
+            doc = self.doc_store.get_memory(mem_id)
+            if not doc:
+                continue
+
+            # 计算综合分数:向量0.6 + 近因0.2 + 重要性0.2
+            vec_score = float(hit.get("score", 0.0))
+            age_days = max(0.0, (now_ts - int(doc["timestamp"])) / 86400.0)
+            recency_score = 1.0 / (1.0 + age_days)
+            imp = float(doc.get("importance", 0.5))
+            
+            # 新评分算法:向量检索纯基于相似度,重要性作为加权因子
+            # 基础相似度得分(不受重要性影响)
+            base_relevance = vec_score * 0.8 + recency_score * 0.2
+            
+            # 重要性作为乘法加权因子,范围 [0.8, 1.2]
+            importance_weight = 0.8 + (imp * 0.4)
+            
+            # 最终得分:相似度 * 重要性权重
+            combined = base_relevance * importance_weight
+
+            item = MemoryItem(
+                id=doc["memory_id"],
+                content=doc["content"],
+                memory_type=doc["memory_type"],
+                user_id=doc["user_id"],
+                timestamp=datetime.fromtimestamp(doc["timestamp"]),
+                importance=doc.get("importance", 0.5),
+                metadata={
+                    **doc.get("properties", {}),
+                    "relevance_score": combined,
+                    "vector_score": vec_score,
+                    "recency_score": recency_score
+                }
+            )
+            results.append((combined, item))
+            seen.add(mem_id)
+
+        # 若向量检索无结果,回退到简单关键词匹配(内存缓存)
+        if not results:
+            query_lower = query.lower()
+            for ep in self._filter_episodes(user_id, session_id, time_range):
+                if query_lower in ep.content.lower():
+                    recency_score = 1.0 / (1.0 + max(0.0, (now_ts - int(ep.timestamp.timestamp())) / 86400.0))
+                    # 回退匹配:新评分算法
+                    keyword_score = 0.5  # 简单关键词匹配的基础分数
+                    base_relevance = keyword_score * 0.8 + recency_score * 0.2
+                    importance_weight = 0.8 + (ep.importance * 0.4)
+                    combined = base_relevance * importance_weight
+                    item = MemoryItem(
+                        id=ep.episode_id,
+                        content=ep.content,
+                        memory_type="episodic",
+                        user_id=ep.user_id,
+                        timestamp=ep.timestamp,
+                        importance=ep.importance,
+                        metadata={
+                            "session_id": ep.session_id,
+                            "context": ep.context,
+                            "outcome": ep.outcome,
+                            "relevance_score": combined
+                        }
+                    )
+                    results.append((combined, item))
+
+        results.sort(key=lambda x: x[0], reverse=True)
+        return [it for _, it in results[:limit]]
+    
+    def update(
+        self,
+        memory_id: str,
+        content: str = None,
+        importance: float = None,
+        metadata: Dict[str, Any] = None
+    ) -> bool:
+        """更新情景记忆(SQLite为权威,Qdrant按需重嵌入)"""
+        updated = False
+        for episode in self.episodes:
+            if episode.episode_id == memory_id:
+                if content is not None:
+                    episode.content = content
+                if importance is not None:
+                    episode.importance = importance
+                if metadata is not None:
+                    episode.context.update(metadata.get("context", {}))
+                    if "outcome" in metadata:
+                        episode.outcome = metadata["outcome"]
+                updated = True
+                break
+
+        # 更新SQLite
+        doc_updated = self.doc_store.update_memory(
+            memory_id=memory_id,
+            content=content,
+            importance=importance,
+            properties=metadata
+        )
+
+        # 如内容变更,重嵌入并upsert到Qdrant
+        if content is not None and self.embedder is not None and self.vector_store is not None:
+            try:
+                embedding = self.embedder.encode(content)
+                if hasattr(embedding, "tolist"):
+                    embedding = embedding.tolist()
+                # 获取更新后的记录以同步payload
+                doc = self.doc_store.get_memory(memory_id)
+                payload = {
+                    "memory_id": memory_id,
+                    "user_id": doc["user_id"] if doc else "",
+                    "memory_type": "episodic",
+                    "importance": (doc.get("importance") if doc else importance) or 0.5,
+                    "session_id": (doc.get("properties", {}) or {}).get("session_id"),
+                    "content": content
+                }
+                self.vector_store.add_vectors(
+                    vectors=[embedding],
+                    metadata=[payload],
+                    ids=[memory_id]
+                )
+            except Exception:
+                pass
+
+        return updated or doc_updated
+    
+    def remove(self, memory_id: str) -> bool:
+        """删除情景记忆(SQLite + Qdrant)"""
+        removed = False
+        for i, episode in enumerate(self.episodes):
+            if episode.episode_id == memory_id:
+                removed_episode = self.episodes.pop(i)
+                session_id = removed_episode.session_id
+                if session_id in self.sessions:
+                    self.sessions[session_id].remove(memory_id)
+                    if not self.sessions[session_id]:
+                        del self.sessions[session_id]
+                removed = True
+                break
+
+        # 权威库删除
+        doc_deleted = self.doc_store.delete_memory(memory_id)
+        
+        # 向量库删除
+        if self.vector_store is not None:
+            try:
+                self.vector_store.delete_memories([memory_id])
+            except Exception:
+                pass
+        
+        return removed or doc_deleted
+    
+    def has_memory(self, memory_id: str) -> bool:
+        """检查记忆是否存在"""
+        return any(episode.episode_id == memory_id for episode in self.episodes)
+    
+    def clear(self):
+        """清空所有情景记忆(仅清理episodic,不影响其他类型)"""
+        # 内存缓存
+        self.episodes.clear()
+        self.sessions.clear()
+        self.patterns_cache.clear()
+
+        # SQLite内的episodic全部删除
+        docs = self.doc_store.search_memories(memory_type="episodic", limit=10000)
+        ids = [d["memory_id"] for d in docs]
+        for mid in ids:
+            self.doc_store.delete_memory(mid)
+
+        # Qdrant按ID删除对应向量
+        try:
+            if ids:
+                self.vector_store.delete_memories(ids)
+        except Exception:
+            pass
+
+    def forget(self, strategy: str = "importance_based", threshold: float = 0.1, max_age_days: int = 30) -> int:
+        """情景记忆遗忘机制(硬删除)"""
+        forgotten_count = 0
+        current_time = datetime.now()
+        
+        to_remove = []  # 收集要删除的记忆ID
+        
+        for episode in self.episodes:
+            should_forget = False
+            
+            if strategy == "importance_based":
+                # 基于重要性遗忘
+                if episode.importance < threshold:
+                    should_forget = True
+            elif strategy == "time_based":
+                # 基于时间遗忘
+                cutoff_time = current_time - timedelta(days=max_age_days)
+                if episode.timestamp < cutoff_time:
+                    should_forget = True
+            elif strategy == "capacity_based":
+                # 基于容量遗忘(保留最重要的)
+                if len(self.episodes) > self.config.max_capacity:
+                    sorted_episodes = sorted(self.episodes, key=lambda e: e.importance)
+                    excess_count = len(self.episodes) - self.config.max_capacity
+                    if episode in sorted_episodes[:excess_count]:
+                        should_forget = True
+            
+            if should_forget:
+                to_remove.append(episode.episode_id)
+        
+        # 执行硬删除
+        for episode_id in to_remove:
+            if self.remove(episode_id):
+                forgotten_count += 1
+                logger.info(f"情景记忆硬删除: {episode_id[:8]}... (策略: {strategy})")
+        
+        return forgotten_count
+
+    def get_all(self) -> List[MemoryItem]:
+        """获取所有情景记忆(转换为MemoryItem格式)"""
+        memory_items = []
+        for episode in self.episodes:
+            memory_item = MemoryItem(
+                id=episode.episode_id,
+                content=episode.content,
+                memory_type="episodic",
+                user_id=episode.user_id,
+                timestamp=episode.timestamp,
+                importance=episode.importance,
+                metadata=episode.metadata
+            )
+            memory_items.append(memory_item)
+        return memory_items
+    
+    def get_stats(self) -> Dict[str, Any]:
+        """获取情景记忆统计信息(合并SQLite与Qdrant)"""
+        # 硬删除模式:所有episodes都是活跃的
+        active_episodes = self.episodes
+        
+        db_stats = self.doc_store.get_database_stats()
+        try:
+            vs_stats = self.vector_store.get_collection_stats()
+        except Exception:
+            vs_stats = {"store_type": "qdrant"}
+        return {
+            "count": len(active_episodes),  # 活跃记忆数量
+            "forgotten_count": 0,  # 硬删除模式下已遗忘的记忆会被直接删除
+            "total_count": len(self.episodes),  # 总记忆数量
+            "sessions_count": len(self.sessions),
+            "avg_importance": sum(e.importance for e in active_episodes) / len(active_episodes) if active_episodes else 0.0,
+            "time_span_days": self._calculate_time_span(),
+            "memory_type": "episodic",
+            "vector_store": vs_stats,
+            "document_store": {k: v for k, v in db_stats.items() if k.endswith("_count") or k in ["store_type", "db_path"]}
+        }
+    
+    def get_session_episodes(self, session_id: str) -> List[Episode]:
+        """获取指定会话的所有情景"""
+        if session_id not in self.sessions:
+            return []
+        
+        episode_ids = self.sessions[session_id]
+        return [e for e in self.episodes if e.episode_id in episode_ids]
+    
+    def find_patterns(self, user_id: str = None, min_frequency: int = 2) -> List[Dict[str, Any]]:
+        """发现用户行为模式"""
+        # 检查缓存
+        cache_key = f"{user_id}_{min_frequency}"
+        if (cache_key in self.patterns_cache and 
+            self.last_pattern_analysis and 
+            (datetime.now() - self.last_pattern_analysis).hours < 1):
+            return self.patterns_cache[cache_key]
+        
+        # 过滤情景
+        episodes = [e for e in self.episodes if user_id is None or e.user_id == user_id]
+        
+        # 简单的模式识别:基于内容关键词
+        keyword_patterns = {}
+        context_patterns = {}
+        
+        for episode in episodes:
+            # 提取关键词
+            words = episode.content.lower().split()
+            for word in words:
+                if len(word) > 3:  # 忽略短词
+                    keyword_patterns[word] = keyword_patterns.get(word, 0) + 1
+            
+            # 提取上下文模式
+            for key, value in episode.context.items():
+                pattern_key = f"{key}:{value}"
+                context_patterns[pattern_key] = context_patterns.get(pattern_key, 0) + 1
+        
+        # 筛选频繁模式
+        patterns = []
+        
+        for keyword, frequency in keyword_patterns.items():
+            if frequency >= min_frequency:
+                patterns.append({
+                    "type": "keyword",
+                    "pattern": keyword,
+                    "frequency": frequency,
+                    "confidence": frequency / len(episodes)
+                })
+        
+        for context_pattern, frequency in context_patterns.items():
+            if frequency >= min_frequency:
+                patterns.append({
+                    "type": "context",
+                    "pattern": context_pattern,
+                    "frequency": frequency,
+                    "confidence": frequency / len(episodes)
+                })
+        
+        # 按频率排序
+        patterns.sort(key=lambda x: x["frequency"], reverse=True)
+        
+        # 缓存结果
+        self.patterns_cache[cache_key] = patterns
+        self.last_pattern_analysis = datetime.now()
+        
+        return patterns
+    
+    def get_timeline(self, user_id: str = None, limit: int = 50) -> List[Dict[str, Any]]:
+        """获取时间线视图"""
+        episodes = [e for e in self.episodes if user_id is None or e.user_id == user_id]
+        episodes.sort(key=lambda x: x.timestamp, reverse=True)
+        
+        timeline = []
+        for episode in episodes[:limit]:
+            timeline.append({
+                "episode_id": episode.episode_id,
+                "timestamp": episode.timestamp.isoformat(),
+                "content": episode.content[:100] + "..." if len(episode.content) > 100 else episode.content,
+                "session_id": episode.session_id,
+                "importance": episode.importance,
+                "outcome": episode.outcome
+            })
+        
+        return timeline
+    
+    def _filter_episodes(
+        self,
+        user_id: str = None,
+        session_id: str = None,
+        time_range: Tuple[datetime, datetime] = None
+    ) -> List[Episode]:
+        """过滤情景"""
+        filtered = self.episodes
+        
+        if user_id:
+            filtered = [e for e in filtered if e.user_id == user_id]
+        
+        if session_id:
+            filtered = [e for e in filtered if e.session_id == session_id]
+        
+        if time_range:
+            start_time, end_time = time_range
+            filtered = [e for e in filtered if start_time <= e.timestamp <= end_time]
+        
+        return filtered
+    
+    def _calculate_time_span(self) -> float:
+        """计算记忆时间跨度(天)"""
+        if not self.episodes:
+            return 0.0
+        
+        timestamps = [e.timestamp for e in self.episodes]
+        min_time = min(timestamps)
+        max_time = max(timestamps)
+        
+        return (max_time - min_time).days
+    
+    def _persist_episode(self, episode: Episode):
+        """持久化情景到存储后端"""
+        if self.storage and hasattr(self.storage, 'add_memory'):
+            self.storage.add_memory(
+                memory_id=episode.episode_id,
+                user_id=episode.user_id,
+                content=episode.content,
+                memory_type="episodic",
+                timestamp=int(episode.timestamp.timestamp()),
+                importance=episode.importance,
+                properties={
+                    "session_id": episode.session_id,
+                    "context": episode.context,
+                    "outcome": episode.outcome
+                }
+            )
+    
+    def _remove_from_storage(self, memory_id: str):
+        """从存储后端删除"""
+        if self.storage and hasattr(self.storage, 'delete_memory'):
+            self.storage.delete_memory(memory_id)

+ 709 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/types/perceptual.py

@@ -0,0 +1,709 @@
+"""感知记忆实现(长存的多模态)
+
+按照第8章架构设计的感知记忆(长期、多模态),提供:
+- 多模态数据存储(文本、图像、音频等)
+- 结构化元数据 + 向量索引(SQLite + Qdrant)
+- 同模态检索(跨模态在无CLIP/CLAP依赖时有限)
+- 懒加载编码:文本用 sentence-transformers;图像/音频用轻量确定性哈希向量
+"""
+
+from typing import List, Dict, Any, Optional, Union, Tuple
+from datetime import datetime, timedelta
+import hashlib
+import os
+import random
+import logging
+
+logger = logging.getLogger(__name__)
+
+from ..base import BaseMemory, MemoryItem, MemoryConfig
+from ..storage import SQLiteDocumentStore, QdrantVectorStore
+from ..embedding import get_text_embedder, get_dimension
+
+class Perception:
+    """感知数据实体"""
+    
+    def __init__(
+        self,
+        perception_id: str,
+        data: Any,
+        modality: str,
+        encoding: Optional[List[float]] = None,
+        metadata: Dict[str, Any] = None
+    ):
+        self.perception_id = perception_id
+        self.data = data
+        self.modality = modality  # text, image, audio, video, structured
+        self.encoding = encoding or []
+        self.metadata = metadata or {}
+        self.timestamp = datetime.now()
+        self.data_hash = self._calculate_hash()
+    
+    def _calculate_hash(self) -> str:
+        """计算数据哈希"""
+        if isinstance(self.data, str):
+            return hashlib.md5(self.data.encode()).hexdigest()
+        elif isinstance(self.data, bytes):
+            return hashlib.md5(self.data).hexdigest()
+        else:
+            return hashlib.md5(str(self.data).encode()).hexdigest()
+
+class PerceptualMemory(BaseMemory):
+    """感知记忆实现
+    
+    特点:
+    - 支持多模态数据(文本、图像、音频等)
+    - 跨模态相似性搜索
+    - 感知数据的语义理解
+    - 支持内容生成和检索
+    """
+    
+    def __init__(self, config: MemoryConfig, storage_backend=None):
+        super().__init__(config, storage_backend)
+        
+        # 感知数据存储(内存缓存)
+        self.perceptions: Dict[str, Perception] = {}
+        self.perceptual_memories: List[MemoryItem] = []
+        
+        # 模态索引
+        self.modality_index: Dict[str, List[str]] = {}  # modality -> perception_ids
+        
+        # 支持的模态
+        self.supported_modalities = set(self.config.perceptual_memory_modalities)
+        
+        # 文档权威存储(SQLite)
+        db_dir = getattr(self.config, 'storage_path', "./memory_data")
+        os.makedirs(db_dir, exist_ok=True)
+        db_path = os.path.join(db_dir, "memory.db")
+        self.doc_store = SQLiteDocumentStore(db_path=db_path)
+
+        # 嵌入维度(与统一文本嵌入保持一致)
+        self.text_embedder = get_text_embedder()
+        self.vector_dim = get_dimension(getattr(self.text_embedder, 'dimension', 384))
+
+        # 可选加载:图像CLIP与音频CLAP(缺依赖则优雅降级为哈希编码)
+        self._clip_model = None
+        self._clip_processor = None
+        self._clap_model = None
+        self._clap_processor = None
+        self._image_dim = None
+        self._audio_dim = None
+        try:
+            from transformers import CLIPModel, CLIPProcessor
+            clip_name = os.getenv("CLIP_MODEL", "openai/clip-vit-base-patch32")
+            self._clip_model = CLIPModel.from_pretrained(clip_name)
+            self._clip_processor = CLIPProcessor.from_pretrained(clip_name)
+            # 估计输出维度
+            self._image_dim = self._clip_model.config.projection_dim if hasattr(self._clip_model.config, 'projection_dim') else 512
+        except Exception:
+            self._clip_model = None
+            self._clip_processor = None
+            self._image_dim = self.vector_dim
+        try:
+            from transformers import ClapProcessor, ClapModel
+            clap_name = os.getenv("CLAP_MODEL", "laion/clap-htsat-unfused")
+            self._clap_model = ClapModel.from_pretrained(clap_name)
+            self._clap_processor = ClapProcessor.from_pretrained(clap_name)
+            # 估计输出维度
+            self._audio_dim = getattr(self._clap_model.config, 'projection_dim', None) or 512
+        except Exception:
+            self._clap_model = None
+            self._clap_processor = None
+            self._audio_dim = self.vector_dim
+
+        # 向量存储(Qdrant)— 按模态拆分集合,避免维度冲突,使用连接管理器避免重复连接
+        from ..storage.qdrant_store import QdrantConnectionManager
+        qdrant_url = os.getenv("QDRANT_URL")
+        qdrant_api_key = os.getenv("QDRANT_API_KEY")
+        base_collection = os.getenv("QDRANT_COLLECTION", "hello_agents_vectors")
+        distance = os.getenv("QDRANT_DISTANCE", "cosine")
+        
+        self.vector_stores: Dict[str, QdrantVectorStore] = {}
+        # 文本集合
+        self.vector_stores["text"] = QdrantConnectionManager.get_instance(
+            url=qdrant_url,
+            api_key=qdrant_api_key,
+            collection_name=f"{base_collection}_perceptual_text",
+            vector_size=self.vector_dim,
+            distance=distance
+        )
+        # 图像集合(若CLIP不可用,维度退化为text维度)
+        self.vector_stores["image"] = QdrantConnectionManager.get_instance(
+            url=qdrant_url,
+            api_key=qdrant_api_key,
+            collection_name=f"{base_collection}_perceptual_image",
+            vector_size=int(self._image_dim or self.vector_dim),
+            distance=distance
+        )
+        # 音频集合(若CLAP不可用,维度退化为text维度)
+        self.vector_stores["audio"] = QdrantConnectionManager.get_instance(
+            url=qdrant_url,
+            api_key=qdrant_api_key,
+            collection_name=f"{base_collection}_perceptual_audio",
+            vector_size=int(self._audio_dim or self.vector_dim),
+            distance=distance
+        )
+        
+        # 编码器(轻量实现;真实场景可替换为CLIP/CLAP等)
+        self.encoders = self._init_encoders()
+    
+    def add(self, memory_item: MemoryItem) -> str:
+        """添加感知记忆(SQLite权威 + Qdrant向量)"""
+        modality = memory_item.metadata.get("modality", "text")
+        raw_data = memory_item.metadata.get("raw_data", memory_item.content)
+        if modality not in self.supported_modalities:
+            raise ValueError(f"不支持的模态类型: {modality}")
+
+        # 编码感知数据
+        perception = self._encode_perception(raw_data, modality, memory_item.id)
+
+        # 缓存与索引
+        self.perceptions[perception.perception_id] = perception
+        if modality not in self.modality_index:
+            self.modality_index[modality] = []
+        self.modality_index[modality].append(perception.perception_id)
+
+        # 存储记忆项(缓存)
+        memory_item.metadata["perception_id"] = perception.perception_id
+        memory_item.metadata["modality"] = modality
+        # 不把大向量放到metadata中,避免膨胀
+        self.perceptual_memories.append(memory_item)
+
+        # 1) SQLite 权威入库
+        ts_int = int(memory_item.timestamp.timestamp())
+        self.doc_store.add_memory(
+            memory_id=memory_item.id,
+            user_id=memory_item.user_id,
+            content=memory_item.content,
+            memory_type="perceptual",
+            timestamp=ts_int,
+            importance=memory_item.importance,
+            properties={
+                "perception_id": perception.perception_id,
+                "modality": modality,
+                "context": memory_item.metadata.get("context", {}),
+                "tags": memory_item.metadata.get("tags", []),
+            }
+        )
+
+        # 2) Qdrant 向量入库(按模态写入对应集合)
+        try:
+            vector = perception.encoding
+            store = self._get_vector_store_for_modality(modality)
+            store.add_vectors(
+                vectors=[vector],
+                metadata=[{
+                    "memory_id": memory_item.id,
+                    "user_id": memory_item.user_id,
+                    "memory_type": "perceptual",
+                    "modality": modality,
+                    "importance": memory_item.importance,
+                    "content": memory_item.content,
+                }],
+                ids=[memory_item.id]
+            )
+        except Exception:
+            pass
+
+        return memory_item.id
+    
+    def retrieve(self, query: str, limit: int = 5, **kwargs) -> List[MemoryItem]:
+        """检索感知记忆(可筛模态;同模态向量检索+时间/重要性融合)"""
+        user_id = kwargs.get("user_id")
+        target_modality = kwargs.get("target_modality")  # 可选:限制目标模态
+        query_modality = kwargs.get("query_modality", target_modality or "text")
+
+        # 仅在同模态情况下进行向量检索(跨模态需要CLIP/CLAP,此处保留简单回退)
+        try:
+            qvec = self._encode_data(query, query_modality)
+            where = {"memory_type": "perceptual"}
+            if user_id:
+                where["user_id"] = user_id
+            if target_modality:
+                where["modality"] = target_modality
+            store = self._get_vector_store_for_modality(target_modality or query_modality)
+            hits = store.search_similar(
+                query_vector=qvec,
+                limit=max(limit * 5, 20),
+                where=where
+            )
+        except Exception:
+            hits = []
+
+        # 融合排序
+        now_ts = int(datetime.now().timestamp())
+        results: List[Tuple[float, MemoryItem]] = []
+        seen = set()
+        for hit in hits:
+            meta = hit.get("metadata", {})
+            mem_id = meta.get("memory_id")
+            if not mem_id or mem_id in seen:
+                continue
+            if target_modality and meta.get("modality") != target_modality:
+                continue
+            doc = self.doc_store.get_memory(mem_id)
+            if not doc:
+                continue
+            vec_score = float(hit.get("score", 0.0))
+            age_days = max(0.0, (now_ts - int(doc["timestamp"])) / 86400.0)
+            recency_score = 1.0 / (1.0 + age_days)
+            imp = float(doc.get("importance", 0.5))
+            
+            # 新评分算法:向量检索纯基于相似度,重要性作为加权因子
+            # 基础相似度得分(不受重要性影响)
+            base_relevance = vec_score * 0.8 + recency_score * 0.2
+            
+            # 重要性作为乘法加权因子,范围 [0.8, 1.2]
+            importance_weight = 0.8 + (imp * 0.4)
+            
+            # 最终得分:相似度 * 重要性权重
+            combined = base_relevance * importance_weight
+
+            item = MemoryItem(
+                id=doc["memory_id"],
+                content=doc["content"],
+                memory_type=doc["memory_type"],
+                user_id=doc["user_id"],
+                timestamp=datetime.fromtimestamp(doc["timestamp"]),
+                importance=imp,
+                metadata={**doc.get("properties", {}), "relevance_score": combined,
+                          "vector_score": vec_score, "recency_score": recency_score}
+            )
+            results.append((combined, item))
+            seen.add(mem_id)
+
+        # 简单回退:若无命中且有目标模态,则按SQLite结构化过滤+关键词兜底
+        if not results:
+            for m in self.perceptual_memories:
+                if target_modality and m.metadata.get("modality") != target_modality:
+                    continue
+                if query.lower() in (m.content or "").lower():
+                    recency_score = 1.0 / (1.0 + max(0.0, (now_ts - int(m.timestamp.timestamp())) / 86400.0))
+                    # 回退匹配:新评分算法
+                    keyword_score = 0.5  # 简单关键词匹配的基础分数
+                    base_relevance = keyword_score * 0.8 + recency_score * 0.2
+                    importance_weight = 0.8 + (m.importance * 0.4)
+                    combined = base_relevance * importance_weight
+                    results.append((combined, m))
+
+        results.sort(key=lambda x: x[0], reverse=True)
+        return [it for _, it in results[:limit]]
+    
+    def update(
+        self,
+        memory_id: str,
+        content: str = None,
+        importance: float = None,
+        metadata: Dict[str, Any] = None
+    ) -> bool:
+        """更新感知记忆"""
+        updated = False
+        modality_cache = None
+        for memory in self.perceptual_memories:
+            if memory.id == memory_id:
+                if content is not None:
+                    memory.content = content
+                if importance is not None:
+                    memory.importance = importance
+                if metadata is not None:
+                    memory.metadata.update(metadata)
+                modality_cache = memory.metadata.get("modality", "text")
+                updated = True
+                break
+
+        # 更新SQLite
+        self.doc_store.update_memory(
+            memory_id=memory_id,
+            content=content,
+            importance=importance,
+            properties=metadata
+        )
+
+        # 如内容或原始数据改变,则重嵌入并upsert到Qdrant
+        if content is not None or (metadata and "raw_data" in metadata):
+            modality = metadata.get("modality", modality_cache or "text") if metadata else (modality_cache or "text")
+            raw = metadata.get("raw_data", content) if metadata else content
+            try:
+                perception = self._encode_perception(raw or "", modality, memory_id)
+                payload = self.doc_store.get_memory(memory_id) or {}
+                self.vector_store.add_vectors(
+                    vectors=[perception.encoding],
+                    metadata=[{
+                        "memory_id": memory_id,
+                        "user_id": payload.get("user_id", ""),
+                        "memory_type": "perceptual",
+                        "modality": modality,
+                        "importance": (payload.get("importance") or importance) or 0.5,
+                        "content": content or (payload.get("content", "")),
+                    }],
+                    ids=[memory_id]
+                )
+            except Exception:
+                pass
+
+        return updated
+    
+    def remove(self, memory_id: str) -> bool:
+        """删除感知记忆"""
+        removed = False
+        for i, memory in enumerate(self.perceptual_memories):
+            if memory.id == memory_id:
+                removed_memory = self.perceptual_memories.pop(i)
+                perception_id = removed_memory.metadata.get("perception_id")
+                if perception_id and perception_id in self.perceptions:
+                    perception = self.perceptions.pop(perception_id)
+                    modality = perception.modality
+                    if modality in self.modality_index:
+                        if perception_id in self.modality_index[modality]:
+                            self.modality_index[modality].remove(perception_id)
+                        if not self.modality_index[modality]:
+                            del self.modality_index[modality]
+                removed = True
+                break
+
+        # 权威库删除
+        self.doc_store.delete_memory(memory_id)
+        # 向量库删除(所有模态集合尝试删除)
+        for store in self.vector_stores.values():
+            try:
+                store.delete_memories([memory_id])
+            except Exception:
+                pass
+
+        return removed
+    
+    def has_memory(self, memory_id: str) -> bool:
+        """检查记忆是否存在"""
+        return any(memory.id == memory_id for memory in self.perceptual_memories)
+    
+    def forget(self, strategy: str = "importance_based", threshold: float = 0.1, max_age_days: int = 30) -> int:
+        """感知记忆遗忘机制(硬删除)"""
+        forgotten_count = 0
+        current_time = datetime.now()
+        
+        to_remove = []  # 收集要删除的记忆ID
+        
+        for memory in self.perceptual_memories:
+            should_forget = False
+            
+            if strategy == "importance_based":
+                # 基于重要性遗忘
+                if memory.importance < threshold:
+                    should_forget = True
+            elif strategy == "time_based":
+                # 基于时间遗忘
+                cutoff_time = current_time - timedelta(days=max_age_days)
+                if memory.timestamp < cutoff_time:
+                    should_forget = True
+            elif strategy == "capacity_based":
+                # 基于容量遗忘(保留最重要的)
+                if len(self.perceptual_memories) > self.config.max_capacity:
+                    sorted_memories = sorted(self.perceptual_memories, key=lambda m: m.importance)
+                    excess_count = len(self.perceptual_memories) - self.config.max_capacity
+                    if memory in sorted_memories[:excess_count]:
+                        should_forget = True
+            
+            if should_forget:
+                to_remove.append(memory.id)
+        
+        # 执行硬删除
+        for memory_id in to_remove:
+            if self.remove(memory_id):
+                forgotten_count += 1
+                logger.info(f"感知记忆硬删除: {memory_id[:8]}... (策略: {strategy})")
+        
+        return forgotten_count
+
+    def clear(self):
+        """清空所有感知记忆"""
+        self.perceptual_memories.clear()
+        self.perceptions.clear()
+        self.modality_index.clear()
+        # 删除SQLite中的perceptual记录
+        docs = self.doc_store.search_memories(memory_type="perceptual", limit=10000)
+        ids = [d["memory_id"] for d in docs]
+        for mid in ids:
+            self.doc_store.delete_memory(mid)
+        # 删除Qdrant向量(所有模态集合)
+        for store in self.vector_stores.values():
+            try:
+                if ids:
+                    store.delete_memories(ids)
+            except Exception:
+                pass
+
+    def get_all(self) -> List[MemoryItem]:
+        """获取所有感知记忆"""
+        return self.perceptual_memories.copy()
+    
+    def get_stats(self) -> Dict[str, Any]:
+        """获取感知记忆统计信息"""
+        # 硬删除模式:所有记忆都是活跃的
+        active_memories = self.perceptual_memories
+        
+        modality_counts = {modality: len(ids) for modality, ids in self.modality_index.items()}
+        vs_stats_all = {}
+        for mod, store in self.vector_stores.items():
+            try:
+                vs_stats_all[mod] = store.get_collection_stats()
+            except Exception:
+                vs_stats_all[mod] = {"store_type": "qdrant"}
+        db_stats = self.doc_store.get_database_stats()
+        
+        return {
+            "count": len(active_memories),  # 活跃记忆数量
+            "forgotten_count": 0,  # 硬删除模式下已遗忘的记忆会被直接删除
+            "total_count": len(self.perceptual_memories),  # 总记忆数量
+            "perceptions_count": len(self.perceptions),
+            "modality_counts": modality_counts,
+            "supported_modalities": list(self.supported_modalities),
+            "avg_importance": sum(m.importance for m in active_memories) / len(active_memories) if active_memories else 0.0,
+            "memory_type": "perceptual",
+            "vector_stores": vs_stats_all,
+            "document_store": {k: v for k, v in db_stats.items() if k.endswith("_count") or k in ["store_type", "db_path"]}
+        }
+    
+    def cross_modal_search(
+        self,
+        query: Any,
+        query_modality: str,
+        target_modality: str = None,
+        limit: int = 5
+    ) -> List[MemoryItem]:
+        """跨模态搜索"""
+        return self.retrieve(
+            query=str(query),
+            limit=limit,
+            query_modality=query_modality,
+            target_modality=target_modality
+        )
+    
+    def get_by_modality(self, modality: str, limit: int = 10) -> List[MemoryItem]:
+        """按模态获取记忆"""
+        if modality not in self.modality_index:
+            return []
+        
+        perception_ids = self.modality_index[modality]
+        results = []
+        
+        for memory in self.perceptual_memories:
+            if memory.metadata.get("perception_id") in perception_ids:
+                results.append(memory)
+                if len(results) >= limit:
+                    break
+        
+        return results
+    
+    def generate_content(self, prompt: str, target_modality: str) -> Optional[str]:
+        """基于感知记忆生成内容"""
+        # 简化的内容生成实现
+        # 实际应用中需要使用生成模型
+        
+        if target_modality not in self.supported_modalities:
+            return None
+        
+        # 检索相关感知记忆
+        relevant_memories = self.retrieve(prompt, limit=3)
+        
+        if not relevant_memories:
+            return None
+        
+        # 简单的内容组合
+        if target_modality == "text":
+            contents = [memory.content for memory in relevant_memories]
+            return f"基于感知记忆生成的内容:\n" + "\n".join(contents)
+        
+        return f"生成的{target_modality}内容(基于{len(relevant_memories)}个相关记忆)"
+    
+    def _init_encoders(self) -> Dict[str, Any]:
+        """初始化编码器(轻量、确定性,统一输出self.vector_dim维)"""
+        encoders = {}
+        for modality in self.supported_modalities:
+            if modality == "text":
+                encoders[modality] = self._text_encoder
+            elif modality == "image":
+                encoders[modality] = self._image_encoder
+            elif modality == "audio":
+                encoders[modality] = self._audio_encoder
+            else:
+                encoders[modality] = self._default_encoder
+        return encoders
+    
+    def _encode_perception(self, data: Any, modality: str, memory_id: str) -> Perception:
+        """编码感知数据"""
+        encoding = self._encode_data(data, modality)
+        
+        perception = Perception(
+            perception_id=f"perception_{memory_id}",
+            data=data,
+            modality=modality,
+            encoding=encoding,
+            metadata={"source": "memory_system"}
+        )
+        
+        return perception
+    
+    def _encode_data(self, data: Any, modality: str) -> List[float]:
+        """编码数据为固定维度向量(按模态维度对齐)"""
+        target_dim = self._get_dim_for_modality(modality)
+        encoder = self.encoders.get(modality, self._default_encoder)
+        vec = encoder(data)
+        if not isinstance(vec, list):
+            vec = list(vec)
+        if len(vec) < target_dim:
+            vec = vec + [0.0] * (target_dim - len(vec))
+        elif len(vec) > target_dim:
+            vec = vec[:target_dim]
+        return vec
+    
+    def _text_encoder(self, text: str) -> List[float]:
+        """文本编码器(使用嵌入模型)"""
+        emb = self.text_embedder.encode(text or "")
+        if hasattr(emb, "tolist"):
+            emb = emb.tolist()
+        return emb
+    
+    def _image_encoder_hash(self, image_data: Any) -> List[float]:
+        """图像编码器(轻量确定性哈希向量,跨环境稳定)"""
+        try:
+            if isinstance(image_data, (bytes, bytearray)):
+                data_bytes = bytes(image_data)
+            elif isinstance(image_data, str) and os.path.exists(image_data):
+                with open(image_data, 'rb') as f:
+                    data_bytes = f.read()
+            else:
+                data_bytes = str(image_data).encode('utf-8', errors='ignore')
+            hex_str = hashlib.sha256(data_bytes).hexdigest()
+            return self._hash_to_vector(hex_str, self._get_dim_for_modality("image"))
+        except Exception:
+            return self._hash_to_vector(str(image_data), self._get_dim_for_modality("image"))
+
+    def _image_encoder(self, image_data: Any) -> List[float]:
+        """图像编码器(优先CLIP,不可用则哈希)"""
+        if self._clip_model is None or self._clip_processor is None:
+            return self._image_encoder_hash(image_data)
+        try:
+            from PIL import Image
+            if isinstance(image_data, str) and os.path.exists(image_data):
+                image = Image.open(image_data).convert('RGB')
+            elif isinstance(image_data, (bytes, bytearray)):
+                from io import BytesIO
+                image = Image.open(BytesIO(bytes(image_data))).convert('RGB')
+            else:
+                # 退回到哈希
+                return self._image_encoder_hash(image_data)
+            inputs = self._clip_processor(images=image, return_tensors="pt")
+            with self._no_grad():
+                feats = self._clip_model.get_image_features(**inputs)
+            vec = feats[0].detach().cpu().numpy().tolist()
+            return vec
+        except Exception:
+            return self._image_encoder_hash(image_data)
+    
+    def _audio_encoder_hash(self, audio_data: Any) -> List[float]:
+        """音频编码器(轻量确定性哈希向量)"""
+        try:
+            if isinstance(audio_data, (bytes, bytearray)):
+                data_bytes = bytes(audio_data)
+            elif isinstance(audio_data, str) and os.path.exists(audio_data):
+                with open(audio_data, 'rb') as f:
+                    data_bytes = f.read()
+            else:
+                data_bytes = str(audio_data).encode('utf-8', errors='ignore')
+            hex_str = hashlib.sha256(data_bytes).hexdigest()
+            return self._hash_to_vector(hex_str, self._get_dim_for_modality("audio"))
+        except Exception:
+            return self._hash_to_vector(str(audio_data), self._get_dim_for_modality("audio"))
+
+    def _audio_encoder(self, audio_data: Any) -> List[float]:
+        """音频编码器(优先CLAP,不可用则哈希)"""
+        if self._clap_model is None or self._clap_processor is None:
+            return self._audio_encoder_hash(audio_data)
+        try:
+            import numpy as np
+            # 加载音频(需要 librosa)
+            import librosa
+            if isinstance(audio_data, str) and os.path.exists(audio_data):
+                speech, sr = librosa.load(audio_data, sr=48000, mono=True)
+            elif isinstance(audio_data, (bytes, bytearray)):
+                # 临时文件方式加载
+                import tempfile
+                with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
+                    tmp.write(bytes(audio_data))
+                    tmp_path = tmp.name
+                speech, sr = librosa.load(tmp_path, sr=48000, mono=True)
+                try:
+                    os.remove(tmp_path)
+                except Exception:
+                    pass
+            else:
+                return self._audio_encoder_hash(audio_data)
+            inputs = self._clap_processor(audios=speech, sampling_rate=48000, return_tensors="pt")
+            with self._no_grad():
+                feats = self._clap_model.get_audio_features(**inputs)
+            vec = feats[0].detach().cpu().numpy().tolist()
+            return vec
+        except Exception:
+            return self._audio_encoder_hash(audio_data)
+
+    def _default_encoder(self, data: Any) -> List[float]:
+        """默认编码器(退化为文本嵌入或哈希)"""
+        try:
+            return self._text_encoder(str(data))
+        except Exception:
+            return self._hash_to_vector(str(data), self.vector_dim)
+    
+    def _calculate_similarity(self, encoding1: List[float], encoding2: List[float]) -> float:
+        """计算编码相似度"""
+        if not encoding1 or not encoding2:
+            return 0.0
+        
+        # 确保长度一致
+        min_len = min(len(encoding1), len(encoding2))
+        if min_len == 0:
+            return 0.0
+        
+        # 计算余弦相似度
+        dot_product = sum(a * b for a, b in zip(encoding1[:min_len], encoding2[:min_len]))
+        norm1 = sum(a * a for a in encoding1[:min_len]) ** 0.5
+        norm2 = sum(a * a for a in encoding2[:min_len]) ** 0.5
+        
+        if norm1 == 0 or norm2 == 0:
+            return 0.0
+        
+        return dot_product / (norm1 * norm2)
+
+    def _hash_to_vector(self, data_str: str, dim: int) -> List[float]:
+        """将字符串哈希为固定维度的[0,1]向量(确定性)"""
+        seed = int(hashlib.sha256(data_str.encode("utf-8", errors="ignore")).hexdigest(), 16) % (2**32)
+        rng = random.Random(seed)
+        return [rng.random() for _ in range(dim)]
+
+    class _no_grad:
+        def __enter__(self):
+            try:
+                import torch
+                self.prev = torch.is_grad_enabled()
+                torch.set_grad_enabled(False)
+            except Exception:
+                self.prev = None
+            return self
+        def __exit__(self, exc_type, exc, tb):
+            try:
+                import torch
+                if self.prev is not None:
+                    torch.set_grad_enabled(self.prev)
+            except Exception:
+                pass
+
+    def _get_vector_store_for_modality(self, modality: Optional[str]) -> QdrantVectorStore:
+        mod = (modality or "text").lower()
+        return self.vector_stores.get(mod, self.vector_stores["text"])
+
+    def _get_dim_for_modality(self, modality: Optional[str]) -> int:
+        mod = (modality or "text").lower()
+        if mod == "image":
+            return int(self._image_dim or self.vector_dim)
+        if mod == "audio":
+            return int(self._audio_dim or self.vector_dim)
+        return int(self.vector_dim)

+ 1181 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/types/semantic.py

@@ -0,0 +1,1181 @@
+"""语义记忆实现
+
+结合向量检索和知识图谱的混合语义记忆,使用:
+- HuggingFace 中文预训练模型进行文本嵌入
+- 向量相似度检索进行快速初筛
+- 知识图谱进行实体关系推理
+- 混合检索策略优化结果质量
+"""
+
+from typing import List, Dict, Any, Optional, Set, Tuple
+from datetime import datetime, timedelta
+import json
+import logging
+import math
+import numpy as np
+
+from ..base import BaseMemory, MemoryItem, MemoryConfig
+from ..embedding import get_text_embedder, get_dimension
+from core.database_config import get_database_config
+
+# 配置日志
+logging.basicConfig(level=logging.INFO)
+logger = logging.getLogger(__name__)
+
+class Entity:
+    """实体类"""
+    
+    def __init__(
+        self,
+        entity_id: str,
+        name: str,
+        entity_type: str = "MISC",
+        description: str = "",
+        properties: Dict[str, Any] = None
+    ):
+        self.entity_id = entity_id
+        self.name = name
+        self.entity_type = entity_type  # PERSON, ORG, PRODUCT, SKILL, CONCEPT等
+        self.description = description
+        self.properties = properties or {}
+        self.created_at = datetime.now()
+        self.updated_at = datetime.now()
+        self.frequency = 1  # 出现频率
+    
+    def to_dict(self) -> Dict[str, Any]:
+        return {
+            "entity_id": self.entity_id,
+            "name": self.name,
+            "entity_type": self.entity_type,
+            "description": self.description,
+            "properties": self.properties,
+            "frequency": self.frequency
+        }
+
+class Relation:
+    """关系类"""
+    
+    def __init__(
+        self,
+        from_entity: str,
+        to_entity: str,
+        relation_type: str,
+        strength: float = 1.0,
+        evidence: str = "",
+        properties: Dict[str, Any] = None
+    ):
+        self.from_entity = from_entity
+        self.to_entity = to_entity
+        self.relation_type = relation_type
+        self.strength = strength
+        self.evidence = evidence  # 支持该关系的原文本
+        self.properties = properties or {}
+        self.created_at = datetime.now()
+        self.frequency = 1  # 关系出现频率
+    
+    def to_dict(self) -> Dict[str, Any]:
+        return {
+            "from_entity": self.from_entity,
+            "to_entity": self.to_entity,
+            "relation_type": self.relation_type,
+            "strength": self.strength,
+            "evidence": self.evidence,
+            "properties": self.properties,
+            "frequency": self.frequency
+        }
+
+
+class SemanticMemory(BaseMemory):
+    """增强语义记忆实现
+    
+    特点:
+    - 使用HuggingFace中文预训练模型进行文本嵌入
+    - 向量检索进行快速相似度匹配
+    - 知识图谱存储实体和关系
+    - 混合检索策略:向量+图+语义推理
+    """
+    
+    def __init__(self, config: MemoryConfig, storage_backend=None):
+        super().__init__(config, storage_backend)
+        
+        # 嵌入模型(统一提供)
+        self.embedding_model = None
+        self._init_embedding_model()
+        
+        # 专业数据库存储
+        self.vector_store = None
+        self.graph_store = None
+        self._init_databases()
+        
+        # 实体和关系缓存 (用于快速访问)
+        self.entities: Dict[str, Entity] = {}
+        self.relations: List[Relation] = []
+        
+        # 实体识别器
+        self.nlp = None
+        self._init_nlp()
+        
+        # 记忆存储
+        self.semantic_memories: List[MemoryItem] = []
+        self.memory_embeddings: Dict[str, np.ndarray] = {}
+        
+        logger.info("增强语义记忆初始化完成(使用Qdrant+Neo4j专业数据库)")
+    
+    def _init_embedding_model(self):
+        """初始化统一嵌入模型(由 embedding_provider 管理)。"""
+        try:
+            self.embedding_model = get_text_embedder()
+            # 轻量健康检查与日志
+            try:
+                test_vec = self.embedding_model.encode("health_check")
+                dim = getattr(self.embedding_model, "dimension", len(test_vec))
+                logger.info(f"✅ 嵌入模型就绪,维度: {dim}")
+            except Exception:
+                logger.info("✅ 嵌入模型就绪")
+        except Exception as e:
+            logger.error(f"❌ 嵌入模型初始化失败: {e}")
+            raise
+    
+    def _init_databases(self):
+        """初始化专业数据库存储"""
+        try:
+            # 获取数据库配置
+            db_config = get_database_config()
+            
+            # 初始化Qdrant向量数据库(使用连接管理器避免重复连接)
+            from ..storage.qdrant_store import QdrantConnectionManager
+            qdrant_config = db_config.get_qdrant_config() or {}
+            qdrant_config["vector_size"] = get_dimension()
+            self.vector_store = QdrantConnectionManager.get_instance(**qdrant_config)
+            logger.info("✅ Qdrant向量数据库初始化完成")
+            
+            # 初始化Neo4j图数据库
+            from ..storage.neo4j_store import Neo4jGraphStore
+            neo4j_config = db_config.get_neo4j_config()
+            self.graph_store = Neo4jGraphStore(**neo4j_config)
+            logger.info("✅ Neo4j图数据库初始化完成")
+            
+            # 验证连接
+            vector_health = self.vector_store.health_check()
+            graph_health = self.graph_store.health_check()
+            
+            if not vector_health:
+                logger.warning("⚠️ Qdrant连接异常,部分功能可能受限")
+            if not graph_health:
+                logger.warning("⚠️ Neo4j连接异常,图搜索功能可能受限")
+            
+            logger.info(f"🏥 数据库健康状态: Qdrant={'✅' if vector_health else '❌'}, Neo4j={'✅' if graph_health else '❌'}")
+            
+        except Exception as e:
+            logger.error(f"❌ 数据库初始化失败: {e}")
+            logger.info("💡 请检查数据库配置和网络连接")
+            logger.info("💡 参考 DATABASE_SETUP_GUIDE.md 进行配置")
+            raise
+    
+    def _init_nlp(self):
+        """初始化NLP处理器 - 智能多语言支持"""
+        try:
+            import spacy
+            self.nlp_models = {}
+            
+            # 尝试加载多语言模型
+            models_to_try = [
+                ("zh_core_web_sm", "中文"),
+                ("en_core_web_sm", "英文")
+            ]
+            
+            loaded_models = []
+            for model_name, lang_name in models_to_try:
+                try:
+                    nlp = spacy.load(model_name)
+                    self.nlp_models[model_name] = nlp
+                    loaded_models.append(lang_name)
+                    logger.info(f"✅ 加载{lang_name}spaCy模型: {model_name}")
+                except OSError:
+                    logger.warning(f"⚠️ {lang_name}spaCy模型不可用: {model_name}")
+            
+            # 设置主要NLP处理器
+            if "zh_core_web_sm" in self.nlp_models:
+                self.nlp = self.nlp_models["zh_core_web_sm"]
+                logger.info("🎯 主要使用中文spaCy模型")
+            elif "en_core_web_sm" in self.nlp_models:
+                self.nlp = self.nlp_models["en_core_web_sm"]
+                logger.info("🎯 主要使用英文spaCy模型")
+            else:
+                self.nlp = None
+                logger.warning("⚠️ 无可用spaCy模型,实体提取将受限")
+            
+            if loaded_models:
+                logger.info(f"📚 可用语言模型: {', '.join(loaded_models)}")
+                
+        except ImportError:
+            logger.warning("⚠️ spaCy不可用,实体提取将受限")
+            self.nlp = None
+            self.nlp_models = {}
+    
+    def add(self, memory_item: MemoryItem) -> str:
+        """添加语义记忆"""
+        try:
+            # 1. 生成文本嵌入
+            embedding = self.embedding_model.encode(memory_item.content)
+            self.memory_embeddings[memory_item.id] = embedding
+            
+            # 2. 提取实体和关系
+            entities = self._extract_entities(memory_item.content)
+            relations = self._extract_relations(memory_item.content, entities)
+            
+            # 3. 存储到Neo4j图数据库
+            for entity in entities:
+                self._add_entity_to_graph(entity, memory_item)
+            
+            for relation in relations:
+                self._add_relation_to_graph(relation, memory_item)
+            
+            # 4. 存储到Qdrant向量数据库
+            metadata = {
+                "memory_id": memory_item.id,
+                "user_id": memory_item.user_id,
+                "content": memory_item.content,
+                "memory_type": memory_item.memory_type,
+                "timestamp": int(memory_item.timestamp.timestamp()),
+                "importance": memory_item.importance,
+                "entities": [e.entity_id for e in entities],
+                "entity_count": len(entities),
+                "relation_count": len(relations)
+            }
+            
+            success = self.vector_store.add_vectors(
+                vectors=[embedding.tolist()],
+                metadata=[metadata],
+                ids=[memory_item.id]
+            )
+            
+            if not success:
+                logger.warning("⚠️ 向量存储失败,但记忆已添加到图数据库")
+            
+            # 5. 添加实体信息到元数据
+            memory_item.metadata["entities"] = [e.entity_id for e in entities]
+            memory_item.metadata["relations"] = [
+                f"{r.from_entity}-{r.relation_type}-{r.to_entity}" for r in relations
+            ]
+            
+            # 6. 存储记忆
+            self.semantic_memories.append(memory_item)
+            
+            logger.info(f"✅ 添加语义记忆: {len(entities)}个实体, {len(relations)}个关系")
+            return memory_item.id
+        
+        except Exception as e:
+            logger.error(f"❌ 添加语义记忆失败: {e}")
+            raise
+    
+    def retrieve(self, query: str, limit: int = 5, **kwargs) -> List[MemoryItem]:
+        """检索语义记忆"""
+        try:
+            user_id = kwargs.get("user_id")
+
+            # 1. 向量检索
+            vector_results = self._vector_search(query, limit * 2, user_id)
+            
+            # 2. 图检索
+            graph_results = self._graph_search(query, limit * 2, user_id)
+            
+            # 3. 混合排序
+            combined_results = self._combine_and_rank_results(
+                vector_results, graph_results, query, limit
+            )
+
+            # 3.1 计算概率(对 combined_score 做 softmax 归一化)
+            scores = [r.get("combined_score", r.get("vector_score", 0.0)) for r in combined_results]
+            if scores:
+                import math
+                max_s = max(scores)
+                exps = [math.exp(s - max_s) for s in scores]
+                denom = sum(exps) or 1.0
+                probs = [e / denom for e in exps]
+            else:
+                probs = []
+            
+            # 4. 过滤已遗忘记忆并转换为MemoryItem
+            result_memories = []
+            for idx, result in enumerate(combined_results):
+                memory_id = result.get("memory_id")
+                
+                # 检查是否已遗忘
+                memory = next((m for m in self.semantic_memories if m.id == memory_id), None)
+                if memory and memory.metadata.get("forgotten", False):
+                    continue  # 跳过已遗忘的记忆
+                
+                # 处理时间戳
+                timestamp = result.get("timestamp")
+                if isinstance(timestamp, str):
+                    try:
+                        timestamp = datetime.fromisoformat(timestamp)
+                    except ValueError:
+                        timestamp = datetime.now()
+                elif isinstance(timestamp, (int, float)):
+                    timestamp = datetime.fromtimestamp(timestamp)
+                else:
+                    timestamp = datetime.now()
+                
+                # 直接从结果数据构建MemoryItem(附带分数与概率)
+                memory_item = MemoryItem(
+                    id=result["memory_id"],
+                    content=result["content"],
+                    memory_type="semantic",
+                    user_id=result.get("user_id", "default"),
+                    timestamp=timestamp,
+                    importance=result.get("importance", 0.5),
+                    metadata={
+                        **result.get("metadata", {}),
+                        "combined_score": result.get("combined_score", 0.0),
+                        "vector_score": result.get("vector_score", 0.0),
+                        "graph_score": result.get("graph_score", 0.0),
+                        "probability": probs[idx] if idx < len(probs) else 0.0,
+                    }
+                )
+                result_memories.append(memory_item)
+            
+            logger.info(f"✅ 检索到 {len(result_memories)} 条相关记忆")
+            return result_memories[:limit]
+                
+        except Exception as e:
+            logger.error(f"❌ 检索语义记忆失败: {e}")
+            return []
+    
+    def _vector_search(self, query: str, limit: int, user_id: Optional[str] = None) -> List[Dict[str, Any]]:
+        """Qdrant向量搜索"""
+        try:
+            # 生成查询向量
+            query_embedding = self.embedding_model.encode(query)
+            
+            # 构建过滤条件
+            where_filter = {"memory_type": "semantic"}
+            if user_id:
+                where_filter["user_id"] = user_id
+
+            # Qdrant向量检索
+            results = self.vector_store.search_similar(
+                query_vector=query_embedding.tolist(),
+                limit=limit,
+                where=where_filter if where_filter else None
+            )
+
+            # 转换结果格式以保持兼容性
+            formatted_results = []
+            for result in results:
+                formatted_result = {
+                    "id": result["id"],
+                    "score": result["score"],
+                    **result["metadata"]  # 包含所有元数据
+                }
+                formatted_results.append(formatted_result)
+
+            logger.debug(f"🔍 Qdrant向量搜索返回 {len(formatted_results)} 个结果")
+            return formatted_results
+                
+        except Exception as e:
+            logger.error(f"❌ Qdrant向量搜索失败: {e}")
+            return []
+
+    def _graph_search(self, query: str, limit: int, user_id: Optional[str] = None) -> List[Dict[str, Any]]:
+        """Neo4j图搜索"""
+        try:
+            # 从查询中提取实体
+            query_entities = self._extract_entities(query)
+            
+            if not query_entities:
+                # 如果没有提取到实体,尝试按名称搜索
+                entities_by_name = self.graph_store.search_entities_by_name(
+                    name_pattern=query, 
+                    limit=10
+                )
+                if entities_by_name:
+                    query_entities = [Entity(
+                        entity_id=e["id"],
+                        name=e["name"],
+                        entity_type=e["type"]
+                    ) for e in entities_by_name[:3]]
+                else:
+                    return []
+            
+            # 在Neo4j图中查找相关实体和记忆
+            related_memory_ids = set()
+            
+            for entity in query_entities:
+                try:
+                    # 查找相关实体
+                    related_entities = self.graph_store.find_related_entities(
+                        entity_id=entity.entity_id,
+                        max_depth=2,
+                        limit=20
+                    )
+                    
+                    # 收集相关记忆ID
+                    for rel_entity in related_entities:
+                        if "memory_id" in rel_entity:
+                            related_memory_ids.add(rel_entity["memory_id"])
+                    
+                    # 也添加直接匹配的实体记忆
+                    entity_rels = self.graph_store.get_entity_relationships(entity.entity_id)
+                    for rel in entity_rels:
+                        rel_data = rel.get("relationship", {})
+                        if "memory_id" in rel_data:
+                            related_memory_ids.add(rel_data["memory_id"])
+                            
+                except Exception as e:
+                    logger.debug(f"图搜索实体 {entity.entity_id} 失败: {e}")
+                    continue
+            
+            # 构建结果 - 从向量数据库获取完整记忆信息
+            results = []
+            for memory_id in list(related_memory_ids)[:limit * 2]:  # 获取更多候选
+                try:
+                    # 优先从本地缓存获取记忆详情,避免占位向量维度不一致问题
+                    mem = self._find_memory_by_id(memory_id)
+                    if not mem:
+                        continue
+
+                    if user_id and mem.user_id != user_id:
+                        continue
+
+                    metadata = {
+                        "content": mem.content,
+                        "user_id": mem.user_id,
+                        "memory_type": mem.memory_type,
+                        "importance": mem.importance,
+                        "timestamp": int(mem.timestamp.timestamp()),
+                        "entities": mem.metadata.get("entities", [])
+                    }
+
+                    # 计算图相关性分数
+                    graph_score = self._calculate_graph_relevance_neo4j(metadata, query_entities)
+
+                    results.append({
+                        "id": memory_id,
+                        "memory_id": memory_id,
+                        "content": metadata.get("content", ""),
+                        "similarity": graph_score,
+                        "user_id": metadata.get("user_id"),
+                        "memory_type": metadata.get("memory_type"),
+                        "importance": metadata.get("importance", 0.5),
+                        "timestamp": metadata.get("timestamp"),
+                        "entities": metadata.get("entities", [])
+                    })
+
+                except Exception as e:
+                    logger.debug(f"获取记忆 {memory_id} 详情失败: {e}")
+                    continue
+            
+            # 按图相关性排序
+            results.sort(key=lambda x: x["similarity"], reverse=True)
+            logger.debug(f"🕸️ Neo4j图搜索返回 {len(results)} 个结果")
+            return results[:limit]
+            
+        except Exception as e:
+            logger.error(f"❌ Neo4j图搜索失败: {e}")
+            return []
+
+    def _combine_and_rank_results(
+        self,
+        vector_results: List[Dict[str, Any]],
+        graph_results: List[Dict[str, Any]],
+        query: str,
+        limit: int
+    ) -> List[Dict[str, Any]]:
+        """混合排序结果 - 仅基于向量与图分数的简单融合"""
+        # 合并结果,按内容去重
+        combined = {}
+        content_seen = set()  # 用于内容去重
+        
+        # 添加向量结果
+        for result in vector_results:
+            memory_id = result["memory_id"]
+            content = result.get("content", "")
+            
+            # 内容去重:检查是否已经有相同或高度相似的内容
+            content_hash = hash(content.strip())
+            if content_hash in content_seen:
+                logger.debug(f"⚠️ 跳过重复内容: {content[:30]}...")
+                continue
+            
+            content_seen.add(content_hash)
+            combined[memory_id] = {
+                **result,
+                "vector_score": result.get("score", 0.0), 
+                "graph_score": 0.0,
+                "content_hash": content_hash
+            }
+        
+        # 添加图结果
+        for result in graph_results:
+            memory_id = result["memory_id"]
+            content = result.get("content", "")
+            content_hash = hash(content.strip())
+            
+            if memory_id in combined:
+                combined[memory_id]["graph_score"] = result.get("similarity", 0.0)
+            elif content_hash not in content_seen:
+                content_seen.add(content_hash)
+                combined[memory_id] = {
+                    **result,
+                    "vector_score": 0.0,
+                    "graph_score": result.get("similarity", 0.0),
+                    "content_hash": content_hash
+                }
+        
+        # 计算混合分数:相似度为主,重要性为辅助排序因子
+        for memory_id, result in combined.items():
+            vector_score = result["vector_score"]
+            graph_score = result["graph_score"]
+            importance = result.get("importance", 0.5)
+            
+            # 新评分算法:向量检索纯基于相似度,重要性作为加权因子
+            # 基础相似度得分(不受重要性影响)
+            base_relevance = vector_score * 0.7 + graph_score * 0.3
+            
+            # 重要性作为乘法加权因子,范围 [0.8, 1.2]
+            # importance in [0,1] -> weight in [0.8,1.2]
+            importance_weight = 0.8 + (importance * 0.4)
+            
+            # 最终得分:相似度 * 重要性权重
+            combined_score = base_relevance * importance_weight
+            
+            # 调试信息:查看分数分解
+            result["debug_info"] = {
+                "base_relevance": base_relevance,
+                "importance_weight": importance_weight,
+                "combined_score": combined_score
+            }
+
+            result["combined_score"] = combined_score
+        
+        # 应用最小相关性阈值
+        min_threshold = 0.1  # 最小相关性阈值
+        filtered_results = [
+            result for result in combined.values() 
+            if result["combined_score"] >= min_threshold
+        ]
+
+        # 排序并返回
+        sorted_results = sorted(
+            filtered_results,
+            key=lambda x: x["combined_score"],
+            reverse=True
+        )
+        
+        # 调试信息
+        logger.debug(f"🔍 向量结果: {len(vector_results)}, 图结果: {len(graph_results)}")
+        logger.debug(f"📝 去重后: {len(combined)}, 过滤后: {len(filtered_results)}")
+        
+        if logger.level <= logging.DEBUG:
+            for i, result in enumerate(sorted_results[:3]):
+                logger.debug(f"  结果{i+1}: 向量={result['vector_score']:.3f}, 图={result['graph_score']:.3f}, 精确={result.get('exact_match_bonus', 0):.3f}, 关键词={result.get('keyword_bonus', 0):.3f}, 公司={result.get('company_bonus', 0):.3f}, 实体={result.get('entity_type_bonus', 0):.3f}, 综合={result['combined_score']:.3f}")
+        
+        return sorted_results[:limit]
+    
+    def _detect_language(self, text: str) -> str:
+        """简单的语言检测"""
+        # 统计中文字符比例(无正则,逐字符判断范围)
+        chinese_chars = sum(1 for ch in text if '\u4e00' <= ch <= '\u9fff')
+        total_chars = len(text.replace(' ', ''))
+        
+        if total_chars == 0:
+            return "en"
+        
+        chinese_ratio = chinese_chars / total_chars
+        return "zh" if chinese_ratio > 0.3 else "en"
+    
+    def _extract_entities(self, text: str) -> List[Entity]:
+        """智能多语言实体提取"""
+        entities = []
+        
+        # 检测文本语言
+        lang = self._detect_language(text)
+        
+        # 选择合适的spaCy模型
+        selected_nlp = None
+        if lang == "zh" and "zh_core_web_sm" in self.nlp_models:
+            selected_nlp = self.nlp_models["zh_core_web_sm"]
+        elif lang == "en" and "en_core_web_sm" in self.nlp_models:
+            selected_nlp = self.nlp_models["en_core_web_sm"]
+        else:
+            # 使用默认模型
+            selected_nlp = self.nlp
+        
+        logger.debug(f"🌐 检测语言: {lang}, 使用模型: {selected_nlp.meta['name'] if selected_nlp else 'None'}")
+        
+        # 使用spaCy进行实体识别和词法分析
+        if selected_nlp:
+            try:
+                doc = selected_nlp(text)
+                logger.debug(f"📝 spaCy处理文本: '{text}' -> {len(doc.ents)} 个实体")
+                
+                # 存储词法分析结果,供Neo4j使用
+                self._store_linguistic_analysis(doc, text)
+                
+                if not doc.ents:
+                    # 如果没有实体,记录详细的词元信息
+                    logger.debug("🔍 未找到实体,词元分析:")
+                    for token in doc[:5]:  # 只显示前5个词元
+                        logger.debug(f"   '{token.text}' -> POS: {token.pos_}, TAG: {token.tag_}, ENT_IOB: {token.ent_iob_}")
+                
+                for ent in doc.ents:
+                    entity = Entity(
+                        entity_id=f"entity_{hash(ent.text)}",
+                        name=ent.text,
+                        entity_type=ent.label_,
+                        description=f"从文本中识别的{ent.label_}实体"
+                    )
+                    entities.append(entity)
+                    # 安全获取置信度信息
+                    confidence = "N/A"
+                    try:
+                        if hasattr(ent._, 'confidence'):
+                            confidence = getattr(ent._, 'confidence', 'N/A')
+                    except:
+                        confidence = "N/A"
+                    
+                    logger.debug(f"🏷️ spaCy识别实体: '{ent.text}' -> {ent.label_} (置信度: {confidence})")
+                
+            except Exception as e:
+                logger.warning(f"⚠️ spaCy实体识别失败: {e}")
+                import traceback
+                logger.debug(f"详细错误: {traceback.format_exc()}")
+        else:
+            logger.warning("⚠️ 没有可用的spaCy模型进行实体识别")
+        
+        return entities
+    
+    def _store_linguistic_analysis(self, doc, text: str):
+        """存储spaCy词法分析结果到Neo4j"""
+        if not self.graph_store:
+            return
+            
+        try:
+            # 为每个词元创建节点
+            for token in doc:
+                # 跳过标点符号和空格
+                if token.is_punct or token.is_space:
+                    continue
+                    
+                token_id = f"token_{hash(token.text + token.pos_)}"
+                
+                # 添加词元节点到Neo4j
+                self.graph_store.add_entity(
+                    entity_id=token_id,
+                    name=token.text,
+                    entity_type="TOKEN",
+                    properties={
+                        "pos": token.pos_,        # 词性(NOUN, VERB等)
+                        "tag": token.tag_,        # 细粒度标签
+                        "lemma": token.lemma_,    # 词元原形
+                        "is_alpha": token.is_alpha,
+                        "is_stop": token.is_stop,
+                        "source_text": text[:50],  # 来源文本片段
+                        "language": self._detect_language(text)
+                    }
+                )
+                
+                # 如果是名词,可能是潜在的概念
+                if token.pos_ in ["NOUN", "PROPN"]:
+                    concept_id = f"concept_{hash(token.text)}"
+                    self.graph_store.add_entity(
+                        entity_id=concept_id,
+                        name=token.text,
+                        entity_type="CONCEPT",
+                        properties={
+                            "category": token.pos_,
+                            "frequency": 1,  # 可以后续累计
+                            "source_text": text[:50]
+                        }
+                    )
+                    
+                    # 建立词元到概念的关系
+                    self.graph_store.add_relationship(
+                        from_entity_id=token_id,
+                        to_entity_id=concept_id,
+                        relationship_type="REPRESENTS",
+                        properties={"confidence": 1.0}
+                    )
+            
+            # 建立词元之间的依存关系
+            for token in doc:
+                if token.is_punct or token.is_space or token.head == token:
+                    continue
+                    
+                from_id = f"token_{hash(token.text + token.pos_)}"
+                to_id = f"token_{hash(token.head.text + token.head.pos_)}"
+                
+                # Neo4j不允许关系类型包含冒号,需要清理
+                relation_type = token.dep_.upper().replace(":", "_")
+                
+                self.graph_store.add_relationship(
+                    from_entity_id=from_id,
+                    to_entity_id=to_id,
+                    relationship_type=relation_type,  # 清理后的依存关系类型
+                    properties={
+                        "dependency": token.dep_,  # 保留原始依存关系
+                        "source_text": text[:50]
+                    }
+                )
+            
+            logger.debug(f"🔗 已将词法分析结果存储到Neo4j: {len([t for t in doc if not t.is_punct and not t.is_space])} 个词元")
+            
+        except Exception as e:
+            logger.warning(f"⚠️ 存储词法分析失败: {e}")
+    
+    def _extract_relations(self, text: str, entities: List[Entity]) -> List[Relation]:
+        """提取关系"""
+        relations = []
+        # 仅保留简单共现关系,不做任何正则/关键词匹配
+        for i, entity1 in enumerate(entities):
+            for entity2 in entities[i+1:]:
+                relations.append(Relation(
+                    from_entity=entity1.entity_id,
+                    to_entity=entity2.entity_id,
+                    relation_type="CO_OCCURS",
+                    strength=0.5,
+                    evidence=text[:100]
+                ))
+        return relations
+    
+    def _add_entity_to_graph(self, entity: Entity, memory_item: MemoryItem):
+        """添加实体到Neo4j图数据库"""
+        try:
+            # 准备实体属性
+            properties = {
+                "name": entity.name,
+                "description": entity.description,
+                "frequency": entity.frequency,
+                "memory_id": memory_item.id,
+                "user_id": memory_item.user_id,
+                "importance": memory_item.importance,
+                **entity.properties
+            }
+            
+            # 添加到Neo4j
+            success = self.graph_store.add_entity(
+                entity_id=entity.entity_id,
+                name=entity.name,
+                entity_type=entity.entity_type,
+                properties=properties
+            )
+            
+            if success:
+                # 同时更新本地缓存
+                if entity.entity_id in self.entities:
+                    self.entities[entity.entity_id].frequency += 1
+                    self.entities[entity.entity_id].updated_at = datetime.now()
+                else:
+                    self.entities[entity.entity_id] = entity
+                    
+            return success
+            
+        except Exception as e:
+            logger.error(f"❌ 添加实体到图数据库失败: {e}")
+            return False
+    
+    def _add_relation_to_graph(self, relation: Relation, memory_item: MemoryItem):
+        """添加关系到Neo4j图数据库"""
+        try:
+            # 准备关系属性
+            properties = {
+                "strength": relation.strength,
+                "memory_id": memory_item.id,
+                "user_id": memory_item.user_id,
+                "importance": memory_item.importance,
+                "evidence": relation.evidence
+            }
+            
+            # 添加到Neo4j
+            success = self.graph_store.add_relationship(
+                from_entity_id=relation.from_entity,
+                to_entity_id=relation.to_entity,
+                relationship_type=relation.relation_type,
+                properties=properties
+            )
+            
+            if success:
+                # 同时更新本地缓存
+                self.relations.append(relation)
+                
+            return success
+            
+        except Exception as e:
+            logger.error(f"❌ 添加关系到图数据库失败: {e}")
+            return False
+    
+    def _calculate_graph_relevance_neo4j(self, memory_metadata: Dict[str, Any], query_entities: List[Entity]) -> float:
+        """计算Neo4j图相关性分数"""
+        try:
+            memory_entities = memory_metadata.get("entities", [])
+            if not memory_entities or not query_entities:
+                return 0.0
+            
+            # 实体匹配度
+            query_entity_ids = {e.entity_id for e in query_entities}
+            matching_entities = len(set(memory_entities).intersection(query_entity_ids))
+            entity_score = matching_entities / len(query_entity_ids) if query_entity_ids else 0
+            
+            # 实体数量加权
+            entity_count = memory_metadata.get("entity_count", 0)
+            entity_density = min(entity_count / 10, 1.0)  # 归一化到[0,1]
+            
+            # 关系数量加权
+            relation_count = memory_metadata.get("relation_count", 0)
+            relation_density = min(relation_count / 5, 1.0)  # 归一化到[0,1]
+            
+            # 综合分数
+            relevance_score = (
+                entity_score * 0.6 +           # 实体匹配权重60%
+                entity_density * 0.2 +         # 实体密度权重20%
+                relation_density * 0.2         # 关系密度权重20%
+            )
+            
+            return min(relevance_score, 1.0)
+            
+        except Exception as e:
+            logger.debug(f"计算图相关性失败: {e}")
+            return 0.0
+
+    def _add_or_update_entity(self, entity: Entity):
+        """添加或更新实体"""
+        if entity.entity_id in self.entities:
+            # 更新现有实体
+            existing = self.entities[entity.entity_id]
+            existing.frequency += 1
+            existing.updated_at = datetime.now()
+        else:
+            # 添加新实体
+            self.entities[entity.entity_id] = entity
+    
+    def _add_or_update_relation(self, relation: Relation):
+        """添加或更新关系"""
+        # 检查是否已存在相同关系
+        existing_relation = None
+        for r in self.relations:
+            if (r.from_entity == relation.from_entity and
+                r.to_entity == relation.to_entity and
+                r.relation_type == relation.relation_type):
+                existing_relation = r
+                break
+        
+        if existing_relation:
+            # 更新现有关系
+            existing_relation.frequency += 1
+            existing_relation.strength = min(1.0, existing_relation.strength + 0.1)
+        else:
+            # 添加新关系
+            self.relations.append(relation)
+    
+    # 旧的图相关性计算方法已被 _calculate_graph_relevance_neo4j 替代
+    
+    def _find_memory_by_id(self, memory_id: str) -> Optional[MemoryItem]:
+        """根据ID查找记忆"""
+        logger.debug(f"🔍 查找记忆ID: {memory_id}, 当前记忆数: {len(self.semantic_memories)}")
+        for memory in self.semantic_memories:
+            if memory.id == memory_id:
+                logger.debug(f"✅ 找到记忆: {memory.content[:50]}...")
+                return memory
+        logger.debug(f"❌ 未找到记忆ID: {memory_id}")
+        return None
+    
+    def update(
+        self,
+        memory_id: str,
+        content: str = None,
+        importance: float = None,
+        metadata: Dict[str, Any] = None
+    ) -> bool:
+        """更新语义记忆"""
+        memory = self._find_memory_by_id(memory_id)
+        if not memory:
+            return False
+        
+        try:
+            if content is not None:
+                # 重新生成嵌入和提取实体
+                embedding = self.embedding_model.encode(content)
+                self.memory_embeddings[memory_id] = embedding
+                
+                # 清理旧的实体关系
+                old_entities = memory.metadata.get("entities", [])
+                self._cleanup_entities_and_relations(old_entities)
+                
+                # 提取新的实体和关系
+                memory.content = content
+                entities = self._extract_entities(content)
+                relations = self._extract_relations(content, entities)
+                
+                # 更新知识图谱
+                for entity in entities:
+                    self._add_or_update_entity(entity)
+                for relation in relations:
+                    self._add_or_update_relation(relation)
+                
+                # 更新元数据
+                memory.metadata["entities"] = [e.entity_id for e in entities]
+                memory.metadata["relations"] = [
+                    f"{r.from_entity}-{r.relation_type}-{r.to_entity}" for r in relations
+                ]
+                
+            if importance is not None:
+                memory.importance = importance
+            
+            if metadata is not None:
+                memory.metadata.update(metadata)
+                
+                return True
+            
+        except Exception as e:
+            logger.error(f"❌ 更新记忆失败: {e}")
+        return False
+    
+    def remove(self, memory_id: str) -> bool:
+        """删除语义记忆"""
+        memory = self._find_memory_by_id(memory_id)
+        if not memory:
+            return False
+        
+        try:
+            # 删除向量
+            self.vector_store.delete_memories([memory_id])
+            
+            # 清理实体和关系
+            entities = memory.metadata.get("entities", [])
+            self._cleanup_entities_and_relations(entities)
+            
+            # 删除记忆
+            self.semantic_memories.remove(memory)
+            if memory_id in self.memory_embeddings:
+                del self.memory_embeddings[memory_id]
+                
+                return True
+            
+        except Exception as e:
+            logger.error(f"❌ 删除记忆失败: {e}")
+        return False
+    
+    def _cleanup_entities_and_relations(self, entity_ids: List[str]):
+        """清理实体和关系"""
+        # 这里可以实现更智能的清理逻辑
+        # 例如,如果实体不再被任何记忆引用,则删除它
+        pass
+    
+    def has_memory(self, memory_id: str) -> bool:
+        """检查记忆是否存在"""
+        return self._find_memory_by_id(memory_id) is not None
+    
+    def forget(self, strategy: str = "importance_based", threshold: float = 0.1, max_age_days: int = 30) -> int:
+        """语义记忆遗忘机制(硬删除)"""
+        forgotten_count = 0
+        current_time = datetime.now()
+        
+        to_remove = []  # 收集要删除的记忆ID
+        
+        for memory in self.semantic_memories:
+            should_forget = False
+            
+            if strategy == "importance_based":
+                # 基于重要性遗忘
+                if memory.importance < threshold:
+                    should_forget = True
+            elif strategy == "time_based":
+                # 基于时间遗忘
+                cutoff_time = current_time - timedelta(days=max_age_days)
+                if memory.timestamp < cutoff_time:
+                    should_forget = True
+            elif strategy == "capacity_based":
+                # 基于容量遗忘(保留最重要的)
+                if len(self.semantic_memories) > self.config.max_capacity:
+                    sorted_memories = sorted(self.semantic_memories, key=lambda m: m.importance)
+                    excess_count = len(self.semantic_memories) - self.config.max_capacity
+                    if memory in sorted_memories[:excess_count]:
+                        should_forget = True
+            
+            if should_forget:
+                to_remove.append(memory.id)
+        
+        # 执行硬删除
+        for memory_id in to_remove:
+            if self.remove(memory_id):
+                forgotten_count += 1
+                logger.info(f"语义记忆硬删除: {memory_id[:8]}... (策略: {strategy})")
+        
+        return forgotten_count
+
+    def clear(self):
+        """清空所有语义记忆 - 包括专业数据库"""
+        try:
+            # 清空Qdrant向量数据库
+            if self.vector_store:
+                success = self.vector_store.clear_collection()
+                if success:
+                    logger.info("✅ Qdrant向量数据库已清空")
+                else:
+                    logger.warning("⚠️ Qdrant清空失败")
+            
+            # 清空Neo4j图数据库
+            if self.graph_store:
+                success = self.graph_store.clear_all()
+                if success:
+                    logger.info("✅ Neo4j图数据库已清空")
+                else:
+                    logger.warning("⚠️ Neo4j清空失败")
+            
+            # 清空本地缓存
+            self.semantic_memories.clear()
+            self.memory_embeddings.clear()
+            self.entities.clear()
+            self.relations.clear()
+            
+            logger.info("🧹 语义记忆系统已完全清空")
+            
+        except Exception as e:
+            logger.error(f"❌ 清空语义记忆失败: {e}")
+            # 即使数据库清空失败,也要清空本地缓存
+        self.semantic_memories.clear()
+        self.memory_embeddings.clear()
+        self.entities.clear()
+        self.relations.clear()
+
+    def get_all(self) -> List[MemoryItem]:
+        """获取所有语义记忆"""
+        return self.semantic_memories.copy()
+    
+    def get_stats(self) -> Dict[str, Any]:
+        """获取语义记忆统计信息"""
+        graph_stats = {}
+        try:
+            if self.graph_store:
+                graph_stats = self.graph_store.get_stats() or {}
+        except Exception:
+            graph_stats = {}
+
+        # 硬删除模式:所有记忆都是活跃的
+        active_memories = self.semantic_memories
+
+        return {
+            "count": len(active_memories),  # 活跃记忆数量
+            "forgotten_count": 0,  # 硬删除模式下已遗忘的记忆会被直接删除
+            "total_count": len(self.semantic_memories),  # 总记忆数量
+            "entities_count": len(self.entities),
+            "relations_count": len(self.relations),
+            "graph_nodes": graph_stats.get("total_nodes", 0),
+            "graph_edges": graph_stats.get("total_relationships", 0),
+            "avg_importance": sum(m.importance for m in active_memories) / len(active_memories) if active_memories else 0.0,
+            "memory_type": "enhanced_semantic"
+        }
+    
+    def get_entity(self, entity_id: str) -> Optional[Entity]:
+        """获取实体"""
+        return self.entities.get(entity_id)
+    
+    def search_entities(self, query: str, limit: int = 10) -> List[Entity]:
+        """搜索实体"""
+        query_lower = query.lower()
+        scored_entities = []
+        
+        for entity in self.entities.values():
+            score = 0.0
+            
+            # 名称匹配
+            if query_lower in entity.name.lower():
+                score += 2.0
+            
+            # 类型匹配
+            if query_lower in entity.entity_type.lower():
+                score += 1.0
+            
+            # 描述匹配
+            if query_lower in entity.description.lower():
+                score += 0.5
+            
+            # 频率权重
+            score *= math.log(1 + entity.frequency)
+            
+            if score > 0:
+                scored_entities.append((score, entity))
+        
+        scored_entities.sort(key=lambda x: x[0], reverse=True)
+        return [entity for _, entity in scored_entities[:limit]]
+    
+    def get_related_entities(
+        self,
+        entity_id: str,
+        relation_types: List[str] = None,
+        max_hops: int = 2
+    ) -> List[Dict[str, Any]]:
+        """获取相关实体 - 使用Neo4j图数据库"""
+        
+        related = []
+        
+        try:
+            # 使用Neo4j图数据库查找相关实体
+            if not self.graph_store:
+                logger.warning("⚠️ Neo4j图数据库不可用")
+                return []
+            
+            # 使用Neo4j查找相关实体
+            related_entities = self.graph_store.find_related_entities(
+                entity_id=entity_id,
+                relationship_types=relation_types,
+                max_depth=max_hops,
+                limit=50
+            )
+            
+            # 转换格式以保持兼容性
+            for entity_data in related_entities:
+                # 尝试从本地缓存获取实体对象
+                entity_obj = self.entities.get(entity_data.get("id"))
+                if not entity_obj:
+                    # 如果本地缓存没有,创建临时实体对象
+                    entity_obj = Entity(
+                        entity_id=entity_data.get("id", entity_id),
+                        name=entity_data.get("name", ""),
+                        entity_type=entity_data.get("type", "MISC")
+                    )
+                
+                    related.append({
+                    "entity": entity_obj,
+                    "relation_type": entity_data.get("relationship_path", ["RELATED"])[-1] if entity_data.get("relationship_path") else "RELATED",
+                    "strength": 1.0 / max(entity_data.get("distance", 1), 1),  # 距离越近强度越高
+                    "distance": entity_data.get("distance", max_hops)
+                })
+            
+            # 按距离和强度排序
+            related.sort(key=lambda x: (x["distance"], -x["strength"]))
+            
+        except Exception as e:
+            logger.error(f"❌ 获取相关实体失败: {e}")
+        
+        return related
+    
+    def export_knowledge_graph(self) -> Dict[str, Any]:
+        """导出知识图谱 - 从Neo4j获取统计信息"""
+        try:
+            # 从Neo4j获取统计信息
+            stats = {}
+            if self.graph_store:
+                stats = self.graph_store.get_stats()
+            
+            return {
+                "entities": {eid: entity.to_dict() for eid, entity in self.entities.items()},
+                "relations": [relation.to_dict() for relation in self.relations],
+                "graph_stats": {
+                    "total_nodes": stats.get("total_nodes", 0),
+                    "entity_nodes": stats.get("entity_nodes", 0),
+                    "memory_nodes": stats.get("memory_nodes", 0),
+                    "total_relationships": stats.get("total_relationships", 0),
+                    "cached_entities": len(self.entities),
+                    "cached_relations": len(self.relations)
+                }
+            }
+        except Exception as e:
+            logger.error(f"❌ 导出知识图谱失败: {e}")
+            return {
+                "entities": {},
+                "relations": [],
+                "graph_stats": {"error": str(e)}
+            }

+ 411 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/memory/types/working.py

@@ -0,0 +1,411 @@
+"""工作记忆实现
+
+按照第8章架构设计的工作记忆,提供:
+- 短期上下文管理
+- 容量和时间限制
+- 优先级管理
+- 自动清理机制
+"""
+
+from typing import List, Dict, Any
+from datetime import datetime, timedelta
+import heapq
+
+from ..base import BaseMemory, MemoryItem, MemoryConfig
+
+class WorkingMemory(BaseMemory):
+    """工作记忆实现
+    
+    特点:
+    - 容量有限(通常10-20条记忆)
+    - 时效性强(会话级别)
+    - 优先级管理
+    - 自动清理过期记忆
+    """
+    
+    def __init__(self, config: MemoryConfig, storage_backend=None):
+        super().__init__(config, storage_backend)
+        
+        # 工作记忆特定配置
+        self.max_capacity = self.config.working_memory_capacity
+        self.max_tokens = self.config.working_memory_tokens
+        # 纯内存TTL(分钟),可通过在 MemoryConfig 上挂载 working_memory_ttl_minutes 覆盖
+        self.max_age_minutes = getattr(self.config, 'working_memory_ttl_minutes', 120)
+        self.current_tokens = 0
+        self.session_start = datetime.now()
+        
+        # 内存存储(工作记忆不需要持久化)
+        self.memories: List[MemoryItem] = []
+        
+        # 使用优先级队列管理记忆
+        self.memory_heap = []  # (priority, timestamp, memory_item)
+    
+    def add(self, memory_item: MemoryItem) -> str:
+        """添加工作记忆"""
+        # 过期清理
+        self._expire_old_memories()
+        # 计算优先级(重要性 + 时间衰减)
+        priority = self._calculate_priority(memory_item)
+        
+        # 添加到堆中
+        heapq.heappush(self.memory_heap, (-priority, memory_item.timestamp, memory_item))
+        self.memories.append(memory_item)
+        
+        # 更新token计数
+        self.current_tokens += len(memory_item.content.split())
+        
+        # 检查容量限制
+        self._enforce_capacity_limits()
+        
+        return memory_item.id
+    
+    def retrieve(self, query: str, limit: int = 5, user_id: str = None, **kwargs) -> List[MemoryItem]:
+        """检索工作记忆 - 混合语义向量检索和关键词匹配"""
+        # 过期清理
+        self._expire_old_memories()
+        if not self.memories:
+            return []
+
+        # 过滤已遗忘的记忆
+        active_memories = [m for m in self.memories if not m.metadata.get("forgotten", False)]
+        
+        # 按用户ID过滤(如果提供)
+        filtered_memories = active_memories
+        if user_id:
+            filtered_memories = [m for m in active_memories if m.user_id == user_id]
+
+        if not filtered_memories:
+            return []
+
+        # 尝试语义向量检索(如果有嵌入模型)
+        vector_scores = {}
+        try:
+            # 简单的语义相似度计算(使用TF-IDF或其他轻量级方法)
+            from sklearn.feature_extraction.text import TfidfVectorizer
+            from sklearn.metrics.pairwise import cosine_similarity
+            import numpy as np
+            
+            # 准备文档
+            documents = [query] + [m.content for m in filtered_memories]
+            
+            # TF-IDF向量化
+            vectorizer = TfidfVectorizer(stop_words=None, lowercase=True)
+            tfidf_matrix = vectorizer.fit_transform(documents)
+            
+            # 计算相似度
+            query_vector = tfidf_matrix[0:1]
+            doc_vectors = tfidf_matrix[1:]
+            similarities = cosine_similarity(query_vector, doc_vectors).flatten()
+            
+            # 存储向量分数
+            for i, memory in enumerate(filtered_memories):
+                vector_scores[memory.id] = similarities[i]
+                
+        except Exception as e:
+            # 如果向量检索失败,回退到关键词匹配
+            vector_scores = {}
+
+        # 计算最终分数
+        query_lower = query.lower()
+        scored_memories = []
+        
+        for memory in filtered_memories:
+            content_lower = memory.content.lower()
+            
+            # 获取向量分数(如果有)
+            vector_score = vector_scores.get(memory.id, 0.0)
+            
+            # 关键词匹配分数
+            keyword_score = 0.0
+            if query_lower in content_lower:
+                keyword_score = len(query_lower) / len(content_lower)
+            else:
+                # 分词匹配
+                query_words = set(query_lower.split())
+                content_words = set(content_lower.split())
+                intersection = query_words.intersection(content_words)
+                if intersection:
+                    keyword_score = len(intersection) / len(query_words.union(content_words)) * 0.8
+
+            # 混合分数:向量检索 + 关键词匹配
+            if vector_score > 0:
+                base_relevance = vector_score * 0.7 + keyword_score * 0.3
+            else:
+                base_relevance = keyword_score
+            
+            # 时间衰减
+            time_decay = self._calculate_time_decay(memory.timestamp)
+            base_relevance *= time_decay
+            
+            # 重要性权重
+            importance_weight = 0.8 + (memory.importance * 0.4)
+            final_score = base_relevance * importance_weight
+            
+            if final_score > 0:
+                scored_memories.append((final_score, memory))
+
+        # 按分数排序并返回
+        scored_memories.sort(key=lambda x: x[0], reverse=True)
+        return [memory for _, memory in scored_memories[:limit]]
+    
+    def update(
+        self,
+        memory_id: str,
+        content: str = None,
+        importance: float = None,
+        metadata: Dict[str, Any] = None
+    ) -> bool:
+        """更新工作记忆"""
+        for memory in self.memories:
+            if memory.id == memory_id:
+                old_tokens = len(memory.content.split())
+                
+                if content is not None:
+                    memory.content = content
+                    # 更新token计数
+                    new_tokens = len(content.split())
+                    self.current_tokens = self.current_tokens - old_tokens + new_tokens
+                
+                if importance is not None:
+                    memory.importance = importance
+                
+                if metadata is not None:
+                    memory.metadata.update(metadata)
+                
+                # 重新计算优先级并更新堆
+                self._update_heap_priority(memory)
+                
+                return True
+        return False
+    
+    def remove(self, memory_id: str) -> bool:
+        """删除工作记忆"""
+        for i, memory in enumerate(self.memories):
+            if memory.id == memory_id:
+                # 从列表中删除
+                removed_memory = self.memories.pop(i)
+                
+                # 从堆中删除(标记删除)
+                self._mark_deleted_in_heap(memory_id)
+                
+                # 更新token计数
+                self.current_tokens -= len(removed_memory.content.split())
+                self.current_tokens = max(0, self.current_tokens)
+                
+                return True
+        return False
+    
+    def has_memory(self, memory_id: str) -> bool:
+        """检查记忆是否存在"""
+        return any(memory.id == memory_id for memory in self.memories)
+    
+    def clear(self):
+        """清空所有工作记忆"""
+        self.memories.clear()
+        self.memory_heap.clear()
+        self.current_tokens = 0
+    
+    def get_stats(self) -> Dict[str, Any]:
+        """获取工作记忆统计信息"""
+        # 过期清理(惰性)
+        self._expire_old_memories()
+        
+        # 工作记忆中的记忆都是活跃的(已遗忘的记忆会被直接删除)
+        active_memories = self.memories
+        
+        return {
+            "count": len(active_memories),  # 活跃记忆数量
+            "forgotten_count": 0,  # 工作记忆中已遗忘的记忆会被直接删除
+            "total_count": len(self.memories),  # 总记忆数量
+            "current_tokens": self.current_tokens,
+            "max_capacity": self.max_capacity,
+            "max_tokens": self.max_tokens,
+            "max_age_minutes": self.max_age_minutes,
+            "session_duration_minutes": (datetime.now() - self.session_start).total_seconds() / 60,
+            "avg_importance": sum(m.importance for m in active_memories) / len(active_memories) if active_memories else 0.0,
+            "capacity_usage": len(active_memories) / self.max_capacity if self.max_capacity > 0 else 0.0,
+            "token_usage": self.current_tokens / self.max_tokens if self.max_tokens > 0 else 0.0,
+            "memory_type": "working"
+        }
+    
+    def get_recent(self, limit: int = 10) -> List[MemoryItem]:
+        """获取最近的记忆"""
+        sorted_memories = sorted(
+            self.memories, 
+            key=lambda x: x.timestamp, 
+            reverse=True
+        )
+        return sorted_memories[:limit]
+    
+    def get_important(self, limit: int = 10) -> List[MemoryItem]:
+        """获取重要记忆"""
+        sorted_memories = sorted(
+            self.memories,
+            key=lambda x: x.importance,
+            reverse=True
+        )
+        return sorted_memories[:limit]
+
+    def get_all(self) -> List[MemoryItem]:
+        """获取所有记忆"""
+        return self.memories.copy()
+    
+    def get_context_summary(self, max_length: int = 500) -> str:
+        """获取上下文摘要"""
+        if not self.memories:
+            return "No working memories available."
+        
+        # 按重要性和时间排序
+        sorted_memories = sorted(
+            self.memories,
+            key=lambda m: (m.importance, m.timestamp),
+            reverse=True
+        )
+        
+        summary_parts = []
+        current_length = 0
+        
+        for memory in sorted_memories:
+            content = memory.content
+            if current_length + len(content) <= max_length:
+                summary_parts.append(content)
+                current_length += len(content)
+            else:
+                # 截断最后一个记忆
+                remaining = max_length - current_length
+                if remaining > 50:  # 至少保留50个字符
+                    summary_parts.append(content[:remaining] + "...")
+                break
+        
+        return "Working Memory Context:\n" + "\n".join(summary_parts)
+    
+    def forget(self, strategy: str = "importance_based", threshold: float = 0.1, max_age_days: int = 1) -> int:
+        """工作记忆遗忘机制"""
+        forgotten_count = 0
+        current_time = datetime.now()
+        
+        to_remove = []
+        
+        # 始终先执行TTL过期(分钟级)
+        cutoff_ttl = current_time - timedelta(minutes=self.max_age_minutes)
+        for memory in self.memories:
+            if memory.timestamp < cutoff_ttl:
+                to_remove.append(memory.id)
+        
+        if strategy == "importance_based":
+            # 删除低重要性记忆
+            for memory in self.memories:
+                if memory.importance < threshold:
+                    to_remove.append(memory.id)
+        
+        elif strategy == "time_based":
+            # 删除过期记忆(工作记忆通常以小时计算)
+            cutoff_time = current_time - timedelta(hours=max_age_days * 24)
+            for memory in self.memories:
+                if memory.timestamp < cutoff_time:
+                    to_remove.append(memory.id)
+        
+        elif strategy == "capacity_based":
+            # 删除超出容量的记忆
+            if len(self.memories) > self.max_capacity:
+                # 按优先级排序,删除最低的
+                sorted_memories = sorted(
+                    self.memories,
+                    key=lambda m: self._calculate_priority(m)
+                )
+                excess_count = len(self.memories) - self.max_capacity
+                for memory in sorted_memories[:excess_count]:
+                    to_remove.append(memory.id)
+        
+        # 执行删除
+        for memory_id in to_remove:
+            if self.remove(memory_id):
+                forgotten_count += 1
+        
+        return forgotten_count
+    
+    def _calculate_priority(self, memory: MemoryItem) -> float:
+        """计算记忆优先级"""
+        # 基础优先级 = 重要性
+        priority = memory.importance
+        
+        # 时间衰减
+        time_decay = self._calculate_time_decay(memory.timestamp)
+        priority *= time_decay
+        
+        return priority
+    
+    def _calculate_time_decay(self, timestamp: datetime) -> float:
+        """计算时间衰减因子"""
+        time_diff = datetime.now() - timestamp
+        hours_passed = time_diff.total_seconds() / 3600
+        
+        # 指数衰减(工作记忆衰减更快)
+        decay_factor = self.config.decay_factor ** (hours_passed / 6)  # 每6小时衰减
+        return max(0.1, decay_factor)  # 最小保持10%的权重
+    
+    def _enforce_capacity_limits(self):
+        """强制执行容量限制"""
+        # 检查记忆数量限制
+        while len(self.memories) > self.max_capacity:
+            self._remove_lowest_priority_memory()
+        
+        # 检查token限制
+        while self.current_tokens > self.max_tokens:
+            self._remove_lowest_priority_memory()
+
+    def _expire_old_memories(self):
+        """按TTL清理过期记忆,并同步更新堆与token计数"""
+        if not self.memories:
+            return
+        cutoff_time = datetime.now() - timedelta(minutes=self.max_age_minutes)
+        # 过滤保留的记忆
+        kept: List[MemoryItem] = []
+        removed_token_sum = 0
+        for m in self.memories:
+            if m.timestamp >= cutoff_time:
+                kept.append(m)
+            else:
+                removed_token_sum += len(m.content.split())
+        if len(kept) == len(self.memories):
+            return
+        # 覆盖列表与token
+        self.memories = kept
+        self.current_tokens = max(0, self.current_tokens - removed_token_sum)
+        # 重建堆
+        self.memory_heap = []
+        for mem in self.memories:
+            priority = self._calculate_priority(mem)
+            heapq.heappush(self.memory_heap, (-priority, mem.timestamp, mem))
+    
+    def _remove_lowest_priority_memory(self):
+        """删除优先级最低的记忆"""
+        if not self.memories:
+            return
+        
+        # 找到优先级最低的记忆
+        lowest_priority = float('inf')
+        lowest_memory = None
+        
+        for memory in self.memories:
+            priority = self._calculate_priority(memory)
+            if priority < lowest_priority:
+                lowest_priority = priority
+                lowest_memory = memory
+        
+        if lowest_memory:
+            self.remove(lowest_memory.id)
+    
+    def _update_heap_priority(self, memory: MemoryItem):
+        """更新堆中记忆的优先级"""
+        # 简单实现:重建堆
+        self.memory_heap = []
+        for mem in self.memories:
+            priority = self._calculate_priority(mem)
+            heapq.heappush(self.memory_heap, (-priority, mem.timestamp, mem))
+    
+    def _mark_deleted_in_heap(self, memory_id: str):
+        """在堆中标记删除的记忆"""
+        # 由于heapq不支持直接删除,我们标记为已删除
+        # 在后续操作中会被清理
+        pass

+ 5 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/requirement.txt

@@ -0,0 +1,5 @@
+openai>=1.0.0
+pydantic>=2.0.0
+python-dotenv>=1.0.0
+tiktoken>=0.5.0
+hello-agents[all]=0.2.7

+ 10 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/__init__.py

@@ -0,0 +1,10 @@
+"""Tools package.
+
+Keep this module lightweight to avoid importing optional dependencies at import time.
+Import concrete tools directly, e.g. `from tools.builtin.terminal_tool import TerminalTool`.
+"""
+
+from .base import Tool, ToolParameter
+from .registry import ToolRegistry, global_registry
+
+__all__ = ["Tool", "ToolParameter", "ToolRegistry", "global_registry"]

+ 184 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/async_executor.py

@@ -0,0 +1,184 @@
+"""异步工具执行器 - HelloAgents异步工具执行支持"""
+
+import asyncio
+import concurrent.futures
+from typing import Dict, Any, List
+from .registry import ToolRegistry
+
+
+class AsyncToolExecutor:
+    """异步工具执行器"""
+
+    def __init__(self, registry: ToolRegistry, max_workers: int = 4):
+        self.registry = registry
+        self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=max_workers)
+
+    async def execute_tool_async(self, tool_name: str, input_data: str) -> str:
+        """异步执行单个工具"""
+        loop = asyncio.get_event_loop()
+        
+        def _execute():
+            return self.registry.execute_tool(tool_name, input_data)
+        
+        try:
+            result = await loop.run_in_executor(self.executor, _execute)
+            return result
+        except Exception as e:
+            return f"❌ 工具 '{tool_name}' 异步执行失败: {e}"
+
+    async def execute_tools_parallel(self, tasks: List[Dict[str, str]]) -> List[Dict[str, Any]]:
+        """
+        并行执行多个工具
+        
+        Args:
+            tasks: 任务列表,每个任务包含 tool_name 和 input_data
+            
+        Returns:
+            执行结果列表,包含任务信息和结果
+        """
+        print(f"🚀 开始并行执行 {len(tasks)} 个工具任务")
+        
+        # 创建异步任务
+        async_tasks = []
+        for i, task in enumerate(tasks):
+            tool_name = task.get("tool_name")
+            input_data = task.get("input_data", "")
+            
+            if not tool_name:
+                continue
+                
+            print(f"📝 创建任务 {i+1}: {tool_name}")
+            async_task = self.execute_tool_async(tool_name, input_data)
+            async_tasks.append((i, task, async_task))
+        
+        # 等待所有任务完成
+        results = []
+        for i, task, async_task in async_tasks:
+            try:
+                result = await async_task
+                results.append({
+                    "task_id": i,
+                    "tool_name": task["tool_name"],
+                    "input_data": task["input_data"],
+                    "result": result,
+                    "status": "success"
+                })
+                print(f"✅ 任务 {i+1} 完成: {task['tool_name']}")
+            except Exception as e:
+                results.append({
+                    "task_id": i,
+                    "tool_name": task["tool_name"],
+                    "input_data": task["input_data"],
+                    "result": str(e),
+                    "status": "error"
+                })
+                print(f"❌ 任务 {i+1} 失败: {task['tool_name']} - {e}")
+        
+        print(f"🎉 并行执行完成,成功: {sum(1 for r in results if r['status'] == 'success')}/{len(results)}")
+        return results
+
+    async def execute_tools_batch(self, tool_name: str, input_list: List[str]) -> List[Dict[str, Any]]:
+        """
+        批量执行同一个工具
+        
+        Args:
+            tool_name: 工具名称
+            input_list: 输入数据列表
+            
+        Returns:
+            执行结果列表
+        """
+        tasks = [
+            {"tool_name": tool_name, "input_data": input_data}
+            for input_data in input_list
+        ]
+        return await self.execute_tools_parallel(tasks)
+
+    def close(self):
+        """关闭执行器"""
+        self.executor.shutdown(wait=True)
+        print("🔒 异步工具执行器已关闭")
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        self.close()
+
+
+# 便捷函数
+async def run_parallel_tools(registry: ToolRegistry, tasks: List[Dict[str, str]], max_workers: int = 4) -> List[Dict[str, Any]]:
+    """
+    便捷函数:并行执行多个工具
+    
+    Args:
+        registry: 工具注册表
+        tasks: 任务列表
+        max_workers: 最大工作线程数
+        
+    Returns:
+        执行结果列表
+    """
+    async with AsyncToolExecutor(registry, max_workers) as executor:
+        return await executor.execute_tools_parallel(tasks)
+
+
+async def run_batch_tool(registry: ToolRegistry, tool_name: str, input_list: List[str], max_workers: int = 4) -> List[Dict[str, Any]]:
+    """
+    便捷函数:批量执行同一个工具
+    
+    Args:
+        registry: 工具注册表
+        tool_name: 工具名称
+        input_list: 输入数据列表
+        max_workers: 最大工作线程数
+        
+    Returns:
+        执行结果列表
+    """
+    async with AsyncToolExecutor(registry, max_workers) as executor:
+        return await executor.execute_tools_batch(tool_name, input_list)
+
+
+# 同步包装函数(为了兼容性)
+def run_parallel_tools_sync(registry: ToolRegistry, tasks: List[Dict[str, str]], max_workers: int = 4) -> List[Dict[str, Any]]:
+    """同步版本的并行工具执行"""
+    return asyncio.run(run_parallel_tools(registry, tasks, max_workers))
+
+
+def run_batch_tool_sync(registry: ToolRegistry, tool_name: str, input_list: List[str], max_workers: int = 4) -> List[Dict[str, Any]]:
+    """同步版本的批量工具执行"""
+    return asyncio.run(run_batch_tool(registry, tool_name, input_list, max_workers))
+
+
+# 示例函数
+async def demo_parallel_execution():
+    """演示并行执行的示例"""
+    from .registry import ToolRegistry
+    
+    # 创建注册表(这里假设已经注册了工具)
+    registry = ToolRegistry()
+    
+    # 定义并行任务
+    tasks = [
+        {"tool_name": "my_calculator", "input_data": "2 + 2"},
+        {"tool_name": "my_calculator", "input_data": "3 * 4"},
+        {"tool_name": "my_calculator", "input_data": "sqrt(16)"},
+        {"tool_name": "my_calculator", "input_data": "10 / 2"},
+    ]
+    
+    # 并行执行
+    results = await run_parallel_tools(registry, tasks)
+    
+    # 显示结果
+    print("\n📊 并行执行结果:")
+    for result in results:
+        status_icon = "✅" if result["status"] == "success" else "❌"
+        print(f"{status_icon} {result['tool_name']}({result['input_data']}) = {result['result']}")
+    
+    return results
+
+
+if __name__ == "__main__":
+    # 运行演示
+    asyncio.run(demo_parallel_execution())

+ 49 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/base.py

@@ -0,0 +1,49 @@
+"""工具基类"""
+
+from abc import ABC, abstractmethod
+from typing import Dict, Any, List
+from pydantic import BaseModel
+
+class ToolParameter(BaseModel):
+    """工具参数定义"""
+    name: str
+    type: str
+    description: str
+    required: bool = True
+    default: Any = None
+
+class Tool(ABC):
+    """工具基类"""
+    
+    def __init__(self, name: str, description: str):
+        self.name = name
+        self.description = description
+    
+    @abstractmethod
+    def run(self, parameters: Dict[str, Any]) -> str:
+        """执行工具"""
+        pass
+    
+    @abstractmethod
+    def get_parameters(self) -> List[ToolParameter]:
+        """获取工具参数定义"""
+        pass
+    
+    def validate_parameters(self, parameters: Dict[str, Any]) -> bool:
+        """验证参数"""
+        required_params = [p.name for p in self.get_parameters() if p.required]
+        return all(param in parameters for param in required_params)
+    
+    def to_dict(self) -> Dict[str, Any]:
+        """转换为字典格式"""
+        return {
+            "name": self.name,
+            "description": self.description,
+            "parameters": [param.dict() for param in self.get_parameters()]
+        }
+    
+    def __str__(self) -> str:
+        return f"Tool(name={self.name})"
+    
+    def __repr__(self) -> str:
+        return self.__str__()

+ 22 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/__init__.py

@@ -0,0 +1,22 @@
+"""Built-in tools.
+
+Keep this module lightweight: avoid importing optional dependencies at import time.
+Import concrete tools directly, e.g. `from tools.builtin.note_tool import NoteTool`.
+"""
+
+__all__ = [
+    "SearchTool",
+    "CalculatorTool",
+    "MemoryTool",
+    "RAGTool",
+    "NoteTool",
+    "TerminalTool",
+    "MCPTool",
+    "A2ATool",
+    "ANPTool",
+    "BFCLEvaluationTool",
+    "GAIAEvaluationTool",
+    "LLMJudgeTool",
+    "WinRateTool",
+    "ContextFetchTool",
+]

+ 300 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/context_fetch_tool.py

@@ -0,0 +1,300 @@
+"""上下文获取工具 - 让模型按需获取扩展上下文
+
+设计理念(借鉴 Claude Code):
+- 保底上下文由 ContextBuilder 自动注入(系统提示、对话历史、上次工具摘要)
+- 扩展上下文通过此工具按需获取(notes、memory、files、tests)
+- 模型自行决定何时需要更多证据,避免盲目全局扫描
+"""
+
+from typing import Dict, Any, List, Optional
+import subprocess
+import os
+
+from ..base import Tool, ToolParameter
+
+
+class ContextFetchTool(Tool):
+    """上下文获取工具
+    
+    让模型按需获取扩展上下文,支持多种数据源:
+    - notes: 检索笔记(blocker、insight、decision 等)
+    - memory: 检索情景记忆(之前的对话/经验)
+    - files: 搜索代码文件(rg + 上下文行)
+    - tests: 获取最近测试失败信息
+    
+    使用场景:
+    - 模型发现证据不足时主动调用
+    - 提到类名/函数名/错误栈时获取相关代码
+    - 询问"之前做了什么"时检索记忆
+    """
+    
+    def __init__(
+        self,
+        workspace: str,
+        note_tool: Optional[Any] = None,
+        memory_tool: Optional[Any] = None,
+        max_tokens_per_source: int = 800,
+        context_lines: int = 5,  # 命中行前后各取 k 行
+    ):
+        super().__init__(
+            name="context_fetch",
+            description=(
+                "获取扩展上下文。当保底上下文不足以回答问题时调用。"
+                "可指定数据源:notes(笔记)、memory(记忆)、files(代码文件)、tests(测试结果)。"
+                "返回结构化的证据块。"
+            ),
+        )
+        self.workspace = workspace
+        self.note_tool = note_tool
+        self.memory_tool = memory_tool
+        self.max_tokens_per_source = max_tokens_per_source
+        self.context_lines = context_lines
+        
+        # 缓存最近的查询结果,避免重复查询
+        self._cache: Dict[str, str] = {}
+        self._cache_max_size = 20
+    
+    def get_parameters(self) -> List[ToolParameter]:
+        """遵循基类接口返回参数定义"""
+        return [
+            ToolParameter(
+                name="sources",
+                type="array",
+                description="要查询的数据源列表,可选: notes, memory, files, tests",
+                required=True,
+            ),
+            ToolParameter(
+                name="query",
+                type="string",
+                description="搜索关键词/符号名/错误栈片段",
+                required=True,
+            ),
+            ToolParameter(
+                name="paths",
+                type="string",
+                description="限定文件搜索范围的 glob 模式,如 'src/**/*.py'",
+                required=False,
+            ),
+            ToolParameter(
+                name="budget_tokens",
+                type="integer",
+                description="单个数据源的 token 上限,默认 800",
+                required=False,
+            ),
+        ]
+    
+    def run(self, parameters: Dict[str, Any]) -> str:
+        """执行上下文获取"""
+        sources = parameters.get("sources", [])
+        query = parameters.get("query", "")
+        paths = parameters.get("paths", "")
+        budget = parameters.get("budget_tokens", self.max_tokens_per_source)
+        
+        if not sources or not query:
+            return "错误:必须指定 sources 和 query 参数"
+        
+        # 检查缓存
+        cache_key = f"{','.join(sorted(sources))}|{query}|{paths}"
+        if cache_key in self._cache:
+            return f"[缓存命中]\n{self._cache[cache_key]}"
+        
+        results: List[str] = []
+        
+        for source in sources:
+            if source == "notes":
+                result = self._fetch_notes(query, budget)
+            elif source == "memory":
+                result = self._fetch_memory(query, budget)
+            elif source == "files":
+                result = self._fetch_files(query, paths, budget)
+            elif source == "tests":
+                result = self._fetch_tests(query, budget)
+            else:
+                result = f"[{source}] 未知数据源"
+            
+            if result:
+                results.append(result)
+        
+        output = "\n\n".join(results) if results else "未找到相关上下文"
+        
+        # 更新缓存
+        if len(self._cache) >= self._cache_max_size:
+            # 简单 LRU:删除最早的
+            oldest_key = next(iter(self._cache))
+            del self._cache[oldest_key]
+        self._cache[cache_key] = output
+        
+        return output
+    
+    def _fetch_notes(self, query: str, budget: int) -> str:
+        """从笔记中检索"""
+        if not self.note_tool:
+            return "[notes] 笔记工具未配置"
+        
+        try:
+            # 搜索相关笔记
+            result = self.note_tool.run({
+                "action": "search",
+                "query": query,
+                "limit": 5,
+            })
+            if result and "未找到" not in result:
+                return f"[notes] 相关笔记:\n{self._truncate(result, budget)}"
+            return "[notes] 未找到相关笔记"
+        except Exception as e:
+            return f"[notes] 检索失败: {e}"
+    
+    def _fetch_memory(self, query: str, budget: int) -> str:
+        """从记忆中检索"""
+        if not self.memory_tool:
+            return "[memory] 记忆工具未配置"
+        
+        try:
+            result = self.memory_tool.run({
+                "action": "search",
+                "query": query,
+                "memory_types": getattr(self.memory_tool, "memory_types", ["episodic"]),
+                "limit": 5,
+                "min_importance": 0.0,
+            })
+            if result and "未找到" not in result:
+                return f"[memory] 相关记忆:\n{self._truncate(result, budget)}"
+            return "[memory] 未找到相关记忆"
+        except Exception as e:
+            return f"[memory] 检索失败: {e}"
+    
+    def _fetch_files(self, query: str, paths: str, budget: int) -> str:
+        """从代码文件中检索"""
+        try:
+            # 使用 ripgrep 搜索
+            cmd = ["rg", "--color=never", "-n", "-C", str(self.context_lines)]
+            
+            if paths:
+                cmd.extend(["-g", paths])
+            
+            cmd.append(query)
+            cmd.append(self.workspace)
+            
+            result = subprocess.run(
+                cmd,
+                capture_output=True,
+                text=True,
+                timeout=10,
+                cwd=self.workspace,
+            )
+            
+            output = result.stdout.strip()
+            if output:
+                # 结构化输出
+                lines = output.split("\n")
+                # 按文件分组
+                grouped = self._group_by_file(lines)
+                formatted = self._format_file_results(grouped, budget)
+                return f"[files] 代码搜索结果:\n{formatted}"
+            return f"[files] 未找到匹配 '{query}' 的内容"
+        except subprocess.TimeoutExpired:
+            return "[files] 搜索超时"
+        except FileNotFoundError:
+            # ripgrep 未安装,降级到 grep
+            return self._fetch_files_fallback(query, paths, budget)
+        except Exception as e:
+            return f"[files] 搜索失败: {e}"
+    
+    def _fetch_files_fallback(self, query: str, paths: str, budget: int) -> str:
+        """ripgrep 不可用时的降级方案"""
+        try:
+            cmd = f"grep -rn '{query}' {self.workspace}"
+            if paths:
+                cmd = f"find {self.workspace} -path '{paths}' -type f | xargs grep -n '{query}'"
+            
+            result = subprocess.run(
+                cmd,
+                shell=True,
+                capture_output=True,
+                text=True,
+                timeout=10,
+            )
+            output = result.stdout.strip()
+            if output:
+                return f"[files] grep 结果:\n{self._truncate(output, budget)}"
+            return f"[files] 未找到匹配 '{query}' 的内容"
+        except Exception as e:
+            return f"[files] grep 搜索失败: {e}"
+    
+    def _fetch_tests(self, query: str, budget: int) -> str:
+        """获取测试相关信息"""
+        # 查找最近的测试输出/日志
+        test_patterns = [
+            ".pytest_cache/v/cache/lastfailed",
+            "test-results.xml",
+            ".coverage",
+        ]
+        
+        results = []
+        for pattern in test_patterns:
+            path = os.path.join(self.workspace, pattern)
+            if os.path.exists(path):
+                try:
+                    with open(path, "r", encoding="utf-8", errors="ignore") as f:
+                        content = f.read()
+                    if query.lower() in content.lower():
+                        results.append(f"[tests] {pattern}:\n{self._truncate(content, budget // 2)}")
+                except Exception:
+                    pass
+        
+        if results:
+            return "\n".join(results)
+        return "[tests] 未找到相关测试信息"
+    
+    def _group_by_file(self, lines: List[str]) -> Dict[str, List[str]]:
+        """按文件分组 ripgrep 输出"""
+        grouped: Dict[str, List[str]] = {}
+        current_file = None
+        
+        for line in lines:
+            if ":" in line:
+                # 格式: file:line:content 或 file-line-content
+                parts = line.split(":", 2) if ":" in line else line.split("-", 2)
+                if len(parts) >= 2:
+                    file_path = parts[0]
+                    if file_path != current_file:
+                        current_file = file_path
+                        grouped[current_file] = []
+                    grouped[current_file].append(line)
+            elif current_file:
+                grouped[current_file].append(line)
+        
+        return grouped
+    
+    def _format_file_results(self, grouped: Dict[str, List[str]], budget: int) -> str:
+        """格式化文件搜索结果"""
+        output_parts = []
+        tokens_used = 0
+        tokens_per_file = budget // max(len(grouped), 1)
+        
+        for file_path, lines in grouped.items():
+            content = "\n".join(lines)
+            truncated = self._truncate(content, tokens_per_file)
+            
+            # 相对路径
+            rel_path = file_path.replace(self.workspace, "").lstrip("/")
+            output_parts.append(f"--- {rel_path} ---\n{truncated}")
+            
+            tokens_used += len(truncated) // 4  # 粗略估算
+            if tokens_used >= budget:
+                output_parts.append("...(更多结果已截断)...")
+                break
+        
+        return "\n\n".join(output_parts)
+    
+    def _truncate(self, text: str, max_tokens: int) -> str:
+        """截断文本到指定 token 上限"""
+        # 粗略估算:1 token ≈ 4 字符(英文),2 字符(中文)
+        max_chars = max_tokens * 3
+        if len(text) <= max_chars:
+            return text
+        return text[:max_chars] + "\n...(已截断)..."
+    
+    def clear_cache(self):
+        """清空缓存"""
+        self._cache.clear()

+ 119 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/mcp_wrapper_tool.py

@@ -0,0 +1,119 @@
+"""
+MCP工具包装器 - 将单个MCP工具包装成HelloAgents Tool
+
+这个模块将MCP服务器的每个工具展开为独立的HelloAgents Tool对象,
+使得Agent可以像调用普通工具一样调用MCP工具。
+"""
+
+from typing import Dict, Any, Optional, List
+from ..base import Tool, ToolParameter
+
+
+class MCPWrappedTool(Tool):
+    """
+    MCP工具包装器 - 将单个MCP工具包装成HelloAgents Tool
+    
+    这个类将MCP服务器的一个工具(如 read_file)包装成一个独立的Tool对象。
+    Agent调用时只需提供参数,无需了解MCP的内部结构。
+    
+    示例:
+        >>> # 内部使用,由MCPTool自动创建
+        >>> wrapped_tool = MCPWrappedTool(
+        ...     mcp_tool=mcp_tool_instance,
+        ...     tool_info={
+        ...         "name": "read_file",
+        ...         "description": "Read a file...",
+        ...         "input_schema": {...}
+        ...     }
+        ... )
+    """
+    
+    def __init__(self,
+                 mcp_tool: 'MCPTool',  # type: ignore
+                 tool_info: Dict[str, Any],
+                 prefix: str = ""):
+        """
+        初始化MCP包装工具
+
+        Args:
+            mcp_tool: 父MCP工具实例
+            tool_info: MCP工具信息(包含name, description, input_schema)
+            prefix: 工具名前缀(如 "filesystem_")
+        """
+        self.mcp_tool = mcp_tool
+        self.tool_info = tool_info
+        self.mcp_tool_name = tool_info.get('name', 'unknown')
+
+        # 构建工具名:prefix + mcp_tool_name
+        tool_name = f"{prefix}{self.mcp_tool_name}" if prefix else self.mcp_tool_name
+
+        # 获取描述
+        description = tool_info.get('description', f'MCP工具: {self.mcp_tool_name}')
+
+        # 解析参数schema
+        self._parameters = self._parse_input_schema(tool_info.get('input_schema', {}))
+
+        # 初始化父类
+        super().__init__(
+            name=tool_name,
+            description=description
+        )
+    
+    def _parse_input_schema(self, input_schema: Dict[str, Any]) -> List[ToolParameter]:
+        """
+        将MCP的input_schema转换为HelloAgents的ToolParameter列表
+
+        Args:
+            input_schema: MCP工具的input_schema(JSON Schema格式)
+
+        Returns:
+            ToolParameter列表
+        """
+        parameters = []
+
+        properties = input_schema.get('properties', {})
+        required_fields = input_schema.get('required', [])
+
+        for param_name, param_info in properties.items():
+            param_type = param_info.get('type', 'string')
+            param_desc = param_info.get('description', '')
+            is_required = param_name in required_fields
+
+            parameters.append(ToolParameter(
+                name=param_name,
+                type=param_type,  # 直接使用JSON Schema的类型字符串
+                description=param_desc,
+                required=is_required
+            ))
+
+        return parameters
+    
+    def get_parameters(self) -> List[ToolParameter]:
+        """
+        获取工具参数定义
+
+        Returns:
+            ToolParameter列表
+        """
+        return self._parameters
+
+    def run(self, params: Dict[str, Any]) -> str:
+        """
+        执行MCP工具
+
+        Args:
+            params: 工具参数(直接传递给MCP工具)
+
+        Returns:
+            执行结果
+        """
+        # 构建MCP调用参数
+        mcp_params = {
+            "action": "call_tool",
+            "tool_name": self.mcp_tool_name,
+            "arguments": params
+        }
+
+        # 调用父MCP工具
+        return self.mcp_tool.run(mcp_params)
+

+ 453 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/memory_tool.py

@@ -0,0 +1,453 @@
+"""记忆工具
+
+为HelloAgents框架提供记忆能力的工具实现。
+可以作为工具添加到任何Agent中,让Agent具备记忆功能。
+"""
+
+from typing import Dict, Any, List
+from datetime import datetime
+
+from ..base import Tool, ToolParameter
+from memory import MemoryManager, MemoryConfig
+
+class MemoryTool(Tool):
+    """记忆工具
+
+    为Agent提供记忆功能:
+    - 添加记忆
+    - 检索相关记忆
+    - 获取记忆摘要
+    - 管理记忆生命周期
+    """
+
+    def __init__(
+        self,
+        user_id: str = "default_user",
+        memory_config: MemoryConfig = None,
+        memory_types: List[str] = None
+    ):
+        super().__init__(
+            name="memory",
+            description="记忆工具 - 可以存储和检索对话历史、知识和经验"
+        )
+
+        # 初始化记忆管理器
+        self.memory_config = memory_config or MemoryConfig()
+        self.memory_types = memory_types or ["working", "episodic", "semantic"]
+
+        self.memory_manager = MemoryManager(
+            config=self.memory_config,
+            user_id=user_id,
+            enable_working="working" in self.memory_types,
+            enable_episodic="episodic" in self.memory_types,
+            enable_semantic="semantic" in self.memory_types,
+            enable_perceptual="perceptual" in self.memory_types
+        )
+
+        # 会话状态
+        self.current_session_id = None
+        self.conversation_count = 0
+
+    def run(self, parameters: Dict[str, Any]) -> str:
+        """执行工具 - Tool基类要求的接口
+
+        Args:
+            parameters: 工具参数字典,必须包含action参数
+
+        Returns:
+            执行结果字符串
+        """
+        if not self.validate_parameters(parameters):
+            return "❌ 参数验证失败:缺少必需的参数"
+
+        action = parameters.get("action")
+        # 移除action参数,传递其余参数给execute方法
+        kwargs = {k: v for k, v in parameters.items() if k != "action"}
+
+        return self.execute(action, **kwargs)
+
+    def get_parameters(self) -> List[ToolParameter]:
+        """获取工具参数定义 - Tool基类要求的接口"""
+        return [
+            ToolParameter(
+                name="action",
+                type="string",
+                description=(
+                    "要执行的操作:"
+                    "add(添加记忆), search(搜索记忆), summary(获取摘要), stats(获取统计), "
+                    "update(更新记忆), remove(删除记忆), forget(遗忘记忆), consolidate(整合记忆), clear_all(清空所有记忆)"
+                ),
+                required=True
+            ),
+            ToolParameter(name="content", type="string", description="记忆内容(add/update时可用;感知记忆可作描述)", required=False),
+            ToolParameter(name="query", type="string", description="搜索查询(search时可用)", required=False),
+            ToolParameter(name="memory_type", type="string", description="记忆类型:working, episodic, semantic, perceptual(默认:working)", required=False, default="working"),
+            ToolParameter(name="importance", type="number", description="重要性分数,0.0-1.0(add/update时可用)", required=False),
+            ToolParameter(name="limit", type="integer", description="搜索结果数量限制(默认:5)", required=False, default=5),
+            ToolParameter(name="memory_id", type="string", description="目标记忆ID(update/remove时必需)", required=False),
+            ToolParameter(name="file_path", type="string", description="感知记忆:本地文件路径(image/audio)", required=False),
+            ToolParameter(name="modality", type="string", description="感知记忆模态:text/image/audio(不传则按扩展名推断)", required=False),
+            ToolParameter(name="strategy", type="string", description="遗忘策略:importance_based/time_based/capacity_based(forget时可用)", required=False, default="importance_based"),
+            ToolParameter(name="threshold", type="number", description="遗忘阈值(forget时可用,默认0.1)", required=False, default=0.1),
+            ToolParameter(name="max_age_days", type="integer", description="最大保留天数(forget策略为time_based时可用)", required=False, default=30),
+            ToolParameter(name="from_type", type="string", description="整合来源类型(consolidate时可用,默认working)", required=False, default="working"),
+            ToolParameter(name="to_type", type="string", description="整合目标类型(consolidate时可用,默认episodic)", required=False, default="episodic"),
+            ToolParameter(name="importance_threshold", type="number", description="整合重要性阈值(默认0.7)", required=False, default=0.7),
+        ]
+
+    def execute(self, action: str, **kwargs) -> str:
+        """执行记忆操作
+
+        支持的操作:
+        - add: 添加记忆
+        - search: 搜索记忆
+        - summary: 获取记忆摘要
+        - stats: 获取统计信息
+        """
+
+        if action == "add":
+            return self._add_memory(**kwargs)
+        elif action == "search":
+            return self._search_memory(**kwargs)
+        elif action == "summary":
+            return self._get_summary(**kwargs)
+        elif action == "stats":
+            return self._get_stats()
+        elif action == "update":
+            return self._update_memory(**kwargs)
+        elif action == "remove":
+            return self._remove_memory(**kwargs)
+        elif action == "forget":
+            return self._forget(**kwargs)
+        elif action == "consolidate":
+            return self._consolidate(**kwargs)
+        elif action == "clear_all":
+            return self._clear_all()
+        else:
+            return f"不支持的操作: {action}。支持的操作: add, search, summary, stats, update, remove, forget, consolidate, clear_all"
+
+    def _add_memory(
+        self,
+        content: str = "",
+        memory_type: str = "working",
+        importance: float = 0.5,
+        file_path: str = None,
+        modality: str = None,
+        **metadata
+    ) -> str:
+        """添加记忆"""
+        try:
+            # 确保会话ID存在
+            if self.current_session_id is None:
+                self.current_session_id = f"session_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
+
+            # 感知记忆文件支持:注入 raw_data 与模态
+            if memory_type == "perceptual" and file_path:
+                inferred = modality or self._infer_modality(file_path)
+                metadata.setdefault("modality", inferred)
+                metadata.setdefault("raw_data", file_path)
+
+            # 添加会话信息到元数据
+            metadata.update({
+                "session_id": self.current_session_id,
+                "timestamp": datetime.now().isoformat()
+            })
+
+            memory_id = self.memory_manager.add_memory(
+                content=content,
+                memory_type=memory_type,
+                importance=importance,
+                metadata=metadata,
+                auto_classify=False  # 禁用自动分类,使用明确指定的类型
+            )
+
+            return f"✅ 记忆已添加 (ID: {memory_id[:8]}...)"
+
+        except Exception as e:
+            return f"❌ 添加记忆失败: {str(e)}"
+
+    def _infer_modality(self, path: str) -> str:
+        """根据扩展名推断模态(默认image/audio/text)"""
+        try:
+            ext = (path.rsplit('.', 1)[-1] or '').lower()
+            if ext in {"png", "jpg", "jpeg", "bmp", "gif", "webp"}:
+                return "image"
+            if ext in {"mp3", "wav", "flac", "m4a", "ogg"}:
+                return "audio"
+            return "text"
+        except Exception:
+            return "text"
+
+    def _search_memory(
+        self,
+        query: str,
+        limit: int = 5,
+        memory_types: List[str] = None,
+        memory_type: str = None,  # 添加单数形式的参数支持
+        min_importance: float = 0.1
+    ) -> str:
+        """搜索记忆"""
+        try:
+            # 处理单数形式的memory_type参数
+            if memory_type and not memory_types:
+                memory_types = [memory_type]
+
+            results = self.memory_manager.retrieve_memories(
+                query=query,
+                limit=limit,
+                memory_types=memory_types,
+                min_importance=min_importance
+            )
+
+            if not results:
+                return f"🔍 未找到与 '{query}' 相关的记忆"
+
+            # 格式化结果
+            formatted_results = []
+            formatted_results.append(f"🔍 找到 {len(results)} 条相关记忆:")
+
+            for i, memory in enumerate(results, 1):
+                memory_type_label = {
+                    "working": "工作记忆",
+                    "episodic": "情景记忆",
+                    "semantic": "语义记忆",
+                    "perceptual": "感知记忆"
+                }.get(memory.memory_type, memory.memory_type)
+
+                content_preview = memory.content[:80] + "..." if len(memory.content) > 80 else memory.content
+                formatted_results.append(
+                    f"{i}. [{memory_type_label}] {content_preview} (重要性: {memory.importance:.2f})"
+                )
+
+            return "\n".join(formatted_results)
+
+        except Exception as e:
+            return f"❌ 搜索记忆失败: {str(e)}"
+
+    def _get_summary(self, limit: int = 10) -> str:
+        """获取记忆摘要"""
+        try:
+            stats = self.memory_manager.get_memory_stats()
+
+            summary_parts = [
+                f"📊 记忆系统摘要",
+                f"总记忆数: {stats['total_memories']}",
+                f"当前会话: {self.current_session_id or '未开始'}",
+                f"对话轮次: {self.conversation_count}"
+            ]
+
+            # 各类型记忆统计
+            if stats['memories_by_type']:
+                summary_parts.append("\n📋 记忆类型分布:")
+                for memory_type, type_stats in stats['memories_by_type'].items():
+                    count = type_stats.get('count', 0)
+                    avg_importance = type_stats.get('avg_importance', 0)
+                    type_label = {
+                        "working": "工作记忆",
+                        "episodic": "情景记忆",
+                        "semantic": "语义记忆",
+                        "perceptual": "感知记忆"
+                    }.get(memory_type, memory_type)
+
+                    summary_parts.append(f"  • {type_label}: {count} 条 (平均重要性: {avg_importance:.2f})")
+
+            # 获取重要记忆 - 修复重复问题
+            important_memories = self.memory_manager.retrieve_memories(
+                query="",
+                memory_types=None,  # 从所有类型中检索
+                limit=limit * 3,  # 获取更多候选,然后去重
+                min_importance=0.5  # 降低阈值以获取更多记忆
+            )
+
+            if important_memories:
+                # 去重:使用记忆ID和内容双重去重
+                seen_ids = set()
+                seen_contents = set()
+                unique_memories = []
+                
+                for memory in important_memories:
+                    # 使用ID去重
+                    if memory.id in seen_ids:
+                        continue
+                    
+                    # 使用内容去重(防止相同内容的不同记忆)
+                    content_key = memory.content.strip().lower()
+                    if content_key in seen_contents:
+                        continue
+                    
+                    seen_ids.add(memory.id)
+                    seen_contents.add(content_key)
+                    unique_memories.append(memory)
+                
+                # 按重要性排序
+                unique_memories.sort(key=lambda x: x.importance, reverse=True)
+                summary_parts.append(f"\n⭐ 重要记忆 (前{min(limit, len(unique_memories))}条):")
+
+                for i, memory in enumerate(unique_memories[:limit], 1):
+                    content_preview = memory.content[:60] + "..." if len(memory.content) > 60 else memory.content
+                    summary_parts.append(f"  {i}. {content_preview} (重要性: {memory.importance:.2f})")
+
+            return "\n".join(summary_parts)
+
+        except Exception as e:
+            return f"❌ 获取摘要失败: {str(e)}"
+
+    def _get_stats(self) -> str:
+        """获取统计信息"""
+        try:
+            stats = self.memory_manager.get_memory_stats()
+
+            stats_info = [
+                f"📈 记忆系统统计",
+                f"总记忆数: {stats['total_memories']}",
+                f"启用的记忆类型: {', '.join(stats['enabled_types'])}",
+                f"会话ID: {self.current_session_id or '未开始'}",
+                f"对话轮次: {self.conversation_count}"
+            ]
+
+            return "\n".join(stats_info)
+
+        except Exception as e:
+            return f"❌ 获取统计信息失败: {str(e)}"
+
+    def auto_record_conversation(self, user_input: str, agent_response: str):
+        """自动记录对话
+
+        这个方法可以被Agent调用来自动记录对话历史
+        """
+        self.conversation_count += 1
+        # 记录用户输入
+        self._add_memory(
+            content=f"用户: {user_input}",
+            memory_type="working",
+            importance=0.6,
+            type="user_input",
+            conversation_id=self.conversation_count
+        )
+
+        # 记录Agent响应
+        self._add_memory(
+            content=f"助手: {agent_response}",
+            memory_type="working",
+            importance=0.7,
+            type="agent_response",
+            conversation_id=self.conversation_count
+        )
+
+        # 如果是重要对话,记录为情景记忆
+        if len(agent_response) > 100 or "重要" in user_input or "记住" in user_input:
+            interaction_content = f"对话 - 用户: {user_input}\n助手: {agent_response}"
+            self._add_memory(
+                content=interaction_content,
+                memory_type="episodic",
+                importance=0.8,
+                type="interaction",
+                conversation_id=self.conversation_count
+            )
+
+    def _update_memory(self, memory_id: str, content: str = None, importance: float = None, **metadata) -> str:
+        """更新记忆"""
+        try:
+            success = self.memory_manager.update_memory(
+                memory_id=memory_id,
+                content=content,
+                importance=importance,
+                metadata=metadata or None
+            )
+            return "✅ 记忆已更新" if success else "⚠️ 未找到要更新的记忆"
+        except Exception as e:
+            return f"❌ 更新记忆失败: {str(e)}"
+
+    def _remove_memory(self, memory_id: str) -> str:
+        """删除记忆"""
+        try:
+            success = self.memory_manager.remove_memory(memory_id)
+            return "✅ 记忆已删除" if success else "⚠️ 未找到要删除的记忆"
+        except Exception as e:
+            return f"❌ 删除记忆失败: {str(e)}"
+
+    def _forget(self, strategy: str = "importance_based", threshold: float = 0.1, max_age_days: int = 30) -> str:
+        """遗忘记忆(支持多种策略)"""
+        try:
+            count = self.memory_manager.forget_memories(
+                strategy=strategy,
+                threshold=threshold,
+                max_age_days=max_age_days
+            )
+            return f"🧹 已遗忘 {count} 条记忆(策略: {strategy})"
+        except Exception as e:
+            return f"❌ 遗忘记忆失败: {str(e)}"
+
+    def _consolidate(self, from_type: str = "working", to_type: str = "episodic", importance_threshold: float = 0.7) -> str:
+        """整合记忆(将重要的短期记忆提升为长期记忆)"""
+        try:
+            count = self.memory_manager.consolidate_memories(
+                from_type=from_type,
+                to_type=to_type,
+                importance_threshold=importance_threshold,
+            )
+            return f"🔄 已整合 {count} 条记忆为长期记忆({from_type} → {to_type},阈值={importance_threshold})"
+        except Exception as e:
+            return f"❌ 整合记忆失败: {str(e)}"
+
+    def _clear_all(self) -> str:
+        """清空所有记忆"""
+        try:
+            self.memory_manager.clear_all_memories()
+            return "🧽 已清空所有记忆"
+        except Exception as e:
+            return f"❌ 清空记忆失败: {str(e)}"
+
+    def add_knowledge(self, content: str, importance: float = 0.9):
+        """添加知识到语义记忆
+
+        便捷方法,用于添加重要知识
+        """
+        return self._add_memory(
+            content=content,
+            memory_type="semantic",
+            importance=importance,
+            knowledge_type="factual",
+            source="manual"
+        )
+
+    def get_context_for_query(self, query: str, limit: int = 3) -> str:
+        """为查询获取相关上下文
+
+        这个方法可以被Agent调用来获取相关的记忆上下文
+        """
+        results = self.memory_manager.retrieve_memories(
+            query=query,
+            limit=limit,
+            min_importance=0.3
+        )
+
+        if not results:
+            return ""
+
+        context_parts = ["相关记忆:"]
+        for memory in results:
+            context_parts.append(f"- {memory.content}")
+
+        return "\n".join(context_parts)
+
+    def clear_session(self):
+        """清除当前会话"""
+        self.current_session_id = None
+        self.conversation_count = 0
+
+        # 清理工作记忆
+        wm = self.memory_manager.memory_types.get('working') if hasattr(self.memory_manager, 'memory_types') else None
+        if wm:
+            wm.clear()
+
+    def consolidate_memories(self):
+        """整合记忆"""
+        return self.memory_manager.consolidate_memories()
+
+    def forget_old_memories(self, max_age_days: int = 30):
+        """遗忘旧记忆"""
+        return self.memory_manager.forget_memories(
+            strategy="time_based",
+            max_age_days=max_age_days
+        )

+ 518 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/note_tool.py

@@ -0,0 +1,518 @@
+"""NoteTool - 结构化笔记工具
+
+为Agent提供结构化笔记能力,支持:
+- 创建/读取/更新/删除笔记
+- 按类型组织(任务状态、结论、阻塞项、行动计划等)
+- 持久化存储(Markdown格式,带YAML前置元数据)
+- 搜索与过滤
+- 与MemoryTool集成(可选)
+
+使用场景:
+- 长时程任务的状态跟踪
+- 关键结论与依赖记录
+- 待办事项与行动计划
+- 项目知识沉淀
+
+笔记格式示例:
+```markdown
+---
+id: note_20250118_120000_0
+title: 项目进展
+type: task_state
+tags: [milestone, phase1]
+created_at: 2025-01-18T12:00:00
+updated_at: 2025-01-18T12:00:00
+---
+
+# 项目进展
+
+已完成需求分析,下一步:设计方案
+
+## 关键里程碑
+- [x] 需求收集
+- [ ] 方案设计
+```
+"""
+
+from typing import Dict, Any, List, Optional
+from datetime import datetime
+from pathlib import Path
+import json
+import os
+import re
+
+from ..base import Tool, ToolParameter
+
+
+class NoteTool(Tool):
+    """笔记工具
+    
+    为Agent提供结构化笔记管理能力,支持多种笔记类型:
+    - task_state: 任务状态
+    - conclusion: 关键结论
+    - blocker: 阻塞项
+    - action: 行动计划
+    - reference: 参考资料
+    - general: 通用笔记
+    
+    用法示例:
+    ```python
+    note_tool = NoteTool(workspace="./project_notes")
+    
+    # 创建笔记
+    note_tool.run({
+        "action": "create",
+        "title": "项目进展",
+        "content": "已完成需求分析,下一步:设计方案",
+        "note_type": "task_state",
+        "tags": ["milestone", "phase1"]
+    })
+    
+    # 读取笔记
+    notes = note_tool.run({"action": "list", "note_type": "task_state"})
+    ```
+    """
+    
+    def __init__(
+        self,
+        workspace: str = "./notes",
+        auto_backup: bool = True,
+        max_notes: int = 1000
+    ):
+        super().__init__(
+            name="note",
+            description="笔记工具 - 创建、读取、更新、删除结构化笔记,支持任务状态、结论、阻塞项等类型"
+        )
+        
+        self.workspace = Path(workspace)
+        self.auto_backup = auto_backup
+        self.max_notes = max_notes
+        
+        # 确保工作目录存在
+        self.workspace.mkdir(parents=True, exist_ok=True)
+        
+        # 笔记索引文件
+        self.index_file = self.workspace / "notes_index.json"
+        self._load_index()
+    
+    def _load_index(self):
+        """加载笔记索引"""
+        if self.index_file.exists():
+            with open(self.index_file, 'r', encoding='utf-8') as f:
+                self.notes_index = json.load(f)
+        else:
+            self.notes_index = {
+                "notes": [],
+                "metadata": {
+                    "created_at": datetime.now().isoformat(),
+                    "total_notes": 0
+                }
+            }
+            self._save_index()
+    
+    def _save_index(self):
+        """保存笔记索引"""
+        with open(self.index_file, 'w', encoding='utf-8') as f:
+            json.dump(self.notes_index, f, ensure_ascii=False, indent=2)
+    
+    def _generate_note_id(self) -> str:
+        """生成笔记ID"""
+        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
+        count = len(self.notes_index["notes"])
+        return f"note_{timestamp}_{count}"
+    
+    def _get_note_path(self, note_id: str) -> Path:
+        """获取笔记文件路径"""
+        return self.workspace / f"{note_id}.md"
+    
+    def _note_to_markdown(self, note: Dict[str, Any]) -> str:
+        """将笔记对象转换为Markdown格式"""
+        # YAML前置元数据
+        frontmatter = "---\n"
+        frontmatter += f"id: {note['id']}\n"
+        frontmatter += f"title: {note['title']}\n"
+        frontmatter += f"type: {note['type']}\n"
+        if note.get('tags'):
+            tags_str = json.dumps(note['tags'])
+            frontmatter += f"tags: {tags_str}\n"
+        frontmatter += f"created_at: {note['created_at']}\n"
+        frontmatter += f"updated_at: {note['updated_at']}\n"
+        frontmatter += "---\n\n"
+        
+        # Markdown内容
+        content = f"# {note['title']}\n\n"
+        content += note['content']
+        
+        return frontmatter + content
+    
+    def _markdown_to_note(self, markdown_text: str) -> Dict[str, Any]:
+        """将Markdown文本解析为笔记对象"""
+        # 提取YAML前置元数据
+        frontmatter_match = re.match(r'^---\s*\n(.*?)\n---\s*\n', markdown_text, re.DOTALL)
+        
+        if not frontmatter_match:
+            raise ValueError("无效的笔记格式:缺少YAML前置元数据")
+        
+        frontmatter_text = frontmatter_match.group(1)
+        content_start = frontmatter_match.end()
+        
+        # 解析YAML(简化版)
+        note = {}
+        for line in frontmatter_text.split('\n'):
+            if ':' in line:
+                key, value = line.split(':', 1)
+                key = key.strip()
+                value = value.strip()
+                
+                # 处理特殊字段
+                if key == 'tags':
+                    try:
+                        note[key] = json.loads(value)
+                    except:
+                        note[key] = []
+                else:
+                    note[key] = value
+        
+        # 提取内容(去掉标题行)
+        markdown_content = markdown_text[content_start:].strip()
+        # 移除第一行的 # 标题
+        lines = markdown_content.split('\n')
+        if lines and lines[0].startswith('# '):
+            markdown_content = '\n'.join(lines[1:]).strip()
+        
+        note['content'] = markdown_content
+        
+        # 添加元数据
+        note['metadata'] = {
+            'word_count': len(markdown_content),
+            'status': 'active'
+        }
+        
+        return note
+    
+    def run(self, parameters: Dict[str, Any]) -> str:
+        """执行工具"""
+        if not self.validate_parameters(parameters):
+            return "❌ 参数验证失败"
+        
+        action = parameters.get("action")
+        
+        if action == "create":
+            return self._create_note(parameters)
+        elif action == "read":
+            return self._read_note(parameters)
+        elif action == "update":
+            return self._update_note(parameters)
+        elif action == "delete":
+            return self._delete_note(parameters)
+        elif action == "list":
+            return self._list_notes(parameters)
+        elif action == "search":
+            return self._search_notes(parameters)
+        elif action == "summary":
+            return self._get_summary()
+        else:
+            return f"❌ 不支持的操作: {action}"
+    
+    def get_parameters(self) -> List[ToolParameter]:
+        """获取工具参数定义"""
+        return [
+            ToolParameter(
+                name="action",
+                type="string",
+                description=(
+                    "操作类型: create(创建), read(读取), update(更新), "
+                    "delete(删除), list(列表), search(搜索), summary(摘要)"
+                ),
+                required=True
+            ),
+            ToolParameter(
+                name="title",
+                type="string",
+                description="笔记标题(create/update时必需)",
+                required=False
+            ),
+            ToolParameter(
+                name="content",
+                type="string",
+                description="笔记内容(create/update时必需)",
+                required=False
+            ),
+            ToolParameter(
+                name="note_type",
+                type="string",
+                description=(
+                    "笔记类型: task_state(任务状态), conclusion(结论), "
+                    "blocker(阻塞项), action(行动计划), reference(参考), general(通用)"
+                ),
+                required=False,
+                default="general"
+            ),
+            ToolParameter(
+                name="tags",
+                type="array",
+                description="标签列表(可选)",
+                required=False
+            ),
+            ToolParameter(
+                name="note_id",
+                type="string",
+                description="笔记ID(read/update/delete时必需)",
+                required=False
+            ),
+            ToolParameter(
+                name="query",
+                type="string",
+                description="搜索关键词(search时必需)",
+                required=False
+            ),
+            ToolParameter(
+                name="limit",
+                type="integer",
+                description="返回结果数量限制(默认10)",
+                required=False,
+                default=10
+            ),
+        ]
+    
+    def _create_note(self, params: Dict[str, Any]) -> str:
+        """创建笔记"""
+        title = params.get("title")
+        content = params.get("content")
+        note_type = params.get("note_type", "general")
+        tags = params.get("tags", [])
+        
+        if not title or not content:
+            return "❌ 创建笔记需要提供 title 和 content"
+        
+        # 检查笔记数量限制
+        if len(self.notes_index["notes"]) >= self.max_notes:
+            return f"❌ 笔记数量已达上限 ({self.max_notes})"
+        
+        # 生成笔记ID
+        note_id = self._generate_note_id()
+        
+        # 创建笔记对象
+        note = {
+            "id": note_id,
+            "title": title,
+            "content": content,
+            "type": note_type,
+            "tags": tags if isinstance(tags, list) else [],
+            "created_at": datetime.now().isoformat(),
+            "updated_at": datetime.now().isoformat(),
+            "metadata": {
+                "word_count": len(content),
+                "status": "active"
+            }
+        }
+        
+        # 保存笔记文件(Markdown格式)
+        note_path = self._get_note_path(note_id)
+        markdown_content = self._note_to_markdown(note)
+        with open(note_path, 'w', encoding='utf-8') as f:
+            f.write(markdown_content)
+        
+        # 更新索引
+        self.notes_index["notes"].append({
+            "id": note_id,
+            "title": title,
+            "type": note_type,
+            "tags": tags if isinstance(tags, list) else [],
+            "created_at": note["created_at"]
+        })
+        self.notes_index["metadata"]["total_notes"] = len(self.notes_index["notes"])
+        self._save_index()
+        
+        return f"✅ 笔记创建成功\nID: {note_id}\n标题: {title}\n类型: {note_type}"
+    
+    def _read_note(self, params: Dict[str, Any]) -> str:
+        """读取笔记"""
+        note_id = params.get("note_id")
+        
+        if not note_id:
+            return "❌ 读取笔记需要提供 note_id"
+        
+        note_path = self._get_note_path(note_id)
+        if not note_path.exists():
+            return f"❌ 笔记不存在: {note_id}"
+        
+        with open(note_path, 'r', encoding='utf-8') as f:
+            markdown_text = f.read()
+        
+        note = self._markdown_to_note(markdown_text)
+        
+        return self._format_note(note)
+    
+    def _update_note(self, params: Dict[str, Any]) -> str:
+        """更新笔记"""
+        note_id = params.get("note_id")
+        
+        if not note_id:
+            return "❌ 更新笔记需要提供 note_id"
+        
+        note_path = self._get_note_path(note_id)
+        if not note_path.exists():
+            return f"❌ 笔记不存在: {note_id}"
+        
+        # 读取现有笔记
+        with open(note_path, 'r', encoding='utf-8') as f:
+            markdown_text = f.read()
+        note = self._markdown_to_note(markdown_text)
+        
+        # 更新字段
+        if "title" in params:
+            note["title"] = params["title"]
+        if "content" in params:
+            note["content"] = params["content"]
+            note["metadata"]["word_count"] = len(params["content"])
+        if "note_type" in params:
+            note["type"] = params["note_type"]
+        if "tags" in params:
+            note["tags"] = params["tags"] if isinstance(params["tags"], list) else []
+        
+        note["updated_at"] = datetime.now().isoformat()
+        
+        # 保存更新(Markdown格式)
+        markdown_content = self._note_to_markdown(note)
+        with open(note_path, 'w', encoding='utf-8') as f:
+            f.write(markdown_content)
+        
+        # 更新索引
+        for idx_note in self.notes_index["notes"]:
+            if idx_note["id"] == note_id:
+                idx_note["title"] = note["title"]
+                idx_note["type"] = note["type"]
+                idx_note["tags"] = note["tags"]
+                break
+        self._save_index()
+        
+        return f"✅ 笔记更新成功: {note_id}"
+    
+    def _delete_note(self, params: Dict[str, Any]) -> str:
+        """删除笔记"""
+        note_id = params.get("note_id")
+        
+        if not note_id:
+            return "❌ 删除笔记需要提供 note_id"
+        
+        note_path = self._get_note_path(note_id)
+        if not note_path.exists():
+            return f"❌ 笔记不存在: {note_id}"
+        
+        # 删除文件
+        note_path.unlink()
+        
+        # 更新索引
+        self.notes_index["notes"] = [
+            n for n in self.notes_index["notes"] if n["id"] != note_id
+        ]
+        self.notes_index["metadata"]["total_notes"] = len(self.notes_index["notes"])
+        self._save_index()
+        
+        return f"✅ 笔记已删除: {note_id}"
+    
+    def _list_notes(self, params: Dict[str, Any]) -> str:
+        """列出笔记"""
+        note_type = params.get("note_type")
+        limit = params.get("limit", 10)
+        
+        # 过滤笔记
+        filtered_notes = self.notes_index["notes"]
+        if note_type:
+            filtered_notes = [n for n in filtered_notes if n["type"] == note_type]
+        
+        # 限制数量
+        filtered_notes = filtered_notes[:limit]
+        
+        if not filtered_notes:
+            return "📝 暂无笔记"
+        
+        result = f"📝 笔记列表(共 {len(filtered_notes)} 条)\n\n"
+        for note in filtered_notes:
+            result += f"• [{note['type']}] {note['title']}\n"
+            result += f"  ID: {note['id']}\n"
+            if note.get('tags'):
+                result += f"  标签: {', '.join(note['tags'])}\n"
+            result += f"  创建时间: {note['created_at']}\n\n"
+        
+        return result
+    
+    def _search_notes(self, params: Dict[str, Any]) -> str:
+        """搜索笔记"""
+        query = params.get("query", "").lower()
+        limit = params.get("limit", 10)
+        
+        if not query:
+            return "❌ 搜索需要提供 query"
+        
+        # 搜索匹配的笔记
+        matched_notes = []
+        for idx_note in self.notes_index["notes"]:
+            note_path = self._get_note_path(idx_note["id"])
+            if note_path.exists():
+                with open(note_path, 'r', encoding='utf-8') as f:
+                    markdown_text = f.read()
+                
+                try:
+                    note = self._markdown_to_note(markdown_text)
+                except Exception as e:
+                    print(f"⚠️ 解析笔记失败 {idx_note['id']}: {e}")
+                    continue
+                
+                # 检查标题、内容、标签是否匹配
+                if (query in note["title"].lower() or
+                    query in note["content"].lower() or
+                    any(query in tag.lower() for tag in note.get("tags", []))):
+                    matched_notes.append(note)
+        
+        # 限制数量
+        matched_notes = matched_notes[:limit]
+        
+        if not matched_notes:
+            return f"📝 未找到匹配 '{query}' 的笔记"
+        
+        result = f"🔍 搜索结果(共 {len(matched_notes)} 条)\n\n"
+        for note in matched_notes:
+            result += self._format_note(note, compact=True) + "\n"
+        
+        return result
+    
+    def _get_summary(self) -> str:
+        """获取笔记摘要"""
+        total = len(self.notes_index["notes"])
+        
+        # 按类型统计
+        type_counts = {}
+        for note in self.notes_index["notes"]:
+            note_type = note["type"]
+            type_counts[note_type] = type_counts.get(note_type, 0) + 1
+        
+        result = f"📊 笔记摘要\n\n"
+        result += f"总笔记数: {total}\n\n"
+        result += "按类型统计:\n"
+        for note_type, count in sorted(type_counts.items()):
+            result += f"  • {note_type}: {count}\n"
+        
+        return result
+    
+    def _format_note(self, note: Dict[str, Any], compact: bool = False) -> str:
+        """格式化笔记输出"""
+        if compact:
+            return (
+                f"[{note['type']}] {note['title']}\n"
+                f"ID: {note['id']}\n"
+                f"内容: {note['content'][:100]}{'...' if len(note['content']) > 100 else ''}"
+            )
+        else:
+            result = f"📝 笔记详情\n\n"
+            result += f"ID: {note['id']}\n"
+            result += f"标题: {note['title']}\n"
+            result += f"类型: {note['type']}\n"
+            if note.get('tags'):
+                result += f"标签: {', '.join(note['tags'])}\n"
+            result += f"创建时间: {note['created_at']}\n"
+            result += f"更新时间: {note['updated_at']}\n"
+            result += f"\n内容:\n{note['content']}\n"
+            return result
+

+ 76 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/plan_tool.py

@@ -0,0 +1,76 @@
+from __future__ import annotations
+
+from pathlib import Path
+from typing import Dict, Any, List, Optional
+
+from core.llm import HelloAgentsLLM
+from tools.base import Tool, ToolParameter
+
+
+class PlanTool(Tool):
+    """规划工具(可选)
+
+    用于在用户强制要求或任务明显需要多步执行时生成计划。
+    建议在 ReAct 中按需调用:plan[{"goal":"..."}] 或 plan[目标文本]
+    """
+
+    def __init__(self, llm: HelloAgentsLLM, prompt_path: Optional[str] = None):
+        super().__init__(name="plan", description="生成可执行计划(仅在需要时调用)")
+        self.llm = llm
+        self.prompt_path = Path(prompt_path).resolve() if prompt_path else None
+
+    def get_parameters(self) -> List[ToolParameter]:
+        return [
+            ToolParameter(
+                name="goal",
+                type="string",
+                description="计划目标(例如:分析项目结构并说明模块职责)",
+                required=True,
+            ),
+            ToolParameter(
+                name="constraints",
+                type="string",
+                description="额外约束(可选)",
+                required=False,
+            ),
+            ToolParameter(
+                name="output",
+                type="string",
+                description="输出格式:markdown|json(默认 markdown)",
+                required=False,
+                default="markdown",
+            ),
+        ]
+
+    def run(self, parameters: Dict[str, Any]) -> str:
+        if not self.validate_parameters(parameters):
+            return "❌ 参数验证失败:缺少 goal"
+
+        goal = str(parameters.get("goal", "")).strip()
+        constraints = parameters.get("constraints")
+        output = str(parameters.get("output", "markdown")).strip() or "markdown"
+
+        if not goal:
+            return "❌ goal 不能为空"
+
+        prompt = ""
+        if self.prompt_path and self.prompt_path.exists():
+            prompt = self.prompt_path.read_text(encoding="utf-8")
+        else:
+            prompt = (
+                "你是一个规划助手。请输出一个可执行计划(5~12步),并包含 Risks 与 Validation。"
+            )
+
+        user_msg = f"目标:{goal}\n期望输出:{output}"
+        if constraints:
+            user_msg += f"\n约束:{constraints}"
+
+        resp = self.llm.invoke(
+            [
+                {"role": "system", "content": prompt},
+                {"role": "user", "content": user_msg},
+            ],
+            max_tokens=800,
+        )
+        return resp or ""
+

+ 851 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/protocol_tools.py

@@ -0,0 +1,851 @@
+"""
+协议工具集合
+
+提供基于协议实现的工具接口:
+- MCP Tool: 基于 fastmcp 库,用于连接和调用 MCP 服务器
+- A2A Tool: 基于官方 a2a 库,用于 Agent 间通信(需要安装 a2a)
+- ANP Tool: 基于概念实现,用于服务发现和网络管理
+"""
+
+from typing import Dict, Any, List, Optional
+from ..base import Tool, ToolParameter
+import os
+
+
+# MCP服务器环境变量映射表
+# 用于自动检测常见MCP服务器需要的环境变量
+MCP_SERVER_ENV_MAP = {
+    "server-github": ["GITHUB_PERSONAL_ACCESS_TOKEN"],
+    "server-slack": ["SLACK_BOT_TOKEN", "SLACK_TEAM_ID"],
+    "server-google-drive": ["GOOGLE_CLIENT_ID", "GOOGLE_CLIENT_SECRET", "GOOGLE_REFRESH_TOKEN"],
+    "server-postgres": ["POSTGRES_CONNECTION_STRING"],
+    "server-sqlite": [],  # 不需要环境变量
+    "server-filesystem": [],  # 不需要环境变量
+}
+
+
+class MCPTool(Tool):
+    """MCP (Model Context Protocol) 工具
+
+    连接到 MCP 服务器并调用其提供的工具、资源和提示词。
+    
+    功能:
+    - 列出服务器提供的工具
+    - 调用服务器工具
+    - 读取服务器资源
+    - 获取提示词模板
+
+    使用示例:
+        >>> from hello_agents.tools.builtin import MCPTool
+        >>>
+        >>> # 方式1: 使用内置演示服务器
+        >>> tool = MCPTool()  # 自动创建内置服务器
+        >>> result = tool.run({"action": "list_tools"})
+        >>>
+        >>> # 方式2: 连接到外部 MCP 服务器
+        >>> tool = MCPTool(server_command=["python", "examples/mcp_example.py"])
+        >>> result = tool.run({"action": "list_tools"})
+        >>>
+        >>> # 方式3: 使用自定义 FastMCP 服务器
+        >>> from fastmcp import FastMCP
+        >>> server = FastMCP("MyServer")
+        >>> tool = MCPTool(server=server)
+
+    注意:使用 fastmcp 库,已包含在依赖中
+    """
+    
+    def __init__(self,
+                 name: str = "mcp",
+                 description: Optional[str] = None,
+                 server_command: Optional[List[str]] = None,
+                 server_args: Optional[List[str]] = None,
+                 server: Optional[Any] = None,
+                 auto_expand: bool = True,
+                 env: Optional[Dict[str, str]] = None,
+                 env_keys: Optional[List[str]] = None):
+        """
+        初始化 MCP 工具
+
+        Args:
+            name: 工具名称(默认为"mcp",建议为不同服务器指定不同名称)
+            description: 工具描述(可选,默认为通用描述)
+            server_command: 服务器启动命令(如 ["python", "server.py"])
+            server_args: 服务器参数列表
+            server: FastMCP 服务器实例(可选,用于内存传输)
+            auto_expand: 是否自动展开为独立工具(默认True)
+            env: 环境变量字典(优先级最高,直接传递给MCP服务器)
+            env_keys: 要从系统环境变量加载的key列表(优先级中等)
+
+        环境变量优先级(从高到低):
+            1. 直接传递的env参数
+            2. env_keys指定的环境变量
+            3. 自动检测的环境变量(根据server_command)
+
+        注意:如果所有参数都为空,将创建内置演示服务器
+
+        示例:
+            >>> # 方式1:直接传递环境变量(优先级最高)
+            >>> github_tool = MCPTool(
+            ...     name="github",
+            ...     server_command=["npx", "-y", "@modelcontextprotocol/server-github"],
+            ...     env={"GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_xxx"}
+            ... )
+            >>>
+            >>> # 方式2:从.env文件加载指定的环境变量
+            >>> github_tool = MCPTool(
+            ...     name="github",
+            ...     server_command=["npx", "-y", "@modelcontextprotocol/server-github"],
+            ...     env_keys=["GITHUB_PERSONAL_ACCESS_TOKEN"]
+            ... )
+            >>>
+            >>> # 方式3:自动检测(最简单,推荐)
+            >>> github_tool = MCPTool(
+            ...     name="github",
+            ...     server_command=["npx", "-y", "@modelcontextprotocol/server-github"]
+            ...     # 自动从环境变量加载GITHUB_PERSONAL_ACCESS_TOKEN
+            ... )
+        """
+        self.server_command = server_command
+        self.server_args = server_args or []
+        self.server = server
+        self._client = None
+        self._available_tools = []
+        self.auto_expand = auto_expand
+        self.prefix = f"{name}_" if auto_expand else ""
+
+        # 环境变量处理(优先级:env > env_keys > 自动检测)
+        self.env = self._prepare_env(env, env_keys, server_command)
+
+        # 如果没有指定任何服务器,创建内置演示服务器
+        if not server_command and not server:
+            self.server = self._create_builtin_server()
+
+        # 自动发现工具
+        self._discover_tools()
+
+        # 设置默认描述或自动生成
+        if description is None:
+            description = self._generate_description()
+
+        super().__init__(
+            name=name,
+            description=description
+        )
+
+    def _prepare_env(self,
+                     env: Optional[Dict[str, str]],
+                     env_keys: Optional[List[str]],
+                     server_command: Optional[List[str]]) -> Dict[str, str]:
+        """
+        准备环境变量
+
+        优先级:env > env_keys > 自动检测
+
+        Args:
+            env: 直接传递的环境变量字典
+            env_keys: 要从系统环境变量加载的key列表
+            server_command: 服务器命令(用于自动检测)
+
+        Returns:
+            合并后的环境变量字典
+        """
+        result_env = {}
+
+        # 1. 自动检测(优先级最低)
+        if server_command:
+            # 从命令中提取服务器名称
+            server_name = None
+            for part in server_command:
+                if "server-" in part:
+                    # 提取类似 "@modelcontextprotocol/server-github" 中的 "server-github"
+                    server_name = part.split("/")[-1] if "/" in part else part
+                    break
+
+            # 查找映射表
+            if server_name and server_name in MCP_SERVER_ENV_MAP:
+                auto_keys = MCP_SERVER_ENV_MAP[server_name]
+                for key in auto_keys:
+                    value = os.getenv(key)
+                    if value:
+                        result_env[key] = value
+                        print(f"🔑 自动加载环境变量: {key}")
+
+        # 2. env_keys指定的环境变量(优先级中等)
+        if env_keys:
+            for key in env_keys:
+                value = os.getenv(key)
+                if value:
+                    result_env[key] = value
+                    print(f"🔑 从env_keys加载环境变量: {key}")
+                else:
+                    print(f"⚠️  警告: 环境变量 {key} 未设置")
+
+        # 3. 直接传递的env(优先级最高)
+        if env:
+            result_env.update(env)
+            for key in env.keys():
+                print(f"🔑 使用直接传递的环境变量: {key}")
+
+        return result_env
+
+    def _create_builtin_server(self):
+        """创建内置演示服务器"""
+        try:
+            from fastmcp import FastMCP
+
+            server = FastMCP("HelloAgents-BuiltinServer")
+
+            @server.tool()
+            def add(a: float, b: float) -> float:
+                """加法计算器"""
+                return a + b
+
+            @server.tool()
+            def subtract(a: float, b: float) -> float:
+                """减法计算器"""
+                return a - b
+
+            @server.tool()
+            def multiply(a: float, b: float) -> float:
+                """乘法计算器"""
+                return a * b
+
+            @server.tool()
+            def divide(a: float, b: float) -> float:
+                """除法计算器"""
+                if b == 0:
+                    raise ValueError("除数不能为零")
+                return a / b
+
+            @server.tool()
+            def greet(name: str = "World") -> str:
+                """友好问候"""
+                return f"Hello, {name}! 欢迎使用 HelloAgents MCP 工具!"
+
+            @server.tool()
+            def get_system_info() -> dict:
+                """获取系统信息"""
+                import platform
+                import sys
+                return {
+                    "platform": platform.system(),
+                    "python_version": sys.version,
+                    "server_name": "HelloAgents-BuiltinServer",
+                    "tools_count": 6
+                }
+
+            return server
+
+        except ImportError:
+            raise ImportError(
+                "创建内置 MCP 服务器需要 fastmcp 库。请安装: pip install fastmcp"
+            )
+
+    def _discover_tools(self):
+        """发现MCP服务器提供的所有工具"""
+        try:
+            from hello_agents.protocols.mcp.client import MCPClient
+            import asyncio
+
+            async def discover():
+                client_source = self.server if self.server else self.server_command
+                async with MCPClient(client_source, self.server_args, env=self.env) as client:
+                    tools = await client.list_tools()
+                    return tools
+
+            # 运行异步发现
+            try:
+                loop = asyncio.get_running_loop()
+                # 如果已有循环,在新线程中运行
+                import concurrent.futures
+                def run_in_thread():
+                    new_loop = asyncio.new_event_loop()
+                    asyncio.set_event_loop(new_loop)
+                    try:
+                        return new_loop.run_until_complete(discover())
+                    finally:
+                        new_loop.close()
+
+                with concurrent.futures.ThreadPoolExecutor() as executor:
+                    future = executor.submit(run_in_thread)
+                    self._available_tools = future.result()
+            except RuntimeError:
+                # 没有运行中的循环
+                self._available_tools = asyncio.run(discover())
+
+        except Exception as e:
+            # 工具发现失败不影响初始化
+            self._available_tools = []
+
+    def _generate_description(self) -> str:
+        """生成增强的工具描述"""
+        if not self._available_tools:
+            return "连接到 MCP 服务器,调用工具、读取资源和获取提示词。支持内置服务器和外部服务器。"
+
+        if self.auto_expand:
+            # 展开模式:简单描述
+            return f"MCP工具服务器,包含{len(self._available_tools)}个工具。这些工具会自动展开为独立的工具供Agent使用。"
+        else:
+            # 非展开模式:详细描述
+            desc_parts = [
+                f"MCP工具服务器,提供{len(self._available_tools)}个工具:"
+            ]
+
+            # 列出所有工具
+            for tool in self._available_tools:
+                tool_name = tool.get('name', 'unknown')
+                tool_desc = tool.get('description', '无描述')
+                # 简化描述,只取第一句
+                short_desc = tool_desc.split('.')[0] if tool_desc else '无描述'
+                desc_parts.append(f"  • {tool_name}: {short_desc}")
+
+            # 添加调用格式说明
+            desc_parts.append("\n调用格式:返回JSON格式的参数")
+            desc_parts.append('{"action": "call_tool", "tool_name": "工具名", "arguments": {...}}')
+
+            # 添加示例
+            if self._available_tools:
+                first_tool = self._available_tools[0]
+                tool_name = first_tool.get('name', 'example')
+                desc_parts.append(f'\n示例:{{"action": "call_tool", "tool_name": "{tool_name}", "arguments": {{...}}}}')
+
+            return "\n".join(desc_parts)
+
+    def get_expanded_tools(self) -> List['Tool']:  # type: ignore
+        """
+        获取展开的工具列表
+
+        将MCP服务器的每个工具包装成独立的Tool对象
+
+        Returns:
+            Tool对象列表
+        """
+        if not self.auto_expand:
+            return []
+
+        from .mcp_wrapper_tool import MCPWrappedTool
+
+        expanded_tools = []
+        for tool_info in self._available_tools:
+            wrapped_tool = MCPWrappedTool(
+                mcp_tool=self,
+                tool_info=tool_info,
+                prefix=self.prefix
+            )
+            expanded_tools.append(wrapped_tool)
+
+        return expanded_tools
+
+    def run(self, parameters: Dict[str, Any]) -> str:
+        """
+        执行 MCP 操作
+
+        Args:
+            parameters: 包含以下参数的字典
+                - action: 操作类型 (list_tools, call_tool, list_resources, read_resource, list_prompts, get_prompt)
+                  如果不指定action但指定了tool_name,会自动推断为call_tool
+                - tool_name: 工具名称(call_tool 需要)
+                - arguments: 工具参数(call_tool 需要)
+                - uri: 资源 URI(read_resource 需要)
+                - prompt_name: 提示词名称(get_prompt 需要)
+                - prompt_arguments: 提示词参数(get_prompt 可选)
+
+        Returns:
+            操作结果
+        """
+        from hello_agents.protocols.mcp.client import MCPClient
+
+        # 智能推断action:如果没有action但有tool_name,自动设置为call_tool
+        action = parameters.get("action", "").lower()
+        if not action and "tool_name" in parameters:
+            action = "call_tool"
+            parameters["action"] = action
+
+        if not action:
+            return "错误:必须指定 action 参数或 tool_name 参数"
+        
+        try:
+            # 使用增强的异步客户端
+            import asyncio
+            from hello_agents.protocols.mcp.client import MCPClient
+
+            async def run_mcp_operation():
+                # 根据配置选择客户端创建方式
+                if self.server:
+                    # 使用内置服务器(内存传输)
+                    client_source = self.server
+                else:
+                    # 使用外部服务器命令
+                    client_source = self.server_command
+
+                async with MCPClient(client_source, self.server_args, env=self.env) as client:
+                    if action == "list_tools":
+                        tools = await client.list_tools()
+                        if not tools:
+                            return "没有找到可用的工具"
+                        result = f"找到 {len(tools)} 个工具:\n"
+                        for tool in tools:
+                            result += f"- {tool['name']}: {tool['description']}\n"
+                        return result
+
+                    elif action == "call_tool":
+                        tool_name = parameters.get("tool_name")
+                        arguments = parameters.get("arguments", {})
+                        if not tool_name:
+                            return "错误:必须指定 tool_name 参数"
+                        result = await client.call_tool(tool_name, arguments)
+                        return f"工具 '{tool_name}' 执行结果:\n{result}"
+
+                    elif action == "list_resources":
+                        resources = await client.list_resources()
+                        if not resources:
+                            return "没有找到可用的资源"
+                        result = f"找到 {len(resources)} 个资源:\n"
+                        for resource in resources:
+                            result += f"- {resource['uri']}: {resource['name']}\n"
+                        return result
+
+                    elif action == "read_resource":
+                        uri = parameters.get("uri")
+                        if not uri:
+                            return "错误:必须指定 uri 参数"
+                        content = await client.read_resource(uri)
+                        return f"资源 '{uri}' 内容:\n{content}"
+
+                    elif action == "list_prompts":
+                        prompts = await client.list_prompts()
+                        if not prompts:
+                            return "没有找到可用的提示词"
+                        result = f"找到 {len(prompts)} 个提示词:\n"
+                        for prompt in prompts:
+                            result += f"- {prompt['name']}: {prompt['description']}\n"
+                        return result
+
+                    elif action == "get_prompt":
+                        prompt_name = parameters.get("prompt_name")
+                        prompt_arguments = parameters.get("prompt_arguments", {})
+                        if not prompt_name:
+                            return "错误:必须指定 prompt_name 参数"
+                        messages = await client.get_prompt(prompt_name, prompt_arguments)
+                        result = f"提示词 '{prompt_name}':\n"
+                        for msg in messages:
+                            result += f"[{msg['role']}] {msg['content']}\n"
+                        return result
+
+                    else:
+                        return f"错误:不支持的操作 '{action}'"
+
+            # 运行异步操作
+            try:
+                # 检查是否已有运行中的事件循环
+                try:
+                    loop = asyncio.get_running_loop()
+                    # 如果有运行中的循环,在新线程中运行新的事件循环
+                    import concurrent.futures
+                    import threading
+
+                    def run_in_thread():
+                        # 在新线程中创建新的事件循环
+                        new_loop = asyncio.new_event_loop()
+                        asyncio.set_event_loop(new_loop)
+                        try:
+                            return new_loop.run_until_complete(run_mcp_operation())
+                        finally:
+                            new_loop.close()
+
+                    with concurrent.futures.ThreadPoolExecutor() as executor:
+                        future = executor.submit(run_in_thread)
+                        return future.result()
+                except RuntimeError:
+                    # 没有运行中的循环,直接运行
+                    return asyncio.run(run_mcp_operation())
+            except Exception as e:
+                return f"异步操作失败: {str(e)}"
+                    
+        except Exception as e:
+            return f"MCP 操作失败: {str(e)}"
+    
+    def get_parameters(self) -> List[ToolParameter]:
+        """获取工具参数定义"""
+        return [
+            ToolParameter(
+                name="action",
+                type="string",
+                description="操作类型: list_tools, call_tool, list_resources, read_resource, list_prompts, get_prompt",
+                required=True
+            ),
+            ToolParameter(
+                name="tool_name",
+                type="string",
+                description="工具名称(call_tool 操作需要)",
+                required=False
+            ),
+            ToolParameter(
+                name="arguments",
+                type="object",
+                description="工具参数(call_tool 操作需要)",
+                required=False
+            ),
+            ToolParameter(
+                name="uri",
+                type="string",
+                description="资源 URI(read_resource 操作需要)",
+                required=False
+            ),
+            ToolParameter(
+                name="prompt_name",
+                type="string",
+                description="提示词名称(get_prompt 操作需要)",
+                required=False
+            ),
+            ToolParameter(
+                name="prompt_arguments",
+                type="object",
+                description="提示词参数(get_prompt 操作可选)",
+                required=False
+            )
+        ]
+
+
+class A2ATool(Tool):
+    """A2A (Agent-to-Agent Protocol) 工具
+
+    连接到 A2A Agent 并进行通信。
+    
+    功能:
+    - 向 Agent 提问
+    - 获取 Agent 信息
+    - 发送自定义消息
+
+    使用示例:
+        >>> from hello_agents.tools.builtin import A2ATool
+        >>> # 连接到 A2A Agent(使用默认名称)
+        >>> tool = A2ATool(agent_url="http://localhost:5000")
+        >>> # 连接到 A2A Agent(自定义名称和描述)
+        >>> tool = A2ATool(
+        ...     agent_url="http://localhost:5000",
+        ...     name="tech_expert",
+        ...     description="技术专家,回答技术相关问题"
+        ... )
+        >>> # 提问
+        >>> result = tool.run({"action": "ask", "question": "计算 2+2"})
+        >>> # 获取信息
+        >>> result = tool.run({"action": "get_info"})
+    
+    注意:需要安装官方 a2a-sdk 库: pip install a2a-sdk
+    详见文档: docs/chapter10/A2A_GUIDE.md
+    官方仓库: https://github.com/a2aproject/a2a-python
+    """
+    
+    def __init__(self, agent_url: str, name: str = "a2a", description: str = None):
+        """
+        初始化 A2A 工具
+
+        Args:
+            agent_url: Agent URL
+            name: 工具名称(可选,默认为 "a2a")
+            description: 工具描述(可选)
+        """
+        if description is None:
+            description = "连接到 A2A Agent,支持提问和获取信息。需要安装官方 a2a-sdk 库。"
+
+        super().__init__(
+            name=name,
+            description=description
+        )
+        self.agent_url = agent_url
+        
+    def run(self, parameters: Dict[str, Any]) -> str:
+        """
+        执行 A2A 操作
+        
+        Args:
+            parameters: 包含以下参数的字典
+                - action: 操作类型 (ask, get_info)
+                - question: 问题文本(ask 需要)
+        
+        Returns:
+            操作结果
+        """
+        try:
+            from hello_agents.protocols.a2a.implementation import A2AClient, A2A_AVAILABLE
+            if not A2A_AVAILABLE:
+                return ("错误:需要安装 a2a-sdk 库\n"
+                       "安装命令: pip install a2a-sdk\n"
+                       "详见文档: docs/chapter10/A2A_GUIDE.md\n"
+                       "官方仓库: https://github.com/a2aproject/a2a-python")
+        except ImportError:
+            return ("错误:无法导入 A2A 模块\n"
+                   "安装命令: pip install a2a-sdk\n"
+                   "详见文档: docs/chapter10/A2A_GUIDE.md\n"
+                   "官方仓库: https://github.com/a2aproject/a2a-python")
+
+        action = parameters.get("action", "").lower()
+        
+        if not action:
+            return "错误:必须指定 action 参数"
+        
+        try:
+            client = A2AClient(self.agent_url)
+            
+            if action == "ask":
+                question = parameters.get("question")
+                if not question:
+                    return "错误:必须指定 question 参数"
+                response = client.ask(question)
+                return f"Agent 回答:\n{response}"
+                
+            elif action == "get_info":
+                info = client.get_info()
+                result = "Agent 信息:\n"
+                for key, value in info.items():
+                    result += f"- {key}: {value}\n"
+                return result
+                
+            else:
+                return f"错误:不支持的操作 '{action}'"
+                
+        except Exception as e:
+            return f"A2A 操作失败: {str(e)}"
+    
+    def get_parameters(self) -> List[ToolParameter]:
+        """获取工具参数定义"""
+        return [
+            ToolParameter(
+                name="action",
+                type="string",
+                description="操作类型: ask(提问), get_info(获取信息)",
+                required=True
+            ),
+            ToolParameter(
+                name="question",
+                type="string",
+                description="问题文本(ask 操作需要)",
+                required=False
+            )
+        ]
+
+
+class ANPTool(Tool):
+    """ANP (Agent Network Protocol) 工具
+
+    提供智能体网络管理功能,包括服务发现、节点管理和消息路由。
+    这是一个概念性实现,用于演示 Agent 网络管理的核心理念。
+    
+    功能:
+    - 注册和发现服务
+    - 添加和管理网络节点
+    - 消息路由
+    - 网络统计
+
+    使用示例:
+        >>> from hello_agents.tools.builtin import ANPTool
+        >>> tool = ANPTool()
+        >>> # 注册服务
+        >>> result = tool.run({
+        ...     "action": "register_service",
+        ...     "service_id": "calc-1",
+        ...     "service_type": "calculator",
+        ...     "endpoint": "http://localhost:5001"
+        ... })
+        >>> # 发现服务
+        >>> result = tool.run({
+        ...     "action": "discover_services",
+        ...     "service_type": "calculator"
+        ... })
+        >>> # 添加节点
+        >>> result = tool.run({
+        ...     "action": "add_node",
+        ...     "node_id": "agent-1",
+        ...     "endpoint": "http://localhost:5001"
+        ... })
+    
+    注意:这是概念性实现,不需要额外依赖
+    详见文档: docs/chapter10/ANP_CONCEPTS.md
+    """
+    
+    def __init__(self, name: str = "anp", description: str = None, discovery=None, network=None):
+        """初始化 ANP 工具
+
+        Args:
+            name: 工具名称
+            description: 工具描述
+            discovery: 可选的 ANPDiscovery 实例,如果不提供则创建新实例
+            network: 可选的 ANPNetwork 实例,如果不提供则创建新实例
+        """
+        if description is None:
+            description = "智能体网络管理工具,支持服务发现、节点管理和消息路由。概念性实现。"
+
+        super().__init__(
+            name=name,
+            description=description
+        )
+        from hello_agents.protocols.anp.implementation import ANPDiscovery, ANPNetwork
+        self._discovery = discovery if discovery is not None else ANPDiscovery()
+        self._network = network if network is not None else ANPNetwork()
+        
+    def run(self, parameters: Dict[str, Any]) -> str:
+        """
+        执行 ANP 操作
+        
+        Args:
+            parameters: 包含以下参数的字典
+                - action: 操作类型 (register_service, discover_services, add_node, route_message, get_stats)
+                - service_id, service_type, endpoint: 服务信息(register_service 需要)
+                - node_id, endpoint: 节点信息(add_node 需要)
+                - from_node, to_node, message: 路由信息(route_message 需要)
+        
+        Returns:
+            操作结果
+        """
+        from hello_agents.protocols.anp.implementation import ServiceInfo
+
+        action = parameters.get("action", "").lower()
+        
+        if not action:
+            return "错误:必须指定 action 参数"
+        
+        try:
+            if action == "register_service":
+                service_id = parameters.get("service_id")
+                service_type = parameters.get("service_type")
+                endpoint = parameters.get("endpoint")
+                metadata = parameters.get("metadata", {})
+                
+                if not all([service_id, service_type, endpoint]):
+                    return "错误:必须指定 service_id, service_type 和 endpoint 参数"
+                
+                service = ServiceInfo(service_id, service_type, endpoint, metadata)
+                self._discovery.register_service(service)
+                return f"✅ 已注册服务 '{service_id}'"
+
+            elif action == "unregister_service":
+                service_id = parameters.get("service_id")
+                if not service_id:
+                    return "错误:必须指定 service_id 参数"
+
+                # 使用 ANPDiscovery 的 unregister_service 方法
+                success = self._discovery.unregister_service(service_id)
+
+                if success:
+                    return f"✅ 已注销服务 '{service_id}'"
+                else:
+                    return f"错误:服务 '{service_id}' 不存在"
+
+            elif action == "discover_services":
+                service_type = parameters.get("service_type")
+                services = self._discovery.discover_services(service_type)
+
+                if not services:
+                    return "没有找到服务"
+
+                result = f"找到 {len(services)} 个服务:\n\n"
+                for service in services:
+                    result += f"服务ID: {service.service_id}\n"
+                    result += f"  名称: {service.service_name}\n"
+                    result += f"  类型: {service.service_type}\n"
+                    result += f"  端点: {service.endpoint}\n"
+                    if service.capabilities:
+                        result += f"  能力: {', '.join(service.capabilities)}\n"
+                    if service.metadata:
+                        result += f"  元数据: {service.metadata}\n"
+                    result += "\n"
+                return result
+                
+            elif action == "add_node":
+                node_id = parameters.get("node_id")
+                endpoint = parameters.get("endpoint")
+                metadata = parameters.get("metadata", {})
+                
+                if not all([node_id, endpoint]):
+                    return "错误:必须指定 node_id 和 endpoint 参数"
+                
+                self._network.add_node(node_id, endpoint, metadata)
+                return f"✅ 已添加节点 '{node_id}'"
+                
+            elif action == "route_message":
+                from_node = parameters.get("from_node")
+                to_node = parameters.get("to_node")
+                message = parameters.get("message", {})
+                
+                if not all([from_node, to_node]):
+                    return "错误:必须指定 from_node 和 to_node 参数"
+                
+                path = self._network.route_message(from_node, to_node, message)
+                if path:
+                    return f"消息路由路径: {' -> '.join(path)}"
+                else:
+                    return "无法找到路由路径"
+                
+            elif action == "get_stats":
+                stats = self._network.get_network_stats()
+                result = "网络统计:\n"
+                for key, value in stats.items():
+                    result += f"- {key}: {value}\n"
+                return result
+                
+            else:
+                return f"错误:不支持的操作 '{action}'"
+                
+        except Exception as e:
+            return f"ANP 操作失败: {str(e)}"
+    
+    def get_parameters(self) -> List[ToolParameter]:
+        """获取工具参数定义"""
+        return [
+            ToolParameter(
+                name="action",
+                type="string",
+                description="操作类型: register_service, unregister_service, discover_services, add_node, route_message, get_stats",
+                required=True
+            ),
+            ToolParameter(
+                name="service_id",
+                type="string",
+                description="服务 ID(register_service, unregister_service 需要)",
+                required=False
+            ),
+            ToolParameter(
+                name="service_type",
+                type="string",
+                description="服务类型(register_service 需要)",
+                required=False
+            ),
+            ToolParameter(
+                name="endpoint",
+                type="string",
+                description="端点地址(register_service, add_node 需要)",
+                required=False
+            ),
+            ToolParameter(
+                name="node_id",
+                type="string",
+                description="节点 ID(add_node 需要)",
+                required=False
+            ),
+            ToolParameter(
+                name="from_node",
+                type="string",
+                description="源节点 ID(route_message 需要)",
+                required=False
+            ),
+            ToolParameter(
+                name="to_node",
+                type="string",
+                description="目标节点 ID(route_message 需要)",
+                required=False
+            ),
+            ToolParameter(
+                name="message",
+                type="object",
+                description="消息内容(route_message 需要)",
+                required=False
+            ),
+            ToolParameter(
+                name="metadata",
+                type="object",
+                description="元数据(register_service, add_node 可选)",
+                required=False
+            )
+        ]
+

+ 264 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/search.py

@@ -0,0 +1,264 @@
+"""搜索工具 - HelloAgents原生搜索实现"""
+
+import os
+from typing import Optional, Dict, Any, List
+
+from ..base import Tool, ToolParameter
+
+class SearchTool(Tool):
+    """
+    智能混合搜索工具
+
+    支持多种搜索引擎后端,智能选择最佳搜索源:
+    1. 混合模式 (hybrid) - 智能选择TAVILY或SERPAPI
+    2. Tavily API (tavily) - 专业AI搜索
+    3. SerpApi (serpapi) - 传统Google搜索
+    """
+
+    def __init__(self, backend: str = "hybrid", tavily_key: Optional[str] = None, serpapi_key: Optional[str] = None):
+        super().__init__(
+            name="search",
+            description="一个智能网页搜索引擎。支持混合搜索模式,自动选择最佳搜索源。当你需要回答关于时事、事实以及在你的知识库中找不到的信息时,应使用此工具。"
+        )
+        self.backend = backend
+        self.tavily_key = tavily_key or os.getenv("TAVILY_API_KEY")
+        self.serpapi_key = serpapi_key or os.getenv("SERPAPI_API_KEY")
+        self.available_backends = []
+        self._setup_backends()
+
+    def _setup_backends(self):
+        """设置搜索后端"""
+        # 检查Tavily可用性
+        if self.tavily_key:
+            try:
+                from tavily import TavilyClient
+                self.tavily_client = TavilyClient(api_key=self.tavily_key)
+                self.available_backends.append("tavily")
+                print("✅ Tavily搜索引擎已初始化")
+            except ImportError:
+                print("⚠️ Tavily未安装,无法使用Tavily搜索")
+        else:
+            print("⚠️ TAVILY_API_KEY未设置")
+
+        # 检查SerpApi可用性
+        if self.serpapi_key:
+            try:
+                import serpapi
+                self.available_backends.append("serpapi")
+                print("✅ SerpApi搜索引擎已初始化")
+            except ImportError:
+                print("⚠️ SerpApi未安装,无法使用SerpApi搜索")
+        else:
+            print("⚠️ SERPAPI_API_KEY未设置")
+
+        # 确定最终使用的后端
+        if self.backend == "hybrid":
+            if self.available_backends:
+                print(f"🔧 混合搜索模式已启用,可用后端: {', '.join(self.available_backends)}")
+            else:
+                print("⚠️ 没有可用的搜索后端,请配置API密钥")
+        elif self.backend == "tavily" and "tavily" not in self.available_backends:
+            print("⚠️ Tavily不可用,请检查TAVILY_API_KEY配置")
+        elif self.backend == "serpapi" and "serpapi" not in self.available_backends:
+            print("⚠️ SerpApi不可用,请检查SERPAPI_API_KEY配置")
+        elif self.backend not in ["tavily", "serpapi", "hybrid"]:
+            print("⚠️ 不支持的搜索后端,将使用hybrid模式")
+            self.backend = "hybrid"
+
+    def run(self, parameters: Dict[str, Any]) -> str:
+        """
+        执行搜索
+
+        Args:
+            parameters: 包含input参数的字典
+
+        Returns:
+            搜索结果
+        """
+        query = parameters.get("input", "").strip()
+        if not query:
+            return "错误:搜索查询不能为空"
+
+        print(f"🔍 正在执行搜索: {query}")
+
+        try:
+            if self.backend == "hybrid":
+                return self._search_hybrid(query)
+            elif self.backend == "tavily":
+                if "tavily" not in self.available_backends:
+                    return self._get_api_config_message()
+                return self._search_tavily(query)
+            elif self.backend == "serpapi":
+                if "serpapi" not in self.available_backends:
+                    return self._get_api_config_message()
+                return self._search_serpapi(query)
+            else:
+                return self._get_api_config_message()
+        except Exception as e:
+            return f"搜索时发生错误: {str(e)}"
+
+    def _search_hybrid(self, query: str) -> str:
+        """混合搜索 - 智能选择最佳搜索源"""
+        # 检查是否有可用的搜索源
+        if not self.available_backends:
+            return self._get_api_config_message()
+
+        # 优先使用Tavily(AI优化的搜索)
+        if "tavily" in self.available_backends:
+            try:
+                print("🎯 使用Tavily进行AI优化搜索")
+                return self._search_tavily(query)
+            except Exception as e:
+                print(f"⚠️ Tavily搜索失败: {e}")
+                # 如果Tavily失败,尝试SerpApi
+                if "serpapi" in self.available_backends:
+                    print("🔄 切换到SerpApi搜索")
+                    return self._search_serpapi(query)
+
+        # 如果Tavily不可用,使用SerpApi
+        elif "serpapi" in self.available_backends:
+            try:
+                print("🎯 使用SerpApi进行Google搜索")
+                return self._search_serpapi(query)
+            except Exception as e:
+                print(f"⚠️ SerpApi搜索失败: {e}")
+
+        # 如果都失败了,返回API配置提示
+        return "❌ 所有搜索源都失败了,请检查网络连接和API密钥配置"
+
+    def _search_tavily(self, query: str) -> str:
+        """使用Tavily搜索"""
+        response = self.tavily_client.search(
+            query=query,
+            search_depth="basic",
+            include_answer=True,
+            max_results=3
+        )
+
+        result = f"🎯 Tavily AI搜索结果:{response.get('answer', '未找到直接答案')}\n\n"
+
+        for i, item in enumerate(response.get('results', [])[:3], 1):
+            result += f"[{i}] {item.get('title', '')}\n"
+            result += f"    {item.get('content', '')[:200]}...\n"
+            result += f"    来源: {item.get('url', '')}\n\n"
+
+        return result
+
+    def _search_serpapi(self, query: str) -> str:
+        """使用SerpApi搜索"""
+        try:
+            from serpapi import SerpApiClient
+        except ImportError:
+            return "错误:SerpApi未安装,请运行 pip install serpapi"
+
+        params = {
+            "engine": "google",
+            "q": query,
+            "api_key": self.serpapi_key,
+            "gl": "cn",
+            "hl": "zh-cn",
+        }
+
+        client = SerpApiClient(params)
+        results = client.get_dict()
+
+        result_text = "🔍 SerpApi Google搜索结果:\n\n"
+
+        # 智能解析:优先寻找最直接的答案
+        if "answer_box" in results and "answer" in results["answer_box"]:
+            result_text += f"💡 直接答案:{results['answer_box']['answer']}\n\n"
+
+        if "knowledge_graph" in results and "description" in results["knowledge_graph"]:
+            result_text += f"📖 知识图谱:{results['knowledge_graph']['description']}\n\n"
+
+        if "organic_results" in results and results["organic_results"]:
+            result_text += "🔗 相关结果:\n"
+            for i, res in enumerate(results["organic_results"][:3], 1):
+                result_text += f"[{i}] {res.get('title', '')}\n"
+                result_text += f"    {res.get('snippet', '')}\n"
+                result_text += f"    来源: {res.get('link', '')}\n\n"
+            return result_text
+
+        return f"对不起,没有找到关于 '{query}' 的信息。"
+
+    def _get_api_config_message(self) -> str:
+        """获取API配置提示信息"""
+        tavily_key = os.getenv("TAVILY_API_KEY")
+        serpapi_key = os.getenv("SERPAPI_API_KEY")
+
+        message = "❌ 没有可用的搜索源,请检查以下配置:\n\n"
+
+        # 检查Tavily
+        message += "1. Tavily API:\n"
+        if not tavily_key:
+            message += "   ❌ 环境变量 TAVILY_API_KEY 未设置\n"
+            message += "   📝 获取地址: https://tavily.com/\n"
+        else:
+            try:
+                import tavily
+                message += "   ✅ API密钥已配置,包已安装\n"
+            except ImportError:
+                message += "   ❌ API密钥已配置,但需要安装包: pip install tavily-python\n"
+
+        message += "\n"
+
+        # 检查SerpAPI
+        message += "2. SerpAPI:\n"
+        if not serpapi_key:
+            message += "   ❌ 环境变量 SERPAPI_API_KEY 未设置\n"
+            message += "   📝 获取地址: https://serpapi.com/\n"
+        else:
+            try:
+                import serpapi
+                message += "   ✅ API密钥已配置,包已安装\n"
+            except ImportError:
+                message += "   ❌ API密钥已配置,但需要安装包: pip install google-search-results\n"
+
+        message += "\n配置方法:\n"
+        message += "- 在.env文件中添加: TAVILY_API_KEY=your_key_here\n"
+        message += "- 或在环境变量中设置: export TAVILY_API_KEY=your_key_here\n"
+        message += "\n配置后重新运行程序。"
+
+        return message
+
+    def get_parameters(self) -> List[ToolParameter]:
+        """获取工具参数定义"""
+        return [
+            ToolParameter(
+                name="input",
+                type="string",
+                description="搜索查询关键词",
+                required=True
+            )
+        ]
+
+# 便捷函数
+def search(query: str, backend: str = "hybrid") -> str:
+    """
+    便捷的搜索函数
+
+    Args:
+        query: 搜索查询关键词
+        backend: 搜索后端 ("hybrid", "tavily", "serpapi")
+
+    Returns:
+        搜索结果
+    """
+    tool = SearchTool(backend=backend)
+    return tool.run({"input": query})
+
+# 专用搜索函数
+def search_tavily(query: str) -> str:
+    """使用Tavily进行AI优化搜索"""
+    tool = SearchTool(backend="tavily")
+    return tool.run({"input": query})
+
+def search_serpapi(query: str) -> str:
+    """使用SerpApi进行Google搜索"""
+    tool = SearchTool(backend="serpapi")
+    return tool.run({"input": query})
+
+def search_hybrid(query: str) -> str:
+    """智能混合搜索,自动选择最佳搜索源"""
+    tool = SearchTool(backend="hybrid")
+    return tool.run({"input": query})

+ 724 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/terminal_tool.py

@@ -0,0 +1,724 @@
+"""TerminalTool - 命令行工具
+
+为Agent提供安全的命令行执行能力,支持:
+- 文件系统操作(ls, cat, head, tail, find, grep)
+- 文本处理(wc, sort, uniq)
+- 目录导航(pwd, cd)
+- 安全限制(白名单命令、路径限制、超时控制)
+
+使用场景:
+- JIT(即时)文件检索与分析
+- 代码仓库探索
+- 日志文件分析
+- 数据文件预览
+
+安全特性:
+- 命令白名单(只允许安全的只读命令)
+- 工作目录限制(沙箱)
+- 超时控制
+- 输出大小限制
+- 禁止危险操作(rm, mv, chmod等)
+"""
+
+from typing import Dict, Any, List, Optional
+import subprocess
+import os
+from pathlib import Path
+import shlex
+import re
+
+from ..base import Tool, ToolParameter
+
+
+class TerminalTool(Tool):
+    """命令行工具
+    
+    提供安全的命令行执行能力,支持常用的文件系统和文本处理命令。
+    
+    安全限制:
+    - 只允许白名单中的命令
+    - 限制在指定工作目录内
+    - 超时控制(默认30秒)
+    - 输出大小限制(默认10MB)
+    
+    用法示例:
+    ```python
+    terminal = TerminalTool(workspace="./project")
+    
+    # 列出文件
+    result = terminal.run({"command": "ls -la"})
+    
+    # 查看文件内容
+    result = terminal.run({"command": "cat README.md"})
+    
+    # 搜索文件
+    result = terminal.run({"command": "grep -r 'TODO' src/"})
+    
+    # 查看文件前10行
+    result = terminal.run({"command": "head -n 10 data.csv"})
+    ```
+    """
+    
+    # 允许的命令白名单
+    # 这些命令被认为是安全的,主要用于文件查看、文本处理和信息获取
+    # 不包含可能修改系统或造成安全风险的命令(如rm、mv、chmod等)
+    ALLOWED_COMMANDS = {
+        # 文件列表与信息
+        'ls', 'dir', 'tree',
+        # 文件内容查看
+        'cat', 'head', 'tail', 'less', 'more',
+        # 文件搜索
+        'find', 'grep', 'egrep', 'fgrep', 'rg',
+        # 文本处理
+        'wc', 'sort', 'uniq', 'cut', 'awk', 'sed',
+        # shell 常见内建(用于管道/小脚本;仍受整体策略约束)
+        'echo', 'printf',
+        # 目录/文件创建(受路径沙箱约束)
+        'mkdir',
+        # 目录操作
+        'pwd', 'cd',
+        # 文件信息
+        'file', 'stat', 'du', 'df',
+        # 其他
+        'which', 'whereis',
+        # 版本控制(只读子命令会被进一步限制)
+        'git',
+    }
+
+    # 常见 shell 元字符(用于检测"组合命令/写盘/子命令"等风险点;不再一刀切禁止)
+    # 这些元字符在shell中有特殊含义,可能用于组合命令或执行危险操作
+    # 系统会检测这些字符的存在,并根据安全策略决定是否允许执行
+    SHELL_META_TOKENS = ["|", "||", "&&", ";", ">", ">>", "<", "$(", "`"]
+
+    # 需要人类确认的高风险命令(MVP:只做识别;是否放行由上层策略决定)
+    # 这些命令可能对系统造成不可逆的修改,需要用户明确确认才能执行
+    DANGEROUS_BASE_COMMANDS = {"rm", "chmod"}
+    # Git的高风险子命令,可能造成代码丢失或历史修改
+    DANGEROUS_GIT_SUBCOMMANDS = {("reset", "--hard"), ("reset", "--hard", "HEAD")}
+    
+    def __init__(
+        self,
+        workspace: str = ".",
+        timeout: int = 30,
+        max_output_size: int = 10 * 1024 * 1024,  # 10MB
+        allow_cd: bool = True,
+        confirm_dangerous: bool = False,
+        default_shell_mode: bool = False,
+    ):
+        """初始化TerminalTool实例
+        
+        Args:
+            workspace: 工作目录路径,所有命令将在此目录或其子目录中执行
+            timeout: 命令执行超时时间(秒),防止长时间运行的命令
+            max_output_size: 输出大小限制(字节),防止过大输出消耗资源
+            allow_cd: 是否允许cd命令,控制目录切换权限
+            confirm_dangerous: 是否在执行高风险命令时提示用户确认
+            default_shell_mode: 默认是否启用shell模式(支持管道、重定向等)
+        """
+        super().__init__(
+            name="terminal",
+            description="命令行工具 - 执行安全的文件系统、文本处理和代码执行命令(ls, cat, grep, head, tail等)"
+        )
+        
+        # 将工作目录转换为绝对路径并规范化
+        self.workspace = Path(workspace).resolve()
+        self.timeout = timeout
+        self.max_output_size = max_output_size
+        self.allow_cd = allow_cd
+        self.confirm_dangerous = confirm_dangerous
+        self.default_shell_mode = default_shell_mode
+        
+        # 当前工作目录(相对于workspace)
+        # 初始设置为工作目录根目录,可通过cd命令更改
+        self.current_dir = self.workspace
+        
+        # 确保工作目录存在,如果不存在则创建
+        self.workspace.mkdir(parents=True, exist_ok=True)
+    
+    def run(self, parameters: Dict[str, Any]) -> str:
+        """执行工具的主入口方法
+        
+        根据参数解析命令并执行,包含完整的安全检查流程:
+        1. 参数验证 - 确保输入参数格式正确且必要参数存在
+        2. 命令解析和分类 - 将命令字符串解析为可执行的参数列表
+        3. 安全策略检查 - 多层安全验证,包括白名单检查、危险操作检测等
+        4. 命令执行和结果处理 - 安全执行命令并格式化返回结果
+        
+        安全机制说明:
+        - 命令白名单:只执行预定义的安全命令,防止恶意命令执行
+        - 路径沙箱:所有文件操作限制在工作目录内,防止越权访问
+        - 危险操作确认:高风险命令需要用户明确确认才能执行
+        - 超时控制:防止长时间运行的命令消耗系统资源
+        - 输出限制:防止过大输出导致内存问题
+        
+        Args:
+            parameters: 包含command、allow_dangerous、shell_mode等参数的字典
+                - command: 要执行的命令字符串
+                - allow_dangerous: 是否允许执行危险操作
+                - shell_mode: 是否启用shell模式(支持管道、重定向等)
+            
+        Returns:
+            str: 命令执行结果或错误信息,包含详细的错误说明
+        """
+        # 第一步:参数验证 - 确保输入参数符合预期格式
+        if not self.validate_parameters(parameters):
+            return "❌ 参数验证失败"
+        
+        # 提取并清理命令参数
+        command = parameters.get("command", "").strip()
+        allow_dangerous = bool(parameters.get("allow_dangerous", False))
+        shell_mode = bool(parameters.get("shell_mode", self.default_shell_mode))
+        
+        # 基础安全检查:拒绝空命令,防止无意义的系统调用
+        if not command:
+            return "❌ 命令不能为空"
+
+        # 执行模式选择:根据shell_mode参数决定执行方式
+        # shell_mode=True: 支持管道、重定向等复杂shell语法,但需要更严格的安全检查
+        # shell_mode=False: 使用argv模式,更安全但不支持shell特性
+        if shell_mode:
+            return self._execute_shell(command, allow_dangerous=allow_dangerous)
+        
+        # 第二步:命令解析 - 使用shlex进行安全的命令分割,处理引号和转义
+        try:
+            parts = shlex.split(command)
+        except ValueError as e:
+            return f"❌ 命令解析失败: {e}"
+        
+        # 解析后再次验证,确保命令不为空
+        if not parts:
+            return "❌ 命令不能为空"
+        
+        base_command = parts[0]
+        
+        # 第三步:安全策略检查 - 命令白名单验证
+        # 这是第一道安全防线,确保只能执行预定义的安全命令
+        if base_command not in self.ALLOWED_COMMANDS:
+            return f"❌ 不允许的命令: {base_command}\n允许的命令: {', '.join(sorted(self.ALLOWED_COMMANDS))}"
+
+        # 特殊命令处理:git命令需要额外的子命令安全检查
+        if base_command == "git":
+            return self._handle_git(parts, allow_dangerous)
+
+        # 第四步:危险操作确认机制
+        # 当用户明确允许危险操作且启用了确认机制时,进行交互式确认
+        # 这为高风险操作提供了最后一道人工确认防线
+        if allow_dangerous and self.confirm_dangerous:
+            ans = input(f"\n⚠️ 高风险命令:{command}\n允许执行?(y/n)\nconfirm> ").strip().lower()
+            if ans not in {"y", "yes"}:
+                return "⛔️ 已取消执行(用户未确认)。"
+
+        # 特殊命令处理:cd命令需要单独处理以维护工作目录状态
+        if base_command == 'cd':
+            return self._handle_cd(parts)
+        
+        # 第五步:执行命令 - 通过所有安全检查后执行命令
+        return self._execute_argv(parts, allow_dangerous=allow_dangerous)
+    
+    def get_parameters(self) -> List[ToolParameter]:
+        """获取工具参数定义"""
+        return [
+            ToolParameter(
+                name="command",
+                type="string",
+                description=(
+                    f"要执行的命令(白名单: {', '.join(sorted(list(self.ALLOWED_COMMANDS)[:10]))}...)\n"
+                    "示例: 'ls -la', 'cat file.txt', 'grep pattern *.py', 'head -n 20 data.csv'"
+                ),
+                required=True
+            ),
+            ToolParameter(
+                name="allow_dangerous",
+                type="boolean",
+                description="是否允许高风险命令(默认false;仅在用户明确确认后才可设置为true)",
+                required=False
+            ),
+            ToolParameter(
+                name="shell_mode",
+                type="boolean",
+                description="是否允许 shell 语义(管道/重定向/多段命令等)。默认继承工具配置。",
+                required=False,
+            ),
+        ]
+
+    def _contains_shell_meta(self, command: str) -> bool:
+        """检查命令中是否包含shell元字符
+        
+        Args:
+            command: 要检查的命令字符串
+            
+        Returns:
+            bool: 如果包含元字符返回True,否则返回False
+        """
+        return any(tok in command for tok in self.SHELL_META_TOKENS)
+
+    # --- shell parsing helpers (ignore operators inside quotes) ---
+    def _split_shell_segments(self, command: str) -> List[str]:
+        """
+        按管道/逻辑与/逻辑或/分号操作符分割shell命令,忽略引号内的操作符。
+        返回分割后的段列表(不包含操作符)。
+        
+        这个方法用于分析复杂的shell命令,确保每个段都是安全的。
+        
+        Args:
+            command: 要分割的shell命令字符串
+            
+        Returns:
+            List[str]: 分割后的命令段列表
+        """
+        ops = ["||", "&&", "|", ";"]
+        segs: List[str] = []
+        buf: List[str] = []
+        i = 0
+        quote: Optional[str] = None
+        while i < len(command):
+            ch = command[i]
+            if ch in {"'", '"'}:
+                if quote is None:
+                    quote = ch
+                elif quote == ch:
+                    quote = None
+                buf.append(ch)
+                i += 1
+                continue
+            if ch == "\\":
+                buf.append(ch)
+                if i + 1 < len(command):
+                    buf.append(command[i + 1])
+                    i += 2
+                else:
+                    i += 1
+                continue
+            if quote is None:
+                matched = False
+                for op in ops:
+                    if command.startswith(op, i):
+                        seg = "".join(buf).strip()
+                        if seg:
+                            segs.append(seg)
+                        buf = []
+                        i += len(op)
+                        matched = True
+                        break
+                if matched:
+                    continue
+            buf.append(ch)
+            i += 1
+        seg = "".join(buf).strip()
+        if seg:
+            segs.append(seg)
+        return segs
+
+    def _has_unquoted(self, command: str, token: str) -> bool:
+        """检查token(如>或$()或|)是否出现在引号外
+        
+        这个方法用于检测可能存在安全风险的shell操作符,
+        确保它们不是在引号内(引号内是安全的字符串字面量)。
+        
+        Args:
+            command: 要检查的命令字符串
+            token: 要查找的token字符串
+            
+        Returns:
+            bool: 如果token出现在引号外返回True,否则返回False
+        """
+        q: Optional[str] = None
+        i = 0
+        while i < len(command):
+            ch = command[i]
+            if ch in {"'", '"'}:
+                if q is None:
+                    q = ch
+                elif q == ch:
+                    q = None
+                i += 1
+                continue
+            if ch == "\\":
+                i += 2
+                continue
+            if q is None and command.startswith(token, i):
+                return True
+            i += 1
+        return False
+
+    def _shell_requires_allow_dangerous(self, command: str) -> bool:
+        """检查shell命令是否需要危险操作权限
+        
+        此方法分析shell命令,判断其是否包含可能对系统造成风险的操作。
+        这是安全机制的重要组成部分,用于识别需要额外权限确认的命令。
+        
+        安全检查逻辑:
+        1. 检查文件写入操作(>、>>重定向),但排除到/dev/null的重定向
+        2. 检查命令替换(`command`或$(command))
+        3. 检查已知的危险基础命令(rm、chmod等)
+        4. 检查Git的危险子命令(如reset --hard)
+        
+        Args:
+            command: 要检查的shell命令字符串
+            
+        Returns:
+            bool: 如果命令需要危险操作权限则返回True,否则返回False
+            
+        Note:
+            - 此方法不会阻止命令执行,只是标识需要额外确认的命令
+            - 实际的权限检查在执行阶段进行
+            - 采用相对保守的策略,对/dev/null重定向等安全操作给予宽容
+        """
+        # 写盘/命令替换通常视为高风险,但常见的只读掩埋(如 2>/dev/null、|| echo)可放宽
+        # 宽容规则:仅当重定向目标不是 /dev/null 时才视为写盘;简单的 "|| echo ..." 视为安全。
+        if self._has_unquoted(command, ">") or self._has_unquoted(command, ">>"):
+            # 忽略 /dev/null 重定向(这是安全的丢弃输出操作)
+            if re.search(r">\s*/dev/null", command) or re.search(r">>\s*/dev/null", command):
+                pass
+            else:
+                # 其他重定向操作可能修改文件内容,需要危险权限
+                return True
+        
+        # 检查命令替换:`command`(反引号)和 $(command) 格式
+        # 命令替换可能执行任意代码,存在代码注入风险
+        if self._has_unquoted(command, "$(") or self._has_unquoted(command, "`"):
+            return True
+        
+        # 检查已知的危险基础命令
+        # 这些命令可能对系统造成不可逆的影响,需要特别关注
+        if re.search(r"(^|\s)rm(\s|$)", command):
+            return True
+        if re.search(r"(^|\s)chmod(\s|$)", command):
+            return True
+        
+        # 检查Git的危险子命令
+        # git reset --hard 可能导致代码丢失,属于高风险操作
+        if re.search(r"git\s+reset\s+--hard", command):
+            return True
+        
+        # 如果没有检测到危险操作,则不需要额外权限
+        return False
+
+    def _shell_all_commands_whitelisted(self, command: str) -> bool:
+        """
+        静态检查shell命令中的所有段是否都在白名单中(尽力而为的检查)
+        
+        此方法通过分割shell命令为多个段,然后检查每个段的第一个命令是否在白名单中。
+        这是对shell命令的安全预检查,用于在未允许危险操作时确保命令的安全性。
+        
+        安全检查策略:
+        1. 使用shell元字符分割命令为多个独立段
+        2. 对每个段进行命令解析,提取基础命令
+        3. 检查基础命令是否在白名单中
+        4. 对git命令进行特殊处理,只允许安全的只读子命令
+        
+        Args:
+            command: 要检查的完整shell命令字符串
+            
+        Returns:
+            bool: 如果所有命令段都在白名单中则返回True,否则返回False
+            
+        Note:
+            - 这是一个尽力而为的检查,不能保证100%准确
+            - 对于复杂的shell语法,可能存在误判
+            - git命令只允许status和diff子命令,其他子命令被视为危险操作
+        """
+        # 预处理:将换行符替换为空格,便于统一处理
+        cmd = command.replace("\n", " ")
+        
+        # 分割命令为多个段,每个段包含一个独立的命令
+        segments = self._split_shell_segments(cmd)
+        
+        # 对每个命令段进行安全检查
+        for seg in segments:
+            seg = seg.strip()
+            if not seg:
+                continue  # 跳过空段
+                
+            try:
+                # 使用shlex进行安全的命令分割,处理引号和转义
+                argv = shlex.split(seg)
+            except Exception:
+                # 如果解析失败,认为不安全
+                return False
+                
+            if not argv:
+                continue  # 跳过空命令段
+                
+            # 获取基础命令(命令名的第一部分)
+            base = argv[0]
+            
+            # 检查基础命令是否在白名单中
+            if base not in self.ALLOWED_COMMANDS:
+                return False
+                
+            # 对git命令进行特殊处理
+            # 只允许安全的只读子命令,其他子命令被视为危险操作
+            if base == "git":
+                if len(argv) < 2:
+                    return False  # git命令必须包含子命令
+                if argv[1] not in {"status", "diff"}:
+                    return False  # 只允许status和diff子命令
+                    
+        # 所有命令段都通过了安全检查
+        return True
+
+    def _execute_shell(self, command: str, allow_dangerous: bool = False) -> str:
+        """
+        执行shell命令字符串(支持Claude Code风格的shell特性)
+        
+        此方法提供安全的shell命令执行能力,支持管道、重定向、命令替换等复杂shell语法。
+        通过多层安全检查机制确保命令执行的安全性。
+        
+        安全防护措施:
+        - 如果shell命令包含重定向/命令替换/已知危险操作 -> 需要allow_dangerous权限
+        - 如果未允许危险操作 -> 要求所有命令段都在白名单中(尽力而为的检查)
+        - confirm_dangerous可以提示用户确认包含shell元字符或需要allow_dangerous的命令
+        
+        执行流程:
+        1. 检查命令是否需要危险权限
+        2. 验证命令白名单(如果未允许危险操作)
+        3. 用户确认(如果启用confirm_dangerous)
+        4. 执行命令并处理输出
+        5. 返回执行结果或错误信息
+        
+        Args:
+            command: 要执行的完整shell命令字符串
+            allow_dangerous: 是否允许执行危险操作(默认False)
+            
+        Returns:
+            str: 命令执行结果或错误信息
+            
+        Note:
+            - 支持管道操作而无需确认(如 'ls | grep .py')
+            - 只有在可能写入文件/转义/执行危险操作时才需要确认
+            - 输出会被截断以防止内存问题
+        """
+        needs_allow = self._shell_requires_allow_dangerous(command)
+        
+        # 第一层安全检查:危险操作检测
+        # 如果命令包含危险操作但未允许危险操作,则拒绝执行
+        # 这是第一道安全防线,防止潜在的恶意操作
+        if needs_allow and not allow_dangerous:
+            return "❌ 该命令包含写盘/子命令替换/高风险操作,需用户确认后再执行(allow_dangerous=true)"
+
+        # 第二层安全检查:白名单验证
+        # 如果未允许危险操作,则检查所有命令段是否都在白名单中
+        # 这是对命令的进一步安全验证,确保只能执行预定义的安全命令
+        if not allow_dangerous and not self._shell_all_commands_whitelisted(command):
+            return "❌ shell_mode 下检测到非白名单命令/不允许的 git 子命令。需要用户确认后再执行(allow_dangerous=true)"
+
+        # Claude Code-like: pipes are allowed without confirmation; only confirm when it may write/escape/execute dangerous ops.
+        if self.confirm_dangerous and (allow_dangerous or needs_allow):
+            ans = input(f"\n⚠️ 即将执行高风险 shell 命令:{command}\n允许执行?(y/n)\nconfirm> ").strip().lower()
+            if ans not in {"y", "yes"}:
+                return "⛔️ 已取消执行(用户未确认)。"
+
+        try:
+            result = subprocess.run(
+                command,
+                shell=True,
+                cwd=str(self.current_dir),
+                capture_output=True,
+                text=True,
+                timeout=self.timeout,
+                env=os.environ.copy(),
+            )
+
+            output = (result.stdout or "") + (result.stderr or "")
+            if len(output.encode("utf-8", errors="ignore")) > self.max_output_size:
+                output = output[: self.max_output_size] + "\n...output truncated...\n"
+
+            if result.returncode != 0:
+                return f"命令执行失败 (返回码 {result.returncode}):\n{output}"
+            return output.strip() if output.strip() else "(no output)"
+        except subprocess.TimeoutExpired:
+            return f"❌ 命令超时(>{self.timeout}s)"
+        except Exception as e:
+            return f"❌ 命令执行异常: {e}"
+    
+    def _handle_cd(self, parts: List[str]) -> str:
+        """处理cd命令,实现安全的目录切换
+        
+        此方法专门处理cd命令,确保目录切换在允许的工作空间范围内进行。
+        支持绝对路径、相对路径和特殊路径(如..、~)的处理。
+        
+        Args:
+            parts: cd命令的参数列表,parts[1]是目标路径
+            
+        Returns:
+            str: 执行结果或错误信息
+            
+        Note:
+            - 只允许切换到工作空间内的目录
+            - 支持路径规范化,处理..和.等相对路径
+            - 不允许切换到工作空间之外的目录
+        """
+        if not self.allow_cd:
+            return "❌ cd 命令已禁用"
+        
+        if len(parts) < 2:
+            # cd命令没有参数时,返回当前目录信息
+            return f"当前目录: {self.current_dir}"
+        
+        target_dir = parts[1]
+        
+        # 处理特殊的相对路径
+        if target_dir == "..":
+            # 切换到父目录
+            new_dir = self.current_dir.parent
+        elif target_dir == ".":
+            # 当前目录
+            new_dir = self.current_dir
+        elif target_dir == "~":
+            # 切换到工作目录根目录
+            new_dir = self.workspace
+        else:
+            # 解析相对或绝对路径
+            new_dir = (self.current_dir / target_dir).resolve()
+        
+        # 检查新目录是否在工作空间范围内
+        try:
+            new_dir.relative_to(self.workspace)
+        except ValueError:
+            return f"❌ 不允许访问工作目录外的路径: {new_dir}"
+        
+        # 验证目录存在性
+        if not new_dir.exists():
+            return f"❌ 目录不存在: {new_dir}"
+        
+        # 确保目标是目录而非文件
+        if not new_dir.is_dir():
+            return f"❌ 不是目录: {new_dir}"
+        
+        # 更新当前工作目录
+        self.current_dir = new_dir
+        return f"✅ 切换到目录: {self.current_dir}"
+    
+    def _execute_argv(self, argv: List[str], allow_dangerous: bool = False) -> str:
+        """执行参数向量形式的命令(不使用shell解释)
+        
+        此方法直接执行命令及其参数,不通过shell解释,因此不支持管道、重定向等shell特性。
+        这种方式更安全,因为避免了shell注入攻击的风险,但功能相对有限。
+        
+        安全检查流程:
+        1. 检查高风险命令(rm、chmod等)是否需要用户确认
+        2. 对允许的高风险命令进行路径沙箱限制
+        3. 对mkdir命令进行路径沙箱限制
+        4. 执行命令并处理结果
+        
+        Args:
+            argv: 命令及其参数的列表,argv[0]是命令名,其余是参数
+            allow_dangerous: 是否允许执行高风险命令(默认False)
+            
+        Returns:
+            str: 命令执行结果或错误信息
+            
+        Note:
+            - 不支持shell特性如管道、重定向、变量替换等
+            - 更安全,避免了shell注入攻击
+            - 适用于简单的命令执行场景
+            - 所有路径操作都被限制在工作空间内
+        """
+        # 第一层安全检查:高风险命令二次门禁
+        # 对明确高风险基命令(rm/chmod等)进行额外检查
+        # 这些命令可能对系统造成不可逆的修改,需要用户明确确认
+        # 这是安全防护的第一道防线,防止意外的系统修改
+        if argv and argv[0] in self.DANGEROUS_BASE_COMMANDS and not allow_dangerous:
+            return f"❌ 高风险命令 {argv[0]} 需要人类确认(allow_dangerous=true)"
+
+        # 第二层安全检查:路径沙箱限制
+        # 对带路径参数的高风险命令做路径沙箱限制(仅当放行时)
+        # 确保即使允许执行危险命令,也只能在工作空间内操作
+        # 这防止了恶意用户通过相对路径或符号链接逃逸沙箱
+        if argv and argv[0] in {"rm", "chmod"} and allow_dangerous:
+            # 保守检查:所有看起来像路径的非标志参数都必须在工作空间内
+            # 使用current_dir作为基准,确保相对路径也被正确限制
+            for a in argv[1:]:
+                if a.startswith("-"):
+                    continue  # 跳过选项参数(如 -r, -f 等)
+                candidate = (self.current_dir / a).resolve()
+                try:
+                    # 检查解析后的绝对路径是否在工作空间内
+                    candidate.relative_to(self.workspace)
+                except ValueError:
+                    # 路径超出工作空间范围,拒绝执行
+                    return f"❌ 拒绝在工作目录外操作: {a}"
+
+        # 第三层安全检查:mkdir命令路径沙箱
+        # mkdir虽然相对安全,但仍需确保不会在工作空间外创建目录
+        # 这防止了通过mkdir命令在工作空间外创建后门或敏感目录
+        if argv and argv[0] == "mkdir":
+            for a in argv[1:]:
+                if a.startswith("-"):
+                    continue  # 跳过选项参数(如 -p, -m 等)
+                candidate = (self.current_dir / a).resolve()
+                try:
+                    # 检查要创建的目录是否在工作空间内
+                    candidate.relative_to(self.workspace)
+                except ValueError:
+                    # 尝试在工作空间外创建目录,拒绝执行
+                    return f"❌ 不允许在工作目录外创建目录: {a}"
+
+        try:
+            # 直接执行命令,不通过shell
+            # 使用shell=False确保命令不被shell解释,避免注入攻击
+            # 使用capture_output=True捕获标准输出和标准错误
+            # 使用text=True确保输出为文本格式
+            result = subprocess.run(
+                argv,
+                shell=False,
+                cwd=str(self.current_dir),
+                capture_output=True,
+                text=True,
+                timeout=self.timeout,
+                env=os.environ.copy(),
+            )
+            
+            # 合并标准输出和标准错误
+            # 这样用户可以看到完整的执行结果
+            output = result.stdout
+            if result.stderr:
+                output += f"\n[stderr]\n{result.stderr}"
+            
+            # 检查输出大小,防止过大输出消耗内存
+            if len(output) > self.max_output_size:
+                output = output[:self.max_output_size]
+                output += f"\n\n⚠️ 输出被截断(超过 {self.max_output_size} 字节)"
+            
+            # 添加返回码信息,帮助用户了解命令执行状态
+            if result.returncode != 0:
+                output = f"⚠️ 命令返回码: {result.returncode}\n\n{output}"
+            
+            return output if output else "✅ 命令执行成功(无输出)"
+            
+        except subprocess.TimeoutExpired:
+            # 命令执行超时,可能是死循环或处理大量数据
+            return f"❌ 命令执行超时(超过 {self.timeout} 秒)"
+        except Exception as e:
+            # 捕获其他异常,如权限错误、文件不存在等
+            return f"❌ 命令执行失败: {e}"
+    
+    def _truncate_output(self, output: str) -> str:
+        """截断过大的输出,防止内存问题
+        
+        此方法检查输出字符串的长度,如果超过配置的最大输出大小,
+        则截断输出并添加提示信息,防止过大的输出消耗过多内存。
+        
+        Args:
+            output: 需要检查的输出字符串
+            
+        Returns:
+            str: 原始输出或截断后的输出(如果超过大小限制)
+            
+        Note:
+            - 截断时会保留开头的内容,丢弃超出限制的部分
+            - 会在截断的输出末尾添加提示信息,说明输出已被截断
+        """
+        if len(output) > self.max_output_size:
+            return output[: self.max_output_size] + f"\n[输出被截断,超过 {self.max_output_size} 字节限制]"
+        return output
+    
+    def get_current_dir(self) -> str:
+        """获取当前工作目录"""
+        return str(self.current_dir)
+    
+    def reset_dir(self):
+        """重置到工作目录根"""
+        self.current_dir = self.workspace
+

+ 213 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/builtin/todo_tool.py

@@ -0,0 +1,213 @@
+"""TodoTool - 轻量级待办板
+
+MVP 目标:
+- 仅支持 add / list / update
+- 状态枚举:pending | in_progress | completed(强约束:同时最多 1 个 in_progress)
+- 存储:.helloagents/todos/todos.json,原子写入 + 简单备份
+- 输出:按状态分组的要点列表,便于 LLM 消化
+"""
+
+from __future__ import annotations
+
+import json
+from dataclasses import dataclass, asdict
+from datetime import datetime
+from pathlib import Path
+from typing import Any, Dict, List, Optional
+
+from ..base import Tool, ToolParameter
+
+
+STATUSES = ("pending", "in_progress", "completed")
+
+
+@dataclass
+class TodoItem:
+    id: int
+    title: str
+    desc: str = ""
+    status: str = "pending"
+    created_at: str = ""
+    updated_at: str = ""
+
+    @classmethod
+    def from_dict(cls, data: Dict[str, Any]) -> "TodoItem":
+        return cls(
+            id=int(data["id"]),
+            title=data.get("title", ""),
+            desc=data.get("desc", ""),
+            status=data.get("status", "pending"),
+            created_at=data.get("created_at", ""),
+            updated_at=data.get("updated_at", ""),
+        )
+
+
+class TodoTool(Tool):
+    def __init__(self, workspace: str):
+        super().__init__(
+            name="todo",
+            description="待办工具:add/list/update;状态 pending|in_progress|completed(同时仅允许1个in_progress)",
+        )
+        self.workspace = Path(workspace)
+        self.workspace.mkdir(parents=True, exist_ok=True)
+        self.data_file = self.workspace / "todos.json"
+        self.backup_file = self.workspace / "todos.json.bak"
+        if not self.data_file.exists():
+            self._save({"items": []})
+
+    def get_parameters(self) -> List[ToolParameter]:
+        return [
+            ToolParameter(name="action", type="string", description="add | list | update", required=True),
+            ToolParameter(name="title", type="string", description="待办标题(add必填,update可选)", required=False),
+            ToolParameter(name="desc", type="string", description="待办描述(可选)", required=False),
+            ToolParameter(name="status", type="string", description="pending|in_progress|completed(update可选)", required=False),
+            ToolParameter(name="id", type="integer", description="要更新的待办ID(update必填)", required=False),
+        ]
+
+    # ---------------- core ops ----------------
+    def run(self, parameters: Dict[str, Any]) -> str:
+        if not self.validate_parameters(parameters):
+            return "参数缺失,需包含 action(add/list/update)。"
+        action = str(parameters.get("action", "")).strip().lower().rstrip("]")
+        if action == "add":
+            return self._add(title=parameters.get("title", ""), desc=parameters.get("desc", ""), status=parameters.get("status", "pending"))
+        if action == "list":
+            return self._list(status_filter=parameters.get("status"))
+        if action == "update":
+            return self._update(
+                todo_id=parameters.get("id"),
+                title=parameters.get("title"),
+                desc=parameters.get("desc"),
+                status=parameters.get("status"),
+            )
+        return "不支持的 action,应为 add/list/update。"
+
+    # ---------------- storage ----------------
+    def _load(self) -> Dict[str, Any]:
+        with open(self.data_file, "r", encoding="utf-8") as f:
+            return json.load(f)
+
+    def _save(self, data: Dict[str, Any]) -> None:
+        tmp = self.data_file.with_suffix(".tmp")
+        if self.data_file.exists():
+            try:
+                self.backup_file.write_text(self.data_file.read_text(encoding="utf-8"), encoding="utf-8")
+            except Exception:
+                pass
+        tmp.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
+        tmp.replace(self.data_file)
+
+    # ---------------- helpers ----------------
+    def _next_id(self, items: List[TodoItem]) -> int:
+        return (max([i.id for i in items], default=0) + 1) if items else 1
+
+    def _now(self) -> str:
+        return datetime.now().isoformat(timespec="seconds")
+
+    def _enforce_single_in_progress(self, items: List[TodoItem], incoming_status: str, incoming_id: Optional[int]) -> Optional[str]:
+        if incoming_status != "in_progress":
+            return None
+        for it in items:
+            if it.status == "in_progress" and (incoming_id is None or it.id != incoming_id):
+                return f"已有进行中的任务 #{it.id}《{it.title}》。先完成/更新它后再切换。"
+        return None
+
+    # ---------------- actions ----------------
+    def _add(self, title: str, desc: str, status: str) -> str:
+        title = (title or "").strip()
+        if not title:
+            return "❌ add 失败:title 不能为空。"
+        status = status if status in STATUSES else "pending"
+        data = self._load()
+        items = [TodoItem.from_dict(i) for i in data.get("items", [])]
+        conflict = self._enforce_single_in_progress(items, status, None)
+        if conflict:
+            return f"❌ add 失败:{conflict}"
+        now = self._now()
+        new_item = TodoItem(id=self._next_id(items), title=title, desc=desc or "", status=status, created_at=now, updated_at=now)
+        items.append(new_item)
+        self._save({"items": [asdict(i) for i in items]})
+        return f"✅ 已添加 #{new_item.id} [{new_item.status}] {new_item.title}"
+
+    def _update(self, todo_id: Any, title: Optional[str], desc: Optional[str], status: Optional[str]) -> str:
+        try:
+            tid = int(todo_id)
+        except Exception:
+            return "❌ update 失败:缺少有效的 id。"
+        if status and status not in STATUSES:
+            return "❌ update 失败:status 必须是 pending|in_progress|completed。"
+
+        data = self._load()
+        items = [TodoItem.from_dict(i) for i in data.get("items", [])]
+        target = next((i for i in items if i.id == tid), None)
+        if not target:
+            return f"❌ update 失败:未找到 id={tid} 的任务。"
+
+        conflict = self._enforce_single_in_progress(items, status or target.status, tid)
+        if conflict:
+            return f"❌ update 失败:{conflict}"
+
+        changed = []
+        if title is not None:
+            target.title = title.strip()
+            changed.append("title")
+        if desc is not None:
+            target.desc = desc
+            changed.append("desc")
+        if status is not None:
+            target.status = status
+            changed.append("status")
+        target.updated_at = self._now()
+
+        self._save({"items": [asdict(i) for i in items]})
+        if not changed:
+            return f"⚠️ 未修改任何字段 #{tid}"
+        return f"✅ 已更新 #{tid} ({', '.join(changed)}) -> [{target.status}] {target.title}"
+
+    def _list(self, status_filter: Optional[str]) -> str:
+        data = self._load()
+        items = [TodoItem.from_dict(i) for i in data.get("items", [])]
+        if status_filter and status_filter in STATUSES:
+            items = [i for i in items if i.status == status_filter]
+
+        groups = {"in_progress": [], "pending": [], "completed": []}
+        for it in items:
+            groups.setdefault(it.status, []).append(it)
+
+        # ANSI colors similar to v2_todo_agent
+        COLOR_PENDING = "\x1b[38;2;176;176;176m"
+        COLOR_PROGRESS = "\x1b[38;2;120;200;255m"
+        COLOR_DONE = "\x1b[38;2;34;139;34m"
+        RESET = "\x1b[0m"
+
+        def fmt(group_name: str, arr: List[TodoItem]) -> str:
+            if not arr:
+                return ""
+            lines = [f"[{group_name.upper()}]"]
+            for it in sorted(arr, key=lambda x: x.id):
+                mark = "☒" if it.status == "completed" else "☐"
+                color = COLOR_PENDING
+                if it.status == "in_progress":
+                    color = COLOR_PROGRESS
+                elif it.status == "completed":
+                    color = COLOR_DONE
+                line_main = f"- {mark} #{it.id} {it.title} (updated {it.updated_at})"
+                line_desc = f"    {it.desc}" if it.desc else None
+
+                if it.status == "completed":
+                    line_main = f"{color}\x1b[9m{line_main}{RESET}"
+                    if line_desc:
+                        line_desc = f"{color}\x1b[9m{line_desc}{RESET}"
+                else:
+                    line_main = f"{color}{line_main}{RESET}"
+                    if line_desc:
+                        line_desc = f"{color}{line_desc}{RESET}"
+
+                lines.append(line_main)
+                if line_desc:
+                    lines.append(line_desc)
+            return "\n".join(lines)
+
+        parts = [fmt("in_progress", groups["in_progress"]), fmt("pending", groups["pending"]), fmt("completed", groups["completed"])]
+        out = "\n\n".join([p for p in parts if p])
+        return out or "暂无待办。"

+ 166 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/chain.py

@@ -0,0 +1,166 @@
+"""工具链管理器 - HelloAgents工具链式调用支持"""
+
+from typing import List, Dict, Any, Optional
+from .registry import ToolRegistry
+
+
+class ToolChain:
+    """工具链 - 支持多个工具的顺序执行"""
+
+    def __init__(self, name: str, description: str):
+        self.name = name
+        self.description = description
+        self.steps: List[Dict[str, Any]] = []
+
+    def add_step(self, tool_name: str, input_template: str, output_key: str = None):
+        """
+        添加工具执行步骤
+        
+        Args:
+            tool_name: 工具名称
+            input_template: 输入模板,支持变量替换,如 "{input}" 或 "{search_result}"
+            output_key: 输出结果的键名,用于后续步骤引用
+        """
+        step = {
+            "tool_name": tool_name,
+            "input_template": input_template,
+            "output_key": output_key or f"step_{len(self.steps)}_result"
+        }
+        self.steps.append(step)
+        print(f"✅ 工具链 '{self.name}' 添加步骤: {tool_name}")
+
+    def execute(self, registry: ToolRegistry, input_data: str, context: Dict[str, Any] = None) -> str:
+        """
+        执行工具链
+        
+        Args:
+            registry: 工具注册表
+            input_data: 初始输入数据
+            context: 执行上下文,用于变量替换
+            
+        Returns:
+            最终执行结果
+        """
+        if not self.steps:
+            return "❌ 工具链为空,无法执行"
+
+        print(f"🚀 开始执行工具链: {self.name}")
+        
+        # 初始化上下文
+        if context is None:
+            context = {}
+        context["input"] = input_data
+        
+        final_result = input_data
+        
+        for i, step in enumerate(self.steps):
+            tool_name = step["tool_name"]
+            input_template = step["input_template"]
+            output_key = step["output_key"]
+            
+            print(f"📝 执行步骤 {i+1}/{len(self.steps)}: {tool_name}")
+            
+            # 替换模板中的变量
+            try:
+                actual_input = input_template.format(**context)
+            except KeyError as e:
+                return f"❌ 模板变量替换失败: {e}"
+            
+            # 执行工具
+            try:
+                result = registry.execute_tool(tool_name, actual_input)
+                context[output_key] = result
+                final_result = result
+                print(f"✅ 步骤 {i+1} 完成")
+            except Exception as e:
+                return f"❌ 工具 '{tool_name}' 执行失败: {e}"
+        
+        print(f"🎉 工具链 '{self.name}' 执行完成")
+        return final_result
+
+
+class ToolChainManager:
+    """工具链管理器"""
+
+    def __init__(self, registry: ToolRegistry):
+        self.registry = registry
+        self.chains: Dict[str, ToolChain] = {}
+
+    def register_chain(self, chain: ToolChain):
+        """注册工具链"""
+        self.chains[chain.name] = chain
+        print(f"✅ 工具链 '{chain.name}' 已注册")
+
+    def execute_chain(self, chain_name: str, input_data: str, context: Dict[str, Any] = None) -> str:
+        """执行指定的工具链"""
+        if chain_name not in self.chains:
+            return f"❌ 工具链 '{chain_name}' 不存在"
+
+        chain = self.chains[chain_name]
+        return chain.execute(self.registry, input_data, context)
+
+    def list_chains(self) -> List[str]:
+        """列出所有已注册的工具链"""
+        return list(self.chains.keys())
+
+    def get_chain_info(self, chain_name: str) -> Optional[Dict[str, Any]]:
+        """获取工具链信息"""
+        if chain_name not in self.chains:
+            return None
+        
+        chain = self.chains[chain_name]
+        return {
+            "name": chain.name,
+            "description": chain.description,
+            "steps": len(chain.steps),
+            "step_details": [
+                {
+                    "tool_name": step["tool_name"],
+                    "input_template": step["input_template"],
+                    "output_key": step["output_key"]
+                }
+                for step in chain.steps
+            ]
+        }
+
+
+# 便捷函数
+def create_research_chain() -> ToolChain:
+    """创建一个研究工具链:搜索 -> 计算 -> 总结"""
+    chain = ToolChain(
+        name="research_and_calculate",
+        description="搜索信息并进行相关计算"
+    )
+
+    # 步骤1:搜索信息
+    chain.add_step(
+        tool_name="search",
+        input_template="{input}",
+        output_key="search_result"
+    )
+
+    # 步骤2:基于搜索结果进行计算
+    chain.add_step(
+        tool_name="my_calculator",
+        input_template="2 + 2",  # 简单的计算示例
+        output_key="calc_result"
+    )
+
+    return chain
+
+
+def create_simple_chain() -> ToolChain:
+    """创建一个简单的工具链示例"""
+    chain = ToolChain(
+        name="simple_demo",
+        description="简单的工具链演示"
+    )
+
+    # 只包含一个计算步骤
+    chain.add_step(
+        tool_name="my_calculator",
+        input_template="{input}",
+        output_key="result"
+    )
+
+    return chain

+ 220 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/tools/registry.py

@@ -0,0 +1,220 @@
+"""工具注册表 - HelloAgents原生工具系统"""
+
+from typing import Optional, Any, Callable
+import json
+from .base import Tool
+
+class ToolRegistry:
+    """
+    HelloAgents工具注册表
+
+    提供工具的注册、管理和执行功能。
+    支持两种工具注册方式:
+    1. Tool对象注册(推荐)
+    2. 函数直接注册(简便)
+    """
+
+    def __init__(self):
+        self._tools: dict[str, Tool] = {}
+        self._functions: dict[str, dict[str, Any]] = {}
+
+    def register_tool(self, tool: Tool):
+        """
+        注册Tool对象
+
+        Args:
+            tool: Tool实例
+        """
+        if tool.name in self._tools:
+            print(f"⚠️ 警告:工具 '{tool.name}' 已存在,将被覆盖。")
+
+        self._tools[tool.name] = tool
+        print(f"✅ 工具 '{tool.name}' 已注册。")
+
+    def register_function(self, name: str, description: str, func: Callable[[str], str]):
+        """
+        直接注册函数作为工具(简便方式)
+
+        Args:
+            name: 工具名称
+            description: 工具描述
+            func: 工具函数,接受字符串参数,返回字符串结果
+        """
+        if name in self._functions:
+            print(f"⚠️ 警告:工具 '{name}' 已存在,将被覆盖。")
+
+        self._functions[name] = {
+            "description": description,
+            "func": func
+        }
+        print(f"✅ 工具 '{name}' 已注册。")
+
+    def unregister(self, name: str):
+        """注销工具"""
+        if name in self._tools:
+            del self._tools[name]
+            print(f"🗑️ 工具 '{name}' 已注销。")
+        elif name in self._functions:
+            del self._functions[name]
+            print(f"🗑️ 工具 '{name}' 已注销。")
+        else:
+            print(f"⚠️ 工具 '{name}' 不存在。")
+
+    def get_tool(self, name: str) -> Optional[Tool]:
+        """获取Tool对象"""
+        return self._tools.get(name)
+
+    def get_function(self, name: str) -> Optional[Callable]:
+        """获取工具函数"""
+        func_info = self._functions.get(name)
+        return func_info["func"] if func_info else None
+
+    def execute_tool(self, name: str, input_text: str) -> str:
+        """
+        执行工具
+
+        Args:
+            name: 工具名称
+            input_text: 输入参数
+
+        Returns:
+            工具执行结果
+        """
+        # 优先查找Tool对象
+        if name in self._tools:
+            tool = self._tools[name]
+            try:
+                raw = (input_text or "").strip()
+                
+                # 预处理:如果输入包含换行和另一个 Action,只取第一行
+                if '\n' in raw and 'Action:' in raw:
+                    lines = raw.split('\n')
+                    raw = lines[0].strip()
+
+                # 1) JSON 直通:允许 ReAct 里用 tool[{"k":"v"}] 精确传参
+                def _try_json(txt: str):
+                    try:
+                        return json.loads(txt)
+                    except Exception:
+                        return None
+
+                obj = None
+                # 1a 单个对象
+                if raw.startswith("{") and raw.endswith("}"):
+                    obj = _try_json(raw)
+                # 1b 常见模型输出尾部多了一个 ']' 的容错
+                if obj is None and raw.startswith("{") and raw.endswith("}]"):
+                    obj = _try_json(raw[:-1].strip())
+                # 1c 模型输出为数组包裹一个对象
+                if obj is None and raw.startswith("[") and raw.endswith("]"):
+                    arr = _try_json(raw)
+                    if isinstance(arr, list) and len(arr) == 1 and isinstance(arr[0], dict):
+                        obj = arr[0]
+                # 1d 错位尾括号(常见:{"a":1,"b":2}])
+                if obj is None and raw.endswith("}]") and raw.count("{") == 1 and raw.count("}") == 2:
+                    obj = _try_json(raw[:-1])
+                # 1e 正则兜底:提取首个完整 JSON 对象
+                if obj is None and "{" in raw and "}" in raw:
+                    try:
+                        import re
+                        # 使用括号匹配而非简单正则
+                        def extract_first_json_object(text: str):
+                            """从文本中提取第一个完整的 JSON 对象"""
+                            start = text.find('{')
+                            if start == -1:
+                                return None
+                            depth = 0
+                            in_string = False
+                            escape = False
+                            for i, c in enumerate(text[start:], start):
+                                if escape:
+                                    escape = False
+                                    continue
+                                if c == '\\' and in_string:
+                                    escape = True
+                                    continue
+                                if c == '"' and not escape:
+                                    in_string = not in_string
+                                    continue
+                                if in_string:
+                                    continue
+                                if c == '{':
+                                    depth += 1
+                                elif c == '}':
+                                    depth -= 1
+                                    if depth == 0:
+                                        return text[start:i+1]
+                            return None
+                        
+                        json_str = extract_first_json_object(raw)
+                        if json_str:
+                            obj = json.loads(json_str)
+                    except Exception:
+                        pass
+
+                if isinstance(obj, dict):
+                    return tool.run(obj)
+
+                # 2) 单参数兜底:如果工具只有一个必填参数,把 input_text 映射到该参数名
+                params = tool.get_parameters()
+                required = [p for p in params if p.required]
+                if len(required) == 1:
+                    return tool.run({required[0].name: input_text})
+
+                # 3) 兼容旧行为:若存在 input 参数,使用 input
+                if any(p.name == "input" for p in params):
+                    return tool.run({"input": input_text})
+
+                return (
+                    f"错误:工具 '{name}' 需要结构化参数。"
+                    "请使用 JSON 形式传参,例如:tool[{\"param\":\"value\"}]"
+                )
+            except Exception as e:
+                return f"错误:执行工具 '{name}' 时发生异常: {str(e)}"
+
+        # 查找函数工具
+        elif name in self._functions:
+            func = self._functions[name]["func"]
+            try:
+                return func(input_text)
+            except Exception as e:
+                return f"错误:执行工具 '{name}' 时发生异常: {str(e)}"
+
+        else:
+            return f"错误:未找到名为 '{name}' 的工具。"
+
+    def get_tools_description(self) -> str:
+        """
+        获取所有可用工具的格式化描述字符串
+
+        Returns:
+            工具描述字符串,用于构建提示词
+        """
+        descriptions = []
+
+        # Tool对象描述
+        for tool in self._tools.values():
+            descriptions.append(f"- {tool.name}: {tool.description}")
+
+        # 函数工具描述
+        for name, info in self._functions.items():
+            descriptions.append(f"- {name}: {info['description']}")
+
+        return "\n".join(descriptions) if descriptions else "暂无可用工具"
+
+    def list_tools(self) -> list[str]:
+        """列出所有工具名称"""
+        return list(self._tools.keys()) + list(self._functions.keys())
+
+    def get_all_tools(self) -> list[Tool]:
+        """获取所有Tool对象"""
+        return list(self._tools.values())
+
+    def clear(self):
+        """清空所有工具"""
+        self._tools.clear()
+        self._functions.clear()
+        print("🧹 所有工具已清空。")
+
+# 全局工具注册表
+global_registry = ToolRegistry()

+ 11 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/__init__.py

@@ -0,0 +1,11 @@
+"""通用工具模块"""
+
+from .logging import setup_logger, get_logger
+from .serialization import serialize_object, deserialize_object
+from .helpers import format_time, validate_config, safe_import
+
+__all__ = [
+    "setup_logger", "get_logger",
+    "serialize_object", "deserialize_object", 
+    "format_time", "validate_config", "safe_import"
+]

+ 86 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/cli_ui.py

@@ -0,0 +1,86 @@
+"""CLI UI helpers (ANSI colors, spinner, consistent formatting).
+
+Kept dependency-free to avoid pulling extra packages into the MVP.
+"""
+
+from __future__ import annotations
+
+import os
+import sys
+import threading
+import time
+from typing import Iterable
+
+
+RESET = "\x1b[0m"
+PRIMARY = "\x1b[38;2;120;200;255m"
+ACCENT = "\x1b[38;2;150;140;255m"
+INFO = "\x1b[38;2;120;120;120m"
+WARN = "\x1b[38;2;255;190;120m"
+ERROR = "\x1b[38;2;255;120;120m"
+
+
+def supports_ansi() -> bool:
+    if os.getenv("NO_COLOR"):
+        return False
+    return sys.stdout.isatty()
+
+
+def c(text: str, color: str) -> str:
+    if not supports_ansi():
+        return text
+    return f"{color}{text}{RESET}"
+
+
+def hr(char: str = "=", width: int = 80) -> str:
+    return char * width
+
+
+def clamp_text(text: str, limit: int = 40_000) -> str:
+    if text is None:
+        return ""
+    if len(text) <= limit:
+        return text
+    return text[:limit] + f"\n\n...<truncated {len(text) - limit} chars>"
+
+
+def log_tool_event(name: str, message: str) -> None:
+    """LangChain-demo-like tool logging."""
+    header = f"⏺ {name}"
+    print(c(header, ACCENT))
+    lines: Iterable[str] = message.splitlines() if message else [""]
+    for line in lines:
+        print(f"  ⎿ {line}")
+
+
+class Spinner:
+    """Simple terminal spinner."""
+
+    def __init__(self, label: str = "Thinking…") -> None:
+        self.label = label
+        self.frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
+        self._index = 0
+        self._running = False
+        self._thread: threading.Thread | None = None
+
+    def start(self) -> None:
+        if not supports_ansi() or self._running:
+            return
+        self._running = True
+
+        def _run() -> None:
+            while self._running:
+                frame = self.frames[self._index % len(self.frames)]
+                self._index += 1
+                print(c(f"{frame} {self.label}", INFO), end="\r", flush=True)
+                time.sleep(0.08)
+
+        self._thread = threading.Thread(target=_run, daemon=True)
+        self._thread.start()
+
+    def stop(self) -> None:
+        if not self._running:
+            return
+        self._running = False
+        if supports_ansi():
+            print("\r\x1b[2K", end="", flush=True)

+ 75 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/helpers.py

@@ -0,0 +1,75 @@
+"""辅助工具函数"""
+
+import importlib
+from datetime import datetime
+from typing import Any, Dict, Optional
+from pathlib import Path
+
+def format_time(timestamp: Optional[datetime] = None, format_str: str = "%Y-%m-%d %H:%M:%S") -> str:
+    """
+    格式化时间
+    
+    Args:
+        timestamp: 时间戳,默认为当前时间
+        format_str: 格式字符串
+        
+    Returns:
+        格式化后的时间字符串
+    """
+    if timestamp is None:
+        timestamp = datetime.now()
+    return timestamp.strftime(format_str)
+
+def validate_config(config: Dict[str, Any], required_keys: list) -> bool:
+    """
+    验证配置是否包含必需的键
+    
+    Args:
+        config: 配置字典
+        required_keys: 必需的键列表
+        
+    Returns:
+        是否验证通过
+    """
+    missing_keys = [key for key in required_keys if key not in config]
+    if missing_keys:
+        raise ValueError(f"配置缺少必需的键: {missing_keys}")
+    return True
+
+def safe_import(module_name: str, class_name: Optional[str] = None) -> Any:
+    """
+    安全导入模块或类
+    
+    Args:
+        module_name: 模块名
+        class_name: 类名(可选)
+        
+    Returns:
+        导入的模块或类
+    """
+    try:
+        module = importlib.import_module(module_name)
+        if class_name:
+            return getattr(module, class_name)
+        return module
+    except (ImportError, AttributeError) as e:
+        raise ImportError(f"无法导入 {module_name}.{class_name or ''}: {e}")
+
+def ensure_dir(path: Path) -> Path:
+    """确保目录存在"""
+    path.mkdir(parents=True, exist_ok=True)
+    return path
+
+def get_project_root() -> Path:
+    """获取项目根目录"""
+    return Path(__file__).parent.parent.parent
+
+def merge_dicts(dict1: Dict, dict2: Dict) -> Dict:
+    """深度合并两个字典"""
+    result = dict1.copy()
+    for key, value in dict2.items():
+        if key in result and isinstance(result[key], dict) and isinstance(value, dict):
+            result[key] = merge_dicts(result[key], value)
+        else:
+            result[key] = value
+    return result

+ 39 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/logging.py

@@ -0,0 +1,39 @@
+"""日志工具"""
+
+import logging
+import sys
+from typing import Optional
+
+def setup_logger(
+    name: str = "hello_agents",
+    level: str = "INFO",
+    format_string: Optional[str] = None
+) -> logging.Logger:
+    """
+    设置日志记录器
+    
+    Args:
+        name: 日志记录器名称
+        level: 日志级别
+        format_string: 日志格式
+        
+    Returns:
+        配置好的日志记录器
+    """
+    logger = logging.getLogger(name)
+    logger.setLevel(getattr(logging, level.upper()))
+    
+    if not logger.handlers:
+        handler = logging.StreamHandler(sys.stdout)
+        formatter = logging.Formatter(
+            format_string or 
+            '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
+        )
+        handler.setFormatter(formatter)
+        logger.addHandler(handler)
+    
+    return logger
+
+def get_logger(name: str = "hello_agents") -> logging.Logger:
+    """获取日志记录器"""
+    return logging.getLogger(name)

+ 61 - 0
Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/serialization.py

@@ -0,0 +1,61 @@
+"""序列化工具"""
+
+import json
+import pickle
+from typing import Any, Union
+from pathlib import Path
+
+def serialize_object(obj: Any, format: str = "json") -> Union[str, bytes]:
+    """
+    序列化对象
+    
+    Args:
+        obj: 要序列化的对象
+        format: 序列化格式 ("json" 或 "pickle")
+        
+    Returns:
+        序列化后的数据
+    """
+    if format == "json":
+        return json.dumps(obj, ensure_ascii=False, indent=2)
+    elif format == "pickle":
+        return pickle.dumps(obj)
+    else:
+        raise ValueError(f"不支持的序列化格式: {format}")
+
+def deserialize_object(data: Union[str, bytes], format: str = "json") -> Any:
+    """
+    反序列化对象
+    
+    Args:
+        data: 序列化的数据
+        format: 序列化格式
+        
+    Returns:
+        反序列化后的对象
+    """
+    if format == "json":
+        return json.loads(data)
+    elif format == "pickle":
+        return pickle.loads(data)
+    else:
+        raise ValueError(f"不支持的反序列化格式: {format}")
+
+def save_to_file(obj: Any, filepath: Union[str, Path], format: str = "json") -> None:
+    """保存对象到文件"""
+    filepath = Path(filepath)
+    data = serialize_object(obj, format)
+    
+    mode = "w" if format == "json" else "wb"
+    with open(filepath, mode) as f:
+        f.write(data)
+
+def load_from_file(filepath: Union[str, Path], format: str = "json") -> Any:
+    """从文件加载对象"""
+    filepath = Path(filepath)
+    mode = "r" if format == "json" else "rb"
+    
+    with open(filepath, mode) as f:
+        data = f.read()
+    
+    return deserialize_object(data, format)